Skip to contentSkip to conversation

xfs: replace deprecated strncpy with memtostr_pad

PATCH New Justin Stitt 1 participant 1 message Apr 10, 2024 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xfs: replace deprecated strncpy with memtostr_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.

sbp->sb_fname may not be NUL-terminated while label is expected to be. memtostr best describes this behavior, specifically, use the pad variant since we're copying out to userspace.

---
Changes in v2:
- use memtostr_pad (thanks Kees)
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Note: This patch relies on the memtostr{_pad} implementation from Kees' patch:
https://lore.kernel.org/all/[email protected]/

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..7ed7a5d57094 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 NULL byte */
 	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);
+	memtostr_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]>