ksmbd: store the security descriptor from a copy
Subsystems (2)
PATCH 1/1 ksmbd: store the security descriptor from a copy
Storing a security descriptor changes the buffer passed by the caller. ksmbd_vfs_set_sd_xattr() adjusts three offsets for the on-disk format in place. For SET_INFO, and for CREATE with a security descriptor context, this modifies the client's request buffer. Current callers do not reuse the descriptor after storing it, but a later reader would find incorrect offsets.
Adjust the offsets in a private copy and make the input argument const. Reject a length shorter than the descriptor header before copying it. This keeps the caller's data intact and preserves the stored format.
Add a KUnit test that checks the input stays unchanged after a successful store and after a short length is rejected.
---
This is a cleanup. Current callers do not reuse the descriptor after storing it, so the patch fixes no bug today.
Tested on ksmbd-for-next 2746a02296a7 with KASAN. Stored descriptors and security query results match the base for SET_INFO, CREATE, inheritance and default descriptors. KUnit passes with GCC and clang, and the new test detects the input changes made by the old code. smbtorture results match the base.
This patch was developed with LLM assistance.
fs/smb/server/tests/smbacl_kunit.c | 36 ++++++++++++++++++++++++++++++ fs/smb/server/vfs.c | 26 ++++++++++++++------- fs/smb/server/vfs.h | 2 +- 3 files changed, 55 insertions(+), 9 deletions(-)
fs/smb/server/tests/smbacl_kunit.c +36 -0
@@ -280,10 +280,46 @@ ksmbd_smb_check_perm_dacl_maximal_boundary_test(struct kunit *test)fput (file); } +/* +* Storing a descriptor must leave the caller's buffer unchanged. +* Check both a successful store and rejection of a short length. +*/ +static void ksmbd_vfs_set_sd_xattr_keeps_caller_sd_test (struct kunit *test) +{ +struct file *file; +struct smb_ntsd *pntsd, *orig; +int ntsd_size, rc; + + pntsd =build_boundary_ntsd (test, &test_owner_sid,FILE_READ_DATA , +FILE_WRITE_DATA , &ntsd_size); +KUNIT_ASSERT_NOT_NULL (test, pntsd); + orig =kunit_kmalloc (test, ntsd_size,GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, orig); +memcpy (orig, pntsd, ntsd_size); + + file =shmem_file_setup ("ksmbd-kunit-sd-copy" ,0 , +mk_vma_flags (VMA_NORESERVE_BIT )); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, file); + + rc =ksmbd_vfs_set_sd_xattr (NULL ,mnt_idmap (file->f_path.mnt), + &file->f_path, pntsd, ntsd_size); +KUNIT_EXPECT_EQ (test,0 , rc); +KUNIT_EXPECT_MEMEQ (test, pntsd, orig, ntsd_size); + + rc =ksmbd_vfs_set_sd_xattr (NULL ,mnt_idmap (file->f_path.mnt), + &file->f_path, pntsd, +sizeof (struct smb_ntsd ) -1 ); +KUNIT_EXPECT_EQ (test, -EINVAL , rc); +KUNIT_EXPECT_MEMEQ (test, pntsd, orig, ntsd_size); + +fput (file); +} +static struct kunit_case ksmbd_smbacl_test_cases[] = {KUNIT_CASE (ksmbd_dacl_walk_must_stop_at_declared_size),KUNIT_CASE (ksmbd_smb_check_perm_dacl_boundary_test),KUNIT_CASE (ksmbd_smb_check_perm_dacl_maximal_boundary_test), +KUNIT_CASE (ksmbd_vfs_set_sd_xattr_keeps_caller_sd_test), {} };
fs/smb/server/vfs.c +18 -8
@@ -1699,7 +1699,7 @@ static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(const struct mnt_idmint ksmbd_vfs_set_sd_xattr (struct ksmbd_conn *conn,const struct mnt_idmap *idmap,const struct path *path, -struct smb_ntsd *pntsd,int len) +const struct smb_ntsd *pntsd,int len) {int rc;struct ndr sd_ndr = {0 }, acl_ndr = {0 };
@@ -1707,6 +1707,15 @@ int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,struct xattr_smb_acl *smb_acl, *def_smb_acl =NULL ;struct dentry *dentry = path->dentry;struct inode *inode =d_inode (dentry); +struct smb_ntsd *sd; + +if (len < (int )sizeof (struct smb_ntsd )) +return -EINVAL ; + +/* Shift the offsets in a copy and leave the caller's SD as it is */ + sd =kmemdup (pntsd, len,KSMBD_DEFAULT_GFP ); +if (!sd) +return -ENOMEM ; acl.version =4 ; acl.hash_type =XATTR_SD_HASH_TYPE_SHA256 ;
@@ -1715,14 +1724,14 @@ int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,memcpy (acl.desc,"posix_acl" ,9 ); acl.desc_len =10 ; - pntsd->osidoffset = -cpu_to_le32 (le32_to_cpu (pntsd->osidoffset) +NDR_NTSD_OFFSETOF ); - pntsd->gsidoffset = -cpu_to_le32 (le32_to_cpu (pntsd->gsidoffset) +NDR_NTSD_OFFSETOF ); - pntsd->dacloffset = -cpu_to_le32 (le32_to_cpu (pntsd->dacloffset) +NDR_NTSD_OFFSETOF ); + sd->osidoffset = +cpu_to_le32 (le32_to_cpu (sd->osidoffset) +NDR_NTSD_OFFSETOF ); + sd->gsidoffset = +cpu_to_le32 (le32_to_cpu (sd->gsidoffset) +NDR_NTSD_OFFSETOF ); + sd->dacloffset = +cpu_to_le32 (le32_to_cpu (sd->dacloffset) +NDR_NTSD_OFFSETOF ); - acl.sd_buf = (char *)pntsd; + acl.sd_buf = sd; acl.sd_size = len;sha256 (acl.sd_buf, acl.sd_size, acl.hash);
@@ -1765,6 +1774,7 @@ int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,kfree (acl_ndr.data);kfree (smb_acl);kfree (def_smb_acl); +kfree (sd);return rc; }EXPORT_SYMBOL_IF_KUNIT (ksmbd_vfs_set_sd_xattr);
fs/smb/server/vfs.h +1 -1
@@ -164,7 +164,7 @@ int ksmbd_vfs_remove_sd_xattrs(const struct mnt_idmap *idmap, const struct pathint ksmbd_vfs_set_sd_xattr (struct ksmbd_conn *conn,const struct mnt_idmap *idmap,const struct path *path, -struct smb_ntsd *pntsd,int len); +const struct smb_ntsd *pntsd,int len); int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn, const struct mnt_idmap *idmap, struct dentry *dentry,
Signature · 1 line
2.47.3
Quoted 8 lines · Ian Bridges
Storing a security descriptor changes the buffer passed by the caller.
ksmbd_vfs_set_sd_xattr()adjusts three offsets for the on-disk format in place. ForSET_INFO, and for CREATE with a security descriptor context, this modifies the client's request buffer. Current callers do not reuse the descriptor after storing it, but a later reader would find incorrect offsets.
Quoted 8 lines
Adjust the offsets in a private copy and make the input argument const. Reject a length shorter than the descriptor header before copying it. This keeps the caller's data intact and preserves the stored format.
Add a KUnit test that checks the input stays unchanged after a successful store and after a short length is rejected.
Signed-off-by Ian Bridges <[email protected]>
Applied it to #ksmbd-for-next.
Thanks.