-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
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
treewide: Replace uses of replace-literal
with replace-secret
to avoid leaking secrets
#121708
Conversation
'/proc/<pid>/cmdline', unlike when 'sed' or 'replace' is used. | ||
|
||
""" | ||
with open(secret_filename) as sf, open(filename, 'r+') as f: |
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.
Should this set a umask or something?
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.
It does the replacement in-place, so the file's permissions are retained. I guess it could force the permissions to something restrictive, but this feels like a surprising behavior..
I can confirm that both mpd and mpdscribble launch fine with this change merged to |
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.
Diff LGTM.
LGTM |
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.
All commits touching replace-secret.py
should be squashed.
@@ -713,11 +710,12 @@ in | |||
cfg.siteSettings | |||
"/run/discourse/config/nixos_site_settings.json" | |||
} | |||
install -T -m 0400 -o discourse ${discourseConf} /run/discourse/config/discourse.conf | |||
install -T -m 0600 -o discourse ${discourseConf} /run/discourse/config/discourse.conf |
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.
Is this change related?
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, this is required for it to be able to write to the file. replace-literal
ignores permissions (or maybe the -f
flag also has that effect), but replace-secret
doesn't.
${mkSecretReplacement cfg.database.passwordFile} | ||
${mkSecretReplacement cfg.mail.outgoing.passwordFile} | ||
${mkSecretReplacement cfg.redis.passwordFile} | ||
${mkSecretReplacement cfg.secretKeyBaseFile} | ||
chmod 0400 /run/discourse/config/discourse.conf |
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 change too perhaps is worth merging, but should at least come in a separate commit.
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 makes sure the file has the same permissions as before the commit, but sets it after inserting the secrets.
@@ -0,0 +1,22 @@ | |||
{ stdenv, lib, python3 }: |
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.
Perhaps add a check phase here that does some trivial usage of the command so we can be sure it still works without having to run nixos tests.
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.
Yeah, that's a good idea. I added two simple checks. If the diff looks okay to you, I'll squash it.
Add a small utility script which securely replaces secrets in files. Doing this with `sed`, `replace-literal` or similar utilities leaks the secrets through the spawned process' `/proc/<pid>/cmdline` file.
Using `replace-literal` to insert secrets leaks the secrets through the `replace-literal` process' `/proc/<pid>/cmdline` file. `replace-secret` solves this by reading the secret straight from the file instead, which also simplifies the code a bit.
Using `replace-literal` to insert secrets leaks the secrets through the `replace-literal` process' `/proc/<pid>/cmdline` file. `replace-secret` solves this by reading the secret straight from the file instead, which also simplifies the code a bit.
Using `replace-literal` to insert secrets leaks the secrets through the `replace-literal` process' `/proc/<pid>/cmdline` file. `replace-secret` solves this by reading the secret straight from the file instead, which also simplifies the code a bit.
Using `replace-literal` to insert secrets leaks the secrets through the `replace-literal` process' `/proc/<pid>/cmdline` file. `replace-secret` solves this by reading the secret straight from the file instead.
Using `replace-literal` to insert secrets leaks the secrets through the `replace-literal` process' `/proc/<pid>/cmdline` file. `replace-secret` solves this by reading the secret straight from the file instead, which also simplifies the code a bit.
Motivation for this change
Using
replace-literal
(or similar) to insert secrets leaks the secrets through thereplace-literal
process'/proc/<pid>/cmdline
file. This introduces a tinyreplace-secret
utility which solves this by reading the secret straight from the file instead, which also usually simplifies the code a bit, since we're always reading the secret from a file anyway.I've focused on replacing uses of
replace-literal
here, since they translate very easily, butsed
usage and other similar ones should also be replaced in the future.The
mpd
andmpdscribble
changes have not been tested with real credentials, so help from someone who uses them would be appreciated.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)