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

Ensure error messages don't leak private key #11523

Conversation

Ericson2314
Copy link
Member

Motivation

Since #8766, invalid base64 is rendered in errors, but we don't actually want to show this in the case of an invalid private keys.

Context

#8766

Priorities and Process

Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

Comment on lines 177 to 185
std::string base64Encode(std::string_view s);
std::string base64Decode(std::string_view s);

/**
* Decode arbitrary bytes to Base64.
*
* @param hideValue Avoid displaying the raw Base64 in error messages,
* e.g. to avoid leaking private keys.
*/
std::string base64Decode(std::string_view s, bool hideValue = false);
Copy link
Member Author

@Ericson2314 Ericson2314 Sep 18, 2024

Choose a reason for hiding this comment

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

@cole-h e.g. these type signatures (strings on both sides) are also not instructive

@Mic92 Mic92 added backport 2.18-maintenance Automatically creates a PR against the branch backport 2.19-maintenance Automatically creates a PR against the branch backport 2.20-maintenance Automatically creates a PR against the branch backport 2.21-maintenance Automatically creates a PR against the branch backport 2.22-maintenance Automatically creates a PR against the branch backport 2.23-maintenance Automatically creates a PR against the branch backport 2.24-maintenance Automatically creates a PR against the branch labels Sep 18, 2024
src/libutil/util.cc Outdated Show resolved Hide resolved
src/libutil/util.hh Outdated Show resolved Hide resolved
src/libutil/util.hh Outdated Show resolved Hide resolved
@Ericson2314 Ericson2314 force-pushed the base64Decode-no-leak-private-key-on-error branch from 18f855c to d8b3626 Compare September 18, 2024 16:09
Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

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

Still not safe enough, and please add a functional test - see comment.

tests/unit/libutil/util.cc Outdated Show resolved Hide resolved
src/libutil/util.hh Outdated Show resolved Hide resolved
@roberth
Copy link
Member

roberth commented Sep 19, 2024

Add release blocker label because this fixes a minor security regression that's not been released yet.

@edolstra
Copy link
Member

edolstra commented Sep 20, 2024

To be honest, I feel that this is too much work to get slightly more useful error messages in some corner cases. (This makes #8766, a one-line patch, explode into 241 lines...) So I would be somewhat in favor of reverting #8766 than adding this much complexity.

It might be better to improve those error messages by adding appropriate error contexts (e.g. "while parsing the extra-substituters of flake.nix: ...").

@Mic92
Copy link
Member

Mic92 commented Sep 20, 2024

Most of the changes here already introduce error traces, so we would only need to remove the sensitive flag.

@edolstra edolstra removed backport 2.18-maintenance Automatically creates a PR against the branch backport 2.19-maintenance Automatically creates a PR against the branch backport 2.20-maintenance Automatically creates a PR against the branch backport 2.21-maintenance Automatically creates a PR against the branch backport 2.22-maintenance Automatically creates a PR against the branch backport 2.23-maintenance Automatically creates a PR against the branch labels Sep 20, 2024
@Ericson2314
Copy link
Member Author

@edolstra Well something along these lines would be very useful to debug a hydra failure were facing.

I do agree this extra parameter is ugly, so I think I will do it all with error traces instead.

@Ericson2314 Ericson2314 force-pushed the base64Decode-no-leak-private-key-on-error branch from d8b3626 to 730afba Compare September 20, 2024 14:39
@github-actions github-actions bot added the fetching Networking with the outside (non-Nix) world, input locking label Sep 20, 2024
… is detected"

We have a safer way of doing this.

This reverts commit dc3ccf0.
@Ericson2314 Ericson2314 force-pushed the base64Decode-no-leak-private-key-on-error branch 2 times, most recently from ecf3513 to 0cd6442 Compare September 20, 2024 14:44
@Ericson2314
Copy link
Member Author

OK, I reverted the other PR and then added a bunch of traces where it is safe to do so.

try {
keyDecoded = base64Decode(k.key);
} catch (Error & e) {
e.addTrace({}, "While decoding public key '%s' used for git signature", k.key);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
e.addTrace({}, "While decoding public key '%s' used for git signature", k.key);
e.addTrace({}, "while decoding public key '%s' used for git signature", k.key);

for consistency with other trace messages.

try {
sig2 = base64Decode(sig);
} catch (Error & e) {
e.addTrace({}, "While decoding signature '%s'", sig);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
e.addTrace({}, "While decoding signature '%s'", sig);
e.addTrace({}, "while decoding signature '%s'", sig);

Comment on lines +31 to +32
if (!sensitiveValue)
extra = fmt(" with raw value '%s'", key);
Copy link
Member

Choose a reason for hiding this comment

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

Do we still need sensitiveValue? I think the context gives enough info.

Copy link
Member Author

Choose a reason for hiding this comment

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

Eh it's pretty self-contained here like the other version (The Key constructor is now protected so it won't get used elsewhere).

I am inclined to keep it also because the thing I need to debug is a public key not private key.

Since NixOS#8766, invalid base64 is rendered in errors, but we don't actually
want to show this in the case of an invalid private keys.

Co-Authored-By: Eelco Dolstra <[email protected]>
@Ericson2314 Ericson2314 force-pushed the base64Decode-no-leak-private-key-on-error branch from 0cd6442 to 2b6b03d Compare September 23, 2024 20:38
@Ericson2314 Ericson2314 dismissed roberth’s stale review September 23, 2024 20:39

I hope with the new code being much more local, we don't need the test as badly. Also many things to do / me short on time.

@Ericson2314 Ericson2314 merged commit 322d2c7 into NixOS:master Sep 23, 2024
10 checks passed
@Ericson2314 Ericson2314 deleted the base64Decode-no-leak-private-key-on-error branch September 23, 2024 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.24-maintenance Automatically creates a PR against the branch fetching Networking with the outside (non-Nix) world, input locking release-blocker
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants