Repository navigation
Conversation
`KeyFile::kdf_key` passed `N`, `r` and `p` from the key file straight to scrypt. Key files are read from the backend and every key is tried before the password is verified, so a corrupt or crafted key file in `keys/` could make opening the repository - panic for `N = 0` (`assert!(x > 0)` in `log_2`), - abort on allocation failure for a huge `N * r` (e.g. `N = 2^31`, `r = 8` tries to allocate 2 TiB), or - run for hours for a huge `p` (e.g. `N = 2^15`, `r = 8`, `p = 2^20`). Reject such parameters with an error before running scrypt: `N` must not be zero, the memory scrypt allocates (`128 * r * (N + p)` bytes) must not exceed 1 GiB and the work (`128 * r * N * p`) must not exceed 2^32. Keys created by rustic (`N = 2^17`, `r = 8`, `p = 1`) use 128 MiB and keys created by restic at most 60 MiB; both need a work of about 2^27, so the limits leave a wide margin for existing keys. Add tests opening a repository with key files using these parameters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
KeyFile::kdf_keypasses the scrypt parametersN,randpfrom the key file to scrypt without checking them. Key files are read from the backend, andfind_key_in_backendtries every key inkeys/before the password is verified. A corrupt or crafted key file therefore decides how much memory and time opening the repository takes:mainN = 0assertion failed: x > 0inlog_2(keyfile.rs:288)N = 2^31,r = 8,p = 1memory allocation of 2199023255552 bytes failed, the process abortsN = 2^15,r = 8,p = 2^20p)This PR checks the parameters in
kdf_keybefore running scrypt. It returns anErrorKind::Keyerror ifNis zero, if the memory scrypt allocates (128 * r * (N + p)bytes) exceeds 1 GiB, or if the work (128 * r * N * p) exceeds 2^32.The limits are far above what rustic and restic create. Timings are from a release build on an AMD Zen 4 machine:
N = 2^17,r = 8,p = 1): 128 MiB, work 2^27, about 0.26 skey1fixture hasN = 2^15,r = 8,p = 6): 32 MiB, work 2^27.6, about 0.29 sr = 0andp = 0are still rejected byscrypt::Params::new, as before.Tests
tests/keys.rsgetstest_keys_with_excessive_scrypt_params_failwith the three cases above. Each case starts from thekey1fixture and changes onlyN,randp. Onmainthe three cases panic, abort and time out. With this change they return the new error immediately.test_working_keys_passesstill opens the existing restic and rustic keys.cargo fmt --checkis clean, and clippy reports nothing for the touched files.cargo test --workspace --all-targets --all-featurespasses, apart from the five snapshot tests intests/integration(backup,chunker,rewrite). Those fail the same way on an unmodifiedmain.A key file rejected this way still stops
find_key_in_backendwith an error, as it already does for parameters thatscrypt::Params::newrejects. Skipping such keys and trying the remaining ones would change behaviour, so I left it out.Disclaimer: Found the issue by test-driving an LM-powered vulnerability scanner I work on heavily.