[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aOybIZ2iqXExpTUw@wunner.de>
Date: Mon, 13 Oct 2025 08:24:33 +0200
From: Lukas Wunner <lukas@...ner.de>
To: Thorsten Blum <thorsten.blum@...ux.dev>
Cc: David Howells <dhowells@...hat.com>,
Ignat Korchagin <ignat@...udflare.com>,
Herbert Xu <herbert@...dor.apana.org.au>,
"David S. Miller" <davem@...emloft.net>,
Vivek Goyal <vgoyal@...hat.com>, stable@...r.kernel.org,
keyrings@...r.kernel.org, linux-crypto@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2 1/2] crypto: asymmetric_keys - prevent overflow in
asymmetric_key_generate_id
On Sun, Oct 12, 2025 at 10:38:40PM +0200, Thorsten Blum wrote:
> Use check_add_overflow() to guard against a potential integer overflow
> when adding the binary blob lengths in asymmetric_key_generate_id() and
> return -EOVERFLOW accordingly. This prevents a possible buffer overflow
> when copying data from potentially malicious X.509 fields that can be
> arbitrarily large, such as ASN.1 INTEGER serial numbers, issuer names,
> etc.
>
> Also use struct_size() to calculate the number of bytes to allocate for
> the new asymmetric key id.
>
> Cc: stable@...r.kernel.org
> Fixes: 7901c1a8effb ("KEYS: Implement binary asymmetric key ID handling")
No stable designation please, this doesn't pass the "obviously correct"
test, see below.
> +++ b/crypto/asymmetric_keys/asymmetric_type.c
> @@ -141,12 +142,14 @@ struct asymmetric_key_id *asymmetric_key_generate_id(const void *val_1,
> size_t len_2)
> {
> struct asymmetric_key_id *kid;
> + size_t len;
>
> - kid = kmalloc(sizeof(struct asymmetric_key_id) + len_1 + len_2,
> - GFP_KERNEL);
> + if (check_add_overflow(len_1, len_2, &len))
> + return ERR_PTR(-EOVERFLOW);
> + kid = kmalloc(struct_size(kid, data, len), GFP_KERNEL);
This will add (at least) 2 bytes to len (namely the size of struct
asymmetric_key_id)) and may cause an overflow (even if len_1 + len_2
did not overflow).
struct_size() truncates to SIZE_MAX and then right below...
> if (!kid)
> return ERR_PTR(-ENOMEM);
> - kid->len = len_1 + len_2;
> + kid->len = len;
> memcpy(kid->data, val_1, len_1);
> memcpy(kid->data + len_1, val_2, len_2);
... this memcpy() operation will perform an out-of-bound access
beyond SIZE_MAX.
Thanks,
Lukas
Powered by blists - more mailing lists