Skip to contentSkip to conversation

smb: client: use unaligned reads in parse_posix_ctxt()

PATCH In review Ren Wei 2 participants 5 messages Jul 1 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Ren Wei

author
#1 lore raw

PATCH 1/3 smb: client: use unaligned reads in parse_posix_ctxt()

From: Zihan Xi <[email protected]>

The server controls create-context DataOffset, so the POSIX context data pointer may be misaligned on strict-alignment architectures. Use get_unaligned_le32() when reading nlink, reparse_tag, and mode.

Fixes 69dda3059e7a ("cifs: add SMB2_open() arg to return POSIX data")
---
 fs/smb/client/smb2pdu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/smb/client/smb2pdu.c +3 -3
index 3c7691b39377..ed782d219d55 100644
@@ -2396,9 +2396,9 @@ parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
 
 	memset(posix, 0, sizeof(*posix));
 
-	posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
-	posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
-	posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
+	posix->nlink = get_unaligned_le32(beg);
+	posix->reparse_tag = get_unaligned_le32(beg + 4);
+	posix->mode = get_unaligned_le32(beg + 8);
 
 	sid = beg + 12;
 	sid_len = posix_info_sid_size(sid, end);
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 2/3 smb: client: check posix_info_parse() in cifs_posix_to_fattr()

In reply to a message not in the archive

From: Zihan Xi <[email protected]>

posix_info_parse() returns an error without filling the output structure. Check the return value before calling sid_to_id() to avoid using uninitialized owner/group SID data.

Fixes 9934430e2178 ("SMB3.1.1: Fix ids returned in POSIX query dir")
---
 fs/smb/client/readdir.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/smb/client/readdir.c +4 -2
index 1ff77f3d1de0..df46aa919c27 100644
@@ -244,9 +244,11 @@ cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info,
 {
 	struct smb2_posix_info_parsed parsed;
 
-	posix_info_parse(info, NULL, &parsed);
-
 	memset(fattr, 0, sizeof(*fattr));
+	if (posix_info_parse(info, NULL, &parsed) < 0) {
+		cifs_dbg(VFS, "Invalid POSIX info payload\n");
+		return;
+	}
 	fattr->cf_uniqueid = le64_to_cpu(info->Inode);
 	fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
 	fattr->cf_eof = le64_to_cpu(info->EndOfFile);
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH 2/3] smb: client: check posix_info_parse() in cifs_posix_to_fattr()

Hi Ren and Zihan,

Thank you for the patch.

I have a concern regarding the placement of the error check. It seems like the check for posix_info_parse() should occur later in the function, specifically before the two lines that utilize the parsed output:

sid_to_id(cifs_sb, &parsed.owner, fattr, SIDOWNER); sid_to_id(cifs_sb, &parsed.group, fattr, SIDGROUP);

Perhaps we should use something like:

if (!posix_info_parse(info, NULL, &parsed)) {
      sid_to_id(cifs_sb, &parsed.owner, fattr, SIDOWNER);
      sid_to_id(cifs_sb, &parsed.group, fattr, SIDGROUP);
}

Could you please take a look and let me know your thoughts?

Thanks,
Steve French

Quoted 37 lines · Ren Wei

From: Zihan Xi <[email protected]>

posix_info_parse() returns an error without filling the output structure. Check the return value before calling sid_to_id() to avoid using uninitialized owner/group SID data.

Fixes 9934430e2178 ("SMB3.1.1: Fix ids returned in POSIX query dir")
---
 fs/smb/client/readdir.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/smb/client/readdir.c +4 -3
index 1ff77f3d1de0..df46aa919c27 100644
@@ -244,9 +244,11 @@ cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info,
 {
        struct smb2_posix_info_parsed parsed;
 
-       posix_info_parse(info, NULL, &parsed);
-
        memset(fattr, 0, sizeof(*fattr));
+       if (posix_info_parse(info, NULL, &parsed) < 0) {
+               cifs_dbg(VFS, "Invalid POSIX info payload\n");
+               return;
+       }
        fattr->cf_uniqueid = le64_to_cpu(info->Inode);
        fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
        fattr->cf_eof = le64_to_cpu(info->EndOfFile);
--

2.43.0

Signature · 3 lines
Thanks,

Steve
author
#4 lore raw

PATCH 3/3 smb: client: use unaligned read for POSIX info name length

In reply to a message not in the archive

From: Zihan Xi <[email protected]>

posix_info_parse() reads the trailing name length via an aligned le32 cast at beg + total_len. Owner and group SID sizes are variable, so that offset may be misaligned on strict-alignment architectures.

Fixes 349e13ad30b4 ("cifs: add smb2 POSIX info level")
---
 fs/smb/client/smb2pdu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/client/smb2pdu.c +1 -1
index ed782d219d55..43d514ee555d 100644
@@ -5450,7 +5450,7 @@ int posix_info_parse(const void *beg, const void *end,
 	/* check name len */
 	if (beg + total_len + 4 > end)
 		return -1;
-	name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
+	name_len = get_unaligned_le32((u8 *)beg + total_len);
 	if (name_len < 1 || name_len > 0xFFFF)
 		return -1;
 	total_len += 4;
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 3/3] smb: client: use unaligned read for POSIX info name length

owner sid sizes are variable but they look like always a multiple of 4 so should be aligned already, right?

See posix_info_sid_size() function

Quoted 32 lines · Ren Wei

From: Zihan Xi <[email protected]>

posix_info_parse() reads the trailing name length via an aligned le32 cast at beg + total_len. Owner and group SID sizes are variable, so that offset may be misaligned on strict-alignment architectures.

Fixes 349e13ad30b4 ("cifs: add smb2 POSIX info level")
---
 fs/smb/client/smb2pdu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/client/smb2pdu.c +1 -2
index ed782d219d55..43d514ee555d 100644
@@ -5450,7 +5450,7 @@ int posix_info_parse(const void *beg, const void *end,
        /* check name len */
        if (beg + total_len + 4 > end)
                return -1;
-       name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
+       name_len = get_unaligned_le32((u8 *)beg + total_len);
        if (name_len < 1 || name_len > 0xFFFF)
                return -1;
        total_len += 4;
--

2.43.0

Signature · 3 lines
Thanks,

Steve