FAILED: patch "[PATCH] KEYS: encrypted: fix integer overflow of datablob_len" failed to apply to 6.6-stable tree
Subsystems (4)
[email protected]
maintainerThe patch below does not apply to the 6.6-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.6.y
git checkout FETCH_HEAD
git cherry-pick -x 8697c431e297eb0d0ab13dda6bc172b48a34f05c
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026092201-gift-election-aa36@gregkh' --subject-prefix 'PATCH 6.6.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 8697c431e297eb0d0ab13dda6bc172b48a34f05c Mon Sep 17 00:00:00 2001
From: Cen Zhang <[email protected]>
Date: Sat, 12 Sep 2026 00:31:16 +0300
Subject: [PATCH] KEYS: encrypted: fix integer overflow of datablob_len
encrypted_key_alloc() stores datablob_len in a u16. It is computed from multiple string and payload lengths. If the result exceeds U16_MAX, the assignment truncates the allocation size. KASAN reports a 32760-byte slab-out-of-bounds write when __ekey_init() copies the master key description into the undersized buffer.
The total payload length stored in key->datalen is also a u16. Use check_add_overflow() to reject values that do not fit either destination, and use kzalloc_flex() for the flexible-array allocation.
security/keys/encrypted-keys/encrypted.c +14 -6
@@ -19,6 +19,7 @@#include <linux/parser.h> #include <linux/string.h> #include <linux/err.h> +#include <linux/overflow.h> #include <keys/user-type.h> #include <keys/trusted-type.h> #include <keys/encrypted-type.h>
@@ -579,6 +580,7 @@ static struct encrypted_key_payload *encrypted_key_alloc(struct key *key, {struct encrypted_key_payload *epayload =NULL ;unsigned short datablob_len; +unsigned short payload_totallen;unsigned short decrypted_datalen;unsigned short payload_datalen;unsigned int encrypted_datalen;
@@ -632,16 +634,22 @@ static struct encrypted_key_payload *encrypted_key_alloc(struct key *key, encrypted_datalen =roundup (decrypted_datalen, blksize); - datablob_len = format_len +1 +strlen (master_desc) +1 - +strlen (datalen) +1 + ivsize +1 + encrypted_datalen; +if (check_add_overflow (format_len +1 +strlen (master_desc) +1 + +strlen (datalen) +1 + ivsize +1 , + encrypted_datalen, &datablob_len)) +return ERR_PTR (-EINVAL ); - ret =key_payload_reserve (key, payload_datalen + datablob_len - +HASH_SIZE +1 ); +if (check_add_overflow (datablob_len, + payload_datalen +HASH_SIZE +1 , + &payload_totallen)) +return ERR_PTR (-EINVAL ); + + ret =key_payload_reserve (key, payload_totallen);if (ret <0 )return ERR_PTR (ret); - epayload =kzalloc (sizeof (*epayload) + payload_datalen + - datablob_len +HASH_SIZE +1 ,GFP_KERNEL ); + epayload =kzalloc_flex (*epayload, payload_data, payload_totallen, +GFP_KERNEL );if (!epayload)return ERR_PTR (-ENOMEM );
Sasha Levin
maintainerPATCH 1/2 slab: Introduce kmalloc_obj() and family
From: Kees Cook <[email protected]>
[ Upstream commit 2932ba8d9c99875b98c951d9d3fd6d651d35df3a ]
Introduce type-aware kmalloc-family helpers to replace the common idioms for single object and arrays of objects allocation:
ptr = kmalloc(sizeof(*ptr), gfp); ptr = kmalloc(sizeof(struct some_obj_name), gfp); ptr = kzalloc(sizeof(*ptr), gfp); ptr = kmalloc_array(count, sizeof(*ptr), gfp); ptr = kcalloc(count, sizeof(*ptr), gfp);
These become, respectively:
ptr = kmalloc_obj(*ptr, gfp); ptr = kmalloc_obj(*ptr, gfp); ptr = kzalloc_obj(*ptr, gfp); ptr = kmalloc_objs(*ptr, count, gfp); ptr = kzalloc_objs(*ptr, count, gfp);
Beyond the other benefits outlined below, the primary ergonomic benefit is the elimination of needing "sizeof" nor the type name, and the enforcement of assignment types (they do not return "void *", but rather a pointer to the type of the first argument). The type name _can_ be used, though, in the case where an assignment is indirect (e.g. via "return"). This additionally allows[1] variables to be declared via __auto_type:
__auto_type ptr = kmalloc_obj(struct foo, gfp);
Internal introspection of the allocated type now becomes possible, allowing for future alignment-aware choices to be made by the allocator and future hardening work that can be type sensitive. For example, adding __alignof(*ptr) as an argument to the internal allocators so that appropriate/efficient alignment choices can be made, or being able to correctly choose per-allocation offset randomization within a bucket that does not break alignment requirements.
Backport to 6.6: place the type-aware macros between the existing kmalloc() and kmalloc_node(), retaining the stable allocator implementations. Do not import the unrelated allocation-profiling, nolock, or bucket APIs from the upstream insertion context.
Also provide kzalloc_flex() using the existing overflow-safe struct_size_t() and kzalloc(), so 8697c431e297 ("KEYS: encrypted: fix integer overflow of datablob_len") can apply and compile without another allocator dependency. The encrypted payload has no __counted_by annotation; this compatibility macro leaves initialization of any count member to callers. No functions are added. Correct the sizeof() syntax in the documentation example.
Stable-dep-of: 8697c431e297 ("KEYS: encrypted: fix integer overflow of datablob_len")
--- Documentation/process/deprecated.rst | 24 +++++++++ include/linux/slab.h | 77 ++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+)
Documentation/process/deprecated.rst +24 -0
@@ -372,3 +372,27 @@ The helper must be used:: DECLARE_FLEX_ARRAY(struct type2, two); }; }; + +Open-coded kmalloc assignments for struct objects +------------------------------------------------- +Performing open-coded kmalloc()-family allocation assignments prevents +the kernel (and compiler) from being able to examine the type of the +variable being assigned, which limits any related introspection that +may help with alignment, wrap-around, or additional hardening. The +kmalloc_obj()-family of macros provide this introspection, which can be +used for the common code patterns for single, array, and flexible object +allocations. For example, these open coded assignments:: + + ptr = kmalloc(sizeof(*ptr), gfp); + ptr = kzalloc(sizeof(*ptr), gfp); + ptr = kmalloc_array(count, sizeof(*ptr), gfp); + ptr = kcalloc(count, sizeof(*ptr), gfp); + ptr = kmalloc(sizeof(struct foo), gfp); + +become, respectively:: + + ptr = kmalloc_obj(*ptr, gfp); + ptr = kzalloc_obj(*ptr, gfp); + ptr = kmalloc_objs(*ptr, count, gfp); + ptr = kzalloc_objs(*ptr, count, gfp); + __auto_type ptr = kmalloc_obj(struct foo, gfp);
include/linux/slab.h +77 -0
89 lines not on this page · raw message
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/2 KEYS: encrypted: fix integer overflow of datablob_len
From: Cen Zhang <[email protected]>
[ Upstream commit 8697c431e297eb0d0ab13dda6bc172b48a34f05c ]
encrypted_key_alloc() stores datablob_len in a u16. It is computed from multiple string and payload lengths. If the result exceeds U16_MAX, the assignment truncates the allocation size. KASAN reports a 32760-byte slab-out-of-bounds write when __ekey_init() copies the master key description into the undersized buffer.
The total payload length stored in key->datalen is also a u16. Use check_add_overflow() to reject values that do not fit either destination, and use kzalloc_flex() for the flexible-array allocation.
--- security/keys/encrypted-keys/encrypted.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
security/keys/encrypted-keys/encrypted.c +14 -6
@@ -18,6 +18,7 @@#include <linux/parser.h> #include <linux/string.h> #include <linux/err.h> +#include <linux/overflow.h> #include <keys/user-type.h> #include <keys/trusted-type.h> #include <keys/encrypted-type.h>
@@ -606,6 +607,7 @@ static struct encrypted_key_payload *encrypted_key_alloc(struct key *key, {struct encrypted_key_payload *epayload =NULL ;unsigned short datablob_len; +unsigned short payload_totallen;unsigned short decrypted_datalen;unsigned short payload_datalen;unsigned int encrypted_datalen;
@@ -659,16 +661,22 @@ static struct encrypted_key_payload *encrypted_key_alloc(struct key *key, encrypted_datalen =roundup (decrypted_datalen, blksize); - datablob_len = format_len +1 +strlen (master_desc) +1 - +strlen (datalen) +1 + ivsize +1 + encrypted_datalen; +if (check_add_overflow (format_len +1 +strlen (master_desc) +1 + +strlen (datalen) +1 + ivsize +1 , + encrypted_datalen, &datablob_len)) +return ERR_PTR (-EINVAL ); + +if (check_add_overflow (datablob_len, + payload_datalen +HASH_SIZE +1 , + &payload_totallen)) +return ERR_PTR (-EINVAL ); - ret =key_payload_reserve (key, payload_datalen + datablob_len - +HASH_SIZE +1 ); + ret =key_payload_reserve (key, payload_totallen);if (ret <0 )return ERR_PTR (ret); - epayload =kzalloc (sizeof (*epayload) + payload_datalen + - datablob_len +HASH_SIZE +1 ,GFP_KERNEL ); + epayload =kzalloc_flex (*epayload, payload_data, payload_totallen, +GFP_KERNEL );if (!epayload)return ERR_PTR (-ENOMEM );
Signature · 1 line
2.53.0