aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnd Bergmann <arnd@arndb.de>2018-06-05 22:42:45 -0400
committerDarrick J. Wong <darrick.wong@oracle.com>2018-06-06 17:17:53 -0400
commit4bb8b65a04273c5acd6d1e08e08b757b4e6f4913 (patch)
tree94f907c8da1ecb4eff88729512bec0e6900dd7f0
parent0b61f8a4079d904b1b1d47946cca898313de8c26 (diff)
xfs: fix string handling in label get/set functions
[sandeen: fix subject, avoid copy-out of uninit data in getlabel] gcc-8 reports two warnings for the newly added getlabel/setlabel code: fs/xfs/xfs_ioctl.c: In function 'xfs_ioc_getlabel': fs/xfs/xfs_ioctl.c:1822:38: error: argument to 'sizeof' in 'strncpy' call is the same expression as the source; did you mean to use the size of the destination? [-Werror=sizeof-pointer-memaccess] strncpy(label, sbp->sb_fname, sizeof(sbp->sb_fname)); ^ In function 'strncpy', inlined from 'xfs_ioc_setlabel' at /git/arm-soc/fs/xfs/xfs_ioctl.c:1863:2, inlined from 'xfs_file_ioctl' at /git/arm-soc/fs/xfs/xfs_ioctl.c:1918:10: include/linux/string.h:254:9: error: '__builtin_strncpy' output may be truncated copying 12 bytes from a string of length 12 [-Werror=stringop-truncation] return __builtin_strncpy(p, q, size); In both cases, part of the problem is that one of the strncpy() arguments is a fixed-length character array with zero-padding rather than a zero-terminated string. In the first one case, we also get an odd warning about sizeof-pointer-memaccess, which doesn't seem right (the sizeof is for an array that happens to be the same as the second strncpy argument). To work around the bogus warning, I use a plain 'XFSLABEL_MAX' for the strncpy() length when copying the label in getlabel. For setlabel(), using memcpy() with the correct length that is already known avoids the second warning and is slightly simpler. In a related issue, it appears that we accidentally skip the trailing \0 when copying a 12-character label back to user space in getlabel(). Using the correct sizeof() argument here copies the extra character. Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85602 Fixes: f7664b31975b ("xfs: implement online get/set fs label") Cc: Eric Sandeen <sandeen@redhat.com> Cc: Martin Sebor <msebor@gmail.com> Signed-off-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Eric Sandeen <sandeen@redhat.com> Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
-rw-r--r--fs/xfs/xfs_ioctl.c10
1 files changed, 5 insertions, 5 deletions
diff --git a/fs/xfs/xfs_ioctl.c b/fs/xfs/xfs_ioctl.c
index 78b2e29ffae3..d0236d82326a 100644
--- a/fs/xfs/xfs_ioctl.c
+++ b/fs/xfs/xfs_ioctl.c
@@ -1816,13 +1816,13 @@ xfs_ioc_getlabel(
1816 /* Paranoia */ 1816 /* Paranoia */
1817 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX); 1817 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
1818 1818
1819 /* 1 larger than sb_fname, so this ensures a trailing NUL char */
1820 memset(label, 0, sizeof(label));
1819 spin_lock(&mp->m_sb_lock); 1821 spin_lock(&mp->m_sb_lock);
1820 strncpy(label, sbp->sb_fname, sizeof(sbp->sb_fname)); 1822 strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
1821 spin_unlock(&mp->m_sb_lock); 1823 spin_unlock(&mp->m_sb_lock);
1822 1824
1823 /* xfs on-disk label is 12 chars, be sure we send a null to user */ 1825 if (copy_to_user(user_label, label, sizeof(label)))
1824 label[XFSLABEL_MAX] = '\0';
1825 if (copy_to_user(user_label, label, sizeof(sbp->sb_fname)))
1826 return -EFAULT; 1826 return -EFAULT;
1827 return 0; 1827 return 0;
1828} 1828}
@@ -1858,7 +1858,7 @@ xfs_ioc_setlabel(
1858 1858
1859 spin_lock(&mp->m_sb_lock); 1859 spin_lock(&mp->m_sb_lock);
1860 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname)); 1860 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
1861 strncpy(sbp->sb_fname, label, sizeof(sbp->sb_fname)); 1861 memcpy(sbp->sb_fname, label, len);
1862 spin_unlock(&mp->m_sb_lock); 1862 spin_unlock(&mp->m_sb_lock);
1863 1863
1864 /* 1864 /*