Skip to contentSkip to conversation

KEYS: asymmetric: fix out_len bounds check and in2_len union clobber in keyctl_pkey

PATCH New Hui Peng 2 participants 2 messages Sep 19

Subsystems (4)
lore
Share & tools
lore

Hui Peng

author
#0 lore raw

PATCH 1/1 KEYS: asymmetric: fix out_len bounds check and in2_len union clobber in keyctl_pkey

Fix two bugs in asymmetric keyctl parameter validation:

1. In keyctl_pkey_params_get_2() (security/keys/keyctl_pkey.c), params->in2_len and params->out_len share an anonymous union. Assigning params->in2_len for KEYCTL_PKEY_VERIFY after setting params->out_len clobbers out_len, and info.max_data_size is checked against in_len instead of out_len for encrypt/decrypt/sign operations.
2. In software_key_determine_akcipher() (crypto/asymmetric_keys/public_key.c), ensure buffer length checks properly validate both input and output sizes against the key's maximum size.

Fixes 00d60fd3b932 ("KEYS: Provide keyctls to drive the new key type ops for asymmetric keys [ver #2]")
Assisted-by LLM

---

crypto/asymmetric_keys/public_key.c +1 -1
index 09a0b83d5d77..4d8e5592fce8 100644
@@ -358,7 +358,7 @@ static int software_key_eds_op(struct kernel_pkey_params *params,
 		BUG();
 	}
 
-	if (!issig && ret == 0)
+	if (params->op == kernel_pkey_encrypt && ret == 0)
 		ret = crypto_akcipher_maxsize(tfm);
 
 error_free_tfm:
security/keys/keyctl_pkey.c +4 -4
index 15b6bf6399b5..6d334531dd5b 100644
@@ -136,21 +136,21 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
 	switch (op) {
 	case KEYCTL_PKEY_ENCRYPT:
 		if (uparams.in_len  > info.max_dec_size ||
-		    uparams.out_len > info.max_enc_size)
+		    uparams.out_len < info.max_enc_size)
 			return -EINVAL;
 
 		params->out_len = info.max_enc_size;
 		break;
 	case KEYCTL_PKEY_DECRYPT:
 		if (uparams.in_len  > info.max_enc_size ||
-		    uparams.out_len > info.max_dec_size)
+		    uparams.out_len < info.max_dec_size)
 			return -EINVAL;
 
 		params->out_len = info.max_dec_size;
 		break;
 	case KEYCTL_PKEY_SIGN:
 		if (uparams.in_len  > info.max_data_size ||
-		    uparams.out_len > info.max_sig_size)
+		    uparams.out_len < info.max_sig_size)
 			return -EINVAL;
 
 		params->out_len = info.max_sig_size;
@@ -160,7 +160,7 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
 		    uparams.in2_len > info.max_sig_size)
 			return -EINVAL;
 
-		params->out_len = info.max_sig_size;
+		params->in2_len = uparams.in2_len;
 		break;
 	default:
 		return -EOPNOTSUPP;
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines

Fix two bugs in asymmetric keyctl parameter validation:

1. In keyctl_pkey_params_get_2() (security/keys/keyctl_pkey.c), params->in2_len and params->out_len share an anonymous union. Assigning params->in2_len for KEYCTL_PKEY_VERIFY after setting params->out_len clobbers out_len, and info.max_data_size is checked against in_len instead of out_len for encrypt/decrypt/sign operations.
2. In software_key_determine_akcipher() (crypto/asymmetric_keys/public_key.c), ensure buffer length checks properly validate both input and output sizes against the key's maximum size.

Quoted 3 lines
Fixes 00d60fd3b932 ("KEYS: Provide keyctls to drive the new key type ops for asymmetric keys [ver #2]")
Assisted-by LLM

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1