Skip to contentSkip to conversation

crypto: caam - allocate enough space for blob descriptors

PATCH Superseded Keisuke Tsukuda 2 participants 2 messages Nov 24 – Nov 30

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: caam - allocate enough space for blob descriptors

From: Keisuke Tsukuda <[email protected]>

Commit a703a4c2a328 ("KEYS: trusted: caam based protected key") replaced the _intlen sequence-pointer helpers in caam_process_blob() with append_seq_in_ptr() and append_seq_out_ptr(). These helpers append an external length word for each u32 length, but CAAM_BLOB_DESC_BYTES_MAX accounts for neither word.

The trusted-key caller uses the 10-byte "SECURE_KEY" modifier. Its 12-byte padded length leaves four bytes unused in the macro's 16-byte modifier allowance. Thus, when the DMA and CAAM pointer widths match, the allocation is four bytes short. On an i.MX6 Quad, "new 32" builds a 48-byte descriptor in a 44-byte allocation. KASAN reports a four-byte slab-out-of-bounds write at append_operation().

On this 32-bit configuration, the protected-key path would build a 68-byte descriptor, beyond the observed kmalloc-64 slot. This path was not tested.

Allocate CAAM_DESC_BYTES_MAX, the existing 64-word descriptor limit, instead of maintaining an incomplete blob-specific size calculation. The same trusted-key operation completes without a KASAN report on the patched kernel.

Fixes a703a4c2a328 ("KEYS: trusted: caam based protected key")
---
 drivers/crypto/caam/blob_gen.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
drivers/crypto/caam/blob_gen.c +1 -13
index c18dbac56..0b19e5f7c 100644
@@ -20,18 +20,6 @@
 #include "jr.h"
 #include "regs.h"
 
-#define CAAM_BLOB_DESC_BYTES_MAX					\
-	/* Command to initialize & stating length of descriptor */	\
-	(CAAM_CMD_SZ +							\
-	/* Command to append the key-modifier + key-modifier data */	\
-	 CAAM_CMD_SZ + CAAM_BLOB_KEYMOD_LENGTH +			\
-	/* Command to include input key + pointer to the input key */	\
-	 CAAM_CMD_SZ + CAAM_PTR_SZ_MAX +				\
-	/* Command to include output key + pointer to the output key */	\
-	 CAAM_CMD_SZ + CAAM_PTR_SZ_MAX +				\
-	/* Command describing the operation to perform */		\
-	 CAAM_CMD_SZ)
-
 struct caam_blob_priv {
 	struct device jrdev;
 };
@@ -109,7 +97,7 @@ int caam_process_blob(struct caam_blob_priv *priv,
 		len = info->input_len;
 	}
 
-	desc = kzalloc(CAAM_BLOB_DESC_BYTES_MAX, GFP_KERNEL);
+	desc = kzalloc(CAAM_DESC_BYTES_MAX, GFP_KERNEL);
 	if (!desc)
 		return -ENOMEM;
 

base-commit: 04cd6899aaaeeab82afb97e58eb8619fde35b47d

Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Keisuke Tsukuda

Commit a703a4c2a328 ("KEYS: trusted: caam based protected key") replaced the _intlen sequence-pointer helpers in caam_process_blob() with append_seq_in_ptr() and append_seq_out_ptr(). These helpers append an external length word for each u32 length, but CAAM_BLOB_DESC_BYTES_MAX accounts for neither word.

[...]

Quoted 2 lines

Allocate CAAM_DESC_BYTES_MAX, the existing 64-word descriptor limit, instead of maintaining an incomplete blob-specific size calculation.

Hi Keisuke,

Thanks for the patch.

I went through caam_process_blob() and agree with the analysis. With P being the pointer size, the descriptor is 44 + 2P bytes for a normal blob and 56 + 4P bytes for the protected-key encap path. The old macro only reserved 36 + 2P bytes, so both paths overflow.

Using CAAM_DESC_BYTES_MAX is the better fix. It is the hardware limit for a job descriptor, so we do not have to keep a separate size calculation in sync with the descriptor code.

Note that Lee Jones posted a fix for the same issue a day earlier, as part of a larger series:

https://lore.kernel.org/linux-crypto/[email protected]/

That one updates CAAM_BLOB_DESC_BYTES_MAX instead. I would prefer your version, and only one of the two should be applied.

Since a703a4c2a328 is in v6.19, please add:

Thanks,
Sahil