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

test: add basic encrypted key management tests #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dlrobertson
Copy link
Contributor

I added some basic tests for the Encrypted KeyType. Note that this change now requires CONFIG_ENCRYPTED_KEYS to be enabled for unit tests to pass.

Copy link
Contributor Author

@dlrobertson dlrobertson left a comment

Choose a reason for hiding this comment

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

:( It appears the runners do not support encrypted keys... 🤷‍♂️ maybe place the tests or all of encrypted keys behind a feature?

keyring.unlink_key(&enc_key).unwrap();

// This should not panic but currently does due to the use
// of ByteBuf when encoding the load payload.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a reason ByteBufs LowerHex is used for the Load payload type? AFAIK that is what is causing this test to fail.

Copy link
Owner

Choose a reason for hiding this comment

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

https://github.com/torvalds/linux/blob/master/Documentation/security/keys/trusted-encrypted.rst

All user level blobs, are displayed and loaded in hex ascii for convenience, and are integrity verified.

In code, it is read here and parsed here by this function which only supports lowercase ASCII hex.

Copy link
Contributor Author

@dlrobertson dlrobertson Jan 9, 2020

Choose a reason for hiding this comment

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

Sorry, I didn't do a good job of explaining the issue I hit.

https://github.com/torvalds/linux/blob/master/security/keys/encrypted-keys/encrypted.c#L162

* load [<format>] <master-key name> <decrypted data length>
*     <encrypted iv + data>

The key format, master key description, and the decrypted data length are parsed before the hex encoded data.

The current payload encoding for the load payload encodes everything but the key command as lower-case hex. This includes the format, master key, and decrypted data length, which AIUI should not be encoded as lower-case hex (why the should_panic test fails when it AFAIK should pass).

Copy link
Owner

Choose a reason for hiding this comment

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

Oh. Let's fix that in the Payload enum representation. I may have mixed up which docs/code I was looking at when making the payload type here.

Tests for the various keytypes was on my list after finishing #34, but the CI issues burned me out for a while on that :/ .

Copy link
Owner

Choose a reason for hiding this comment

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

I suppose a patch to fix the documentation for the load format should also be made:

keyctl add encrypted name "load hex_blob" ring

@mathstuf
Copy link
Owner

mathstuf commented Jan 9, 2020

:( It appears the runners do not support encrypted keys... man_shrugging maybe place the tests or all of encrypted keys behind a feature?

Yes, testing this stuff has been…painful on public CI infrastructure. They have this fetish with running as root inside containers which really messes with the permission checking done here. Quotas are also wacky and cause…interesting failures. See #34 for some fun.


#[test]
#[should_panic(
expected = "called `Result::unwrap()` on an `Err` value: Errno { code: 22, description: Some(\"Invalid argument\") }"
Copy link
Owner

Choose a reason for hiding this comment

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

See #34 for the preferred method for checking for expected errors. The main issue with this strategy is that it doesn't check which unwrap caused this problem.

@@ -129,3 +132,103 @@ fn add_key_to_session() {
assert_eq!(new_payload, new_expected);
keyring.unlink_key(&key).unwrap();
}

#[test]
fn add_encrypted_key_to_session() {
Copy link
Owner

Choose a reason for hiding this comment

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

I'd rather that keytype-specific tests go into src/keytypes/TYPE.rs than at the operation level.

keyring.unlink_key(&enc_key).unwrap();

// This should not panic but currently does due to the use
// of ByteBuf when encoding the load payload.
Copy link
Owner

Choose a reason for hiding this comment

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

https://github.com/torvalds/linux/blob/master/Documentation/security/keys/trusted-encrypted.rst

All user level blobs, are displayed and loaded in hex ascii for convenience, and are integrity verified.

In code, it is read here and parsed here by this function which only supports lowercase ASCII hex.

@mathstuf
Copy link
Owner

mathstuf commented Jan 9, 2020

Note that this change now requires CONFIG_ENCRYPTED_KEYS to be enabled for unit tests to pass.

#34 also has code to make tests into no-ops if a feature is not supported. I wish there were a way to say "this test is skipped" in Rust, but that is tied up in rust-lang/rust#50297.

@mathstuf
Copy link
Owner

I've just merged #34 (with #39 as a TODO item) and some holes for tests when run as root. Please move the tests to the encrypted.rs file and use some of the patterns there for capability checking.

@dlrobertson
Copy link
Contributor Author

Awesome! Will update soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants