Skip to contentSkip to conversation

xfs: replace deprecated strncpy with strscpy_pad

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

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: replace deprecated strncpy with strscpy_pad

strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string interfaces.

The current code has taken care of NUL-termination by memset()'ing @label. This is followed by a strncpy() to perform the string copy.

Instead, use strscpy_pad() to get both 1) NUL-termination and 2) NUL-padding which is needed as this is copied out to userspace.

Note that this patch uses the new 2-argument version of strscpy_pad introduced in Commit e6584c3964f2f ("string: Allow 2-argument strscpy()").

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

---
 fs/xfs/xfs_ioctl.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/xfs/xfs_ioctl.c +2 -3
index d0e2cec6210d..a1156a8b1e15 100644
@@ -1750,15 +1750,14 @@ xfs_ioc_getlabel(
 	char			__user *user_label)
 {
 	struct xfs_sb		*sbp = &mp->m_sb;
+	/* 1 larger than sb_fname, for a trailing NUL char */
 	char			label[XFSLABEL_MAX + 1];
 
 	/* Paranoia */
 	BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
 
-	/* 1 larger than sb_fname, so this ensures a trailing NUL char */
-	memset(label, 0, sizeof(label));
 	spin_lock(&mp->m_sb_lock);
-	strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
+	strscpy_pad(label, sbp->sb_fname);
 	spin_unlock(&mp->m_sb_lock);
 
 	if (copy_to_user(user_label, label, sizeof(label)))

---
base-commit: c85af715cac0a951eea97393378e84bb49384734
change-id: 20240405-strncpy-xfs-split1-a2c408b934c6

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

#1 lore raw
Quoted 36 lines · Justin Stitt

strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string interfaces.

The current code has taken care of NUL-termination by memset()'ing @label. This is followed by a strncpy() to perform the string copy.

Instead, use strscpy_pad() to get both 1) NUL-termination and 2) NUL-padding which is needed as this is copied out to userspace.

Note that this patch uses the new 2-argument version of strscpy_pad introduced in Commit e6584c3964f2f ("string: Allow 2-argument strscpy()").

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

---
 fs/xfs/xfs_ioctl.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/xfs/xfs_ioctl.c +1 -0
index d0e2cec6210d..a1156a8b1e15 100644
@@ -1750,15 +1750,14 @@ xfs_ioc_getlabel(
 	char			__user *user_label)
 {
 	struct xfs_sb		*sbp = &mp->m_sb;
+	/* 1 larger than sb_fname, for a trailing NUL char */

fs/xfs/xfs_ioctl.c xfs_ioc_getlabel( · line 1760

Quoted 10 lines
fs/xfs/xfs_ioctl.c +1 -3
 	char			label[XFSLABEL_MAX + 1];
 
 	/* Paranoia */
 	BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
 
-	/* 1 larger than sb_fname, so this ensures a trailing NUL char */
-	memset(label, 0, sizeof(label));
 	spin_lock(&mp->m_sb_lock);
-	strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
+	strscpy_pad(label, sbp->sb_fname);

Is sbp->sb_fname itself NUL-terminated? This looks like another case of needing the memtostr() helper?

-Kees

Quoted 13 lines
fs/xfs/xfs_ioctl.c +0 -1
 	spin_unlock(&mp->m_sb_lock);
 
 	if (copy_to_user(user_label, label, sizeof(label)))
 
---

base-commit: c85af715cac0a951eea97393378e84bb49384734
change-id: 20240405-strncpy-xfs-split1-a2c408b934c6

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

Signature · 1 line
Kees Cook
#2 lore raw
Quoted 10 lines · Kees Cook

fs/xfs/xfs_ioctl.c xfs_ioc_getlabel( · line 1760

Quoted 6 lines
fs/xfs/xfs_ioctl.c +1 -3
-     /* 1 larger than sb_fname, so this ensures a trailing NUL char */
-     memset(label, 0, sizeof(label));
      spin_lock(&mp->m_sb_lock);
-     strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
+     strscpy_pad(label, sbp->sb_fname);

Is sbp->sb_fname itself NUL-terminated? This looks like another case of needing the memtostr() helper?

I sent a patch [1].

Obviously it depends on your implementation patch landing first; what tree should it go to?

Quoted 1 line

Kees Cook

[1]: https://lore.kernel.org/r/[email protected]

#3 lore raw
Quoted 8 lines · Justin Stitt
Quoted 7 lines · Kees Cook
Quoted 6 lines
fs/xfs/xfs_ioctl.c +1 -3
-     /* 1 larger than sb_fname, so this ensures a trailing NUL char */
-     memset(label, 0, sizeof(label));
      spin_lock(&mp->m_sb_lock);
-     strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
+     strscpy_pad(label, sbp->sb_fname);
Quoted 8 lines
Quoted 3 lines

Is sbp->sb_fname itself NUL-terminated? This looks like another case of needing the memtostr() helper?

I sent a patch [1].

Obviously it depends on your implementation patch landing first; what tree should it go to?

This "flavor" of conversion may need to wait a release? There's no urgency on the conversion, and there are plenty more to do for this cycle. ;)

-Kees

Quoted 1 line

[1]: https://lore.kernel.org/r/[email protected]

Signature · 1 line
Kees Cook
#4 lore raw

From: Kees Cook

Quoted 15 lines

Sent: 11 April 2024 16:32

Quoted 12 lines · Justin Stitt
Quoted 10 lines · Kees Cook

fs/xfs/xfs_ioctl.c xfs_ioc_getlabel( · line 1760

Quoted 6 lines
fs/xfs/xfs_ioctl.c +1 -3
-     /* 1 larger than sb_fname, so this ensures a trailing NUL char */
-     memset(label, 0, sizeof(label));
      spin_lock(&mp->m_sb_lock);
-     strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
+     strscpy_pad(label, sbp->sb_fname);

Is sbp->sb_fname itself NUL-terminated? This looks like another case of needing the memtostr() helper?

Quoted 8 lines
Quoted 4 lines

I sent a patch [1].

Obviously it depends on your implementation patch landing first; what tree should it go to?

This "flavor" of conversion may need to wait a release? There's no urgency on the conversion, and there are plenty more to do for this cycle. ;)

In this case:

	char label[sizeof (sbp->fb_fname) + 1];
	memcpy(label, sbp->sb_fname, sizeof (sbp->sb_fname));
	label[sizeof (sbp->fname)] = 0;

is probably the clearest code.
(it is [12] - so no point faffing with the copy.)

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)