Skip to content

Commit 7dcbf17

Browse files
JustinStittkees
authored andcommitted
hfsplus: refactor copy_name to not use strncpy
strncpy() is deprecated with NUL-terminated destination strings [1]. The copy_name() method does a lot of manual buffer manipulation to eventually arrive with its desired string. If we don't know the namespace this attr has or belongs to we want to prepend "osx." to our final string. Following this, we're copying xattr_name and doing a bizarre manual NUL-byte assignment with a memset where n=1. Really, we can use some more obvious string APIs to acomplish this, improving readability and security. Following the same control flow as before: if we don't know the namespace let's use scnprintf() to form our prefix + xattr_name pairing (while NUL-terminating too!). Otherwise, use strscpy() to return the number of bytes copied into our buffer. Additionally, for non-empty strings, include the NUL-byte in the length -- matching the behavior of the previous implementation. Note that strscpy() _can_ return -E2BIG but this is already handled by all callsites: In both hfsplus_listxattr_finder_info() and hfsplus_listxattr(), ret is already type ssize_t so we can change the return type of copy_name() to match (understanding that scnprintf()'s return type is different yet fully representable by ssize_t). Furthermore, listxattr() in fs/xattr.c is well-equipped to handle a potential -E2BIG return result from vfs_listxattr(): | ssize_t error; ... | error = vfs_listxattr(d, klist, size); | if (error > 0) { | if (size && copy_to_user(list, klist, error)) | error = -EFAULT; | } else if (error == -ERANGE && size >= XATTR_LIST_MAX) { | /* The file system tried to returned a list bigger | than XATTR_LIST_MAX bytes. Not possible. */ | error = -E2BIG; | } ... the error can potentially already be -E2BIG, skipping this else-if and ending up at the same state as other errors. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html Link: KSPP#90 Cc: <[email protected]> Signed-off-by: Justin Stitt <[email protected]> Reviewed-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 31ca7e7 commit 7dcbf17

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

fs/hfsplus/xattr.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -400,21 +400,19 @@ static int name_len(const char *xattr_name, int xattr_name_len)
400400
return len;
401401
}
402402

403-
static int copy_name(char *buffer, const char *xattr_name, int name_len)
403+
static ssize_t copy_name(char *buffer, const char *xattr_name, int name_len)
404404
{
405-
int len = name_len;
406-
int offset = 0;
405+
ssize_t len;
407406

408-
if (!is_known_namespace(xattr_name)) {
409-
memcpy(buffer, XATTR_MAC_OSX_PREFIX, XATTR_MAC_OSX_PREFIX_LEN);
410-
offset += XATTR_MAC_OSX_PREFIX_LEN;
411-
len += XATTR_MAC_OSX_PREFIX_LEN;
412-
}
413-
414-
strncpy(buffer + offset, xattr_name, name_len);
415-
memset(buffer + offset + name_len, 0, 1);
416-
len += 1;
407+
if (!is_known_namespace(xattr_name))
408+
len = scnprintf(buffer, name_len + XATTR_MAC_OSX_PREFIX_LEN,
409+
"%s%s", XATTR_MAC_OSX_PREFIX, xattr_name);
410+
else
411+
len = strscpy(buffer, xattr_name, name_len + 1);
417412

413+
/* include NUL-byte in length for non-empty name */
414+
if (len >= 0)
415+
len++;
418416
return len;
419417
}
420418

0 commit comments

Comments
 (0)