]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
dcache: permit dynamic_dname()s up to NAME_MAX
authorAleksa Sarai <aleksa@amutable.com>
Tue, 31 Mar 2026 14:46:21 +0000 (01:46 +1100)
committerChristian Brauner <brauner@kernel.org>
Tue, 7 Apr 2026 10:32:22 +0000 (12:32 +0200)
dynamic_dname() has had an implicit limit of 64 characters since it was
introduced in commit c23fbb6bcb3e ("VFS: delay the dentry name
generation on sockets and pipes"), however it seems that this was a
fairly arbitrary number (suspiciously it was double the previously
hardcoded buffer size).

NAME_MAX seems like a more reasonable and consistent limit for d_name
lengths. While we're at it, we can also remove the unnecessary
stack-allocated array and just memmove() the formatted string to the end
of the buffer.

It should also be noted that at least one driver (in particular,
liveupdate's usage of anon_inode for session files) already exceeded
this limit without noticing that readlink(/proc/self/fd/$n) always
returns -ENAMETOOLONG, so this fixes those drivers as well.

Fixes: 0153094d03df ("liveupdate: luo_session: add sessions support")
Fixes: c23fbb6bcb3e ("VFS: delay the dentry name generation on sockets and pipes")
Signed-off-by: Aleksa Sarai <aleksa@amutable.com>
Link: https://patch.msgid.link/20260401-dynamic-dname-name_max-v1-1-8ca20ab2642e@amutable.com
Tested-by: Luca Boccassi <luca.boccassi@gmail.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/d_path.c

index bb365511066b21eb447e18858f2dd3bcb351423c..a48957c0971ef6297be2024f3239c0b437058f72 100644 (file)
@@ -301,18 +301,19 @@ EXPORT_SYMBOL(d_path);
 char *dynamic_dname(char *buffer, int buflen, const char *fmt, ...)
 {
        va_list args;
-       char temp[64];
+       char *start;
        int sz;
 
        va_start(args, fmt);
-       sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
+       sz = vsnprintf(buffer, buflen, fmt, args) + 1;
        va_end(args);
 
-       if (sz > sizeof(temp) || sz > buflen)
+       if (sz > NAME_MAX || sz > buflen)
                return ERR_PTR(-ENAMETOOLONG);
 
-       buffer += buflen - sz;
-       return memcpy(buffer, temp, sz);
+       /* Move the formatted d_name to the end of the buffer. */
+       start = buffer + (buflen - sz);
+       return memmove(start, buffer, sz);
 }
 
 char *simple_dname(struct dentry *dentry, char *buffer, int buflen)