mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Piyus Kedia <piyus@iiitd.ac.in>
To: Namjae Jeon <linkinjeon@kernel.org>, linux-cifs@vger.kernel.org
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
	Tom Talpey <tom@talpey.com>,
	ChenXiaoSong <chenxiaosong@chenxiaosong.com>,
	linux-kernel@vger.kernel.org,
	Kapil Vaswani <kapil@presceralabs.ai>
Subject: [BUG] ksmbd: slab-out-of-bounds read in ksmbd_compare_user() on session re-authentication
Date: Fri,  4 Dec 2026 20:44:26 +0530	[thread overview]
Message-ID: <ksmbd-compare-user-passkey.20261204@iiitd.ac.in> (raw)

Hi,

ksmbd_compare_user() compares two users' passkeys over the length of
the first one only:

  fs/smb/server/mgmt/user_config.c:108
      if (memcmp(u1->passkey, u2->passkey, u1->passkey_sz))

Each passkey is allocated by ksmbd_alloc_user() at the hash_sz of its
own login response from the user-space daemon:

  fs/smb/server/mgmt/user_config.c:59
      user->passkey_sz = resp->hash_sz;
      user->passkey = kmalloc(resp->hash_sz, KSMBD_DEFAULT_GFP);

so nothing makes u2's passkey as long as u1's.  ntlm_authenticate()
calls it when a client re-authenticates a session, with u1 the
session's user and u2 the user just logged in for this request
(fs/smb/server/smb2pdu.c:2344; the expired-session branch at 2355 makes
the same call).  When the second login response carried a shorter
hash, memcmp() reads past the end of the new passkey:

  BUG: KASAN: slab-out-of-bounds in memcmp+0x51/0xd0 lib/string.c:665
  Read of size 8 at addr ffff888040fc1dc0 by task kworker/2:2/3253
  Workqueue: ksmbd-io handle_ksmbd_work
   memcmp+0x51/0xd0 lib/string.c:665
   bcmp+0x36/0x60 lib/string.c:697
   ksmbd_compare_user+0x97/0xd0 fs/smb/server/mgmt/user_config.c:108
   ntlm_authenticate+0x37e/0x9a0 fs/smb/server/smb2pdu.c:2344
   smb2_sess_setup+0x155e/0x1730 fs/smb/server/smb2pdu.c:2774
   __process_request+0x28f/0x4e0 fs/smb/server/server.c:163
   __handle_ksmbd_work+0x440/0xbc0 fs/smb/server/server.c:296
   handle_ksmbd_work+0x5f/0xa0 fs/smb/server/server.c:387

  Allocated by task 3253:
   __kmalloc_noprof+0x2ba/0x620 mm/slub.c:5562
   ksmbd_alloc_user+0x13b/0x360 fs/smb/server/mgmt/user_config.c:60
   ksmbd_login_user+0xb2/0x100 fs/smb/server/mgmt/user_config.c:28
   session_user+0x1ba/0x240 fs/smb/server/smb2pdu.c:2294
   ntlm_authenticate+0x212/0x9a0 fs/smb/server/smb2pdu.c:2328

  The buggy address belongs to the object at ffff888040fc1dc0
   which belongs to the cache kmalloc-8 of size 8
  The buggy address is located 0 bytes inside of
   allocated 1-byte region [ffff888040fc1dc0, ffff888040fc1dc1)

How it is reached.  The reproducer registers itself as the ksmbd
daemon over the ksmbd generic netlink family (KSMBD_EVENT_STARTING_UP),
then connects to the server over TCP as an SMB 3.1.1 client and does
an NTLMSSP session setup as user "u" twice on the same SessionId.  Its
daemon answers the two LOGIN_REQUESTs for "u" with hash_sz 18 and then
1, with KSMBD_USER_FLAG_GUEST_ACCOUNT set so the NTLMv2 check is
skipped and the first setup leaves the session valid.  The second
setup then compares the session's 18-byte passkey with the new 1-byte
one.  It has reproduced every time we ran it: five runs on two
kernels, one of them unprivileged (below).

Impact - who can trigger it:

  - The two lengths come from the daemon's login responses; an SMB
    client controls only the account name and the NTLMSSP blobs.  So
    the trigger is a daemon that reports a different hash size for the
    same account on a later login.  We have not found a way to make
    ksmbd.mountd do that from the network, but we have not audited
    ksmbd-tools either.  This is the trust boundary of 7405d0ba2943
    ("ksmbd: fix slab-out-of-bounds read in ksmbd_alloc_user()"),
    which bounds hash_sz from above; nothing bounds it from below
    against the session's earlier login.

  - With CONFIG_SMB_SERVER_CHECK_CAP_NET_ADMIN=y, the default,
    handle_startup_event() and handle_generic_event() require
    CAP_NET_ADMIN, checked by netlink_capable() against the initial
    user namespace.  An unprivileged user cannot register as the
    daemon or send it a login response, from a user namespace either,
    so on a default build this needs root (or a compromised or buggy
    daemon).

  - With that option disabled, neither handler checks anything.  We
    ran the same reproducer as uid 65534 with every capability dropped
    (CapEff and CapBnd 0), on a 7.4.0-rc5 build with the option off
    and no daemon running: it registered as the daemon and triggered
    the same KASAN report.  So on such a build any local user can
    trigger it while ksmbd.mountd is not running.  While it is
    running, handle_startup_event() refuses a second daemon, but
    handle_generic_event() matches a response to its request only by
    type and by a handle from an IDA, not by sender, so a forged
    LOGIN_RESPONSE from another process would be accepted as well; we
    have not tried that.

  - What the read does: it reads up to passkey_sz bytes from a
    shorter allocation, here 17 bytes past a 1-byte kmalloc-8 object.
    Nothing read is returned to the client; the bytes only decide
    whether the re-authentication is accepted or fails with
    -EKEYREJECTED.

Seen on 7.4.0-rc5 and REPRODUCED ON ksmbd-for-next (d70570cf8003,
2026-12-04) with KASAN.  On 7.4-rc5 the read came from the
valid-session call (smb2pdu.c:2344 there); on ksmbd-for-next the same
program reached it through the expired-session call, smb2pdu.c:2402:

  BUG: KASAN: slab-out-of-bounds in memcmp+0x8b/0xb0
  Read of size 8 at addr ffff88805834a120 by task kworker/1:0/26
   memcmp+0x8b/0xb0 lib/string.c:665
   ksmbd_compare_user+0x82/0xb0 fs/smb/server/mgmt/user_config.c:108
   smb2_sess_setup+0x222d/0x3610 fs/smb/server/smb2pdu.c:2402

ksmbd_compare_user() and both calls are the same in mainline
(007722b8211f, 2026-12-03).  The comparison has been this way since it
was added in a58b45a4dbfd ("ksmbd: set RSS capable in
FSCTL_QUERY_NETWORK_INTERFACE_INFO"), so v5.17 and later.

A patch follows in reply to this mail.  Users whose passkeys differ in
length are not the same user, so it compares the lengths before the
bytes.  On ksmbd-for-next, same config and image before and after,
only that change between the two builds:

  without the patch   KASAN slab-out-of-bounds read, as above
  with the patch      the same program runs to the end and the
                      re-authentication is rejected (STATUS_ACCESS_DENIED,
                      as before), no report, twice

This was found by AI-driven fuzzing built on top of syzkaller; its
analysis was then checked by hand against the code and every claim
above was reproduced.  The reproducer is fuzzer-generated, runs the
daemon and the client in one program over loopback, and we are happy
to send it on request.

Kapil Vaswani
Piyus Kedia

WARNING: multiple messages have this Message-ID
From: Piyus Kedia <piyus@iiitd.ac.in>
To: Namjae Jeon <linkinjeon@kernel.org>, linux-cifs@vger.kernel.org
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
	Tom Talpey <tom@talpey.com>,
	ChenXiaoSong <chenxiaosong@chenxiaosong.com>,
	linux-kernel@vger.kernel.org,
	Kapil Vaswani <kapil@presceralabs.ai>
Subject: [BUG] ksmbd: slab-out-of-bounds read in ksmbd_compare_user() on session re-authentication
Date: Fri,  4 Dec 2026 20:44:45 +0530	[thread overview]
Message-ID: <ksmbd-compare-user-passkey.20261204@iiitd.ac.in> (raw)
Message-ID: <20261204151445.0JTQkluyTK-SmGb2K12_LG_pBCkcgyIqkEzh9Y-KTzQ@z> (raw)

Hi,

ksmbd_compare_user() compares two users' passkeys over the length of
the first one only:

  fs/smb/server/mgmt/user_config.c:108
      if (memcmp(u1->passkey, u2->passkey, u1->passkey_sz))

Each passkey is allocated by ksmbd_alloc_user() at the hash_sz of its
own login response from the user-space daemon:

  fs/smb/server/mgmt/user_config.c:59
      user->passkey_sz = resp->hash_sz;
      user->passkey = kmalloc(resp->hash_sz, KSMBD_DEFAULT_GFP);

so nothing makes u2's passkey as long as u1's.  ntlm_authenticate()
calls it when a client re-authenticates a session, with u1 the
session's user and u2 the user just logged in for this request
(fs/smb/server/smb2pdu.c:2344; the expired-session branch at 2355 makes
the same call).  When the second login response carried a shorter
hash, memcmp() reads past the end of the new passkey:

  BUG: KASAN: slab-out-of-bounds in memcmp+0x51/0xd0 lib/string.c:665
  Read of size 8 at addr ffff888040fc1dc0 by task kworker/2:2/3253
  Workqueue: ksmbd-io handle_ksmbd_work
   memcmp+0x51/0xd0 lib/string.c:665
   bcmp+0x36/0x60 lib/string.c:697
   ksmbd_compare_user+0x97/0xd0 fs/smb/server/mgmt/user_config.c:108
   ntlm_authenticate+0x37e/0x9a0 fs/smb/server/smb2pdu.c:2344
   smb2_sess_setup+0x155e/0x1730 fs/smb/server/smb2pdu.c:2774
   __process_request+0x28f/0x4e0 fs/smb/server/server.c:163
   __handle_ksmbd_work+0x440/0xbc0 fs/smb/server/server.c:296
   handle_ksmbd_work+0x5f/0xa0 fs/smb/server/server.c:387

  Allocated by task 3253:
   __kmalloc_noprof+0x2ba/0x620 mm/slub.c:5562
   ksmbd_alloc_user+0x13b/0x360 fs/smb/server/mgmt/user_config.c:60
   ksmbd_login_user+0xb2/0x100 fs/smb/server/mgmt/user_config.c:28
   session_user+0x1ba/0x240 fs/smb/server/smb2pdu.c:2294
   ntlm_authenticate+0x212/0x9a0 fs/smb/server/smb2pdu.c:2328

  The buggy address belongs to the object at ffff888040fc1dc0
   which belongs to the cache kmalloc-8 of size 8
  The buggy address is located 0 bytes inside of
   allocated 1-byte region [ffff888040fc1dc0, ffff888040fc1dc1)

How it is reached.  The reproducer registers itself as the ksmbd
daemon over the ksmbd generic netlink family (KSMBD_EVENT_STARTING_UP),
then connects to the server over TCP as an SMB 3.1.1 client and does
an NTLMSSP session setup as user "u" twice on the same SessionId.  Its
daemon answers the two LOGIN_REQUESTs for "u" with hash_sz 18 and then
1, with KSMBD_USER_FLAG_GUEST_ACCOUNT set so the NTLMv2 check is
skipped and the first setup leaves the session valid.  The second
setup then compares the session's 18-byte passkey with the new 1-byte
one.  It has reproduced every time we ran it: five runs on two
kernels, one of them unprivileged (below).

Impact - who can trigger it:

  - The two lengths come from the daemon's login responses; an SMB
    client controls only the account name and the NTLMSSP blobs.  So
    the trigger is a daemon that reports a different hash size for the
    same account on a later login.  We have not found a way to make
    ksmbd.mountd do that from the network, but we have not audited
    ksmbd-tools either.  This is the trust boundary of 7405d0ba2943
    ("ksmbd: fix slab-out-of-bounds read in ksmbd_alloc_user()"),
    which bounds hash_sz from above; nothing bounds it from below
    against the session's earlier login.

  - With CONFIG_SMB_SERVER_CHECK_CAP_NET_ADMIN=y, the default,
    handle_startup_event() and handle_generic_event() require
    CAP_NET_ADMIN, checked by netlink_capable() against the initial
    user namespace.  An unprivileged user cannot register as the
    daemon or send it a login response, from a user namespace either,
    so on a default build this needs root (or a compromised or buggy
    daemon).

  - With that option disabled, neither handler checks anything.  We
    ran the same reproducer as uid 65534 with every capability dropped
    (CapEff and CapBnd 0), on a 7.4.0-rc5 build with the option off
    and no daemon running: it registered as the daemon and triggered
    the same KASAN report.  So on such a build any local user can
    trigger it while ksmbd.mountd is not running.  While it is
    running, handle_startup_event() refuses a second daemon, but
    handle_generic_event() matches a response to its request only by
    type and by a handle from an IDA, not by sender, so a forged
    LOGIN_RESPONSE from another process would be accepted as well; we
    have not tried that.

  - What the read does: it reads up to passkey_sz bytes from a
    shorter allocation, here 17 bytes past a 1-byte kmalloc-8 object.
    Nothing read is returned to the client; the bytes only decide
    whether the re-authentication is accepted or fails with
    -EKEYREJECTED.

Seen on 7.4.0-rc5 and REPRODUCED ON ksmbd-for-next (d70570cf8003,
2026-12-04) with KASAN.  On 7.4-rc5 the read came from the
valid-session call (smb2pdu.c:2344 there); on ksmbd-for-next the same
program reached it through the expired-session call, smb2pdu.c:2402:

  BUG: KASAN: slab-out-of-bounds in memcmp+0x8b/0xb0
  Read of size 8 at addr ffff88805834a120 by task kworker/1:0/26
   memcmp+0x8b/0xb0 lib/string.c:665
   ksmbd_compare_user+0x82/0xb0 fs/smb/server/mgmt/user_config.c:108
   smb2_sess_setup+0x222d/0x3610 fs/smb/server/smb2pdu.c:2402

ksmbd_compare_user() and both calls are the same in mainline
(007722b8211f, 2026-12-03).  The comparison has been this way since it
was added in a58b45a4dbfd ("ksmbd: set RSS capable in
FSCTL_QUERY_NETWORK_INTERFACE_INFO"), so v5.17 and later.

A patch follows in reply to this mail.  Users whose passkeys differ in
length are not the same user, so it compares the lengths before the
bytes.  On ksmbd-for-next, same config and image before and after,
only that change between the two builds:

  without the patch   KASAN slab-out-of-bounds read, as above
  with the patch      the same program runs to the end and the
                      re-authentication is rejected (STATUS_ACCESS_DENIED,
                      as before), no report, twice

This was found by AI-driven fuzzing built on top of syzkaller; its
analysis was then checked by hand against the code and every claim
above was reproduced.  The reproducer is fuzzer-generated, runs the
daemon and the client in one program over loopback, and we are happy
to send it on request.

Kapil Vaswani
Piyus Kedia

             reply	other threads:[~2026-12-04 15:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 15:14 Piyus Kedia [this message]
2026-12-04 15:14 ` [PATCH] ksmbd: compare the passkey lengths in ksmbd_compare_user() Piyus Kedia
2026-12-04 15:14 ` [BUG] ksmbd: slab-out-of-bounds read in ksmbd_compare_user() on session re-authentication Piyus Kedia
2026-12-04 15:14 ` [PATCH] ksmbd: compare the passkey lengths in ksmbd_compare_user() Piyus Kedia
2026-12-04 19:16   ` Ian Bridges
2026-12-05  2:48     ` Namjae Jeon

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=ksmbd-compare-user-passkey.20261204@iiitd.ac.in \
    --to=piyus@iiitd.ac.in \
    --cc=chenxiaosong@chenxiaosong.com \
    --cc=kapil@presceralabs.ai \
    --cc=linkinjeon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=senozhatsky@chromium.org \
    --cc=tom@talpey.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

all inboxes | Powered by JetHome®