fit: cipher: bounds checks on the ciphered image path
cover fit: cipher: bounds checks on the ciphered image path
Three fixes in the FIT cipher path, found while auditing for image- controlled values used as buffer lengths without validation. They were reported to the list earlier; these are the fixes.
Patch 1 is an out-of-bounds write. image_aes_decrypt() sizes its output buffer with cipher_len but decrypts DIV_ROUND_UP(cipher_len, block) blocks into it, so a 'data' length that is not block aligned writes past the end of the allocation.
Patches 2 and 3 are the two halves of the unciphered size never being
checked: the 'data-size-unciphered' property is read without asking for its length, and the value it yields is never related to the amount of data that was actually decrypted. The second matters because that property sits outside the per-image hash and signature, so it can be changed on a signed FIT without disturbing verification.
I have kept them as separate patches since they are independent defects with different consequences, but I am happy to squash 1 and 3 if you would rather have one change per function.
checkpatch-clean, builds for sandbox (CONFIG_FIT_CIPHER=y) with no new warnings at W=1, and no change in test/py results.
Pranav Rajendran (3):
lib: aes: reject a ciphertext length that is not a whole number of
blocks
image-fit: check the length of the data-size-unciphered property
lib: aes: reject an unciphered size larger than the ciphertext
boot/image-fit.c | 6 +++++- lib/aes/aes-decrypt.c | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH 1/3 lib: aes: reject a ciphertext length that is not a whole number of blocks
image_aes_decrypt() allocates cipher_len bytes for the plaintext but then asks aes_cbc_decrypt_blocks() to write DIV_ROUND_UP(cipher_len, AES_BLOCK_LENGTH) blocks into it. For a cipher_len that is not a multiple of AES_BLOCK_LENGTH the rounding up adds one block, so the last block is written up to AES_BLOCK_LENGTH - 1 bytes past the end of the allocation, and read the same distance past the end of the ciphertext.
cipher_len is the size of the image data in the FIT, so an image with a 'data' property whose length is not block aligned is enough to reach this. The overflowing bytes are decryption output, i.e. they depend on the key, but the length itself is not covered by anything that would stop the image from being parsed this far.
A CBC ciphertext is a whole number of blocks by construction, so treat anything else as a malformed image and reject it before allocating. With that established, compute the block count with a plain division so the buffer size and the write length cannot drift apart again.
--- lib/aes/aes-decrypt.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
lib/aes/aes-decrypt.c +11 -1
@@ -17,6 +17,16 @@ int image_aes_decrypt(struct image_cipher_info *info,unsigned char key_exp[AES256_EXPAND_KEY_LENGTH ];unsigned int aes_blocks, key_len = info->cipher->key_len; +/* +* The ciphertext is a whole number of AES blocks by construction, and +* the decryption below writes one full block at a time, so anything +* else would overflow the output buffer. +*/ +if (!cipher_len || cipher_len %AES_BLOCK_LENGTH ) { +printf ("Invalid ciphertext length\n" ); +return -EINVAL ; + } + *data =malloc (cipher_len);if (!*data) {printf ("Can't allocate memory to decrypt\n" );
@@ -30,7 +40,7 @@ int image_aes_decrypt(struct image_cipher_info *info,aes_expand_key ((u8 *)info->key, key_len, key_exp);/* Calculate the number of AES blocks to encrypt. */ - aes_blocks =DIV_ROUND_UP (cipher_len,AES_BLOCK_LENGTH ); + aes_blocks = cipher_len /AES_BLOCK_LENGTH ;aes_cbc_decrypt_blocks (key_len, key_exp, (u8 *)info->iv, (u8 *)cipher, *data, aes_blocks);
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH v1 1/3] lib: aes: reject a ciphertext length that is not a whole number of blocks
Quoted 21 lines · Pranav Rajendran
lib: aes: reject a ciphertext length that is not a whole number of blocks
image_aes_decrypt()allocatescipher_lenbytes for the plaintext but then asksaes_cbc_decrypt_blocks()to writeDIV_ROUND_UP(cipher_len, AES_BLOCK_LENGTH)blocks into it. For acipher_lenthat is not a multiple ofAES_BLOCK_LENGTHthe rounding up adds one block, so the last block is written up toAES_BLOCK_LENGTH- 1 bytes past the end of the allocation, and read the same distance past the end of the ciphertext.
cipher_lenis the size of the image data in the FIT, so an image with a 'data' property whose length is not block aligned is enough to reach this. The overflowing bytes are decryption output, i.e. they depend on the key, but the length itself is not covered by anything that would stop the image from being parsed this far.A CBC ciphertext is a whole number of blocks by construction, so treat anything else as a malformed image and reject it before allocating. With that established, compute the block count with a plain division so the buffer size and the write length cannot drift apart again.
Quoted 5 lines
Fixes 4df3578119b0 ("u-boot: fit: add support to decrypt fit with aes")Signed-off-by Pranav Rajendran <[email protected]>
lib/aes/aes-decrypt.c| 12 +++++++++++-1 file changed, 11 insertions(+), 1 deletion(-)
PATCH 2/3 image-fit: check the length of the data-size-unciphered property
In reply to Pranav Rajendranfit_image_get_data_size_unciphered() passes NULL as fdt_getprop()'s length argument, so it accepts a 'data-size-unciphered' property of any size and then dereferences the first four bytes of it. A property shorter than that is read past its end, and the bytes that follow it in the FIT are returned to the caller as the unciphered size.
Ask for the length and require it to be exactly one fdt32_t, as the binding describes.
--- boot/image-fit.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
boot/image-fit.c +5 -1
@@ -1040,11 +1040,15 @@ int fit_image_get_data_size_unciphered(const void *fit, int noffset, size_t *data_size) {const fdt32_t *val; +int len; - val =fdt_getprop (fit, noffset,"data-size-unciphered" ,NULL ); + val =fdt_getprop (fit, noffset,"data-size-unciphered" , &len);if (!val)return -ENOENT ; +if (len !=sizeof (*val)) +return -EINVAL ; + *data_size = (size_t )fdt32_to_cpu (*val);return 0 ;
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH v1 2/3] image-fit: check the length of the data-size-unciphered property
Hi Pranav,
Quoted 23 lines · Pranav Rajendran
image-fit: check the length of the data-size-unciphered property
fit_image_get_data_size_unciphered()passes NULL as fdt_getprop()'s length argument, so it accepts a 'data-size-unciphered' property of any size and then dereferences the first four bytes of it. A property shorter than that is read past its end, and the bytes that follow it in the FIT are returned to the caller as the unciphered size.Ask for the length and require it to be exactly one
fdt32_t, as the binding describes.Fixes 4df3578119b0 ("u-boot: fit: add support to decrypt fit with aes")Signed-off-by Pranav Rajendran <[email protected]>
boot/image-fit.c| 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)boot/image-fit.c +1
-0@@ -1040,11 +1040,15 @@ int fit_image_get_data_size_unciphered(const void *fit, int noffset, size_t *data_size) {const fdt32_t *val; +int len;
boot/image-fit.c int fit_image_get_data_size_unciphered(const void *fit, int noffset, · line 1052
Quoted 10 lines
boot/image-fit.c +4
-1- val =fdt_getprop (fit, noffset,"data-size-unciphered" ,NULL ); + val =fdt_getprop (fit, noffset,"data-size-unciphered" , &len);if (!val)return -ENOENT ; +if (len !=sizeof (*val)) +return -EINVAL ; + *data_size = (size_t )fdt32_to_cpu (*val);
Please add -EINVAL to the kernel-doc block above so the contract matches the code. Otherwise this looks right.
With that:
Regards,
Simon
PATCH 3/3 lib: aes: reject an unciphered size larger than the ciphertext
In reply to Pranav Rajendranimage_aes_decrypt() allocates cipher_len bytes, decrypts into them, and then reports the plaintext length to its caller as info->size_unciphered, without relating the two. size_unciphered comes from the image's 'data-size-unciphered' property, so an image can claim a plaintext larger than the buffer that was allocated for it.
fit_image_uncipher() propagates that length as the image size, and everything downstream - the load, the copy to the entry point - works from it, reading up to 4 GiB past the end of the decrypted buffer.
Unlike the image data itself, 'data-size-unciphered' is not covered by the per-image hash or signature, so this is reachable on a signed FIT whose signature still verifies.
Decryption produces exactly cipher_len bytes, so require the claimed size to fit within that.
--- lib/aes/aes-decrypt.c | 9 +++++++++ 1 file changed, 9 insertions(+)
lib/aes/aes-decrypt.c +9 -0
@@ -27,6 +27,15 @@ int image_aes_decrypt(struct image_cipher_info *info,return -EINVAL ; } +/* +* Decryption produces exactly cipher_len bytes, so the unciphered +* size the image claims cannot be larger than that. +*/ +if (info->size_unciphered > cipher_len) { +printf ("Invalid unciphered size\n" ); +return -EINVAL ; + } + *data =malloc (cipher_len);if (!*data) {printf ("Can't allocate memory to decrypt\n" );
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH v1 3/3] lib: aes: reject an unciphered size larger than the ciphertext
Quoted 16 lines · Pranav Rajendran
lib: aes: reject an unciphered size larger than the ciphertext
image_aes_decrypt()allocatescipher_lenbytes, decrypts into them, and then reports the plaintext length to its caller asinfo->size_unciphered, without relating the two.size_uncipheredcomes from the image's 'data-size-unciphered' property, so an image can claim a plaintext larger than the buffer that was allocated for it.
fit_image_uncipher()propagates that length as the image size, and everything downstream - the load, the copy to the entry point - works from it, reading up to 4 GiB past the end of the decrypted buffer.Unlike the image data itself, 'data-size-unciphered' is not covered by the per-image hash or signature, so this is reachable on a signed FIT whose signature still verifies.
Quoted 8 lines
Decryption produces exactly
cipher_lenbytes, so require the claimed size to fit within that.Fixes 4df3578119b0 ("u-boot: fit: add support to decrypt fit with aes")Signed-off-by Pranav Rajendran <[email protected]>
lib/aes/aes-decrypt.c| 9 +++++++++1 file changed, 9 insertions(+)
Hi Pranav,
Quoted 3 lines · Pranav Rajendran
I have kept them as separate patches since they are independent defects with different consequences, but I am happy to squash 1 and 3 if you would rather have one change per function.
Please keep them separate - distinct Fixes: targets, and easier to back-port independently.
Since these are security fixes, how about adding regression tests? test/lib/test_aes.c covers the primitive, but nothing exercises image_aes_decrypt() or fit_image_get_data_size_unciphered() against a malformed FIT. A small sandbox test constructing a FIT with a non-block-aligned 'data', a truncated 'data-size-unciphered', and an oversized unciphered size would pin all three down. What do you think?
Regards,
Simon
v2 cover fit: cipher: bounds checks on the ciphered image path
In reply to Pranav RajendranChanges in v2:
- Kept patches 1-3 as separate changes per Simon's request (distinct
Fixes: targets, easier to back-port independently).
- Patch 2: documented the new -EINVAL return in the kernel-doc block, per Simon's review comment.
- Added patch 5: a fit_cipher sandbox test suite covering all three defects (non-block-aligned 'data', a truncated 'data-size-unciphered', and an oversized unciphered size), per Simon's request for regression tests since these are security fixes.
- Added patch 4 as a prerequisite for patch 5: <u-boot/aes.h> defines the disabled-config cipher stubs with external linkage, so a second translation unit including it (the new test file) failed to link until those stubs were made static inline. Found while writing the test, not previously reported.
Patches 1 and 3 are unchanged from v1 and carry Simon's Reviewed-by.
checkpatch-clean, builds for sandbox (CONFIG_FIT_CIPHER=y) with no new warnings at W=1, and the new fit_cipher suite plus the existing lib_test_aes and fit_verity suites pass under ut.
Pranav Rajendran (5):
lib: aes: reject a ciphertext length that is not a whole number of
blocks
image-fit: check the length of the data-size-unciphered property
lib: aes: reject an unciphered size larger than the ciphertext
include: u-boot: aes: make disabled cipher stubs static inline
test: boot: add regression tests for the FIT cipher bounds checks
boot/image-fit.c | 7 ++- include/u-boot/aes.h | 17 ++++--- lib/aes/aes-decrypt.c | 21 +++++++- test/boot/Makefile | 1 + test/boot/fit_cipher.c | 113 +++++++++++++++++++++++++++++++++++++++++ test/cmd_ut.c | 2 + 6 files changed, 151 insertions(+), 10 deletions(-) create mode 100644 test/boot/fit_cipher.c
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 1/5 lib: aes: reject a ciphertext length that is not a whole number of blocks
image_aes_decrypt() allocates cipher_len bytes for the plaintext but then asks aes_cbc_decrypt_blocks() to write DIV_ROUND_UP(cipher_len, AES_BLOCK_LENGTH) blocks into it. For a cipher_len that is not a multiple of AES_BLOCK_LENGTH the rounding up adds one block, so the last block is written up to AES_BLOCK_LENGTH - 1 bytes past the end of the allocation, and read the same distance past the end of the ciphertext.
cipher_len is the size of the image data in the FIT, so an image with a 'data' property whose length is not block aligned is enough to reach this. The overflowing bytes are decryption output, i.e. they depend on the key, but the length itself is not covered by anything that would stop the image from being parsed this far.
A CBC ciphertext is a whole number of blocks by construction, so treat anything else as a malformed image and reject it before allocating. With that established, compute the block count with a plain division so the buffer size and the write length cannot drift apart again.
---
(no changes since v1)
lib/aes/aes-decrypt.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
lib/aes/aes-decrypt.c +11 -1
@@ -17,6 +17,16 @@ int image_aes_decrypt(struct image_cipher_info *info,unsigned char key_exp[AES256_EXPAND_KEY_LENGTH ];unsigned int aes_blocks, key_len = info->cipher->key_len; +/* +* The ciphertext is a whole number of AES blocks by construction, and +* the decryption below writes one full block at a time, so anything +* else would overflow the output buffer. +*/ +if (!cipher_len || cipher_len %AES_BLOCK_LENGTH ) { +printf ("Invalid ciphertext length\n" ); +return -EINVAL ; + } + *data =malloc (cipher_len);if (!*data) {printf ("Can't allocate memory to decrypt\n" );
@@ -30,7 +40,7 @@ int image_aes_decrypt(struct image_cipher_info *info,aes_expand_key ((u8 *)info->key, key_len, key_exp);/* Calculate the number of AES blocks to encrypt. */ - aes_blocks =DIV_ROUND_UP (cipher_len,AES_BLOCK_LENGTH ); + aes_blocks = cipher_len /AES_BLOCK_LENGTH ;aes_cbc_decrypt_blocks (key_len, key_exp, (u8 *)info->iv, (u8 *)cipher, *data, aes_blocks);
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 2/5 image-fit: check the length of the data-size-unciphered property
In reply to Pranav Rajendranfit_image_get_data_size_unciphered() passes NULL as fdt_getprop()'s length argument, so it accepts a 'data-size-unciphered' property of any size and then dereferences the first four bytes of it. A property shorter than that is read past its end, and the bytes that follow it in the FIT are returned to the caller as the unciphered size.
Ask for the length and require it to be exactly one fdt32_t, as the binding describes.
---
v2:
- Document the new -EINVAL return in the kernel-doc block above the function, per Simon's review comment.
boot/image-fit.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
boot/image-fit.c +6 -1
@@ -1035,16 +1035,21 @@ int fit_image_get_data_size(const void *fit, int noffset, int *data_size)* returns: * 0, on success * -ENOENT if the property could not be found +* -EINVAL if the property is not exactly one fdt32_t long */ int fit_image_get_data_size_unciphered (const void *fit,int noffset,size_t *data_size) {const fdt32_t *val; +int len; - val =fdt_getprop (fit, noffset,"data-size-unciphered" ,NULL ); + val =fdt_getprop (fit, noffset,"data-size-unciphered" , &len);if (!val)return -ENOENT ; +if (len !=sizeof (*val)) +return -EINVAL ; + *data_size = (size_t )fdt32_to_cpu (*val);return 0 ;
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 3/5 lib: aes: reject an unciphered size larger than the ciphertext
In reply to Pranav Rajendranimage_aes_decrypt() allocates cipher_len bytes, decrypts into them, and then reports the plaintext length to its caller as info->size_unciphered, without relating the two. size_unciphered comes from the image's 'data-size-unciphered' property, so an image can claim a plaintext larger than the buffer that was allocated for it.
fit_image_uncipher() propagates that length as the image size, and everything downstream - the load, the copy to the entry point - works from it, reading up to 4 GiB past the end of the decrypted buffer.
Unlike the image data itself, 'data-size-unciphered' is not covered by the per-image hash or signature, so this is reachable on a signed FIT whose signature still verifies.
Decryption produces exactly cipher_len bytes, so require the claimed size to fit within that.
---
(no changes since v1)
lib/aes/aes-decrypt.c | 9 +++++++++ 1 file changed, 9 insertions(+)
lib/aes/aes-decrypt.c +9 -0
@@ -27,6 +27,15 @@ int image_aes_decrypt(struct image_cipher_info *info,return -EINVAL ; } +/* +* Decryption produces exactly cipher_len bytes, so the unciphered +* size the image claims cannot be larger than that. +*/ +if (info->size_unciphered > cipher_len) { +printf ("Invalid unciphered size\n" ); +return -EINVAL ; + } + *data =malloc (cipher_len);if (!*data) {printf ("Can't allocate memory to decrypt\n" );
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 4/5 include: u-boot: aes: make disabled cipher stubs static inline
In reply to Pranav Rajendranimage_aes_encrypt(), image_aes_add_cipher_data() and image_aes_decrypt() are defined directly in this header, with full bodies rather than just prototypes, whenever the corresponding IMAGE_ENABLE_ENCRYPT/DECRYPT feature macro is 0. On target that is always the encrypt side, since ciphering is host-tool only.
Those definitions have external linkage, so a second translation unit including this header under the same disabled configuration collides with the first at link time. Only boot/image-cipher.c currently includes the header on target, so this has not been reachable before, but it stops any other file - such as a unit test - from including it too.
Make the disabled-path stubs static inline, as is conventional for header-defined no-op fallbacks, so the header can be included from more than one translation unit.
---
v2:
- New in v2. Needed by patch 5: the new fit_cipher test suite is a second translation unit including this header under the same disabled configuration, which failed to link before this change.
include/u-boot/aes.h | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
include/u-boot/aes.h +9 -8
@@ -16,15 +16,16 @@ int image_aes_encrypt(struct image_cipher_info *info,int image_aes_add_cipher_data (struct image_cipher_info *info,void *keydest,void *fit,int node_noffset);#else -int image_aes_encrypt (struct image_cipher_info *info, -const unsigned char *data,int size, -unsigned char **cipher,int *cipher_len) +static inline int image_aes_encrypt (struct image_cipher_info *info, +const unsigned char *data,int size, +unsigned char **cipher,int *cipher_len) {return -ENXIO ; } -int image_aes_add_cipher_data (struct image_cipher_info *info,void *keydest, -void *fit,int node_noffset) +static inline int image_aes_add_cipher_data (struct image_cipher_info *info, +void *keydest,void *fit, +int node_noffset) {return -ENXIO ; }
@@ -35,9 +36,9 @@ int image_aes_decrypt(struct image_cipher_info *info, const void *cipher, size_t cipher_len, void **data, size_t *size);#else -int image_aes_decrypt (struct image_cipher_info *info, -const void *cipher,size_t cipher_len, -void **data,size_t *size) +static inline int image_aes_decrypt (struct image_cipher_info *info, +const void *cipher,size_t cipher_len, +void **data,size_t *size) {return -ENXIO ; }
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 5/5 test: boot: add regression tests for the FIT cipher bounds checks
In reply to Pranav RajendranAdd a fit_cipher unit test suite, gated on CONFIG_FIT_CIPHER like the code it exercises, covering the three defects fixed in this series:
- image_aes_decrypt() rejects a cipher_len that is not a whole number of AES blocks
- image_aes_decrypt() rejects an unciphered size larger than the ciphertext, and accepts the boundary case where they are equal
- fit_image_get_data_size_unciphered() rejects a 'data-size-unciphered' property that is not exactly one fdt32_t long, built via a minimal FIT constructed with libfdt, and accepts a correctly sized one
---
v2:
- New in v2, per Simon's request for regression tests on this series.
test/boot/Makefile | 1 + test/boot/fit_cipher.c | 113 +++++++++++++++++++++++++++++++++++++++++ test/cmd_ut.c | 2 + 3 files changed, 116 insertions(+) create mode 100644 test/boot/fit_cipher.c
test/boot/Makefile +1 -0
@@ -18,6 +18,7 @@ ifdef CONFIG_UT_DM obj-$(CONFIG_$(PHASE_)OF_LIBFDT) += image_fdt.o endif endif +obj-$(CONFIG_$(PHASE_)FIT_CIPHER) += fit_cipher.o obj-$(CONFIG_$(PHASE_)FIT_VERITY) += fit_verity.o obj-$(CONFIG_MEASURED_BOOT) += measurement.o
test/boot/fit_cipher.c new file +113 -0
113 lines not on this page · raw message
test/cmd_ut.c +2 -0
@@ -59,6 +59,7 @@ SUITE_DECL(env);SUITE_DECL (exit);SUITE_DECL (fdt);SUITE_DECL (fdt_overlay); +SUITE_DECL (fit_cipher);SUITE_DECL (fit_verity);SUITE_DECL (font);SUITE_DECL (hush);
@@ -88,6 +89,7 @@ static struct suite suites[] = {SUITE (exit,"shell exit and variables" ),SUITE (fdt,"fdt command" ),SUITE (fdt_overlay,"device tree overlays" ), +SUITE (fit_cipher,"FIT cipher bounds checks" ),SUITE (fit_verity,"FIT dm-verity cmdline generation" ),SUITE (font,"font command" ), SUITE(hush, "hush behaviour"),
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 15 lines · Pranav Rajendran
Changes in v2:
- Kept patches 1-3 as separate changes per Simon's request (distinct
Fixes: targets, easier to back-port independently).
- Patch 2: documented the new-EINVALreturn in the kernel-doc block, per Simon's review comment.
- Added patch 5: afit_ciphersandbox test suite covering all three defects (non-block-aligned 'data', a truncated 'data-size-unciphered', and an oversized unciphered size), per Simon's request for regression tests since these are security fixes.
- Added patch 4 as a prerequisite for patch 5: <u-boot/aes.h> defines the disabled-config cipher stubs with external linkage, so a second translation unit including it (the new test file) failed to link until those stubs were made static inline. Found while writing the test, not previously reported.
Quoted 1 line
[...]
Applied to u-boot/next, thanks!
[1/5] lib: aes: reject a ciphertext length that is not a whole number of blocks
commit: 38363ac783de606798c273175e9fa746651e5e06
[2/5] image-fit: check the length of the data-size-unciphered property
commit: c2b8eefd4e108b7fee67eccb08a345668a29d677
[3/5] lib: aes: reject an unciphered size larger than the ciphertext
commit: 2dde840a70f91fb184ecb6b997a8203f6c6f91ad
[4/5] include: u-boot: aes: make disabled cipher stubs static inline
commit: 4dc05884af34f75ac3a66f2f2ef4dd662cecfb97
[5/5] test: boot: add regression tests for the FIT cipher bounds checks
commit: 973a05bb1b1677a9ab7ecfe3807b82582039ff6fSignature · 1 line
Tom