From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B134531AAA3 for ; Wed, 26 Aug 2026 15:44:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787759102; cv=none; b=PKwAF3HtxVmqhMMcRmNr1fKkDuyUw8wes6Py0WudxdcghkWsEyNBoCYsO0iX0urtpuxR14GangLEBpRjRUgoML3IBOTF7ODSMZwlWY3rLaIUzJx8YefxusoepdmC3SYfj+Pa6SLzVPl/tedKT6PS7DYoWBrjZVN9tK99YMWm5Z4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787759102; c=relaxed/simple; bh=Wpda8V4mB+G1v6M4jdRqgjIHcIik2OOufAHm4b1jPHM=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=vGZ1Zl6EUlMtjpXauVZqQM9yCrBOvs0yLKIuOhuSfPc0RdkIhLO9yF8QxP+WmYDV7LtsxqI7pc90yGdTBEcuWlPBL8oaCfI9a5vYvgOjOa/eFiJ0kBWkgBxu78bnqNxB6HwN/TssgHFaxUfzCamKN3SbvEd52S6Jm4chaXmKyHY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=qk2CaZ9U; arc=none smtp.client-ip=209.85.128.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="qk2CaZ9U" Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-81ecf499af9so19018687b3.1 for ; Wed, 26 Aug 2026 08:44:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787759098; x=1788363898; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=6lCs31xrtlUjQWf/CteVcVdyEY/EdqnqYRaq66tpHgs=; b=qk2CaZ9U0gqkZ5PqCfbI1b58hD7WoAkTG+Hqyn5puILDvhvskYa+qQQSPrDd9RMYyz gPvNbKN1tu2wdGA1bi7r2LPNIzFQEaTFQmoOH+pcqcwfVQYevTjeTBH3uI7vUj4fbWcg 3G9uJkaIPTY0tK0GRGPnz6z0+LXjAV/S8Lj1s0Zl8EVU61cRexoqf0QyYJ+btMD1lgty i2PQU4380rdTyJk3hMtrYKkhRZCxGF3C/+02Z1LhlK3rWVy2Fpsrt2kxq91tYVCd2WxU qC62FAxt+jm3uApTkR2CVdk4ZutNcXAE2q/o6YBjHZQw7fB2nMvRnC5aeiUrc/nOyxa1 vpwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787759098; x=1788363898; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=6lCs31xrtlUjQWf/CteVcVdyEY/EdqnqYRaq66tpHgs=; b=DVGRCqdut+SSxr4izUahNG9rjYBTIDwqVfiakmZJif1S5IE4iSeJa/06+q8m9EdzeP /TOTrk8IbNZTFWI0YjVqXft8pYb+1om1tp9d5Ow0NMPutMrz5ggj/S5QZKKu6ZVdDiRx Lgc7t+cebsX4ifALPc5CuNhUsHhKFrY4R+q3T7e7HpZSUtjg3zm77kkAR9GGdlpqroQD 9dM5tuCOQ//2NhhDpK9FpADucLOVK8rh83+T+zAQ457Z9xLq03YCLmJj3w6PHnRw1jkv oDcqTyOERUVNiHTjqroZgLtDgeW81bm5UxLRd/jLD104Ebd4XSPyiDu8KRhJl1a0ujAL LWqA== X-Forwarded-Encrypted: i=1; AHgh+RphDfyM+HnluZUtGv1a5wohS+7nbpODOaILh6SfR9RmYLpyUeyzSzEwreK6Erq/jFmil+dZOggIy9FWWZ3J+q4=@vger.kernel.org X-Gm-Message-State: AFuF++mdAQfhxxmSSAMqcEks2opjP6nHNr88in3OR3gjH+C7bDBhKaVV EYTFtUN7pcW7Z0gyg1WndmKt275qhhGLX6n/GD49k61WfgXt13fEv2RH X-Gm-Gg: AR+sD137rzNLvvY4bAYOqK84ZB6ZYPJs8CvlW4+1iY3dCWdzJpqhnwB/+AX7lL3xnsD Nzkqh83JOSyVpfvilaA09XRhw7zF97Io3cKTlPaR1eL1hE62ri1pYQYbS7UdJUG9UiuHOxbfZHO j7r5qWd0euaAfcuqs6vlHbpfjdfxvprqjYJCavffgU1v3kWc6+uG2yiCC1zAUcBzIupSoIFzXcX 75u6MxZnZiDxc4K8IIdEC6TqvTNseSfY9U/v5NHyYyyu4Y8/RhSDx+5NJYpYSlzsKXZs9Y7sKDy 9lilfC7zmPtQOWEErrUdkKWW9YXyp2ak9we8aIDyFo8nIZ4BKiICLnuIi53ge797hGglQ4MV03l OxiKLUozQi0Xkr2rjfS4cBQ94IRaM9Hx46gvm2jGP/wRia+ComATrEdlzs4gCiokg4ZdyiXhg8D 7MN046eNNxtPqgL589CWjRbhytlgKvPauk1XtU0UElxeOXA/jpbx8pSQkRDyKrUhNnIUs= X-Received: by 2002:a05:690e:1b55:b0:66c:e3b6:40c7 with SMTP id 956f58d0204a3-66d2580c8aemr2153868d50.47.1787759097975; Wed, 26 Aug 2026 08:44:57 -0700 (PDT) Received: from mac.lan ([136.55.173.105]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8570dfcf578sm18285107b3.47.2026.08.26.08.44.57 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 26 Aug 2026 08:44:57 -0700 (PDT) From: "Cen Zhang (Microsoft Security FORGE Labs)" To: Mimi Zohar Cc: David Howells , Jarkko Sakkinen , Paul Moore , James Morris , "Serge E. Hallyn" , Roberto Sassu , David Safford , Greg Kroah-Hartman , Kees Cook , Francis Perron , linux-integrity@vger.kernel.org, keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Akrites SIRT , AutonomousCodeSecurity@microsoft.com, Cen Zhang Subject: [PATCH v2] KEYS: encrypted: fix integer overflow of datablob_len Date: Wed, 26 Aug 2026 11:44:56 -0400 Message-ID: <20260826154456.85974-1-blbllhy@gmail.com> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Cen Zhang (Microsoft Security FORGE Labs)" 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) Signed-off-by: Francis Perron (Akrites SIRT) --- 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 #include #include +#include #include #include #include @@ -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