Skip to content

Commit 5b56bf5

Browse files
keesgregkh
authored andcommitted
kernfs: Convert kernfs_name_locked() from strlcpy() to strscpy()
strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated[1]. Additionally, it returns the size of the source string, not the resulting size of the destination string. In an effort to remove strlcpy() completely[2], replace strlcpy() here with strscpy(). Nothing actually checks the return value coming from kernfs_name_locked(), so this has no impact on error paths. The caller hierarchy is: kernfs_name_locked() kernfs_name() pr_cont_kernfs_name() return value ignored cgroup_name() current_css_set_cg_links_read() return value ignored print_page_owner_memcg() return value ignored Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy [1] Link: KSPP#89 [2] Cc: Greg Kroah-Hartman <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Azeem Shaikh <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 792e047 commit 5b56bf5

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

fs/kernfs/dir.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ static bool kernfs_lockdep(struct kernfs_node *kn)
5454
static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen)
5555
{
5656
if (!kn)
57-
return strlcpy(buf, "(null)", buflen);
57+
return strscpy(buf, "(null)", buflen);
5858

59-
return strlcpy(buf, kn->parent ? kn->name : "/", buflen);
59+
return strscpy(buf, kn->parent ? kn->name : "/", buflen);
6060
}
6161

6262
/* kernfs_node_depth - compute depth from @from to @to */
@@ -182,12 +182,12 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
182182
* @buflen: size of @buf
183183
*
184184
* Copies the name of @kn into @buf of @buflen bytes. The behavior is
185-
* similar to strlcpy().
185+
* similar to strscpy().
186186
*
187187
* Fills buffer with "(null)" if @kn is %NULL.
188188
*
189-
* Return: the length of @kn's name and if @buf isn't long enough,
190-
* it's filled up to @buflen-1 and nul terminated.
189+
* Return: the resulting length of @buf. If @buf isn't long enough,
190+
* it's filled up to @buflen-1 and nul terminated, and returns -E2BIG.
191191
*
192192
* This function can be called from any context.
193193
*/

0 commit comments

Comments
 (0)