| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/10/24 22:27 | flow | patch-triage |
3mArgs:null Results: {
"EnableConfigs": [
"CGROUPS",
"RESCTRL_FS",
"X86_CPU_RESCTRL"
],
"FocusSymbols": [
"lookup_open",
"may_o_create",
"finish_lookup",
"vfs_mkdir",
"kernfs_iop_mkdir",
"kernfs_get_inode_locked",
"cgroup_mkdir",
"rdtgroup_mkdir"
],
"KMSANReasoning": "The patch series introduces support for atomic directory creation via openat() using the O_CREAT | O_DIRECTORY | O_EXCL flags. It refactors VFS path lookup and creation logic (lookup_open, finish_lookup, vfs_mkdir_no_perm), adds the SB_I_NO_ATOMIC_MKDIR superblock flag for unsupported filesystems, and updates kernfs/cgroup/resctrl mkdir callbacks to return the newly created kernfs_node directly.\n\nKMSAN applicability assessment:\n1. No Kernel-to-User Info Leaks: The changes only affect file creation and path lookup control flow, returning standard integer file descriptors or error codes to userspace. No uninitialized structures, buffers, or padding bytes are copied or leaked to user space.\n2. No Uninitialized Memory Branching/Reads: All local variables (flags, mode, dentry pointers, delegated_inode) are explicitly initialized before use. In kernfs, new inodes allocated with new_inode() are standard VFS inodes initialized via inode_init_always() and kernfs_init_inode().\n3. Bug Profile vs Tooling: Potential regressions from this refactoring involve VFS locking (lock nesting and rwsems, which are covered by LOCKDEP), dentry/inode reference counting and use-after-free/double-free (which are covered by KASAN), or semantic error handling.\n\nTherefore, standard KASAN and LOCKDEP builds are fully equipped to catch any memory or locking bugs introduced by these changes, and a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch series implements atomic directory creation via openat() with O_CREAT | O_DIRECTORY | O_EXCL. It modifies core VFS path lookup and creation logic (fs/namei.c, fs/open.c), refactors vfs_mkdir and vfs_create, adds new runtime assertions (WARN_ON_ONCE), and adapts kernfs (kernfs_iop_mkdir, kernfs_get_inode_locked) along with cgroup and resctrl subsystems to instantiate and return directory dentries/inodes atomically. These changes are in core reachable kernel code accessible to userspace syscalls.",
"WorthFuzzing": true
} |
| 1/1 | 2026/10/24 22:27 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit b5331efeb2ca83571d91add2b1e2e364569ba29a\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Sat Oct 24 22:27:22 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/arch/alpha/include/uapi/asm/errno.h b/arch/alpha/include/uapi/asm/errno.h\nindex 1a99f38813c79..0384f69989748 100644\n--- a/arch/alpha/include/uapi/asm/errno.h\n+++ b/arch/alpha/include/uapi/asm/errno.h\n@@ -19,7 +19,7 @@\n #define\tENOPROTOOPT\t42\t/* Protocol not available */\n #define\tEPROTONOSUPPORT\t43\t/* Protocol not supported */\n #define\tESOCKTNOSUPPORT\t44\t/* Socket type not supported */\n-#define\tEOPNOTSUPP\t45\t/* Operation not supported on transport endpoint */\n+#define\tEOPNOTSUPP\t45\t/* Operation not supported */\n #define\tEPFNOSUPPORT\t46\t/* Protocol family not supported */\n #define\tEAFNOSUPPORT\t47\t/* Address family not supported by protocol */\n #define\tEADDRINUSE\t48\t/* Address already in use */\ndiff --git a/arch/mips/include/uapi/asm/errno.h b/arch/mips/include/uapi/asm/errno.h\nindex 1835a50b69cef..2b6813715d1e5 100644\n--- a/arch/mips/include/uapi/asm/errno.h\n+++ b/arch/mips/include/uapi/asm/errno.h\n@@ -75,7 +75,7 @@\n #define ENOPROTOOPT\t99\t/* Protocol not available */\n #define EPROTONOSUPPORT 120\t/* Protocol not supported */\n #define ESOCKTNOSUPPORT 121\t/* Socket type not supported */\n-#define EOPNOTSUPP\t122\t/* Operation not supported on transport endpoint */\n+#define EOPNOTSUPP\t122\t/* Operation not supported */\n #define EPFNOSUPPORT\t123\t/* Protocol family not supported */\n #define EAFNOSUPPORT\t124\t/* Address family not supported by protocol */\n #define EADDRINUSE\t125\t/* Address already in use */\ndiff --git a/arch/parisc/include/uapi/asm/errno.h b/arch/parisc/include/uapi/asm/errno.h\nindex 93194fbb0a80d..74bd65d791457 100644\n--- a/arch/parisc/include/uapi/asm/errno.h\n+++ b/arch/parisc/include/uapi/asm/errno.h\n@@ -84,7 +84,7 @@\n #define\tENOPROTOOPT\t220\t/* Protocol not available */\n #define\tEPROTONOSUPPORT\t221\t/* Protocol not supported */\n #define\tESOCKTNOSUPPORT\t222\t/* Socket type not supported */\n-#define\tEOPNOTSUPP\t223\t/* Operation not supported on transport endpoint */\n+#define\tEOPNOTSUPP\t223\t/* Operation not supported */\n #define\tEPFNOSUPPORT\t224\t/* Protocol family not supported */\n #define\tEAFNOSUPPORT\t225\t/* Address family not supported by protocol */\n #define\tEADDRINUSE\t226\t/* Address already in use */\ndiff --git a/arch/sparc/include/uapi/asm/errno.h b/arch/sparc/include/uapi/asm/errno.h\nindex 71940ec9130b4..699f648e1a356 100644\n--- a/arch/sparc/include/uapi/asm/errno.h\n+++ b/arch/sparc/include/uapi/asm/errno.h\n@@ -16,7 +16,7 @@\n #define\tENOPROTOOPT\t42\t/* Protocol not available */\n #define\tEPROTONOSUPPORT\t43\t/* Protocol not supported */\n #define\tESOCKTNOSUPPORT\t44\t/* Socket type not supported */\n-#define\tEOPNOTSUPP\t45\t/* Op not supported on transport endpoint */\n+#define\tEOPNOTSUPP\t45\t/* Operation not supported */\n #define\tEPFNOSUPPORT\t46\t/* Protocol family not supported */\n #define\tEAFNOSUPPORT\t47\t/* Address family not supported by protocol */\n #define\tEADDRINUSE\t48\t/* Address already in use */\ndiff --git a/fs/9p/vfs_super.c b/fs/9p/vfs_super.c\nindex 79526044685c9..d0c594bee9918 100644\n--- a/fs/9p/vfs_super.c\n+++ b/fs/9p/vfs_super.c\n@@ -40,6 +40,7 @@ static int v9fs_fill_super(struct super_block *sb)\n \tsb-\u003es_blocksize_bits = fls(v9ses-\u003emaxdata - 1);\n \tsb-\u003es_blocksize = 1 \u003c\u003c sb-\u003es_blocksize_bits;\n \tsb-\u003es_magic = V9FS_MAGIC;\n+\tsb-\u003es_iflags |= SB_I_NO_ATOMIC_MKDIR;\n \tif (v9fs_proto_dotl(v9ses)) {\n \t\tsb-\u003es_op = \u0026v9fs_super_ops_dotl;\n \t\tif (!(v9ses-\u003eflags \u0026 V9FS_NO_XATTR))\ndiff --git a/fs/ceph/super.c b/fs/ceph/super.c\nindex 72935f665f11c..fa1cf1f4f57d6 100644\n--- a/fs/ceph/super.c\n+++ b/fs/ceph/super.c\n@@ -1226,6 +1226,7 @@ static int ceph_set_super(struct super_block *s, struct fs_context *fc)\n \tfsc-\u003emax_file_size = 1ULL \u003c\u003c 40; /* temp value until we get mdsmap */\n \n \ts-\u003es_op = \u0026ceph_super_ops;\n+\ts-\u003es_iflags |= SB_I_NO_ATOMIC_MKDIR;\n \tset_default_d_op(s, \u0026ceph_dentry_ops);\n \ts-\u003es_export_op = \u0026ceph_export_ops;\n \ndiff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c\nindex 5f37cddb956fb..8ed6076d50afd 100644\n--- a/fs/ecryptfs/main.c\n+++ b/fs/ecryptfs/main.c\n@@ -477,6 +477,7 @@ static int ecryptfs_get_tree(struct fs_context *fc)\n \t/* -\u003ekill_sb() will take care of sbi after that point */\n \tsbi = NULL;\n \ts-\u003es_op = \u0026ecryptfs_sops;\n+\ts-\u003es_iflags |= SB_I_NO_ATOMIC_MKDIR;\n \ts-\u003es_xattr = ecryptfs_xattr_handlers;\n \tset_default_d_op(s, \u0026ecryptfs_dops);\n \ndiff --git a/fs/fuse/inode.c b/fs/fuse/inode.c\nindex e9552be3637bc..cf766c6feea43 100644\n--- a/fs/fuse/inode.c\n+++ b/fs/fuse/inode.c\n@@ -1608,6 +1608,7 @@ static void fuse_sb_defaults(struct super_block *sb)\n \tsb-\u003es_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;\n \tsb-\u003es_iflags |= SB_I_NOIDMAP;\n \tsb-\u003es_iflags |= SB_I_NO_DATA_INTEGRITY;\n+\tsb-\u003es_iflags |= SB_I_NO_ATOMIC_MKDIR;\n \tif (sb-\u003es_user_ns != \u0026init_user_ns)\n \t\tsb-\u003es_iflags |= SB_I_UNTRUSTED_MOUNTER;\n \tsb-\u003es_flags \u0026= ~(SB_NOSEC | SB_I_VERSION);\ndiff --git a/fs/gfs2/ops_fstype.c b/fs/gfs2/ops_fstype.c\nindex 6ec47376aecf2..31bb09367d6bc 100644\n--- a/fs/gfs2/ops_fstype.c\n+++ b/fs/gfs2/ops_fstype.c\n@@ -1137,6 +1137,7 @@ static int gfs2_fill_super(struct super_block *sb, struct fs_context *fc)\n \tsb-\u003es_flags |= SB_NOSEC;\n \tsb-\u003es_magic = GFS2_MAGIC;\n \tsb-\u003es_op = \u0026gfs2_super_ops;\n+\tsb-\u003es_iflags |= SB_I_NO_ATOMIC_MKDIR;\n \n \tset_default_d_op(sb, \u0026gfs2_dops);\n \tsb-\u003es_export_op = \u0026gfs2_export_ops;\ndiff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c\nindex 324d61a00545b..abb6b0d212142 100644\n--- a/fs/kernfs/dir.c\n+++ b/fs/kernfs/dir.c\n@@ -37,7 +37,7 @@ static bool __kernfs_active(struct kernfs_node *kn)\n \treturn atomic_read(\u0026kn-\u003eactive) \u003e= 0;\n }\n \n-static bool kernfs_active(struct kernfs_node *kn)\n+bool kernfs_active(struct kernfs_node *kn)\n {\n \tlockdep_assert_held(\u0026kernfs_root(kn)-\u003ekernfs_rwsem);\n \treturn __kernfs_active(kn);\n@@ -1293,18 +1293,44 @@ static struct dentry *kernfs_iop_mkdir(const struct mnt_idmap *idmap,\n {\n \tstruct kernfs_node *parent = dir-\u003ei_private;\n \tstruct kernfs_syscall_ops *scops = kernfs_root(parent)-\u003esyscall_ops;\n-\tint ret;\n+\tstruct kernfs_node *kn;\n+\tstruct inode *inode;\n \n \tif (!scops || !scops-\u003emkdir)\n-\t\treturn ERR_PTR(-EPERM);\n+\t\treturn ERR_PTR(-EOPNOTSUPP);\n+\n+\tinode = new_inode(dir-\u003ei_sb);\n+\tif (!inode)\n+\t\treturn ERR_PTR(-ENOMEM);\n \n-\tif (!kernfs_get_active(parent))\n+\tif (!kernfs_get_active(parent)) {\n+\t\tiput(inode);\n \t\treturn ERR_PTR(-ENODEV);\n+\t}\n \n-\tret = scops-\u003emkdir(parent, dentry-\u003ed_name.name, mode);\n+\tkn = scops-\u003emkdir(parent, dentry-\u003ed_name.name, mode);\n \n \tkernfs_put_active(parent);\n-\treturn ERR_PTR(ret);\n+\tif (IS_ERR(kn)) {\n+\t\tiput(inode);\n+\t\treturn ERR_CAST(kn);\n+\t}\n+\n+\tscoped_guard(rwsem_read, \u0026kernfs_root(kn)-\u003ekernfs_rwsem)\n+\t\tinode = kernfs_get_inode_locked(inode, kn, false);\n+\tkernfs_put(kn);\n+\n+\tif (!inode)\n+\t\treturn ERR_PTR(-ESTALE);\n+\n+\t/*\n+\t * We may race with kernfs_fh_to_dentry() which calls d_obtain_alias(),\n+\t * so the inode may already have a disconnected alias. Use\n+\t * d_splice_alias() to move it into place, which does not accept\n+\t * hashed dentries, so drop the negative dentry first.\n+\t */\n+\td_drop(dentry);\n+\treturn d_splice_alias(inode, dentry);\n }\n \n static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)\ndiff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c\nindex 6630b29d7c078..0da4e46ec75bf 100644\n--- a/fs/kernfs/inode.c\n+++ b/fs/kernfs/inode.c\n@@ -204,7 +204,8 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)\n \tinode-\u003ei_generation = kernfs_gen(kn);\n \n \tset_default_inode_attr(inode, kn-\u003emode);\n-\tkernfs_refresh_inode(kn, inode);\n+\tscoped_guard(rwsem_read, \u0026kernfs_root(kn)-\u003ekernfs_iattr_rwsem)\n+\t\tkernfs_refresh_inode(kn, inode);\n \n \t/* initialize inode according to type */\n \tswitch (kernfs_type(kn)) {\n@@ -254,6 +255,63 @@ struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)\n \treturn inode;\n }\n \n+static int kernfs_inode_test(struct inode *inode, void *data)\n+{\n+\treturn inode-\u003ei_ino == *(u64 *)data;\n+}\n+\n+static int kernfs_inode_set(struct inode *inode, void *data)\n+{\n+\tinode-\u003ei_ino = *(u64 *)data;\n+\treturn 0;\n+}\n+\n+/**\n+ * kernfs_get_inode_locked - get the inode of @kn under kernfs_rwsem\n+ * @inode: inode allocated with new_inode() on the target superblock\n+ * @kn: kernfs_node to get the inode for\n+ * @lookup: should we do a ilookup before attempting to insert the inode\n+ *\n+ * Attaches @inode to @kn. Caller should pass an @inode allocated with\n+ * new_inode(), and hold the kernfs_rwsem semaphore for reading on the @kn\n+ * root. Because the kernfs_rwsem is held and it is checked that @kn is still\n+ * active, __kernfs_remove() always finds @inode to clear its i_nlink. If\n+ * lookup is true, a quick ilookup is attempted first.\n+ *\n+ * Returns the inode that was found or inserted for @kn, or NULL if @kn is\n+ * inactive. @inode is consumed.\n+ */\n+struct inode *kernfs_get_inode_locked(struct inode *inode, struct kernfs_node *kn,\n+\t\t\t\t bool lookup)\n+{\n+\tu64 ino = kernfs_ino(kn);\n+\tstruct inode *old = NULL, *new = inode;\n+\n+\tlockdep_assert_held_read(\u0026kernfs_root(kn)-\u003ekernfs_rwsem);\n+\n+\tif (lookup) {\n+\t\told = ilookup(inode-\u003ei_sb, ino);\n+\t\tif (old)\n+\t\t\tgoto out_iput;\n+\t}\n+\n+\tif (!kernfs_active(kn))\n+\t\tgoto out_iput;\n+\n+\tinode = inode_insert5(new, ino, kernfs_inode_test, kernfs_inode_set,\n+\t\t\t \u0026ino);\n+\tif (inode != new) {\n+\t\told = inode;\n+\t\tgoto out_iput;\n+\t}\n+\n+\tkernfs_init_inode(kn, inode);\n+\treturn inode;\n+out_iput:\n+\tiput(new);\n+\treturn old;\n+}\n+\n /*\n * The kernfs_node serves as both an inode and a directory entry for\n * kernfs. To prevent the kernfs inode numbers from being freed\ndiff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h\nindex f1e93b09e27df..ec6e1f75513ba 100644\n--- a/fs/kernfs/kernfs-internal.h\n+++ b/fs/kernfs/kernfs-internal.h\n@@ -176,6 +176,9 @@ extern struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;\n * inode.c\n */\n extern const struct xattr_handler * const kernfs_xattr_handlers[];\n+struct inode *kernfs_get_inode_locked(struct inode *inode,\n+\t\t\t\t struct kernfs_node *kn,\n+\t\t\t\t bool lookup);\n void kernfs_evict_inode(struct inode *inode);\n int kernfs_iop_permission(const struct mnt_idmap *idmap,\n \t\t\t struct inode *inode, int mask);\n@@ -194,6 +197,7 @@ extern const struct dentry_operations kernfs_dops;\n extern const struct file_operations kernfs_dir_fops;\n extern const struct inode_operations kernfs_dir_iops;\n \n+bool kernfs_active(struct kernfs_node *kn);\n struct kernfs_node *kernfs_get_active(struct kernfs_node *kn);\n void kernfs_put_active(struct kernfs_node *kn);\n int kernfs_add_one(struct kernfs_node *kn);\ndiff --git a/fs/namei.c b/fs/namei.c\nindex 22c4f328b8a97..7365975a0a32a 100644\n--- a/fs/namei.c\n+++ b/fs/namei.c\n@@ -1889,12 +1889,34 @@ static struct dentry *lookup_fast(struct nameidata *nd)\n \treturn dentry;\n }\n \n+/*\n+ * If @dentry is in-lookup, look it up in @dir through -\u003elookup(). Returns\n+ * the dentry to use from now on: @dentry itself, or the dentry or ERR_PTR()\n+ * that -\u003elookup() returned instead, in which case @dentry is dropped.\n+ */\n+static struct dentry *finish_lookup(struct inode *dir, struct dentry *dentry,\n+\t\t\t\tunsigned int flags)\n+{\n+\tstruct dentry *res;\n+\n+\tif (!d_in_lookup(dentry))\n+\t\treturn dentry;\n+\n+\tres = dir-\u003ei_op-\u003elookup(dir, dentry, flags);\n+\td_lookup_done(dentry);\n+\tif (unlikely(res)) {\n+\t\tdput(dentry);\n+\t\treturn res;\n+\t}\n+\treturn dentry;\n+}\n+\n /* Fast lookup failed, do it the slow way */\n static struct dentry *__lookup_slow(const struct qstr *name,\n \t\t\t\t struct dentry *dir,\n \t\t\t\t unsigned int flags)\n {\n-\tstruct dentry *dentry, *old;\n+\tstruct dentry *dentry;\n \tstruct inode *inode = dir-\u003ed_inode;\n \n \t/* Don't go there if it's already dead */\n@@ -1913,17 +1935,10 @@ static struct dentry *__lookup_slow(const struct qstr *name,\n \t\t\t\tgoto again;\n \t\t\t}\n \t\t\tdput(dentry);\n-\t\t\tdentry = ERR_PTR(error);\n-\t\t}\n-\t} else {\n-\t\told = inode-\u003ei_op-\u003elookup(inode, dentry, flags);\n-\t\td_lookup_done(dentry);\n-\t\tif (unlikely(old)) {\n-\t\t\tdput(dentry);\n-\t\t\tdentry = old;\n+\t\t\treturn ERR_PTR(error);\n \t\t}\n \t}\n-\treturn dentry;\n+\treturn finish_lookup(inode, dentry, flags);\n }\n \n static noinline struct dentry *lookup_slow(const struct qstr *name,\n@@ -2781,9 +2796,16 @@ static const char *path_init(struct nameidata *nd, unsigned flags)\n \treturn s;\n }\n \n+static inline bool trailing_slashes(const struct qstr *last)\n+{\n+\t/* last-\u003elen is set by hash_name() to the length of the current\n+\t * component -\u003ename, terminating with '/' or a NUL character. */\n+\treturn (bool)last-\u003ename[last-\u003elen];\n+}\n+\n static inline const char *lookup_last(struct nameidata *nd)\n {\n-\tif (nd-\u003elast_type == LAST_NORM \u0026\u0026 nd-\u003elast.name[nd-\u003elast.len])\n+\tif (nd-\u003elast_type == LAST_NORM \u0026\u0026 trailing_slashes(\u0026nd-\u003elast))\n \t\tnd-\u003eflags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;\n \n \treturn walk_component(nd, WALK_TRAILING);\n@@ -4159,6 +4181,24 @@ static inline umode_t vfs_prepare_mode(const struct mnt_idmap *idmap,\n \treturn mode;\n }\n \n+static inline\n+int vfs_create_no_perm(const struct mnt_idmap *idmap, struct dentry *dentry,\n+\t\t umode_t mode, struct delegated_inode *di)\n+{\n+\tstruct inode *dir = d_inode(dentry-\u003ed_parent);\n+\tint error;\n+\n+\terror = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, di);\n+\tif (error)\n+\t\treturn error;\n+\n+\terror = dir-\u003ei_op-\u003ecreate(idmap, dir, dentry, mode);\n+\tif (!error)\n+\t\tfsnotify_create(dir, dentry);\n+\n+\treturn error;\n+}\n+\n /**\n * vfs_create - create new file\n * @idmap:\tidmap of the mount the inode was found from\n@@ -4185,19 +4225,14 @@ int vfs_create(const struct mnt_idmap *idmap, struct dentry *dentry, umode_t mod\n \t\treturn error;\n \n \tif (!dir-\u003ei_op-\u003ecreate)\n-\t\treturn -EACCES;\t/* shouldn't it be ENOSYS? */\n+\t\treturn -EOPNOTSUPP;\n \n \tmode = vfs_prepare_mode(idmap, dir, mode, S_IALLUGO, S_IFREG);\n \terror = security_inode_create(idmap, dir, dentry, mode);\n \tif (error)\n \t\treturn error;\n-\terror = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, di);\n-\tif (error)\n-\t\treturn error;\n-\terror = dir-\u003ei_op-\u003ecreate(idmap, dir, dentry, mode);\n-\tif (!error)\n-\t\tfsnotify_create(dir, dentry);\n-\treturn error;\n+\n+\treturn vfs_create_no_perm(idmap, dentry, mode, di);\n }\n EXPORT_SYMBOL(vfs_create);\n \n@@ -4314,21 +4349,43 @@ static inline int open_to_namei_flags(int flag)\n \n static int may_o_create(const struct mnt_idmap *idmap,\n \t\t\tconst struct path *dir, struct dentry *dentry,\n-\t\t\tumode_t mode)\n+\t\t\tint open_flag, umode_t mode)\n {\n-\tint error = security_path_mknod(dir, dentry, mode, 0);\n+\tstruct inode *dir_inode = dir-\u003edentry-\u003ed_inode;\n+\tbool create_dir = O_IS_MKDIR(open_flag);\n+\tint error;\n+\n+\tWARN_ON_ONCE(create_dir \u0026\u0026 !S_ISDIR(mode));\n+\n+\tif (create_dir)\n+\t\terror = security_path_mkdir(dir, dentry, mode);\n+\telse\n+\t\terror = security_path_mknod(dir, dentry, mode, 0);\n \tif (error)\n \t\treturn error;\n \n \tif (!fsuidgid_has_mapping(dir-\u003edentry-\u003ed_sb, idmap))\n \t\treturn -EOVERFLOW;\n \n-\terror = inode_permission(idmap, dir-\u003edentry-\u003ed_inode,\n-\t\t\t\t MAY_WRITE | MAY_EXEC);\n+\terror = inode_permission(idmap, dir_inode, MAY_WRITE | MAY_EXEC);\n \tif (error)\n \t\treturn error;\n \n-\treturn security_inode_create(idmap, dir-\u003edentry-\u003ed_inode, dentry, mode);\n+\tif (create_dir)\n+\t\terror = security_inode_mkdir(idmap, dir_inode, dentry, mode);\n+\telse\n+\t\terror = security_inode_create(idmap, dir_inode, dentry, mode);\n+\n+\treturn error;\n+}\n+\n+static inline umode_t o_create_mode(const struct mnt_idmap *idmap, const struct inode *dir,\n+\t\t\t\t int open_flag, umode_t mode)\n+{\n+\tif (O_IS_MKDIR(open_flag))\n+\t\treturn vfs_prepare_mode(idmap, dir, mode, S_IRWXUGO | S_ISVTX, S_IFDIR);\n+\telse\n+\t\treturn vfs_prepare_mode(idmap, dir, mode, S_IALLUGO, S_IFREG);\n }\n \n /**\n@@ -4364,8 +4421,10 @@ static struct dentry *atomic_open(const struct path *path, struct dentry *dentry\n \n \tfile-\u003e__f_path.dentry = DENTRY_NOT_SET;\n \tfile-\u003e__f_path.mnt = path-\u003emnt;\n+\n \terror = dir_inode-\u003ei_op-\u003eatomic_open(dir_inode, dentry, file,\n-\t\t\t\t open_to_namei_flags(open_flag), mode);\n+\t\t\t\t\t open_to_namei_flags(open_flag), mode);\n+\n \td_lookup_done(dentry);\n \n \tif (!error) {\n@@ -4409,11 +4468,24 @@ static struct dentry *atomic_open(const struct path *path, struct dentry *dentry\n \t\t\terror = create_error;\n \t\t}\n \t\tdput(dentry);\n-\t\tdentry = ERR_PTR(error);\n+\t\treturn ERR_PTR(error);\n \t}\n+\n+\tif (file-\u003ef_mode \u0026 FMODE_CREATED) {\n+\t\tif (d_is_dir(dentry))\n+\t\t\tfsnotify_mkdir(dir_inode, dentry);\n+\t\telse\n+\t\t\tfsnotify_create(dir_inode, dentry);\n+\t}\n+\tif (file-\u003ef_mode \u0026 FMODE_OPENED)\n+\t\tfsnotify_open(file);\n+\n \treturn dentry;\n }\n \n+static inline\n+struct dentry *vfs_mkdir_no_perm(const struct mnt_idmap *, struct inode *, struct dentry *,\n+\t\t\t\t umode_t, struct delegated_inode *);\n /*\n * Look up and maybe create and open the last component.\n *\n@@ -4435,6 +4507,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,\n \tconst struct mnt_idmap *idmap;\n \tstruct dentry *dir = nd-\u003epath.dentry;\n \tstruct inode *dir_inode = dir-\u003ed_inode;\n+\tbool create_dir = O_IS_MKDIR(op-\u003eopen_flag);\n \tint open_flag;\n \tstruct dentry *dentry;\n \tint error, create_error;\n@@ -4449,6 +4522,9 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,\n \tcreate_error = 0;\n \tshared_flag = (open_flag \u0026 O_CREAT) ? 0 : LOOKUP_SHARED;\n \n+\tif (create_dir \u0026\u0026 (dir_inode-\u003ei_sb-\u003es_iflags \u0026 SB_I_NO_ATOMIC_MKDIR))\n+\t\treturn ERR_PTR(-EOPNOTSUPP);\n+\n \tif (open_flag \u0026 (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {\n \t\tgot_write = !mnt_want_write(nd-\u003epath.mnt);\n \t\t/*\n@@ -4456,10 +4532,11 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,\n \t\t * a different error; we'll be dropping this one anyway.\n \t\t */\n \t}\n+\n \tif (shared_flag)\n-\t\tinode_lock_shared(dir_inode);\n+\t\tinode_lock_shared_nested(dir_inode, I_MUTEX_PARENT);\n \telse\n-\t\tinode_lock(dir_inode);\n+\t\tinode_lock_nested(dir_inode, I_MUTEX_PARENT);\n \n \tif (unlikely(IS_DEADDIR(dir_inode))) {\n \t\tdentry = ERR_PTR(-ENOENT);\n@@ -4509,10 +4586,10 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,\n \tif (open_flag \u0026 O_CREAT) {\n \t\tif (open_flag \u0026 O_EXCL)\n \t\t\topen_flag \u0026= ~O_TRUNC;\n-\t\tmode = vfs_prepare_mode(idmap, dir_inode, mode, mode, mode);\n+\t\tmode = o_create_mode(idmap, dir_inode, open_flag, mode);\n \t\tif (likely(got_write))\n \t\t\tcreate_error = may_o_create(idmap, \u0026nd-\u003epath,\n-\t\t\t\t\t\t dentry, mode);\n+\t\t\t\t\t\t dentry, open_flag, mode);\n \t\telse\n \t\t\tcreate_error = -EROFS;\n \t}\n@@ -4526,19 +4603,10 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,\n \t\tgoto out;\n \t}\n \n-\tif (d_in_lookup(dentry)) {\n-\t\tstruct dentry *res = dir_inode-\u003ei_op-\u003elookup(dir_inode, dentry,\n-\t\t\t\t\t\t\t nd-\u003eflags | shared_flag);\n-\t\td_lookup_done(dentry);\n-\t\tif (unlikely(res)) {\n-\t\t\tif (IS_ERR(res)) {\n-\t\t\t\terror = PTR_ERR(res);\n-\t\t\t\tgoto out_dput;\n-\t\t\t}\n-\t\t\tdput(dentry);\n-\t\t\tdentry = res;\n-\t\t}\n-\t}\n+\tdentry = finish_lookup(dir_inode, dentry, nd-\u003eflags | shared_flag);\n+\tif (IS_ERR(dentry))\n+\t\tgoto out;\n+\n \tif (dentry-\u003ed_inode || !(op-\u003eopen_flag \u0026 O_CREAT)) {\n \t\t/*\n \t\t * No need to create a file. If lookup returned a positive\n@@ -4556,26 +4624,33 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,\n \t\tgoto out_dput;\n \t}\n \n-\terror = try_break_deleg(dir_inode, LEASE_BREAK_DIR_CREATE, \u0026delegated_inode);\n-\tif (error)\n-\t\tgoto out_dput;\n-\n-\tfile-\u003ef_mode |= FMODE_CREATED;\n-\tif (!dir_inode-\u003ei_op-\u003ecreate) {\n-\t\terror = -EACCES;\n+\tif ((create_dir \u0026\u0026 !dir_inode-\u003ei_op-\u003emkdir)\n+\t\t|| (!create_dir \u0026\u0026 !dir_inode-\u003ei_op-\u003ecreate)) {\n+\t\terror = -EOPNOTSUPP;\n \t\tgoto out_dput;\n \t}\n \n-\terror = dir_inode-\u003ei_op-\u003ecreate(idmap, dir_inode, dentry, mode);\n+\tif (create_dir) {\n+\t\tstruct dentry *res = vfs_mkdir_no_perm(idmap, dir_inode, dentry, mode,\n+\t\t\t\t\t\t \u0026delegated_inode);\n+\t\terror = PTR_ERR_OR_ZERO(res);\n+\t\tif (!error) {\n+\t\t\tdentry = res;\n+\t\t\t/*\n+\t\t\t * Filesystems whose -\u003emkdir() may leave the dentry\n+\t\t\t * negative must set SB_I_NO_ATOMIC_MKDIR.\n+\t\t\t */\n+\t\t\tif (WARN_ON_ONCE(d_is_negative(dentry)))\n+\t\t\t\terror = -EIO;\n+\t\t}\n+\t} else {\n+\t\terror = vfs_create_no_perm(idmap, dentry, mode, \u0026delegated_inode);\n+\t}\n \tif (error)\n \t\tgoto out_dput;\n+\n+\tfile-\u003ef_mode |= FMODE_CREATED;\n out:\n-\tif (!IS_ERR(dentry)) {\n-\t\tif (file-\u003ef_mode \u0026 FMODE_CREATED)\n-\t\t\tfsnotify_create(dir_inode, dentry);\n-\t\tif (file-\u003ef_mode \u0026 FMODE_OPENED)\n-\t\t\tfsnotify_open(file);\n-\t}\n \tif (shared_flag)\n \t\tinode_unlock_shared(dir_inode);\n \telse\n@@ -4697,17 +4772,12 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,\n }\n EXPORT_SYMBOL_FOR_MODULES(vfs_lookup_open, \"nfsd\");\n \n-static inline bool trailing_slashes(struct nameidata *nd)\n-{\n-\treturn (bool)nd-\u003elast.name[nd-\u003elast.len];\n-}\n-\n static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag)\n {\n \tstruct dentry *dentry;\n \n \tif (open_flag \u0026 O_CREAT) {\n-\t\tif (trailing_slashes(nd))\n+\t\tif (trailing_slashes(\u0026nd-\u003elast) \u0026\u0026 !(open_flag \u0026 O_DIRECTORY))\n \t\t\treturn ERR_PTR(-EISDIR);\n \n \t\t/* Don't bother on an O_EXCL create */\n@@ -4715,7 +4785,7 @@ static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag)\n \t\t\treturn NULL;\n \t}\n \n-\tif (trailing_slashes(nd))\n+\tif (trailing_slashes(\u0026nd-\u003elast))\n \t\tnd-\u003eflags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;\n \n \tdentry = lookup_fast(nd);\n@@ -4744,6 +4814,13 @@ static const char *open_last_lookups(struct nameidata *nd,\n \tif (nd-\u003elast_type != LAST_NORM) {\n \t\tif (nd-\u003edepth)\n \t\t\tput_link(nd);\n+\t\t/*\n+\t\t * Return -EOPNOTSUPP consistently, not -EEXIST, for paths\n+\t\t * ending in . or .., as lookup_open() does for other names.\n+\t\t */\n+\t\tif (O_IS_MKDIR(open_flag) \u0026\u0026\n+\t\t (nd-\u003epath.dentry-\u003ed_sb-\u003es_iflags \u0026 SB_I_NO_ATOMIC_MKDIR))\n+\t\t\treturn ERR_PTR(-EOPNOTSUPP);\n \t\treturn handle_dots(nd, nd-\u003elast_type);\n \t}\n \n@@ -4808,7 +4885,7 @@ static int do_open(struct nameidata *nd,\n \tif (open_flag \u0026 O_CREAT) {\n \t\tif ((open_flag \u0026 O_EXCL) \u0026\u0026 !(file-\u003ef_mode \u0026 FMODE_CREATED))\n \t\t\treturn -EEXIST;\n-\t\tif (d_is_dir(nd-\u003epath.dentry))\n+\t\tif (!(open_flag \u0026 O_DIRECTORY) \u0026\u0026 d_is_dir(nd-\u003epath.dentry))\n \t\t\treturn -EISDIR;\n \t\terror = may_create_in_sticky(idmap, nd,\n \t\t\t\t\t d_backing_inode(nd-\u003epath.dentry));\n@@ -5097,7 +5174,7 @@ static struct dentry *filename_create(int dfd, struct filename *name,\n \t * Do the final lookup. Suppress 'create' if there is a trailing\n \t * '/', and a directory wasn't requested.\n \t */\n-\tif (last.name[last.len] \u0026\u0026 !want_dir)\n+\tif (trailing_slashes(\u0026last) \u0026\u0026 !want_dir)\n \t\tcreate_flags \u0026= ~LOOKUP_CREATE;\n \tdentry = start_dirop(path-\u003edentry, \u0026last, reval_flag | create_flags);\n \tif (IS_ERR(dentry))\n@@ -5155,7 +5232,7 @@ inline struct dentry *start_creating_user_path(\n EXPORT_SYMBOL(start_creating_user_path);\n \n /**\n- * dentry_create - Create and open a file\n+ * dentry_create - Create and open a regular file\n * @path: path to create\n * @flags: O\\_ flags\n * @mode: mode bits for new file\n@@ -5192,7 +5269,7 @@ struct file *dentry_create(struct path *path, int flags, umode_t mode,\n \t\tpath-\u003edentry = dir;\n \t\tmode = vfs_prepare_mode(idmap, dir_inode, mode, S_IALLUGO, S_IFREG);\n \n-\t\tcreate_error = may_o_create(idmap, path, dentry, mode);\n+\t\tcreate_error = may_o_create(idmap, path, dentry, flags, mode);\n \t\tif (create_error)\n \t\t\tflags \u0026= ~O_CREAT;\n \n@@ -5208,13 +5285,6 @@ struct file *dentry_create(struct path *path, int flags, umode_t mode,\n \t\t\t/* Drop the extra reference */\n \t\t\tdput(orig_dentry);\n \n-\t\tif (!error) {\n-\t\t\tif (file-\u003ef_mode \u0026 FMODE_CREATED)\n-\t\t\t\tfsnotify_create(dir-\u003ed_inode, dentry);\n-\t\t\tif (file-\u003ef_mode \u0026 FMODE_OPENED)\n-\t\t\t\tfsnotify_open(file);\n-\t\t}\n-\n \t\tpath-\u003edentry = dentry;\n \n \t} else {\n@@ -5366,6 +5436,33 @@ SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, d\n \treturn filename_mknodat(AT_FDCWD, name, mode, dev);\n }\n \n+static inline\n+struct dentry *vfs_mkdir_no_perm(const struct mnt_idmap *idmap, struct inode *dir,\n+\t\t\t\t struct dentry *dentry, umode_t mode,\n+\t\t\t\t struct delegated_inode *di)\n+{\n+\tunsigned max_links = dir-\u003ei_sb-\u003es_max_links;\n+\tstruct dentry *de;\n+\tint error;\n+\n+\tif (max_links \u0026\u0026 dir-\u003ei_nlink \u003e= max_links)\n+\t\treturn ERR_PTR(-EMLINK);\n+\n+\terror = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, di);\n+\tif (error)\n+\t\treturn ERR_PTR(error);\n+\n+\tde = dir-\u003ei_op-\u003emkdir(idmap, dir, dentry, mode);\n+\tif (IS_ERR(de))\n+\t\treturn de;\n+\tif (de) {\n+\t\tdput(dentry);\n+\t\tdentry = de;\n+\t}\n+\tfsnotify_mkdir(dir, dentry);\n+\treturn dentry;\n+}\n+\n /**\n * vfs_mkdir - create directory returning correct dentry if possible\n * @idmap:\t\tidmap of the mount the inode was found from\n@@ -5393,14 +5490,13 @@ struct dentry *vfs_mkdir(const struct mnt_idmap *idmap, struct inode *dir,\n \t\t\t struct delegated_inode *delegated_inode)\n {\n \tint error;\n-\tunsigned max_links = dir-\u003ei_sb-\u003es_max_links;\n \tstruct dentry *de;\n \n \terror = may_create_dentry(idmap, dir, dentry);\n \tif (error)\n \t\tgoto err;\n \n-\terror = -EPERM;\n+\terror = -EOPNOTSUPP;\n \tif (!dir-\u003ei_op-\u003emkdir)\n \t\tgoto err;\n \n@@ -5409,24 +5505,12 @@ struct dentry *vfs_mkdir(const struct mnt_idmap *idmap, struct inode *dir,\n \tif (error)\n \t\tgoto err;\n \n-\terror = -EMLINK;\n-\tif (max_links \u0026\u0026 dir-\u003ei_nlink \u003e= max_links)\n+\tde = vfs_mkdir_no_perm(idmap, dir, dentry, mode, delegated_inode);\n+\tif (IS_ERR(de)) {\n+\t\terror = PTR_ERR(de);\n \t\tgoto err;\n-\n-\terror = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, delegated_inode);\n-\tif (error)\n-\t\tgoto err;\n-\n-\tde = dir-\u003ei_op-\u003emkdir(idmap, dir, dentry, mode);\n-\terror = PTR_ERR(de);\n-\tif (IS_ERR(de))\n-\t\tgoto err;\n-\tif (de) {\n-\t\tdput(dentry);\n-\t\tdentry = de;\n \t}\n-\tfsnotify_mkdir(dir, dentry);\n-\treturn dentry;\n+\treturn de;\n \n err:\n \tend_creating(dentry);\n@@ -5713,7 +5797,7 @@ int filename_unlinkat(int dfd, struct filename *name)\n \t\tgoto exit_drop_write;\n \n \t/* Why not before? Because we want correct error value */\n-\tif (unlikely(last.name[last.len])) {\n+\tif (unlikely(trailing_slashes(\u0026last))) {\n \t\tif (d_is_dir(dentry))\n \t\t\terror = -EISDIR;\n \t\telse\n@@ -6315,16 +6399,16 @@ int filename_renameat2(int olddfd, struct filename *from,\n \tif (flags \u0026 RENAME_EXCHANGE) {\n \t\tif (!d_is_dir(rd.new_dentry)) {\n \t\t\terror = -ENOTDIR;\n-\t\t\tif (new_last.name[new_last.len])\n+\t\t\tif (trailing_slashes(\u0026new_last))\n \t\t\t\tgoto exit_unlock;\n \t\t}\n \t}\n \t/* unless the source is a directory trailing slashes give -ENOTDIR */\n \tif (!d_is_dir(rd.old_dentry)) {\n \t\terror = -ENOTDIR;\n-\t\tif (old_last.name[old_last.len])\n+\t\tif (trailing_slashes(\u0026old_last))\n \t\t\tgoto exit_unlock;\n-\t\tif (!(flags \u0026 RENAME_EXCHANGE) \u0026\u0026 new_last.name[new_last.len])\n+\t\tif (!(flags \u0026 RENAME_EXCHANGE) \u0026\u0026 trailing_slashes(\u0026new_last))\n \t\t\tgoto exit_unlock;\n \t}\n \ndiff --git a/fs/nfs/super.c b/fs/nfs/super.c\nindex cb19f1540d984..f4452b630f52b 100644\n--- a/fs/nfs/super.c\n+++ b/fs/nfs/super.c\n@@ -1077,6 +1077,7 @@ static void nfs_fill_super(struct super_block *sb, struct nfs_fs_context *ctx)\n \tsb-\u003es_blocksize = 0;\n \tsb-\u003es_xattr = server-\u003enfs_client-\u003ecl_nfs_mod-\u003exattr;\n \tsb-\u003es_op = server-\u003enfs_client-\u003ecl_nfs_mod-\u003esops;\n+\tsb-\u003es_iflags |= SB_I_NO_ATOMIC_MKDIR;\n \tif (server-\u003ebsize)\n \t\tsb-\u003es_blocksize =\n \t\t\tnfs_block_size(server-\u003ebsize, \u0026sb-\u003es_blocksize_bits);\ndiff --git a/fs/open.c b/fs/open.c\nindex e43f02ff64acb..8217106a6f4b4 100644\n--- a/fs/open.c\n+++ b/fs/open.c\n@@ -1264,29 +1264,30 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)\n \tif (WILL_CREATE(flags)) {\n \t\tif (how-\u003emode \u0026 ~S_IALLUGO)\n \t\t\treturn -EINVAL;\n-\t\top-\u003emode = how-\u003emode | S_IFREG;\n+\t\tif (O_IS_MKDIR(flags))\n+\t\t\top-\u003emode = how-\u003emode | S_IFDIR;\n+\t\telse\n+\t\t\top-\u003emode = how-\u003emode | S_IFREG;\n \t} else {\n \t\tif (how-\u003emode != 0)\n \t\t\treturn -EINVAL;\n \t\top-\u003emode = 0;\n \t}\n \n-\t/*\n-\t * Block bugs where O_DIRECTORY | O_CREAT created regular files.\n-\t * Note, that blocking O_DIRECTORY | O_CREAT here also protects\n-\t * O_TMPFILE below which requires O_DIRECTORY being raised.\n-\t */\n-\tif ((flags \u0026 (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT))\n-\t\treturn -EINVAL;\n-\n \t/* Now handle the creative implementation of O_TMPFILE. */\n \tif (flags \u0026 __O_TMPFILE) {\n \t\t/*\n \t\t * In order to ensure programs get explicit errors when trying\n \t\t * to use O_TMPFILE on old kernels we enforce that O_DIRECTORY\n-\t\t * is raised alongside __O_TMPFILE.\n+\t\t * is raised alongside __O_TMPFILE, but without O_CREAT. The\n+\t\t * reason for disallowing O_CREAT|O_TMPFILE is that\n+\t\t * O_DIRECTORY|O_CREAT used to work and created a regular file\n+\t\t * if nothing existed at the open path. Hence, allowing the\n+\t\t * combination would have caused O_CREAT|O_TMPFILE to create a\n+\t\t * regular (non-temporary) file on old kernels, while the caller\n+\t\t * would believe they created an actual O_TMPFILE.\n \t\t */\n-\t\tif (!(flags \u0026 O_DIRECTORY))\n+\t\tif (!(flags \u0026 O_DIRECTORY) || (flags \u0026 O_CREAT))\n \t\t\treturn -EINVAL;\n \t\tif (!(acc_mode \u0026 MAY_WRITE))\n \t\t\treturn -EINVAL;\n@@ -1343,12 +1344,29 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)\n \n \top-\u003eintent = flags \u0026 O_PATH ? 0 : LOOKUP_OPEN;\n \n+\t/*\n+\t * Requesting write access on a directory can never succeed. Rather\n+\t * than performing a path-walk to determine whether the target is\n+\t * actually a directory (-EISDIR) or not (-ENOTDIR), we short-circuit\n+\t * to -ENOTDIR.\n+\t */\n+\tif ((flags \u0026 O_DIRECTORY) \u0026\u0026 !(flags \u0026 __O_TMPFILE) \u0026\u0026 (acc_mode \u0026 MAY_WRITE))\n+\t\treturn -ENOTDIR;\n+\n \tif (flags \u0026 O_CREAT) {\n-\t\top-\u003eintent |= LOOKUP_CREATE;\n+\t\tif (flags \u0026 O_DIRECTORY) {\n+\t\t\t/*\n+\t\t\t * O_CREAT|O_DIRECTORY is provided to prevent TOCTOUs, so\n+\t\t\t * it makes little sense to allow it without O_EXCL.\n+\t\t\t */\n+\t\t\tif (!(flags \u0026 O_EXCL))\n+\t\t\t\treturn -EINVAL;\n+\t\t}\n \t\tif (flags \u0026 O_EXCL) {\n \t\t\top-\u003eintent |= LOOKUP_EXCL;\n \t\t\tflags |= O_NOFOLLOW;\n \t\t}\n+\t\top-\u003eintent |= LOOKUP_CREATE;\n \t}\n \n \tif (flags \u0026 O_DIRECTORY)\ndiff --git a/fs/resctrl/rdtgroup.c b/fs/resctrl/rdtgroup.c\nindex 57ab090072c21..be151cc8fd29c 100644\n--- a/fs/resctrl/rdtgroup.c\n+++ b/fs/resctrl/rdtgroup.c\n@@ -4040,15 +4040,16 @@ static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)\n * and monitor group(ctrl_mon). This is a resource group\n * to monitor a subset of tasks and cpus in its parent ctrl_mon group.\n */\n-static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,\n-\t\t\t const char *name, umode_t mode)\n+static struct kernfs_node *rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,\n+\t\t\t\t\t const char *name, umode_t mode)\n {\n \tstruct rdtgroup *rdtgrp, *prgrp;\n+\tstruct kernfs_node *kn;\n \tint ret;\n \n \tret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, \u0026rdtgrp);\n \tif (ret)\n-\t\treturn ret;\n+\t\treturn ERR_PTR(ret);\n \n \tprgrp = rdtgrp-\u003emon.parent;\n \trdtgrp-\u003eclosid = prgrp-\u003eclosid;\n@@ -4067,17 +4068,22 @@ static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,\n \t */\n \tlist_add_tail(\u0026rdtgrp-\u003emon.crdtgrp_list, \u0026prgrp-\u003emon.crdtgrp_list);\n \n+\tkn = rdtgrp-\u003ekn;\n+\tkernfs_get(kn);\n+\trdtgroup_kn_unlock(parent_kn);\n+\treturn kn;\n+\n out_unlock:\n \trdtgroup_kn_unlock(parent_kn);\n-\treturn ret;\n+\treturn ERR_PTR(ret);\n }\n \n /*\n * These are rdtgroups created under the root directory. Can be used\n * to allocate and monitor resources.\n */\n-static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,\n-\t\t\t\t const char *name, umode_t mode)\n+static struct kernfs_node *rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,\n+\t\t\t\t\t\t const char *name, umode_t mode)\n {\n \tstruct rdtgroup *rdtgrp;\n \tstruct kernfs_node *kn;\n@@ -4086,7 +4092,7 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,\n \n \tret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, \u0026rdtgrp);\n \tif (ret)\n-\t\treturn ret;\n+\t\treturn ERR_PTR(ret);\n \n \tkn = rdtgrp-\u003ekn;\n \tret = closid_alloc();\n@@ -4125,7 +4131,9 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,\n \t\t\trdtgrp-\u003emba_mbps_event = mba_mbps_default_event;\n \t}\n \n-\tgoto out_unlock;\n+\tkernfs_get(kn);\n+\trdtgroup_kn_unlock(parent_kn);\n+\treturn kn;\n \n out_del_list:\n \tlist_del(\u0026rdtgrp-\u003erdtgroup_list);\n@@ -4135,17 +4143,16 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,\n \tclosid_free(closid);\n out_common_fail:\n \tmkdir_rdt_prepare_clean(rdtgrp);\n-out_unlock:\n \trdtgroup_kn_unlock(parent_kn);\n-\treturn ret;\n+\treturn ERR_PTR(ret);\n }\n \n-static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,\n-\t\t\t umode_t mode)\n+static struct kernfs_node *rdtgroup_mkdir(struct kernfs_node *parent_kn,\n+\t\t\t\t\t const char *name, umode_t mode)\n {\n \t/* Do not accept '\\n' to avoid unparsable situation. */\n \tif (strchr(name, '\\n'))\n-\t\treturn -EINVAL;\n+\t\treturn ERR_PTR(-EINVAL);\n \n \t/*\n \t * If the parent directory is the root directory and RDT\n@@ -4159,7 +4166,7 @@ static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,\n \tif (resctrl_arch_mon_capable())\n \t\treturn rdtgroup_mkdir_mon(parent_kn, name, mode);\n \n-\treturn -EPERM;\n+\treturn ERR_PTR(-EPERM);\n }\n \n static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)\ndiff --git a/fs/smb/client/cifsfs.c b/fs/smb/client/cifsfs.c\nindex 576155caf888e..f00dd2530b758 100644\n--- a/fs/smb/client/cifsfs.c\n+++ b/fs/smb/client/cifsfs.c\n@@ -246,6 +246,7 @@ cifs_read_super(struct super_block *sb)\n \n \tsb-\u003es_magic = CIFS_SUPER_MAGIC;\n \tsb-\u003es_op = \u0026cifs_super_ops;\n+\tsb-\u003es_iflags |= SB_I_NO_ATOMIC_MKDIR;\n \tsb-\u003es_xattr = cifs_xattr_handlers;\n \trc = super_setup_bdi(sb);\n \tif (rc)\ndiff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c\nindex 5020365ca7048..b679adf68c8fb 100644\n--- a/fs/tracefs/inode.c\n+++ b/fs/tracefs/inode.c\n@@ -471,6 +471,7 @@ static int tracefs_fill_super(struct super_block *sb, struct fs_context *fc)\n \t\treturn err;\n \n \tsb-\u003es_op = \u0026tracefs_super_operations;\n+\tsb-\u003es_iflags |= SB_I_NO_ATOMIC_MKDIR;\n \ttracefs_apply_options(sb, false);\n \tset_default_d_op(sb, \u0026tracefs_dentry_operations);\n \ndiff --git a/fs/vboxsf/super.c b/fs/vboxsf/super.c\nindex a61fbab51d370..190b76f04d4f8 100644\n--- a/fs/vboxsf/super.c\n+++ b/fs/vboxsf/super.c\n@@ -196,6 +196,7 @@ static int vboxsf_fill_super(struct super_block *sb, struct fs_context *fc)\n \tsb-\u003es_blocksize = 1024;\n \tsb-\u003es_maxbytes = MAX_LFS_FILESIZE;\n \tsb-\u003es_op = \u0026vboxsf_super_ops;\n+\tsb-\u003es_iflags |= SB_I_NO_ATOMIC_MKDIR;\n \tset_default_d_op(sb, \u0026vboxsf_dentry_ops);\n \n \tiroot = iget_locked(sb, 0);\ndiff --git a/include/linux/fcntl.h b/include/linux/fcntl.h\nindex 6ad6b9e7a226a..204e16bbe2634 100644\n--- a/include/linux/fcntl.h\n+++ b/include/linux/fcntl.h\n@@ -30,6 +30,12 @@\n */\n #define __O_REGULAR\t\t(1 \u003c\u003c 30)\n \n+#define O_MKDIR_MASK\t(O_CREAT | O_DIRECTORY)\n+static inline bool O_IS_MKDIR(unsigned int flags)\n+{\n+\treturn (flags \u0026 O_MKDIR_MASK) == O_MKDIR_MASK;\n+}\n+\n /* List of all valid flags for the how-\u003eresolve argument: */\n #define VALID_RESOLVE_FLAGS \\\n \t(RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS | \\\ndiff --git a/include/linux/fs/super_types.h b/include/linux/fs/super_types.h\nindex ecd96aeb1cee7..521b00c47d0aa 100644\n--- a/include/linux/fs/super_types.h\n+++ b/include/linux/fs/super_types.h\n@@ -352,5 +352,6 @@ struct super_block {\n #define SB_I_NOIDMAP\t0x00002000\t/* No idmapped mounts on this superblock */\n #define SB_I_ALLOW_HSM\t0x00004000\t/* Allow HSM events on this superblock */\n #define SB_I_NO_DATA_INTEGRITY\t0x00008000 /* fs cannot guarantee data persistence on sync */\n+#define SB_I_NO_ATOMIC_MKDIR\t0x00010000 /* no O_CREAT|O_DIRECTORY on this fs */\n \n #endif /* _LINUX_FS_SUPER_TYPES_H */\ndiff --git a/include/linux/kernfs.h b/include/linux/kernfs.h\nindex 73786b567ccd8..2a7baa6a56bc0 100644\n--- a/include/linux/kernfs.h\n+++ b/include/linux/kernfs.h\n@@ -241,13 +241,14 @@ struct kernfs_node {\n * syscalls. These optional callbacks are invoked on the matching syscalls\n * and can perform any kernfs operations which don't necessarily have to be\n * the exact operation requested. An active reference is held for each\n- * kernfs_node parameter.\n+ * kernfs_node parameter. @mkdir returns the kernfs_node of the new\n+ * directory with a reference held, or an ERR_PTR() on failure.\n */\n struct kernfs_syscall_ops {\n \tint (*show_options)(struct seq_file *sf, struct kernfs_root *root);\n \n-\tint (*mkdir)(struct kernfs_node *parent, const char *name,\n-\t\t umode_t mode);\n+\tstruct kernfs_node *(*mkdir)(struct kernfs_node *parent,\n+\t\t\t\t const char *name, umode_t mode);\n \tint (*rmdir)(struct kernfs_node *kn);\n \tint (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,\n \t\t const char *new_name);\ndiff --git a/include/uapi/asm-generic/errno.h b/include/uapi/asm-generic/errno.h\nindex bd78e69e0a43f..c84ebf89c8b6e 100644\n--- a/include/uapi/asm-generic/errno.h\n+++ b/include/uapi/asm-generic/errno.h\n@@ -76,7 +76,7 @@\n #define\tENOPROTOOPT\t92\t/* Protocol not available */\n #define\tEPROTONOSUPPORT\t93\t/* Protocol not supported */\n #define\tESOCKTNOSUPPORT\t94\t/* Socket type not supported */\n-#define\tEOPNOTSUPP\t95\t/* Operation not supported on transport endpoint */\n+#define\tEOPNOTSUPP\t95\t/* Operation not supported */\n #define\tEPFNOSUPPORT\t96\t/* Protocol family not supported */\n #define\tEAFNOSUPPORT\t97\t/* Address family not supported by protocol */\n #define\tEADDRINUSE\t98\t/* Address already in use */\ndiff --git a/kernel/cgroup/cgroup-internal.h b/kernel/cgroup/cgroup-internal.h\nindex 233fb8b45d2c3..5d05fd75d0c04 100644\n--- a/kernel/cgroup/cgroup-internal.h\n+++ b/kernel/cgroup/cgroup-internal.h\n@@ -256,7 +256,8 @@ void cgroup_procs_write_finish(struct task_struct *task,\n \n int cgroup_lock_and_drain_offline(struct cgroup *cgrp);\n \n-int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode);\n+struct kernfs_node *cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,\n+\t\t\t\t umode_t mode);\n int cgroup_rmdir(struct kernfs_node *kn);\n int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,\n \t\t struct kernfs_root *kf_root);\ndiff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c\nindex 14634246a666d..a52d754478320 100644\n--- a/kernel/cgroup/cgroup.c\n+++ b/kernel/cgroup/cgroup.c\n@@ -6149,18 +6149,20 @@ static bool cgroup_check_hierarchy_limits(struct cgroup *parent)\n \treturn ret;\n }\n \n-int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)\n+struct kernfs_node *cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,\n+\t\t\t\t umode_t mode)\n {\n \tstruct cgroup *parent, *cgrp;\n+\tstruct kernfs_node *kn;\n \tint ret;\n \n \t/* do not accept '\\n' to prevent making /proc/\u003cpid\u003e/cgroup unparsable */\n \tif (strchr(name, '\\n'))\n-\t\treturn -EINVAL;\n+\t\treturn ERR_PTR(-EINVAL);\n \n \tparent = cgroup_kn_lock_live(parent_kn, false);\n \tif (IS_ERR(parent))\n-\t\treturn PTR_ERR(parent);\n+\t\treturn ERR_CAST(parent);\n \n \tif (!cgroup_check_hierarchy_limits(parent)) {\n \t\tret = -EAGAIN;\n@@ -6192,14 +6194,16 @@ int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)\n \t/* let's create and online css's */\n \tkernfs_activate(cgrp-\u003ekn);\n \n-\tret = 0;\n-\tgoto out_unlock;\n+\tkn = cgrp-\u003ekn;\n+\tkernfs_get(kn);\n+\tcgroup_kn_unlock(parent_kn);\n+\treturn kn;\n \n out_destroy:\n \tcgroup_destroy_locked(cgrp);\n out_unlock:\n \tcgroup_kn_unlock(parent_kn);\n-\treturn ret;\n+\treturn ERR_PTR(ret);\n }\n \n /*\ndiff --git a/security/apparmor/apparmorfs.c b/security/apparmor/apparmorfs.c\nindex d1386722537d0..36c9bb7ebf3d8 100644\n--- a/security/apparmor/apparmorfs.c\n+++ b/security/apparmor/apparmorfs.c\n@@ -248,6 +248,7 @@ static int apparmorfs_fill_super(struct super_block *sb, struct fs_context *fc)\n \tif (error)\n \t\treturn error;\n \tsb-\u003es_op = \u0026aafs_super_ops;\n+\tsb-\u003es_iflags |= SB_I_NO_ATOMIC_MKDIR;\n \n \treturn 0;\n }\ndiff --git a/tools/testing/selftests/filesystems/.gitignore b/tools/testing/selftests/filesystems/.gitignore\nindex cf79000e4092a..8ccf7a5f9ec9d 100644\n--- a/tools/testing/selftests/filesystems/.gitignore\n+++ b/tools/testing/selftests/filesystems/.gitignore\n@@ -1,4 +1,5 @@\n # SPDX-License-Identifier: GPL-2.0-only\n+open_o_creat_o_dir\n dnotify_test\n devpts_pts\n fclog\ndiff --git a/tools/testing/selftests/filesystems/Makefile b/tools/testing/selftests/filesystems/Makefile\nindex fbd5c27505ef5..004b946fd2e0b 100644\n--- a/tools/testing/selftests/filesystems/Makefile\n+++ b/tools/testing/selftests/filesystems/Makefile\n@@ -1,7 +1,7 @@\n # SPDX-License-Identifier: GPL-2.0\n \n CFLAGS += $(KHDR_INCLUDES)\n-TEST_GEN_PROGS := devpts_pts anon_inode_test kernfs_test fclog ustat_test rw_hint_test\n+TEST_GEN_PROGS := devpts_pts anon_inode_test kernfs_test fclog ustat_test rw_hint_test open_o_creat_o_dir \n TEST_GEN_PROGS += idmapped_tmpfile\n TEST_GEN_PROGS_EXTENDED := dnotify_test\n \ndiff --git a/tools/testing/selftests/filesystems/open_o_creat_o_dir.c b/tools/testing/selftests/filesystems/open_o_creat_o_dir.c\nnew file mode 100644\nindex 0000000000000..93c0f4dea266a\n--- /dev/null\n+++ b/tools/testing/selftests/filesystems/open_o_creat_o_dir.c\n@@ -0,0 +1,346 @@\n+// SPDX-License-Identifier: GPL-2.0\n+#include \u003csys/stat.h\u003e\n+#include \u003csys/vfs.h\u003e\n+#include \u003cerrno.h\u003e\n+#include \u003climits.h\u003e\n+#include \u003cfcntl.h\u003e\n+#include \u003cstdio.h\u003e\n+#include \u003cstring.h\u003e\n+#include \u003cunistd.h\u003e\n+#include \u003clinux/magic.h\u003e\n+\n+#include \"kselftest_harness.h\"\n+#include \"wrappers.h\"\n+\n+#define openat_o_mkdir_checked_flags(dfd, pathname, flags) ({\t\\\n+\tstruct stat __st;\t\t\t\t\t\t\\\n+\tint __fd = openat_o_mkdir(dfd, pathname, flags, S_IRWXU);\t\\\n+\tASSERT_GE(__fd, 0);\t\t\t\t\t\t\\\n+\tASSERT_EQ(fstat(__fd, \u0026__st), 0);\t\t\t\t\\\n+\tEXPECT_TRUE(S_ISDIR(__st.st_mode));\t\t\t\t\\\n+\t__fd;\t\t\t\t\t\t\t\t\\\n+})\n+\n+#define openat_o_mkdir_checked(dfd, pathname) \\\n+\topenat_o_mkdir_checked_flags(dfd, pathname, O_RDONLY)\n+\n+FIXTURE(open_o_creat_o_dir) {\n+\tchar dirpath[PATH_MAX];\n+\tint dfd;\n+};\n+\n+FIXTURE_SETUP(open_o_creat_o_dir)\n+{\n+\tstrcpy(self-\u003edirpath, \"/tmp/open_o_creat_o_dir_test.XXXXXX\");\n+\tASSERT_NE(mkdtemp(self-\u003edirpath), NULL);\n+\tself-\u003edfd = open(self-\u003edirpath, O_DIRECTORY);\n+\tASSERT_GE(self-\u003edfd, 0);\n+}\n+\n+FIXTURE_TEARDOWN(open_o_creat_o_dir)\n+{\n+\tclose(self-\u003edfd);\n+\trmdir(self-\u003edirpath);\n+}\n+\n+/* Does open_o_creat_o_dir return a fd at all? */\n+TEST_F(open_o_creat_o_dir, returns_fd)\n+{\n+\tint fd = openat_o_mkdir_checked(self-\u003edfd, \"newdir\");\n+\tEXPECT_EQ(close(fd), 0);\n+\tEXPECT_EQ(unlinkat(self-\u003edfd, \"newdir\", AT_REMOVEDIR), 0);\n+}\n+\n+/* The fd must refer to the directory that was just created. */\n+TEST_F(open_o_creat_o_dir, fd_is_created_dir)\n+{\n+\tint fd;\n+\tstruct stat st_via_fd, st_via_path;\n+\tchar path[PATH_MAX];\n+\n+\tfd = openat_o_mkdir_checked(self-\u003edfd, \"checkdir\");\n+\n+\tASSERT_EQ(fstat(fd, \u0026st_via_fd), 0);\n+\n+\tsnprintf(path, sizeof(path), \"%s/checkdir\", self-\u003edirpath);\n+\tASSERT_EQ(stat(path, \u0026st_via_path), 0);\n+\n+\tEXPECT_EQ(st_via_fd.st_ino, st_via_path.st_ino);\n+\tEXPECT_EQ(st_via_fd.st_dev, st_via_path.st_dev);\n+\n+\tEXPECT_EQ(close(fd), 0);\n+\tEXPECT_EQ(rmdir(path), 0);\n+}\n+\n+/* Missing parent component must fail with ENOENT. */\n+TEST_F(open_o_creat_o_dir, enoent_missing_parent)\n+{\n+\tEXPECT_EQ(openat_o_mkdir(self-\u003edfd, \"nonexistent/child\", O_RDONLY, S_IRWXU), -1);\n+\tEXPECT_EQ(errno, ENOENT);\n+}\n+\n+/* An invalid dfd must fail with EBADF. */\n+TEST_F(open_o_creat_o_dir, ebadf)\n+{\n+\tEXPECT_EQ(openat_o_mkdir(FD_INVALID, \"badfdir\", O_RDONLY, S_IRWXU), -1);\n+\tEXPECT_EQ(errno, EBADF);\n+}\n+\n+/* A dfd that points to a file (not a directory) must fail with ENOTDIR. */\n+TEST_F(open_o_creat_o_dir, enotdir_dfd)\n+{\n+\tint file_fd;\n+\n+\tfile_fd = openat(self-\u003edfd, \"file\",\n+\t\t\t O_CREAT | O_RDONLY, S_IRWXU);\n+\tASSERT_GE(file_fd, 0);\n+\n+\tEXPECT_EQ(openat_o_mkdir(file_fd, \"subdir\", O_RDONLY, S_IRWXU), -1);\n+\tEXPECT_EQ(errno, ENOTDIR);\n+\n+\tEXPECT_EQ(close(file_fd), 0);\n+\tEXPECT_EQ(unlinkat(self-\u003edfd, \"file\", 0), 0);\n+}\n+\n+/*\n+ * O_CREAT|O_DIRECTORY without O_EXCL must fail with EINVAL and must not\n+ * create anything at the path.\n+ */\n+TEST_F(open_o_creat_o_dir, requires_o_excl)\n+{\n+\tstruct stat st;\n+\n+\tEXPECT_EQ(openat(self-\u003edfd, \"noexcl\", O_CREAT | O_DIRECTORY | O_RDONLY,\n+\t\t\t S_IRWXU), -1);\n+\tEXPECT_EQ(errno, EINVAL);\n+\n+\tEXPECT_EQ(fstatat(self-\u003edfd, \"noexcl\", \u0026st, 0), -1);\n+\tEXPECT_EQ(errno, ENOENT);\n+}\n+\n+/*\n+ * Once the directory exists, repeating O_CREAT|O_DIRECTORY|O_EXCL must\n+ * fail with EEXIST.\n+ */\n+TEST_F(open_o_creat_o_dir, existing_dir_eexist)\n+{\n+\tint excldir_fd;\n+\n+\texcldir_fd = openat_o_mkdir_checked(self-\u003edfd, \"excldir\");\n+\n+\tEXPECT_EQ(openat_o_mkdir(self-\u003edfd, \"excldir\", O_RDONLY, S_IRWXU), -1);\n+\tEXPECT_EQ(errno, EEXIST);\n+\n+\tEXPECT_EQ(close(excldir_fd), 0);\n+\tEXPECT_EQ(unlinkat(self-\u003edfd, \"excldir\", AT_REMOVEDIR), 0);\n+}\n+\n+/*\n+ * O_CREAT|O_DIRECTORY combined with a writable access mode must be\n+ * rejected: a directory cannot be opened for writing.\n+ */\n+TEST_F(open_o_creat_o_dir, rejects_writable_acc_mode)\n+{\n+\tEXPECT_EQ(openat_o_mkdir(self-\u003edfd, \"rdwrdir\", O_RDWR, S_IRWXU), -1);\n+\tEXPECT_EQ(errno, ENOTDIR);\n+}\n+\n+/*\n+ * openat(O_CREAT|O_DIRECTORY) with a trailing slash should work.\n+ */\n+TEST_F(open_o_creat_o_dir, trailing_slash)\n+{\n+\tint fd = openat_o_mkdir_checked(self-\u003edfd, \"newdir/\");\n+\tEXPECT_EQ(close(fd), 0);\n+\tEXPECT_EQ(unlinkat(self-\u003edfd, \"newdir\", AT_REMOVEDIR), 0);\n+}\n+\n+/*\n+ * openat(O_CREAT) with a trailing slash but without O_DIRECTORY\n+ * must fail with EISDIR and must not create anything at the path.\n+ */\n+TEST_F(open_o_creat_o_dir, trailing_slash_no_o_dir)\n+{\n+\tint fd;\n+\tstruct stat st;\n+\n+\tfd = openat(self-\u003edfd, \"trailing/\", O_CREAT | O_RDONLY, S_IRWXU);\n+\tEXPECT_EQ(fd, -1);\n+\tEXPECT_EQ(errno, EISDIR);\n+\n+\tEXPECT_EQ(fstatat(self-\u003edfd, \"trailing\", \u0026st, 0), -1);\n+\tEXPECT_EQ(errno, ENOENT);\n+}\n+\n+/*\n+ * The returned fd must be usable as a dfd for further *at() calls.\n+ */\n+TEST_F(open_o_creat_o_dir, fd_usable_as_dfd)\n+{\n+\tint parent_fd, child_fd;\n+\tchar path[PATH_MAX];\n+\n+\tparent_fd = openat_o_mkdir_checked(self-\u003edfd, \"parent\");\n+\tchild_fd = openat_o_mkdir_checked(parent_fd, \"child\");\n+\n+\tEXPECT_EQ(close(child_fd), 0);\n+\tEXPECT_EQ(close(parent_fd), 0);\n+\n+\tsnprintf(path, sizeof(path), \"%s/parent/child\", self-\u003edirpath);\n+\tEXPECT_EQ(rmdir(path), 0);\n+\tsnprintf(path, sizeof(path), \"%s/parent\", self-\u003edirpath);\n+\tEXPECT_EQ(rmdir(path), 0);\n+}\n+\n+/*\n+ * O_EXCL means a trailing symlink is never followed, so O_CREAT|O_DIRECTORY\n+ * must refuse to create through a dangling one, and must not create\n+ * anything at the symlink target.\n+ */\n+TEST_F(open_o_creat_o_dir, dangling_symlink_eexist)\n+{\n+\tstruct stat st;\n+\n+\tASSERT_EQ(symlinkat(\"danglink_target\", self-\u003edfd, \"danglink\"), 0);\n+\n+\tEXPECT_EQ(openat_o_mkdir(self-\u003edfd, \"danglink\", O_RDONLY, S_IRWXU), -1);\n+\tEXPECT_EQ(errno, EEXIST);\n+\n+\t/* Nothing must have been created at the target. */\n+\tEXPECT_EQ(fstatat(self-\u003edfd, \"danglink_target\", \u0026st, 0), -1);\n+\tEXPECT_EQ(errno, ENOENT);\n+\n+\tEXPECT_EQ(unlinkat(self-\u003edfd, \"danglink\", 0), 0);\n+}\n+\n+/*\n+ * A trailing symlink is an existing entry, even if it resolves to a\n+ * directory, so O_CREAT|O_DIRECTORY must fail with EEXIST.\n+ */\n+TEST_F(open_o_creat_o_dir, symlink_to_dir_eexist)\n+{\n+\tASSERT_EQ(mkdirat(self-\u003edfd, \"realdir\", 0700), 0);\n+\tASSERT_EQ(symlinkat(\"realdir\", self-\u003edfd, \"dirlink\"), 0);\n+\n+\tEXPECT_EQ(openat_o_mkdir(self-\u003edfd, \"dirlink\", O_RDONLY, S_IRWXU), -1);\n+\tEXPECT_EQ(errno, EEXIST);\n+\n+\tEXPECT_EQ(unlinkat(self-\u003edfd, \"dirlink\", 0), 0);\n+\tEXPECT_EQ(unlinkat(self-\u003edfd, \"realdir\", AT_REMOVEDIR), 0);\n+}\n+\n+/*\n+ * O_TMPFILE is encoded as __O_TMPFILE|O_DIRECTORY. Now that O_CREAT is no\n+ * longer rejected alongside O_DIRECTORY, O_TMPFILE|O_CREAT must still be\n+ * rejected explicitly so that it cannot create a persistent file on kernels\n+ * that predate O_TMPFILE.\n+ */\n+TEST_F(open_o_creat_o_dir, tmpfile_with_o_creat_einval)\n+{\n+\tEXPECT_EQ(openat(self-\u003edfd, \".\", O_TMPFILE | O_CREAT | O_RDWR, S_IRWXU),\n+\t\t -1);\n+\tEXPECT_EQ(errno, EINVAL);\n+}\n+\n+/*\n+ * Open the first of @paths that is a mounted filesystem of type @magic.\n+ * Returns -1 if there is none.\n+ */\n+static int open_fs_root(const char *const *paths, long magic)\n+{\n+\tfor (; *paths; paths++) {\n+\t\tstruct statfs sfs;\n+\t\tint fd;\n+\n+\t\tfd = open(*paths, O_DIRECTORY | O_RDONLY);\n+\t\tif (fd \u003c 0)\n+\t\t\tcontinue;\n+\t\tif (!fstatfs(fd, \u0026sfs) \u0026\u0026 sfs.f_type == magic)\n+\t\t\treturn fd;\n+\t\tclose(fd);\n+\t}\n+\treturn -1;\n+}\n+\n+/*\n+ * kernfs: O_CREAT|O_DIRECTORY on cgroup2 must create a cgroup and return a\n+ * fd to that very cgroup.\n+ */\n+TEST(kernfs_cgroup2)\n+{\n+\tstatic const char *const paths[] = {\n+\t\t\"/sys/fs/cgroup\", \"/sys/fs/cgroup/unified\", NULL\n+\t};\n+\tstruct stat st_fd, st_path;\n+\tint root_fd, fd, procs_fd;\n+\tchar name[64];\n+\n+\tif (geteuid() != 0)\n+\t\tSKIP(return, \"needs root to create a cgroup\");\n+\troot_fd = open_fs_root(paths, CGROUP2_SUPER_MAGIC);\n+\tif (root_fd \u003c 0)\n+\t\tSKIP(return, \"cgroup2 is not mounted\");\n+\n+\tsnprintf(name, sizeof(name), \"open_o_creat_o_dir.%d\", getpid());\n+\n+\tfd = openat_o_mkdir_checked(root_fd, name);\n+\n+\t/* The fd refers to the cgroup that is now visible at the name. */\n+\tASSERT_EQ(fstat(fd, \u0026st_fd), 0);\n+\tASSERT_EQ(fstatat(root_fd, name, \u0026st_path, 0), 0);\n+\tEXPECT_EQ(st_fd.st_ino, st_path.st_ino);\n+\tEXPECT_EQ(st_fd.st_dev, st_path.st_dev);\n+\n+\t/* It is a populated cgroup, not just an empty directory. */\n+\tprocs_fd = openat(fd, \"cgroup.procs\", O_RDONLY);\n+\tEXPECT_GE(procs_fd, 0);\n+\tif (procs_fd \u003e= 0)\n+\t\tEXPECT_EQ(close(procs_fd), 0);\n+\n+\tEXPECT_EQ(close(fd), 0);\n+\tEXPECT_EQ(unlinkat(root_fd, name, AT_REMOVEDIR), 0);\n+\tEXPECT_EQ(close(root_fd), 0);\n+}\n+\n+/*\n+ * tracefs sets SB_I_NO_ATOMIC_MKDIR: O_CREAT|O_DIRECTORY is refused with\n+ * EOPNOTSUPP before any lookup, whether or not the name exists, and\n+ * creates nothing.\n+ */\n+TEST(tracefs_eopnotsupp)\n+{\n+\tstatic const char *const paths[] = {\n+\t\t\"/sys/kernel/tracing\", \"/sys/kernel/debug/tracing\", NULL\n+\t};\n+\tint tracing_fd, inst_fd;\n+\tchar name[64];\n+\tstruct stat st;\n+\n+\tif (geteuid() != 0)\n+\t\tSKIP(return, \"needs root to access tracefs\");\n+\ttracing_fd = open_fs_root(paths, TRACEFS_MAGIC);\n+\tif (tracing_fd \u003c 0)\n+\t\tSKIP(return, \"tracefs is not mounted\");\n+\tinst_fd = openat(tracing_fd, \"instances\", O_DIRECTORY | O_RDONLY);\n+\tif (inst_fd \u003c 0) {\n+\t\tclose(tracing_fd);\n+\t\tSKIP(return, \"no tracefs instances directory\");\n+\t}\n+\n+\tsnprintf(name, sizeof(name), \"open_o_creat_o_dir.%d\", getpid());\n+\n+\t/* Nonexistent name, where mkdir(2) would create an instance */\n+\tEXPECT_EQ(openat_o_mkdir(inst_fd, name, O_RDONLY, S_IRWXU), -1);\n+\tEXPECT_EQ(errno, EOPNOTSUPP);\n+\tEXPECT_EQ(fstatat(inst_fd, name, \u0026st, 0), -1);\n+\tEXPECT_EQ(errno, ENOENT);\n+\n+\t/* Existing name */\n+\tEXPECT_EQ(openat_o_mkdir(tracing_fd, \"instances\", O_RDONLY, S_IRWXU), -1);\n+\tEXPECT_EQ(errno, EOPNOTSUPP);\n+\n+\tEXPECT_EQ(close(inst_fd), 0);\n+\tEXPECT_EQ(close(tracing_fd), 0);\n+}\n+\n+TEST_HARNESS_MAIN\ndiff --git a/tools/testing/selftests/filesystems/wrappers.h b/tools/testing/selftests/filesystems/wrappers.h\nindex 420ae4f908cf2..bc58bdb800631 100644\n--- a/tools/testing/selftests/filesystems/wrappers.h\n+++ b/tools/testing/selftests/filesystems/wrappers.h\n@@ -13,6 +13,10 @@\n #define STATX_MNT_ID_UNIQUE 0x00004000U /* Want/got extended stx_mount_id */\n #endif\n \n+#ifndef FD_INVALID\n+#define FD_INVALID -10009\n+#endif\n+\n static inline int sys_fsopen(const char *fsname, unsigned int flags)\n {\n \treturn syscall(__NR_fsopen, fsname, flags);\n@@ -105,4 +109,11 @@ static inline int sys_open_tree(int dfd, const char *filename, unsigned int flag\n \treturn syscall(__NR_open_tree, dfd, filename, flags);\n }\n \n+static inline int openat_o_mkdir(int dfd, const char *pathname,\n+\t\t\t\t unsigned int flags, mode_t mode)\n+{\n+\treturn syscall(__NR_openat, dfd, pathname,\n+\t\t flags | O_DIRECTORY | O_CREAT | O_EXCL, mode);\n+}\n+\n #endif\n"
} |
| 2/1 | 2026/10/24 22:27 | agent | patch-evaluator |
2mModel:core Args: null Results: {
"EnableConfigs": [
"CGROUPS",
"RESCTRL_FS",
"X86_CPU_RESCTRL"
],
"FocusSymbols": [
"lookup_open",
"may_o_create",
"finish_lookup",
"vfs_mkdir",
"kernfs_iop_mkdir",
"kernfs_get_inode_locked",
"cgroup_mkdir",
"rdtgroup_mkdir"
],
"Reasoning": "The patch series implements atomic directory creation via openat() with O_CREAT | O_DIRECTORY | O_EXCL. It modifies core VFS path lookup and creation logic (fs/namei.c, fs/open.c), refactors vfs_mkdir and vfs_create, adds new runtime assertions (WARN_ON_ONCE), and adapts kernfs (kernfs_iop_mkdir, kernfs_get_inode_locked) along with cgroup and resctrl subsystems to instantiate and return directory dentries/inodes atomically. These changes are in core reachable kernel code accessible to userspace syscalls.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit b5331efeb2ca83571d91add2b1e2e364569ba29a
Author: syz-cluster <triage@syzkaller.com>
Date: Sat Oct 24 22:27:22 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/alpha/include/uapi/asm/errno.h b/arch/alpha/include/uapi/asm/errno.h
index 1a99f38813c79..0384f69989748 100644
--- a/arch/alpha/include/uapi/asm/errno.h
+++ b/arch/alpha/include/uapi/asm/errno.h
@@ -19,7 +19,7 @@
#define ENOPROTOOPT 42 /* Protocol not available */
#define EPROTONOSUPPORT 43 /* Protocol not supported */
#define ESOCKTNOSUPPORT 44 /* Socket type not supported */
-#define EOPNOTSUPP 45 /* Operation not supported on transport endpoint */
+#define EOPNOTSUPP 45 /* Operation not supported */
#define EPFNOSUPPORT 46 /* Protocol family not supported */
#define EAFNOSUPPORT 47 /* Address family not supported by protocol */
#define EADDRINUSE 48 /* Address already in use */
diff --git a/arch/mips/include/uapi/asm/errno.h b/arch/mips/include/uapi/asm/errno.h
index 1835a50b69cef..2b6813715d1e5 100644
--- a/arch/mips/include/uapi/asm/errno.h
+++ b/arch/mips/include/uapi/asm/errno.h
@@ -75,7 +75,7 @@
#define ENOPROTOOPT 99 /* Protocol not available */
#define EPROTONOSUPPORT 120 /* Protocol not supported */
#define ESOCKTNOSUPPORT 121 /* Socket type not supported */
-#define EOPNOTSUPP 122 /* Operation not supported on transport endpoint */
+#define EOPNOTSUPP 122 /* Operation not supported */
#define EPFNOSUPPORT 123 /* Protocol family not supported */
#define EAFNOSUPPORT 124 /* Address family not supported by protocol */
#define EADDRINUSE 125 /* Address already in use */
diff --git a/arch/parisc/include/uapi/asm/errno.h b/arch/parisc/include/uapi/asm/errno.h
index 93194fbb0a80d..74bd65d791457 100644
--- a/arch/parisc/include/uapi/asm/errno.h
+++ b/arch/parisc/include/uapi/asm/errno.h
@@ -84,7 +84,7 @@
#define ENOPROTOOPT 220 /* Protocol not available */
#define EPROTONOSUPPORT 221 /* Protocol not supported */
#define ESOCKTNOSUPPORT 222 /* Socket type not supported */
-#define EOPNOTSUPP 223 /* Operation not supported on transport endpoint */
+#define EOPNOTSUPP 223 /* Operation not supported */
#define EPFNOSUPPORT 224 /* Protocol family not supported */
#define EAFNOSUPPORT 225 /* Address family not supported by protocol */
#define EADDRINUSE 226 /* Address already in use */
diff --git a/arch/sparc/include/uapi/asm/errno.h b/arch/sparc/include/uapi/asm/errno.h
index 71940ec9130b4..699f648e1a356 100644
--- a/arch/sparc/include/uapi/asm/errno.h
+++ b/arch/sparc/include/uapi/asm/errno.h
@@ -16,7 +16,7 @@
#define ENOPROTOOPT 42 /* Protocol not available */
#define EPROTONOSUPPORT 43 /* Protocol not supported */
#define ESOCKTNOSUPPORT 44 /* Socket type not supported */
-#define EOPNOTSUPP 45 /* Op not supported on transport endpoint */
+#define EOPNOTSUPP 45 /* Operation not supported */
#define EPFNOSUPPORT 46 /* Protocol family not supported */
#define EAFNOSUPPORT 47 /* Address family not supported by protocol */
#define EADDRINUSE 48 /* Address already in use */
diff --git a/fs/9p/vfs_super.c b/fs/9p/vfs_super.c
index 79526044685c9..d0c594bee9918 100644
--- a/fs/9p/vfs_super.c
+++ b/fs/9p/vfs_super.c
@@ -40,6 +40,7 @@ static int v9fs_fill_super(struct super_block *sb)
sb->s_blocksize_bits = fls(v9ses->maxdata - 1);
sb->s_blocksize = 1 << sb->s_blocksize_bits;
sb->s_magic = V9FS_MAGIC;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
if (v9fs_proto_dotl(v9ses)) {
sb->s_op = &v9fs_super_ops_dotl;
if (!(v9ses->flags & V9FS_NO_XATTR))
diff --git a/fs/ceph/super.c b/fs/ceph/super.c
index 72935f665f11c..fa1cf1f4f57d6 100644
--- a/fs/ceph/super.c
+++ b/fs/ceph/super.c
@@ -1226,6 +1226,7 @@ static int ceph_set_super(struct super_block *s, struct fs_context *fc)
fsc->max_file_size = 1ULL << 40; /* temp value until we get mdsmap */
s->s_op = &ceph_super_ops;
+ s->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
set_default_d_op(s, &ceph_dentry_ops);
s->s_export_op = &ceph_export_ops;
diff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c
index 5f37cddb956fb..8ed6076d50afd 100644
--- a/fs/ecryptfs/main.c
+++ b/fs/ecryptfs/main.c
@@ -477,6 +477,7 @@ static int ecryptfs_get_tree(struct fs_context *fc)
/* ->kill_sb() will take care of sbi after that point */
sbi = NULL;
s->s_op = &ecryptfs_sops;
+ s->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
s->s_xattr = ecryptfs_xattr_handlers;
set_default_d_op(s, &ecryptfs_dops);
diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c
index e9552be3637bc..cf766c6feea43 100644
--- a/fs/fuse/inode.c
+++ b/fs/fuse/inode.c
@@ -1608,6 +1608,7 @@ static void fuse_sb_defaults(struct super_block *sb)
sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
sb->s_iflags |= SB_I_NOIDMAP;
sb->s_iflags |= SB_I_NO_DATA_INTEGRITY;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
if (sb->s_user_ns != &init_user_ns)
sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
diff --git a/fs/gfs2/ops_fstype.c b/fs/gfs2/ops_fstype.c
index 6ec47376aecf2..31bb09367d6bc 100644
--- a/fs/gfs2/ops_fstype.c
+++ b/fs/gfs2/ops_fstype.c
@@ -1137,6 +1137,7 @@ static int gfs2_fill_super(struct super_block *sb, struct fs_context *fc)
sb->s_flags |= SB_NOSEC;
sb->s_magic = GFS2_MAGIC;
sb->s_op = &gfs2_super_ops;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
set_default_d_op(sb, &gfs2_dops);
sb->s_export_op = &gfs2_export_ops;
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 324d61a00545b..abb6b0d212142 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -37,7 +37,7 @@ static bool __kernfs_active(struct kernfs_node *kn)
return atomic_read(&kn->active) >= 0;
}
-static bool kernfs_active(struct kernfs_node *kn)
+bool kernfs_active(struct kernfs_node *kn)
{
lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem);
return __kernfs_active(kn);
@@ -1293,18 +1293,44 @@ static struct dentry *kernfs_iop_mkdir(const struct mnt_idmap *idmap,
{
struct kernfs_node *parent = dir->i_private;
struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
- int ret;
+ struct kernfs_node *kn;
+ struct inode *inode;
if (!scops || !scops->mkdir)
- return ERR_PTR(-EPERM);
+ return ERR_PTR(-EOPNOTSUPP);
+
+ inode = new_inode(dir->i_sb);
+ if (!inode)
+ return ERR_PTR(-ENOMEM);
- if (!kernfs_get_active(parent))
+ if (!kernfs_get_active(parent)) {
+ iput(inode);
return ERR_PTR(-ENODEV);
+ }
- ret = scops->mkdir(parent, dentry->d_name.name, mode);
+ kn = scops->mkdir(parent, dentry->d_name.name, mode);
kernfs_put_active(parent);
- return ERR_PTR(ret);
+ if (IS_ERR(kn)) {
+ iput(inode);
+ return ERR_CAST(kn);
+ }
+
+ scoped_guard(rwsem_read, &kernfs_root(kn)->kernfs_rwsem)
+ inode = kernfs_get_inode_locked(inode, kn, false);
+ kernfs_put(kn);
+
+ if (!inode)
+ return ERR_PTR(-ESTALE);
+
+ /*
+ * We may race with kernfs_fh_to_dentry() which calls d_obtain_alias(),
+ * so the inode may already have a disconnected alias. Use
+ * d_splice_alias() to move it into place, which does not accept
+ * hashed dentries, so drop the negative dentry first.
+ */
+ d_drop(dentry);
+ return d_splice_alias(inode, dentry);
}
static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 6630b29d7c078..0da4e46ec75bf 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -204,7 +204,8 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
inode->i_generation = kernfs_gen(kn);
set_default_inode_attr(inode, kn->mode);
- kernfs_refresh_inode(kn, inode);
+ scoped_guard(rwsem_read, &kernfs_root(kn)->kernfs_iattr_rwsem)
+ kernfs_refresh_inode(kn, inode);
/* initialize inode according to type */
switch (kernfs_type(kn)) {
@@ -254,6 +255,63 @@ struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
return inode;
}
+static int kernfs_inode_test(struct inode *inode, void *data)
+{
+ return inode->i_ino == *(u64 *)data;
+}
+
+static int kernfs_inode_set(struct inode *inode, void *data)
+{
+ inode->i_ino = *(u64 *)data;
+ return 0;
+}
+
+/**
+ * kernfs_get_inode_locked - get the inode of @kn under kernfs_rwsem
+ * @inode: inode allocated with new_inode() on the target superblock
+ * @kn: kernfs_node to get the inode for
+ * @lookup: should we do a ilookup before attempting to insert the inode
+ *
+ * Attaches @inode to @kn. Caller should pass an @inode allocated with
+ * new_inode(), and hold the kernfs_rwsem semaphore for reading on the @kn
+ * root. Because the kernfs_rwsem is held and it is checked that @kn is still
+ * active, __kernfs_remove() always finds @inode to clear its i_nlink. If
+ * lookup is true, a quick ilookup is attempted first.
+ *
+ * Returns the inode that was found or inserted for @kn, or NULL if @kn is
+ * inactive. @inode is consumed.
+ */
+struct inode *kernfs_get_inode_locked(struct inode *inode, struct kernfs_node *kn,
+ bool lookup)
+{
+ u64 ino = kernfs_ino(kn);
+ struct inode *old = NULL, *new = inode;
+
+ lockdep_assert_held_read(&kernfs_root(kn)->kernfs_rwsem);
+
+ if (lookup) {
+ old = ilookup(inode->i_sb, ino);
+ if (old)
+ goto out_iput;
+ }
+
+ if (!kernfs_active(kn))
+ goto out_iput;
+
+ inode = inode_insert5(new, ino, kernfs_inode_test, kernfs_inode_set,
+ &ino);
+ if (inode != new) {
+ old = inode;
+ goto out_iput;
+ }
+
+ kernfs_init_inode(kn, inode);
+ return inode;
+out_iput:
+ iput(new);
+ return old;
+}
+
/*
* The kernfs_node serves as both an inode and a directory entry for
* kernfs. To prevent the kernfs inode numbers from being freed
diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h
index f1e93b09e27df..ec6e1f75513ba 100644
--- a/fs/kernfs/kernfs-internal.h
+++ b/fs/kernfs/kernfs-internal.h
@@ -176,6 +176,9 @@ extern struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;
* inode.c
*/
extern const struct xattr_handler * const kernfs_xattr_handlers[];
+struct inode *kernfs_get_inode_locked(struct inode *inode,
+ struct kernfs_node *kn,
+ bool lookup);
void kernfs_evict_inode(struct inode *inode);
int kernfs_iop_permission(const struct mnt_idmap *idmap,
struct inode *inode, int mask);
@@ -194,6 +197,7 @@ extern const struct dentry_operations kernfs_dops;
extern const struct file_operations kernfs_dir_fops;
extern const struct inode_operations kernfs_dir_iops;
+bool kernfs_active(struct kernfs_node *kn);
struct kernfs_node *kernfs_get_active(struct kernfs_node *kn);
void kernfs_put_active(struct kernfs_node *kn);
int kernfs_add_one(struct kernfs_node *kn);
diff --git a/fs/namei.c b/fs/namei.c
index 22c4f328b8a97..7365975a0a32a 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -1889,12 +1889,34 @@ static struct dentry *lookup_fast(struct nameidata *nd)
return dentry;
}
+/*
+ * If @dentry is in-lookup, look it up in @dir through ->lookup(). Returns
+ * the dentry to use from now on: @dentry itself, or the dentry or ERR_PTR()
+ * that ->lookup() returned instead, in which case @dentry is dropped.
+ */
+static struct dentry *finish_lookup(struct inode *dir, struct dentry *dentry,
+ unsigned int flags)
+{
+ struct dentry *res;
+
+ if (!d_in_lookup(dentry))
+ return dentry;
+
+ res = dir->i_op->lookup(dir, dentry, flags);
+ d_lookup_done(dentry);
+ if (unlikely(res)) {
+ dput(dentry);
+ return res;
+ }
+ return dentry;
+}
+
/* Fast lookup failed, do it the slow way */
static struct dentry *__lookup_slow(const struct qstr *name,
struct dentry *dir,
unsigned int flags)
{
- struct dentry *dentry, *old;
+ struct dentry *dentry;
struct inode *inode = dir->d_inode;
/* Don't go there if it's already dead */
@@ -1913,17 +1935,10 @@ static struct dentry *__lookup_slow(const struct qstr *name,
goto again;
}
dput(dentry);
- dentry = ERR_PTR(error);
- }
- } else {
- old = inode->i_op->lookup(inode, dentry, flags);
- d_lookup_done(dentry);
- if (unlikely(old)) {
- dput(dentry);
- dentry = old;
+ return ERR_PTR(error);
}
}
- return dentry;
+ return finish_lookup(inode, dentry, flags);
}
static noinline struct dentry *lookup_slow(const struct qstr *name,
@@ -2781,9 +2796,16 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
return s;
}
+static inline bool trailing_slashes(const struct qstr *last)
+{
+ /* last->len is set by hash_name() to the length of the current
+ * component ->name, terminating with '/' or a NUL character. */
+ return (bool)last->name[last->len];
+}
+
static inline const char *lookup_last(struct nameidata *nd)
{
- if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
+ if (nd->last_type == LAST_NORM && trailing_slashes(&nd->last))
nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
return walk_component(nd, WALK_TRAILING);
@@ -4159,6 +4181,24 @@ static inline umode_t vfs_prepare_mode(const struct mnt_idmap *idmap,
return mode;
}
+static inline
+int vfs_create_no_perm(const struct mnt_idmap *idmap, struct dentry *dentry,
+ umode_t mode, struct delegated_inode *di)
+{
+ struct inode *dir = d_inode(dentry->d_parent);
+ int error;
+
+ error = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, di);
+ if (error)
+ return error;
+
+ error = dir->i_op->create(idmap, dir, dentry, mode);
+ if (!error)
+ fsnotify_create(dir, dentry);
+
+ return error;
+}
+
/**
* vfs_create - create new file
* @idmap: idmap of the mount the inode was found from
@@ -4185,19 +4225,14 @@ int vfs_create(const struct mnt_idmap *idmap, struct dentry *dentry, umode_t mod
return error;
if (!dir->i_op->create)
- return -EACCES; /* shouldn't it be ENOSYS? */
+ return -EOPNOTSUPP;
mode = vfs_prepare_mode(idmap, dir, mode, S_IALLUGO, S_IFREG);
error = security_inode_create(idmap, dir, dentry, mode);
if (error)
return error;
- error = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, di);
- if (error)
- return error;
- error = dir->i_op->create(idmap, dir, dentry, mode);
- if (!error)
- fsnotify_create(dir, dentry);
- return error;
+
+ return vfs_create_no_perm(idmap, dentry, mode, di);
}
EXPORT_SYMBOL(vfs_create);
@@ -4314,21 +4349,43 @@ static inline int open_to_namei_flags(int flag)
static int may_o_create(const struct mnt_idmap *idmap,
const struct path *dir, struct dentry *dentry,
- umode_t mode)
+ int open_flag, umode_t mode)
{
- int error = security_path_mknod(dir, dentry, mode, 0);
+ struct inode *dir_inode = dir->dentry->d_inode;
+ bool create_dir = O_IS_MKDIR(open_flag);
+ int error;
+
+ WARN_ON_ONCE(create_dir && !S_ISDIR(mode));
+
+ if (create_dir)
+ error = security_path_mkdir(dir, dentry, mode);
+ else
+ error = security_path_mknod(dir, dentry, mode, 0);
if (error)
return error;
if (!fsuidgid_has_mapping(dir->dentry->d_sb, idmap))
return -EOVERFLOW;
- error = inode_permission(idmap, dir->dentry->d_inode,
- MAY_WRITE | MAY_EXEC);
+ error = inode_permission(idmap, dir_inode, MAY_WRITE | MAY_EXEC);
if (error)
return error;
- return security_inode_create(idmap, dir->dentry->d_inode, dentry, mode);
+ if (create_dir)
+ error = security_inode_mkdir(idmap, dir_inode, dentry, mode);
+ else
+ error = security_inode_create(idmap, dir_inode, dentry, mode);
+
+ return error;
+}
+
+static inline umode_t o_create_mode(const struct mnt_idmap *idmap, const struct inode *dir,
+ int open_flag, umode_t mode)
+{
+ if (O_IS_MKDIR(open_flag))
+ return vfs_prepare_mode(idmap, dir, mode, S_IRWXUGO | S_ISVTX, S_IFDIR);
+ else
+ return vfs_prepare_mode(idmap, dir, mode, S_IALLUGO, S_IFREG);
}
/**
@@ -4364,8 +4421,10 @@ static struct dentry *atomic_open(const struct path *path, struct dentry *dentry
file->__f_path.dentry = DENTRY_NOT_SET;
file->__f_path.mnt = path->mnt;
+
error = dir_inode->i_op->atomic_open(dir_inode, dentry, file,
- open_to_namei_flags(open_flag), mode);
+ open_to_namei_flags(open_flag), mode);
+
d_lookup_done(dentry);
if (!error) {
@@ -4409,11 +4468,24 @@ static struct dentry *atomic_open(const struct path *path, struct dentry *dentry
error = create_error;
}
dput(dentry);
- dentry = ERR_PTR(error);
+ return ERR_PTR(error);
}
+
+ if (file->f_mode & FMODE_CREATED) {
+ if (d_is_dir(dentry))
+ fsnotify_mkdir(dir_inode, dentry);
+ else
+ fsnotify_create(dir_inode, dentry);
+ }
+ if (file->f_mode & FMODE_OPENED)
+ fsnotify_open(file);
+
return dentry;
}
+static inline
+struct dentry *vfs_mkdir_no_perm(const struct mnt_idmap *, struct inode *, struct dentry *,
+ umode_t, struct delegated_inode *);
/*
* Look up and maybe create and open the last component.
*
@@ -4435,6 +4507,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
const struct mnt_idmap *idmap;
struct dentry *dir = nd->path.dentry;
struct inode *dir_inode = dir->d_inode;
+ bool create_dir = O_IS_MKDIR(op->open_flag);
int open_flag;
struct dentry *dentry;
int error, create_error;
@@ -4449,6 +4522,9 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
create_error = 0;
shared_flag = (open_flag & O_CREAT) ? 0 : LOOKUP_SHARED;
+ if (create_dir && (dir_inode->i_sb->s_iflags & SB_I_NO_ATOMIC_MKDIR))
+ return ERR_PTR(-EOPNOTSUPP);
+
if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
got_write = !mnt_want_write(nd->path.mnt);
/*
@@ -4456,10 +4532,11 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
* a different error; we'll be dropping this one anyway.
*/
}
+
if (shared_flag)
- inode_lock_shared(dir_inode);
+ inode_lock_shared_nested(dir_inode, I_MUTEX_PARENT);
else
- inode_lock(dir_inode);
+ inode_lock_nested(dir_inode, I_MUTEX_PARENT);
if (unlikely(IS_DEADDIR(dir_inode))) {
dentry = ERR_PTR(-ENOENT);
@@ -4509,10 +4586,10 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
if (open_flag & O_CREAT) {
if (open_flag & O_EXCL)
open_flag &= ~O_TRUNC;
- mode = vfs_prepare_mode(idmap, dir_inode, mode, mode, mode);
+ mode = o_create_mode(idmap, dir_inode, open_flag, mode);
if (likely(got_write))
create_error = may_o_create(idmap, &nd->path,
- dentry, mode);
+ dentry, open_flag, mode);
else
create_error = -EROFS;
}
@@ -4526,19 +4603,10 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
goto out;
}
- if (d_in_lookup(dentry)) {
- struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
- nd->flags | shared_flag);
- d_lookup_done(dentry);
- if (unlikely(res)) {
- if (IS_ERR(res)) {
- error = PTR_ERR(res);
- goto out_dput;
- }
- dput(dentry);
- dentry = res;
- }
- }
+ dentry = finish_lookup(dir_inode, dentry, nd->flags | shared_flag);
+ if (IS_ERR(dentry))
+ goto out;
+
if (dentry->d_inode || !(op->open_flag & O_CREAT)) {
/*
* No need to create a file. If lookup returned a positive
@@ -4556,26 +4624,33 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
goto out_dput;
}
- error = try_break_deleg(dir_inode, LEASE_BREAK_DIR_CREATE, &delegated_inode);
- if (error)
- goto out_dput;
-
- file->f_mode |= FMODE_CREATED;
- if (!dir_inode->i_op->create) {
- error = -EACCES;
+ if ((create_dir && !dir_inode->i_op->mkdir)
+ || (!create_dir && !dir_inode->i_op->create)) {
+ error = -EOPNOTSUPP;
goto out_dput;
}
- error = dir_inode->i_op->create(idmap, dir_inode, dentry, mode);
+ if (create_dir) {
+ struct dentry *res = vfs_mkdir_no_perm(idmap, dir_inode, dentry, mode,
+ &delegated_inode);
+ error = PTR_ERR_OR_ZERO(res);
+ if (!error) {
+ dentry = res;
+ /*
+ * Filesystems whose ->mkdir() may leave the dentry
+ * negative must set SB_I_NO_ATOMIC_MKDIR.
+ */
+ if (WARN_ON_ONCE(d_is_negative(dentry)))
+ error = -EIO;
+ }
+ } else {
+ error = vfs_create_no_perm(idmap, dentry, mode, &delegated_inode);
+ }
if (error)
goto out_dput;
+
+ file->f_mode |= FMODE_CREATED;
out:
- if (!IS_ERR(dentry)) {
- if (file->f_mode & FMODE_CREATED)
- fsnotify_create(dir_inode, dentry);
- if (file->f_mode & FMODE_OPENED)
- fsnotify_open(file);
- }
if (shared_flag)
inode_unlock_shared(dir_inode);
else
@@ -4697,17 +4772,12 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
}
EXPORT_SYMBOL_FOR_MODULES(vfs_lookup_open, "nfsd");
-static inline bool trailing_slashes(struct nameidata *nd)
-{
- return (bool)nd->last.name[nd->last.len];
-}
-
static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag)
{
struct dentry *dentry;
if (open_flag & O_CREAT) {
- if (trailing_slashes(nd))
+ if (trailing_slashes(&nd->last) && !(open_flag & O_DIRECTORY))
return ERR_PTR(-EISDIR);
/* Don't bother on an O_EXCL create */
@@ -4715,7 +4785,7 @@ static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag)
return NULL;
}
- if (trailing_slashes(nd))
+ if (trailing_slashes(&nd->last))
nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
dentry = lookup_fast(nd);
@@ -4744,6 +4814,13 @@ static const char *open_last_lookups(struct nameidata *nd,
if (nd->last_type != LAST_NORM) {
if (nd->depth)
put_link(nd);
+ /*
+ * Return -EOPNOTSUPP consistently, not -EEXIST, for paths
+ * ending in . or .., as lookup_open() does for other names.
+ */
+ if (O_IS_MKDIR(open_flag) &&
+ (nd->path.dentry->d_sb->s_iflags & SB_I_NO_ATOMIC_MKDIR))
+ return ERR_PTR(-EOPNOTSUPP);
return handle_dots(nd, nd->last_type);
}
@@ -4808,7 +4885,7 @@ static int do_open(struct nameidata *nd,
if (open_flag & O_CREAT) {
if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
return -EEXIST;
- if (d_is_dir(nd->path.dentry))
+ if (!(open_flag & O_DIRECTORY) && d_is_dir(nd->path.dentry))
return -EISDIR;
error = may_create_in_sticky(idmap, nd,
d_backing_inode(nd->path.dentry));
@@ -5097,7 +5174,7 @@ static struct dentry *filename_create(int dfd, struct filename *name,
* Do the final lookup. Suppress 'create' if there is a trailing
* '/', and a directory wasn't requested.
*/
- if (last.name[last.len] && !want_dir)
+ if (trailing_slashes(&last) && !want_dir)
create_flags &= ~LOOKUP_CREATE;
dentry = start_dirop(path->dentry, &last, reval_flag | create_flags);
if (IS_ERR(dentry))
@@ -5155,7 +5232,7 @@ inline struct dentry *start_creating_user_path(
EXPORT_SYMBOL(start_creating_user_path);
/**
- * dentry_create - Create and open a file
+ * dentry_create - Create and open a regular file
* @path: path to create
* @flags: O\_ flags
* @mode: mode bits for new file
@@ -5192,7 +5269,7 @@ struct file *dentry_create(struct path *path, int flags, umode_t mode,
path->dentry = dir;
mode = vfs_prepare_mode(idmap, dir_inode, mode, S_IALLUGO, S_IFREG);
- create_error = may_o_create(idmap, path, dentry, mode);
+ create_error = may_o_create(idmap, path, dentry, flags, mode);
if (create_error)
flags &= ~O_CREAT;
@@ -5208,13 +5285,6 @@ struct file *dentry_create(struct path *path, int flags, umode_t mode,
/* Drop the extra reference */
dput(orig_dentry);
- if (!error) {
- if (file->f_mode & FMODE_CREATED)
- fsnotify_create(dir->d_inode, dentry);
- if (file->f_mode & FMODE_OPENED)
- fsnotify_open(file);
- }
-
path->dentry = dentry;
} else {
@@ -5366,6 +5436,33 @@ SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, d
return filename_mknodat(AT_FDCWD, name, mode, dev);
}
+static inline
+struct dentry *vfs_mkdir_no_perm(const struct mnt_idmap *idmap, struct inode *dir,
+ struct dentry *dentry, umode_t mode,
+ struct delegated_inode *di)
+{
+ unsigned max_links = dir->i_sb->s_max_links;
+ struct dentry *de;
+ int error;
+
+ if (max_links && dir->i_nlink >= max_links)
+ return ERR_PTR(-EMLINK);
+
+ error = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, di);
+ if (error)
+ return ERR_PTR(error);
+
+ de = dir->i_op->mkdir(idmap, dir, dentry, mode);
+ if (IS_ERR(de))
+ return de;
+ if (de) {
+ dput(dentry);
+ dentry = de;
+ }
+ fsnotify_mkdir(dir, dentry);
+ return dentry;
+}
+
/**
* vfs_mkdir - create directory returning correct dentry if possible
* @idmap: idmap of the mount the inode was found from
@@ -5393,14 +5490,13 @@ struct dentry *vfs_mkdir(const struct mnt_idmap *idmap, struct inode *dir,
struct delegated_inode *delegated_inode)
{
int error;
- unsigned max_links = dir->i_sb->s_max_links;
struct dentry *de;
error = may_create_dentry(idmap, dir, dentry);
if (error)
goto err;
- error = -EPERM;
+ error = -EOPNOTSUPP;
if (!dir->i_op->mkdir)
goto err;
@@ -5409,24 +5505,12 @@ struct dentry *vfs_mkdir(const struct mnt_idmap *idmap, struct inode *dir,
if (error)
goto err;
- error = -EMLINK;
- if (max_links && dir->i_nlink >= max_links)
+ de = vfs_mkdir_no_perm(idmap, dir, dentry, mode, delegated_inode);
+ if (IS_ERR(de)) {
+ error = PTR_ERR(de);
goto err;
-
- error = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, delegated_inode);
- if (error)
- goto err;
-
- de = dir->i_op->mkdir(idmap, dir, dentry, mode);
- error = PTR_ERR(de);
- if (IS_ERR(de))
- goto err;
- if (de) {
- dput(dentry);
- dentry = de;
}
- fsnotify_mkdir(dir, dentry);
- return dentry;
+ return de;
err:
end_creating(dentry);
@@ -5713,7 +5797,7 @@ int filename_unlinkat(int dfd, struct filename *name)
goto exit_drop_write;
/* Why not before? Because we want correct error value */
- if (unlikely(last.name[last.len])) {
+ if (unlikely(trailing_slashes(&last))) {
if (d_is_dir(dentry))
error = -EISDIR;
else
@@ -6315,16 +6399,16 @@ int filename_renameat2(int olddfd, struct filename *from,
if (flags & RENAME_EXCHANGE) {
if (!d_is_dir(rd.new_dentry)) {
error = -ENOTDIR;
- if (new_last.name[new_last.len])
+ if (trailing_slashes(&new_last))
goto exit_unlock;
}
}
/* unless the source is a directory trailing slashes give -ENOTDIR */
if (!d_is_dir(rd.old_dentry)) {
error = -ENOTDIR;
- if (old_last.name[old_last.len])
+ if (trailing_slashes(&old_last))
goto exit_unlock;
- if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
+ if (!(flags & RENAME_EXCHANGE) && trailing_slashes(&new_last))
goto exit_unlock;
}
diff --git a/fs/nfs/super.c b/fs/nfs/super.c
index cb19f1540d984..f4452b630f52b 100644
--- a/fs/nfs/super.c
+++ b/fs/nfs/super.c
@@ -1077,6 +1077,7 @@ static void nfs_fill_super(struct super_block *sb, struct nfs_fs_context *ctx)
sb->s_blocksize = 0;
sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
sb->s_op = server->nfs_client->cl_nfs_mod->sops;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
if (server->bsize)
sb->s_blocksize =
nfs_block_size(server->bsize, &sb->s_blocksize_bits);
diff --git a/fs/open.c b/fs/open.c
index e43f02ff64acb..8217106a6f4b4 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -1264,29 +1264,30 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
if (WILL_CREATE(flags)) {
if (how->mode & ~S_IALLUGO)
return -EINVAL;
- op->mode = how->mode | S_IFREG;
+ if (O_IS_MKDIR(flags))
+ op->mode = how->mode | S_IFDIR;
+ else
+ op->mode = how->mode | S_IFREG;
} else {
if (how->mode != 0)
return -EINVAL;
op->mode = 0;
}
- /*
- * Block bugs where O_DIRECTORY | O_CREAT created regular files.
- * Note, that blocking O_DIRECTORY | O_CREAT here also protects
- * O_TMPFILE below which requires O_DIRECTORY being raised.
- */
- if ((flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT))
- return -EINVAL;
-
/* Now handle the creative implementation of O_TMPFILE. */
if (flags & __O_TMPFILE) {
/*
* In order to ensure programs get explicit errors when trying
* to use O_TMPFILE on old kernels we enforce that O_DIRECTORY
- * is raised alongside __O_TMPFILE.
+ * is raised alongside __O_TMPFILE, but without O_CREAT. The
+ * reason for disallowing O_CREAT|O_TMPFILE is that
+ * O_DIRECTORY|O_CREAT used to work and created a regular file
+ * if nothing existed at the open path. Hence, allowing the
+ * combination would have caused O_CREAT|O_TMPFILE to create a
+ * regular (non-temporary) file on old kernels, while the caller
+ * would believe they created an actual O_TMPFILE.
*/
- if (!(flags & O_DIRECTORY))
+ if (!(flags & O_DIRECTORY) || (flags & O_CREAT))
return -EINVAL;
if (!(acc_mode & MAY_WRITE))
return -EINVAL;
@@ -1343,12 +1344,29 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
+ /*
+ * Requesting write access on a directory can never succeed. Rather
+ * than performing a path-walk to determine whether the target is
+ * actually a directory (-EISDIR) or not (-ENOTDIR), we short-circuit
+ * to -ENOTDIR.
+ */
+ if ((flags & O_DIRECTORY) && !(flags & __O_TMPFILE) && (acc_mode & MAY_WRITE))
+ return -ENOTDIR;
+
if (flags & O_CREAT) {
- op->intent |= LOOKUP_CREATE;
+ if (flags & O_DIRECTORY) {
+ /*
+ * O_CREAT|O_DIRECTORY is provided to prevent TOCTOUs, so
+ * it makes little sense to allow it without O_EXCL.
+ */
+ if (!(flags & O_EXCL))
+ return -EINVAL;
+ }
if (flags & O_EXCL) {
op->intent |= LOOKUP_EXCL;
flags |= O_NOFOLLOW;
}
+ op->intent |= LOOKUP_CREATE;
}
if (flags & O_DIRECTORY)
diff --git a/fs/resctrl/rdtgroup.c b/fs/resctrl/rdtgroup.c
index 57ab090072c21..be151cc8fd29c 100644
--- a/fs/resctrl/rdtgroup.c
+++ b/fs/resctrl/rdtgroup.c
@@ -4040,15 +4040,16 @@ static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
* and monitor group(ctrl_mon). This is a resource group
* to monitor a subset of tasks and cpus in its parent ctrl_mon group.
*/
-static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
- const char *name, umode_t mode)
+static struct kernfs_node *rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
+ const char *name, umode_t mode)
{
struct rdtgroup *rdtgrp, *prgrp;
+ struct kernfs_node *kn;
int ret;
ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp);
if (ret)
- return ret;
+ return ERR_PTR(ret);
prgrp = rdtgrp->mon.parent;
rdtgrp->closid = prgrp->closid;
@@ -4067,17 +4068,22 @@ static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
*/
list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
+ kn = rdtgrp->kn;
+ kernfs_get(kn);
+ rdtgroup_kn_unlock(parent_kn);
+ return kn;
+
out_unlock:
rdtgroup_kn_unlock(parent_kn);
- return ret;
+ return ERR_PTR(ret);
}
/*
* These are rdtgroups created under the root directory. Can be used
* to allocate and monitor resources.
*/
-static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
- const char *name, umode_t mode)
+static struct kernfs_node *rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
+ const char *name, umode_t mode)
{
struct rdtgroup *rdtgrp;
struct kernfs_node *kn;
@@ -4086,7 +4092,7 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp);
if (ret)
- return ret;
+ return ERR_PTR(ret);
kn = rdtgrp->kn;
ret = closid_alloc();
@@ -4125,7 +4131,9 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
rdtgrp->mba_mbps_event = mba_mbps_default_event;
}
- goto out_unlock;
+ kernfs_get(kn);
+ rdtgroup_kn_unlock(parent_kn);
+ return kn;
out_del_list:
list_del(&rdtgrp->rdtgroup_list);
@@ -4135,17 +4143,16 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
closid_free(closid);
out_common_fail:
mkdir_rdt_prepare_clean(rdtgrp);
-out_unlock:
rdtgroup_kn_unlock(parent_kn);
- return ret;
+ return ERR_PTR(ret);
}
-static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
- umode_t mode)
+static struct kernfs_node *rdtgroup_mkdir(struct kernfs_node *parent_kn,
+ const char *name, umode_t mode)
{
/* Do not accept '\n' to avoid unparsable situation. */
if (strchr(name, '\n'))
- return -EINVAL;
+ return ERR_PTR(-EINVAL);
/*
* If the parent directory is the root directory and RDT
@@ -4159,7 +4166,7 @@ static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
if (resctrl_arch_mon_capable())
return rdtgroup_mkdir_mon(parent_kn, name, mode);
- return -EPERM;
+ return ERR_PTR(-EPERM);
}
static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
diff --git a/fs/smb/client/cifsfs.c b/fs/smb/client/cifsfs.c
index 576155caf888e..f00dd2530b758 100644
--- a/fs/smb/client/cifsfs.c
+++ b/fs/smb/client/cifsfs.c
@@ -246,6 +246,7 @@ cifs_read_super(struct super_block *sb)
sb->s_magic = CIFS_SUPER_MAGIC;
sb->s_op = &cifs_super_ops;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
sb->s_xattr = cifs_xattr_handlers;
rc = super_setup_bdi(sb);
if (rc)
diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c
index 5020365ca7048..b679adf68c8fb 100644
--- a/fs/tracefs/inode.c
+++ b/fs/tracefs/inode.c
@@ -471,6 +471,7 @@ static int tracefs_fill_super(struct super_block *sb, struct fs_context *fc)
return err;
sb->s_op = &tracefs_super_operations;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
tracefs_apply_options(sb, false);
set_default_d_op(sb, &tracefs_dentry_operations);
diff --git a/fs/vboxsf/super.c b/fs/vboxsf/super.c
index a61fbab51d370..190b76f04d4f8 100644
--- a/fs/vboxsf/super.c
+++ b/fs/vboxsf/super.c
@@ -196,6 +196,7 @@ static int vboxsf_fill_super(struct super_block *sb, struct fs_context *fc)
sb->s_blocksize = 1024;
sb->s_maxbytes = MAX_LFS_FILESIZE;
sb->s_op = &vboxsf_super_ops;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
set_default_d_op(sb, &vboxsf_dentry_ops);
iroot = iget_locked(sb, 0);
diff --git a/include/linux/fcntl.h b/include/linux/fcntl.h
index 6ad6b9e7a226a..204e16bbe2634 100644
--- a/include/linux/fcntl.h
+++ b/include/linux/fcntl.h
@@ -30,6 +30,12 @@
*/
#define __O_REGULAR (1 << 30)
+#define O_MKDIR_MASK (O_CREAT | O_DIRECTORY)
+static inline bool O_IS_MKDIR(unsigned int flags)
+{
+ return (flags & O_MKDIR_MASK) == O_MKDIR_MASK;
+}
+
/* List of all valid flags for the how->resolve argument: */
#define VALID_RESOLVE_FLAGS \
(RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS | \
diff --git a/include/linux/fs/super_types.h b/include/linux/fs/super_types.h
index ecd96aeb1cee7..521b00c47d0aa 100644
--- a/include/linux/fs/super_types.h
+++ b/include/linux/fs/super_types.h
@@ -352,5 +352,6 @@ struct super_block {
#define SB_I_NOIDMAP 0x00002000 /* No idmapped mounts on this superblock */
#define SB_I_ALLOW_HSM 0x00004000 /* Allow HSM events on this superblock */
#define SB_I_NO_DATA_INTEGRITY 0x00008000 /* fs cannot guarantee data persistence on sync */
+#define SB_I_NO_ATOMIC_MKDIR 0x00010000 /* no O_CREAT|O_DIRECTORY on this fs */
#endif /* _LINUX_FS_SUPER_TYPES_H */
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index 73786b567ccd8..2a7baa6a56bc0 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -241,13 +241,14 @@ struct kernfs_node {
* syscalls. These optional callbacks are invoked on the matching syscalls
* and can perform any kernfs operations which don't necessarily have to be
* the exact operation requested. An active reference is held for each
- * kernfs_node parameter.
+ * kernfs_node parameter. @mkdir returns the kernfs_node of the new
+ * directory with a reference held, or an ERR_PTR() on failure.
*/
struct kernfs_syscall_ops {
int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
- int (*mkdir)(struct kernfs_node *parent, const char *name,
- umode_t mode);
+ struct kernfs_node *(*mkdir)(struct kernfs_node *parent,
+ const char *name, umode_t mode);
int (*rmdir)(struct kernfs_node *kn);
int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
const char *new_name);
diff --git a/include/uapi/asm-generic/errno.h b/include/uapi/asm-generic/errno.h
index bd78e69e0a43f..c84ebf89c8b6e 100644
--- a/include/uapi/asm-generic/errno.h
+++ b/include/uapi/asm-generic/errno.h
@@ -76,7 +76,7 @@
#define ENOPROTOOPT 92 /* Protocol not available */
#define EPROTONOSUPPORT 93 /* Protocol not supported */
#define ESOCKTNOSUPPORT 94 /* Socket type not supported */
-#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */
+#define EOPNOTSUPP 95 /* Operation not supported */
#define EPFNOSUPPORT 96 /* Protocol family not supported */
#define EAFNOSUPPORT 97 /* Address family not supported by protocol */
#define EADDRINUSE 98 /* Address already in use */
diff --git a/kernel/cgroup/cgroup-internal.h b/kernel/cgroup/cgroup-internal.h
index 233fb8b45d2c3..5d05fd75d0c04 100644
--- a/kernel/cgroup/cgroup-internal.h
+++ b/kernel/cgroup/cgroup-internal.h
@@ -256,7 +256,8 @@ void cgroup_procs_write_finish(struct task_struct *task,
int cgroup_lock_and_drain_offline(struct cgroup *cgrp);
-int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode);
+struct kernfs_node *cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
+ umode_t mode);
int cgroup_rmdir(struct kernfs_node *kn);
int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
struct kernfs_root *kf_root);
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 14634246a666d..a52d754478320 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -6149,18 +6149,20 @@ static bool cgroup_check_hierarchy_limits(struct cgroup *parent)
return ret;
}
-int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
+struct kernfs_node *cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
+ umode_t mode)
{
struct cgroup *parent, *cgrp;
+ struct kernfs_node *kn;
int ret;
/* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
if (strchr(name, '\n'))
- return -EINVAL;
+ return ERR_PTR(-EINVAL);
parent = cgroup_kn_lock_live(parent_kn, false);
if (IS_ERR(parent))
- return PTR_ERR(parent);
+ return ERR_CAST(parent);
if (!cgroup_check_hierarchy_limits(parent)) {
ret = -EAGAIN;
@@ -6192,14 +6194,16 @@ int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
/* let's create and online css's */
kernfs_activate(cgrp->kn);
- ret = 0;
- goto out_unlock;
+ kn = cgrp->kn;
+ kernfs_get(kn);
+ cgroup_kn_unlock(parent_kn);
+ return kn;
out_destroy:
cgroup_destroy_locked(cgrp);
out_unlock:
cgroup_kn_unlock(parent_kn);
- return ret;
+ return ERR_PTR(ret);
}
/*
diff --git a/security/apparmor/apparmorfs.c b/security/apparmor/apparmorfs.c
index d1386722537d0..36c9bb7ebf3d8 100644
--- a/security/apparmor/apparmorfs.c
+++ b/security/apparmor/apparmorfs.c
@@ -248,6 +248,7 @@ static int apparmorfs_fill_super(struct super_block *sb, struct fs_context *fc)
if (error)
return error;
sb->s_op = &aafs_super_ops;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
return 0;
}
diff --git a/tools/testing/selftests/filesystems/.gitignore b/tools/testing/selftests/filesystems/.gitignore
index cf79000e4092a..8ccf7a5f9ec9d 100644
--- a/tools/testing/selftests/filesystems/.gitignore
+++ b/tools/testing/selftests/filesystems/.gitignore
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0-only
+open_o_creat_o_dir
dnotify_test
devpts_pts
fclog
diff --git a/tools/testing/selftests/filesystems/Makefile b/tools/testing/selftests/filesystems/Makefile
index fbd5c27505ef5..004b946fd2e0b 100644
--- a/tools/testing/selftests/filesystems/Makefile
+++ b/tools/testing/selftests/filesystems/Makefile
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: GPL-2.0
CFLAGS += $(KHDR_INCLUDES)
-TEST_GEN_PROGS := devpts_pts anon_inode_test kernfs_test fclog ustat_test rw_hint_test
+TEST_GEN_PROGS := devpts_pts anon_inode_test kernfs_test fclog ustat_test rw_hint_test open_o_creat_o_dir
TEST_GEN_PROGS += idmapped_tmpfile
TEST_GEN_PROGS_EXTENDED := dnotify_test
diff --git a/tools/testing/selftests/filesystems/open_o_creat_o_dir.c b/tools/testing/selftests/filesystems/open_o_creat_o_dir.c
new file mode 100644
index 0000000000000..93c0f4dea266a
--- /dev/null
+++ b/tools/testing/selftests/filesystems/open_o_creat_o_dir.c
@@ -0,0 +1,346 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <sys/stat.h>
+#include <sys/vfs.h>
+#include <errno.h>
+#include <limits.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <linux/magic.h>
+
+#include "kselftest_harness.h"
+#include "wrappers.h"
+
+#define openat_o_mkdir_checked_flags(dfd, pathname, flags) ({ \
+ struct stat __st; \
+ int __fd = openat_o_mkdir(dfd, pathname, flags, S_IRWXU); \
+ ASSERT_GE(__fd, 0); \
+ ASSERT_EQ(fstat(__fd, &__st), 0); \
+ EXPECT_TRUE(S_ISDIR(__st.st_mode)); \
+ __fd; \
+})
+
+#define openat_o_mkdir_checked(dfd, pathname) \
+ openat_o_mkdir_checked_flags(dfd, pathname, O_RDONLY)
+
+FIXTURE(open_o_creat_o_dir) {
+ char dirpath[PATH_MAX];
+ int dfd;
+};
+
+FIXTURE_SETUP(open_o_creat_o_dir)
+{
+ strcpy(self->dirpath, "/tmp/open_o_creat_o_dir_test.XXXXXX");
+ ASSERT_NE(mkdtemp(self->dirpath), NULL);
+ self->dfd = open(self->dirpath, O_DIRECTORY);
+ ASSERT_GE(self->dfd, 0);
+}
+
+FIXTURE_TEARDOWN(open_o_creat_o_dir)
+{
+ close(self->dfd);
+ rmdir(self->dirpath);
+}
+
+/* Does open_o_creat_o_dir return a fd at all? */
+TEST_F(open_o_creat_o_dir, returns_fd)
+{
+ int fd = openat_o_mkdir_checked(self->dfd, "newdir");
+ EXPECT_EQ(close(fd), 0);
+ EXPECT_EQ(unlinkat(self->dfd, "newdir", AT_REMOVEDIR), 0);
+}
+
+/* The fd must refer to the directory that was just created. */
+TEST_F(open_o_creat_o_dir, fd_is_created_dir)
+{
+ int fd;
+ struct stat st_via_fd, st_via_path;
+ char path[PATH_MAX];
+
+ fd = openat_o_mkdir_checked(self->dfd, "checkdir");
+
+ ASSERT_EQ(fstat(fd, &st_via_fd), 0);
+
+ snprintf(path, sizeof(path), "%s/checkdir", self->dirpath);
+ ASSERT_EQ(stat(path, &st_via_path), 0);
+
+ EXPECT_EQ(st_via_fd.st_ino, st_via_path.st_ino);
+ EXPECT_EQ(st_via_fd.st_dev, st_via_path.st_dev);
+
+ EXPECT_EQ(close(fd), 0);
+ EXPECT_EQ(rmdir(path), 0);
+}
+
+/* Missing parent component must fail with ENOENT. */
+TEST_F(open_o_creat_o_dir, enoent_missing_parent)
+{
+ EXPECT_EQ(openat_o_mkdir(self->dfd, "nonexistent/child", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, ENOENT);
+}
+
+/* An invalid dfd must fail with EBADF. */
+TEST_F(open_o_creat_o_dir, ebadf)
+{
+ EXPECT_EQ(openat_o_mkdir(FD_INVALID, "badfdir", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, EBADF);
+}
+
+/* A dfd that points to a file (not a directory) must fail with ENOTDIR. */
+TEST_F(open_o_creat_o_dir, enotdir_dfd)
+{
+ int file_fd;
+
+ file_fd = openat(self->dfd, "file",
+ O_CREAT | O_RDONLY, S_IRWXU);
+ ASSERT_GE(file_fd, 0);
+
+ EXPECT_EQ(openat_o_mkdir(file_fd, "subdir", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, ENOTDIR);
+
+ EXPECT_EQ(close(file_fd), 0);
+ EXPECT_EQ(unlinkat(self->dfd, "file", 0), 0);
+}
+
+/*
+ * O_CREAT|O_DIRECTORY without O_EXCL must fail with EINVAL and must not
+ * create anything at the path.
+ */
+TEST_F(open_o_creat_o_dir, requires_o_excl)
+{
+ struct stat st;
+
+ EXPECT_EQ(openat(self->dfd, "noexcl", O_CREAT | O_DIRECTORY | O_RDONLY,
+ S_IRWXU), -1);
+ EXPECT_EQ(errno, EINVAL);
+
+ EXPECT_EQ(fstatat(self->dfd, "noexcl", &st, 0), -1);
+ EXPECT_EQ(errno, ENOENT);
+}
+
+/*
+ * Once the directory exists, repeating O_CREAT|O_DIRECTORY|O_EXCL must
+ * fail with EEXIST.
+ */
+TEST_F(open_o_creat_o_dir, existing_dir_eexist)
+{
+ int excldir_fd;
+
+ excldir_fd = openat_o_mkdir_checked(self->dfd, "excldir");
+
+ EXPECT_EQ(openat_o_mkdir(self->dfd, "excldir", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, EEXIST);
+
+ EXPECT_EQ(close(excldir_fd), 0);
+ EXPECT_EQ(unlinkat(self->dfd, "excldir", AT_REMOVEDIR), 0);
+}
+
+/*
+ * O_CREAT|O_DIRECTORY combined with a writable access mode must be
+ * rejected: a directory cannot be opened for writing.
+ */
+TEST_F(open_o_creat_o_dir, rejects_writable_acc_mode)
+{
+ EXPECT_EQ(openat_o_mkdir(self->dfd, "rdwrdir", O_RDWR, S_IRWXU), -1);
+ EXPECT_EQ(errno, ENOTDIR);
+}
+
+/*
+ * openat(O_CREAT|O_DIRECTORY) with a trailing slash should work.
+ */
+TEST_F(open_o_creat_o_dir, trailing_slash)
+{
+ int fd = openat_o_mkdir_checked(self->dfd, "newdir/");
+ EXPECT_EQ(close(fd), 0);
+ EXPECT_EQ(unlinkat(self->dfd, "newdir", AT_REMOVEDIR), 0);
+}
+
+/*
+ * openat(O_CREAT) with a trailing slash but without O_DIRECTORY
+ * must fail with EISDIR and must not create anything at the path.
+ */
+TEST_F(open_o_creat_o_dir, trailing_slash_no_o_dir)
+{
+ int fd;
+ struct stat st;
+
+ fd = openat(self->dfd, "trailing/", O_CREAT | O_RDONLY, S_IRWXU);
+ EXPECT_EQ(fd, -1);
+ EXPECT_EQ(errno, EISDIR);
+
+ EXPECT_EQ(fstatat(self->dfd, "trailing", &st, 0), -1);
+ EXPECT_EQ(errno, ENOENT);
+}
+
+/*
+ * The returned fd must be usable as a dfd for further *at() calls.
+ */
+TEST_F(open_o_creat_o_dir, fd_usable_as_dfd)
+{
+ int parent_fd, child_fd;
+ char path[PATH_MAX];
+
+ parent_fd = openat_o_mkdir_checked(self->dfd, "parent");
+ child_fd = openat_o_mkdir_checked(parent_fd, "child");
+
+ EXPECT_EQ(close(child_fd), 0);
+ EXPECT_EQ(close(parent_fd), 0);
+
+ snprintf(path, sizeof(path), "%s/parent/child", self->dirpath);
+ EXPECT_EQ(rmdir(path), 0);
+ snprintf(path, sizeof(path), "%s/parent", self->dirpath);
+ EXPECT_EQ(rmdir(path), 0);
+}
+
+/*
+ * O_EXCL means a trailing symlink is never followed, so O_CREAT|O_DIRECTORY
+ * must refuse to create through a dangling one, and must not create
+ * anything at the symlink target.
+ */
+TEST_F(open_o_creat_o_dir, dangling_symlink_eexist)
+{
+ struct stat st;
+
+ ASSERT_EQ(symlinkat("danglink_target", self->dfd, "danglink"), 0);
+
+ EXPECT_EQ(openat_o_mkdir(self->dfd, "danglink", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, EEXIST);
+
+ /* Nothing must have been created at the target. */
+ EXPECT_EQ(fstatat(self->dfd, "danglink_target", &st, 0), -1);
+ EXPECT_EQ(errno, ENOENT);
+
+ EXPECT_EQ(unlinkat(self->dfd, "danglink", 0), 0);
+}
+
+/*
+ * A trailing symlink is an existing entry, even if it resolves to a
+ * directory, so O_CREAT|O_DIRECTORY must fail with EEXIST.
+ */
+TEST_F(open_o_creat_o_dir, symlink_to_dir_eexist)
+{
+ ASSERT_EQ(mkdirat(self->dfd, "realdir", 0700), 0);
+ ASSERT_EQ(symlinkat("realdir", self->dfd, "dirlink"), 0);
+
+ EXPECT_EQ(openat_o_mkdir(self->dfd, "dirlink", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, EEXIST);
+
+ EXPECT_EQ(unlinkat(self->dfd, "dirlink", 0), 0);
+ EXPECT_EQ(unlinkat(self->dfd, "realdir", AT_REMOVEDIR), 0);
+}
+
+/*
+ * O_TMPFILE is encoded as __O_TMPFILE|O_DIRECTORY. Now that O_CREAT is no
+ * longer rejected alongside O_DIRECTORY, O_TMPFILE|O_CREAT must still be
+ * rejected explicitly so that it cannot create a persistent file on kernels
+ * that predate O_TMPFILE.
+ */
+TEST_F(open_o_creat_o_dir, tmpfile_with_o_creat_einval)
+{
+ EXPECT_EQ(openat(self->dfd, ".", O_TMPFILE | O_CREAT | O_RDWR, S_IRWXU),
+ -1);
+ EXPECT_EQ(errno, EINVAL);
+}
+
+/*
+ * Open the first of @paths that is a mounted filesystem of type @magic.
+ * Returns -1 if there is none.
+ */
+static int open_fs_root(const char *const *paths, long magic)
+{
+ for (; *paths; paths++) {
+ struct statfs sfs;
+ int fd;
+
+ fd = open(*paths, O_DIRECTORY | O_RDONLY);
+ if (fd < 0)
+ continue;
+ if (!fstatfs(fd, &sfs) && sfs.f_type == magic)
+ return fd;
+ close(fd);
+ }
+ return -1;
+}
+
+/*
+ * kernfs: O_CREAT|O_DIRECTORY on cgroup2 must create a cgroup and return a
+ * fd to that very cgroup.
+ */
+TEST(kernfs_cgroup2)
+{
+ static const char *const paths[] = {
+ "/sys/fs/cgroup", "/sys/fs/cgroup/unified", NULL
+ };
+ struct stat st_fd, st_path;
+ int root_fd, fd, procs_fd;
+ char name[64];
+
+ if (geteuid() != 0)
+ SKIP(return, "needs root to create a cgroup");
+ root_fd = open_fs_root(paths, CGROUP2_SUPER_MAGIC);
+ if (root_fd < 0)
+ SKIP(return, "cgroup2 is not mounted");
+
+ snprintf(name, sizeof(name), "open_o_creat_o_dir.%d", getpid());
+
+ fd = openat_o_mkdir_checked(root_fd, name);
+
+ /* The fd refers to the cgroup that is now visible at the name. */
+ ASSERT_EQ(fstat(fd, &st_fd), 0);
+ ASSERT_EQ(fstatat(root_fd, name, &st_path, 0), 0);
+ EXPECT_EQ(st_fd.st_ino, st_path.st_ino);
+ EXPECT_EQ(st_fd.st_dev, st_path.st_dev);
+
+ /* It is a populated cgroup, not just an empty directory. */
+ procs_fd = openat(fd, "cgroup.procs", O_RDONLY);
+ EXPECT_GE(procs_fd, 0);
+ if (procs_fd >= 0)
+ EXPECT_EQ(close(procs_fd), 0);
+
+ EXPECT_EQ(close(fd), 0);
+ EXPECT_EQ(unlinkat(root_fd, name, AT_REMOVEDIR), 0);
+ EXPECT_EQ(close(root_fd), 0);
+}
+
+/*
+ * tracefs sets SB_I_NO_ATOMIC_MKDIR: O_CREAT|O_DIRECTORY is refused with
+ * EOPNOTSUPP before any lookup, whether or not the name exists, and
+ * creates nothing.
+ */
+TEST(tracefs_eopnotsupp)
+{
+ static const char *const paths[] = {
+ "/sys/kernel/tracing", "/sys/kernel/debug/tracing", NULL
+ };
+ int tracing_fd, inst_fd;
+ char name[64];
+ struct stat st;
+
+ if (geteuid() != 0)
+ SKIP(return, "needs root to access tracefs");
+ tracing_fd = open_fs_root(paths, TRACEFS_MAGIC);
+ if (tracing_fd < 0)
+ SKIP(return, "tracefs is not mounted");
+ inst_fd = openat(tracing_fd, "instances", O_DIRECTORY | O_RDONLY);
+ if (inst_fd < 0) {
+ close(tracing_fd);
+ SKIP(return, "no tracefs instances directory");
+ }
+
+ snprintf(name, sizeof(name), "open_o_creat_o_dir.%d", getpid());
+
+ /* Nonexistent name, where mkdir(2) would create an instance */
+ EXPECT_EQ(openat_o_mkdir(inst_fd, name, O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, EOPNOTSUPP);
+ EXPECT_EQ(fstatat(inst_fd, name, &st, 0), -1);
+ EXPECT_EQ(errno, ENOENT);
+
+ /* Existing name */
+ EXPECT_EQ(openat_o_mkdir(tracing_fd, "instances", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, EOPNOTSUPP);
+
+ EXPECT_EQ(close(inst_fd), 0);
+ EXPECT_EQ(close(tracing_fd), 0);
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/filesystems/wrappers.h b/tools/testing/selftests/filesystems/wrappers.h
index 420ae4f908cf2..bc58bdb800631 100644
--- a/tools/testing/selftests/filesystems/wrappers.h
+++ b/tools/testing/selftests/filesystems/wrappers.h
@@ -13,6 +13,10 @@
#define STATX_MNT_ID_UNIQUE 0x00004000U /* Want/got extended stx_mount_id */
#endif
+#ifndef FD_INVALID
+#define FD_INVALID -10009
+#endif
+
static inline int sys_fsopen(const char *fsname, unsigned int flags)
{
return syscall(__NR_fsopen, fsname, flags);
@@ -105,4 +109,11 @@ static inline int sys_open_tree(int dfd, const char *filename, unsigned int flag
return syscall(__NR_open_tree, dfd, filename, flags);
}
+static inline int openat_o_mkdir(int dfd, const char *pathname,
+ unsigned int flags, mode_t mode)
+{
+ return syscall(__NR_openat, dfd, pathname,
+ flags | O_DIRECTORY | O_CREAT | O_EXCL, mode);
+}
+
#endif
|
| 3/2 | 2026/10/24 22:27 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 24197 output: 40 thoughts: 1469 Thoughts: **Patch Analysis: Atomic Directory Creation via openat** Okay, let's dive into this patch. My initial impression is that this is significant. This patch introduces atomic directory creation through `openat(..., O_CREAT | O_DIRECTORY | O_EXCL, ...)` - that is potentially juicy. First, I see several files being touched. It’s not just a small change. It updates core headers, adds `SB_I_NO_ATOMIC_MKDIR` to filesystems, and heavily modifies Kernfs, and also cgroups and resctrl, and the heart of the VFS (`fs/namei.c` and `fs/open.c`). `vfs_mkdir`, `lookup_open`, and more are getting a refactor. I am already seeing potential for issues as this has the chance to introduce regressions in existing functionality. The changes in `fs/namei.c` are particularly interesting. The addition of `finish_lookup`, `vfs_create_no_perm`, `vfs_mkdir_no_perm`, modifications to `may_o_create`, `atomic_open` update, and the new `lookup_open` logic, nested inode locking, and the checks with `WARN_ON_ONCE` are all critical. The checks for `d_is_negative(dentry)` and the other assertions are good, but they also highlight where bugs might lurk. This sort of logic is very easy to mess up, and a mistake can easily have security implications, especially with file system and directory creation. The `fs/open.c` changes, allowing `O_DIRECTORY | O_CREAT` (specifically `O_CREAT | O_DIRECTORY | O_EXCL`) and related flag/mode logic are crucial and will likely be the source of interesting bugs. Given that this is a core VFS addition, it's immediately jumping out as a candidate for fuzzing. The feature is completely reachable from unprivileged userspace via the `openat` and `openat2` syscalls, making it prime syzkaller material. As for configs, the modified code appears to be using standard VFS, kernfs, cgroups configs. I'll need to check the Kconfig guards for specific files like `rdtgroup.c`, etc., via a `git grep`. It is possible that this patch indirectly requires specific configs to be enabled to allow this code to run, and that is important to discover. Based on everything I see, this absolutely warrants syzkaller. The code touches core VFS functionality, introduces new logic, and is reachable from userspace. It's time to get this tested. |
| 4/2 | 2026/10/24 22:27 | tool | grepper |
0mArgs:{
"Expression": "CONFIG_RESCTRL|CGROUPS",
"PathPrefixes": [
"fs/resctrl",
"kernel/cgroup"
]
}Results: {
"Output": "fs/resctrl/Makefile-1-# SPDX-License-Identifier: GPL-2.0\nfs/resctrl/Makefile:2:obj-$(CONFIG_RESCTRL_FS)\t\t+= rdtgroup.o ctrlmondata.o monitor.o\nfs/resctrl/Makefile:3:obj-$(CONFIG_RESCTRL_FS_PSEUDO_LOCK)\t+= pseudo_lock.o\nfs/resctrl/Makefile-4-\n--\nfs/resctrl/internal.h=470=u32 resctrl_io_alloc_closid(struct rdt_resource *r);\nfs/resctrl/internal.h-471-\nfs/resctrl/internal.h:472:#ifdef CONFIG_RESCTRL_FS_PSEUDO_LOCK\nfs/resctrl/internal.h-473-int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp);\n--\nfs/resctrl/internal.h=517=static inline void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp) { }\nfs/resctrl/internal.h:518:#endif /* CONFIG_RESCTRL_FS_PSEUDO_LOCK */\nfs/resctrl/internal.h-519-\n--\nfs/resctrl/monitor.c=55=static LIST_HEAD(rmid_free_lru);\n--\nfs/resctrl/monitor.c-58- * @closid_num_dirty_rmid The number of dirty RMID each CLOSID has.\nfs/resctrl/monitor.c:59: * Only allocated when CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID is defined.\nfs/resctrl/monitor.c-60- * Indexed by CLOSID. Protected by rdtgroup_mutex.\n--\nfs/resctrl/monitor.c=116=static void limbo_release_entry(struct rmid_entry *entry)\n--\nfs/resctrl/monitor.c-122-\nfs/resctrl/monitor.c:123:\tif (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID))\nfs/resctrl/monitor.c-124-\t\tclosid_num_dirty_rmid[entry-\u003eclosid]--;\n--\nfs/resctrl/monitor.c=245=int resctrl_find_cleanest_closid(void)\n--\nfs/resctrl/monitor.c-251-\nfs/resctrl/monitor.c:252:\tif (!IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID))\nfs/resctrl/monitor.c-253-\t\treturn -EIO;\n--\nfs/resctrl/monitor.c=298=static void add_rmid_to_limbo(struct rmid_entry *entry)\n--\nfs/resctrl/monitor.c-324-\trmid_limbo_count++;\nfs/resctrl/monitor.c:325:\tif (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID))\nfs/resctrl/monitor.c-326-\t\tclosid_num_dirty_rmid[entry-\u003eclosid]++;\n--\nfs/resctrl/monitor.c=1875=static int closid_num_dirty_rmid_alloc(struct rdt_resource *r)\nfs/resctrl/monitor.c-1876-{\nfs/resctrl/monitor.c:1877:\tif (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) {\nfs/resctrl/monitor.c-1878-\t\tu32 num_closid = resctrl_arch_get_num_closid(r);\n--\nfs/resctrl/monitor.c=1904=static void closid_num_dirty_rmid_free(void)\nfs/resctrl/monitor.c-1905-{\nfs/resctrl/monitor.c:1906:\tif (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) {\nfs/resctrl/monitor.c-1907-\t\tmutex_lock(\u0026rdtgroup_mutex);\n--\nfs/resctrl/rdtgroup.c=186=static int closid_alloc(void)\n--\nfs/resctrl/rdtgroup.c-192-\nfs/resctrl/rdtgroup.c:193:\tif (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID) \u0026\u0026\nfs/resctrl/rdtgroup.c-194-\t resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID)) {\n--\nfs/resctrl/rdtgroup.c=1518=static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,\n--\nfs/resctrl/rdtgroup.c-1572-\t\trdtgrp-\u003emode = RDT_MODE_EXCLUSIVE;\nfs/resctrl/rdtgroup.c:1573:\t} else if (IS_ENABLED(CONFIG_RESCTRL_FS_PSEUDO_LOCK) \u0026\u0026\nfs/resctrl/rdtgroup.c-1574-\t\t !strcmp(buf, \"pseudo-locksetup\")) {\n"
} |
| 5/2 | 2026/10/24 22:27 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 27572 output: 27 thoughts: 143 Thoughts: **My Configuration Exploration for cgroups and kernfs** Alright, let's dive into the necessary configurations for cgroups and kernfs. First, I know kernfs is inherently tied to SYSFS and CGROUPS, meaning it's either always compiled in or enabled through those. Let me quickly verify the cgroups config; yes, that's `CONFIG_CGROUPS`. Now, I want to make sure I understand the core VFS changes that kernfs touches. The crucial parts are going to be in `fs/namei.c` and `fs/open.c`. Good, those are unconditionally compiled in - no surprises there. And, as expected, `fs/kernfs/dir.c` and `fs/kernfs/inode.c` are guarded by `CONFIG_KERNFS`. So, let's just make sure that `CONFIG_KERNFS` is set up correctly in the Kconfig. |
| 6/2 | 2026/10/24 22:27 | tool | grepper |
0mArgs:{
"Expression": "config KERNFS",
"PathPrefixes": [
"fs/"
]
}Results: {
"Output": "fs/kernfs/Kconfig-5-\nfs/kernfs/Kconfig:6:config KERNFS\nfs/kernfs/Kconfig-7-\tbool\n"
} |
| 7/2 | 2026/10/24 22:27 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 27989 output: 18 thoughts: 10 Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience: **Analyzing the Selection of KERNFS** Alright, let's dive in. My mind is immediately drawn to understanding *why* KERNFS is being selected. What are the specific requirements or conditions that are driving this choice? I need to understand the context. Is it for a particular filesystem implementation, a specific kernel module interaction, or a broader system design choice? The implications are vast, so I need to pinpoint the motivation. Is the goal performance, security, resource management, or something else entirely? I'm thinking about the trade-offs involved. KERNFS, with its hierarchical structure and attribute-based interface, offers certain advantages, but it also introduces complexity. I'm considering potential bottlenecks, overhead, and the learning curve involved. What alternatives were considered and rejected, and what were the rationales for those decisions? Understanding the design space is crucial. Furthermore, I am also pondering if this selection is the current best practice for the underlying operating system (OS). Is this selection driven by existing conventions or a recent technology decision? |
| 8/2 | 2026/10/24 22:27 | tool | grepper |
0mArgs:{
"Expression": "select KERNFS"
}Results: {
"Output": "fs/resctrl/Kconfig=1=config RESCTRL_FS\n--\nfs/resctrl/Kconfig-3-\tdepends on ARCH_HAS_CPU_RESCTRL\nfs/resctrl/Kconfig:4:\tselect KERNFS\nfs/resctrl/Kconfig-5-\tselect PROC_CPU_RESCTRL if PROC_FS\n--\nfs/sysfs/Kconfig=2=config SYSFS\n--\nfs/sysfs/Kconfig-4-\tdefault y\nfs/sysfs/Kconfig:5:\tselect KERNFS\nfs/sysfs/Kconfig-6-\thelp\n--\ninit/Kconfig=529=config PSI\ninit/Kconfig-530-\tbool \"Pressure stall information tracking\"\ninit/Kconfig:531:\tselect KERNFS\ninit/Kconfig-532-\thelp\n--\ninit/Kconfig=859=menuconfig CGROUPS\ninit/Kconfig-860-\tbool \"Control Group support\"\ninit/Kconfig:861:\tselect KERNFS\ninit/Kconfig-862-\thelp\n"
} |
| 9/2 | 2026/10/24 22:27 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28525 output: 33 thoughts: 794 Thoughts: **Analyzing Filesystem Changes for Atomic mkdir Support** Okay, so I'm looking at how `KERNFS` is being selected by `SYSFS`, `CGROUPS`, and `RESCTRL_FS`. That immediately flags a change in the filesystem layer related to `KERNFS` and, more specifically, atomic directory creation (`mkdir`). I need to understand the scope of this change. First, let's see which other filesystems might be impacted. I see a list: `9p`, `ceph`, `ecryptfs`, `fuse`, `gfs2`, `nfs`, `cifs`, `tracefs`, `vboxsf`, and `apparmorfs`. Interesting. The common modification for these filesystems seems to be setting `sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;`. This indicates a broader effort to introduce atomic `mkdir` support into the Virtual File System (VFS) layer, specifically through the `kernfs_iop_mkdir` implementation. This is being used by cgroups and resctrl. Now, let's dig into the core changes within `fs/kernfs/dir.c`. The listed functions are all relevant: `kernfs_iop_mkdir`, `kernfs_active`, `kernfs_get_inode_locked`, and the cgroup/rdtgroup related functions: `cgroup_mkdir`, `rdtgroup_mkdir`, `rdtgroup_mkdir_mon`, `rdtgroup_mkdir_ctrl_mon`. These are the implementation details. And then the related support functions: `build_open_flags`, `finish_lookup`, `lookup_open`, `atomic_open`, `vfs_mkdir_no_perm`, `vfs_create_no_perm`, `may_o_create`, `vfs_mkdir`, and `vfs_create`. Next step, I'll need to check the changes in `fs/namei.c` and `fs/open.c`. I need to ensure that the static functions which are critical, like `finish_lookup`, `lookup_open`, `atomic_open`, `vfs_mkdir_no_perm` and `may_o_create` are appropriately handled. I need to understand what non-static functions in those files are involved. Okay, in `fs/namei.c`, I see that `vfs_create`, `vfs_mkdir`, `lookup_slow` (which calls `finish_lookup`), `dentry_create`, `filename_unlinkat`, and `filename_renameat2` are the non-static functions of interest. I also need to check the calls of `open_last_lookups` and `do_open`. I'll trace them up to their callers, which I expect to lead me to `path_openat`. |
| 10/2 | 2026/10/24 22:28 | tool | grepper |
0mArgs:{
"Expression": "open_last_lookups",
"PathPrefixes": [
"fs/namei.c"
]
}Results: {
"Output": "fs/namei.c=4503=static struct dentry *lookup_open(struct nameidata *nd, struct file *file,\n--\nfs/namei.c-4686- * Open a file after lookup and/or create. This provides similar\nfs/namei.c:4687: * functionality to open_last_lookups() for non-VFS users, particularly\nfs/namei.c-4688- * nfsd.\n--\nfs/namei.c=4775=static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag)\n--\nfs/namei.c-4804-\nfs/namei.c:4805:static const char *open_last_lookups(struct nameidata *nd,\nfs/namei.c-4806-\t\t struct file *file, const struct open_flags *op)\n--\nfs/namei.c=5067=static struct file *path_openat(struct nameidata *nd,\n--\nfs/namei.c-5083-\t\twhile (!(error = link_path_walk(s, nd)) \u0026\u0026\nfs/namei.c:5084:\t\t (s = open_last_lookups(nd, file, op)) != NULL)\nfs/namei.c-5085-\t\t\t;\n"
} |
| 11/2 | 2026/10/24 22:28 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 30303 output: 30 thoughts: 11 Thoughts: Here's my analysis of the request, thinking as an expert in the field: **Analyzing `lookup_open` Callers** Okay, so the prompt is directing me to examine the code that calls the function `lookup_open`. My initial instinct is to approach this systematically, as that's the only way to avoid missing something. I need to understand *why* this information is being sought. Is this a debugging exercise? A performance investigation? A security audit? The context is crucial for knowing *how deep* I need to go. The most straightforward way is to use a code search tool to find every instance where `lookup_open` is invoked. However, I can't be sure if that alone will give a complete picture. Are there any indirect calls? Are there any wrappers, macros, or metaprogramming tricks that might obscure the direct calls to the function? I need to consider these possibilities. If the project uses a debugger, I could set a breakpoint on `lookup_open` and observe the call stack in real-time. This might quickly reveal the calling context and pinpoint the relevant code paths. This approach will only work though if the tool works with the source code, and if there are breakpoints in the source. Beyond the immediate callers, I also need to think about the *inputs* to `lookup_open`. What arguments are being passed? Understanding the arguments, in conjunction with the calling context, would give me a deeper understanding of the function's role. I would look at the data being passed, the context in which it's being used, and what its used for. Moreover, are there any similar functions or related modules? Understanding the broader architecture helps. Is there an object-oriented design? If so, what classes or objects are interacting with `lookup_open`? This is critical for getting a more robust understanding. I'm essentially trying to trace the data flow and control flow associated with `lookup_open`. This means, tracing inputs, control flow, and output of the function in question. |
| 12/2 | 2026/10/24 22:28 | tool | grepper |
0mArgs:{
"Expression": "lookup_open",
"PathPrefixes": [
"fs/namei.c"
]
}Results: {
"Output": "fs/namei.c=4487=struct dentry *vfs_mkdir_no_perm(const struct mnt_idmap *, struct inode *, struct dentry *,\n--\nfs/namei.c-4502- */\nfs/namei.c:4503:static struct dentry *lookup_open(struct nameidata *nd, struct file *file,\nfs/namei.c-4504-\t\t\t\t const struct open_flags *op)\n--\nfs/namei.c-4679-/**\nfs/namei.c:4680: * vfs_lookup_open - open and possibly create a regular file\nfs/namei.c-4681- * @parent: directory to contain file\n--\nfs/namei.c-4701- */\nfs/namei.c:4702:struct file *vfs_lookup_open(struct path *parent, struct qstr *last,\nfs/namei.c-4703-\t\t\t int open_flag, umode_t mode)\n--\nfs/namei.c-4735-\top.mode = mode;\nfs/namei.c:4736:\tdentry = lookup_open(\u0026nd, file, \u0026op);\nfs/namei.c-4737-\n--\nfs/namei.c-4772-}\nfs/namei.c:4773:EXPORT_SYMBOL_FOR_MODULES(vfs_lookup_open, \"nfsd\");\nfs/namei.c-4774-\n--\nfs/namei.c=4805=static const char *open_last_lookups(struct nameidata *nd,\n--\nfs/namei.c-4818-\t\t * Return -EOPNOTSUPP consistently, not -EEXIST, for paths\nfs/namei.c:4819:\t\t * ending in . or .., as lookup_open() does for other names.\nfs/namei.c-4820-\t\t */\n--\nfs/namei.c-4844-\nfs/namei.c:4845:\tdentry = lookup_open(nd, file, op);\nfs/namei.c-4846-\tif (IS_ERR(dentry))\n"
} |
| 13/2 | 2026/10/24 22:28 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31335 output: 31 thoughts: 55 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Investigating the `open_last_lookups` Call** Right, so `path_openat` seems to be the entry point that kicks off the whole `open_last_lookups` sequence, which, as we know, eventually leads into a call to `lookup_open`. I'm trying to get a clearer picture of how this flow works, and I need to establish a few key things. Specifically, I'm trying to determine the scope of `path_openat`. Is it a static function, or is it exposed more widely? Knowing that will significantly impact my understanding of how other parts of the kernel interact with the file opening process. The first place I'm going to check is `fs/namei.c`. That's where I would expect to find the definition of `path_openat`. Let's see what's what. |
| 14/2 | 2026/10/24 22:28 | tool | grepper |
0mArgs:{
"Expression": "path_openat",
"PathPrefixes": [
"fs/namei.c"
]
}Results: {
"Output": "fs/namei.c=5055=static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)\n--\nfs/namei.c-5066-\nfs/namei.c:5067:static struct file *path_openat(struct nameidata *nd,\nfs/namei.c-5068-\t\t\tconst struct open_flags *op, unsigned flags)\n--\nfs/namei.c=5106=struct file *do_file_open(int dfd, struct filename *pathname,\n--\nfs/namei.c-5115-\tset_nameidata(\u0026nd, dfd, pathname, NULL);\nfs/namei.c:5116:\tfilp = path_openat(\u0026nd, op, flags | LOOKUP_RCU);\nfs/namei.c-5117-\tif (unlikely(filp == ERR_PTR(-ECHILD)))\nfs/namei.c:5118:\t\tfilp = path_openat(\u0026nd, op, flags);\nfs/namei.c-5119-\tif (unlikely(filp == ERR_PTR(-ESTALE)))\nfs/namei.c:5120:\t\tfilp = path_openat(\u0026nd, op, flags | LOOKUP_REVAL);\nfs/namei.c-5121-\trestore_nameidata();\n--\nfs/namei.c=5125=struct file *do_file_open_root(const struct path *root,\n--\nfs/namei.c-5139-\tset_nameidata(\u0026nd, -1, filename, root);\nfs/namei.c:5140:\tfile = path_openat(\u0026nd, op, flags | LOOKUP_RCU);\nfs/namei.c-5141-\tif (unlikely(file == ERR_PTR(-ECHILD)))\nfs/namei.c:5142:\t\tfile = path_openat(\u0026nd, op, flags);\nfs/namei.c-5143-\tif (unlikely(file == ERR_PTR(-ESTALE)))\nfs/namei.c:5144:\t\tfile = path_openat(\u0026nd, op, flags | LOOKUP_REVAL);\nfs/namei.c-5145-\trestore_nameidata();\n"
} |
| 15/2 | 2026/10/24 22:28 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32204 output: 31 thoughts: 47 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Analyzing the `fs/open.c` Flow** Right, so `do_file_open` is being called from within `do_sys_openat2` – that's a key initial observation. Specifically, looking at the code path in `fs/open.c`. I need to understand how the flags are handled; the `build_open_flags` function is relevant here. I'll need to drill down around that point in `fs/open.c` to understand precisely how the flags from `do_sys_openat2` are being translated and used by `do_file_open`. I'll be looking for any potential discrepancies or interesting interactions between the system call and the file opening logic. This probably includes the flag parsing, any permission checks, and how the file descriptor is constructed. Time to get my hands dirty. |
| 16/2 | 2026/10/24 22:28 | tool | grepper |
0mArgs:{
"Expression": "build_open_flags",
"PathPrefixes": [
"fs/open.c"
]
}Results: {
"Output": "fs/open.c=1210=inline struct open_how build_open_how(int flags, umode_t mode)\n--\nfs/open.c-1225-\nfs/open.c:1226:inline int build_open_flags(const struct open_how *how, struct open_flags *op)\nfs/open.c-1227-{\n--\nfs/open.c-1250-\t * Older syscalls implicitly clear all of the invalid flags or argument\nfs/open.c:1251:\t * values before calling build_open_flags(), but openat2(2) checks all\nfs/open.c-1252-\t * of its arguments.\n--\nfs/open.c=1411=struct file *file_open_name(struct filename *name, int flags, umode_t mode)\n--\nfs/open.c-1414-\tstruct open_how how = build_open_how(flags, mode);\nfs/open.c:1415:\tint err = build_open_flags(\u0026how, \u0026op);\nfs/open.c-1416-\tif (err)\n--\nfs/open.c=1439=struct file *file_open_root(const struct path *root,\n--\nfs/open.c-1443-\tstruct open_how how = build_open_how(flags, mode);\nfs/open.c:1444:\tint err = build_open_flags(\u0026how, \u0026op);\nfs/open.c-1445-\tif (err)\n--\nfs/open.c=1451=static int do_sys_openat2(int dfd, const char __user *filename,\n--\nfs/open.c-1454-\tstruct open_flags op;\nfs/open.c:1455:\tint err = build_open_flags(how, \u0026op);\nfs/open.c-1456-\tif (unlikely(err))\n"
} |
| 17/2 | 2026/10/24 22:28 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32982 output: 27 thoughts: 1875 Thoughts: **Focus Symbol Analysis for Fuzzing with Kernfs and cgroups** Okay, I need to figure out the right `FocusSymbols` to use for fuzzing this patch, making sure I don't dilute the fuzzer's efforts. The goal is to specifically target the new functionality related to kernfs, cgroups, and resctrl directory creation. I've got to avoid listing ubiquitous functions that get called everywhere – that's a key principle. First, `do_sys_openat2` is out. It's called on *every* open and openat call; it's a lifecycle hot-path. Too generic. Dilution risk is high. Now, let's examine the others. `kernfs_iop_mkdir`? Not ubiquitous! It's specific to the kernfs inode operations for `mkdir`. Good candidate. `kernfs_get_inode_locked`, also likely good, it's new and relates to inode handling in `fs/kernfs/inode.c`. `cgroup_mkdir` and `rdtgroup_mkdir` are also promising; they're specific to cgroup and resctrl directory creation respectively. `vfs_mkdir` seems like a key function in `openat(..., O_CREAT | O_DIRECTORY | O_EXCL)` but its definition has been refactored, and it might be called on every mkdir. This is worth investigating. Now, for `lookup_open`, I need to check whether it's static and, if so, whether it's inlined. The documentation in Section 4 is really important here: static inlines in headers are not going to work, but non-inlined static functions *in .c files* *do* have symbols. I confirmed `lookup_open` is static. Now I need to check whether this static function is inlined. After checking the source, it's clear that `vfs_mkdir` calls `vfs_mkdir_no_perm`. And `vfs_create` also calls a `vfs_create_no_perm`. That `vfs_mkdir` is a caller, so it's a good candidate for inclusion. Okay, back to `kernfs_iop_mkdir`. It's the `mkdir` method in the `inode_operations` struct for kernfs! So, when `mkdir` is called on a kernfs mount (like a cgroup mount), this is the function that gets invoked. Inside `kernfs_iop_mkdir`, there's a call to `scops->mkdir`, which might be either `cgroup_mkdir` or `rdtgroup_mkdir`. Then it calls `kernfs_get_inode_locked`, which is a non-static symbol. Bingo! These are definitely good candidates for inclusion. Also, `kernfs_active` is a good non-static symbol. `cgroup_mkdir` and `rdtgroup_mkdir` are also non-ubiquitous, and specific to the cgroup and resctrl paths. Finally, `finish_lookup` and `may_o_create` are also important to check for inclusion in `FocusSymbols`. And so are the external symbols, `vfs_lookup_open` and `dentry_create`. |
| 18/2 | 2026/10/24 22:28 | tool | grepper |
0mArgs:{
"Expression": "dentry_create",
"PathPrefixes": [
"fs/"
]
}Results: {
"Output": "fs/namei.c=5232=EXPORT_SYMBOL(start_creating_user_path);\n--\nfs/namei.c-5234-/**\nfs/namei.c:5235: * dentry_create - Create and open a regular file\nfs/namei.c-5236- * @path: path to create\n--\nfs/namei.c-5250- */\nfs/namei.c:5251:struct file *dentry_create(struct path *path, int flags, umode_t mode,\nfs/namei.c-5252-\t\t\t const struct cred *cred)\n--\nfs/namei.c-5299-}\nfs/namei.c:5300:EXPORT_SYMBOL(dentry_create);\nfs/namei.c-5301-\n--\nfs/nfsd/nfs4proc.c=206=nfsd4_vfs_create(struct svc_fh *fhp, struct dentry **child,\n--\nfs/nfsd/nfs4proc.c-229-\tpath.dentry = *child;\nfs/nfsd/nfs4proc.c:230:\tfilp = dentry_create(\u0026path, oflags, open-\u003eop_iattr.ia_mode,\nfs/nfsd/nfs4proc.c-231-\t\t\t current_cred());\n--\nfs/overlayfs/dir.c=598=static const struct cred *ovl_override_creator_creds(const struct cred *original_creds,\n--\nfs/overlayfs/dir.c-612-\nfs/overlayfs/dir.c:613:\terr = security_dentry_create_files_as(dentry, mode, \u0026dentry-\u003ed_name,\nfs/overlayfs/dir.c-614-\t\t\t\t\t original_creds, override_cred);\n"
} |
| 19/2 | 2026/10/24 22:28 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 36064 output: 22 thoughts: 1485 Thoughts: **Patch Analysis and Fuzzing Prioritization** Okay, let's break down this patch and see if it's worth fuzzing. I need to understand the scope of changes and how they impact the system. It looks like we're dealing with modifications to several functions related to directory creation. First, I see the following functions have been modified: `kernfs_iop_mkdir`, `kernfs_get_inode_locked`, `cgroup_mkdir`, `rdtgroup_mkdir` variants, `lookup_open`, `vfs_mkdir`, `finish_lookup`, `may_o_create`, `vfs_create`, and `dentry_create`. My initial thought is that this is quite a broad range of changes, but I need to drill down and understand the core logic. I have some concerns about `vfs_create` and `dentry_create` which seem ubiquitous. However, `vfs_create` isn't always invoked directly; syzkaller's invocation is critical for assessing coverage. It appears that standard `open(O_CREAT)` calls often bypass these via `lookup_open` or `vfs_create_no_perm`. That leads me to focus on `lookup_open`. I need to understand under what circumstances `lookup_open` is actually called. It looks like `lookup_open` gets called within `path_openat` when the open flag includes `O_CREAT`, `O_TRUNC`, `O_WRONLY`, or `O_RDWR`. However, if a "fast lookup" succeeds for a read-only open, it skips `lookup_open`. But if `O_CREAT` (or `O_DIRECTORY | O_CREAT`) is used, fast lookup either returns NULL, or fails to complete, and then calls `lookup_open`. That's where the critical atomic mkdir logic lives. The patch includes the introduction of atomic mkdir and associated checks. `lookup_open` calls `vfs_mkdir_no_perm` if the open flag sets `O_IS_MKDIR` and then it checks if `SB_I_NO_ATOMIC_MKDIR` is set. Also, `may_o_create` checks `S_ISDIR(mode)` and does security checks. `kernfs_iop_mkdir` is where `kernfs` is being updated to support this new functionality, and `kernfs_get_inode_locked` is where inode operations are handled. It looks like `cgroup_mkdir` returns the `kernfs_node *`. Now, does this patch meet the criteria for fuzzing? I think it does. The changes touch core VFS logic for directory creation when `O_CREAT | O_DIRECTORY | O_EXCL` is used. There are refactorings related to `vfs_mkdir`. Also, runtime assertions are being added (`WARN_ON_ONCE`). It seems that kernfs directory handling is being updated and there are changes to cgroups. This all suggests this is very much `WorthFuzzing`. Next, I need to look at the relevant configurations for the affected modules. The core VFS code is always compiled. `kernfs` is controlled by `CONFIG_KERNFS`, which is enabled by `SYSFS`, `CGROUPS`, or `RESCTRL_FS`. Checking Kconfig, `CGROUPS` is `CONFIG_CGROUPS`. `RESCTRL_FS` depends on `ARCH_HAS_CPU_RESCTRL`. We need to determine if x86/amd64 systems select `ARCH_HAS_CPU_RESCTRL`. |
| 20/2 | 2026/10/24 22:28 | tool | grepper |
0mArgs:{
"Expression": "ARCH_HAS_CPU_RESCTRL"
}Results: {
"Output": "arch/Kconfig=1616=config ARCH_HAS_PHYS_TO_DMA\n--\narch/Kconfig-1618-\narch/Kconfig:1619:config ARCH_HAS_CPU_RESCTRL\narch/Kconfig-1620-\tbool\n--\narch/arm64/Kconfig=2148=config ARM64_MPAM\n--\narch/arm64/Kconfig-2150-\tselect ARM64_MPAM_DRIVER\narch/arm64/Kconfig:2151:\tselect ARCH_HAS_CPU_RESCTRL\narch/arm64/Kconfig-2152-\thelp\n--\narch/x86/Kconfig=516=config X86_CPU_RESCTRL\n--\narch/x86/Kconfig-519-\tdepends on MISC_FILESYSTEMS\narch/x86/Kconfig:520:\tselect ARCH_HAS_CPU_RESCTRL\narch/x86/Kconfig-521-\tselect RESCTRL_FS\n--\nfs/resctrl/Kconfig=1=config RESCTRL_FS\nfs/resctrl/Kconfig-2-\tbool \"CPU Resource Control Filesystem (resctrl)\"\nfs/resctrl/Kconfig:3:\tdepends on ARCH_HAS_CPU_RESCTRL\nfs/resctrl/Kconfig-4-\tselect KERNFS\n--\ninclude/linux/resctrl.h-10-\ninclude/linux/resctrl.h:11:#ifdef CONFIG_ARCH_HAS_CPU_RESCTRL\ninclude/linux/resctrl.h-12-#include \u003casm/resctrl.h\u003e\n"
} |
| 21/2 | 2026/10/24 22:28 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 38750 output: 35 thoughts: 52 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **X86 RESCTRL Configuration - A Quick Check** Alright, so the system's indicating that `X86_CPU_RESCTRL` is linked to `RESCTRL_FS` on x86/amd64. That's a good starting point, but I need to quickly verify this and refresh my understanding of where this is defined within the kernel. I'll need to jump directly into `arch/x86/Kconfig` to confirm the configuration and ensure there aren't any nuances I'm missing. Time to confirm if this matches my prior assumptions or if there's something new to consider, like dependencies, default values, or potential conflicts with other features. Understanding the precise relationship between these options is vital before I can proceed. |
| 22/2 | 2026/10/24 22:28 | tool | grepper |
0mArgs:{
"Expression": "config X86_CPU_RESCTRL",
"PathPrefixes": [
"arch/x86"
]
}Results: {
"Output": "arch/x86/Kconfig=508=config X86_MPPARSE\n--\narch/x86/Kconfig-515-\narch/x86/Kconfig:516:config X86_CPU_RESCTRL\narch/x86/Kconfig-517-\tbool \"x86 CPU resource control support\"\n--\narch/x86/Kconfig-538-\narch/x86/Kconfig:539:config X86_CPU_RESCTRL_INTEL_AET\narch/x86/Kconfig-540-\tbool \"Intel Application Energy Telemetry\"\n"
} |
| 23/2 | 2026/10/24 22:28 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39180 output: 32 thoughts: 18 Thoughts: Here's my summary, as I delve into the code: **Unraveling the Dependencies of `X86_CPU_RESCTRL`** Alright, let's get down to brass tacks. I need to understand the architectural underpinnings of `X86_CPU_RESCTRL`. My initial thought is to trace its dependencies. Where does this feature sit within the wider x86 landscape? What pieces of the puzzle need to be in place for `X86_CPU_RESCTRL` to even be a viable option? I'll need to look at the include files, the Kconfig entries, the driver code if available, and any architecture-specific documentation. I need to figure out the minimal set of conditions that *must* be true. Is it dependent on a specific processor family or feature set like Intel RDT or AMD RAPL? Or is it more tightly coupled to the system's memory hierarchy? This involves a meticulous line-by-line inspection to build a complete mental model of its prerequisites. I'm aiming for a comprehensive understanding that's essential for any optimization or troubleshooting that might be required down the line. |
| 24/2 | 2026/10/24 22:28 | tool | grepper |
0mArgs:{
"Expression": "depends on.*",
"PathPrefixes": [
"arch/x86/Kconfig"
]
}Results: {
"Output": "\nFull output is too long, showing 200 out of 976 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (1 files in total):\narch/x86/Kconfig:235\n\narch/x86/Kconfig=10=config X86_32\narch/x86/Kconfig-11-\tdef_bool y\narch/x86/Kconfig:12:\tdepends on !64BIT\narch/x86/Kconfig-13-\t# Options that are inherently 32-bit kernel only:\n--\narch/x86/Kconfig=23=config X86_64\narch/x86/Kconfig-24-\tdef_bool y\narch/x86/Kconfig:25:\tdepends on 64BIT\narch/x86/Kconfig-26-\t# Options that are inherently 64-bit kernel only:\n--\narch/x86/Kconfig=40=config FORCE_DYNAMIC_FTRACE\narch/x86/Kconfig-41-\tdef_bool y\narch/x86/Kconfig:42:\tdepends on X86_32\narch/x86/Kconfig:43:\tdepends on FUNCTION_TRACER\narch/x86/Kconfig-44-\tselect DYNAMIC_FTRACE\n--\narch/x86/Kconfig=341=config INSTRUCTION_DECODER\narch/x86/Kconfig-342-\tdef_bool y\narch/x86/Kconfig:343:\tdepends on KPROBES || PERF_EVENTS || UPROBES\narch/x86/Kconfig-344-\n--\narch/x86/Kconfig=376=config GENERIC_ISA_DMA\narch/x86/Kconfig-377-\tdef_bool y\narch/x86/Kconfig:378:\tdepends on ISA_DMA_API\narch/x86/Kconfig-379-\n--\narch/x86/Kconfig=384=config GENERIC_BUG\narch/x86/Kconfig-385-\tdef_bool y\narch/x86/Kconfig:386:\tdepends on BUG\narch/x86/Kconfig-387-\tselect GENERIC_BUG_RELATIVE_POINTERS\n--\narch/x86/Kconfig=392=config ARCH_MAY_HAVE_PC_FDC\narch/x86/Kconfig-393-\tdef_bool y\narch/x86/Kconfig:394:\tdepends on ISA_DMA_API\narch/x86/Kconfig-395-\n--\narch/x86/Kconfig=411=config KASAN_SHADOW_OFFSET\narch/x86/Kconfig-412-\thex\narch/x86/Kconfig:413:\tdepends on KASAN\narch/x86/Kconfig-414-\tdefault 0xdffffc0000000000\n--\narch/x86/Kconfig=416=config HAVE_INTEL_TXT\narch/x86/Kconfig-417-\tdef_bool y\narch/x86/Kconfig:418:\tdepends on INTEL_IOMMU \u0026\u0026 ACPI\narch/x86/Kconfig-419-\n--\narch/x86/Kconfig=460=config X86_X2APIC\narch/x86/Kconfig-461-\tbool \"x2APIC interrupt controller architecture support\"\narch/x86/Kconfig:462:\tdepends on X86_LOCAL_APIC \u0026\u0026 X86_64 \u0026\u0026 (IRQ_REMAP || HYPERVISOR_GUEST)\narch/x86/Kconfig-463-\tdefault y\n--\narch/x86/Kconfig=484=config AMD_SECURE_AVIC\narch/x86/Kconfig-485-\tbool \"AMD Secure AVIC\"\narch/x86/Kconfig:486:\tdepends on AMD_MEM_ENCRYPT \u0026\u0026 X86_X2APIC\narch/x86/Kconfig-487-\thelp\n--\narch/x86/Kconfig=497=config X86_POSTED_MSI\narch/x86/Kconfig-498-\tbool \"Enable MSI and MSI-x delivery by posted interrupts\"\narch/x86/Kconfig:499:\tdepends on X86_64 \u0026\u0026 IRQ_REMAP\narch/x86/Kconfig-500-\thelp\n--\narch/x86/Kconfig=508=config X86_MPPARSE\n--\narch/x86/Kconfig-510-\tdefault y\narch/x86/Kconfig:511:\tdepends on X86_LOCAL_APIC\narch/x86/Kconfig-512-\thelp\n--\narch/x86/Kconfig=516=config X86_CPU_RESCTRL\narch/x86/Kconfig-517-\tbool \"x86 CPU resource control support\"\narch/x86/Kconfig:518:\tdepends on X86 \u0026\u0026 (CPU_SUP_INTEL || CPU_SUP_AMD)\narch/x86/Kconfig:519:\tdepends on MISC_FILESYSTEMS\narch/x86/Kconfig-520-\tselect ARCH_HAS_CPU_RESCTRL\n--\narch/x86/Kconfig=539=config X86_CPU_RESCTRL_INTEL_AET\narch/x86/Kconfig-540-\tbool \"Intel Application Energy Telemetry\"\narch/x86/Kconfig:541:\tdepends on X86_64 \u0026\u0026 X86_CPU_RESCTRL \u0026\u0026 CPU_SUP_INTEL \u0026\u0026 INTEL_PMT_TELEMETRY=y \u0026\u0026 INTEL_TPMI=y\narch/x86/Kconfig-542-\thelp\n--\narch/x86/Kconfig=552=config X86_FRED\narch/x86/Kconfig-553-\tbool \"Flexible Return and Event Delivery\"\narch/x86/Kconfig:554:\tdepends on X86_64\narch/x86/Kconfig-555-\thelp\n--\narch/x86/Kconfig=591=config X86_NUMACHIP\narch/x86/Kconfig-592-\tbool \"Numascale NumaChip\"\narch/x86/Kconfig:593:\tdepends on X86_64\narch/x86/Kconfig:594:\tdepends on X86_EXTENDED_PLATFORM\narch/x86/Kconfig:595:\tdepends on NUMA\narch/x86/Kconfig:596:\tdepends on SMP\narch/x86/Kconfig:597:\tdepends on X86_X2APIC\narch/x86/Kconfig:598:\tdepends on PCI_MMCONFIG\narch/x86/Kconfig-599-\thelp\n--\narch/x86/Kconfig=604=config X86_VSMP\n--\narch/x86/Kconfig-607-\tselect PARAVIRT\narch/x86/Kconfig:608:\tdepends on X86_64 \u0026\u0026 PCI\narch/x86/Kconfig:609:\tdepends on X86_EXTENDED_PLATFORM\narch/x86/Kconfig:610:\tdepends on SMP\narch/x86/Kconfig-611-\thelp\n--\narch/x86/Kconfig=616=config X86_UV\narch/x86/Kconfig-617-\tbool \"SGI Ultraviolet\"\narch/x86/Kconfig:618:\tdepends on X86_64\narch/x86/Kconfig:619:\tdepends on X86_EXTENDED_PLATFORM\narch/x86/Kconfig:620:\tdepends on NUMA\narch/x86/Kconfig:621:\tdepends on EFI\narch/x86/Kconfig:622:\tdepends on KEXEC_CORE\narch/x86/Kconfig:623:\tdepends on X86_X2APIC\narch/x86/Kconfig:624:\tdepends on PCI\narch/x86/Kconfig-625-\thelp\n--\narch/x86/Kconfig=629=config X86_INTEL_MID\narch/x86/Kconfig-630-\tbool \"Intel Z34xx/Z35xx MID platform support\"\narch/x86/Kconfig:631:\tdepends on X86_EXTENDED_PLATFORM\narch/x86/Kconfig:632:\tdepends on X86_PLATFORM_DEVICES\narch/x86/Kconfig:633:\tdepends on PCI\narch/x86/Kconfig:634:\tdepends on X86_64 || (EXPERT \u0026\u0026 PCI_GOANY)\narch/x86/Kconfig:635:\tdepends on X86_IO_APIC\narch/x86/Kconfig-636-\tselect I2C\n--\narch/x86/Kconfig=655=config X86_GOLDFISH\narch/x86/Kconfig-656-\tbool \"Goldfish (Virtual Platform)\"\narch/x86/Kconfig:657:\tdepends on X86_EXTENDED_PLATFORM\narch/x86/Kconfig-658-\thelp\n--\narch/x86/Kconfig=666=config X86_INTEL_CE\narch/x86/Kconfig-667-\tbool \"CE4100 TV platform\"\narch/x86/Kconfig:668:\tdepends on PCI\narch/x86/Kconfig:669:\tdepends on PCI_GODIRECT\narch/x86/Kconfig:670:\tdepends on X86_IO_APIC\narch/x86/Kconfig:671:\tdepends on X86_32\narch/x86/Kconfig:672:\tdepends on X86_EXTENDED_PLATFORM\narch/x86/Kconfig-673-\tselect X86_REBOOTFIXUPS\n--\narch/x86/Kconfig=681=config X86_INTEL_QUARK\narch/x86/Kconfig-682-\tbool \"Intel Quark platform support\"\narch/x86/Kconfig:683:\tdepends on X86_32\narch/x86/Kconfig:684:\tdepends on X86_EXTENDED_PLATFORM\narch/x86/Kconfig:685:\tdepends on X86_PLATFORM_DEVICES\narch/x86/Kconfig:686:\tdepends on PCI\narch/x86/Kconfig:687:\tdepends on PCI_GOANY\narch/x86/Kconfig:688:\tdepends on X86_IO_APIC\narch/x86/Kconfig-689-\tselect IOSF_MBI\n--\narch/x86/Kconfig=697=config X86_INTEL_LPSS\narch/x86/Kconfig-698-\tbool \"Intel Low Power Subsystem Support\"\narch/x86/Kconfig:699:\tdepends on X86 \u0026\u0026 ACPI \u0026\u0026 PCI\narch/x86/Kconfig-700-\tselect COMMON_CLK\n--\narch/x86/Kconfig=709=config X86_AMD_PLATFORM_DEVICE\narch/x86/Kconfig-710-\tbool \"AMD ACPI2Platform devices support\"\narch/x86/Kconfig:711:\tdepends on ACPI\narch/x86/Kconfig-712-\tselect COMMON_CLK\n--\narch/x86/Kconfig=720=config IOSF_MBI\narch/x86/Kconfig-721-\ttristate \"Intel SoC IOSF Sideband support for SoC platforms\"\narch/x86/Kconfig:722:\tdepends on PCI\narch/x86/Kconfig-723-\thelp\n--\narch/x86/Kconfig=737=config IOSF_MBI_DEBUG\narch/x86/Kconfig-738-\tbool \"Enable IOSF sideband access through debugfs\"\narch/x86/Kconfig:739:\tdepends on IOSF_MBI \u0026\u0026 DEBUG_FS\narch/x86/Kconfig-740-\thelp\n--\narch/x86/Kconfig=750=config X86_SUPPORTS_MEMORY_FAILURE\n--\narch/x86/Kconfig-752-\t# MCE code calls memory_failure():\narch/x86/Kconfig:753:\tdepends on X86_MCE\narch/x86/Kconfig-754-\t# On 32-bit this adds too big of NODES_SHIFT and we run out of page flags:\narch/x86/Kconfig-755-\t# On 32-bit SPARSEMEM adds too big of SECTIONS_WIDTH:\narch/x86/Kconfig:756:\tdepends on X86_64 || !SPARSEMEM\narch/x86/Kconfig-757-\tselect ARCH_SUPPORTS_MEMORY_FAILURE\n--\narch/x86/Kconfig=759=config X86_32_IRIS\narch/x86/Kconfig-760-\ttristate \"Eurobraille/Iris poweroff module\"\narch/x86/Kconfig:761:\tdepends on X86_32\narch/x86/Kconfig-762-\thelp\n--\narch/x86/Kconfig=772=config SCHED_OMIT_FRAME_POINTER\n--\narch/x86/Kconfig-774-\tprompt \"Single-depth WCHAN output\"\narch/x86/Kconfig:775:\tdepends on X86\narch/x86/Kconfig-776-\thelp\n--\narch/x86/Kconfig=796=config PARAVIRT\narch/x86/Kconfig-797-\tbool \"Enable paravirtualization code\"\narch/x86/Kconfig:798:\tdepends on HAVE_STATIC_CALL\narch/x86/Kconfig-799-\tselect HAVE_PV_STEAL_CLOCK_GEN\n--\narch/x86/Kconfig=806=config PARAVIRT_XXL\narch/x86/Kconfig-807-\tbool\narch/x86/Kconfig:808:\tdepends on X86_64\narch/x86/Kconfig-809-\tselect ARCH_HAS_LAZY_MMU_MODE\n--\narch/x86/Kconfig=811=config PARAVIRT_SPINLOCKS\narch/x86/Kconfig-812-\tbool \"Paravirtualization layer for spinlocks\"\narch/x86/Kconfig:813:\tdepends on PARAVIRT \u0026\u0026 SMP\narch/x86/Kconfig-814-\thelp\n--\n\n"
} |
| 25/2 | 2026/10/24 22:28 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43299 output: 244 thoughts: 7014 Thoughts: **Analysis of Atomic Directory Creation Patch for Fuzzing Focus**
Alright, let's break this down. We're looking at a patch series that implements atomic directory creation using `openat()` with `O_CREAT | O_DIRECTORY | O_EXCL`. My job is to figure out the best focus symbols to guide fuzzing towards the key changes without drowning the effort in noise.
First, the dependencies: `X86_CPU_RESCTRL` relies on `X86`, Intel/AMD CPUs, and `MISC_FILESYSTEMS`. `CGROUPS` is straightforward. The core of this is about atomic mkdir, so `kernfs` and `CGROUPS` are very relevant here. Also `RESCTRL_FS` or `X86_CPU_RESCTRL`.
The key principle is to avoid ubiquitous functions that are called almost everywhere, like memory allocators or common syscall entry points. We want to pinpoint functions that are *specific* to the logic being added or altered.
The most important functions impacted are:
1. `kernfs_iop_mkdir`: This has undergone substantial logic changes, allocating inodes, checking active states, and so on.
2. `kernfs_get_inode_locked`: This is a completely new function, handling locking and lookup/insertion of inodes within `kernfs`.
3. `cgroup_mkdir`: This function's signature has been changed to return `struct kernfs_node *`, which means significant functionality changes here.
4. `rdtgroup_mkdir`: Has also been modified to return `struct kernfs_node *`.
5. `may_o_create`: This is important, as it handles permissions and includes a new warning check for directory creation.
6. `finish_lookup`: This is a brand new helper function.
7. `vfs_mkdir`: Refactored to use `vfs_mkdir_no_perm`.
8. `lookup_open`: The central core logic, modified to support `O_CREAT | O_DIRECTORY`, adds the `SB_I_NO_ATOMIC_MKDIR` check, and calls `vfs_mkdir_no_perm`.
Now, we need to determine if any of these are inlined or ubiquitous. `finish_lookup` and `may_o_create` are static. `lookup_open` is static. It's not inlined. `vfs_mkdir` is not static. `kernfs_iop_mkdir` is static. `kernfs_get_inode_locked` is non-static and exported. `cgroup_mkdir` is non-static and global. `rdtgroup_mkdir` is static.
Here's the tricky part: `lookup_open` is called when creating files or looking up files with `O_CREAT`. It's not as ubiquitous as memory allocators but it's called on every file creation. The crucial thing is it also has the atomic mkdir logic. It specifically has this logic:
```c
if (create_dir && (dir_inode->i_sb->s_iflags & SB_I_NO_ATOMIC_MKDIR))
return ERR_PTR(-EOPNOTSUPP);
...
if (create_dir) {
struct dentry *res = vfs_mkdir_no_perm(idmap, dir_inode, dentry, mode,
&delegated_inode);
error = PTR_ERR_OR_ZERO(res);
if (!error) {
dentry = res;
/*
* Filesystems whose ->mkdir() may leave the dentry
* negative must set SB_I_NO_ATOMIC_MKDIR.
*/
if (WARN_ON_ONCE(d_is_negative(dentry)))
error = -EIO;
}
} else {
error = vfs_create_no_perm(idmap, dentry, mode, &delegated_inode);
}
```
If we *don't* include it, the fuzzer might miss the new logic entirely! With `lookup_open` included, we will fuzz the `WARN_ON_ONCE`.
However, `vfs_mkdir` also had a refactor:
```c
struct dentry *vfs_mkdir(const struct mnt_idmap *idmap, struct inode *dir,
struct dentry *dentry, umode_t mode,
struct delegated_inode *delegated_inode)
{
...
+ de = vfs_mkdir_no_perm(idmap, dir, dentry, mode, delegated
|
| 26/2 | 2026/10/24 22:29 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"CGROUPS",
"RESCTRL_FS",
"X86_CPU_RESCTRL"
],
"FocusSymbols": [
"lookup_open",
"may_o_create",
"finish_lookup",
"vfs_mkdir",
"kernfs_iop_mkdir",
"kernfs_get_inode_locked",
"cgroup_mkdir",
"rdtgroup_mkdir"
],
"Reasoning": "The patch series implements atomic directory creation via openat() with O_CREAT | O_DIRECTORY | O_EXCL. It modifies core VFS path lookup and creation logic (fs/namei.c, fs/open.c), refactors vfs_mkdir and vfs_create, adds new runtime assertions (WARN_ON_ONCE), and adapts kernfs (kernfs_iop_mkdir, kernfs_get_inode_locked) along with cgroup and resctrl subsystems to instantiate and return directory dentries/inodes atomically. These changes are in core reachable kernel code accessible to userspace syscalls.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"CGROUPS",
"RESCTRL_FS",
"X86_CPU_RESCTRL"
],
"FocusSymbols": [
"lookup_open",
"may_o_create",
"finish_lookup",
"vfs_mkdir",
"kernfs_iop_mkdir",
"kernfs_get_inode_locked",
"cgroup_mkdir",
"rdtgroup_mkdir"
],
"Reasoning": "The patch series implements atomic directory creation via openat() with O_CREAT | O_DIRECTORY | O_EXCL. It modifies core VFS path lookup and creation logic (fs/namei.c, fs/open.c), refactors vfs_mkdir and vfs_create, adds new runtime assertions (WARN_ON_ONCE), and adapts kernfs (kernfs_iop_mkdir, kernfs_get_inode_locked) along with cgroup and resctrl subsystems to instantiate and return directory dentries/inodes atomically. These changes are in core reachable kernel code accessible to userspace syscalls.",
"WorthFuzzing": true
} |
| 27/1 | 2026/10/24 22:29 | action | If |
0mArgs:{
"WorthFuzzing": true
}Results: null |
| 28/2 | 2026/10/24 22:29 | agent | kmsan-evaluator |
0mModel:core Args: null Results: {
"KMSANReasoning": "The patch series introduces support for atomic directory creation via openat() using the O_CREAT | O_DIRECTORY | O_EXCL flags. It refactors VFS path lookup and creation logic (lookup_open, finish_lookup, vfs_mkdir_no_perm), adds the SB_I_NO_ATOMIC_MKDIR superblock flag for unsupported filesystems, and updates kernfs/cgroup/resctrl mkdir callbacks to return the newly created kernfs_node directly.\n\nKMSAN applicability assessment:\n1. No Kernel-to-User Info Leaks: The changes only affect file creation and path lookup control flow, returning standard integer file descriptors or error codes to userspace. No uninitialized structures, buffers, or padding bytes are copied or leaked to user space.\n2. No Uninitialized Memory Branching/Reads: All local variables (flags, mode, dentry pointers, delegated_inode) are explicitly initialized before use. In kernfs, new inodes allocated with new_inode() are standard VFS inodes initialized via inode_init_always() and kernfs_init_inode().\n3. Bug Profile vs Tooling: Potential regressions from this refactoring involve VFS locking (lock nesting and rwsems, which are covered by LOCKDEP), dentry/inode reference counting and use-after-free/double-free (which are covered by KASAN), or semantic error handling.\n\nTherefore, standard KASAN and LOCKDEP builds are fully equipped to catch any memory or locking bugs introduced by these changes, and a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit b5331efeb2ca83571d91add2b1e2e364569ba29a
Author: syz-cluster <triage@syzkaller.com>
Date: Sat Oct 24 22:27:22 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/alpha/include/uapi/asm/errno.h b/arch/alpha/include/uapi/asm/errno.h
index 1a99f38813c79..0384f69989748 100644
--- a/arch/alpha/include/uapi/asm/errno.h
+++ b/arch/alpha/include/uapi/asm/errno.h
@@ -19,7 +19,7 @@
#define ENOPROTOOPT 42 /* Protocol not available */
#define EPROTONOSUPPORT 43 /* Protocol not supported */
#define ESOCKTNOSUPPORT 44 /* Socket type not supported */
-#define EOPNOTSUPP 45 /* Operation not supported on transport endpoint */
+#define EOPNOTSUPP 45 /* Operation not supported */
#define EPFNOSUPPORT 46 /* Protocol family not supported */
#define EAFNOSUPPORT 47 /* Address family not supported by protocol */
#define EADDRINUSE 48 /* Address already in use */
diff --git a/arch/mips/include/uapi/asm/errno.h b/arch/mips/include/uapi/asm/errno.h
index 1835a50b69cef..2b6813715d1e5 100644
--- a/arch/mips/include/uapi/asm/errno.h
+++ b/arch/mips/include/uapi/asm/errno.h
@@ -75,7 +75,7 @@
#define ENOPROTOOPT 99 /* Protocol not available */
#define EPROTONOSUPPORT 120 /* Protocol not supported */
#define ESOCKTNOSUPPORT 121 /* Socket type not supported */
-#define EOPNOTSUPP 122 /* Operation not supported on transport endpoint */
+#define EOPNOTSUPP 122 /* Operation not supported */
#define EPFNOSUPPORT 123 /* Protocol family not supported */
#define EAFNOSUPPORT 124 /* Address family not supported by protocol */
#define EADDRINUSE 125 /* Address already in use */
diff --git a/arch/parisc/include/uapi/asm/errno.h b/arch/parisc/include/uapi/asm/errno.h
index 93194fbb0a80d..74bd65d791457 100644
--- a/arch/parisc/include/uapi/asm/errno.h
+++ b/arch/parisc/include/uapi/asm/errno.h
@@ -84,7 +84,7 @@
#define ENOPROTOOPT 220 /* Protocol not available */
#define EPROTONOSUPPORT 221 /* Protocol not supported */
#define ESOCKTNOSUPPORT 222 /* Socket type not supported */
-#define EOPNOTSUPP 223 /* Operation not supported on transport endpoint */
+#define EOPNOTSUPP 223 /* Operation not supported */
#define EPFNOSUPPORT 224 /* Protocol family not supported */
#define EAFNOSUPPORT 225 /* Address family not supported by protocol */
#define EADDRINUSE 226 /* Address already in use */
diff --git a/arch/sparc/include/uapi/asm/errno.h b/arch/sparc/include/uapi/asm/errno.h
index 71940ec9130b4..699f648e1a356 100644
--- a/arch/sparc/include/uapi/asm/errno.h
+++ b/arch/sparc/include/uapi/asm/errno.h
@@ -16,7 +16,7 @@
#define ENOPROTOOPT 42 /* Protocol not available */
#define EPROTONOSUPPORT 43 /* Protocol not supported */
#define ESOCKTNOSUPPORT 44 /* Socket type not supported */
-#define EOPNOTSUPP 45 /* Op not supported on transport endpoint */
+#define EOPNOTSUPP 45 /* Operation not supported */
#define EPFNOSUPPORT 46 /* Protocol family not supported */
#define EAFNOSUPPORT 47 /* Address family not supported by protocol */
#define EADDRINUSE 48 /* Address already in use */
diff --git a/fs/9p/vfs_super.c b/fs/9p/vfs_super.c
index 79526044685c9..d0c594bee9918 100644
--- a/fs/9p/vfs_super.c
+++ b/fs/9p/vfs_super.c
@@ -40,6 +40,7 @@ static int v9fs_fill_super(struct super_block *sb)
sb->s_blocksize_bits = fls(v9ses->maxdata - 1);
sb->s_blocksize = 1 << sb->s_blocksize_bits;
sb->s_magic = V9FS_MAGIC;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
if (v9fs_proto_dotl(v9ses)) {
sb->s_op = &v9fs_super_ops_dotl;
if (!(v9ses->flags & V9FS_NO_XATTR))
diff --git a/fs/ceph/super.c b/fs/ceph/super.c
index 72935f665f11c..fa1cf1f4f57d6 100644
--- a/fs/ceph/super.c
+++ b/fs/ceph/super.c
@@ -1226,6 +1226,7 @@ static int ceph_set_super(struct super_block *s, struct fs_context *fc)
fsc->max_file_size = 1ULL << 40; /* temp value until we get mdsmap */
s->s_op = &ceph_super_ops;
+ s->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
set_default_d_op(s, &ceph_dentry_ops);
s->s_export_op = &ceph_export_ops;
diff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c
index 5f37cddb956fb..8ed6076d50afd 100644
--- a/fs/ecryptfs/main.c
+++ b/fs/ecryptfs/main.c
@@ -477,6 +477,7 @@ static int ecryptfs_get_tree(struct fs_context *fc)
/* ->kill_sb() will take care of sbi after that point */
sbi = NULL;
s->s_op = &ecryptfs_sops;
+ s->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
s->s_xattr = ecryptfs_xattr_handlers;
set_default_d_op(s, &ecryptfs_dops);
diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c
index e9552be3637bc..cf766c6feea43 100644
--- a/fs/fuse/inode.c
+++ b/fs/fuse/inode.c
@@ -1608,6 +1608,7 @@ static void fuse_sb_defaults(struct super_block *sb)
sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
sb->s_iflags |= SB_I_NOIDMAP;
sb->s_iflags |= SB_I_NO_DATA_INTEGRITY;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
if (sb->s_user_ns != &init_user_ns)
sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
diff --git a/fs/gfs2/ops_fstype.c b/fs/gfs2/ops_fstype.c
index 6ec47376aecf2..31bb09367d6bc 100644
--- a/fs/gfs2/ops_fstype.c
+++ b/fs/gfs2/ops_fstype.c
@@ -1137,6 +1137,7 @@ static int gfs2_fill_super(struct super_block *sb, struct fs_context *fc)
sb->s_flags |= SB_NOSEC;
sb->s_magic = GFS2_MAGIC;
sb->s_op = &gfs2_super_ops;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
set_default_d_op(sb, &gfs2_dops);
sb->s_export_op = &gfs2_export_ops;
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 324d61a00545b..abb6b0d212142 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -37,7 +37,7 @@ static bool __kernfs_active(struct kernfs_node *kn)
return atomic_read(&kn->active) >= 0;
}
-static bool kernfs_active(struct kernfs_node *kn)
+bool kernfs_active(struct kernfs_node *kn)
{
lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem);
return __kernfs_active(kn);
@@ -1293,18 +1293,44 @@ static struct dentry *kernfs_iop_mkdir(const struct mnt_idmap *idmap,
{
struct kernfs_node *parent = dir->i_private;
struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
- int ret;
+ struct kernfs_node *kn;
+ struct inode *inode;
if (!scops || !scops->mkdir)
- return ERR_PTR(-EPERM);
+ return ERR_PTR(-EOPNOTSUPP);
+
+ inode = new_inode(dir->i_sb);
+ if (!inode)
+ return ERR_PTR(-ENOMEM);
- if (!kernfs_get_active(parent))
+ if (!kernfs_get_active(parent)) {
+ iput(inode);
return ERR_PTR(-ENODEV);
+ }
- ret = scops->mkdir(parent, dentry->d_name.name, mode);
+ kn = scops->mkdir(parent, dentry->d_name.name, mode);
kernfs_put_active(parent);
- return ERR_PTR(ret);
+ if (IS_ERR(kn)) {
+ iput(inode);
+ return ERR_CAST(kn);
+ }
+
+ scoped_guard(rwsem_read, &kernfs_root(kn)->kernfs_rwsem)
+ inode = kernfs_get_inode_locked(inode, kn, false);
+ kernfs_put(kn);
+
+ if (!inode)
+ return ERR_PTR(-ESTALE);
+
+ /*
+ * We may race with kernfs_fh_to_dentry() which calls d_obtain_alias(),
+ * so the inode may already have a disconnected alias. Use
+ * d_splice_alias() to move it into place, which does not accept
+ * hashed dentries, so drop the negative dentry first.
+ */
+ d_drop(dentry);
+ return d_splice_alias(inode, dentry);
}
static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 6630b29d7c078..0da4e46ec75bf 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -204,7 +204,8 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
inode->i_generation = kernfs_gen(kn);
set_default_inode_attr(inode, kn->mode);
- kernfs_refresh_inode(kn, inode);
+ scoped_guard(rwsem_read, &kernfs_root(kn)->kernfs_iattr_rwsem)
+ kernfs_refresh_inode(kn, inode);
/* initialize inode according to type */
switch (kernfs_type(kn)) {
@@ -254,6 +255,63 @@ struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
return inode;
}
+static int kernfs_inode_test(struct inode *inode, void *data)
+{
+ return inode->i_ino == *(u64 *)data;
+}
+
+static int kernfs_inode_set(struct inode *inode, void *data)
+{
+ inode->i_ino = *(u64 *)data;
+ return 0;
+}
+
+/**
+ * kernfs_get_inode_locked - get the inode of @kn under kernfs_rwsem
+ * @inode: inode allocated with new_inode() on the target superblock
+ * @kn: kernfs_node to get the inode for
+ * @lookup: should we do a ilookup before attempting to insert the inode
+ *
+ * Attaches @inode to @kn. Caller should pass an @inode allocated with
+ * new_inode(), and hold the kernfs_rwsem semaphore for reading on the @kn
+ * root. Because the kernfs_rwsem is held and it is checked that @kn is still
+ * active, __kernfs_remove() always finds @inode to clear its i_nlink. If
+ * lookup is true, a quick ilookup is attempted first.
+ *
+ * Returns the inode that was found or inserted for @kn, or NULL if @kn is
+ * inactive. @inode is consumed.
+ */
+struct inode *kernfs_get_inode_locked(struct inode *inode, struct kernfs_node *kn,
+ bool lookup)
+{
+ u64 ino = kernfs_ino(kn);
+ struct inode *old = NULL, *new = inode;
+
+ lockdep_assert_held_read(&kernfs_root(kn)->kernfs_rwsem);
+
+ if (lookup) {
+ old = ilookup(inode->i_sb, ino);
+ if (old)
+ goto out_iput;
+ }
+
+ if (!kernfs_active(kn))
+ goto out_iput;
+
+ inode = inode_insert5(new, ino, kernfs_inode_test, kernfs_inode_set,
+ &ino);
+ if (inode != new) {
+ old = inode;
+ goto out_iput;
+ }
+
+ kernfs_init_inode(kn, inode);
+ return inode;
+out_iput:
+ iput(new);
+ return old;
+}
+
/*
* The kernfs_node serves as both an inode and a directory entry for
* kernfs. To prevent the kernfs inode numbers from being freed
diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h
index f1e93b09e27df..ec6e1f75513ba 100644
--- a/fs/kernfs/kernfs-internal.h
+++ b/fs/kernfs/kernfs-internal.h
@@ -176,6 +176,9 @@ extern struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;
* inode.c
*/
extern const struct xattr_handler * const kernfs_xattr_handlers[];
+struct inode *kernfs_get_inode_locked(struct inode *inode,
+ struct kernfs_node *kn,
+ bool lookup);
void kernfs_evict_inode(struct inode *inode);
int kernfs_iop_permission(const struct mnt_idmap *idmap,
struct inode *inode, int mask);
@@ -194,6 +197,7 @@ extern const struct dentry_operations kernfs_dops;
extern const struct file_operations kernfs_dir_fops;
extern const struct inode_operations kernfs_dir_iops;
+bool kernfs_active(struct kernfs_node *kn);
struct kernfs_node *kernfs_get_active(struct kernfs_node *kn);
void kernfs_put_active(struct kernfs_node *kn);
int kernfs_add_one(struct kernfs_node *kn);
diff --git a/fs/namei.c b/fs/namei.c
index 22c4f328b8a97..7365975a0a32a 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -1889,12 +1889,34 @@ static struct dentry *lookup_fast(struct nameidata *nd)
return dentry;
}
+/*
+ * If @dentry is in-lookup, look it up in @dir through ->lookup(). Returns
+ * the dentry to use from now on: @dentry itself, or the dentry or ERR_PTR()
+ * that ->lookup() returned instead, in which case @dentry is dropped.
+ */
+static struct dentry *finish_lookup(struct inode *dir, struct dentry *dentry,
+ unsigned int flags)
+{
+ struct dentry *res;
+
+ if (!d_in_lookup(dentry))
+ return dentry;
+
+ res = dir->i_op->lookup(dir, dentry, flags);
+ d_lookup_done(dentry);
+ if (unlikely(res)) {
+ dput(dentry);
+ return res;
+ }
+ return dentry;
+}
+
/* Fast lookup failed, do it the slow way */
static struct dentry *__lookup_slow(const struct qstr *name,
struct dentry *dir,
unsigned int flags)
{
- struct dentry *dentry, *old;
+ struct dentry *dentry;
struct inode *inode = dir->d_inode;
/* Don't go there if it's already dead */
@@ -1913,17 +1935,10 @@ static struct dentry *__lookup_slow(const struct qstr *name,
goto again;
}
dput(dentry);
- dentry = ERR_PTR(error);
- }
- } else {
- old = inode->i_op->lookup(inode, dentry, flags);
- d_lookup_done(dentry);
- if (unlikely(old)) {
- dput(dentry);
- dentry = old;
+ return ERR_PTR(error);
}
}
- return dentry;
+ return finish_lookup(inode, dentry, flags);
}
static noinline struct dentry *lookup_slow(const struct qstr *name,
@@ -2781,9 +2796,16 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
return s;
}
+static inline bool trailing_slashes(const struct qstr *last)
+{
+ /* last->len is set by hash_name() to the length of the current
+ * component ->name, terminating with '/' or a NUL character. */
+ return (bool)last->name[last->len];
+}
+
static inline const char *lookup_last(struct nameidata *nd)
{
- if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
+ if (nd->last_type == LAST_NORM && trailing_slashes(&nd->last))
nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
return walk_component(nd, WALK_TRAILING);
@@ -4159,6 +4181,24 @@ static inline umode_t vfs_prepare_mode(const struct mnt_idmap *idmap,
return mode;
}
+static inline
+int vfs_create_no_perm(const struct mnt_idmap *idmap, struct dentry *dentry,
+ umode_t mode, struct delegated_inode *di)
+{
+ struct inode *dir = d_inode(dentry->d_parent);
+ int error;
+
+ error = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, di);
+ if (error)
+ return error;
+
+ error = dir->i_op->create(idmap, dir, dentry, mode);
+ if (!error)
+ fsnotify_create(dir, dentry);
+
+ return error;
+}
+
/**
* vfs_create - create new file
* @idmap: idmap of the mount the inode was found from
@@ -4185,19 +4225,14 @@ int vfs_create(const struct mnt_idmap *idmap, struct dentry *dentry, umode_t mod
return error;
if (!dir->i_op->create)
- return -EACCES; /* shouldn't it be ENOSYS? */
+ return -EOPNOTSUPP;
mode = vfs_prepare_mode(idmap, dir, mode, S_IALLUGO, S_IFREG);
error = security_inode_create(idmap, dir, dentry, mode);
if (error)
return error;
- error = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, di);
- if (error)
- return error;
- error = dir->i_op->create(idmap, dir, dentry, mode);
- if (!error)
- fsnotify_create(dir, dentry);
- return error;
+
+ return vfs_create_no_perm(idmap, dentry, mode, di);
}
EXPORT_SYMBOL(vfs_create);
@@ -4314,21 +4349,43 @@ static inline int open_to_namei_flags(int flag)
static int may_o_create(const struct mnt_idmap *idmap,
const struct path *dir, struct dentry *dentry,
- umode_t mode)
+ int open_flag, umode_t mode)
{
- int error = security_path_mknod(dir, dentry, mode, 0);
+ struct inode *dir_inode = dir->dentry->d_inode;
+ bool create_dir = O_IS_MKDIR(open_flag);
+ int error;
+
+ WARN_ON_ONCE(create_dir && !S_ISDIR(mode));
+
+ if (create_dir)
+ error = security_path_mkdir(dir, dentry, mode);
+ else
+ error = security_path_mknod(dir, dentry, mode, 0);
if (error)
return error;
if (!fsuidgid_has_mapping(dir->dentry->d_sb, idmap))
return -EOVERFLOW;
- error = inode_permission(idmap, dir->dentry->d_inode,
- MAY_WRITE | MAY_EXEC);
+ error = inode_permission(idmap, dir_inode, MAY_WRITE | MAY_EXEC);
if (error)
return error;
- return security_inode_create(idmap, dir->dentry->d_inode, dentry, mode);
+ if (create_dir)
+ error = security_inode_mkdir(idmap, dir_inode, dentry, mode);
+ else
+ error = security_inode_create(idmap, dir_inode, dentry, mode);
+
+ return error;
+}
+
+static inline umode_t o_create_mode(const struct mnt_idmap *idmap, const struct inode *dir,
+ int open_flag, umode_t mode)
+{
+ if (O_IS_MKDIR(open_flag))
+ return vfs_prepare_mode(idmap, dir, mode, S_IRWXUGO | S_ISVTX, S_IFDIR);
+ else
+ return vfs_prepare_mode(idmap, dir, mode, S_IALLUGO, S_IFREG);
}
/**
@@ -4364,8 +4421,10 @@ static struct dentry *atomic_open(const struct path *path, struct dentry *dentry
file->__f_path.dentry = DENTRY_NOT_SET;
file->__f_path.mnt = path->mnt;
+
error = dir_inode->i_op->atomic_open(dir_inode, dentry, file,
- open_to_namei_flags(open_flag), mode);
+ open_to_namei_flags(open_flag), mode);
+
d_lookup_done(dentry);
if (!error) {
@@ -4409,11 +4468,24 @@ static struct dentry *atomic_open(const struct path *path, struct dentry *dentry
error = create_error;
}
dput(dentry);
- dentry = ERR_PTR(error);
+ return ERR_PTR(error);
}
+
+ if (file->f_mode & FMODE_CREATED) {
+ if (d_is_dir(dentry))
+ fsnotify_mkdir(dir_inode, dentry);
+ else
+ fsnotify_create(dir_inode, dentry);
+ }
+ if (file->f_mode & FMODE_OPENED)
+ fsnotify_open(file);
+
return dentry;
}
+static inline
+struct dentry *vfs_mkdir_no_perm(const struct mnt_idmap *, struct inode *, struct dentry *,
+ umode_t, struct delegated_inode *);
/*
* Look up and maybe create and open the last component.
*
@@ -4435,6 +4507,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
const struct mnt_idmap *idmap;
struct dentry *dir = nd->path.dentry;
struct inode *dir_inode = dir->d_inode;
+ bool create_dir = O_IS_MKDIR(op->open_flag);
int open_flag;
struct dentry *dentry;
int error, create_error;
@@ -4449,6 +4522,9 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
create_error = 0;
shared_flag = (open_flag & O_CREAT) ? 0 : LOOKUP_SHARED;
+ if (create_dir && (dir_inode->i_sb->s_iflags & SB_I_NO_ATOMIC_MKDIR))
+ return ERR_PTR(-EOPNOTSUPP);
+
if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
got_write = !mnt_want_write(nd->path.mnt);
/*
@@ -4456,10 +4532,11 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
* a different error; we'll be dropping this one anyway.
*/
}
+
if (shared_flag)
- inode_lock_shared(dir_inode);
+ inode_lock_shared_nested(dir_inode, I_MUTEX_PARENT);
else
- inode_lock(dir_inode);
+ inode_lock_nested(dir_inode, I_MUTEX_PARENT);
if (unlikely(IS_DEADDIR(dir_inode))) {
dentry = ERR_PTR(-ENOENT);
@@ -4509,10 +4586,10 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
if (open_flag & O_CREAT) {
if (open_flag & O_EXCL)
open_flag &= ~O_TRUNC;
- mode = vfs_prepare_mode(idmap, dir_inode, mode, mode, mode);
+ mode = o_create_mode(idmap, dir_inode, open_flag, mode);
if (likely(got_write))
create_error = may_o_create(idmap, &nd->path,
- dentry, mode);
+ dentry, open_flag, mode);
else
create_error = -EROFS;
}
@@ -4526,19 +4603,10 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
goto out;
}
- if (d_in_lookup(dentry)) {
- struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
- nd->flags | shared_flag);
- d_lookup_done(dentry);
- if (unlikely(res)) {
- if (IS_ERR(res)) {
- error = PTR_ERR(res);
- goto out_dput;
- }
- dput(dentry);
- dentry = res;
- }
- }
+ dentry = finish_lookup(dir_inode, dentry, nd->flags | shared_flag);
+ if (IS_ERR(dentry))
+ goto out;
+
if (dentry->d_inode || !(op->open_flag & O_CREAT)) {
/*
* No need to create a file. If lookup returned a positive
@@ -4556,26 +4624,33 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
goto out_dput;
}
- error = try_break_deleg(dir_inode, LEASE_BREAK_DIR_CREATE, &delegated_inode);
- if (error)
- goto out_dput;
-
- file->f_mode |= FMODE_CREATED;
- if (!dir_inode->i_op->create) {
- error = -EACCES;
+ if ((create_dir && !dir_inode->i_op->mkdir)
+ || (!create_dir && !dir_inode->i_op->create)) {
+ error = -EOPNOTSUPP;
goto out_dput;
}
- error = dir_inode->i_op->create(idmap, dir_inode, dentry, mode);
+ if (create_dir) {
+ struct dentry *res = vfs_mkdir_no_perm(idmap, dir_inode, dentry, mode,
+ &delegated_inode);
+ error = PTR_ERR_OR_ZERO(res);
+ if (!error) {
+ dentry = res;
+ /*
+ * Filesystems whose ->mkdir() may leave the dentry
+ * negative must set SB_I_NO_ATOMIC_MKDIR.
+ */
+ if (WARN_ON_ONCE(d_is_negative(dentry)))
+ error = -EIO;
+ }
+ } else {
+ error = vfs_create_no_perm(idmap, dentry, mode, &delegated_inode);
+ }
if (error)
goto out_dput;
+
+ file->f_mode |= FMODE_CREATED;
out:
- if (!IS_ERR(dentry)) {
- if (file->f_mode & FMODE_CREATED)
- fsnotify_create(dir_inode, dentry);
- if (file->f_mode & FMODE_OPENED)
- fsnotify_open(file);
- }
if (shared_flag)
inode_unlock_shared(dir_inode);
else
@@ -4697,17 +4772,12 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
}
EXPORT_SYMBOL_FOR_MODULES(vfs_lookup_open, "nfsd");
-static inline bool trailing_slashes(struct nameidata *nd)
-{
- return (bool)nd->last.name[nd->last.len];
-}
-
static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag)
{
struct dentry *dentry;
if (open_flag & O_CREAT) {
- if (trailing_slashes(nd))
+ if (trailing_slashes(&nd->last) && !(open_flag & O_DIRECTORY))
return ERR_PTR(-EISDIR);
/* Don't bother on an O_EXCL create */
@@ -4715,7 +4785,7 @@ static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag)
return NULL;
}
- if (trailing_slashes(nd))
+ if (trailing_slashes(&nd->last))
nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
dentry = lookup_fast(nd);
@@ -4744,6 +4814,13 @@ static const char *open_last_lookups(struct nameidata *nd,
if (nd->last_type != LAST_NORM) {
if (nd->depth)
put_link(nd);
+ /*
+ * Return -EOPNOTSUPP consistently, not -EEXIST, for paths
+ * ending in . or .., as lookup_open() does for other names.
+ */
+ if (O_IS_MKDIR(open_flag) &&
+ (nd->path.dentry->d_sb->s_iflags & SB_I_NO_ATOMIC_MKDIR))
+ return ERR_PTR(-EOPNOTSUPP);
return handle_dots(nd, nd->last_type);
}
@@ -4808,7 +4885,7 @@ static int do_open(struct nameidata *nd,
if (open_flag & O_CREAT) {
if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
return -EEXIST;
- if (d_is_dir(nd->path.dentry))
+ if (!(open_flag & O_DIRECTORY) && d_is_dir(nd->path.dentry))
return -EISDIR;
error = may_create_in_sticky(idmap, nd,
d_backing_inode(nd->path.dentry));
@@ -5097,7 +5174,7 @@ static struct dentry *filename_create(int dfd, struct filename *name,
* Do the final lookup. Suppress 'create' if there is a trailing
* '/', and a directory wasn't requested.
*/
- if (last.name[last.len] && !want_dir)
+ if (trailing_slashes(&last) && !want_dir)
create_flags &= ~LOOKUP_CREATE;
dentry = start_dirop(path->dentry, &last, reval_flag | create_flags);
if (IS_ERR(dentry))
@@ -5155,7 +5232,7 @@ inline struct dentry *start_creating_user_path(
EXPORT_SYMBOL(start_creating_user_path);
/**
- * dentry_create - Create and open a file
+ * dentry_create - Create and open a regular file
* @path: path to create
* @flags: O\_ flags
* @mode: mode bits for new file
@@ -5192,7 +5269,7 @@ struct file *dentry_create(struct path *path, int flags, umode_t mode,
path->dentry = dir;
mode = vfs_prepare_mode(idmap, dir_inode, mode, S_IALLUGO, S_IFREG);
- create_error = may_o_create(idmap, path, dentry, mode);
+ create_error = may_o_create(idmap, path, dentry, flags, mode);
if (create_error)
flags &= ~O_CREAT;
@@ -5208,13 +5285,6 @@ struct file *dentry_create(struct path *path, int flags, umode_t mode,
/* Drop the extra reference */
dput(orig_dentry);
- if (!error) {
- if (file->f_mode & FMODE_CREATED)
- fsnotify_create(dir->d_inode, dentry);
- if (file->f_mode & FMODE_OPENED)
- fsnotify_open(file);
- }
-
path->dentry = dentry;
} else {
@@ -5366,6 +5436,33 @@ SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, d
return filename_mknodat(AT_FDCWD, name, mode, dev);
}
+static inline
+struct dentry *vfs_mkdir_no_perm(const struct mnt_idmap *idmap, struct inode *dir,
+ struct dentry *dentry, umode_t mode,
+ struct delegated_inode *di)
+{
+ unsigned max_links = dir->i_sb->s_max_links;
+ struct dentry *de;
+ int error;
+
+ if (max_links && dir->i_nlink >= max_links)
+ return ERR_PTR(-EMLINK);
+
+ error = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, di);
+ if (error)
+ return ERR_PTR(error);
+
+ de = dir->i_op->mkdir(idmap, dir, dentry, mode);
+ if (IS_ERR(de))
+ return de;
+ if (de) {
+ dput(dentry);
+ dentry = de;
+ }
+ fsnotify_mkdir(dir, dentry);
+ return dentry;
+}
+
/**
* vfs_mkdir - create directory returning correct dentry if possible
* @idmap: idmap of the mount the inode was found from
@@ -5393,14 +5490,13 @@ struct dentry *vfs_mkdir(const struct mnt_idmap *idmap, struct inode *dir,
struct delegated_inode *delegated_inode)
{
int error;
- unsigned max_links = dir->i_sb->s_max_links;
struct dentry *de;
error = may_create_dentry(idmap, dir, dentry);
if (error)
goto err;
- error = -EPERM;
+ error = -EOPNOTSUPP;
if (!dir->i_op->mkdir)
goto err;
@@ -5409,24 +5505,12 @@ struct dentry *vfs_mkdir(const struct mnt_idmap *idmap, struct inode *dir,
if (error)
goto err;
- error = -EMLINK;
- if (max_links && dir->i_nlink >= max_links)
+ de = vfs_mkdir_no_perm(idmap, dir, dentry, mode, delegated_inode);
+ if (IS_ERR(de)) {
+ error = PTR_ERR(de);
goto err;
-
- error = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, delegated_inode);
- if (error)
- goto err;
-
- de = dir->i_op->mkdir(idmap, dir, dentry, mode);
- error = PTR_ERR(de);
- if (IS_ERR(de))
- goto err;
- if (de) {
- dput(dentry);
- dentry = de;
}
- fsnotify_mkdir(dir, dentry);
- return dentry;
+ return de;
err:
end_creating(dentry);
@@ -5713,7 +5797,7 @@ int filename_unlinkat(int dfd, struct filename *name)
goto exit_drop_write;
/* Why not before? Because we want correct error value */
- if (unlikely(last.name[last.len])) {
+ if (unlikely(trailing_slashes(&last))) {
if (d_is_dir(dentry))
error = -EISDIR;
else
@@ -6315,16 +6399,16 @@ int filename_renameat2(int olddfd, struct filename *from,
if (flags & RENAME_EXCHANGE) {
if (!d_is_dir(rd.new_dentry)) {
error = -ENOTDIR;
- if (new_last.name[new_last.len])
+ if (trailing_slashes(&new_last))
goto exit_unlock;
}
}
/* unless the source is a directory trailing slashes give -ENOTDIR */
if (!d_is_dir(rd.old_dentry)) {
error = -ENOTDIR;
- if (old_last.name[old_last.len])
+ if (trailing_slashes(&old_last))
goto exit_unlock;
- if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
+ if (!(flags & RENAME_EXCHANGE) && trailing_slashes(&new_last))
goto exit_unlock;
}
diff --git a/fs/nfs/super.c b/fs/nfs/super.c
index cb19f1540d984..f4452b630f52b 100644
--- a/fs/nfs/super.c
+++ b/fs/nfs/super.c
@@ -1077,6 +1077,7 @@ static void nfs_fill_super(struct super_block *sb, struct nfs_fs_context *ctx)
sb->s_blocksize = 0;
sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
sb->s_op = server->nfs_client->cl_nfs_mod->sops;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
if (server->bsize)
sb->s_blocksize =
nfs_block_size(server->bsize, &sb->s_blocksize_bits);
diff --git a/fs/open.c b/fs/open.c
index e43f02ff64acb..8217106a6f4b4 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -1264,29 +1264,30 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
if (WILL_CREATE(flags)) {
if (how->mode & ~S_IALLUGO)
return -EINVAL;
- op->mode = how->mode | S_IFREG;
+ if (O_IS_MKDIR(flags))
+ op->mode = how->mode | S_IFDIR;
+ else
+ op->mode = how->mode | S_IFREG;
} else {
if (how->mode != 0)
return -EINVAL;
op->mode = 0;
}
- /*
- * Block bugs where O_DIRECTORY | O_CREAT created regular files.
- * Note, that blocking O_DIRECTORY | O_CREAT here also protects
- * O_TMPFILE below which requires O_DIRECTORY being raised.
- */
- if ((flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT))
- return -EINVAL;
-
/* Now handle the creative implementation of O_TMPFILE. */
if (flags & __O_TMPFILE) {
/*
* In order to ensure programs get explicit errors when trying
* to use O_TMPFILE on old kernels we enforce that O_DIRECTORY
- * is raised alongside __O_TMPFILE.
+ * is raised alongside __O_TMPFILE, but without O_CREAT. The
+ * reason for disallowing O_CREAT|O_TMPFILE is that
+ * O_DIRECTORY|O_CREAT used to work and created a regular file
+ * if nothing existed at the open path. Hence, allowing the
+ * combination would have caused O_CREAT|O_TMPFILE to create a
+ * regular (non-temporary) file on old kernels, while the caller
+ * would believe they created an actual O_TMPFILE.
*/
- if (!(flags & O_DIRECTORY))
+ if (!(flags & O_DIRECTORY) || (flags & O_CREAT))
return -EINVAL;
if (!(acc_mode & MAY_WRITE))
return -EINVAL;
@@ -1343,12 +1344,29 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
+ /*
+ * Requesting write access on a directory can never succeed. Rather
+ * than performing a path-walk to determine whether the target is
+ * actually a directory (-EISDIR) or not (-ENOTDIR), we short-circuit
+ * to -ENOTDIR.
+ */
+ if ((flags & O_DIRECTORY) && !(flags & __O_TMPFILE) && (acc_mode & MAY_WRITE))
+ return -ENOTDIR;
+
if (flags & O_CREAT) {
- op->intent |= LOOKUP_CREATE;
+ if (flags & O_DIRECTORY) {
+ /*
+ * O_CREAT|O_DIRECTORY is provided to prevent TOCTOUs, so
+ * it makes little sense to allow it without O_EXCL.
+ */
+ if (!(flags & O_EXCL))
+ return -EINVAL;
+ }
if (flags & O_EXCL) {
op->intent |= LOOKUP_EXCL;
flags |= O_NOFOLLOW;
}
+ op->intent |= LOOKUP_CREATE;
}
if (flags & O_DIRECTORY)
diff --git a/fs/resctrl/rdtgroup.c b/fs/resctrl/rdtgroup.c
index 57ab090072c21..be151cc8fd29c 100644
--- a/fs/resctrl/rdtgroup.c
+++ b/fs/resctrl/rdtgroup.c
@@ -4040,15 +4040,16 @@ static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
* and monitor group(ctrl_mon). This is a resource group
* to monitor a subset of tasks and cpus in its parent ctrl_mon group.
*/
-static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
- const char *name, umode_t mode)
+static struct kernfs_node *rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
+ const char *name, umode_t mode)
{
struct rdtgroup *rdtgrp, *prgrp;
+ struct kernfs_node *kn;
int ret;
ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp);
if (ret)
- return ret;
+ return ERR_PTR(ret);
prgrp = rdtgrp->mon.parent;
rdtgrp->closid = prgrp->closid;
@@ -4067,17 +4068,22 @@ static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
*/
list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
+ kn = rdtgrp->kn;
+ kernfs_get(kn);
+ rdtgroup_kn_unlock(parent_kn);
+ return kn;
+
out_unlock:
rdtgroup_kn_unlock(parent_kn);
- return ret;
+ return ERR_PTR(ret);
}
/*
* These are rdtgroups created under the root directory. Can be used
* to allocate and monitor resources.
*/
-static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
- const char *name, umode_t mode)
+static struct kernfs_node *rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
+ const char *name, umode_t mode)
{
struct rdtgroup *rdtgrp;
struct kernfs_node *kn;
@@ -4086,7 +4092,7 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp);
if (ret)
- return ret;
+ return ERR_PTR(ret);
kn = rdtgrp->kn;
ret = closid_alloc();
@@ -4125,7 +4131,9 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
rdtgrp->mba_mbps_event = mba_mbps_default_event;
}
- goto out_unlock;
+ kernfs_get(kn);
+ rdtgroup_kn_unlock(parent_kn);
+ return kn;
out_del_list:
list_del(&rdtgrp->rdtgroup_list);
@@ -4135,17 +4143,16 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
closid_free(closid);
out_common_fail:
mkdir_rdt_prepare_clean(rdtgrp);
-out_unlock:
rdtgroup_kn_unlock(parent_kn);
- return ret;
+ return ERR_PTR(ret);
}
-static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
- umode_t mode)
+static struct kernfs_node *rdtgroup_mkdir(struct kernfs_node *parent_kn,
+ const char *name, umode_t mode)
{
/* Do not accept '\n' to avoid unparsable situation. */
if (strchr(name, '\n'))
- return -EINVAL;
+ return ERR_PTR(-EINVAL);
/*
* If the parent directory is the root directory and RDT
@@ -4159,7 +4166,7 @@ static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
if (resctrl_arch_mon_capable())
return rdtgroup_mkdir_mon(parent_kn, name, mode);
- return -EPERM;
+ return ERR_PTR(-EPERM);
}
static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
diff --git a/fs/smb/client/cifsfs.c b/fs/smb/client/cifsfs.c
index 576155caf888e..f00dd2530b758 100644
--- a/fs/smb/client/cifsfs.c
+++ b/fs/smb/client/cifsfs.c
@@ -246,6 +246,7 @@ cifs_read_super(struct super_block *sb)
sb->s_magic = CIFS_SUPER_MAGIC;
sb->s_op = &cifs_super_ops;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
sb->s_xattr = cifs_xattr_handlers;
rc = super_setup_bdi(sb);
if (rc)
diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c
index 5020365ca7048..b679adf68c8fb 100644
--- a/fs/tracefs/inode.c
+++ b/fs/tracefs/inode.c
@@ -471,6 +471,7 @@ static int tracefs_fill_super(struct super_block *sb, struct fs_context *fc)
return err;
sb->s_op = &tracefs_super_operations;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
tracefs_apply_options(sb, false);
set_default_d_op(sb, &tracefs_dentry_operations);
diff --git a/fs/vboxsf/super.c b/fs/vboxsf/super.c
index a61fbab51d370..190b76f04d4f8 100644
--- a/fs/vboxsf/super.c
+++ b/fs/vboxsf/super.c
@@ -196,6 +196,7 @@ static int vboxsf_fill_super(struct super_block *sb, struct fs_context *fc)
sb->s_blocksize = 1024;
sb->s_maxbytes = MAX_LFS_FILESIZE;
sb->s_op = &vboxsf_super_ops;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
set_default_d_op(sb, &vboxsf_dentry_ops);
iroot = iget_locked(sb, 0);
diff --git a/include/linux/fcntl.h b/include/linux/fcntl.h
index 6ad6b9e7a226a..204e16bbe2634 100644
--- a/include/linux/fcntl.h
+++ b/include/linux/fcntl.h
@@ -30,6 +30,12 @@
*/
#define __O_REGULAR (1 << 30)
+#define O_MKDIR_MASK (O_CREAT | O_DIRECTORY)
+static inline bool O_IS_MKDIR(unsigned int flags)
+{
+ return (flags & O_MKDIR_MASK) == O_MKDIR_MASK;
+}
+
/* List of all valid flags for the how->resolve argument: */
#define VALID_RESOLVE_FLAGS \
(RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS | \
diff --git a/include/linux/fs/super_types.h b/include/linux/fs/super_types.h
index ecd96aeb1cee7..521b00c47d0aa 100644
--- a/include/linux/fs/super_types.h
+++ b/include/linux/fs/super_types.h
@@ -352,5 +352,6 @@ struct super_block {
#define SB_I_NOIDMAP 0x00002000 /* No idmapped mounts on this superblock */
#define SB_I_ALLOW_HSM 0x00004000 /* Allow HSM events on this superblock */
#define SB_I_NO_DATA_INTEGRITY 0x00008000 /* fs cannot guarantee data persistence on sync */
+#define SB_I_NO_ATOMIC_MKDIR 0x00010000 /* no O_CREAT|O_DIRECTORY on this fs */
#endif /* _LINUX_FS_SUPER_TYPES_H */
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index 73786b567ccd8..2a7baa6a56bc0 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -241,13 +241,14 @@ struct kernfs_node {
* syscalls. These optional callbacks are invoked on the matching syscalls
* and can perform any kernfs operations which don't necessarily have to be
* the exact operation requested. An active reference is held for each
- * kernfs_node parameter.
+ * kernfs_node parameter. @mkdir returns the kernfs_node of the new
+ * directory with a reference held, or an ERR_PTR() on failure.
*/
struct kernfs_syscall_ops {
int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
- int (*mkdir)(struct kernfs_node *parent, const char *name,
- umode_t mode);
+ struct kernfs_node *(*mkdir)(struct kernfs_node *parent,
+ const char *name, umode_t mode);
int (*rmdir)(struct kernfs_node *kn);
int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
const char *new_name);
diff --git a/include/uapi/asm-generic/errno.h b/include/uapi/asm-generic/errno.h
index bd78e69e0a43f..c84ebf89c8b6e 100644
--- a/include/uapi/asm-generic/errno.h
+++ b/include/uapi/asm-generic/errno.h
@@ -76,7 +76,7 @@
#define ENOPROTOOPT 92 /* Protocol not available */
#define EPROTONOSUPPORT 93 /* Protocol not supported */
#define ESOCKTNOSUPPORT 94 /* Socket type not supported */
-#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */
+#define EOPNOTSUPP 95 /* Operation not supported */
#define EPFNOSUPPORT 96 /* Protocol family not supported */
#define EAFNOSUPPORT 97 /* Address family not supported by protocol */
#define EADDRINUSE 98 /* Address already in use */
diff --git a/kernel/cgroup/cgroup-internal.h b/kernel/cgroup/cgroup-internal.h
index 233fb8b45d2c3..5d05fd75d0c04 100644
--- a/kernel/cgroup/cgroup-internal.h
+++ b/kernel/cgroup/cgroup-internal.h
@@ -256,7 +256,8 @@ void cgroup_procs_write_finish(struct task_struct *task,
int cgroup_lock_and_drain_offline(struct cgroup *cgrp);
-int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode);
+struct kernfs_node *cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
+ umode_t mode);
int cgroup_rmdir(struct kernfs_node *kn);
int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
struct kernfs_root *kf_root);
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 14634246a666d..a52d754478320 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -6149,18 +6149,20 @@ static bool cgroup_check_hierarchy_limits(struct cgroup *parent)
return ret;
}
-int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
+struct kernfs_node *cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
+ umode_t mode)
{
struct cgroup *parent, *cgrp;
+ struct kernfs_node *kn;
int ret;
/* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
if (strchr(name, '\n'))
- return -EINVAL;
+ return ERR_PTR(-EINVAL);
parent = cgroup_kn_lock_live(parent_kn, false);
if (IS_ERR(parent))
- return PTR_ERR(parent);
+ return ERR_CAST(parent);
if (!cgroup_check_hierarchy_limits(parent)) {
ret = -EAGAIN;
@@ -6192,14 +6194,16 @@ int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
/* let's create and online css's */
kernfs_activate(cgrp->kn);
- ret = 0;
- goto out_unlock;
+ kn = cgrp->kn;
+ kernfs_get(kn);
+ cgroup_kn_unlock(parent_kn);
+ return kn;
out_destroy:
cgroup_destroy_locked(cgrp);
out_unlock:
cgroup_kn_unlock(parent_kn);
- return ret;
+ return ERR_PTR(ret);
}
/*
diff --git a/security/apparmor/apparmorfs.c b/security/apparmor/apparmorfs.c
index d1386722537d0..36c9bb7ebf3d8 100644
--- a/security/apparmor/apparmorfs.c
+++ b/security/apparmor/apparmorfs.c
@@ -248,6 +248,7 @@ static int apparmorfs_fill_super(struct super_block *sb, struct fs_context *fc)
if (error)
return error;
sb->s_op = &aafs_super_ops;
+ sb->s_iflags |= SB_I_NO_ATOMIC_MKDIR;
return 0;
}
diff --git a/tools/testing/selftests/filesystems/.gitignore b/tools/testing/selftests/filesystems/.gitignore
index cf79000e4092a..8ccf7a5f9ec9d 100644
--- a/tools/testing/selftests/filesystems/.gitignore
+++ b/tools/testing/selftests/filesystems/.gitignore
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0-only
+open_o_creat_o_dir
dnotify_test
devpts_pts
fclog
diff --git a/tools/testing/selftests/filesystems/Makefile b/tools/testing/selftests/filesystems/Makefile
index fbd5c27505ef5..004b946fd2e0b 100644
--- a/tools/testing/selftests/filesystems/Makefile
+++ b/tools/testing/selftests/filesystems/Makefile
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: GPL-2.0
CFLAGS += $(KHDR_INCLUDES)
-TEST_GEN_PROGS := devpts_pts anon_inode_test kernfs_test fclog ustat_test rw_hint_test
+TEST_GEN_PROGS := devpts_pts anon_inode_test kernfs_test fclog ustat_test rw_hint_test open_o_creat_o_dir
TEST_GEN_PROGS += idmapped_tmpfile
TEST_GEN_PROGS_EXTENDED := dnotify_test
diff --git a/tools/testing/selftests/filesystems/open_o_creat_o_dir.c b/tools/testing/selftests/filesystems/open_o_creat_o_dir.c
new file mode 100644
index 0000000000000..93c0f4dea266a
--- /dev/null
+++ b/tools/testing/selftests/filesystems/open_o_creat_o_dir.c
@@ -0,0 +1,346 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <sys/stat.h>
+#include <sys/vfs.h>
+#include <errno.h>
+#include <limits.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <linux/magic.h>
+
+#include "kselftest_harness.h"
+#include "wrappers.h"
+
+#define openat_o_mkdir_checked_flags(dfd, pathname, flags) ({ \
+ struct stat __st; \
+ int __fd = openat_o_mkdir(dfd, pathname, flags, S_IRWXU); \
+ ASSERT_GE(__fd, 0); \
+ ASSERT_EQ(fstat(__fd, &__st), 0); \
+ EXPECT_TRUE(S_ISDIR(__st.st_mode)); \
+ __fd; \
+})
+
+#define openat_o_mkdir_checked(dfd, pathname) \
+ openat_o_mkdir_checked_flags(dfd, pathname, O_RDONLY)
+
+FIXTURE(open_o_creat_o_dir) {
+ char dirpath[PATH_MAX];
+ int dfd;
+};
+
+FIXTURE_SETUP(open_o_creat_o_dir)
+{
+ strcpy(self->dirpath, "/tmp/open_o_creat_o_dir_test.XXXXXX");
+ ASSERT_NE(mkdtemp(self->dirpath), NULL);
+ self->dfd = open(self->dirpath, O_DIRECTORY);
+ ASSERT_GE(self->dfd, 0);
+}
+
+FIXTURE_TEARDOWN(open_o_creat_o_dir)
+{
+ close(self->dfd);
+ rmdir(self->dirpath);
+}
+
+/* Does open_o_creat_o_dir return a fd at all? */
+TEST_F(open_o_creat_o_dir, returns_fd)
+{
+ int fd = openat_o_mkdir_checked(self->dfd, "newdir");
+ EXPECT_EQ(close(fd), 0);
+ EXPECT_EQ(unlinkat(self->dfd, "newdir", AT_REMOVEDIR), 0);
+}
+
+/* The fd must refer to the directory that was just created. */
+TEST_F(open_o_creat_o_dir, fd_is_created_dir)
+{
+ int fd;
+ struct stat st_via_fd, st_via_path;
+ char path[PATH_MAX];
+
+ fd = openat_o_mkdir_checked(self->dfd, "checkdir");
+
+ ASSERT_EQ(fstat(fd, &st_via_fd), 0);
+
+ snprintf(path, sizeof(path), "%s/checkdir", self->dirpath);
+ ASSERT_EQ(stat(path, &st_via_path), 0);
+
+ EXPECT_EQ(st_via_fd.st_ino, st_via_path.st_ino);
+ EXPECT_EQ(st_via_fd.st_dev, st_via_path.st_dev);
+
+ EXPECT_EQ(close(fd), 0);
+ EXPECT_EQ(rmdir(path), 0);
+}
+
+/* Missing parent component must fail with ENOENT. */
+TEST_F(open_o_creat_o_dir, enoent_missing_parent)
+{
+ EXPECT_EQ(openat_o_mkdir(self->dfd, "nonexistent/child", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, ENOENT);
+}
+
+/* An invalid dfd must fail with EBADF. */
+TEST_F(open_o_creat_o_dir, ebadf)
+{
+ EXPECT_EQ(openat_o_mkdir(FD_INVALID, "badfdir", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, EBADF);
+}
+
+/* A dfd that points to a file (not a directory) must fail with ENOTDIR. */
+TEST_F(open_o_creat_o_dir, enotdir_dfd)
+{
+ int file_fd;
+
+ file_fd = openat(self->dfd, "file",
+ O_CREAT | O_RDONLY, S_IRWXU);
+ ASSERT_GE(file_fd, 0);
+
+ EXPECT_EQ(openat_o_mkdir(file_fd, "subdir", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, ENOTDIR);
+
+ EXPECT_EQ(close(file_fd), 0);
+ EXPECT_EQ(unlinkat(self->dfd, "file", 0), 0);
+}
+
+/*
+ * O_CREAT|O_DIRECTORY without O_EXCL must fail with EINVAL and must not
+ * create anything at the path.
+ */
+TEST_F(open_o_creat_o_dir, requires_o_excl)
+{
+ struct stat st;
+
+ EXPECT_EQ(openat(self->dfd, "noexcl", O_CREAT | O_DIRECTORY | O_RDONLY,
+ S_IRWXU), -1);
+ EXPECT_EQ(errno, EINVAL);
+
+ EXPECT_EQ(fstatat(self->dfd, "noexcl", &st, 0), -1);
+ EXPECT_EQ(errno, ENOENT);
+}
+
+/*
+ * Once the directory exists, repeating O_CREAT|O_DIRECTORY|O_EXCL must
+ * fail with EEXIST.
+ */
+TEST_F(open_o_creat_o_dir, existing_dir_eexist)
+{
+ int excldir_fd;
+
+ excldir_fd = openat_o_mkdir_checked(self->dfd, "excldir");
+
+ EXPECT_EQ(openat_o_mkdir(self->dfd, "excldir", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, EEXIST);
+
+ EXPECT_EQ(close(excldir_fd), 0);
+ EXPECT_EQ(unlinkat(self->dfd, "excldir", AT_REMOVEDIR), 0);
+}
+
+/*
+ * O_CREAT|O_DIRECTORY combined with a writable access mode must be
+ * rejected: a directory cannot be opened for writing.
+ */
+TEST_F(open_o_creat_o_dir, rejects_writable_acc_mode)
+{
+ EXPECT_EQ(openat_o_mkdir(self->dfd, "rdwrdir", O_RDWR, S_IRWXU), -1);
+ EXPECT_EQ(errno, ENOTDIR);
+}
+
+/*
+ * openat(O_CREAT|O_DIRECTORY) with a trailing slash should work.
+ */
+TEST_F(open_o_creat_o_dir, trailing_slash)
+{
+ int fd = openat_o_mkdir_checked(self->dfd, "newdir/");
+ EXPECT_EQ(close(fd), 0);
+ EXPECT_EQ(unlinkat(self->dfd, "newdir", AT_REMOVEDIR), 0);
+}
+
+/*
+ * openat(O_CREAT) with a trailing slash but without O_DIRECTORY
+ * must fail with EISDIR and must not create anything at the path.
+ */
+TEST_F(open_o_creat_o_dir, trailing_slash_no_o_dir)
+{
+ int fd;
+ struct stat st;
+
+ fd = openat(self->dfd, "trailing/", O_CREAT | O_RDONLY, S_IRWXU);
+ EXPECT_EQ(fd, -1);
+ EXPECT_EQ(errno, EISDIR);
+
+ EXPECT_EQ(fstatat(self->dfd, "trailing", &st, 0), -1);
+ EXPECT_EQ(errno, ENOENT);
+}
+
+/*
+ * The returned fd must be usable as a dfd for further *at() calls.
+ */
+TEST_F(open_o_creat_o_dir, fd_usable_as_dfd)
+{
+ int parent_fd, child_fd;
+ char path[PATH_MAX];
+
+ parent_fd = openat_o_mkdir_checked(self->dfd, "parent");
+ child_fd = openat_o_mkdir_checked(parent_fd, "child");
+
+ EXPECT_EQ(close(child_fd), 0);
+ EXPECT_EQ(close(parent_fd), 0);
+
+ snprintf(path, sizeof(path), "%s/parent/child", self->dirpath);
+ EXPECT_EQ(rmdir(path), 0);
+ snprintf(path, sizeof(path), "%s/parent", self->dirpath);
+ EXPECT_EQ(rmdir(path), 0);
+}
+
+/*
+ * O_EXCL means a trailing symlink is never followed, so O_CREAT|O_DIRECTORY
+ * must refuse to create through a dangling one, and must not create
+ * anything at the symlink target.
+ */
+TEST_F(open_o_creat_o_dir, dangling_symlink_eexist)
+{
+ struct stat st;
+
+ ASSERT_EQ(symlinkat("danglink_target", self->dfd, "danglink"), 0);
+
+ EXPECT_EQ(openat_o_mkdir(self->dfd, "danglink", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, EEXIST);
+
+ /* Nothing must have been created at the target. */
+ EXPECT_EQ(fstatat(self->dfd, "danglink_target", &st, 0), -1);
+ EXPECT_EQ(errno, ENOENT);
+
+ EXPECT_EQ(unlinkat(self->dfd, "danglink", 0), 0);
+}
+
+/*
+ * A trailing symlink is an existing entry, even if it resolves to a
+ * directory, so O_CREAT|O_DIRECTORY must fail with EEXIST.
+ */
+TEST_F(open_o_creat_o_dir, symlink_to_dir_eexist)
+{
+ ASSERT_EQ(mkdirat(self->dfd, "realdir", 0700), 0);
+ ASSERT_EQ(symlinkat("realdir", self->dfd, "dirlink"), 0);
+
+ EXPECT_EQ(openat_o_mkdir(self->dfd, "dirlink", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, EEXIST);
+
+ EXPECT_EQ(unlinkat(self->dfd, "dirlink", 0), 0);
+ EXPECT_EQ(unlinkat(self->dfd, "realdir", AT_REMOVEDIR), 0);
+}
+
+/*
+ * O_TMPFILE is encoded as __O_TMPFILE|O_DIRECTORY. Now that O_CREAT is no
+ * longer rejected alongside O_DIRECTORY, O_TMPFILE|O_CREAT must still be
+ * rejected explicitly so that it cannot create a persistent file on kernels
+ * that predate O_TMPFILE.
+ */
+TEST_F(open_o_creat_o_dir, tmpfile_with_o_creat_einval)
+{
+ EXPECT_EQ(openat(self->dfd, ".", O_TMPFILE | O_CREAT | O_RDWR, S_IRWXU),
+ -1);
+ EXPECT_EQ(errno, EINVAL);
+}
+
+/*
+ * Open the first of @paths that is a mounted filesystem of type @magic.
+ * Returns -1 if there is none.
+ */
+static int open_fs_root(const char *const *paths, long magic)
+{
+ for (; *paths; paths++) {
+ struct statfs sfs;
+ int fd;
+
+ fd = open(*paths, O_DIRECTORY | O_RDONLY);
+ if (fd < 0)
+ continue;
+ if (!fstatfs(fd, &sfs) && sfs.f_type == magic)
+ return fd;
+ close(fd);
+ }
+ return -1;
+}
+
+/*
+ * kernfs: O_CREAT|O_DIRECTORY on cgroup2 must create a cgroup and return a
+ * fd to that very cgroup.
+ */
+TEST(kernfs_cgroup2)
+{
+ static const char *const paths[] = {
+ "/sys/fs/cgroup", "/sys/fs/cgroup/unified", NULL
+ };
+ struct stat st_fd, st_path;
+ int root_fd, fd, procs_fd;
+ char name[64];
+
+ if (geteuid() != 0)
+ SKIP(return, "needs root to create a cgroup");
+ root_fd = open_fs_root(paths, CGROUP2_SUPER_MAGIC);
+ if (root_fd < 0)
+ SKIP(return, "cgroup2 is not mounted");
+
+ snprintf(name, sizeof(name), "open_o_creat_o_dir.%d", getpid());
+
+ fd = openat_o_mkdir_checked(root_fd, name);
+
+ /* The fd refers to the cgroup that is now visible at the name. */
+ ASSERT_EQ(fstat(fd, &st_fd), 0);
+ ASSERT_EQ(fstatat(root_fd, name, &st_path, 0), 0);
+ EXPECT_EQ(st_fd.st_ino, st_path.st_ino);
+ EXPECT_EQ(st_fd.st_dev, st_path.st_dev);
+
+ /* It is a populated cgroup, not just an empty directory. */
+ procs_fd = openat(fd, "cgroup.procs", O_RDONLY);
+ EXPECT_GE(procs_fd, 0);
+ if (procs_fd >= 0)
+ EXPECT_EQ(close(procs_fd), 0);
+
+ EXPECT_EQ(close(fd), 0);
+ EXPECT_EQ(unlinkat(root_fd, name, AT_REMOVEDIR), 0);
+ EXPECT_EQ(close(root_fd), 0);
+}
+
+/*
+ * tracefs sets SB_I_NO_ATOMIC_MKDIR: O_CREAT|O_DIRECTORY is refused with
+ * EOPNOTSUPP before any lookup, whether or not the name exists, and
+ * creates nothing.
+ */
+TEST(tracefs_eopnotsupp)
+{
+ static const char *const paths[] = {
+ "/sys/kernel/tracing", "/sys/kernel/debug/tracing", NULL
+ };
+ int tracing_fd, inst_fd;
+ char name[64];
+ struct stat st;
+
+ if (geteuid() != 0)
+ SKIP(return, "needs root to access tracefs");
+ tracing_fd = open_fs_root(paths, TRACEFS_MAGIC);
+ if (tracing_fd < 0)
+ SKIP(return, "tracefs is not mounted");
+ inst_fd = openat(tracing_fd, "instances", O_DIRECTORY | O_RDONLY);
+ if (inst_fd < 0) {
+ close(tracing_fd);
+ SKIP(return, "no tracefs instances directory");
+ }
+
+ snprintf(name, sizeof(name), "open_o_creat_o_dir.%d", getpid());
+
+ /* Nonexistent name, where mkdir(2) would create an instance */
+ EXPECT_EQ(openat_o_mkdir(inst_fd, name, O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, EOPNOTSUPP);
+ EXPECT_EQ(fstatat(inst_fd, name, &st, 0), -1);
+ EXPECT_EQ(errno, ENOENT);
+
+ /* Existing name */
+ EXPECT_EQ(openat_o_mkdir(tracing_fd, "instances", O_RDONLY, S_IRWXU), -1);
+ EXPECT_EQ(errno, EOPNOTSUPP);
+
+ EXPECT_EQ(close(inst_fd), 0);
+ EXPECT_EQ(close(tracing_fd), 0);
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/filesystems/wrappers.h b/tools/testing/selftests/filesystems/wrappers.h
index 420ae4f908cf2..bc58bdb800631 100644
--- a/tools/testing/selftests/filesystems/wrappers.h
+++ b/tools/testing/selftests/filesystems/wrappers.h
@@ -13,6 +13,10 @@
#define STATX_MNT_ID_UNIQUE 0x00004000U /* Want/got extended stx_mount_id */
#endif
+#ifndef FD_INVALID
+#define FD_INVALID -10009
+#endif
+
static inline int sys_fsopen(const char *fsname, unsigned int flags)
{
return syscall(__NR_fsopen, fsname, flags);
@@ -105,4 +109,11 @@ static inline int sys_open_tree(int dfd, const char *filename, unsigned int flag
return syscall(__NR_open_tree, dfd, filename, flags);
}
+static inline int openat_o_mkdir(int dfd, const char *pathname,
+ unsigned int flags, mode_t mode)
+{
+ return syscall(__NR_openat, dfd, pathname,
+ flags | O_DIRECTORY | O_CREAT | O_EXCL, mode);
+}
+
#endif
|
| 29/3 | 2026/10/24 22:29 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 23409 output: 36 thoughts: 2061 Thoughts: **KMSAN Fuzzing Session Assessment for Atomic Directory Creation Patch** Okay, let's break this down. The objective is to determine if this patch series, which introduces atomic directory creation using `O_CREAT | O_DIRECTORY | O_EXCL` and related support, necessitates a KMSAN fuzzing session. I need to apply my knowledge and the provided rules to make a reasoned judgment. First, I remind myself of the KMSAN rules: avoid KMSAN if existing tools will catch the bug, only engage if there are risks of uninitialized memory usage, info leaks, or interactions with kernel structures that are exposed to userspace. Standard sanitizers and KASAN should handle the rest. I see that this patch series modifies file system and VFS code to implement the new atomic directory creation. Now, let's step through the patch and look for any of those KMSAN-worthy risks. The changes look pretty targeted, so I'll go section by section. The first change appears to be a comment, so that's nothing. Then there are some changes to add `SB_I_NO_ATOMIC_MKDIR;` to various filesystems. This means a flag is set so other file systems can control whether or not this behavior is supported. This looks safe. Next I review `kernfs_iop_mkdir`, `kernfs_get_inode_locked`, and `kernfs_init_inode` which seems like the core logic. `new_inode` allocates an inode. It looks like `inode` and `kn` are initialized locally. `new_inode` is a VFS allocation function, so I'll verify the init function just in case. I'll read through `kernfs_init_inode` to check how it's used. Based on these files, this is not a KMSAN-relevant area. I see that there are helpers created to handle trailing slashes and permissions. I read through the helper functions and consider whether uninitialized memory or info leaks are possible. I see there are a lot of checks. These are low level calls. Again, this looks safe. Then, there is `build_open_flags`. It looks like flags will set the mode appropriately. I will need to verify that is safe. Next, I see a return type change in cgroup and resctrl code. It looks like it is changing return types to `struct kernfs_node *` which looks fine. It looks like `kernfs_node` is a common abstraction. It looks like I need to verify how `new_inode` and `kernfs_init_inode` are used in VFS, which I can read using a tool. I need to make sure the inode is initialized properly after the allocation, as that's my main area of concern. In summary, based on my analysis, I haven't spotted any immediate red flags for uninitialized memory reads or information leaks that would warrant a dedicated KMSAN session. The patch seems to carefully initialize data structures and handle flags, and it's mostly refactoring and adding flags, not introducing new complex data structures with unclear initialization paths. I think standard sanitizers and testing should be sufficient here. |
| 30/3 | 2026/10/24 22:29 | tool | read-file |
0mArgs:{
"File": "fs/kernfs/inode.c",
"FirstLine": 200,
"LineCount": 60
}Results: {
"Contents": " 200:\t\tkernfs_get(kn);\n 201:\t\tinode-\u003ei_private = kn;\n 202:\t\tinode-\u003ei_mapping-\u003ea_ops = \u0026ram_aops;\n 203:\t\tinode-\u003ei_op = \u0026kernfs_iops;\n 204:\t\tinode-\u003ei_generation = kernfs_gen(kn);\n 205:\t\n 206:\t\tset_default_inode_attr(inode, kn-\u003emode);\n 207:\t\tscoped_guard(rwsem_read, \u0026kernfs_root(kn)-\u003ekernfs_iattr_rwsem)\n 208:\t\t\tkernfs_refresh_inode(kn, inode);\n 209:\t\n 210:\t\t/* initialize inode according to type */\n 211:\t\tswitch (kernfs_type(kn)) {\n 212:\t\tcase KERNFS_DIR:\n 213:\t\t\tinode-\u003ei_op = \u0026kernfs_dir_iops;\n 214:\t\t\tinode-\u003ei_fop = \u0026kernfs_dir_fops;\n 215:\t\t\tif (kn-\u003eflags \u0026 KERNFS_EMPTY_DIR)\n 216:\t\t\t\tmake_empty_dir_inode(inode);\n 217:\t\t\tbreak;\n 218:\t\tcase KERNFS_FILE:\n 219:\t\t\tinode-\u003ei_size = kn-\u003eattr.size;\n 220:\t\t\tinode-\u003ei_fop = \u0026kernfs_file_fops;\n 221:\t\t\tbreak;\n 222:\t\tcase KERNFS_LINK:\n 223:\t\t\tinode-\u003ei_op = \u0026kernfs_symlink_iops;\n 224:\t\t\tbreak;\n 225:\t\tdefault:\n 226:\t\t\tBUG();\n 227:\t\t}\n 228:\t\n 229:\t\tunlock_new_inode(inode);\n 230:\t}\n 231:\t\n 232:\t/**\n 233:\t *\tkernfs_get_inode - get inode for kernfs_node\n 234:\t *\t@sb: super block\n 235:\t *\t@kn: kernfs_node to allocate inode for\n 236:\t *\n 237:\t *\tGet inode for @kn. If such inode doesn't exist, a new inode is\n 238:\t *\tallocated and basics are initialized. New inode is returned\n 239:\t *\tlocked.\n 240:\t *\n 241:\t *\tLocking:\n 242:\t *\tKernel thread context (may sleep).\n 243:\t *\n 244:\t *\tReturn:\n 245:\t *\tPointer to allocated inode on success, %NULL on failure.\n 246:\t */\n 247:\tstruct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)\n 248:\t{\n 249:\t\tstruct inode *inode;\n 250:\t\n 251:\t\tinode = iget_locked(sb, kernfs_ino(kn));\n 252:\t\tif (inode \u0026\u0026 (inode_state_read_once(inode) \u0026 I_NEW))\n 253:\t\t\tkernfs_init_inode(kn, inode);\n 254:\t\n 255:\t\treturn inode;\n 256:\t}\n 257:\t\n 258:\tstatic int kernfs_inode_test(struct inode *inode, void *data)\n 259:\t{\n"
} |
| 31/3 | 2026/10/24 22:29 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 27087 output: 31 thoughts: 242 Thoughts: **My Analysis of Inode Initialization and File Operations** Okay, so here's what's running through my head as I dissect this code: First, I see `new_inode` handling inode initialization. It goes through `alloc_inode` and then `inode_init_always`. Makes sense, standard initialization. Then, the insertion happens with `inode_insert5`. If it's a new inode, `kernfs_init_inode` is called to set up the kernfs-specific stuff, followed by `unlock_new_inode`. If we aren't dealing with a *new* inode, a cleanup happens with `iput(new)`. Standard stuff, though I'd like to trace how `new_inode` is *actually* new. Now, let's switch gears and check out `lookup_open` and the broader `openat` functions, and specifically, let's make sure there aren't any sneaky uninitialized variables hiding in `fs/namei.c`. My eyes are immediately drawn to `lookup_open`. I'm seeing `bool create_dir = O_IS_MKDIR(op->open_flag);` - nice, we're handling directory creation correctly. Then, there's `struct dentry *dentry;`, `int error, create_error;` - all seem properly declared, and the important mode is taken directly from the caller, or function param `umode_t mode = op->mode;`. No immediate red flags here. I'm also seeing `delegated_inode`, which is being initialized to NULL in `lookup_open` to an empty structure. Good, better safe than sorry, and it's initialized. I should double-check the specific implementation there, though. Overall, so far, the initialization and open path looks pretty clean. But I'll need to dig deeper in the code to make sure nothing is missed! |
| 32/3 | 2026/10/24 22:29 | tool | grepper |
0mArgs:{
"Expression": "delegated_inode",
"PathPrefixes": [
"fs/namei.c"
]
}Results: {
"Output": "\nFull output is too long, showing 200 out of 268 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (1 files in total):\nfs/namei.c:65\n\nfs/namei.c=4185=int vfs_create_no_perm(const struct mnt_idmap *idmap, struct dentry *dentry,\nfs/namei.c:4186:\t\t umode_t mode, struct delegated_inode *di)\nfs/namei.c-4187-{\n--\nfs/namei.c=4217=int vfs_create(const struct mnt_idmap *idmap, struct dentry *dentry, umode_t mode,\nfs/namei.c:4218:\t struct delegated_inode *di)\nfs/namei.c-4219-{\n--\nfs/namei.c=4487=struct dentry *vfs_mkdir_no_perm(const struct mnt_idmap *, struct inode *, struct dentry *,\nfs/namei.c:4488:\t\t\t\t umode_t, struct delegated_inode *);\nfs/namei.c-4489-/*\n--\nfs/namei.c=4503=static struct dentry *lookup_open(struct nameidata *nd, struct file *file,\n--\nfs/namei.c-4505-{\nfs/namei.c:4506:\tstruct delegated_inode delegated_inode = { };\nfs/namei.c-4507-\tconst struct mnt_idmap *idmap;\n--\nfs/namei.c-4634-\t\tstruct dentry *res = vfs_mkdir_no_perm(idmap, dir_inode, dentry, mode,\nfs/namei.c:4635:\t\t\t\t\t\t \u0026delegated_inode);\nfs/namei.c-4636-\t\terror = PTR_ERR_OR_ZERO(res);\n--\nfs/namei.c-4646-\t} else {\nfs/namei.c:4647:\t\terror = vfs_create_no_perm(idmap, dentry, mode, \u0026delegated_inode);\nfs/namei.c-4648-\t}\n--\nfs/namei.c-4661-\nfs/namei.c:4662:\tif (is_delegated(\u0026delegated_inode)) {\nfs/namei.c-4663-\t\t/* Must have come through out_dput: dentry is an ERR_PTR() */\nfs/namei.c:4664:\t\terror = break_deleg_wait(\u0026delegated_inode);\nfs/namei.c-4665-\n--\nfs/namei.c=5300=EXPORT_SYMBOL(dentry_create);\n--\nfs/namei.c-5308- * @dev:\t\tdevice number of device to create\nfs/namei.c:5309: * @delegated_inode:\treturns parent inode, if the inode is delegated.\nfs/namei.c-5310- *\n--\nfs/namei.c=5319=int vfs_mknod(const struct mnt_idmap *idmap, struct inode *dir,\nfs/namei.c-5320-\t struct dentry *dentry, umode_t mode, dev_t dev,\nfs/namei.c:5321:\t struct delegated_inode *delegated_inode)\nfs/namei.c-5322-{\n--\nfs/namei.c-5344-\nfs/namei.c:5345:\terror = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, delegated_inode);\nfs/namei.c-5346-\tif (error)\n--\nfs/namei.c=5373=int filename_mknodat(int dfd, struct filename *name, umode_t mode,\n--\nfs/namei.c-5375-{\nfs/namei.c:5376:\tstruct delegated_inode di = { };\nfs/namei.c-5377-\tconst struct mnt_idmap *idmap;\n--\nfs/namei.c=5440=struct dentry *vfs_mkdir_no_perm(const struct mnt_idmap *idmap, struct inode *dir,\nfs/namei.c-5441-\t\t\t\t struct dentry *dentry, umode_t mode,\nfs/namei.c:5442:\t\t\t\t struct delegated_inode *di)\nfs/namei.c-5443-{\n--\nfs/namei.c-5471- * @mode:\t\tmode of the child directory\nfs/namei.c:5472: * @delegated_inode:\treturns parent inode, if the inode is delegated.\nfs/namei.c-5473- *\n--\nfs/namei.c=5488=struct dentry *vfs_mkdir(const struct mnt_idmap *idmap, struct inode *dir,\nfs/namei.c-5489-\t\t\t struct dentry *dentry, umode_t mode,\nfs/namei.c:5490:\t\t\t struct delegated_inode *delegated_inode)\nfs/namei.c-5491-{\n--\nfs/namei.c-5507-\nfs/namei.c:5508:\tde = vfs_mkdir_no_perm(idmap, dir, dentry, mode, delegated_inode);\nfs/namei.c-5509-\tif (IS_ERR(de)) {\n--\nfs/namei.c=5521=int filename_mkdirat(int dfd, struct filename *name, umode_t mode)\n--\nfs/namei.c-5526-\tunsigned int lookup_flags = LOOKUP_DIRECTORY;\nfs/namei.c:5527:\tstruct delegated_inode delegated_inode = { };\nfs/namei.c-5528-\n--\nfs/namei.c-5537-\t\tdentry = vfs_mkdir(mnt_idmap(path.mnt), path.dentry-\u003ed_inode,\nfs/namei.c:5538:\t\t\t\t dentry, mode, \u0026delegated_inode);\nfs/namei.c-5539-\t\tif (IS_ERR(dentry))\n--\nfs/namei.c-5542-\tend_creating_path(\u0026path, dentry);\nfs/namei.c:5543:\tif (is_delegated(\u0026delegated_inode)) {\nfs/namei.c:5544:\t\terror = break_deleg_wait(\u0026delegated_inode);\nfs/namei.c-5545-\t\tif (!error)\n--\nfs/namei.c=5561=SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)\n--\nfs/namei.c-5571- * @dentry:\t\tdentry of the child directory\nfs/namei.c:5572: * @delegated_inode:\treturns parent inode, if it's delegated.\nfs/namei.c-5573- *\n--\nfs/namei.c=5582=int vfs_rmdir(const struct mnt_idmap *idmap, struct inode *dir,\nfs/namei.c:5583:\t struct dentry *dentry, struct delegated_inode *delegated_inode)\nfs/namei.c-5584-{\n--\nfs/namei.c-5604-\nfs/namei.c:5605:\terror = try_break_deleg(dir, LEASE_BREAK_DIR_DELETE, delegated_inode);\nfs/namei.c-5606-\tif (error)\n--\nfs/namei.c=5627=int filename_rmdir(int dfd, struct filename *name)\n--\nfs/namei.c-5634-\tunsigned int lookup_flags = 0;\nfs/namei.c:5635:\tstruct delegated_inode delegated_inode = { };\nfs/namei.c-5636-retry:\n--\nfs/namei.c-5666-\terror = vfs_rmdir(mnt_idmap(path.mnt), path.dentry-\u003ed_inode,\nfs/namei.c:5667:\t\t\t dentry, \u0026delegated_inode);\nfs/namei.c-5668-exit4:\n--\nfs/namei.c-5673-\tpath_put(\u0026path);\nfs/namei.c:5674:\tif (is_delegated(\u0026delegated_inode)) {\nfs/namei.c:5675:\t\terror = break_deleg_wait(\u0026delegated_inode);\nfs/namei.c-5676-\t\tif (!error)\n--\nfs/namei.c=5686=SYSCALL_DEFINE1(rmdir, const char __user *, pathname)\n--\nfs/namei.c-5696- * @dentry:\tvictim\nfs/namei.c:5697: * @delegated_inode: returns victim inode, if the inode is delegated.\nfs/namei.c-5698- *\n--\nfs/namei.c-5701- * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and\nfs/namei.c:5702: * return a reference to the inode in delegated_inode. The caller\nfs/namei.c-5703- * should then break the delegation on that inode and retry. Because\n--\nfs/namei.c-5706- *\nfs/namei.c:5707: * Alternatively, a caller may pass NULL for delegated_inode. This may\nfs/namei.c-5708- * be appropriate for callers that expect the underlying filesystem not\n--\nfs/namei.c=5717=int vfs_unlink(const struct mnt_idmap *idmap, struct inode *dir,\nfs/namei.c:5718:\t struct dentry *dentry, struct delegated_inode *delegated_inode)\nfs/namei.c-5719-{\n--\nfs/namei.c-5736-\t\tif (!error) {\nfs/namei.c:5737:\t\t\terror = try_break_deleg(dir, LEASE_BREAK_DIR_DELETE, delegated_inode);\nfs/namei.c-5738-\t\t\tif (error)\nfs/namei.c-5739-\t\t\t\tgoto out;\nfs/namei.c:5740:\t\t\terror = try_break_deleg(target, 0, delegated_inode);\nfs/namei.c-5741-\t\t\tif (error)\n--\nfs/namei.c=5771=int filename_unlinkat(int dfd, struct filename *name)\n--\nfs/namei.c-5778-\tstruct inode *inode;\nfs/namei.c:5779:\tstruct delegated_inode delegated_inode = { };\nfs/namei.c-5780-\tunsigned int lookup_flags = 0;\n--\nfs/namei.c-5813-\terror = vfs_unlink(mnt_idmap(path.mnt), path.dentry-\u003ed_inode,\nfs/namei.c:5814:\t\t\t dentry, \u0026delegated_inode);\nfs/namei.c-5815-exit_end_dirop:\n--\nfs/namei.c-5817-\tiput(inode);\t/* truncate the inode here */\nfs/namei.c:5818:\tif (is_delegated(\u0026delegated_inode)) {\nfs/namei.c:5819:\t\terror = break_deleg_wait(\u0026delegated_inode);\nfs/namei.c-5820-\t\tif (!error)\n--\nfs/namei.c=5845=SYSCALL_DEFINE1(unlink, const char __user *, pathname)\n--\nfs/namei.c-5856- * @oldname:\tname of the file to link to\nfs/namei.c:5857: * @delegated_inode: returns victim inode, if the inode is delegated.\nfs/namei.c-5858- *\n--\nfs/namei.c=5867=int vfs_symlink(const struct mnt_idmap *idmap, struct inode *dir,\nfs/namei.c-5868-\t\tstruct dentry *dentry, const char *oldname,\nfs/namei.c:5869:\t\tstruct delegated_inode *delegated_inode)\nfs/namei.c-5870-{\n--\nfs/namei.c-5883-\nfs/namei.c:5884:\terror = try_break_deleg(dir, LEASE_BREAK_DIR_CREATE, delegated_inode);\nfs/namei.c-5885-\tif (error)\n--\nfs/namei.c=5895=int filename_symlinkat(struct filename *from, int newdfd, struct filename *to)\n--\nfs/namei.c-5900-\tunsigned int lookup_flags = 0;\nfs/namei.c:5901:\tstruct delegated_inode delegated_inode = { };\nfs/namei.c-5902-\n--\nfs/namei.c-5913-\t\terror = vfs_symlink(mnt_idmap(path.mnt), path.dentry-\u003ed_inode,\nfs/namei.c:5914:\t\t\t\t dentry, from-\u003ename, \u0026delegated_inode);\nfs/namei.c-5915-\tend_creating_path(\u0026path, dentry);\nfs/namei.c:5916:\tif (is_delegated(\u0026delegated_inode)) {\nfs/namei.c:5917:\t\terror = break_deleg_wait(\u0026delegated_inode);\nfs/namei.c-5918-\t\tif (!error)\n--\nfs/namei.c=5936=SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)\n--\nfs/namei.c-5948- * @new_dentry:\twhere to create the new link\nfs/namei.c:5949: * @delegated_inode: returns inode needing a delegation break\nfs/namei.c-5950- *\n--\nfs/namei.c-5954- * of breaking, it will return -EWOULDBLOCK and return a reference to the\nfs/namei.c:5955: * inode in delegated_inode. The caller should then break the delegation\nfs/namei.c-5956- * and retry. Because breaking a delegation may take a long time, the\n--\nfs/namei.c-5958- *\nfs/namei.c:5959: * Alternatively, a caller may pass NULL for delegated_inode. This may\nfs/namei.c-5960- * be appropriate for callers that expect the underlying filesystem not\n--\nfs/namei.c=5969=int vfs_link(struct dentry *old_dentry, const struct mnt_idmap *idmap,\nfs/namei.c-5970-\t struct inode *dir, struct dentry *new_dentry,\nfs/namei.c:5971:\t struct delegated_inode *delegated_inode)\n\n"
} |
| 33/3 | 2026/10/24 22:29 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31679 output: 35 thoughts: 24 Thoughts: Here's a summary of my thought process, as requested: **Diving into `fs/namei.c`** Okay, I need to get into the weeds on this specific issue. I'm going to jump straight to the relevant part of the kernel code. Let's see... I'll focus on lines 4500 through 4520 in `fs/namei.c`. I expect this region to contain the key logic. Time to get my hands dirty. I'm hoping I can identify the relevant function calls and perhaps get a good overview of the state variables that are at play at that point. |
| 34/3 | 2026/10/24 22:30 | tool | read-file |
0mArgs:{
"File": "fs/namei.c",
"FirstLine": 4500,
"LineCount": 40
}Results: {
"Contents": "4500:\t *\n4501:\t * Returns ERR_PTR() on failure.\n4502:\t */\n4503:\tstatic struct dentry *lookup_open(struct nameidata *nd, struct file *file,\n4504:\t\t\t\t\t const struct open_flags *op)\n4505:\t{\n4506:\t\tstruct delegated_inode delegated_inode = { };\n4507:\t\tconst struct mnt_idmap *idmap;\n4508:\t\tstruct dentry *dir = nd-\u003epath.dentry;\n4509:\t\tstruct inode *dir_inode = dir-\u003ed_inode;\n4510:\t\tbool create_dir = O_IS_MKDIR(op-\u003eopen_flag);\n4511:\t\tint open_flag;\n4512:\t\tstruct dentry *dentry;\n4513:\t\tint error, create_error;\n4514:\t\tumode_t mode;\n4515:\t\tbool got_write;\n4516:\t\tunsigned int shared_flag;\n4517:\t\n4518:\tretry:\n4519:\t\topen_flag = op-\u003eopen_flag;\n4520:\t\tgot_write = false;\n4521:\t\tmode = op-\u003emode;\n4522:\t\tcreate_error = 0;\n4523:\t\tshared_flag = (open_flag \u0026 O_CREAT) ? 0 : LOOKUP_SHARED;\n4524:\t\n4525:\t\tif (create_dir \u0026\u0026 (dir_inode-\u003ei_sb-\u003es_iflags \u0026 SB_I_NO_ATOMIC_MKDIR))\n4526:\t\t\treturn ERR_PTR(-EOPNOTSUPP);\n4527:\t\n4528:\t\tif (open_flag \u0026 (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {\n4529:\t\t\tgot_write = !mnt_want_write(nd-\u003epath.mnt);\n4530:\t\t\t/*\n4531:\t\t\t * do _not_ fail yet - we might not need that or fail with\n4532:\t\t\t * a different error; we'll be dropping this one anyway.\n4533:\t\t\t */\n4534:\t\t}\n4535:\t\n4536:\t\tif (shared_flag)\n4537:\t\t\tinode_lock_shared_nested(dir_inode, I_MUTEX_PARENT);\n4538:\t\telse\n4539:\t\t\tinode_lock_nested(dir_inode, I_MUTEX_PARENT);\n"
} |
| 35/3 | 2026/10/24 22:30 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32527 output: 35 thoughts: 24 Thoughts: Okay, here's my take on those lines of code, presented as a first-person internal monologue, assuming I'm an expert in this area: **Digging into `fs/namei.c` - Line 4620-4660** Alright, let's zero in on lines 4620-4660 in `fs/namei.c`. I suspect we're likely in the thick of name resolution, probably dealing with either path traversal, symbolic link handling, or maybe even some permission checks during the lookup process. Knowing this code base, those are the usual suspects around that area. I'm going to quickly skim the surrounding context to get my bearings... ah, yeah, I see the `path_walk` function. That solidifies my initial assumption - we're definitely tracing a file path through the file system structure. Now, let me see... lines 4620-4660... Okay, it looks like we're past the initial checks and we're *inside* the loop. I'm seeing calls to `inode_permission` and `security_inode_permission` – good. Looks like we're doing the critical access control checks at each component of the path, before entering the next inode. That's as it should be. The conditional branches look standard, probably checking the returned result from the permission checks - whether the requested operation (read, write, execute, etc.) is allowed. Hmm, line 4645 seems a little long. I'll need to drill down, but it seems to me we are dealing with a potential issue, something that's not fully defined at this point. Yeah, this is typical, but critical. We're getting right to the heart of how the kernel enforces file system security. Makes me think it could be some of the edge-case handling, possibly related to dealing with specific security modules. I'll have to dig deeper, this is where the interesting logic usually resides. |
| 36/3 | 2026/10/24 22:30 | tool | read-file |
0mArgs:{
"File": "fs/namei.c",
"FirstLine": 4620,
"LineCount": 45
}Results: {
"Contents": "4620:\t\n4621:\t\tif (unlikely(create_error)) {\n4622:\t\t\t/* should have done a create, but we already errored */\n4623:\t\t\terror = create_error;\n4624:\t\t\tgoto out_dput;\n4625:\t\t}\n4626:\t\n4627:\t\tif ((create_dir \u0026\u0026 !dir_inode-\u003ei_op-\u003emkdir)\n4628:\t\t\t|| (!create_dir \u0026\u0026 !dir_inode-\u003ei_op-\u003ecreate)) {\n4629:\t\t\terror = -EOPNOTSUPP;\n4630:\t\t\tgoto out_dput;\n4631:\t\t}\n4632:\t\n4633:\t\tif (create_dir) {\n4634:\t\t\tstruct dentry *res = vfs_mkdir_no_perm(idmap, dir_inode, dentry, mode,\n4635:\t\t\t\t\t\t\t \u0026delegated_inode);\n4636:\t\t\terror = PTR_ERR_OR_ZERO(res);\n4637:\t\t\tif (!error) {\n4638:\t\t\t\tdentry = res;\n4639:\t\t\t\t/*\n4640:\t\t\t\t * Filesystems whose -\u003emkdir() may leave the dentry\n4641:\t\t\t\t * negative must set SB_I_NO_ATOMIC_MKDIR.\n4642:\t\t\t\t */\n4643:\t\t\t\tif (WARN_ON_ONCE(d_is_negative(dentry)))\n4644:\t\t\t\t\terror = -EIO;\n4645:\t\t\t}\n4646:\t\t} else {\n4647:\t\t\terror = vfs_create_no_perm(idmap, dentry, mode, \u0026delegated_inode);\n4648:\t\t}\n4649:\t\tif (error)\n4650:\t\t\tgoto out_dput;\n4651:\t\n4652:\t\tfile-\u003ef_mode |= FMODE_CREATED;\n4653:\tout:\n4654:\t\tif (shared_flag)\n4655:\t\t\tinode_unlock_shared(dir_inode);\n4656:\t\telse\n4657:\t\t\tinode_unlock(dir_inode);\n4658:\t\n4659:\t\tif (got_write)\n4660:\t\t\tmnt_drop_write(nd-\u003epath.mnt);\n4661:\t\n4662:\t\tif (is_delegated(\u0026delegated_inode)) {\n4663:\t\t\t/* Must have come through out_dput: dentry is an ERR_PTR() */\n4664:\t\t\terror = break_deleg_wait(\u0026delegated_inode);\n"
} |
| 37/3 | 2026/10/24 22:30 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 33688 output: 358 thoughts: 1448 Thoughts: **KMSAN Evaluation for Atomic Directory Creation Patch** Okay, let's break down this patch series and determine if we need a KMSAN session. First, I reviewed the code, focusing on `finish_lookup` and how it interacts with the `lookup` inode operation. I checked for proper initialization and handling of variables within that function. Then, I went through the KMSAN vs. KASAN checklist. Given my understanding of the kernel's memory safety landscape, I know it's crucial to avoid redundant testing. KASAN, LOCKDEP, and the standard bug detectors already cover a lot of ground. KMSAN should only be used if we're dealing with risks specific to uninitialized memory reads or information leaks that KASAN wouldn't catch. This is a good design choice, to avoid overloading the test infrastructure with unnecessary KMSAN sessions. I applied the KMSAN criteria to this patch. It does not introduce any kernel structures that are directly exposed to user space, so point #1 is irrelevant. The core of this patch series is about supporting atomic directory creation via `openat()`, which is mostly VFS plumbing and doesn't appear to have memory-related risks. Checking variables like `open_flag`, `mode`, `create_dir`, `create_error`, and so on, I confirmed they are initialized before use, and no branching depends on potentially uninitialized fields. Thus I can dismiss point #2 as well. Examining inode allocation in `kernfs`, it is handled by `new_inode(dir->i_sb)`, and then properly zeroed and initialized via `alloc_inode`, `inode_init_always` and `kernfs_init_inode`. No risks arise due to uninitialized data structures. So, point #3 is covered. Finally, the patch uses `qstr` strings, which are NUL-terminated or bounded by `len`, eliminating the possibility of uninitialized byte access through bounds checks. So, point #4 is likewise covered. The potential risks here are primarily in locking (LOCKDEP would catch those), reference counting and object lifecycle (KASAN and refcount checking would catch those), and filesystem semantic bugs. None of these are related to uninitialized memory. Therefore, `NeedsKMSAN` should definitely be set to `false`. Now, let's formulate the `KMSANReasoning`. This patch adds support for atomic directory creation using `openat()` with `O_CREAT | O_DIRECTORY | O_EXCL` flags. It wires `vfs_mkdir_no_perm` in `lookup_open()` and introduces `SB_I_NO_ATOMIC_MKDIR`. This is extended to `kernfs`, `cgroup`, and `resctrl` to return the created `kernfs_node` directly. KMSAN is not needed because: 1. There are no new kernel-to-user copies or information leaks: The patch modifies path resolution and directory creation, returning file descriptors or error codes to user space. No struct data is copied. 2. No uninitialized memory is used: All variables and data structures touched are fully initialized before usage. The newly created inode (e.g. within `kernfs`) is fully initialized, using existing VFS initialization helpers. 3. The main risks are around concurrency/locking (handled by LOCKDEP), reference counting and freeing (covered by KASAN and refcount checks), or VFS error code regressions. KASAN and the standard debuggers, plus LOCKDEP, are sufficient to catch any bugs introduced by this patch. Therefore, a dedicated KMSAN fuzzing session is not warranted here. Ready to invoke `set-results`. |
| 38/3 | 2026/10/24 22:30 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series introduces support for atomic directory creation via openat() using the O_CREAT | O_DIRECTORY | O_EXCL flags. It refactors VFS path lookup and creation logic (lookup_open, finish_lookup, vfs_mkdir_no_perm), adds the SB_I_NO_ATOMIC_MKDIR superblock flag for unsupported filesystems, and updates kernfs/cgroup/resctrl mkdir callbacks to return the newly created kernfs_node directly.\n\nKMSAN applicability assessment:\n1. No Kernel-to-User Info Leaks: The changes only affect file creation and path lookup control flow, returning standard integer file descriptors or error codes to userspace. No uninitialized structures, buffers, or padding bytes are copied or leaked to user space.\n2. No Uninitialized Memory Branching/Reads: All local variables (flags, mode, dentry pointers, delegated_inode) are explicitly initialized before use. In kernfs, new inodes allocated with new_inode() are standard VFS inodes initialized via inode_init_always() and kernfs_init_inode().\n3. Bug Profile vs Tooling: Potential regressions from this refactoring involve VFS locking (lock nesting and rwsems, which are covered by LOCKDEP), dentry/inode reference counting and use-after-free/double-free (which are covered by KASAN), or semantic error handling.\n\nTherefore, standard KASAN and LOCKDEP builds are fully equipped to catch any memory or locking bugs introduced by these changes, and a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series introduces support for atomic directory creation via openat() using the O_CREAT | O_DIRECTORY | O_EXCL flags. It refactors VFS path lookup and creation logic (lookup_open, finish_lookup, vfs_mkdir_no_perm), adds the SB_I_NO_ATOMIC_MKDIR superblock flag for unsupported filesystems, and updates kernfs/cgroup/resctrl mkdir callbacks to return the newly created kernfs_node directly.\n\nKMSAN applicability assessment:\n1. No Kernel-to-User Info Leaks: The changes only affect file creation and path lookup control flow, returning standard integer file descriptors or error codes to userspace. No uninitialized structures, buffers, or padding bytes are copied or leaked to user space.\n2. No Uninitialized Memory Branching/Reads: All local variables (flags, mode, dentry pointers, delegated_inode) are explicitly initialized before use. In kernfs, new inodes allocated with new_inode() are standard VFS inodes initialized via inode_init_always() and kernfs_init_inode().\n3. Bug Profile vs Tooling: Potential regressions from this refactoring involve VFS locking (lock nesting and rwsems, which are covered by LOCKDEP), dentry/inode reference counting and use-after-free/double-free (which are covered by KASAN), or semantic error handling.\n\nTherefore, standard KASAN and LOCKDEP builds are fully equipped to catch any memory or locking bugs introduced by these changes, and a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|