public inbox for io-uring@vger.kernel.org
 help / color / mirror / Atom feed
From: Al Viro <viro@zeniv.linux.org.uk>
To: linux-fsdevel@vger.kernel.org
Cc: torvalds@linux-foundation.org, brauner@kernel.org, jack@suse.cz,
	mjguzik@gmail.com, paul@paul-moore.com, axboe@kernel.dk,
	audit@vger.kernel.org, io-uring@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v4 15/59] struct filename: saner handling of long names
Date: Thu,  8 Jan 2026 07:37:19 +0000	[thread overview]
Message-ID: <20260108073803.425343-16-viro@zeniv.linux.org.uk> (raw)
In-Reply-To: <20260108073803.425343-1-viro@zeniv.linux.org.uk>

Always allocate struct filename from names_cachep, long name or short;
short names would be embedded into struct filename.  Longer ones do
not cannibalize the original struct filename - put them into kmalloc'ed
buffers (PATH_MAX-sized for import from userland, strlen() + 1 - for
ones originating kernel-side, where we know the length beforehand).

Cutoff length for short names is chosen so that struct filename would be
192 bytes long - that's both a multiple of 64 and large enough to cover
the majority of real-world uses.

Simplifies logics in getname()/putname() and friends.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
---
 fs/namei.c         | 87 ++++++++++++++++++----------------------------
 include/linux/fs.h | 10 ++++--
 2 files changed, 41 insertions(+), 56 deletions(-)

diff --git a/fs/namei.c b/fs/namei.c
index 468e3db62f53..9053aeee05d5 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -123,15 +123,14 @@
  * PATH_MAX includes the nul terminator --RR.
  */
 
-#define EMBEDDED_NAME_MAX	(PATH_MAX - offsetof(struct filename, iname))
-
 /* SLAB cache for struct filename instances */
 static struct kmem_cache *names_cachep __ro_after_init;
 
 void __init filename_init(void)
 {
-	names_cachep = kmem_cache_create_usercopy("names_cache", PATH_MAX, 0,
-			SLAB_HWCACHE_ALIGN|SLAB_PANIC, 0, PATH_MAX, NULL);
+	names_cachep = kmem_cache_create_usercopy("names_cache", sizeof(struct filename), 0,
+			SLAB_HWCACHE_ALIGN|SLAB_PANIC, offsetof(struct filename, iname),
+						EMBEDDED_NAME_MAX, NULL);
 }
 
 static inline struct filename *alloc_filename(void)
@@ -150,30 +149,23 @@ static inline void initname(struct filename *name)
 	atomic_set(&name->refcnt, 1);
 }
 
-static struct filename *getname_long(struct filename *old,
-				     const char __user *filename)
+static int getname_long(struct filename *name, const char __user *filename)
 {
 	int len;
-	/*
-	 * size is chosen that way we to guarantee that
-	 * p->iname[0] is within the same object and that
-	 * p->name can't be equal to p->iname, no matter what.
-	 */
-	const size_t size = offsetof(struct filename, iname[1]);
-	struct filename *p __free(kfree) = kzalloc(size, GFP_KERNEL);
+	char *p __free(kfree) = kmalloc(PATH_MAX, GFP_KERNEL);
 	if (unlikely(!p))
-		return ERR_PTR(-ENOMEM);
+		return -ENOMEM;
 
-	memmove(old, &old->iname, EMBEDDED_NAME_MAX);
-	p->name = (char *)old;
-	len = strncpy_from_user((char *)old + EMBEDDED_NAME_MAX,
+	memcpy(p, &name->iname, EMBEDDED_NAME_MAX);
+	len = strncpy_from_user(p + EMBEDDED_NAME_MAX,
 				filename + EMBEDDED_NAME_MAX,
 				PATH_MAX - EMBEDDED_NAME_MAX);
 	if (unlikely(len < 0))
-		return ERR_PTR(len);
+		return len;
 	if (unlikely(len == PATH_MAX - EMBEDDED_NAME_MAX))
-		return ERR_PTR(-ENAMETOOLONG);
-	return no_free_ptr(p);
+		return -ENAMETOOLONG;
+	name->name = no_free_ptr(p);
+	return 0;
 }
 
 struct filename *
@@ -199,16 +191,9 @@ getname_flags(const char __user *filename, int flags)
 	 * Handle both empty path and copy failure in one go.
 	 */
 	if (unlikely(len <= 0)) {
-		if (unlikely(len < 0)) {
-			free_filename(result);
-			return ERR_PTR(len);
-		}
-
 		/* The empty path is special. */
-		if (!(flags & LOOKUP_EMPTY)) {
-			free_filename(result);
-			return ERR_PTR(-ENOENT);
-		}
+		if (!len && !(flags & LOOKUP_EMPTY))
+			len = -ENOENT;
 	}
 
 	/*
@@ -217,14 +202,13 @@ getname_flags(const char __user *filename, int flags)
 	 * names_cache allocation for the pathname, and re-do the copy from
 	 * userland.
 	 */
-	if (unlikely(len == EMBEDDED_NAME_MAX)) {
-		struct filename *p = getname_long(result, filename);
-		if (IS_ERR(p)) {
-			free_filename(result);
-			return p;
-		}
-		result = p;
+	if (unlikely(len == EMBEDDED_NAME_MAX))
+		len = getname_long(result, filename);
+	if (unlikely(len < 0)) {
+		free_filename(result);
+		return ERR_PTR(len);
 	}
+
 	initname(result);
 	audit_getname(result);
 	return result;
@@ -260,29 +244,26 @@ struct filename *getname_kernel(const char * filename)
 {
 	struct filename *result;
 	int len = strlen(filename) + 1;
+	char *p;
+
+	if (unlikely(len > PATH_MAX))
+		return ERR_PTR(-ENAMETOOLONG);
 
 	result = alloc_filename();
 	if (unlikely(!result))
 		return ERR_PTR(-ENOMEM);
 
 	if (len <= EMBEDDED_NAME_MAX) {
-		result->name = (char *)result->iname;
-	} else if (len <= PATH_MAX) {
-		const size_t size = offsetof(struct filename, iname[1]);
-		struct filename *tmp;
-
-		tmp = kmalloc(size, GFP_KERNEL);
-		if (unlikely(!tmp)) {
+		p = (char *)result->iname;
+		memcpy(p, filename, len);
+	} else {
+		p = kmemdup(filename, len, GFP_KERNEL);
+		if (unlikely(!p)) {
 			free_filename(result);
 			return ERR_PTR(-ENOMEM);
 		}
-		tmp->name = (char *)result;
-		result = tmp;
-	} else {
-		free_filename(result);
-		return ERR_PTR(-ENAMETOOLONG);
 	}
-	memcpy((char *)result->name, filename, len);
+	result->name = p;
 	initname(result);
 	audit_getname(result);
 	return result;
@@ -305,11 +286,9 @@ void putname(struct filename *name)
 			return;
 	}
 
-	if (unlikely(name->name != name->iname)) {
-		free_filename((struct filename *)name->name);
-		kfree(name);
-	} else
-		free_filename(name);
+	if (unlikely(name->name != name->iname))
+		kfree(name->name);
+	free_filename(name);
 }
 EXPORT_SYMBOL(putname);
 
diff --git a/include/linux/fs.h b/include/linux/fs.h
index e906f157905c..a74ffcbe8407 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2409,13 +2409,19 @@ extern struct kobject *fs_kobj;
 
 /* fs/open.c */
 struct audit_names;
-struct filename {
+
+struct __filename_head {
 	const char		*name;	/* pointer to actual string */
 	atomic_t		refcnt;
 	struct audit_names	*aname;
-	const char		iname[];
+};
+#define EMBEDDED_NAME_MAX	192 - sizeof(struct __filename_head)
+struct filename {
+	struct __filename_head;
+	const char		iname[EMBEDDED_NAME_MAX];
 };
 static_assert(offsetof(struct filename, iname) % sizeof(long) == 0);
+static_assert(sizeof(struct filename) % 64 == 0);
 
 static inline struct mnt_idmap *file_mnt_idmap(const struct file *file)
 {
-- 
2.47.3


  parent reply	other threads:[~2026-01-08  7:36 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-08  7:37 [PATCH v4 00/59] struct filename series Al Viro
2026-01-08  7:37 ` [PATCH v4 01/59] do_faccessat(): import pathname only once Al Viro
2026-01-08  7:37 ` [PATCH v4 02/59] do_fchmodat(): " Al Viro
2026-01-08  7:37 ` [PATCH v4 03/59] do_fchownat(): " Al Viro
2026-01-08  7:37 ` [PATCH v4 04/59] do_utimes_path(): " Al Viro
2026-01-08  7:37 ` [PATCH v4 05/59] chdir(2): " Al Viro
2026-01-08  7:37 ` [PATCH v4 06/59] chroot(2): " Al Viro
2026-01-08  7:37 ` [PATCH v4 07/59] user_statfs(): " Al Viro
2026-01-08  7:37 ` [PATCH v4 08/59] do_sys_truncate(): " Al Viro
2026-01-08  7:37 ` [PATCH v4 09/59] do_readlinkat(): " Al Viro
2026-01-08  7:37 ` [PATCH v4 10/59] get rid of audit_reusename() Al Viro
2026-01-08  7:37 ` [PATCH v4 11/59] ntfs: ->d_compare() must not block Al Viro
2026-01-08  7:37 ` [PATCH v4 12/59] getname_flags() massage, part 1 Al Viro
2026-01-08  7:37 ` [PATCH v4 13/59] getname_flags() massage, part 2 Al Viro
2026-01-08  7:37 ` [PATCH v4 14/59] struct filename: use names_cachep only for getname() and friends Al Viro
2026-01-08  7:37 ` Al Viro [this message]
2026-01-13 15:31   ` [PATCH v4 15/59] struct filename: saner handling of long names Mark Brown
2026-01-13 15:39     ` Al Viro
2026-01-13 17:51       ` Mark Brown
2026-01-13 19:07       ` Al Viro
2026-01-13 19:17         ` Al Viro
2026-01-08  7:37 ` [PATCH v4 16/59] fs: hide names_cache behind runtime const machinery Al Viro
2026-01-08  7:37 ` [PATCH v4 17/59] allow to use CLASS() for struct filename * Al Viro
2026-01-08  7:37 ` [PATCH v4 18/59] switch __getname_maybe_null() to CLASS(filename_flags) Al Viro
2026-01-08  7:37 ` [PATCH v4 19/59] allow incomplete imports of filenames Al Viro
2026-01-08  7:37 ` [PATCH v4 20/59] struct filename ->refcnt doesn't need to be atomic Al Viro
2026-01-08  7:37 ` [PATCH v4 21/59] file_getattr(): filename_lookup() accepts ERR_PTR() as filename Al Viro
2026-01-08  7:37 ` [PATCH v4 22/59] file_setattr(): " Al Viro
2026-01-08  7:37 ` [PATCH v4 23/59] move_mount(): " Al Viro
2026-01-08  7:37 ` [PATCH v4 24/59] ksmbd_vfs_path_lookup(): vfs_path_parent_lookup() accepts ERR_PTR() as name Al Viro
2026-01-08  7:37 ` [PATCH v4 25/59] ksmbd_vfs_rename(): " Al Viro
2026-01-08  7:37 ` [PATCH v4 26/59] do_filp_open(): DTRT when getting ERR_PTR() as pathname Al Viro
2026-01-08  7:37 ` [PATCH v4 27/59] rename do_filp_open() to do_file_open() Al Viro
2026-01-08  7:37 ` [PATCH v4 28/59] do_sys_openat2(): get rid of useless check, switch to CLASS(filename) Al Viro
2026-01-08  7:37 ` [PATCH v4 29/59] simplify the callers of file_open_name() Al Viro
2026-01-08  7:37 ` [PATCH v4 30/59] simplify the callers of do_open_execat() Al Viro
2026-01-08  7:37 ` [PATCH v4 31/59] simplify the callers of alloc_bprm() Al Viro
2026-01-08  7:37 ` [PATCH v4 32/59] switch {alloc,free}_bprm() to CLASS() Al Viro
2026-01-08  7:37 ` [PATCH v4 33/59] file_[gs]etattr(2): switch to CLASS(filename_maybe_null) Al Viro
2026-01-08  7:37 ` [PATCH v4 34/59] mount_setattr(2): don't mess with LOOKUP_EMPTY Al Viro
2026-01-08  7:37 ` [PATCH v4 35/59] do_open_execat(): don't care about LOOKUP_EMPTY Al Viro
2026-01-08  7:37 ` [PATCH v4 36/59] vfs_open_tree(): use CLASS(filename_uflags) Al Viro
2026-01-08  7:37 ` [PATCH v4 37/59] name_to_handle_at(): " Al Viro
2026-01-08  7:37 ` [PATCH v4 38/59] fspick(2): use CLASS(filename_flags) Al Viro
2026-01-08  7:37 ` [PATCH v4 39/59] do_fchownat(): unspaghettify a bit Al Viro
2026-01-08  7:37 ` [PATCH v4 40/59] chdir(2): " Al Viro
2026-01-08  7:37 ` [PATCH v4 41/59] do_utimes_path(): switch to CLASS(filename_uflags) Al Viro
2026-01-08  7:37 ` [PATCH v4 42/59] do_sys_truncate(): switch to CLASS(filename) Al Viro
2026-01-08  7:37 ` [PATCH v4 43/59] do_readlinkat(): switch to CLASS(filename_flags) Al Viro
2026-01-08  7:37 ` [PATCH v4 44/59] do_f{chmod,chown,access}at(): use CLASS(filename_uflags) Al Viro
2026-01-08  7:37 ` [PATCH v4 45/59] do_{renameat2,linkat,symlinkat}(): use CLASS(filename_consume) Al Viro
2026-01-08  7:37 ` [PATCH v4 46/59] do_{mknodat,mkdirat,unlinkat,rmdir}(): " Al Viro
2026-01-08  7:37 ` [PATCH v4 47/59] namei.c: convert getname_kernel() callers to CLASS(filename_kernel) Al Viro
2026-01-08  7:37 ` [PATCH v4 48/59] namei.c: switch user pathname imports to CLASS(filename{,_flags}) Al Viro
2026-01-08  7:37 ` [PATCH v4 49/59] filename_...xattr(): don't consume filename reference Al Viro
2026-01-08  7:37 ` [PATCH v4 50/59] move_mount(2): switch to CLASS(filename_maybe_null) Al Viro
2026-01-08  7:37 ` [PATCH v4 51/59] chroot(2): switch to CLASS(filename) Al Viro
2026-01-08  7:37 ` [PATCH v4 52/59] quotactl_block(): " Al Viro
2026-01-08  7:37 ` [PATCH v4 53/59] statx: switch to CLASS(filename_maybe_null) Al Viro
2026-01-08  7:37 ` [PATCH v4 54/59] user_statfs(): switch to CLASS(filename) Al Viro
2026-01-08  7:37 ` [PATCH v4 55/59] mqueue: " Al Viro
2026-01-08  7:38 ` [PATCH v4 56/59] ksmbd: use CLASS(filename_kernel) Al Viro
2026-01-08  7:38 ` [PATCH v4 57/59] alpha: switch osf_mount() to strndup_user() Al Viro
2026-01-08  7:38 ` [PATCH v4 58/59] sysfs(2): fs_index() argument is _not_ a pathname Al Viro
2026-01-08  7:38 ` [PATCH v4 59/59] switch init_mkdir() to use of do_mkdirat(), etc Al Viro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260108073803.425343-16-viro@zeniv.linux.org.uk \
    --to=viro@zeniv.linux.org.uk \
    --cc=audit@vger.kernel.org \
    --cc=axboe@kernel.dk \
    --cc=brauner@kernel.org \
    --cc=io-uring@vger.kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mjguzik@gmail.com \
    --cc=paul@paul-moore.com \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox