[PATCH] smb: client: check the cifs.spnego key lengths against its payload

From: Piyus Kedia

Date: Sat Dec 05 2026 - 05:16:09 EST


The payload of a cifs.spnego key is a struct cifs_spnego_msg followed by
the session key and the security blob, whose lengths are in the header:
sesskey_len and secblob_len. cifs_spnego_key_instantiate() stores the
payload as the request-key helper sent it, and nothing keeps its length,
so the session setup code has nothing to check the two against.
SMB2_auth_kerberos() copies sesskey_len bytes from msg->data into the
session key and sends secblob_len bytes from msg->data + sesskey_len to
the server as the security buffer; sess_auth_kerberos() does the same
for SMB1. A header that claims more than the payload holds makes both
read past the end of it:

BUG: KASAN: slab-out-of-bounds in SMB2_auth_kerberos+0x3a8/0x5b0
Read of size 4096 at addr ffff888040f862b0 by task poc/9451
__asan_memcpy+0x24/0x60 mm/kasan/shadow.c:105
SMB2_auth_kerberos+0x3a8/0x5b0 fs/smb/client/smb2pdu.c:1809
SMB2_sess_setup+0x1b4/0x470 fs/smb/client/smb2pdu.c:2087
cifs_setup_session+0x49d/0xaf0 fs/smb/client/connect.c:4240
cifs_get_smb_ses+0xf4a/0x14e0 fs/smb/client/connect.c:2535
cifs_mount_get_session+0xc0/0x2e0 fs/smb/client/connect.c:3660
Allocated by task 9458:
kmemdup_noprof+0x2b/0x60 mm/util.c:138
cifs_spnego_key_instantiate+0x3a/0x80 fs/smb/client/cifs_spnego.c:28
key_instantiate_and_link+0x314/0x330 security/keys/key.c:544
keyctl_instantiate_key_common+0x240/0x350 security/keys/keyctl.c:1228
The buggy address is located 0 bytes to the right of
allocated 16-byte region [ffff888040f862a0, ffff888040f862b0)

Check the lengths once, where the payload comes in, so that its readers
can rely on them: the header has to be there and the session key and
the blob have to fit in what follows it. Only an overrun is refused; a
payload with bytes to spare after the blob is accepted as before.

Only the helper that the kernel runs for the key, holding the key's
authorisation, can instantiate it, and the type's vet_description
refuses cifs.spnego keys created from userspace, so this hardens the
client against a buggy or compromised helper.

We found this with a syzkaller-based fuzzing harness driven by an AI tool.

Fixes: f1d662a7d5e5 ("[CIFS] Add upcall files for cifs to use spnego/kerberos")
Closes: https://lore.kernel.org/r/cifs-spnego-key-lengths.20261205@xxxxxxxxxxx/
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
Tested on ksmbd-for-next (d70570cf8003), whose fs/smb/client is the
same as mainline's, with KASAN; this patch was the only change between
the two builds. The reproducer installs itself as /sbin/request-key,
instantiates the cifs.spnego key with a chosen payload, and mounts a
minimal SMB server on loopback with sec=krb5, as SMB 3.1.1 and as
vers=1.0:

before 16-byte header claiming sesskey_len 4096: the report above
with SMB 3.1.1, and in sess_auth_kerberos() with vers=1.0,
4 of 4 boots each; a payload with the session key but
secblob_len 64 and no blob, and one a byte of blob short:
KASAN in _copy_from_iter() as the blob is sent
after KEYCTL_INSTANTIATE fails with EINVAL for all three, no
SESSION_SETUP goes out and the mount fails with ENOKEY; no
report in 12 boots (the first payload 3 times per dialect)
both consistent payloads, exact and with 4 bytes to spare, are
accepted; the server receives the blob they carry

The patch applies to cifs-next (9b62aa64ed00) and mainline
(c3500c5ef648).

fs/smb/client/cifs_spnego.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/fs/smb/client/cifs_spnego.c b/fs/smb/client/cifs_spnego.c
index 44c407275680..69c3e3eecea4 100644
--- a/fs/smb/client/cifs_spnego.c
+++ b/fs/smb/client/cifs_spnego.c
@@ -25,8 +25,22 @@ static const struct cred *spnego_cred;
static int
cifs_spnego_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
{
- char *payload = kmemdup(prep->data, prep->datalen, GFP_KERNEL);
+ const struct cifs_spnego_msg *msg = prep->data;
+ size_t len;
+ char *payload;

+ /*
+ * Session setup reads sesskey_len and then secblob_len bytes from
+ * msg->data without knowing the payload's length, so check here that
+ * the upcall sent that much.
+ */
+ if (prep->datalen < sizeof(*msg))
+ return -EINVAL;
+ len = prep->datalen - sizeof(*msg);
+ if (msg->sesskey_len > len || msg->secblob_len > len - msg->sesskey_len)
+ return -EINVAL;
+
+ payload = kmemdup(prep->data, prep->datalen, GFP_KERNEL);
if (!payload)
return -ENOMEM;

--
2.34.1