mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [BUG] smb: client: slab-out-of-bounds write in SMB2_set_info_init()
@ 2026-12-05 20:14 Piyus Kedia
  2026-12-05 20:14 ` [PATCH] smb: client: check the data length " Piyus Kedia
  0 siblings, 1 reply; 2+ messages in thread
From: Piyus Kedia @ 2026-12-05 20:14 UTC (permalink / raw)
  To: Paulo Alcantara, Namjae Jeon, linux-cifs
  Cc: Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM,
	samba-technical, linux-kernel, Kapil Vaswani

Hi,

SMB2_set_info_init() copies the first data element of a SET_INFO
request into the request buffer, after the fixed part of the request,
without comparing its length with the buffer:

  fs/smb/client/smb2pdu.c:6000 (SMB2_set_info_init)
      req->BufferLength = cpu_to_le32(*size);

      memcpy(req->Buffer, *data, *size);

The buffer comes from cifs_buf_get() (smb2_plain_req_init(),
smb2pdu.c:5985) and is CIFSMaxBufSize + MAX_SMB2_HDR_SIZE bytes, 16588
by default; req->Buffer is at offset 96, so it holds 16492 bytes.  Two
callers can pass more.

1. smb2_set_ea(), for setxattr() of a user.* attribute.
   cifs_xattr_set() lets a value of up to CIFSMaxBufSize (16384) bytes
   through (xattr.c:127) and smb2_set_ea() makes it a
   FILE_FULL_EA_INFORMATION of 8 + name + 1 + value bytes
   (smb2ops.c:1314): up to 16644, since the VFS allows 255 bytes for
   the whole name and os2.* is treated like user.*.  The one limit on
   that is a check meant to keep the file's EAs readable, which first
   asks the server how many bytes of EAs the file has:

     fs/smb/client/smb2ops.c:1270 (smb2_set_ea)
             used_len = le32_to_cpu(rsp->OutputBufferLength);
     ...
     fs/smb/client/smb2ops.c:1280
         if (CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
            MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
            used_len + ea_name_len + ea_value_len + 1) {
             rc = -ENOSPC;

   used_len is an int, and nothing holds the server's value to the
   data that came with it: smb2_get_data_area_len() logs a length that
   is negative as an int ("invalid data area (off=72 len=-16000)") and
   counts no data area (smb2misc.c:438-444), and smb2_check_message()
   accepts a response longer than that (smb2misc.c:339).  With
   OutputBufferLength 0xFFFFC180 (-16000), a 250-character name and a
   16384-byte value the right-hand side is 635, the check passes, and:

     BUG: KASAN: slab-out-of-bounds in SMB2_set_info_init (fs/smb/client/smb2pdu.c:6000)
     Write of size 16643 at addr ffff8880436b0060 by task poc/9452
      __asan_memcpy (mm/kasan/shadow.c:106)
      SMB2_set_info_init (fs/smb/client/smb2pdu.c:6000)
      smb2_set_ea (fs/smb/client/smb2ops.c:1329)
      cifs_xattr_set (fs/smb/client/xattr.c:156)
      __vfs_setxattr (fs/xattr.c:223)
      ...
      __x64_sys_setxattr (fs/xattr.c:768 fs/xattr.c:764 fs/xattr.c:764)

     Allocated by task 9452:
      ...
      cifs_buf_get (fs/smb/client/misc.c:202)
      __smb2_plain_req_init (fs/smb/client/smb2pdu.c:591)
      SMB2_set_info_init (fs/smb/client/smb2pdu.c:625 fs/smb/client/smb2pdu.c:5985)
      smb2_set_ea (fs/smb/client/smb2ops.c:1329)

     The buggy address belongs to the object at ffff8880436b0000
      which belongs to the cache cifs_request of size 16588
     The buggy address is located 96 bytes inside of
      allocated 16588-byte region [ffff8880436b0000, ffff8880436b40cc)

   151 bytes past the object.  The check is the only bound: it runs
   whenever a value is set (query_all_EAs is set in every SMB2
   operations table next to set_EA), with used_len 0 if the query
   fails, and with a used_len between 0 and 2^31 it does keep the EA
   (at most 15132 bytes) inside the buffer.

2. set_smb2_acl() -> SMB2_set_acl() -> send_set_info(), for chmod() on
   a cifsacl or modefromsid mount.  As on SMB1, where we report the
   same thing in CIFSSMBSetCIFSACL() separately ("[BUG] smb: client:
   slab-out-of-bounds write in CIFSSMBSetCIFSACL() on chmod"),
   id_mode_to_cifs_acl() builds the descriptor from the server's:
   set_chmod_dacl() keeps the server's ACEs other than those for the
   owner, the group and three well-known SIDs, and adds up to five for
   the new mode (cifsacl.c:1286-1329), so it goes back longer than it
   came in.  On SMB2 the server's descriptor alone may already be too
   long: a response of up to CIFSMaxBufSize + MAX_SMB2_HDR_SIZE bytes
   is accepted (connect.c:1200), which leaves 16516 bytes for it.
   With a well-formed 16504-byte descriptor (an owner and a group SID
   of 15 sub-authorities and 215 ACCESS_ALLOWED ACEs of 76 bytes)
   chmod(file, 0640) adds 172 bytes:

     BUG: KASAN: slab-out-of-bounds in SMB2_set_info_init (fs/smb/client/smb2pdu.c:6000)
     Write of size 16676 at addr ffff888043750060 by task poc/9458
      __asan_memcpy (mm/kasan/shadow.c:106)
      SMB2_set_info_init (fs/smb/client/smb2pdu.c:6000)
      send_set_info (fs/smb/client/smb2pdu.c:6062)
      SMB2_set_acl (fs/smb/client/smb2pdu.c:6144)
      set_smb2_acl (fs/smb/client/smb2ops.c:3479)
      id_mode_to_cifs_acl (fs/smb/client/cifsacl.c:1903)
      cifs_setattr (fs/smb/client/inode.c:3490 fs/smb/client/inode.c:3592)
      notify_change (fs/attr.c:556)
      chmod_common (fs/open.c:686)
     ...
     The buggy address is located 96 bytes inside of
      allocated 16588-byte region [ffff888043750000, ffff8880437540cc)

   184 bytes past the object.

KASAN skips a copy it reports (__asan_memcpy() returns without
copying), and on SMB2 nothing else stops the request: in both cases our
server received the SET_INFO, with BufferLength 16643 and 16676, and
setxattr() and chmod() returned 0.

The other callers of SMB2_set_info_init() pass data of a fixed size
(disposition, end of file, basic, rename and link information - the
new name goes in an iov of its own - and the set-info passthrough's 8
bytes), and setxattr() of system.cifs_acl and the related names passes
a descriptor of at most CIFSMaxBufSize bytes (xattr.c:127), which fits.

How it is reached.  The reproducer runs a minimal SMB 3.0 server on
loopback (NTLMSSP accepted unchecked) and mounts it.  For the EA, the
server answers the FILE_FULL_EA_INFORMATION query with
OutputBufferLength 0xFFFFC180 over 16 bytes of data, and the program
sets user.<250 x 'a'> to a 16384-byte value; for the descriptor it
mounts with cifsacl and calls chmod() on a file.  The controls: when
the server reports the 16 bytes of EAs it really sends, the same
setxattr() fails with ENOSPC from the check above and no SET_INFO is
built; with 10 ACEs (a 924-byte descriptor) the chmod() works - the
server receives a 1096-byte descriptor and chmod() returns 0.  Neither
gives a report.

Impact - who can trigger it:

  - The EA write needs two parties.  The server has to answer the EA
    query with an OutputBufferLength of 2^31 or more that makes the sum
    wrap; an honest server does not, but one value covers every EA large
    enough to overflow (-16000 wraps for anything from 16000 to 31124
    bytes).  And someone on the client has to set a user.* attribute
    whose name and value come to more than 16484 bytes - with the
    16384-byte maximum value, a name of 100 characters or more.  That
    needs write permission to the file as the client sees it, no
    privilege, on any SMB2/3 mount that has not turned user xattrs off
    (nouser_xattr).  The bytes written past the buffer are the end of
    that caller's value.

  - The descriptor write needs a server descriptor that nearly fills a
    response, which need not be malformed - ours is not - and chmod(2)
    of that file by its owner (as the client sees it) on an SMB2/3
    mount with cifsacl or modefromsid.  As on SMB1, the bytes written
    past the buffer are the end of the descriptor the client built: the
    new ACEs in our runs, or the server's inherited ACEs, which
    set_chmod_dacl() puts after them.  We have not tried a Windows or
    Samba server and do not know how one answers for an ACL that size.

  - Mounting needs CAP_SYS_ADMIN in the initial user namespace.  We ran
    everything as root.

  - What the write does: without KASAN the copy is done and the request
    is sent.  With the default CIFSMaxBufSize the EA can end at most
    152 bytes past the object (from the code: a 251-character os2.*
    name); we measured 151, and 184 for the descriptor.  We did not
    establish what lies past a cifs_request object on a production
    kernel, which depends on the slab layout and so on CIFSMaxBufSize.

Seen on 7.4.0-rc5, where the fuzzer's server sent the same EA answer
and the same 16643-byte write was reported at the same lines (twice);
the descriptor path we found while writing this up.  REPRODUCED ON
ksmbd-for-next (d70570cf8003, 2026-12-04), whose fs/smb/client is
identical to mainline (c3500c5ef648, 2026-12-04), with KASAN: the EA
gave the first report above in 3 of 3 boots (and the hunt's own program
in 1 of 1), the descriptor the second in 3 of 3, and the controls none.
SMB2_set_info_init(), smb2_set_ea() and set_smb2_acl() are the same in
cifs-next (9b62aa64ed00, 2026-12-03).

History.  SET_INFO data first went into a 448-byte small buffer, and
the first caller with data of a variable size, 366ed846df60 ("cifs: Use
smb 2 - 3 and cifsacl mount options setacl function"), v4.13, overflowed
it with security descriptors; f46ecbd97f50 ("cifs: Fix
slab-out-of-bounds in send_set_info() on SMB2 ACE setting"), v4.18,
moved SET_INFO to the large buffer, without a bound.  smb2_set_ea()
came in 5517554e4313 ("cifs: Add support for writing attributes on
SMB2+"), v4.14, with no limit; 63d37fb4ce5a ("CIFS: fix max ea value
size"), v5.4, capped the value at CIFSMaxBufSize for this copy, but not
the name and the header; the check above came in 85db6b7ae65f ("cifs:
make sure we do not overflow the max EA buffer size"), v5.6, for the
EAs' sake, and bounds the copy only while used_len is not negative.
f5065508897a ("cifs: Retain old ACEs when converting between mode bits
and ACL."), v5.12, made the chmod descriptor grow.  33eae65c6f49 ("smb:
client: fix OOB in SMB2_query_info_init()"), v6.7, fixed the same kind
of overflow in SMB2_query_info_init() by checking the length there.

A patch follows in reply to this mail.  Like 33eae65c6f49, it checks
the length where the copy is, so that it holds for both callers: data
longer than CIFSMaxBufSize is refused with -E2BIG before the buffer is
allocated.  CIFSMaxBufSize rather than the 16492 bytes that fit leaves
room for the padding smb2_set_next_command() writes into the buffer of
an encrypted compound (smb2_set_ea()'s is one), and it is the limit
setxattr() already puts on a descriptor and that SMB1 has
(SendReceive()).  A descriptor of 16385 to 16492 bytes, which chmod()
on SMB2 could send before, is refused too.  smb2_set_ea()'s check
still accepts a negative used_len, but with the patch that can only let
through an EA that fits the request, so we left it alone.  On
ksmbd-for-next, same config and image, the patch the only change
between the two builds:

  without the patch   the reports above; setxattr() and chmod()
                      return 0
  with the patch      no report in 6 boots (the EA 3 times, the
                      descriptor 3 times); no SET_INFO is sent and
                      setxattr() and chmod() fail with E2BIG
  both                the controls: setxattr() fails with ENOSPC, the
                      924-byte-descriptor chmod() returns 0 and the
                      server receives the 1096-byte descriptor

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 a
minimal SMB 3.0 server and the mount in one program over loopback, and
we are happy to send it on request.

Kapil Vaswani
Piyus Kedia

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH] smb: client: check the data length in SMB2_set_info_init()
  2026-12-05 20:14 [BUG] smb: client: slab-out-of-bounds write in SMB2_set_info_init() Piyus Kedia
@ 2026-12-05 20:14 ` Piyus Kedia
  0 siblings, 0 replies; 2+ messages in thread
From: Piyus Kedia @ 2026-12-05 20:14 UTC (permalink / raw)
  To: Paulo Alcantara, Namjae Jeon, linux-cifs
  Cc: Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM,
	samba-technical, linux-kernel, Kapil Vaswani

SMB2_set_info_init() copies data[0] into the request buffer after the
fixed part of the request (offset 96) without comparing its length with
the buffer, a cifs_buf_get() one of CIFSMaxBufSize + MAX_SMB2_HDR_SIZE
bytes (16588 by default).  It has been so since the first caller with
data of a variable size, commit 366ed846df60 ("cifs: Use smb 2 - 3 and
cifsacl mount options setacl function"); commit f46ecbd97f50 ("cifs:
Fix slab-out-of-bounds in send_set_info() on SMB2 ACE setting") moved
SET_INFO from the small buffer to this one, without a bound.  Two
callers can pass more than it holds:

 - smb2_set_ea() builds a FILE_FULL_EA_INFORMATION of up to 16644 bytes
   for a setxattr() of a user.* attribute.  Its only limit is a check
   that adds the OutputBufferLength of the server's
   FILE_FULL_EA_INFORMATION response, as an int, to the size of the new
   EA; a server that answers 0xFFFFC180 makes the sum wrap, and a
   16384-byte value with a 250-character name is copied:

     BUG: KASAN: slab-out-of-bounds in SMB2_set_info_init (fs/smb/client/smb2pdu.c:6000)
     Write of size 16643 at addr ffff8880436b0060 by task poc/9452
      __asan_memcpy (mm/kasan/shadow.c:106)
      SMB2_set_info_init (fs/smb/client/smb2pdu.c:6000)
      smb2_set_ea (fs/smb/client/smb2ops.c:1329)
      cifs_xattr_set (fs/smb/client/xattr.c:156)
     The buggy address belongs to the object at ffff8880436b0000
      which belongs to the cache cifs_request of size 16588
     The buggy address is located 96 bytes inside of
      allocated 16588-byte region [ffff8880436b0000, ffff8880436b40cc)

 - set_smb2_acl() sends the security descriptor id_mode_to_cifs_acl()
   built for chmod() on a cifsacl or modefromsid mount: most of the
   server's ACEs plus up to five new ones, which can be longer than the
   buffer holds (a 16504-byte descriptor from the server made chmod()
   copy 16676 bytes).

Check the length where the copy is, as commit 33eae65c6f49 ("smb:
client: fix OOB in SMB2_query_info_init()") did for
SMB2_query_info_init(), and refuse data longer than CIFSMaxBufSize with
-E2BIG, which setxattr() and chmod() then return.  CIFSMaxBufSize
rather than the 16492 bytes that fit leaves room for the padding
smb2_set_next_command() writes into the buffer of an encrypted
compound, and is the limit setxattr() already puts on a security
descriptor (MAX_EA_VALUE_SIZE) and SendReceive() on SMB1; a descriptor
of up to 108 bytes more, which chmod() on SMB2 could send before, is
refused as well.  The other callers pass fixed-size information of a
few dozen bytes.  smb2_set_ea()'s check is left as it is: with this, a
negative OutputBufferLength can only let through an EA that fits.

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

Fixes: 366ed846df60 ("cifs: Use smb 2 - 3 and cifsacl mount options setacl function")
Closes: https://lore.kernel.org/r/cifs-set-info-overflow.20261205@iiitd.ac.in/
Cc: stable@vger.kernel.org
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
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 runs a minimal SMB 3.0 server on
loopback and mounts it:

  before   ea: the server answers the EA query with OutputBufferLength
           0xFFFFC180, setxattr(f, user.<250 x 'a'>, 16384 bytes): the
           report above in 3 of 3 boots; acl: cifsacl, a 16504-byte
           descriptor, chmod(f, 0640): a 16676-byte write in 3 of 3;
           the server receives both SET_INFOs, setxattr() and chmod()
           return 0
  after    no report in 6 boots (ea 3 times, acl 3 times); no
           SET_INFO is sent and setxattr() and chmod() fail with E2BIG
  both     the server reporting its real 16 bytes of EAs: setxattr()
           fails with ENOSPC; a 924-byte descriptor: chmod() returns 0
           and the server receives the 1096-byte descriptor

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

 fs/smb/client/smb2pdu.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/fs/smb/client/smb2pdu.c b/fs/smb/client/smb2pdu.c
index fd7499c79664..9ed88b034ad7 100644
--- a/fs/smb/client/smb2pdu.c
+++ b/fs/smb/client/smb2pdu.c
@@ -5982,6 +5982,14 @@ SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
 	unsigned int i, total_len;
 	int rc;
 
+	/*
+	 * data[0] is copied into the request buffer, which holds
+	 * CIFSMaxBufSize bytes beyond the largest SMB2 header; the other
+	 * elements are sent from where they are.
+	 */
+	if (*size > CIFSMaxBufSize)
+		return -E2BIG;
+
 	rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
 				 (void **) &req, &total_len);
 	if (rc)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-05 20:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-05 20:14 [BUG] smb: client: slab-out-of-bounds write in SMB2_set_info_init() Piyus Kedia
2026-12-05 20:14 ` [PATCH] smb: client: check the data length " Piyus Kedia

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®