-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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
wireguard module: generatePrivateKeyFile: Fix chmod security race #121294
wireguard module: generatePrivateKeyFile: Fix chmod security race #121294
Conversation
NixOS#121288 Until now, the `touch + chmod 600 + write` approach made it possible for an unprivileged local user read the private key file, by opening the file after the touch, before the read permissions are restricted. This was only the case if `generatePrivateKeyFile = true` and the parent directory of `privateKeyFile` already existed and was readable. This commit fixes it by using `umask`, which ensures kernel-side that the `touch` creates the file with the correct permissions atomically. This commit also: * Removes `mkdir --mode 0644 -p "${dirOf values.privateKeyFile}"` because setting permissions `drw-r--r--` ("nobody can enter that dir") is awkward. `drwx------` would perhaps make sense, like for `.ssh`. However, setting the permissions on the private key file is enough, and likely better, because `privateKeyFile` is about that file specifically and no docs suggest that there's something special about its parent dir. * Removes the `chmod 0400 "${values.privateKeyFile}"` because there isn't really a point in removing write access from the owner of the private key.
8a545d0
to
0dc08b4
Compare
@@ -246,12 +246,15 @@ let | |||
}; | |||
|
|||
script = '' | |||
mkdir --mode 0644 -p "${dirOf values.privateKeyFile}" | |||
set -e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is automatic:
nixpkgs/nixos/modules/system/boot/systemd.nix
Line 213 in c709fd4
#!${pkgs.runtimeShell} -e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but who knows if it will stay that way forever; thus especially for the more sensitive stuff, it may be good to be explicit.
(Also evidently a ton of shell users don't know about set -e
, that it's off by default, and on-by-default in some NixOS scripts, so they may get surprises when copying scripts from one place to the other.)
I've added a release notes entry. |
Fixes #121288.
Until now, the
touch + chmod 600 + write
approach made it possible foran unprivileged local user read the private key file, by opening
the file after the touch, before the read permissions are restricted.
This was only the case if
generatePrivateKeyFile = true
and the parentdirectory of
privateKeyFile
already existed and was readable.This commit fixes it by using
umask
, which ensures kernel-side thatthe
touch
creates the file with the correct permissions atomically.This commit also:
mkdir --mode 0644 -p "${dirOf values.privateKeyFile}"
because setting permissions
drw-r--r--
("nobody can enter that dir")is awkward.
drwx------
would perhaps make sense, like for.ssh
.However, setting the permissions on the private key file is enough,
and likely better, because
privateKeyFile
is about that filespecifically and no docs suggest that there's something special
about its parent dir.
chmod 0400 "${values.privateKeyFile}"
because there isn't really a point in removing write access from
the owner of the private key.
Motivation for this change
Things done
sandbox
innix.conf
on non-NixOS linux)nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
./result/bin/
)nix path-info -S
before and after)