Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a type for options that contain the path to a secret file #174978

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

symphorien
Copy link
Member

Description of changes

This should avoid mistakes like passwordFile = /etc/secret; where that path is unquoted so nix copies it to the nix store and makes it public.
I converted three modules to illustrate, but if this is merged we could add many others. It will be a breaking change but for security's sake.

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 22.05 Release Notes (or backporting 21.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@infinisil
Copy link
Member

Check out #78640 as well

@symphorien
Copy link
Member Author

Ahah it's embarrassing: I linked to this PR about two years ago, but then forgot about it and reimplemented it yesterday...

Anyway, what blocks it? I see a rfc mentioned but it's closed.

lib/types.nix Outdated
Comment on lines 416 to 417
type = "derivation";
name = "secretFileMadeWorldReadable";
outPath = "${path}";
outputs = [ "out" ];
out = res;
outputName = "out";
canBePublic = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about something simpler:

Suggested change
type = "derivation";
name = "secretFileMadeWorldReadable";
outPath = "${path}";
outputs = [ "out" ];
out = res;
outputName = "out";
canBePublic = true;
inherit path;
__toString = r: r.path;
canBePublic = true;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially tried toString but then "${config.the.option}" does not work anymore, you explicitly need to use toString. This way makes the migration fully transparent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

odd, it seems to work fine here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed it works, I must have tested wrong.

# deliberately refuse paths, because toString x and "${x}" behave
# differently. Some modules might handle paths correctly, others will
# not, and anyway users can either quote the path or use makeWorldReadable.
check = x: ((isCoercibleToString x && (x.canBePublic or false)) || (isString x && !(hasPrefix "/nix/store" x))) && (hasPrefix "/" "${x}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing that you can guarantee is that you can assert that the secret has already been leaked, but not prevent it from leaking.

My understanding of why this is not possible without a special builtin which does not evaluate its operand, or which provide another evaluation loop which is guaranteed to not push any secret is the following:
isString x Return true if x evaluates to a string, and false otherwise.

The evaluation of a path, is similar to a derivation, and as far as I recall submit derivation directly to the Nix daemon.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not quite: path copying is really lazy. paths shouldn't be copied to the store unless they are interpolated into strings, constructed through builtins.path, or copyingly coerced by plugins. (can be checked by taking a path to a large file and munging it a bit. only when it is coerced to string, eg by interpolation or taking substrings, does the copy happen)

this check looks safe as long as we can guarantee that no interpolations are evaluated before the check is, but that too might be hard to do with perfect accuracy

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh indeed the copy happens even when only evaluating builtins like builtins.hasContext... I was very surprised.

$ uuidgen > /tmp/secret
$  nix repl "<nixpkgs>"
Welcome to Nix 2.8.1. Type :? for help.

Loading '<nixpkgs>'...
Added 16461 variables.

nix-repl> a = runCommand "foo" {} "cat ${./secret} sdf" 

nix-repl> builtins.hasContext "${a}" 
true

$  rg $(cat /tmp/secret ) /nix/store --glob "*secret"
/nix/store/8gnhfxbv1zzar3pwxlmsxx8lr7876cp4-secret
1:97c31780-96ec-41b1-b8bf-a0bbbe113cb5

The check has the following property: a configuration that evaluates does not leak secrets. (well it's not true because you can trick it with "/${./secret}" but you get my point). Maybe we could tweak the error message to say that if you see the error message the secret already leaked?

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/low-effort-and-high-impact-tasks/21095/15

@wegank wegank added 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 2.status: merge conflict labels Mar 19, 2024
@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants