smb: client: fix uid/gid override in getattr with posix extensions
Subsystems (3)
Paulo Alcantara
maintainerPATCH v4 1/7 smb: client: fix uid/gid override in getattr with posix extensions
When mounting with 'multiuser,posix' options, cifs_getattr() overrides the server-provided uid/gid with the current process's fsuid/fsgid. This is because the condition only checks for unix extensions (tcon->unix_ext) but not posix extensions (tcon->posix_extensions).
With SMB3 POSIX extensions, the server provides real uid/gid values just like with unix extensions, so they should be preserved rather than replaced with the caller's credentials.
Add a tcon->posix_extensions check to the condition so that uid/gid from the server are properly reported in stat results.
--- fs/smb/client/inode.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fs/smb/client/inode.c +4 -4
@@ -2992,14 +2992,14 @@ int cifs_getattr(struct mnt_idmap *idmap, const struct path *path, stat->attributes |=STATX_ATTR_ENCRYPTED ;/* -* If on a multiuser mount without unix extensions or cifsacl being -* enabled, and the admin hasn't overridden them, set the ownership -* to the fsuid/fsgid of the current process. +* If on a multiuser mount without unix extensions, posix extensions +* or cifsacl being enabled, and the admin hasn't overridden them, +* set the ownership to the fsuid/fsgid of the current process. */ sbflags =cifs_sb_flags (cifs_sb);if ((sbflags &CIFS_MOUNT_MULTIUSER ) && !(sbflags &CIFS_MOUNT_CIFS_ACL ) && - !tcon->unix_ext) { + !tcon->unix_ext && !tcon->posix_extensions) {if (!(sbflags &CIFS_MOUNT_OVERR_UID )) stat->uid =current_fsuid ();if (!(sbflags &CIFS_MOUNT_OVERR_GID ))
Signature · 1 line
2.55.0
Paulo Alcantara
maintainerPATCH v4 2/7 smb: client: honor forceuid/forcegid when mapping SIDs to uid/gid
When the administrator mounts with forceuid or forcegid (uid=/gid= mount options), they expect all files to appear owned by the specified user/group. However, several code paths unconditionally called sid_to_id() to overwrite cf_uid/cf_gid with server-provided values, ignoring the administrator's explicit override:
- smb311_posix_info_to_fattr() (stat via POSIX extensions)
- cifs_posix_to_fattr() (readdir via POSIX extensions)
- parse_sec_desc() (CIFS ACL ownership mapping)
This allowed an untrusted server to dictate local file ownership even when the mount was configured to force specific uid/gid values.
Fix all three call sites to check CIFS_MOUNT_OVERR_UID and CIFS_MOUNT_OVERR_GID before calling sid_to_id(), following the same pattern already used by cifs_unix_basic_to_fattr() for unix extensions.
--- fs/smb/client/cifsacl.c | 29 ++++++++++++++++++----------- fs/smb/client/inode.c | 9 +++++++-- fs/smb/client/readdir.c | 9 +++++++-- 3 files changed, 32 insertions(+), 15 deletions(-)
fs/smb/client/cifsacl.c +18 -11
@@ -1346,6 +1346,7 @@ static int parse_sec_desc(struct cifs_sb_info *cifs_sb, {int rc =0 ;struct smb_sid *owner_sid_ptr, *group_sid_ptr; +unsigned int sbflags =cifs_sb_flags (cifs_sb);struct smb_acl *dacl_ptr;/* no need for SACL ptr */ char *end_of_acl;__u32 dacloffset, osidoffset, gsidoffset;
@@ -1364,17 +1365,21 @@ static int parse_sec_desc(struct cifs_sb_info *cifs_sb,cifs_dbg (NOISY ,"revision %d type 0x%x ooffset 0x%x goffset 0x%x sacloffset 0x%x dacloffset 0x%x\n" , pntsd->revision, pntsd->type, osidoffset, gsidoffset,le32_to_cpu (pntsd->sacloffset), dacloffset); -/* cifs_dump_mem("owner_sid: ", owner_sid_ptr, 64); */ + fattr->cf_uid = cifs_sb->ctx->linux_uid; + fattr->cf_gid = cifs_sb->ctx->linux_gid; + rc =sid_from_sd (pntsd, acl_len, osidoffset, &owner_sid_ptr);if (rc) {cifs_dbg (FYI ,"%s: Error %d parsing Owner SID\n" , __func__, rc);return rc; } - rc =sid_to_id (cifs_sb, owner_sid_ptr, fattr,SIDOWNER ); -if (rc) { -cifs_dbg (FYI ,"%s: Error %d mapping Owner SID to uid\n" , - __func__, rc); -return rc; +if (!(sbflags &CIFS_MOUNT_OVERR_UID )) { + rc =sid_to_id (cifs_sb, owner_sid_ptr, fattr,SIDOWNER ); +if (rc) { +cifs_dbg (FYI ,"%s: Error %d mapping Owner SID to uid\n" , + __func__, rc); +return rc; + } } rc =sid_from_sd (pntsd, acl_len, gsidoffset, &group_sid_ptr);
@@ -1383,11 +1388,13 @@ static int parse_sec_desc(struct cifs_sb_info *cifs_sb, __func__, rc);return rc; } - rc =sid_to_id (cifs_sb, group_sid_ptr, fattr,SIDGROUP ); -if (rc) { -cifs_dbg (FYI ,"%s: Error %d mapping Group SID to gid\n" , - __func__, rc); -return rc; +if (!(sbflags &CIFS_MOUNT_OVERR_GID )) { + rc =sid_to_id (cifs_sb, group_sid_ptr, fattr,SIDGROUP ); +if (rc) { +cifs_dbg (FYI ,"%s: Error %d mapping Group SID to gid\n" , + __func__, rc); +return rc; + } }if (dacloffset) {
fs/smb/client/inode.c +7 -2
@@ -851,6 +851,7 @@ static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr,struct smb311_posix_qinfo *info = &data->posix_fi;struct cifs_sb_info *cifs_sb =CIFS_SB (sb);struct cifs_tcon *tcon =cifs_sb_master_tcon (cifs_sb); +unsigned int sbflags =cifs_sb_flags (cifs_sb);memset (fattr,0 ,sizeof (*fattr));
@@ -895,8 +896,12 @@ static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr, fattr->cf_symlink_target = data->symlink_target; data->symlink_target =NULL ; } -sid_to_id (cifs_sb, &data->posix_owner, fattr,SIDOWNER ); -sid_to_id (cifs_sb, &data->posix_group, fattr,SIDGROUP ); + fattr->cf_uid = cifs_sb->ctx->linux_uid; + fattr->cf_gid = cifs_sb->ctx->linux_gid; +if (!(sbflags &CIFS_MOUNT_OVERR_UID )) +sid_to_id (cifs_sb, &data->posix_owner, fattr,SIDOWNER ); +if (!(sbflags &CIFS_MOUNT_OVERR_GID )) +sid_to_id (cifs_sb, &data->posix_group, fattr,SIDGROUP );cifs_dbg (FYI ,"POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n" , fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
fs/smb/client/readdir.c +7 -2
@@ -242,6 +242,7 @@ static void cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info, struct cifs_sb_info *cifs_sb) { + unsigned int sbflags = cifs_sb_flags(cifs_sb);struct smb2_posix_info_parsed parsed;posix_info_parse (info,NULL , &parsed);
@@ -281,8 +282,12 @@ cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info,le32_to_cpu (info->ReparseTag), le32_to_cpu(info->Mode)); -sid_to_id (cifs_sb, &parsed.owner, fattr,SIDOWNER ); -sid_to_id (cifs_sb, &parsed.group, fattr,SIDGROUP ); + fattr->cf_uid = cifs_sb->ctx->linux_uid; + fattr->cf_gid = cifs_sb->ctx->linux_gid; +if (!(sbflags &CIFS_MOUNT_OVERR_UID )) +sid_to_id (cifs_sb, &parsed.owner, fattr,SIDOWNER ); +if (!(sbflags &CIFS_MOUNT_OVERR_GID )) +sid_to_id (cifs_sb, &parsed.group, fattr,SIDGROUP ); }static void __dir_info_to_fattr (struct cifs_fattr *fattr,const void *info)
Signature · 1 line
2.55.0
Re: [PATCH v4 2/7] smb: client: honor forceuid/forcegid when mapping SIDs to uid/gid
Quoted 19 lines · Paulo Alcantara
When the administrator mounts with forceuid or forcegid (uid=/gid= mount options), they expect all files to appear owned by the specified user/group. However, several code paths unconditionally called
sid_to_id()to overwrite cf_uid/cf_gid with server-provided values, ignoring the administrator's explicit override:-
smb311_posix_info_to_fattr()(stat via POSIX extensions)
-cifs_posix_to_fattr()(readdir via POSIX extensions)
-parse_sec_desc()(CIFS ACL ownership mapping)This allowed an untrusted server to dictate local file ownership even when the mount was configured to force specific uid/gid values.
Fix all three call sites to check
CIFS_MOUNT_OVERR_UIDandCIFS_MOUNT_OVERR_GIDbefore callingsid_to_id(), following the same pattern already used bycifs_unix_basic_to_fattr()for unix extensions.
Quoted 8 lines
Signed-off-by Paulo Alcantara <[email protected]>Cc Namjae Jeon <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Shyam Prasad N <[email protected]>Cc Tom Talpey <[email protected]>Cc Bharath SM <[email protected]>
Thanks!
Paulo Alcantara
maintainerPATCH v4 3/7 smb: client: fix WSL reparse point uid/gid override
In reply to Paulo Alcantarawsl_to_fattr() unconditionally overwrites cf_uid/cf_gid with values from WSL extended attributes ($LXUID/$LXGID), ignoring the forceuid and forcegid mount options.
Fix this by initializing cf_uid/cf_gid to the mount defaults and gating the $LXUID/$LXGID EA parsing on forceuid/forcegid.
--- fs/smb/client/reparse.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
fs/smb/client/reparse.c +11 -5
@@ -1137,10 +1137,14 @@ static bool wsl_to_fattr(struct cifs_open_info_data *data,struct cifs_sb_info *cifs_sb, u32 tag, struct cifs_fattr *fattr) { + unsigned int sbflags = cifs_sb_flags(cifs_sb);struct smb2_file_full_ea_info *ea;bool have_xattr_dev = false;u32 next =0 ; + fattr->cf_uid = cifs_sb->ctx->linux_uid; + fattr->cf_gid = cifs_sb->ctx->linux_gid; +switch (tag) {case IO_REPARSE_TAG_LX_SYMLINK : fattr->cf_mode |=S_IFLNK ;
@@ -1177,11 +1181,13 @@ static bool wsl_to_fattr(struct cifs_open_info_data *data, nlen = ea->ea_name_length; v = (void *)((u8 *)ea->ea_data + ea->ea_name_length +1 ); -if (!strncmp (name,SMB2_WSL_XATTR_UID , nlen)) - fattr->cf_uid =wsl_make_kuid (cifs_sb, v); -else if (!strncmp (name,SMB2_WSL_XATTR_GID , nlen)) - fattr->cf_gid =wsl_make_kgid (cifs_sb, v); -else if (!strncmp (name,SMB2_WSL_XATTR_MODE , nlen)) { +if (!strncmp (name,SMB2_WSL_XATTR_UID , nlen)) { +if (!(sbflags &CIFS_MOUNT_OVERR_UID )) + fattr->cf_uid =wsl_make_kuid (cifs_sb, v); + }else if (!strncmp (name,SMB2_WSL_XATTR_GID , nlen)) { +if (!(sbflags &CIFS_MOUNT_OVERR_GID )) + fattr->cf_gid =wsl_make_kgid (cifs_sb, v); + }else if (!strncmp (name,SMB2_WSL_XATTR_MODE , nlen)) {/* File type in reparse point tag and in xattr mode must match. */ if (S_DT (fattr->cf_mode) !=S_DT (le32_to_cpu (*(__le32 *)v)))return false;
Signature · 1 line
2.55.0
Re: [PATCH v4 3/7] smb: client: fix WSL reparse point uid/gid override
Quoted 8 lines · Paulo Alcantara
wsl_to_fattr()unconditionally overwrites cf_uid/cf_gid with values from WSL extended attributes ($LXUID/$LXGID), ignoring the forceuid and forcegid mount options.Fix this by initializing cf_uid/cf_gid to the mount defaults and gating the $LXUID/$LXGID EA parsing on forceuid/forcegid.
Quoted 8 lines
Signed-off-by Paulo Alcantara <[email protected]>Cc Namjae Jeon <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Shyam Prasad N <[email protected]>Cc Tom Talpey <[email protected]>Cc Bharath SM <[email protected]>
Thanks!
Paulo Alcantara
maintainerPATCH v4 4/7 smb: client: avoid using uninitialized SIDs in cifs_posix_to_fattr()
In reply to Paulo Alcantaracifs_posix_to_fattr() ignores the return value of posix_info_parse(). When a malformed POSIX directory entry is encountered (e.g. invalid SID lengths from an untrusted server), posix_info_parse() returns -1 without populating the 'parsed' struct. The uninitialized stack memory in parsed.owner and parsed.group is then passed to sid_to_id(), which processes the garbage bytes and passes them to request_key() to construct a SID string, potentially leaking kernel stack contents to the userspace idmap daemon.
Fix this by checking the return value and skipping the SID-to-id mapping when parsing fails. The remaining fattr fields (timestamps, mode, etc.) are populated directly from the 'info' pointer so they are unaffected.
--- fs/smb/client/readdir.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
fs/smb/client/readdir.c +11 -5
@@ -244,8 +244,9 @@ cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info, {unsigned int sbflags =cifs_sb_flags (cifs_sb);struct smb2_posix_info_parsed parsed; +int rc; -posix_info_parse (info,NULL , &parsed); + rc =posix_info_parse (info,NULL , &parsed);memset (fattr,0 ,sizeof (*fattr)); fattr->cf_uniqueid =le64_to_cpu (info->Inode);
@@ -284,10 +285,15 @@ cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info, fattr->cf_uid = cifs_sb->ctx->linux_uid; fattr->cf_gid = cifs_sb->ctx->linux_gid; -if (!(sbflags &CIFS_MOUNT_OVERR_UID )) -sid_to_id (cifs_sb, &parsed.owner, fattr,SIDOWNER ); -if (!(sbflags &CIFS_MOUNT_OVERR_GID )) -sid_to_id (cifs_sb, &parsed.group, fattr,SIDGROUP ); +if (rc <0 ) { +cifs_dbg (VFS ,"%s: failed to parse SIDs: %d\n" , + __func__, rc); + }else { +if (!(sbflags &CIFS_MOUNT_OVERR_UID )) +sid_to_id (cifs_sb, &parsed.owner, fattr,SIDOWNER ); +if (!(sbflags &CIFS_MOUNT_OVERR_GID )) +sid_to_id (cifs_sb, &parsed.group, fattr,SIDGROUP ); + } }static void __dir_info_to_fattr (struct cifs_fattr *fattr,const void *info)
Signature · 1 line
2.55.0
Re: [PATCH v4 4/7] smb: client: avoid using uninitialized SIDs in cifs_posix_to_fattr()
Quoted 15 lines · Paulo Alcantara
cifs_posix_to_fattr()ignores the return value ofposix_info_parse(). When a malformed POSIX directory entry is encountered (e.g. invalid SID lengths from an untrusted server),posix_info_parse()returns -1 without populating the 'parsed' struct. The uninitialized stack memory in parsed.owner and parsed.group is then passed tosid_to_id(), which processes the garbage bytes and passes them torequest_key()to construct a SID string, potentially leaking kernel stack contents to the userspace idmap daemon.Fix this by checking the return value and skipping the SID-to-id mapping when parsing fails. The remaining fattr fields (timestamps, mode, etc.) are populated directly from the 'info' pointer so they are unaffected.
Quoted 9 lines
Signed-off-by Paulo Alcantara <[email protected]>Cc Namjae Jeon <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Shyam Prasad N <[email protected]>Cc Tom Talpey <[email protected]>Cc Bharath SM <[email protected]>
Thanks!
Paulo Alcantara
maintainerPATCH v4 5/7 smb: client: fix file type corruption in wsl_to_fattr()
In reply to Paulo AlcantaraSetting the file type in cf_mode without clearing the existing S_IFMT bits first is wrong as it corrupts the file type when cf_mode already has type bits set (e.g. S_IFREG | S_IFCHR == S_IFLNK).
Clear S_IFMT before the switch statement.
--- fs/smb/client/reparse.c | 1 + 1 file changed, 1 insertion(+)
fs/smb/client/reparse.c +1 -0
@@ -1145,6 +1145,7 @@ static bool wsl_to_fattr(struct cifs_open_info_data *data, fattr->cf_uid = cifs_sb->ctx->linux_uid; fattr->cf_gid = cifs_sb->ctx->linux_gid; + fattr->cf_mode &= ~S_IFMT ;switch (tag) {case IO_REPARSE_TAG_LX_SYMLINK : fattr->cf_mode |=S_IFLNK ;
Signature · 1 line
2.55.0
Re: [PATCH v4 5/7] smb: client: fix file type corruption in wsl_to_fattr()
Quoted 7 lines · Paulo Alcantara
Setting the file type in
cf_modewithout clearing the existingS_IFMTbits first is wrong as it corrupts the file type whencf_modealready has type bits set (e.g.S_IFREG|S_IFCHR==S_IFLNK).Clear
S_IFMTbefore the switch statement.
Quoted 8 lines
Signed-off-by Paulo Alcantara <[email protected]>Cc Namjae Jeon <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Shyam Prasad N <[email protected]>Cc Tom Talpey <[email protected]>Cc Bharath SM <[email protected]>
Thanks!
Paulo Alcantara
maintainerPATCH v4 6/7 smb: client: fix file type corruption in posix_reparse_to_fattr()
In reply to Paulo AlcantaraSetting the file type in cf_mode without clearing the existing S_IFMT bits first is wrong as it corrupts the file type when cf_mode already has type bits set (e.g. S_IFREG | S_IFCHR == S_IFLNK).
Use a local ftype variable to collect the new file type and apply it after validation succeeds, clearing S_IFMT and setting the new type in a single assignment. This avoids stripping cf_mode on malformed reparse points where the function returns false early.
--- fs/smb/client/reparse.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
fs/smb/client/reparse.c +7 -5
@@ -1212,6 +1212,7 @@ static bool posix_reparse_to_fattr(struct cifs_sb_info *cifs_sb, struct cifs_open_info_data *data) {struct reparse_nfs_data_buffer *buf = (struct reparse_nfs_data_buffer *)data->reparse.buf; +umode_t ftype;if (buf ==NULL )return true;
@@ -1227,7 +1228,7 @@ static bool posix_reparse_to_fattr(struct cifs_sb_info *cifs_sb,WARN_ON_ONCE (1 );return false; } - fattr->cf_mode |=S_IFCHR ; + ftype =S_IFCHR ; fattr->cf_rdev =reparse_mkdev (buf->DataBuffer);break ;case NFS_SPECFILE_BLK :
@@ -1235,22 +1236,23 @@ static bool posix_reparse_to_fattr(struct cifs_sb_info *cifs_sb,WARN_ON_ONCE (1 );return false; } - fattr->cf_mode |=S_IFBLK ; + ftype =S_IFBLK ; fattr->cf_rdev =reparse_mkdev (buf->DataBuffer);break ;case NFS_SPECFILE_FIFO : - fattr->cf_mode |=S_IFIFO ; + ftype =S_IFIFO ;break ;case NFS_SPECFILE_SOCK : - fattr->cf_mode |=S_IFSOCK ; + ftype =S_IFSOCK ;break ;case NFS_SPECFILE_LNK : - fattr->cf_mode |=S_IFLNK ; + ftype =S_IFLNK ;break ;default :WARN_ON_ONCE (1 );return false; } + fattr->cf_mode = (fattr->cf_mode & ~S_IFMT ) | ftype;return true; }
Signature · 1 line
2.55.0
Re: [PATCH v4 6/7] smb: client: fix file type corruption in posix_reparse_to_fattr()
Quoted 10 lines · Paulo Alcantara
Setting the file type in
cf_modewithout clearing the existingS_IFMTbits first is wrong as it corrupts the file type whencf_modealready has type bits set (e.g.S_IFREG|S_IFCHR==S_IFLNK).Use a local ftype variable to collect the new file type and apply it after validation succeeds, clearing
S_IFMTand setting the new type in a single assignment. This avoids strippingcf_modeon malformed reparse points where the function returns false early.
Quoted 8 lines
Signed-off-by Paulo Alcantara <[email protected]>Cc Namjae Jeon <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Shyam Prasad N <[email protected]>Cc Tom Talpey <[email protected]>Cc Bharath SM <[email protected]>
Thanks!
Paulo Alcantara
maintainerPATCH v4 7/7 smb: client: fix file type corruption in cifs_reparse_point_to_fattr()
In reply to Paulo AlcantaraSetting the file type in cf_mode without clearing the existing S_IFMT bits first is wrong as it corrupts the file type when cf_mode already has type bits set (e.g. S_IFREG | S_IFLNK == S_IFDIR | S_IFREG).
Clear S_IFMT before setting S_IFLNK for native and SMB1 symlinks.
--- fs/smb/client/reparse.c | 1 + 1 file changed, 1 insertion(+)
fs/smb/client/reparse.c +1 -0
@@ -1280,6 +1280,7 @@ bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,break ;case 0 :/* SMB1 symlink */ case IO_REPARSE_TAG_SYMLINK : + fattr->cf_mode &= ~S_IFMT ; fattr->cf_mode |=S_IFLNK ;break ;default :
Signature · 1 line
2.55.0
Re: [PATCH v4 7/7] smb: client: fix file type corruption in cifs_reparse_point_to_fattr()
Quoted 7 lines · Paulo Alcantara
Setting the file type in
cf_modewithout clearing the existingS_IFMTbits first is wrong as it corrupts the file type whencf_modealready has type bits set (e.g.S_IFREG|S_IFLNK==S_IFDIR|S_IFREG).Clear
S_IFMTbefore settingS_IFLNKfor native and SMB1 symlinks.
Quoted 8 lines
Signed-off-by Paulo Alcantara <[email protected]>Cc Namjae Jeon <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Shyam Prasad N <[email protected]>Cc Tom Talpey <[email protected]>Cc Bharath SM <[email protected]>
Thanks!
Quoted 13 lines · Paulo Alcantara
When mounting with 'multiuser,posix' options,
cifs_getattr()overrides the server-provided uid/gid with the current process's fsuid/fsgid. This is because the condition only checks for unix extensions (tcon->unix_ext) but not posix extensions (tcon->posix_extensions).With SMB3 POSIX extensions, the server provides real uid/gid values just like with unix extensions, so they should be preserved rather than replaced with the caller's credentials.
Add a
tcon->posix_extensionscheck to the condition so that uid/gid from the server are properly reported in stat results.
Quoted 10 lines
Reported-by Arthur Lesuisse <[email protected]>Closes https://lore.kernel.org/r/DB9P190MB2012266F6B8DECBE5D26A1798DB52@DB9P190MB2012.EURP190.PROD.OUTLOOK.COM open hereSuggested-by Arthur Lesuisse <[email protected]>Signed-off-by Paulo Alcantara <[email protected]>Cc Namjae Jeon <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Shyam Prasad N <[email protected]>Cc Tom Talpey <[email protected]>Cc Bharath SM <[email protected]>
Thanks!