Skip to contentSkip to conversation

crypto: af_alg - Support symmetric encryption via keyring keys

RFC PATCH In review Frederick Lawler 3 participants 4 messages Oct 4, 2022 – Oct 12, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 crypto: af_alg - Support symmetric encryption via keyring keys

We want to leverage keyring to store sensitive keys, and then use those keys for symmetric encryption via the crypto API. Among the key types we wish to support are: user, logon, encrypted, and trusted.

User key types are already able to have their data copied to user space, but logon does not support this. Further, trusted and encrypted keys will return their encrypted data back to user space on read, which make them not ideal for symmetric encryption.

To support symmetric encryption for these key types, add a new ALG_SET_KEY_BY_KEY_SERIAL setsockopt() option to the crypto API. This allows users to pass a key_serial_t to the crypto API to perform symmetric encryption. The behavior is the same as ALG_SET_KEY, but the crypto key data is copied in kernel space from a keyring key, which allows for the support of logon, encrypted, and trusted key types.

Keyring keys must have the KEY_(POS|USR|GRP|OTH)_SEARCH permission set to leverage this feature. This follows the asymmetric_key type where key lookup calls eventually lead to keyring_search_rcu() without the KEYRING_SEARCH_NO_CHECK_PERM flag set.

---
 Documentation/crypto/userspace-if.rst |  15 ++-
 crypto/af_alg.c                       | 142 +++++++++++++++++++++++++-
 include/uapi/linux/if_alg.h           |   1 +
 3 files changed, 154 insertions(+), 4 deletions(-)
Documentation/crypto/userspace-if.rst +12 -3
index b45dabbf69d6..f80f243e227e 100644
@@ -131,9 +131,9 @@ from the kernel crypto API. If the buffer is too small for the message
 digest, the flag MSG_TRUNC is set by the kernel.
 
 In order to set a message digest key, the calling application must use
-the setsockopt() option of ALG_SET_KEY. If the key is not set the HMAC
-operation is performed without the initial HMAC state change caused by
-the key.
+the setsockopt() option of ALG_SET_KEY or ALG_SET_KEY_BY_KEY_SERIAL. If the
+key is not set the HMAC operation is performed without the initial HMAC state
+change caused by the key.
 
 Symmetric Cipher API
 --------------------
@@ -382,6 +382,15 @@ mentioned optname:
 
    -  the RNG cipher type to provide the seed
 
+- ALG_SET_KEY_BY_KEY_SERIAL -- Setting the key via keyring key_serial_t.
+   This operation behaves the same as ALG_SET_KEY. The decrypted
+   data is copied from a keyring key, and uses that data as the
+   key for symmetric encryption.
+
+   The passed in key_serial_t must have the KEY_(POS|USR|GRP|OTH)_SEARCH
+   permission set, otherwise -EPERM is returned. Supports key types: user,
+   logon, encrypted, and trusted.
+
 -  ALG_SET_AEAD_AUTHSIZE -- Setting the authentication tag size for
    AEAD ciphers. For a encryption operation, the authentication tag of
    the given size will be generated. For a decryption operation, the
crypto/af_alg.c +141 -1

171 lines not on this page · raw message

include/uapi/linux/if_alg.h +1 -0
index 578b18aab821..0824fbc026a1 100644
@@ -52,6 +52,7 @@ struct af_alg_iv {
 #define ALG_SET_AEAD_ASSOCLEN		4
 #define ALG_SET_AEAD_AUTHSIZE		5
 #define ALG_SET_DRBG_ENTROPY		6
+#define ALG_SET_KEY_BY_KEY_SERIAL	7
 
 /* Operations */
 #define ALG_OP_DECRYPT			0
Signature · 1 line
2.30.2
#1 lore raw

Hi Frederick,

Quoted 16 lines · Frederick Lawler

We want to leverage keyring to store sensitive keys, and then use those keys for symmetric encryption via the crypto API. Among the key types we wish to support are: user, logon, encrypted, and trusted.

User key types are already able to have their data copied to user space, but logon does not support this. Further, trusted and encrypted keys will return their encrypted data back to user space on read, which make them not ideal for symmetric encryption.

To support symmetric encryption for these key types, add a new ALG_SET_KEY_BY_KEY_SERIAL setsockopt() option to the crypto API. This allows users to pass a key_serial_t to the crypto API to perform symmetric encryption. The behavior is the same as ALG_SET_KEY, but the crypto key data is copied in kernel space from a keyring key, which allows for the support of logon, encrypted, and trusted key types.

Quoted 6 lines

Keyring keys must have the KEY_(POS|USR|GRP|OTH)_SEARCH permission set to leverage this feature. This follows the asymmetric_key type where key lookup calls eventually lead to keyring_search_rcu() without the KEYRING_SEARCH_NO_CHECK_PERM flag set.

There was a similar patch several years ago by Ondrej Mosnacek:
https://lore.kernel.org/linux-crypto/[email protected]/T/#u

Have you addressed all the feedback that was raised on that one?

Two random nits below:

crypto/af_alg.c static int alg_setkey(struct sock *sk, sockptr_t ukey, unsigned int keylen) · line 246

Quoted 6 lines
crypto/af_alg.c +6 -0
+	*dest_len = key->datalen;
+	*dest = kmalloc(*dest_len, GFP_KERNEL);
+	if (!*dest)
+		return -ENOMEM;
+
+	memcpy(*dest, ukp->data, *dest_len);

This should use kmemdup().

crypto/af_alg.c static int alg_setkey(struct sock *sk, sockptr_t ukey, unsigned int keylen) · line 327

Quoted 6 lines
crypto/af_alg.c +6 -0
+	} else if (IS_ENABLED(CONFIG_ENCRYPTED_KEYS) &&
+			   !strcmp(key->type->name, "encrypted")) {
+		read_key = &read_key_type_encrypted;
+	} else if (IS_ENABLED(CONFIG_TRUSTED_KEYS) &&
+			   !strcmp(key->type->name, "trusted")) {
+		read_key = &read_key_type_trusted;

These need to use IS_REACHABLE(), not IS_ENABLED().

- Eric

#2 lore raw

Hi Eric,

Quoted 24 lines · Eric Biggers

Hi Frederick,

Quoted 21 lines · Frederick Lawler

We want to leverage keyring to store sensitive keys, and then use those keys for symmetric encryption via the crypto API. Among the key types we wish to support are: user, logon, encrypted, and trusted.

User key types are already able to have their data copied to user space, but logon does not support this. Further, trusted and encrypted keys will return their encrypted data back to user space on read, which make them not ideal for symmetric encryption.

To support symmetric encryption for these key types, add a new ALG_SET_KEY_BY_KEY_SERIAL setsockopt() option to the crypto API. This allows users to pass a key_serial_t to the crypto API to perform symmetric encryption. The behavior is the same as ALG_SET_KEY, but the crypto key data is copied in kernel space from a keyring key, which allows for the support of logon, encrypted, and trusted key types.

Keyring keys must have the KEY_(POS|USR|GRP|OTH)_SEARCH permission set to leverage this feature. This follows the asymmetric_key type where key lookup calls eventually lead to keyring_search_rcu() without the KEYRING_SEARCH_NO_CHECK_PERM flag set.

Quoted 6 lines
Quoted 1 line

There was a similar patch several years ago by Ondrej Mosnacek:
https://lore.kernel.org/linux-crypto/[email protected]/T/#u

Have you addressed all the feedback that was raised on that one?

Thanks for sharing that.

I believe I've addressed most of the feedback. Starting with we agree preferring key_serial_t. I changed to to use IS_REACHABLE(), and set ALG_SET_KEY_BY_KEY_SERIAL to 10 leaving a comment about libkcapi reserving values 7-9.

I've made other additional changes since the RFC, so we should consider this code outdated. I'll submit a v1 that is a bit cleaner after the merge window.

Your comment about broken crypto algorithms exposing sensitive data is interesting. We've had similar thoughts about adding additional permission, but ultimately decided to stick to the pattern asymmetric key types use.

lookup_user_key() ultimately makes a call into a security hook security_key_permission() given a key_ref_t, so users can further restrict access based on keys that way if enabled. We've also had similar discussions regarding X.509 certificates, and I'm not opposed to Ondrej's suggestion of disabling this feature by default with Kconfig. I'll look into this a bit more, and we're open to suggestions here.

Quoted 21 lines

Two random nits below:

crypto/af_alg.c static int alg_setkey(struct sock *sk, sockptr_t ukey, unsigned int keylen) · line 246

Quoted 6 lines
crypto/af_alg.c +6 -0
+	*dest_len = key->datalen;
+	*dest = kmalloc(*dest_len, GFP_KERNEL);
+	if (!*dest)
+		return -ENOMEM;
+
+	memcpy(*dest, ukp->data, *dest_len);

This should use kmemdup(). >

crypto/af_alg.c static int alg_setkey(struct sock *sk, sockptr_t ukey, unsigned int keylen) · line 327

Quoted 6 lines
crypto/af_alg.c +6 -0
+	} else if (IS_ENABLED(CONFIG_ENCRYPTED_KEYS) &&
+			   !strcmp(key->type->name, "encrypted")) {
+		read_key = &read_key_type_encrypted;
+	} else if (IS_ENABLED(CONFIG_TRUSTED_KEYS) &&
+			   !strcmp(key->type->name, "trusted")) {
+		read_key = &read_key_type_trusted;

These need to use IS_REACHABLE(), not IS_ENABLED().

- Eric

#3 lore raw

Am Mittwoch, 12. Oktober 2022, 16:49:56 CEST schrieb Frederick Lawler:

Hi Frederick,

Quoted 4 lines

I believe I've addressed most of the feedback. Starting with we agree preferring key_serial_t. I changed to to use IS_REACHABLE(), and set ALG_SET_KEY_BY_KEY_SERIAL to 10 leaving a comment about libkcapi reserving values 7-9.

This reservation should not be observed. I provided patches for adding AF_ALG interfaces for KPP and AKCIPHER some time ago which were rejected. Libkcapi still contains the interface implementations but are not compiled by default.

As the patches are rejected, their values should not be considered as relevant. Thus, I think your patch should not keep holes in the numbers.

Ciao
Stephan