Kernel keyring development
 help / color / mirror / Atom feed
From: "Cen Zhang (Microsoft Security FORGE Labs)" <blbllhy@gmail.com>
To: Mimi Zohar <zohar@linux.ibm.com>
Cc: David Howells <dhowells@redhat.com>,
	Jarkko Sakkinen <jarkko@kernel.org>,
	Paul Moore <paul@paul-moore.com>,
	James Morris <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	Roberto Sassu <roberto.sassu@polito.it>,
	David Safford <safford@watson.ibm.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Kees Cook <kees@kernel.org>, Francis Perron <francis@akrites.dev>,
	linux-integrity@vger.kernel.org, keyrings@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org, Akrites SIRT <sirt@akrites.dev>,
	AutonomousCodeSecurity@microsoft.com,
	Cen Zhang <cenzhang@microsoft.com>
Subject: [PATCH v2] KEYS: encrypted: fix integer overflow of datablob_len
Date: Wed, 26 Aug 2026 11:44:56 -0400	[thread overview]
Message-ID: <20260826154456.85974-1-blbllhy@gmail.com> (raw)

From: "Cen Zhang (Microsoft Security FORGE Labs)" <cenzhang@microsoft.com>

The datablob_len field in struct encrypted_key_payload and the local
variable in encrypted_key_alloc() are declared as unsigned short, which
has a maximum value of 65535. The datablob_len is computed as:

  format_len + 1 + strlen(master_desc) + 1 + strlen(datalen) + 1
  + ivsize + 1 + encrypted_datalen

An attacker can create an encrypted key with a very long datalen string
(e.g., 32756 characters of leading zeros followed by "4096", which
kstrtol() happily parses as 4096), and then update it with a very long
master_desc string (~32760 characters). The combined lengths exceed
65535, causing the unsigned short to silently wrap around. This results
in a grossly undersized kzalloc() allocation, and the subsequent
memcpy() in __ekey_init() writes ~32KB past the end of the buffer,
corrupting adjacent slab objects.

Fix this by:
1. Using check_add_overflow() to calculate datablob_len directly into
   its existing unsigned short destination.
2. Checking the total payload length the same way before passing it to
   key_payload_reserve(), since key->datalen is also unsigned short.
3. Using kzalloc_flex() to allocate encrypted_key_payload together with
   its trailing payload_data[] array.

Fixes: 7e70cb497850 ("keys: add new key-type encrypted")
Cc: stable@vger.kernel.org
Assisted-by: GitHub-Copilot:claude-opus-4.6
Signed-off-by: Cen Zhang (Microsoft Security FORGE Labs) <cenzhang@microsoft.com>
Signed-off-by: Francis Perron (Akrites SIRT) <francis@akrites.dev>
---
Changes in v2:
- Keep datablob_len unchanged and check both unsigned short bounds with
  check_add_overflow().
- Use kzalloc_flex() for the trailing payload_data[] array.
- Correct the attribution and sign-off trailers.

The initial version was discussed off-list and is not publicly archived.

 security/keys/encrypted-keys/encrypted.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/security/keys/encrypted-keys/encrypted.c b/security/keys/encrypted-keys/encrypted.c
index 59cb77b237b3..e07092ea301a 100644
--- a/security/keys/encrypted-keys/encrypted.c
+++ b/security/keys/encrypted-keys/encrypted.c
@@ -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);
+
+	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);
 
-- 
2.55.0

             reply	other threads:[~2026-08-26 15:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-26 15:44 Cen Zhang (Microsoft Security FORGE Labs) [this message]
2026-08-28  1:53 ` [PATCH v2] KEYS: encrypted: fix integer overflow of datablob_len Jarkko Sakkinen
2026-08-28  1:55 ` Jarkko Sakkinen
2026-08-28  1:59 ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260826154456.85974-1-blbllhy@gmail.com \
    --to=blbllhy@gmail.com \
    --cc=AutonomousCodeSecurity@microsoft.com \
    --cc=cenzhang@microsoft.com \
    --cc=dhowells@redhat.com \
    --cc=francis@akrites.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=jarkko@kernel.org \
    --cc=jmorris@namei.org \
    --cc=kees@kernel.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=roberto.sassu@polito.it \
    --cc=safford@watson.ibm.com \
    --cc=serge@hallyn.com \
    --cc=sirt@akrites.dev \
    --cc=zohar@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox