Skip to contentSkip to conversation

xfs: xattr: replace strncpy and check for truncation

PATCH Superseded Justin Stitt 3 participants 5 messages Apr 5, 2024 – Apr 10, 2024 XFS filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: xattr: replace strncpy and check for truncation

strncpy is deprecated and as such we should prefer less ambiguous and more robust string interfaces [1].

There's a lot of manual memory management to get a prefix and name into a string. Let's use an easier to understand and more robust interface in scnprintf() to accomplish the same task while enabling us to check for possible truncation, resulting in a soft warning.

---
Split from https://lore.kernel.org/all/[email protected]/
with feedback from Christoph H.

---
 fs/xfs/xfs_xattr.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
fs/xfs/xfs_xattr.c +11 -5
index 364104e1b38a..bc7246eaebdd 100644
@@ -206,6 +206,7 @@ __xfs_xattr_put_listent(
 {
 	char *offset;
 	int arraytop;
+	size_t combined_len, actual_len;
 
 	if (context->count < 0 || context->seen_enough)
 		return;
@@ -220,11 +221,16 @@ __xfs_xattr_put_listent(
 		return;
 	}
 	offset = context->buffer + context->count;
-	memcpy(offset, prefix, prefix_len);
-	offset += prefix_len;
-	strncpy(offset, (char *)name, namelen);			/* real name */
-	offset += namelen;
-	*offset = '\0';
+
+	combined_len = prefix_len + namelen;
+
+	/* plus one byte for \0 */
+	actual_len = scnprintf(offset, combined_len + 1, "%s%s", prefix, name);
+
+	if (actual_len < combined_len)
+		xfs_warn(context->dp->i_mount,
+	"cannot completely copy %s%s to context buffer resulting in truncation",
+			prefix, name);
 
 compute_size:
 	context->count += prefix_len + namelen + 1;

---
base-commit: c85af715cac0a951eea97393378e84bb49384734
change-id: 20240405-strncpy-xattr-split2-0a3aff0c6a20

Best regards,
--
Justin Stitt <[email protected]>

#1 lore raw

fs/xfs/xfs_xattr.c __xfs_xattr_put_listent( · line 230

Quoted 12 lines · Justin Stitt
fs/xfs/xfs_xattr.c +7 -5
-	memcpy(offset, prefix, prefix_len);
-	offset += prefix_len;
-	strncpy(offset, (char *)name, namelen);			/* real name */
-	offset += namelen;
-	*offset = '\0';
+
+	combined_len = prefix_len + namelen;
+
+	/* plus one byte for \0 */
+	actual_len = scnprintf(offset, combined_len + 1, "%s%s", prefix, name);
+
+	if (actual_len < combined_len)

Shouldn't this be a != ?

That being said I think this is actually wrong - the attr names are not NULL-terminated on disk, which is why we have the explicit zero terminataion above.

How was this tested?

#2 lore raw

Hi,

Quoted 16 lines · Christoph Hellwig

Review

Quoted 12 lines · Justin Stitt
(quoted diff) +7 -5
-     memcpy(offset, prefix, prefix_len);
-     offset += prefix_len;
-     strncpy(offset, (char *)name, namelen);                 /* real name */
-     offset += namelen;
-     *offset = '\0';
+
+     combined_len = prefix_len + namelen;
+
+     /* plus one byte for \0 */
+     actual_len = scnprintf(offset, combined_len + 1, "%s%s", prefix, name);
+
+     if (actual_len < combined_len)

Shouldn't this be a != ?

I guess it could be. It's a truncation check so I figured just checking if the amount of bytes actually copied was less than the total would suffice.

Quoted 4 lines

That being said I think this is actually wrong - the attr names are not NULL-terminated on disk, which is why we have the explicit zero terminataion above.

Gotcha, in which case we could use the "%.*s" format specifier which allows for a length argument. Does something like this look better?

fs/xfs/xfs_xattr.c +11 -5
index 364104e1b38a..1b7e886e0f29 100644
@@ -206,6 +206,7 @@ __xfs_xattr_put_listent(
 {
  char *offset;
  int arraytop;
+ size_t combined_len, actual_len;
 
  if (context->count < 0 || context->seen_enough)
  return;
@@ -220,11 +221,16 @@ __xfs_xattr_put_listent(
  return;
  }
  offset = context->buffer + context->count;
- memcpy(offset, prefix, prefix_len);
- offset += prefix_len;
- strncpy(offset, (char *)name, namelen); /* real name */
- offset += namelen;
- *offset = '\0';
+
+ combined_len = prefix_len + namelen;
+
+ /* plus one byte for \0 */
+ actual_len = scnprintf(offset, combined_len + 1, "%.*s%.*s",
+        prefix_len, prefix, namelen, name);
+
+ if (actual_len < combined_len)
+ xfs_warn(context->dp->i_mount,
+ "cannot completely copy context buffer resulting in truncation");
 
 compute_size:
  context->count += prefix_len + namelen + 1;

---

Quoted 2 lines

How was this tested?

With https://git.kernel.org/pub/scm/fs/xfs/xfstests-dev.git/about/

but using scripts + image from: https://github.com/tytso/xfstests-bld

here's the output log: https://pastebin.com/V2gFhbNZ wherein I ran the 5 default ones (I think?):

|        Ran: generic/475 generic/476 generic/521 generic/522 generic/642
|        Passed all 5 tests

Thanks
Justin

#3 lore raw
Quoted 58 lines · Justin Stitt

Hi,

Quoted 16 lines · Christoph Hellwig

fs/xfs/xfs_xattr.c __xfs_xattr_put_listent( · line 231

Quoted 12 lines · Justin Stitt
fs/xfs/xfs_xattr.c +7 -5
-     memcpy(offset, prefix, prefix_len);
-     offset += prefix_len;
-     strncpy(offset, (char *)name, namelen);                 /* real name */
-     offset += namelen;
-     *offset = '\0';
+
+     combined_len = prefix_len + namelen;
+
+     /* plus one byte for \0 */
+     actual_len = scnprintf(offset, combined_len + 1, "%s%s", prefix, name);
+
+     if (actual_len < combined_len)

Shouldn't this be a != ?

I guess it could be. It's a truncation check so I figured just checking if the amount of bytes actually copied was less than the total would suffice.

Quoted 4 lines

That being said I think this is actually wrong - the attr names are not NULL-terminated on disk, which is why we have the explicit zero terminataion above.

Gotcha, in which case we could use the "%.*s" format specifier which allows for a length argument. Does something like this look better?

fs/xfs/xfs_xattr.c +5 -5
index 364104e1b38a..1b7e886e0f29 100644
@@ -206,6 +206,7 @@ __xfs_xattr_put_listent(
 {
  char *offset;
  int arraytop;
+ size_t combined_len, actual_len;
 
  if (context->count < 0 || context->seen_enough)
  return;
@@ -220,11 +221,16 @@ __xfs_xattr_put_listent(
  return;
  }
  offset = context->buffer + context->count;
- memcpy(offset, prefix, prefix_len);
- offset += prefix_len;
- strncpy(offset, (char *)name, namelen); /* real name */
- offset += namelen;
- *offset = '\0';
+
+ combined_len = prefix_len + namelen;
+
+ /* plus one byte for \0 */

fs/xfs/xfs_xattr.c __xfs_xattr_put_listent(

Quoted 10 lines
fs/xfs/xfs_xattr.c +6 -1
+ actual_len = scnprintf(offset, combined_len + 1, "%.*s%.*s",
+        prefix_len, prefix, namelen, name);
+
+ if (actual_len < combined_len)
+ xfs_warn(context->dp->i_mount,
+ "cannot completely copy context buffer resulting in truncation");
 
 compute_size:
  context->count += prefix_len + namelen + 1;
---

I copy pasted from vim -> gmail and it completely ate all my tabs. When I actually send the new patch, if needed, it will be formatted correctly :)

Quoted 18 lines
Quoted 5 lines

How was this tested?

With https://git.kernel.org/pub/scm/fs/xfs/xfstests-dev.git/about/

but using scripts + image from: https://github.com/tytso/xfstests-bld

here's the output log: https://pastebin.com/V2gFhbNZ wherein I ran the 5 default ones (I think?):

|        Ran: generic/475 generic/476 generic/521 generic/522 generic/642
|        Passed all 5 tests

Thanks
Justin

#4 lore raw
Quoted 66 lines · Justin Stitt
Quoted 65 lines · Justin Stitt

Hi,

Quoted 16 lines · Christoph Hellwig

fs/xfs/xfs_xattr.c __xfs_xattr_put_listent( · line 231

Quoted 12 lines · Justin Stitt
fs/xfs/xfs_xattr.c +7 -5
-     memcpy(offset, prefix, prefix_len);
-     offset += prefix_len;
-     strncpy(offset, (char *)name, namelen);                 /* real name */
-     offset += namelen;
-     *offset = '\0';
+
+     combined_len = prefix_len + namelen;
+
+     /* plus one byte for \0 */
+     actual_len = scnprintf(offset, combined_len + 1, "%s%s", prefix, name);
+
+     if (actual_len < combined_len)

Shouldn't this be a != ?

I guess it could be. It's a truncation check so I figured just checking if the amount of bytes actually copied was less than the total would suffice.

Quoted 4 lines

That being said I think this is actually wrong - the attr names are not NULL-terminated on disk, which is why we have the explicit zero terminataion above.

Gotcha, in which case we could use the "%.*s" format specifier which allows for a length argument. Does something like this look better?

fs/xfs/xfs_xattr.c +11 -5
index 364104e1b38a..1b7e886e0f29 100644
@@ -206,6 +206,7 @@ __xfs_xattr_put_listent(
 {
  char *offset;
  int arraytop;
+ size_t combined_len, actual_len;
 
  if (context->count < 0 || context->seen_enough)
  return;
@@ -220,11 +221,16 @@ __xfs_xattr_put_listent(
  return;
  }
  offset = context->buffer + context->count;
- memcpy(offset, prefix, prefix_len);
- offset += prefix_len;
- strncpy(offset, (char *)name, namelen); /* real name */
- offset += namelen;
- *offset = '\0';
+
+ combined_len = prefix_len + namelen;
+
+ /* plus one byte for \0 */
+ actual_len = scnprintf(offset, combined_len + 1, "%.*s%.*s",
+        prefix_len, prefix, namelen, name);
+
+ if (actual_len < combined_len)
+ xfs_warn(context->dp->i_mount,
+ "cannot completely copy context buffer resulting in truncation");
 
Quoted 7 lines

fs/xfs/xfs_xattr.c __xfs_xattr_put_listent(

Quoted 3 lines
fs/xfs/xfs_xattr.c +0 -1
 compute_size:
  context->count += prefix_len + namelen + 1;
---

I copy pasted from vim -> gmail and it completely ate all my tabs. When I actually send the new patch, if needed, it will be formatted correctly :)

Yeah, the "%.*s" version looks better.

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

How was this tested?

With https://git.kernel.org/pub/scm/fs/xfs/xfstests-dev.git/about/

Quoted 7 lines
Quoted 7 lines

but using scripts + image from: https://github.com/tytso/xfstests-bld

here's the output log: https://pastebin.com/V2gFhbNZ wherein I ran the 5 default ones (I think?):

|        Ran: generic/475 generic/476 generic/521 generic/522 generic/642
|        Passed all 5 tests

Would you mind adding "-g attr,label" into the mix so that you're running all the functional tests for xattr and fs label functionality?

--D

Quoted 4 lines
Quoted 3 lines

Thanks
Justin