Skip to contentSkip to conversation

ksmbd: decode and honor DOS attribute validity flags

PATCH Applied koraynilay 3 participants 6 messages Nov 15 – Nov 16

Subsystems (2)
lore
Share & tools
lore

koraynilay

author
#0 lore raw

PATCH v3 1/2 ksmbd: decode and honor DOS attribute validity flags

Currently, when setting file attributes via FSCTL_SET_SPARSE, FSCTL_SET_COMPRESSION, or FileBasicInformation, ksmbd hardcodes flags = 0x51 (XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | XATTR_DOSINFO_ITIME). This injects unrequested creation time and itime into user.DOSATTRIB on disk, even though the client only requested an attribute modification.

Furthermore, ndr_decode_dos_attr() discards validity flags on read by reading them into NULL. Because of this, modifying an existing file via FSCTL_SET_SPARSE or FSCTL_SET_COMPRESSION stripped flags down to 0x01 (XATTR_DOSINFO_ATTRIB) while leaving the creation timestamp on disk. Conversely, on a file with only XATTR_DOSINFO_ATTRIB set (and with create_time == 0 on disk), ksmbd treats the 0 as a valid timestamp and reports NTTIME(0) instead of falling back to the underlying filesystem's btime.

Fix this by:
1. decoding da->flags in ndr_decode_dos_attr(), which will get carried over when writing the xattr back;
2. updating create_time only if XATTR_DOSINFO_CREATE_TIME is set or if da.create_time > 0 (to retain stored timestamps on files written by older ksmbd versions), falling back to filesystem btime when zero (in smb2_update_xattrs() and ksmbd_vfs_fill_dentry_attrs());
3. setting only XATTR_DOSINFO_ATTRIB and the requested attributes instead of injecting timestamps by setting da.flags = 0 on new xattrs (in fsctl_set_sparse(), __ksmbd_vfs_set_compression() and set_file_basic_info());
4. self-healing damaged xattrs by setting XATTR_DOSINFO_CREATE_TIME when rewriting an existing xattr that has da.create_time > 0 (in fsctl_set_sparse(), __ksmbd_vfs_set_compression() and set_file_basic_info()).

Finally, keep reading DOS attributes unconditionally to match the behaviour in Samba.

Reproducer, with /tmp as the local filesystem folder exported as [test] in ksmbd.conf and /mnt as the smb folder mounted with mount -t cifs:

mount -t cifs //127.0.0.1/test /mnt -o username=user,password=pass

test with sparse (for compression, use chattr +c instead of fallocate):

echo data > /tmp/sparse.txt
chmod 777 /tmp/sparse.txt

stat -c 'Local btime (%n): %w' /tmp/sparse.txt getfattr -n user.DOSATTRIB -e hex /tmp/sparse.txt

  # punching hole via CIFS triggers FSCTL_SET_SPARSE
  fallocate -p -o 0 -l 4 /mnt/sparse.txt
  getfattr -n user.DOSATTRIB -e hex /tmp/sparse.txt

stat -c 'CIFS btime (%n): %w' /mnt/sparse.txt smbclient //127.0.0.1/test -U user%pass -c 'allinfo sparse.txt'

  # evict cache so btime is read fresh again
  umount /mnt
  mount -t cifs //127.0.0.1/test /mnt -o username=user,password=pass
  stat -c 'CIFS btime (%n):  %w' /mnt/sparse.txt

which prints:

Local btime (/tmp/sparse.txt): 2026-11-13 21:13:27.167926440 +0000 /tmp/sparse.txt: user.DOSATTRIB: No such attribute

# file: tmp/sparse.txt user.DOSATTRIB=0x000004000400000051000000200200000000000000000000209542d3b373dd01

     0x51 = 0x1 | 0x10 | 0x40 -----^^         |      unrequested --^^^^^^^^^^^^^^^^
   (ATTRIB | CREATE_TIME | ITIME)             |   creation time = btime
                                     SPARSE --^

CIFS btime (/mnt/sparse.txt): 2026-11-13 21:13:27.167926400 +0000 [...]
create_time: Fri Nov 13 21:13:27 2026 UTC
[...]
CIFS btime (/mnt/sparse.txt): 2026-11-13 21:13:27.167926400 +0000

test with setmode:

echo data > /tmp/attrib.txt
chmod 777 /tmp/attrib.txt

stat -c 'Local btime (%n): %w' /tmp/attrib.txt getfattr -n user.DOSATTRIB -e hex /tmp/attrib.txt

  # modify attribute via CIFS
  smbclient //127.0.0.1/test -U user%pass -c 'setmode attrib.txt +r'
  getfattr -n user.DOSATTRIB -e hex /tmp/attrib.txt

stat -c 'CIFS btime (%n): %w' /mnt/attrib.txt smbclient //127.0.0.1/test -U user%pass -c 'allinfo attrib.txt'

  # evict cache so btime is read fresh again
  umount /mnt
  mount -t cifs //127.0.0.1/test /mnt -o username=user,password=pass
  stat -c 'CIFS btime (%n):  %w' /mnt/attrib.txt

which prints:

Local btime (/tmp/attrib.txt): 2026-11-13 21:13:27.195647069 +0000 /tmp/attrib.txt: user.DOSATTRIB: No such attribute

# file: tmp/attrib.txt user.DOSATTRIB=0x000004000400000051000000210000000000000000000000f6cf46d3b373dd01

     0x51 = 0x1 | 0x10 | 0x40 -----^^       |        unrequested --^^^^^^^^^^^^^^^^

(ATTRIB | CREATE_TIME | ITIME) | creation time = btime
READONLY --^
CIFS btime (/mnt/attrib.txt): 2026-11-13 21:13:27.195647000 +0000 [...]
create_time: Fri Nov 13 21:13:27 2026 UTC
[...]
CIFS btime (/mnt/attrib.txt): 2026-11-13 21:13:27.195647000 +0000

After this patch, the outputs should be:

sparse:

Local btime (/tmp/sparse.txt): 2026-11-13 21:20:25.856425751 +0000 /tmp/sparse.txt: user.DOSATTRIB: No such attribute
# file: tmp/sparse.txt user.DOSATTRIB=0x0000040004000000010000002002000000000000000000000000000000000000

          0x01 = 0x1 (ATTRIB) -----^^         | no creation time --^^^^^^^^^^^^^^^^
                                     SPARSE --^

CIFS btime (/mnt/sparse.txt): 2026-11-13 21:20:25.856425700 +0000
create_time: Fri Nov 13 21:20:26 2026 UTC
CIFS btime (/mnt/sparse.txt): 2026-11-13 21:20:25.856425700 +0000

setmode:

Local btime (/tmp/attrib.txt): 2026-11-13 21:20:25.976415893 +0000 /tmp/attrib.txt: user.DOSATTRIB: No such attribute
# file: tmp/attrib.txt user.DOSATTRIB=0x0000040004000000010000002100000000000000000000000000000000000000

          0x01 = 0x1 (ATTRIB) -----^^       |   no creation time --^^^^^^^^^^^^^^^^
                                 READONLY --^

CIFS btime (/mnt/attrib.txt): 2026-11-13 21:20:25.976415800 +0000
create_time: Fri Nov 13 21:20:26 2026 UTC
CIFS btime (/mnt/attrib.txt): 2026-11-13 21:20:25.976415800 +0000

Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3") # ndr decode/basic info
Fixes f44158485826 ("cifsd: add file operations") # ksmbd_vfs_fill_dentry_attrs
Fixes 159e727f763d ("ksmbd: persist FSCTL_SET_SPARSE state") # sparse
Fixes 0fb327626ad9 ("ksmbd: support file compression attributes") # compression
Assisted-by Gemini:3.8-flash-high antigravity-cli-1.5.3

---
v3 changes (based on v2 feedback):
- keep DOS attribute reading unconditional to match Samba's behavior;
- add backwards-compatibility fallback (da.create_time > 0) to preserve creation times on files modified by older ksmbd writers;
- restore XATTR_DOSINFO_CREATE_TIME on write when rewriting an existing xattr with da.create_time > 0 to self-heal damaged xattrs;
- remove itime validity checks;
- split xattr version 3 field decoding out into a separate patch [2/2] as IMO they were out of place here.

v2 changes:
- Check XATTR_DOSINFO_ATTRIB on read in smb2_update_xattrs() and ksmbd_vfs_fill_dentry_attrs() to prevent overwriting in-memory attributes with 0 when an xattr lacks DOS attributes (found by sashiko[2]);
- unconditionally preserve current attributes and set XATTR_DOSINFO_ATTRIB in set_file_basic_info() so setting CreationTime alone does not write zeroed attributes to disk (found by sashiko[2]);
- decode version 3 fields (ea_size, size, alloc_size, change_time) in ndr_decode_dos_attr() instead of reading into NULL so preserving flags on version 3 xattrs retains their values (found by sashiko[2]).

This should be (hopefully) the last of the fixes needed to implement ptime[1]. Thanks for testing all of them and merging them <3

Best,
koraynilay

[1]: https://lore.kernel.org/all/[email protected]/T/#t
[2]: https://sashiko.dev/#/patchset/20261113221610.392113-1-koray.fra%40gmail.com

 fs/smb/server/ndr.c     |  2 +-
 fs/smb/server/smb2pdu.c | 43 +++++++++++++++++++++++++++++++----------
 fs/smb/server/vfs.c     | 21 +++++++++++++++-----
 3 files changed, 50 insertions(+), 16 deletions(-)
fs/smb/server/ndr.c +1 -1
index 7e546c22e284..b5190528340f 100644
@@ -256,7 +256,7 @@ int ndr_decode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da)
 		return -EINVAL;
 	}
 
-	ret = ndr_read_int32(n, NULL);
+	ret = ndr_read_int32(n, &da->flags);
 	if (ret)
 		return ret;
 
fs/smb/server/smb2pdu.c +33 -10

62 lines not on this page · raw message

fs/smb/server/vfs.c +16 -5
index 09dad151360c..da13f13f8980 100644
@@ -1936,7 +1936,13 @@ int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work,
 		rc = ksmbd_vfs_get_dos_attrib_xattr(idmap, dentry, &da);
 		if (rc > 0) {
 			ksmbd_kstat->file_attributes = cpu_to_le32(da.attr);
-			ksmbd_kstat->create_time = da.create_time;
+			/*
+			 * Older ksmbd writers cleared XATTR_DOSINFO_CREATE_TIME on sparse
+			 * or compression updates. Fall back to using a non-zero stored time
+			 * for backwards compatibility.
+			 */
+			if ((da.flags & XATTR_DOSINFO_CREATE_TIME) || da.create_time > 0)
+				ksmbd_kstat->create_time = da.create_time;
 		} else {
 			ksmbd_debug(VFS, "fail to load dos attribute.\n");
 		}
@@ -2481,10 +2487,15 @@ static int __ksmbd_vfs_set_compression(struct ksmbd_work *work,
 		rc = ksmbd_vfs_get_dos_attrib_xattr(idmap, dentry, &da);
 		if (rc <= 0) {
 			da.version = 4;
-			da.itime = fp->itime;
-			da.create_time = fp->create_time;
-			da.flags = XATTR_DOSINFO_CREATE_TIME |
-				XATTR_DOSINFO_ITIME;
+			da.flags = 0;
+		} else {
+			/*
+			 * Heal the xattr damaged by older ksmbd writers which
+			 * wrongly cleared XATTR_DOSINFO_CREATE_TIME on compression
+			 * updates.
+			 */
+			if (da.create_time > 0)
+				da.flags |= XATTR_DOSINFO_CREATE_TIME;
 		}
 
 		da.attr = le32_to_cpu(fp->f_ci->m_fattr);

base-commit: 3bc1fb9c3b20cd478c83496ab16addecb401770d
prerequisite-patch-id: 049ae0eba275b5be35a238833107e0b251a55aa2

Signature · 1 line
2.56.0
author
#1 lore raw

PATCH v3 2/2 ksmbd: decode version 3 fields in ndr_decode_dos_attr()

When decoding a version 3 user.DOSATTRIB xattr, ndr_decode_dos_attr() discards ea_size, size, alloc_size, and change_time into NULL. When the xattr is subsequently rewritten, ndr_encode_dos_attr() writes zeros for these fields, clobbering their existing values on disk.

Fix this by decoding ea_size, size, alloc_size, and change_time into struct xattr_dos_attrib so their values are preserved.

Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Assisted-by Gemini:3.8-flash-high antigravity-cli-1.5.7

---
v3 changes:
- split out from v2;

 fs/smb/server/ndr.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/smb/server/ndr.c +4 -4
index b5190528340f..1376d5a59fc8 100644
@@ -271,15 +271,15 @@ int ndr_decode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da)
 
 		ret = ndr_read_int64(n, &da->create_time);
 	} else {
-		ret = ndr_read_int32(n, NULL);
+		ret = ndr_read_int32(n, &da->ea_size);
 		if (ret)
 			return ret;
 
-		ret = ndr_read_int64(n, NULL);
+		ret = ndr_read_int64(n, &da->size);
 		if (ret)
 			return ret;
 
-		ret = ndr_read_int64(n, NULL);
+		ret = ndr_read_int64(n, &da->alloc_size);
 		if (ret)
 			return ret;
 
@@ -287,7 +287,7 @@ int ndr_decode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da)
 		if (ret)
 			return ret;
 
-		ret = ndr_read_int64(n, NULL);
+		ret = ndr_read_int64(n, &da->change_time);
 	}
 
 	return ret;
Signature · 1 line
2.56.0
Reviewed-by Tested-by on v3 2/2
#2 lore raw

Re: [PATCH v3 2/2] ksmbd: decode version 3 fields in ndr_decode_dos_attr()

Hi,

Quoted 4 lines · koraynilay

When decoding a version 3 user.DOSATTRIB xattr, ndr_decode_dos_attr() discards ea_size, size, alloc_size, and change_time into NULL. When the xattr is subsequently rewritten, ndr_encode_dos_attr() writes zeros for these fields, clobbering their existing values on disk.

I tested this on top of 1/2. A version 3 xattr keeps its flags and its size fields when ksmbd rewrites it. Without the series the rewrite cleared the creation time flag and zeroed the sizes. smbtorture gives the same results as without the series, and KASAN reports nothing.

maintainer Applied
#3 lore raw

Re: [PATCH v3 2/2] ksmbd: decode version 3 fields in ndr_decode_dos_attr()

Quoted 9 lines · Ian Bridges

Hi,

Quoted 4 lines · koraynilay

When decoding a version 3 user.DOSATTRIB xattr, ndr_decode_dos_attr() discards ea_size, size, alloc_size, and change_time into NULL. When the xattr is subsequently rewritten, ndr_encode_dos_attr() writes zeros for these fields, clobbering their existing values on disk.

Quoted 7 lines

I tested this on top of 1/2. A version 3 xattr keeps its flags and its size fields when ksmbd rewrites it. Without the series the rewrite cleared the creation time flag and zeroed the sizes. smbtorture gives the same results as without the series, and KASAN reports nothing.

Applied it to #ksmbd-for-next.
Thanks!

Reviewed-by Tested-by on v3 1/2
#4 lore raw
In reply to koraynilay

Hi,

Quoted 6 lines · koraynilay

Currently, when setting file attributes via FSCTL_SET_SPARSE, FSCTL_SET_COMPRESSION, or FileBasicInformation, ksmbd hardcodes flags = 0x51 (XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | XATTR_DOSINFO_ITIME). This injects unrequested creation time and itime into user.DOSATTRIB on disk, even though the client only requested an attribute modification.

I tested both patches with the server code of ksmbd-for-next (3bc1fb9c3b20). Files whose xattr older ksmbd left without the creation time flag now report their stored creation time and attributes, and a rewrite sets the flag again. A file created over SMB keeps its flags and itime when it is made sparse or compressed. A creation time set through a second handle survives a later sparse request through the first. smbtorture gives the same results as without the series, and KASAN reports nothing.

maintainer Applied
#5 lore raw
Quoted 20 lines · Ian Bridges

Hi,

Quoted 6 lines · koraynilay

Currently, when setting file attributes via FSCTL_SET_SPARSE, FSCTL_SET_COMPRESSION, or FileBasicInformation, ksmbd hardcodes flags = 0x51 (XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | XATTR_DOSINFO_ITIME). This injects unrequested creation time and itime into user.DOSATTRIB on disk, even though the client only requested an attribute modification.

I tested both patches with the server code of ksmbd-for-next (3bc1fb9c3b20). Files whose xattr older ksmbd left without the creation time flag now report their stored creation time and attributes, and a rewrite sets the flag again. A file created over SMB keeps its flags and itime when it is made sparse or compressed. A creation time set through a second handle survives a later sparse request through the first. smbtorture gives the same results as without the series, and KASAN reports nothing.

Quoted 2 lines

Applied it to #ksmbd-for-next.
Thanks!