Skip to contentSkip to conversation

OPENAT2_REGULAR flag support for openat2

PATCH Applied Dorjoy Chowdhury 6 participants 33 messages Mar 28 – May 21

Subsystems (16)
VFS Kselftest MIPS NFS client +12 more subsystems
lore
Share & tools
lore
#0 lore raw

v6 cover OPENAT2_REGULAR flag support for openat2

Hi,

I came upon this "Ability to only open regular files" uapi feature suggestion from https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files
and thought it would be something I could do as a first patch and get to know the kernel code a bit better.

The following filesystems have been tested by building and booting the kernel x86 bzImage in a Fedora 43 VM in QEMU. I have tested with OPENAT2_REGULAR that regular files can be successfully opened and non-regular files (directory, fifo etc) return -EFTYPE.
- btrfs
- NFS (loopback)
- SMB (loopback)

Changes in v6:
- OPENAT2_REGULAR stripped from file->f_flags in do_dentry_open so that it doesn't leak in fcntl(fd, F_GETFL)
- BUILD_BUG_ON updated to use VALID_OPENAT2_FLAGS instead of VALID_OPEN_FLAGS in build_open_flags and in fcntl_init
- v5 is at: https://lore.kernel.org/linux-fsdevel/[email protected]/T/

Changes in v5:
- EFTYPE is already used in BSDs mentioned in commit message
- consistently return -EFTYPE in all filesystems
- v4 is at: https://lore.kernel.org/linux-fsdevel/[email protected]/T/

Changes in v4:
- changed O_REGULAR to OPENAT2_REGULAR
- OPENAT2_REGULAR does not affect O_PATH
- atomic_open codepaths updated to work properly for OPENAT2_REGULAR
- commit message includes the uapi-group URL
- v3 is at: https://lore.kernel.org/linux-fsdevel/[email protected]/T/

Changes in v3:
- included motivation about O_REGULAR flag in commit message e.g., programs not wanting to be tricked into opening device nodes
- fixed commit message wrongly referencing ENOTREGULAR instead of ENOTREG
- fixed the O_REGULAR flag in arch/parisc/include/uapi/asm/fcntl.h from 060000000 to 0100000000
- added 2 commits converting arch/{mips,sparc}/include/uapi/asm/fcntl.h O_* macros from hex to octal
- v2 is at: https://lore.kernel.org/linux-fsdevel/[email protected]/T/

Changes in v2:
- rename ENOTREGULAR to ENOTREG
- define ENOTREG in uapi/asm-generic/errno.h (instead of errno-base.h) and in arch/*/include/uapi/asm/errno.h files
- override O_REGULAR in arch/{alpha,sparc,parisc}/include/uapi/asm/fcntl.h due to clash with include/uapi/asm-generic/fcntl.h
- I have kept the kselftest but now that O_REGULAR and ENOTREG can have different value on different architectures I am not sure if it's right
- v1 is at: https://lore.kernel.org/linux-fsdevel/[email protected]/T/

Thanks.

Regards,
Dorjoy

Dorjoy Chowdhury (4):
openat2: new OPENAT2_REGULAR flag support kselftest/openat2: test for OPENAT2_REGULAR flag sparc/fcntl.h: convert O_* flag macros from hex to octal mips/fcntl.h: convert O_* flag macros from hex to octal

 arch/alpha/include/uapi/asm/errno.h           |  2 +
 arch/alpha/include/uapi/asm/fcntl.h           |  1 +
 arch/mips/include/uapi/asm/errno.h            |  2 +
 arch/mips/include/uapi/asm/fcntl.h            | 22 +++++------
 arch/parisc/include/uapi/asm/errno.h          |  2 +
 arch/parisc/include/uapi/asm/fcntl.h          |  1 +
 arch/sparc/include/uapi/asm/errno.h           |  2 +
 arch/sparc/include/uapi/asm/fcntl.h           | 35 +++++++++---------
 fs/ceph/file.c                                |  4 ++
 fs/fcntl.c                                    |  4 +-
 fs/gfs2/inode.c                               |  6 +++
 fs/namei.c                                    |  4 ++
 fs/nfs/dir.c                                  |  4 ++
 fs/open.c                                     |  8 ++--
 fs/smb/client/dir.c                           | 14 ++++++-
 include/linux/fcntl.h                         |  2 +
 include/uapi/asm-generic/errno.h              |  2 +
 include/uapi/asm-generic/fcntl.h              |  4 ++
 tools/arch/alpha/include/uapi/asm/errno.h     |  2 +
 tools/arch/mips/include/uapi/asm/errno.h      |  2 +
 tools/arch/parisc/include/uapi/asm/errno.h    |  2 +
 tools/arch/sparc/include/uapi/asm/errno.h     |  2 +
 tools/include/uapi/asm-generic/errno.h        |  2 +
 .../testing/selftests/openat2/openat2_test.c  | 37 ++++++++++++++++++-
 24 files changed, 131 insertions(+), 35 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v6 1/4 openat2: new OPENAT2_REGULAR flag support

This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. This is a requested feature from the uapi-group[1].

A corresponding error code EFTYPE has been introduced. For example, if openat2 is called on path /dev/null with OPENAT2_REGULAR in the flag param, it will return -EFTYPE. EFTYPE is already used in BSD systems like FreeBSD, macOS.

When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -EFTYPE is returned.

When OPENAT2_REGULAR is combined with O_DIRECTORY, -EINVAL is returned as it doesn't make sense to open a path that is both a directory and a regular file.

[1]: https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files

---
 arch/alpha/include/uapi/asm/errno.h        |  2 ++
 arch/alpha/include/uapi/asm/fcntl.h        |  1 +
 arch/mips/include/uapi/asm/errno.h         |  2 ++
 arch/parisc/include/uapi/asm/errno.h       |  2 ++
 arch/parisc/include/uapi/asm/fcntl.h       |  1 +
 arch/sparc/include/uapi/asm/errno.h        |  2 ++
 arch/sparc/include/uapi/asm/fcntl.h        |  1 +
 fs/ceph/file.c                             |  4 ++++
 fs/fcntl.c                                 |  4 ++--
 fs/gfs2/inode.c                            |  6 ++++++
 fs/namei.c                                 |  4 ++++
 fs/nfs/dir.c                               |  4 ++++
 fs/open.c                                  |  8 +++++---
 fs/smb/client/dir.c                        | 14 +++++++++++++-
 include/linux/fcntl.h                      |  2 ++
 include/uapi/asm-generic/errno.h           |  2 ++
 include/uapi/asm-generic/fcntl.h           |  4 ++++
 tools/arch/alpha/include/uapi/asm/errno.h  |  2 ++
 tools/arch/mips/include/uapi/asm/errno.h   |  2 ++
 tools/arch/parisc/include/uapi/asm/errno.h |  2 ++
 tools/arch/sparc/include/uapi/asm/errno.h  |  2 ++
 tools/include/uapi/asm-generic/errno.h     |  2 ++
 22 files changed, 67 insertions(+), 6 deletions(-)
arch/alpha/include/uapi/asm/errno.h +2 -0
index 6791f6508632..1a99f38813c7 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON	139	/* Memory page has hardware error */
 
+#define EFTYPE		140	/* Wrong file type for the intended operation */
+
 #endif
arch/alpha/include/uapi/asm/fcntl.h +1 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
 #define O_PATH		040000000
 #define __O_TMPFILE	0100000000
+#define OPENAT2_REGULAR	0200000000
 
 #define F_GETLK		7
 #define F_SETLK		8
arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..1835a50b69ce 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON	168	/* Memory page has hardware error */
 
+#define EFTYPE		169	/* Wrong file type for the intended operation */
+
 #define EDQUOT		1133	/* Quota exceeded */
 
 
arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..93194fbb0a80 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON	257	/* Memory page has hardware error */
 
+#define EFTYPE		258	/* Wrong file type for the intended operation */
+
 #endif
arch/parisc/include/uapi/asm/fcntl.h +1 -0
index 03dee816cb13..d46812f2f0f4 100644
@@ -19,6 +19,7 @@
 
 #define O_PATH		020000000
 #define __O_TMPFILE	040000000
+#define OPENAT2_REGULAR	0100000000
 
 #define F_GETLK64	8
 #define F_SETLK64	9
arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..71940ec9130b 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON	135	/* Memory page has hardware error */
 
+#define EFTYPE		136	/* Wrong file type for the intended operation */
+
 #endif
arch/sparc/include/uapi/asm/fcntl.h +1 -0
index 67dae75e5274..bb6e9fa94bc9 100644
@@ -37,6 +37,7 @@
 
 #define O_PATH		0x1000000
 #define __O_TMPFILE	0x2000000
+#define OPENAT2_REGULAR	0x4000000
 
 #define F_GETOWN	5	/*  for sockets. */
 #define F_SETOWN	6	/*  for sockets. */
fs/ceph/file.c +4 -0
index 66bbf6d517a9..6d8d4c7765e6 100644
@@ -977,6 +977,10 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
 			ceph_init_inode_acls(newino, &as_ctx);
 			file->f_mode |= FMODE_CREATED;
 		}
+		if ((flags & OPENAT2_REGULAR) && !d_is_reg(dentry)) {
+			err = -EFTYPE;
+			goto out_req;
+		}
 		err = finish_open(file, dentry, ceph_open);
 	}
 out_req:
fs/fcntl.c +2 -2
index beab8080badf..240bb511557a 100644
@@ -1169,9 +1169,9 @@ static int __init fcntl_init(void)
 	 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
 	 * is defined as O_NONBLOCK on some platforms and not on others.
 	 */
-	BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ !=
+	BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
 		HWEIGHT32(
-			(VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
+			(VALID_OPENAT2_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
 			__FMODE_EXEC));
 
 	fasync_cache = kmem_cache_create("fasync_cache",
fs/gfs2/inode.c +6 -0
index 8344040ecaf7..4604e2e8a9cc 100644
@@ -738,6 +738,12 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
 	inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
 	error = PTR_ERR(inode);
 	if (!IS_ERR(inode)) {
+		if (file && (file->f_flags & OPENAT2_REGULAR) && !S_ISREG(inode->i_mode)) {
+			iput(inode);
+			inode = NULL;
+			error = -EFTYPE;
+			goto fail_gunlock;
+		}
 		if (S_ISDIR(inode->i_mode)) {
 			iput(inode);
 			inode = NULL;
fs/namei.c +4 -0
index 2113958c3b7a..e557c538c238 100644
@@ -4679,6 +4679,10 @@ static int do_open(struct nameidata *nd,
 		if (unlikely(error))
 			return error;
 	}
+
+	if ((open_flag & OPENAT2_REGULAR) && !d_is_reg(nd->path.dentry))
+		return -EFTYPE;
+
 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
 		return -ENOTDIR;
 
fs/nfs/dir.c +4 -0
index ddc3789363a5..bfe9470327c8 100644
@@ -2195,6 +2195,10 @@ int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
 			break;
 		case -EISDIR:
 		case -ENOTDIR:
+			if (open_flags & OPENAT2_REGULAR) {
+				err = -EFTYPE;
+				break;
+			}
 			goto no_open;
 		case -ELOOP:
 			if (!(open_flags & O_NOFOLLOW))
fs/open.c +5 -3
index 681d405bc61e..a6f445f72181 100644
@@ -960,7 +960,7 @@ static int do_dentry_open(struct file *f,
 	if (f->f_mapping->a_ops && f->f_mapping->a_ops->direct_IO)
 		f->f_mode |= FMODE_CAN_ODIRECT;
 
-	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
+	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | OPENAT2_REGULAR);
 	f->f_iocb_flags = iocb_flags(f);
 
 	file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
@@ -1183,7 +1183,7 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
 	int lookup_flags = 0;
 	int acc_mode = ACC_MODE(flags);
 
-	BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPEN_FLAGS),
+	BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPENAT2_FLAGS),
 			 "struct open_flags doesn't yet handle flags > 32 bits");
 
 	/*
@@ -1196,7 +1196,7 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
 	 * values before calling build_open_flags(), but openat2(2) checks all
 	 * of its arguments.
 	 */
-	if (flags & ~VALID_OPEN_FLAGS)
+	if (flags & ~VALID_OPENAT2_FLAGS)
 		return -EINVAL;
 	if (how->resolve & ~VALID_RESOLVE_FLAGS)
 		return -EINVAL;
@@ -1235,6 +1235,8 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
 			return -EINVAL;
 		if (!(acc_mode & MAY_WRITE))
 			return -EINVAL;
+	} else if ((flags & O_DIRECTORY) && (flags & OPENAT2_REGULAR)) {
+		return -EINVAL;
 	}
 	if (flags & O_PATH) {
 		/* O_PATH only permits certain other flags to be set. */
fs/smb/client/dir.c +13 -1
index 953f1fee8cb8..355681ebacf1 100644
@@ -222,6 +222,13 @@ static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned
 				goto cifs_create_get_file_info;
 			}
 
+			if ((oflags & OPENAT2_REGULAR) && !S_ISREG(newinode->i_mode)) {
+				CIFSSMBClose(xid, tcon, fid->netfid);
+				iput(newinode);
+				rc = -EFTYPE;
+				goto out;
+			}
+
 			if (S_ISDIR(newinode->i_mode)) {
 				CIFSSMBClose(xid, tcon, fid->netfid);
 				iput(newinode);
@@ -436,11 +443,16 @@ static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned
 		goto out_err;
 	}
 
-	if (newinode)
+	if (newinode) {
+		if ((oflags & OPENAT2_REGULAR) && !S_ISREG(newinode->i_mode)) {
+			rc = -EFTYPE;
+			goto out_err;
+		}
 		if (S_ISDIR(newinode->i_mode)) {
 			rc = -EISDIR;
 			goto out_err;
 		}
+	}
 
 	d_drop(direntry);
 	d_add(direntry, newinode);
include/linux/fcntl.h +2 -0
index a332e79b3207..a80026718217 100644
@@ -12,6 +12,8 @@
 	 FASYNC	| O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \
 	 O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE)
 
+#define VALID_OPENAT2_FLAGS (VALID_OPEN_FLAGS | OPENAT2_REGULAR)
+
 /* List of all valid flags for the how->resolve argument: */
 #define VALID_RESOLVE_FLAGS \
 	(RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS | \
include/uapi/asm-generic/errno.h +2 -0
index 92e7ae493ee3..bd78e69e0a43 100644
@@ -122,4 +122,6 @@
 
 #define EHWPOISON	133	/* Memory page has hardware error */
 
+#define EFTYPE		134	/* Wrong file type for the intended operation */
+
 #endif
include/uapi/asm-generic/fcntl.h +4 -0
index 613475285643..b2c2ddd0edc0 100644
@@ -88,6 +88,10 @@
 #define __O_TMPFILE	020000000
 #endif
 
+#ifndef OPENAT2_REGULAR
+#define OPENAT2_REGULAR	040000000
+#endif
+
 /* a horrid kludge trying to make sure that this will fail on old kernels */
 #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
 
tools/arch/alpha/include/uapi/asm/errno.h +2 -0
index 6791f6508632..1a99f38813c7 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON	139	/* Memory page has hardware error */
 
+#define EFTYPE		140	/* Wrong file type for the intended operation */
+
 #endif
tools/arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..1835a50b69ce 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON	168	/* Memory page has hardware error */
 
+#define EFTYPE		169	/* Wrong file type for the intended operation */
+
 #define EDQUOT		1133	/* Quota exceeded */
 
 
tools/arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..93194fbb0a80 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON	257	/* Memory page has hardware error */
 
+#define EFTYPE		258	/* Wrong file type for the intended operation */
+
 #endif
tools/arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..71940ec9130b 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON	135	/* Memory page has hardware error */
 
+#define EFTYPE		136	/* Wrong file type for the intended operation */
+
 #endif
tools/include/uapi/asm-generic/errno.h +2 -0
index 92e7ae493ee3..bd78e69e0a43 100644
@@ -122,4 +122,6 @@
 
 #define EHWPOISON	133	/* Memory page has hardware error */
 
+#define EFTYPE		134	/* Wrong file type for the intended operation */
+
 #endif
Signature · 1 line
2.53.0
maintainer
#2 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 134 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. This is a requested feature from the uapi-group[1].

A corresponding error code EFTYPE has been introduced. For example, if openat2 is called on path /dev/null with OPENAT2_REGULAR in the flag param, it will return -EFTYPE. EFTYPE is already used in BSD systems like FreeBSD, macOS.

When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -EFTYPE is returned.

When OPENAT2_REGULAR is combined with O_DIRECTORY, -EINVAL is returned as it doesn't make sense to open a path that is both a directory and a regular file.

[1]: https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files

---
 arch/alpha/include/uapi/asm/errno.h        |  2 ++
 arch/alpha/include/uapi/asm/fcntl.h        |  1 +
 arch/mips/include/uapi/asm/errno.h         |  2 ++
 arch/parisc/include/uapi/asm/errno.h       |  2 ++
 arch/parisc/include/uapi/asm/fcntl.h       |  1 +
 arch/sparc/include/uapi/asm/errno.h        |  2 ++
 arch/sparc/include/uapi/asm/fcntl.h        |  1 +
 fs/ceph/file.c                             |  4 ++++
 fs/fcntl.c                                 |  4 ++--
 fs/gfs2/inode.c                            |  6 ++++++
 fs/namei.c                                 |  4 ++++
 fs/nfs/dir.c                               |  4 ++++
 fs/open.c                                  |  8 +++++---
 fs/smb/client/dir.c                        | 14 +++++++++++++-
 include/linux/fcntl.h                      |  2 ++
 include/uapi/asm-generic/errno.h           |  2 ++
 include/uapi/asm-generic/fcntl.h           |  4 ++++
 tools/arch/alpha/include/uapi/asm/errno.h  |  2 ++
 tools/arch/mips/include/uapi/asm/errno.h   |  2 ++
 tools/arch/parisc/include/uapi/asm/errno.h |  2 ++
 tools/arch/sparc/include/uapi/asm/errno.h  |  2 ++
 tools/include/uapi/asm-generic/errno.h     |  2 ++
 22 files changed, 67 insertions(+), 6 deletions(-)
arch/alpha/include/uapi/asm/errno.h +2 -0
index 6791f6508632..1a99f38813c7 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON	139	/* Memory page has hardware error */
 
+#define EFTYPE		140	/* Wrong file type for the intended operation */
+
 #endif
arch/alpha/include/uapi/asm/fcntl.h +1 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
 #define O_PATH		040000000
 #define __O_TMPFILE	0100000000
+#define OPENAT2_REGULAR	0200000000
 
 #define F_GETLK		7
 #define F_SETLK		8
arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..1835a50b69ce 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON	168	/* Memory page has hardware error */
 
+#define EFTYPE		169	/* Wrong file type for the intended operation */
+
 #define EDQUOT		1133	/* Quota exceeded */
 
 
arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..93194fbb0a80 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON	257	/* Memory page has hardware error */
 
+#define EFTYPE		258	/* Wrong file type for the intended operation */
+
 #endif
arch/parisc/include/uapi/asm/fcntl.h +1 -0
index 03dee816cb13..d46812f2f0f4 100644
@@ -19,6 +19,7 @@
 
 #define O_PATH		020000000
 #define __O_TMPFILE	040000000
+#define OPENAT2_REGULAR	0100000000
 
 #define F_GETLK64	8
 #define F_SETLK64	9
arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..71940ec9130b 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON	135	/* Memory page has hardware error */
 
+#define EFTYPE		136	/* Wrong file type for the intended operation */
+
 #endif
arch/sparc/include/uapi/asm/fcntl.h +1 -0
index 67dae75e5274..bb6e9fa94bc9 100644
@@ -37,6 +37,7 @@
 
 #define O_PATH		0x1000000
 #define __O_TMPFILE	0x2000000
+#define OPENAT2_REGULAR	0x4000000
 
 #define F_GETOWN	5	/*  for sockets. */
 #define F_SETOWN	6	/*  for sockets. */
fs/ceph/file.c +0 -0
index 66bbf6d517a9..6d8d4c7765e6 100644

fs/ceph/file.c int ceph_atomic_open(struct inode *dir, struct dentry *dentry, · line 983

Quoted 7 lines
fs/ceph/file.c +4 -0
@@ -977,6 +977,10 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
 			ceph_init_inode_acls(newino, &as_ctx);
 			file->f_mode |= FMODE_CREATED;
 		}
+		if ((flags & OPENAT2_REGULAR) && !d_is_reg(dentry)) {
+			err = -EFTYPE;
+			goto out_req;
+		}

^^^
This doesn't look quite right. Here's a larger chunk of the code:

-------------------------8<--------------------------

        if (d_in_lookup(dentry)) {                                                                  
                dn = ceph_finish_lookup(req, dentry, err);                                          
                if (IS_ERR(dn))                                                                     
                        err = PTR_ERR(dn);                                                          
        } else {                                                                                    
                /* we were given a hashed negative dentry */                                        
                dn = NULL;                                                                          
        }                                                                                           
        if (err)                                                                                    
                goto out_req;                                                                       
        if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {                           
                /* make vfs retry on splice, ENOENT, or symlink */                                  
                doutc(cl, "finish_no_open on dn %p\n", dn);                                         
                err = finish_no_open(file, dn);                                                     
        } else {                                                                                    
                if (IS_ENCRYPTED(dir) &&                                                            
                    !fscrypt_has_permitted_context(dir, d_inode(dentry))) {                         
                        pr_warn_client(cl,                                                          
                                "Inconsistent encryption context (parent %llx:%llx child %llx:%llx)\n",
                                ceph_vinop(dir), ceph_vinop(d_inode(dentry)));                      
                        goto out_req;                                                               
                }                                                                                   
                                                                                                    
                doutc(cl, "finish_open on dn %p\n", dn);                                            
                if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {          
                        struct inode *newino = d_inode(dentry);                                     
                                                                                                    
                        cache_file_layout(dir, newino);                                             
                        ceph_init_inode_acls(newino, &as_ctx);                                      
                        file->f_mode |= FMODE_CREATED;                                              
                }                                                                                   
                err = finish_open(file, dentry, ceph_open);                                         
        }                                                                                           

-------------------------8<--------------------------

It looks like this won't handle it correctly if the pathwalk terminates on a symlink (re: d_is_symlink() case). You should either set up a test ceph cluster on your own, or reach out to the ceph community and ask them to test this.

Quoted 24 lines
fs/ceph/file.c +0 -0
 		err = finish_open(file, dentry, ceph_open);
 	}
 out_req:
fs/fcntl.c +2 -2
index beab8080badf..240bb511557a 100644
@@ -1169,9 +1169,9 @@ static int __init fcntl_init(void)
 	 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
 	 * is defined as O_NONBLOCK on some platforms and not on others.
 	 */
-	BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ !=
+	BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
 		HWEIGHT32(
-			(VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
+			(VALID_OPENAT2_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
 			__FMODE_EXEC));
 
 	fasync_cache = kmem_cache_create("fasync_cache",
fs/gfs2/inode.c +0 -0
index 8344040ecaf7..4604e2e8a9cc 100644

fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 741

Quoted 4 lines
fs/gfs2/inode.c +1 -0
@@ -738,6 +738,12 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
 	inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
 	error = PTR_ERR(inode);
 	if (!IS_ERR(inode)) {
+		if (file && (file->f_flags & OPENAT2_REGULAR) && !S_ISREG(inode->i_mode)) {

Isn't OPENAT2_REGULAR getting masked off in ->f_flags now?

JFYI: it's quite simple to set up a single-node gfs2 fs to test this.

Quoted 210 lines
fs/gfs2/inode.c +5 -0
+			iput(inode);
+			inode = NULL;
+			error = -EFTYPE;
+			goto fail_gunlock;
+		}
 		if (S_ISDIR(inode->i_mode)) {
 			iput(inode);
 			inode = NULL;
fs/namei.c +4 -0
index 2113958c3b7a..e557c538c238 100644
@@ -4679,6 +4679,10 @@ static int do_open(struct nameidata *nd,
 		if (unlikely(error))
 			return error;
 	}
+
+	if ((open_flag & OPENAT2_REGULAR) && !d_is_reg(nd->path.dentry))
+		return -EFTYPE;
+
 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
 		return -ENOTDIR;
 
fs/nfs/dir.c +4 -0
index ddc3789363a5..bfe9470327c8 100644
@@ -2195,6 +2195,10 @@ int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
 			break;
 		case -EISDIR:
 		case -ENOTDIR:
+			if (open_flags & OPENAT2_REGULAR) {
+				err = -EFTYPE;
+				break;
+			}
 			goto no_open;
 		case -ELOOP:
 			if (!(open_flags & O_NOFOLLOW))
fs/open.c +5 -3
index 681d405bc61e..a6f445f72181 100644
@@ -960,7 +960,7 @@ static int do_dentry_open(struct file *f,
 	if (f->f_mapping->a_ops && f->f_mapping->a_ops->direct_IO)
 		f->f_mode |= FMODE_CAN_ODIRECT;
 
-	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
+	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | OPENAT2_REGULAR);
 	f->f_iocb_flags = iocb_flags(f);
 
 	file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
@@ -1183,7 +1183,7 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
 	int lookup_flags = 0;
 	int acc_mode = ACC_MODE(flags);
 
-	BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPEN_FLAGS),
+	BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPENAT2_FLAGS),
 			 "struct open_flags doesn't yet handle flags > 32 bits");
 
 	/*
@@ -1196,7 +1196,7 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
 	 * values before calling build_open_flags(), but openat2(2) checks all
 	 * of its arguments.
 	 */
-	if (flags & ~VALID_OPEN_FLAGS)
+	if (flags & ~VALID_OPENAT2_FLAGS)
 		return -EINVAL;
 	if (how->resolve & ~VALID_RESOLVE_FLAGS)
 		return -EINVAL;
@@ -1235,6 +1235,8 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
 			return -EINVAL;
 		if (!(acc_mode & MAY_WRITE))
 			return -EINVAL;
+	} else if ((flags & O_DIRECTORY) && (flags & OPENAT2_REGULAR)) {
+		return -EINVAL;
 	}
 	if (flags & O_PATH) {
 		/* O_PATH only permits certain other flags to be set. */
fs/smb/client/dir.c +13 -1
index 953f1fee8cb8..355681ebacf1 100644
@@ -222,6 +222,13 @@ static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned
 				goto cifs_create_get_file_info;
 			}
 
+			if ((oflags & OPENAT2_REGULAR) && !S_ISREG(newinode->i_mode)) {
+				CIFSSMBClose(xid, tcon, fid->netfid);
+				iput(newinode);
+				rc = -EFTYPE;
+				goto out;
+			}
+
 			if (S_ISDIR(newinode->i_mode)) {
 				CIFSSMBClose(xid, tcon, fid->netfid);
 				iput(newinode);
@@ -436,11 +443,16 @@ static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned
 		goto out_err;
 	}
 
-	if (newinode)
+	if (newinode) {
+		if ((oflags & OPENAT2_REGULAR) && !S_ISREG(newinode->i_mode)) {
+			rc = -EFTYPE;
+			goto out_err;
+		}
 		if (S_ISDIR(newinode->i_mode)) {
 			rc = -EISDIR;
 			goto out_err;
 		}
+	}
 
 	d_drop(direntry);
 	d_add(direntry, newinode);
include/linux/fcntl.h +2 -0
index a332e79b3207..a80026718217 100644
@@ -12,6 +12,8 @@
 	 FASYNC	| O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \
 	 O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE)
 
+#define VALID_OPENAT2_FLAGS (VALID_OPEN_FLAGS | OPENAT2_REGULAR)
+
 /* List of all valid flags for the how->resolve argument: */
 #define VALID_RESOLVE_FLAGS \
 	(RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS | \
include/uapi/asm-generic/errno.h +2 -0
index 92e7ae493ee3..bd78e69e0a43 100644
@@ -122,4 +122,6 @@
 
 #define EHWPOISON	133	/* Memory page has hardware error */
 
+#define EFTYPE		134	/* Wrong file type for the intended operation */
+
 #endif
include/uapi/asm-generic/fcntl.h +4 -0
index 613475285643..b2c2ddd0edc0 100644
@@ -88,6 +88,10 @@
 #define __O_TMPFILE	020000000
 #endif
 
+#ifndef OPENAT2_REGULAR
+#define OPENAT2_REGULAR	040000000
+#endif
+
 /* a horrid kludge trying to make sure that this will fail on old kernels */
 #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
 
tools/arch/alpha/include/uapi/asm/errno.h +2 -0
index 6791f6508632..1a99f38813c7 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON	139	/* Memory page has hardware error */
 
+#define EFTYPE		140	/* Wrong file type for the intended operation */
+
 #endif
tools/arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..1835a50b69ce 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON	168	/* Memory page has hardware error */
 
+#define EFTYPE		169	/* Wrong file type for the intended operation */
+
 #define EDQUOT		1133	/* Quota exceeded */
 
 
tools/arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..93194fbb0a80 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON	257	/* Memory page has hardware error */
 
+#define EFTYPE		258	/* Wrong file type for the intended operation */
+
 #endif
tools/arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..71940ec9130b 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON	135	/* Memory page has hardware error */
 
+#define EFTYPE		136	/* Wrong file type for the intended operation */
+
 #endif
tools/include/uapi/asm-generic/errno.h +2 -0
index 92e7ae493ee3..bd78e69e0a43 100644
@@ -122,4 +122,6 @@
 
 #define EHWPOISON	133	/* Memory page has hardware error */
 
+#define EFTYPE		134	/* Wrong file type for the intended operation */
+
 #endif
Signature · 1 line
Jeff Layton <[email protected]>
#3 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 181 lines · Jeff Layton
Quoted 134 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. This is a requested feature from the uapi-group[1].

A corresponding error code EFTYPE has been introduced. For example, if openat2 is called on path /dev/null with OPENAT2_REGULAR in the flag param, it will return -EFTYPE. EFTYPE is already used in BSD systems like FreeBSD, macOS.

When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -EFTYPE is returned.

When OPENAT2_REGULAR is combined with O_DIRECTORY, -EINVAL is returned as it doesn't make sense to open a path that is both a directory and a regular file.

[1]: https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files

---
 arch/alpha/include/uapi/asm/errno.h        |  2 ++
 arch/alpha/include/uapi/asm/fcntl.h        |  1 +
 arch/mips/include/uapi/asm/errno.h         |  2 ++
 arch/parisc/include/uapi/asm/errno.h       |  2 ++
 arch/parisc/include/uapi/asm/fcntl.h       |  1 +
 arch/sparc/include/uapi/asm/errno.h        |  2 ++
 arch/sparc/include/uapi/asm/fcntl.h        |  1 +
 fs/ceph/file.c                             |  4 ++++
 fs/fcntl.c                                 |  4 ++--
 fs/gfs2/inode.c                            |  6 ++++++
 fs/namei.c                                 |  4 ++++
 fs/nfs/dir.c                               |  4 ++++
 fs/open.c                                  |  8 +++++---
 fs/smb/client/dir.c                        | 14 +++++++++++++-
 include/linux/fcntl.h                      |  2 ++
 include/uapi/asm-generic/errno.h           |  2 ++
 include/uapi/asm-generic/fcntl.h           |  4 ++++
 tools/arch/alpha/include/uapi/asm/errno.h  |  2 ++
 tools/arch/mips/include/uapi/asm/errno.h   |  2 ++
 tools/arch/parisc/include/uapi/asm/errno.h |  2 ++
 tools/arch/sparc/include/uapi/asm/errno.h  |  2 ++
 tools/include/uapi/asm-generic/errno.h     |  2 ++
 22 files changed, 67 insertions(+), 6 deletions(-)
arch/alpha/include/uapi/asm/errno.h +2 -0
index 6791f6508632..1a99f38813c7 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON    139     /* Memory page has hardware error */
 
+#define EFTYPE               140     /* Wrong file type for the intended operation */
+
 #endif
arch/alpha/include/uapi/asm/fcntl.h +1 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
 #define O_PATH               040000000
 #define __O_TMPFILE  0100000000
+#define OPENAT2_REGULAR      0200000000
 
 #define F_GETLK              7
 #define F_SETLK              8
arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..1835a50b69ce 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON    168     /* Memory page has hardware error */
 
+#define EFTYPE               169     /* Wrong file type for the intended operation */
+
 #define EDQUOT               1133    /* Quota exceeded */
 
 
arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..93194fbb0a80 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON    257     /* Memory page has hardware error */
 
+#define EFTYPE               258     /* Wrong file type for the intended operation */
+
 #endif
arch/parisc/include/uapi/asm/fcntl.h +1 -0
index 03dee816cb13..d46812f2f0f4 100644
@@ -19,6 +19,7 @@
 
 #define O_PATH               020000000
 #define __O_TMPFILE  040000000
+#define OPENAT2_REGULAR      0100000000
 
 #define F_GETLK64    8
 #define F_SETLK64    9
arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..71940ec9130b 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON    135     /* Memory page has hardware error */
 
+#define EFTYPE               136     /* Wrong file type for the intended operation */
+
 #endif
arch/sparc/include/uapi/asm/fcntl.h +1 -0
index 67dae75e5274..bb6e9fa94bc9 100644
@@ -37,6 +37,7 @@
 
 #define O_PATH               0x1000000
 #define __O_TMPFILE  0x2000000
+#define OPENAT2_REGULAR      0x4000000
 
 #define F_GETOWN     5       /*  for sockets. */
 #define F_SETOWN     6       /*  for sockets. */
fs/ceph/file.c +0 -0
index 66bbf6d517a9..6d8d4c7765e6 100644

fs/ceph/file.c int ceph_atomic_open(struct inode *dir, struct dentry *dentry, · line 983

Quoted 7 lines
fs/ceph/file.c +4 -0
@@ -977,6 +977,10 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
                      ceph_init_inode_acls(newino, &as_ctx);
                      file->f_mode |= FMODE_CREATED;
              }
+             if ((flags & OPENAT2_REGULAR) && !d_is_reg(dentry)) {
+                     err = -EFTYPE;
+                     goto out_req;
+             }

^^^
This doesn't look quite right. Here's a larger chunk of the code:

-------------------------8<--------------------------

        if (d_in_lookup(dentry)) {
                dn = ceph_finish_lookup(req, dentry, err);
                if (IS_ERR(dn))
                        err = PTR_ERR(dn);
        } else {
                /* we were given a hashed negative dentry */
                dn = NULL;
        }
        if (err)
                goto out_req;
        if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
                /* make vfs retry on splice, ENOENT, or symlink */
                doutc(cl, "finish_no_open on dn %p\n", dn);
                err = finish_no_open(file, dn);
        } else {
                if (IS_ENCRYPTED(dir) &&
                    !fscrypt_has_permitted_context(dir, d_inode(dentry))) {
                        pr_warn_client(cl,
                                "Inconsistent encryption context (parent %llx:%llx child %llx:%llx)\n",
                                ceph_vinop(dir), ceph_vinop(d_inode(dentry)));
                        goto out_req;
                }

                doutc(cl, "finish_open on dn %p\n", dn);
                if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
                        struct inode *newino = d_inode(dentry);

                        cache_file_layout(dir, newino);
                        ceph_init_inode_acls(newino, &as_ctx);
                        file->f_mode |= FMODE_CREATED;
                }
                err = finish_open(file, dentry, ceph_open);
        }
Quoted 7 lines

-------------------------8<--------------------------

It looks like this won't handle it correctly if the pathwalk terminates on a symlink (re: d_is_symlink() case). You should either set up a test ceph cluster on your own, or reach out to the ceph community and ask them to test this.

Thanks for reviewing. The d_is_symlink() case seems to be calling finish_no_open so shouldn't this be okay?

Quoted 18 lines
Quoted 18 lines
fs/ceph/file.c +0 -0
              err = finish_open(file, dentry, ceph_open);
      }
 out_req:
fs/fcntl.c +2 -2
index beab8080badf..240bb511557a 100644
@@ -1169,9 +1169,9 @@ static int __init fcntl_init(void)
       * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
       * is defined as O_NONBLOCK on some platforms and not on others.
       */
-     BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ !=
+     BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
              HWEIGHT32(
-                     (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
+                     (VALID_OPENAT2_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
                      __FMODE_EXEC));
 
Quoted 13 lines

fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 741

Quoted 10 lines
fs/fcntl.c +0 -0
      fasync_cache = kmem_cache_create("fasync_cache",
fs/gfs2/inode.c +1 -0
index 8344040ecaf7..4604e2e8a9cc 100644
@@ -738,6 +738,12 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
      inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
      error = PTR_ERR(inode);
      if (!IS_ERR(inode)) {
+             if (file && (file->f_flags & OPENAT2_REGULAR) && !S_ISREG(inode->i_mode)) {

Isn't OPENAT2_REGULAR getting masked off in ->f_flags now?

Yes, I thought the masking off was happening after this codepath got executed. Maybe it's better anyway to pass another flags param to this function and forward the flags from the gfs2_atomic_open function and in other call sites pass 0 ? What do you think?

Regards,
Dorjoy

maintainer Reviewed-by
#4 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 184 lines · Dorjoy Chowdhury
Quoted 183 lines · Jeff Layton
Quoted 134 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. This is a requested feature from the uapi-group[1].

A corresponding error code EFTYPE has been introduced. For example, if openat2 is called on path /dev/null with OPENAT2_REGULAR in the flag param, it will return -EFTYPE. EFTYPE is already used in BSD systems like FreeBSD, macOS.

When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -EFTYPE is returned.

When OPENAT2_REGULAR is combined with O_DIRECTORY, -EINVAL is returned as it doesn't make sense to open a path that is both a directory and a regular file.

[1]: https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files

---
 arch/alpha/include/uapi/asm/errno.h        |  2 ++
 arch/alpha/include/uapi/asm/fcntl.h        |  1 +
 arch/mips/include/uapi/asm/errno.h         |  2 ++
 arch/parisc/include/uapi/asm/errno.h       |  2 ++
 arch/parisc/include/uapi/asm/fcntl.h       |  1 +
 arch/sparc/include/uapi/asm/errno.h        |  2 ++
 arch/sparc/include/uapi/asm/fcntl.h        |  1 +
 fs/ceph/file.c                             |  4 ++++
 fs/fcntl.c                                 |  4 ++--
 fs/gfs2/inode.c                            |  6 ++++++
 fs/namei.c                                 |  4 ++++
 fs/nfs/dir.c                               |  4 ++++
 fs/open.c                                  |  8 +++++---
 fs/smb/client/dir.c                        | 14 +++++++++++++-
 include/linux/fcntl.h                      |  2 ++
 include/uapi/asm-generic/errno.h           |  2 ++
 include/uapi/asm-generic/fcntl.h           |  4 ++++
 tools/arch/alpha/include/uapi/asm/errno.h  |  2 ++
 tools/arch/mips/include/uapi/asm/errno.h   |  2 ++
 tools/arch/parisc/include/uapi/asm/errno.h |  2 ++
 tools/arch/sparc/include/uapi/asm/errno.h  |  2 ++
 tools/include/uapi/asm-generic/errno.h     |  2 ++
 22 files changed, 67 insertions(+), 6 deletions(-)
arch/alpha/include/uapi/asm/errno.h +2 -0
index 6791f6508632..1a99f38813c7 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON    139     /* Memory page has hardware error */
 
+#define EFTYPE               140     /* Wrong file type for the intended operation */
+
 #endif
arch/alpha/include/uapi/asm/fcntl.h +1 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
 #define O_PATH               040000000
 #define __O_TMPFILE  0100000000
+#define OPENAT2_REGULAR      0200000000
 
 #define F_GETLK              7
 #define F_SETLK              8
arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..1835a50b69ce 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON    168     /* Memory page has hardware error */
 
+#define EFTYPE               169     /* Wrong file type for the intended operation */
+
 #define EDQUOT               1133    /* Quota exceeded */
 
 
arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..93194fbb0a80 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON    257     /* Memory page has hardware error */
 
+#define EFTYPE               258     /* Wrong file type for the intended operation */
+
 #endif
arch/parisc/include/uapi/asm/fcntl.h +1 -0
index 03dee816cb13..d46812f2f0f4 100644
@@ -19,6 +19,7 @@
 
 #define O_PATH               020000000
 #define __O_TMPFILE  040000000
+#define OPENAT2_REGULAR      0100000000
 
 #define F_GETLK64    8
 #define F_SETLK64    9
arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..71940ec9130b 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON    135     /* Memory page has hardware error */
 
+#define EFTYPE               136     /* Wrong file type for the intended operation */
+
 #endif
arch/sparc/include/uapi/asm/fcntl.h +1 -0
index 67dae75e5274..bb6e9fa94bc9 100644
@@ -37,6 +37,7 @@
 
 #define O_PATH               0x1000000
 #define __O_TMPFILE  0x2000000
+#define OPENAT2_REGULAR      0x4000000
 
 #define F_GETOWN     5       /*  for sockets. */
 #define F_SETOWN     6       /*  for sockets. */
fs/ceph/file.c +0 -0
index 66bbf6d517a9..6d8d4c7765e6 100644

fs/ceph/file.c int ceph_atomic_open(struct inode *dir, struct dentry *dentry, · line 983

Quoted 7 lines
fs/ceph/file.c +4 -0
@@ -977,6 +977,10 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
                      ceph_init_inode_acls(newino, &as_ctx);
                      file->f_mode |= FMODE_CREATED;
              }
+             if ((flags & OPENAT2_REGULAR) && !d_is_reg(dentry)) {
+                     err = -EFTYPE;
+                     goto out_req;
+             }

^^^
This doesn't look quite right. Here's a larger chunk of the code:

-------------------------8<--------------------------

        if (d_in_lookup(dentry)) {
                dn = ceph_finish_lookup(req, dentry, err);
                if (IS_ERR(dn))
                        err = PTR_ERR(dn);
        } else {
                /* we were given a hashed negative dentry */
                dn = NULL;
        }
        if (err)
                goto out_req;
        if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
                /* make vfs retry on splice, ENOENT, or symlink */
                doutc(cl, "finish_no_open on dn %p\n", dn);
                err = finish_no_open(file, dn);
        } else {
                if (IS_ENCRYPTED(dir) &&
                    !fscrypt_has_permitted_context(dir, d_inode(dentry))) {
                        pr_warn_client(cl,
                                "Inconsistent encryption context (parent %llx:%llx child %llx:%llx)\n",
                                ceph_vinop(dir), ceph_vinop(d_inode(dentry)));
                        goto out_req;
                }

                doutc(cl, "finish_open on dn %p\n", dn);
                if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
                        struct inode *newino = d_inode(dentry);

                        cache_file_layout(dir, newino);
                        ceph_init_inode_acls(newino, &as_ctx);
                        file->f_mode |= FMODE_CREATED;
                }
                err = finish_open(file, dentry, ceph_open);
        }

-------------------------8<--------------------------

Quoted 9 lines
Quoted 5 lines

It looks like this won't handle it correctly if the pathwalk terminates on a symlink (re: d_is_symlink() case). You should either set up a test ceph cluster on your own, or reach out to the ceph community and ask them to test this.

Thanks for reviewing. The d_is_symlink() case seems to be calling finish_no_open so shouldn't this be okay?

My mistake -- you're correct. I keep forgetting that finish_no_open() will handle this case regardless of what else happens.

Quoted 31 lines
Quoted 18 lines
Quoted 18 lines
fs/ceph/file.c +0 -0
              err = finish_open(file, dentry, ceph_open);
      }
 out_req:
fs/fcntl.c +2 -2
index beab8080badf..240bb511557a 100644
@@ -1169,9 +1169,9 @@ static int __init fcntl_init(void)
       * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
       * is defined as O_NONBLOCK on some platforms and not on others.
       */
-     BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ !=
+     BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
              HWEIGHT32(
-                     (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
+                     (VALID_OPENAT2_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
                      __FMODE_EXEC));
 
Quoted 13 lines

fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 741

Quoted 10 lines
fs/fcntl.c +0 -0
      fasync_cache = kmem_cache_create("fasync_cache",
fs/gfs2/inode.c +1 -0
index 8344040ecaf7..4604e2e8a9cc 100644
@@ -738,6 +738,12 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
      inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
      error = PTR_ERR(inode);
      if (!IS_ERR(inode)) {
+             if (file && (file->f_flags & OPENAT2_REGULAR) && !S_ISREG(inode->i_mode)) {

Isn't OPENAT2_REGULAR getting masked off in ->f_flags now?

Quoted 5 lines

Yes, I thought the masking off was happening after this codepath got executed. Maybe it's better anyway to pass another flags param to this function and forward the flags from the gfs2_atomic_open function and in other call sites pass 0 ? What do you think?

Also my mistake. That happens in do_dentry_open() which happens in finish_open(), so you should be OK here.

#5 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 236 lines · Jeff Layton
Quoted 184 lines · Dorjoy Chowdhury
Quoted 183 lines · Jeff Layton
Quoted 134 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. This is a requested feature from the uapi-group[1].

A corresponding error code EFTYPE has been introduced. For example, if openat2 is called on path /dev/null with OPENAT2_REGULAR in the flag param, it will return -EFTYPE. EFTYPE is already used in BSD systems like FreeBSD, macOS.

When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -EFTYPE is returned.

When OPENAT2_REGULAR is combined with O_DIRECTORY, -EINVAL is returned as it doesn't make sense to open a path that is both a directory and a regular file.

[1]: https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files

---
 arch/alpha/include/uapi/asm/errno.h        |  2 ++
 arch/alpha/include/uapi/asm/fcntl.h        |  1 +
 arch/mips/include/uapi/asm/errno.h         |  2 ++
 arch/parisc/include/uapi/asm/errno.h       |  2 ++
 arch/parisc/include/uapi/asm/fcntl.h       |  1 +
 arch/sparc/include/uapi/asm/errno.h        |  2 ++
 arch/sparc/include/uapi/asm/fcntl.h        |  1 +
 fs/ceph/file.c                             |  4 ++++
 fs/fcntl.c                                 |  4 ++--
 fs/gfs2/inode.c                            |  6 ++++++
 fs/namei.c                                 |  4 ++++
 fs/nfs/dir.c                               |  4 ++++
 fs/open.c                                  |  8 +++++---
 fs/smb/client/dir.c                        | 14 +++++++++++++-
 include/linux/fcntl.h                      |  2 ++
 include/uapi/asm-generic/errno.h           |  2 ++
 include/uapi/asm-generic/fcntl.h           |  4 ++++
 tools/arch/alpha/include/uapi/asm/errno.h  |  2 ++
 tools/arch/mips/include/uapi/asm/errno.h   |  2 ++
 tools/arch/parisc/include/uapi/asm/errno.h |  2 ++
 tools/arch/sparc/include/uapi/asm/errno.h  |  2 ++
 tools/include/uapi/asm-generic/errno.h     |  2 ++
 22 files changed, 67 insertions(+), 6 deletions(-)
arch/alpha/include/uapi/asm/errno.h +2 -0
index 6791f6508632..1a99f38813c7 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON    139     /* Memory page has hardware error */
 
+#define EFTYPE               140     /* Wrong file type for the intended operation */
+
 #endif
arch/alpha/include/uapi/asm/fcntl.h +1 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
 #define O_PATH               040000000
 #define __O_TMPFILE  0100000000
+#define OPENAT2_REGULAR      0200000000
 
 #define F_GETLK              7
 #define F_SETLK              8
arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..1835a50b69ce 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON    168     /* Memory page has hardware error */
 
+#define EFTYPE               169     /* Wrong file type for the intended operation */
+
 #define EDQUOT               1133    /* Quota exceeded */
 
 
arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..93194fbb0a80 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON    257     /* Memory page has hardware error */
 
+#define EFTYPE               258     /* Wrong file type for the intended operation */
+
 #endif
arch/parisc/include/uapi/asm/fcntl.h +1 -0
index 03dee816cb13..d46812f2f0f4 100644
@@ -19,6 +19,7 @@
 
 #define O_PATH               020000000
 #define __O_TMPFILE  040000000
+#define OPENAT2_REGULAR      0100000000
 
 #define F_GETLK64    8
 #define F_SETLK64    9
arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..71940ec9130b 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON    135     /* Memory page has hardware error */
 
+#define EFTYPE               136     /* Wrong file type for the intended operation */
+
 #endif
arch/sparc/include/uapi/asm/fcntl.h +1 -0
index 67dae75e5274..bb6e9fa94bc9 100644
@@ -37,6 +37,7 @@
 
 #define O_PATH               0x1000000
 #define __O_TMPFILE  0x2000000
+#define OPENAT2_REGULAR      0x4000000
 
 #define F_GETOWN     5       /*  for sockets. */
 #define F_SETOWN     6       /*  for sockets. */
fs/ceph/file.c +0 -0
index 66bbf6d517a9..6d8d4c7765e6 100644

fs/ceph/file.c int ceph_atomic_open(struct inode *dir, struct dentry *dentry, · line 983

Quoted 7 lines
fs/ceph/file.c +4 -0
@@ -977,6 +977,10 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
                      ceph_init_inode_acls(newino, &as_ctx);
                      file->f_mode |= FMODE_CREATED;
              }
+             if ((flags & OPENAT2_REGULAR) && !d_is_reg(dentry)) {
+                     err = -EFTYPE;
+                     goto out_req;
+             }

^^^
This doesn't look quite right. Here's a larger chunk of the code:

-------------------------8<--------------------------

        if (d_in_lookup(dentry)) {
                dn = ceph_finish_lookup(req, dentry, err);
                if (IS_ERR(dn))
                        err = PTR_ERR(dn);
        } else {
                /* we were given a hashed negative dentry */
                dn = NULL;
        }
        if (err)
                goto out_req;
        if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
                /* make vfs retry on splice, ENOENT, or symlink */
                doutc(cl, "finish_no_open on dn %p\n", dn);
                err = finish_no_open(file, dn);
        } else {
                if (IS_ENCRYPTED(dir) &&
                    !fscrypt_has_permitted_context(dir, d_inode(dentry))) {
                        pr_warn_client(cl,
                                "Inconsistent encryption context (parent %llx:%llx child %llx:%llx)\n",
                                ceph_vinop(dir), ceph_vinop(d_inode(dentry)));
                        goto out_req;
                }

                doutc(cl, "finish_open on dn %p\n", dn);
                if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
                        struct inode *newino = d_inode(dentry);

                        cache_file_layout(dir, newino);
                        ceph_init_inode_acls(newino, &as_ctx);
                        file->f_mode |= FMODE_CREATED;
                }
                err = finish_open(file, dentry, ceph_open);
        }

-------------------------8<--------------------------

Quoted 9 lines
Quoted 5 lines

It looks like this won't handle it correctly if the pathwalk terminates on a symlink (re: d_is_symlink() case). You should either set up a test ceph cluster on your own, or reach out to the ceph community and ask them to test this.

Thanks for reviewing. The d_is_symlink() case seems to be calling finish_no_open so shouldn't this be okay?

My mistake -- you're correct. I keep forgetting that finish_no_open() will handle this case regardless of what else happens.

Quoted 31 lines
Quoted 18 lines
Quoted 18 lines
fs/ceph/file.c +0 -0
              err = finish_open(file, dentry, ceph_open);
      }
 out_req:
fs/fcntl.c +2 -2
index beab8080badf..240bb511557a 100644
@@ -1169,9 +1169,9 @@ static int __init fcntl_init(void)
       * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
       * is defined as O_NONBLOCK on some platforms and not on others.
       */
-     BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ !=
+     BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
              HWEIGHT32(
-                     (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
+                     (VALID_OPENAT2_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
                      __FMODE_EXEC));
 
Quoted 13 lines

fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 741

Quoted 10 lines
fs/fcntl.c +0 -0
      fasync_cache = kmem_cache_create("fasync_cache",
fs/gfs2/inode.c +1 -0
index 8344040ecaf7..4604e2e8a9cc 100644
@@ -738,6 +738,12 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
      inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
      error = PTR_ERR(inode);
      if (!IS_ERR(inode)) {
+             if (file && (file->f_flags & OPENAT2_REGULAR) && !S_ISREG(inode->i_mode)) {

Isn't OPENAT2_REGULAR getting masked off in ->f_flags now?

Quoted 5 lines

Yes, I thought the masking off was happening after this codepath got executed. Maybe it's better anyway to pass another flags param to this function and forward the flags from the gfs2_atomic_open function and in other call sites pass 0 ? What do you think?

Quoted 4 lines

Also my mistake. That happens in do_dentry_open() which happens in finish_open(), so you should be OK here.

Thanks for patiently reviewing this! I am planning on sending patches for man-pages and looking into some xfs-tests for this. But I am not sure if this patch series will get more reviews from others or if it will be picked up in the vfs branch?

Regards,
Dorjoy

maintainer
#6 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 240 lines · Dorjoy Chowdhury
Quoted 239 lines · Jeff Layton
Quoted 184 lines · Dorjoy Chowdhury
Quoted 183 lines · Jeff Layton
Quoted 134 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. This is a requested feature from the uapi-group[1].

A corresponding error code EFTYPE has been introduced. For example, if openat2 is called on path /dev/null with OPENAT2_REGULAR in the flag param, it will return -EFTYPE. EFTYPE is already used in BSD systems like FreeBSD, macOS.

When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -EFTYPE is returned.

When OPENAT2_REGULAR is combined with O_DIRECTORY, -EINVAL is returned as it doesn't make sense to open a path that is both a directory and a regular file.

[1]: https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files

---
 arch/alpha/include/uapi/asm/errno.h        |  2 ++
 arch/alpha/include/uapi/asm/fcntl.h        |  1 +
 arch/mips/include/uapi/asm/errno.h         |  2 ++
 arch/parisc/include/uapi/asm/errno.h       |  2 ++
 arch/parisc/include/uapi/asm/fcntl.h       |  1 +
 arch/sparc/include/uapi/asm/errno.h        |  2 ++
 arch/sparc/include/uapi/asm/fcntl.h        |  1 +
 fs/ceph/file.c                             |  4 ++++
 fs/fcntl.c                                 |  4 ++--
 fs/gfs2/inode.c                            |  6 ++++++
 fs/namei.c                                 |  4 ++++
 fs/nfs/dir.c                               |  4 ++++
 fs/open.c                                  |  8 +++++---
 fs/smb/client/dir.c                        | 14 +++++++++++++-
 include/linux/fcntl.h                      |  2 ++
 include/uapi/asm-generic/errno.h           |  2 ++
 include/uapi/asm-generic/fcntl.h           |  4 ++++
 tools/arch/alpha/include/uapi/asm/errno.h  |  2 ++
 tools/arch/mips/include/uapi/asm/errno.h   |  2 ++
 tools/arch/parisc/include/uapi/asm/errno.h |  2 ++
 tools/arch/sparc/include/uapi/asm/errno.h  |  2 ++
 tools/include/uapi/asm-generic/errno.h     |  2 ++
 22 files changed, 67 insertions(+), 6 deletions(-)
arch/alpha/include/uapi/asm/errno.h +2 -0
index 6791f6508632..1a99f38813c7 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON    139     /* Memory page has hardware error */
 
+#define EFTYPE               140     /* Wrong file type for the intended operation */
+
 #endif
arch/alpha/include/uapi/asm/fcntl.h +1 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
 #define O_PATH               040000000
 #define __O_TMPFILE  0100000000
+#define OPENAT2_REGULAR      0200000000
 
 #define F_GETLK              7
 #define F_SETLK              8
arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..1835a50b69ce 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON    168     /* Memory page has hardware error */
 
+#define EFTYPE               169     /* Wrong file type for the intended operation */
+
 #define EDQUOT               1133    /* Quota exceeded */
 
 
arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..93194fbb0a80 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON    257     /* Memory page has hardware error */
 
+#define EFTYPE               258     /* Wrong file type for the intended operation */
+
 #endif
arch/parisc/include/uapi/asm/fcntl.h +1 -0
index 03dee816cb13..d46812f2f0f4 100644
@@ -19,6 +19,7 @@
 
 #define O_PATH               020000000
 #define __O_TMPFILE  040000000
+#define OPENAT2_REGULAR      0100000000
 
 #define F_GETLK64    8
 #define F_SETLK64    9
arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..71940ec9130b 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON    135     /* Memory page has hardware error */
 
+#define EFTYPE               136     /* Wrong file type for the intended operation */
+
 #endif
arch/sparc/include/uapi/asm/fcntl.h +1 -0
index 67dae75e5274..bb6e9fa94bc9 100644
@@ -37,6 +37,7 @@
 
 #define O_PATH               0x1000000
 #define __O_TMPFILE  0x2000000
+#define OPENAT2_REGULAR      0x4000000
 
 #define F_GETOWN     5       /*  for sockets. */
 #define F_SETOWN     6       /*  for sockets. */
fs/ceph/file.c +0 -0
index 66bbf6d517a9..6d8d4c7765e6 100644

fs/ceph/file.c int ceph_atomic_open(struct inode *dir, struct dentry *dentry, · line 983

Quoted 7 lines
fs/ceph/file.c +4 -0
@@ -977,6 +977,10 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
                      ceph_init_inode_acls(newino, &as_ctx);
                      file->f_mode |= FMODE_CREATED;
              }
+             if ((flags & OPENAT2_REGULAR) && !d_is_reg(dentry)) {
+                     err = -EFTYPE;
+                     goto out_req;
+             }

^^^
This doesn't look quite right. Here's a larger chunk of the code:

-------------------------8<--------------------------

        if (d_in_lookup(dentry)) {
                dn = ceph_finish_lookup(req, dentry, err);
                if (IS_ERR(dn))
                        err = PTR_ERR(dn);
        } else {
                /* we were given a hashed negative dentry */
                dn = NULL;
        }
        if (err)
                goto out_req;
        if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
                /* make vfs retry on splice, ENOENT, or symlink */
                doutc(cl, "finish_no_open on dn %p\n", dn);
                err = finish_no_open(file, dn);
        } else {
                if (IS_ENCRYPTED(dir) &&
                    !fscrypt_has_permitted_context(dir, d_inode(dentry))) {
                        pr_warn_client(cl,
                                "Inconsistent encryption context (parent %llx:%llx child %llx:%llx)\n",
                                ceph_vinop(dir), ceph_vinop(d_inode(dentry)));
                        goto out_req;
                }

                doutc(cl, "finish_open on dn %p\n", dn);
                if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
                        struct inode *newino = d_inode(dentry);

                        cache_file_layout(dir, newino);
                        ceph_init_inode_acls(newino, &as_ctx);
                        file->f_mode |= FMODE_CREATED;
                }
                err = finish_open(file, dentry, ceph_open);
        }

-------------------------8<--------------------------

Quoted 9 lines
Quoted 5 lines

It looks like this won't handle it correctly if the pathwalk terminates on a symlink (re: d_is_symlink() case). You should either set up a test ceph cluster on your own, or reach out to the ceph community and ask them to test this.

Thanks for reviewing. The d_is_symlink() case seems to be calling finish_no_open so shouldn't this be okay?

My mistake -- you're correct. I keep forgetting that finish_no_open() will handle this case regardless of what else happens.

Quoted 31 lines
Quoted 18 lines
Quoted 18 lines
fs/ceph/file.c +0 -0
              err = finish_open(file, dentry, ceph_open);
      }
 out_req:
fs/fcntl.c +2 -2
index beab8080badf..240bb511557a 100644
@@ -1169,9 +1169,9 @@ static int __init fcntl_init(void)
       * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
       * is defined as O_NONBLOCK on some platforms and not on others.
       */
-     BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ !=
+     BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
              HWEIGHT32(
-                     (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
+                     (VALID_OPENAT2_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
                      __FMODE_EXEC));
 
Quoted 13 lines

fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 741

Quoted 10 lines
fs/fcntl.c +0 -0
      fasync_cache = kmem_cache_create("fasync_cache",
fs/gfs2/inode.c +1 -0
index 8344040ecaf7..4604e2e8a9cc 100644
@@ -738,6 +738,12 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
      inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
      error = PTR_ERR(inode);
      if (!IS_ERR(inode)) {
+             if (file && (file->f_flags & OPENAT2_REGULAR) && !S_ISREG(inode->i_mode)) {

Isn't OPENAT2_REGULAR getting masked off in ->f_flags now?

Quoted 5 lines

Yes, I thought the masking off was happening after this codepath got executed. Maybe it's better anyway to pass another flags param to this function and forward the flags from the gfs2_atomic_open function and in other call sites pass 0 ? What do you think?

Also my mistake. That happens in do_dentry_open() which happens in finish_open(), so you should be OK here.

Quoted 7 lines
Quoted 1 line

Thanks for patiently reviewing this! I am planning on sending patches for man-pages and looking into some xfs-tests for this. But I am not sure if this patch series will get more reviews from others or if it will be picked up in the vfs branch?

This is a change to rather core VFS infrastructure so yes, you should expect some more review. Assuming no major issues are found, then yes, this should eventually get picked up by the VFS maintainers.

Cheers,

Signature · 1 line
Jeff Layton <[email protected]>
#7 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 250 lines · Jeff Layton
Quoted 240 lines · Dorjoy Chowdhury
Quoted 239 lines · Jeff Layton
Quoted 184 lines · Dorjoy Chowdhury
Quoted 183 lines · Jeff Layton
Quoted 134 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. This is a requested feature from the uapi-group[1].

A corresponding error code EFTYPE has been introduced. For example, if openat2 is called on path /dev/null with OPENAT2_REGULAR in the flag param, it will return -EFTYPE. EFTYPE is already used in BSD systems like FreeBSD, macOS.

When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -EFTYPE is returned.

When OPENAT2_REGULAR is combined with O_DIRECTORY, -EINVAL is returned as it doesn't make sense to open a path that is both a directory and a regular file.

[1]: https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files

---
 arch/alpha/include/uapi/asm/errno.h        |  2 ++
 arch/alpha/include/uapi/asm/fcntl.h        |  1 +
 arch/mips/include/uapi/asm/errno.h         |  2 ++
 arch/parisc/include/uapi/asm/errno.h       |  2 ++
 arch/parisc/include/uapi/asm/fcntl.h       |  1 +
 arch/sparc/include/uapi/asm/errno.h        |  2 ++
 arch/sparc/include/uapi/asm/fcntl.h        |  1 +
 fs/ceph/file.c                             |  4 ++++
 fs/fcntl.c                                 |  4 ++--
 fs/gfs2/inode.c                            |  6 ++++++
 fs/namei.c                                 |  4 ++++
 fs/nfs/dir.c                               |  4 ++++
 fs/open.c                                  |  8 +++++---
 fs/smb/client/dir.c                        | 14 +++++++++++++-
 include/linux/fcntl.h                      |  2 ++
 include/uapi/asm-generic/errno.h           |  2 ++
 include/uapi/asm-generic/fcntl.h           |  4 ++++
 tools/arch/alpha/include/uapi/asm/errno.h  |  2 ++
 tools/arch/mips/include/uapi/asm/errno.h   |  2 ++
 tools/arch/parisc/include/uapi/asm/errno.h |  2 ++
 tools/arch/sparc/include/uapi/asm/errno.h  |  2 ++
 tools/include/uapi/asm-generic/errno.h     |  2 ++
 22 files changed, 67 insertions(+), 6 deletions(-)
arch/alpha/include/uapi/asm/errno.h +2 -0
index 6791f6508632..1a99f38813c7 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON    139     /* Memory page has hardware error */
 
+#define EFTYPE               140     /* Wrong file type for the intended operation */
+
 #endif
arch/alpha/include/uapi/asm/fcntl.h +1 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
 #define O_PATH               040000000
 #define __O_TMPFILE  0100000000
+#define OPENAT2_REGULAR      0200000000
 
 #define F_GETLK              7
 #define F_SETLK              8
arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..1835a50b69ce 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON    168     /* Memory page has hardware error */
 
+#define EFTYPE               169     /* Wrong file type for the intended operation */
+
 #define EDQUOT               1133    /* Quota exceeded */
 
 
arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..93194fbb0a80 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON    257     /* Memory page has hardware error */
 
+#define EFTYPE               258     /* Wrong file type for the intended operation */
+
 #endif
arch/parisc/include/uapi/asm/fcntl.h +1 -0
index 03dee816cb13..d46812f2f0f4 100644
@@ -19,6 +19,7 @@
 
 #define O_PATH               020000000
 #define __O_TMPFILE  040000000
+#define OPENAT2_REGULAR      0100000000
 
 #define F_GETLK64    8
 #define F_SETLK64    9
arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..71940ec9130b 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON    135     /* Memory page has hardware error */
 
+#define EFTYPE               136     /* Wrong file type for the intended operation */
+
 #endif
arch/sparc/include/uapi/asm/fcntl.h +1 -0
index 67dae75e5274..bb6e9fa94bc9 100644
@@ -37,6 +37,7 @@
 
 #define O_PATH               0x1000000
 #define __O_TMPFILE  0x2000000
+#define OPENAT2_REGULAR      0x4000000
 
 #define F_GETOWN     5       /*  for sockets. */
 #define F_SETOWN     6       /*  for sockets. */
fs/ceph/file.c +0 -0
index 66bbf6d517a9..6d8d4c7765e6 100644

fs/ceph/file.c int ceph_atomic_open(struct inode *dir, struct dentry *dentry, · line 983

Quoted 7 lines
fs/ceph/file.c +4 -0
@@ -977,6 +977,10 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
                      ceph_init_inode_acls(newino, &as_ctx);
                      file->f_mode |= FMODE_CREATED;
              }
+             if ((flags & OPENAT2_REGULAR) && !d_is_reg(dentry)) {
+                     err = -EFTYPE;
+                     goto out_req;
+             }

^^^
This doesn't look quite right. Here's a larger chunk of the code:

-------------------------8<--------------------------

        if (d_in_lookup(dentry)) {
                dn = ceph_finish_lookup(req, dentry, err);
                if (IS_ERR(dn))
                        err = PTR_ERR(dn);
        } else {
                /* we were given a hashed negative dentry */
                dn = NULL;
        }
        if (err)
                goto out_req;
        if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
                /* make vfs retry on splice, ENOENT, or symlink */
                doutc(cl, "finish_no_open on dn %p\n", dn);
                err = finish_no_open(file, dn);
        } else {
                if (IS_ENCRYPTED(dir) &&
                    !fscrypt_has_permitted_context(dir, d_inode(dentry))) {
                        pr_warn_client(cl,
                                "Inconsistent encryption context (parent %llx:%llx child %llx:%llx)\n",
                                ceph_vinop(dir), ceph_vinop(d_inode(dentry)));
                        goto out_req;
                }

                doutc(cl, "finish_open on dn %p\n", dn);
                if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
                        struct inode *newino = d_inode(dentry);

                        cache_file_layout(dir, newino);
                        ceph_init_inode_acls(newino, &as_ctx);
                        file->f_mode |= FMODE_CREATED;
                }
                err = finish_open(file, dentry, ceph_open);
        }

-------------------------8<--------------------------

Quoted 9 lines
Quoted 5 lines

It looks like this won't handle it correctly if the pathwalk terminates on a symlink (re: d_is_symlink() case). You should either set up a test ceph cluster on your own, or reach out to the ceph community and ask them to test this.

Thanks for reviewing. The d_is_symlink() case seems to be calling finish_no_open so shouldn't this be okay?

My mistake -- you're correct. I keep forgetting that finish_no_open() will handle this case regardless of what else happens.

Quoted 31 lines
Quoted 18 lines
Quoted 18 lines
fs/ceph/file.c +0 -0
              err = finish_open(file, dentry, ceph_open);
      }
 out_req:
fs/fcntl.c +2 -2
index beab8080badf..240bb511557a 100644
@@ -1169,9 +1169,9 @@ static int __init fcntl_init(void)
       * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
       * is defined as O_NONBLOCK on some platforms and not on others.
       */
-     BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ !=
+     BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
              HWEIGHT32(
-                     (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
+                     (VALID_OPENAT2_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
                      __FMODE_EXEC));
 
Quoted 13 lines

fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 741

Quoted 10 lines
fs/fcntl.c +0 -0
      fasync_cache = kmem_cache_create("fasync_cache",
fs/gfs2/inode.c +1 -0
index 8344040ecaf7..4604e2e8a9cc 100644
@@ -738,6 +738,12 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
      inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
      error = PTR_ERR(inode);
      if (!IS_ERR(inode)) {
+             if (file && (file->f_flags & OPENAT2_REGULAR) && !S_ISREG(inode->i_mode)) {

Isn't OPENAT2_REGULAR getting masked off in ->f_flags now?

Quoted 5 lines

Yes, I thought the masking off was happening after this codepath got executed. Maybe it's better anyway to pass another flags param to this function and forward the flags from the gfs2_atomic_open function and in other call sites pass 0 ? What do you think?

Also my mistake. That happens in do_dentry_open() which happens in finish_open(), so you should be OK here.

Quoted 7 lines
Quoted 1 line

Thanks for patiently reviewing this! I am planning on sending patches for man-pages and looking into some xfs-tests for this. But I am not sure if this patch series will get more reviews from others or if it will be picked up in the vfs branch?

Quoted 7 lines

This is a change to rather core VFS infrastructure so yes, you should expect some more review. Assuming no major issues are found, then yes, this should eventually get picked up by the VFS maintainers.

Cheers,
--
Jeff Layton <[email protected]>

Ping....
This patch series got a "Reviewed-by" from Jeff Layton but it probably requires more reviews from other maintainers/reviewers as well. So requesting for review on this patch series. Thanks!

Regards,
Dorjoy

#8 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 256 lines · Dorjoy Chowdhury
Quoted 254 lines · Jeff Layton
Quoted 240 lines · Dorjoy Chowdhury
Quoted 239 lines · Jeff Layton
Quoted 184 lines · Dorjoy Chowdhury
Quoted 183 lines · Jeff Layton
Quoted 134 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. This is a requested feature from the uapi-group[1].

A corresponding error code EFTYPE has been introduced. For example, if openat2 is called on path /dev/null with OPENAT2_REGULAR in the flag param, it will return -EFTYPE. EFTYPE is already used in BSD systems like FreeBSD, macOS.

When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -EFTYPE is returned.

When OPENAT2_REGULAR is combined with O_DIRECTORY, -EINVAL is returned as it doesn't make sense to open a path that is both a directory and a regular file.

[1]: https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files

---
 arch/alpha/include/uapi/asm/errno.h        |  2 ++
 arch/alpha/include/uapi/asm/fcntl.h        |  1 +
 arch/mips/include/uapi/asm/errno.h         |  2 ++
 arch/parisc/include/uapi/asm/errno.h       |  2 ++
 arch/parisc/include/uapi/asm/fcntl.h       |  1 +
 arch/sparc/include/uapi/asm/errno.h        |  2 ++
 arch/sparc/include/uapi/asm/fcntl.h        |  1 +
 fs/ceph/file.c                             |  4 ++++
 fs/fcntl.c                                 |  4 ++--
 fs/gfs2/inode.c                            |  6 ++++++
 fs/namei.c                                 |  4 ++++
 fs/nfs/dir.c                               |  4 ++++
 fs/open.c                                  |  8 +++++---
 fs/smb/client/dir.c                        | 14 +++++++++++++-
 include/linux/fcntl.h                      |  2 ++
 include/uapi/asm-generic/errno.h           |  2 ++
 include/uapi/asm-generic/fcntl.h           |  4 ++++
 tools/arch/alpha/include/uapi/asm/errno.h  |  2 ++
 tools/arch/mips/include/uapi/asm/errno.h   |  2 ++
 tools/arch/parisc/include/uapi/asm/errno.h |  2 ++
 tools/arch/sparc/include/uapi/asm/errno.h  |  2 ++
 tools/include/uapi/asm-generic/errno.h     |  2 ++
 22 files changed, 67 insertions(+), 6 deletions(-)
arch/alpha/include/uapi/asm/errno.h +2 -0
index 6791f6508632..1a99f38813c7 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON    139     /* Memory page has hardware error */
 
+#define EFTYPE               140     /* Wrong file type for the intended operation */
+
 #endif
arch/alpha/include/uapi/asm/fcntl.h +1 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
 #define O_PATH               040000000
 #define __O_TMPFILE  0100000000
+#define OPENAT2_REGULAR      0200000000
 
 #define F_GETLK              7
 #define F_SETLK              8
arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..1835a50b69ce 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON    168     /* Memory page has hardware error */
 
+#define EFTYPE               169     /* Wrong file type for the intended operation */
+
 #define EDQUOT               1133    /* Quota exceeded */
 
 
arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..93194fbb0a80 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON    257     /* Memory page has hardware error */
 
+#define EFTYPE               258     /* Wrong file type for the intended operation */
+
 #endif
arch/parisc/include/uapi/asm/fcntl.h +1 -0
index 03dee816cb13..d46812f2f0f4 100644
@@ -19,6 +19,7 @@
 
 #define O_PATH               020000000
 #define __O_TMPFILE  040000000
+#define OPENAT2_REGULAR      0100000000
 
 #define F_GETLK64    8
 #define F_SETLK64    9
arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..71940ec9130b 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON    135     /* Memory page has hardware error */
 
+#define EFTYPE               136     /* Wrong file type for the intended operation */
+
 #endif
arch/sparc/include/uapi/asm/fcntl.h +1 -0
index 67dae75e5274..bb6e9fa94bc9 100644
@@ -37,6 +37,7 @@
 
 #define O_PATH               0x1000000
 #define __O_TMPFILE  0x2000000
+#define OPENAT2_REGULAR      0x4000000
 
 #define F_GETOWN     5       /*  for sockets. */
 #define F_SETOWN     6       /*  for sockets. */
fs/ceph/file.c +0 -0
index 66bbf6d517a9..6d8d4c7765e6 100644

fs/ceph/file.c int ceph_atomic_open(struct inode *dir, struct dentry *dentry, · line 983

Quoted 7 lines
fs/ceph/file.c +4 -0
@@ -977,6 +977,10 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
                      ceph_init_inode_acls(newino, &as_ctx);
                      file->f_mode |= FMODE_CREATED;
              }
+             if ((flags & OPENAT2_REGULAR) && !d_is_reg(dentry)) {
+                     err = -EFTYPE;
+                     goto out_req;
+             }

^^^
This doesn't look quite right. Here's a larger chunk of the code:

-------------------------8<--------------------------

        if (d_in_lookup(dentry)) {
                dn = ceph_finish_lookup(req, dentry, err);
                if (IS_ERR(dn))
                        err = PTR_ERR(dn);
        } else {
                /* we were given a hashed negative dentry */
                dn = NULL;
        }
        if (err)
                goto out_req;
        if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
                /* make vfs retry on splice, ENOENT, or symlink */
                doutc(cl, "finish_no_open on dn %p\n", dn);
                err = finish_no_open(file, dn);
        } else {
                if (IS_ENCRYPTED(dir) &&
                    !fscrypt_has_permitted_context(dir, d_inode(dentry))) {
                        pr_warn_client(cl,
                                "Inconsistent encryption context (parent %llx:%llx child %llx:%llx)\n",
                                ceph_vinop(dir), ceph_vinop(d_inode(dentry)));
                        goto out_req;
                }

                doutc(cl, "finish_open on dn %p\n", dn);
                if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
                        struct inode *newino = d_inode(dentry);

                        cache_file_layout(dir, newino);
                        ceph_init_inode_acls(newino, &as_ctx);
                        file->f_mode |= FMODE_CREATED;
                }
                err = finish_open(file, dentry, ceph_open);
        }

-------------------------8<--------------------------

Quoted 9 lines
Quoted 5 lines

It looks like this won't handle it correctly if the pathwalk terminates on a symlink (re: d_is_symlink() case). You should either set up a test ceph cluster on your own, or reach out to the ceph community and ask them to test this.

Thanks for reviewing. The d_is_symlink() case seems to be calling finish_no_open so shouldn't this be okay?

My mistake -- you're correct. I keep forgetting that finish_no_open() will handle this case regardless of what else happens.

Quoted 31 lines
Quoted 18 lines
Quoted 18 lines
fs/ceph/file.c +0 -0
              err = finish_open(file, dentry, ceph_open);
      }
 out_req:
fs/fcntl.c +2 -2
index beab8080badf..240bb511557a 100644
@@ -1169,9 +1169,9 @@ static int __init fcntl_init(void)
       * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
       * is defined as O_NONBLOCK on some platforms and not on others.
       */
-     BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ !=
+     BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
              HWEIGHT32(
-                     (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
+                     (VALID_OPENAT2_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
                      __FMODE_EXEC));
 
Quoted 13 lines

fs/gfs2/inode.c static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, · line 741

Quoted 10 lines
fs/fcntl.c +0 -0
      fasync_cache = kmem_cache_create("fasync_cache",
fs/gfs2/inode.c +1 -0
index 8344040ecaf7..4604e2e8a9cc 100644
@@ -738,6 +738,12 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
      inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
      error = PTR_ERR(inode);
      if (!IS_ERR(inode)) {
+             if (file && (file->f_flags & OPENAT2_REGULAR) && !S_ISREG(inode->i_mode)) {

Isn't OPENAT2_REGULAR getting masked off in ->f_flags now?

Quoted 5 lines

Yes, I thought the masking off was happening after this codepath got executed. Maybe it's better anyway to pass another flags param to this function and forward the flags from the gfs2_atomic_open function and in other call sites pass 0 ? What do you think?

Also my mistake. That happens in do_dentry_open() which happens in finish_open(), so you should be OK here.

Quoted 7 lines
Quoted 1 line

Thanks for patiently reviewing this! I am planning on sending patches for man-pages and looking into some xfs-tests for this. But I am not sure if this patch series will get more reviews from others or if it will be picked up in the vfs branch?

This is a change to rather core VFS infrastructure so yes, you should expect some more review. Assuming no major issues are found, then yes, this should eventually get picked up by the VFS maintainers.

Quoted 9 lines
Quoted 3 lines

Cheers,
--
Jeff Layton <[email protected]>

Ping....
This patch series got a "Reviewed-by" from Jeff Layton but it probably requires more reviews from other maintainers/reviewers as well. So requesting for review on this patch series. Thanks!

Ping...
Requesting for review on this patch series please.

Regards,
Dorjoy

Reviewed-by on v6 1/4
#9 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

In reply to Dorjoy Chowdhury
Quoted 15 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. This is a requested feature from the uapi-group[1].

A corresponding error code EFTYPE has been introduced. For example, if openat2 is called on path /dev/null with OPENAT2_REGULAR in the flag param, it will return -EFTYPE. EFTYPE is already used in BSD systems like FreeBSD, macOS.

When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -EFTYPE is returned.

Quoted 8 lines

When OPENAT2_REGULAR is combined with O_DIRECTORY, -EINVAL is returned as it doesn't make sense to open a path that is both a directory and a regular file.

[1]: https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files

---

Aside from the nit below, feel free to take a

fs/open.c static int do_dentry_open(struct file *f, · line 963

Quoted 10 lines
fs/open.c +1 -1
index 681d405bc61e..a6f445f72181 100644
@@ -960,7 +960,7 @@ static int do_dentry_open(struct file *f,
 	if (f->f_mapping->a_ops && f->f_mapping->a_ops->direct_IO)
 		f->f_mode |= FMODE_CAN_ODIRECT;
 
-	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
+	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | OPENAT2_REGULAR);

It's not clear to me why you dropped this, I didn't see a review mentioning it either. (General note: Ideally the cover letter changelog would mention who suggested a change in brackets after the changelog line so it's easier to track where a change might've come from.)

I would personally keep it since O_DIRECTORY is not dropped (I do find it interesting that O_EXCL is dropped too -- you could imagine a userspace program wanting to know that the file was opened with O_EXCL, though it provides you very little information).

Signature · 2 lines
Aleksa Sarai
https://www.cyphar.com/
#10 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 21 lines · Aleksa Sarai
Quoted 19 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. This is a requested feature from the uapi-group[1].

A corresponding error code EFTYPE has been introduced. For example, if openat2 is called on path /dev/null with OPENAT2_REGULAR in the flag param, it will return -EFTYPE. EFTYPE is already used in BSD systems like FreeBSD, macOS.

When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -EFTYPE is returned.

When OPENAT2_REGULAR is combined with O_DIRECTORY, -EINVAL is returned as it doesn't make sense to open a path that is both a directory and a regular file.

Quoted 9 lines
Quoted 4 lines

[1]: https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files

---

Aside from the nit below, feel free to take a

Thanks for reviewing!

Quoted 8 lines
Quoted 8 lines
fs/open.c +0 -0
index 681d405bc61e..a6f445f72181 100644
@@ -960,7 +960,7 @@ static int do_dentry_open(struct file *f,
      if (f->f_mapping->a_ops && f->f_mapping->a_ops->direct_IO)
              f->f_mode |= FMODE_CAN_ODIRECT;
 
Quoted 8 lines

fs/open.c static int do_dentry_open(struct file *f, · line 963

Quoted 2 lines
fs/open.c +1 -1
-     f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
+     f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | OPENAT2_REGULAR);

It's not clear to me why you dropped this, I didn't see a review mentioning it either. (General note: Ideally the cover letter changelog would mention who suggested a change in brackets after the changelog line so it's easier to track where a change might've come from.)

Thanks for the general note. I will keep that in mind.

The review was from Jeff Layton in v5
https://lore.kernel.org/linux-fsdevel/[email protected]/
" 1. OPENAT2_REGULAR leaks into f_flags - do_dentry_open() strips open-time-only flags (O_CREAT|O_EXCL|O_NOCTTY|O_TRUNC)
but does not strip OPENAT2_REGULAR. When a regular file is
successfully opened via openat2() with this flag, the bit
persists in file->f_flags and will be returned by fcntl(fd, F_GETFL)."

I think it makes sense to strip off as OPENAT2_REGULAR is an open time only flag (like O_CREAT and the others already), right?

Regards,
Dorjoy

#11 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 61 lines · Dorjoy Chowdhury
Quoted 21 lines · Aleksa Sarai
Quoted 19 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. This is a requested feature from the uapi-group[1].

A corresponding error code EFTYPE has been introduced. For example, if openat2 is called on path /dev/null with OPENAT2_REGULAR in the flag param, it will return -EFTYPE. EFTYPE is already used in BSD systems like FreeBSD, macOS.

When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -EFTYPE is returned.

When OPENAT2_REGULAR is combined with O_DIRECTORY, -EINVAL is returned as it doesn't make sense to open a path that is both a directory and a regular file.

Quoted 9 lines
Quoted 4 lines

[1]: https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files

---

Aside from the nit below, feel free to take a

Thanks for reviewing!

Quoted 8 lines
Quoted 8 lines
fs/open.c +0 -0
index 681d405bc61e..a6f445f72181 100644
@@ -960,7 +960,7 @@ static int do_dentry_open(struct file *f,
      if (f->f_mapping->a_ops && f->f_mapping->a_ops->direct_IO)
              f->f_mode |= FMODE_CAN_ODIRECT;
 
Quoted 8 lines

fs/open.c static int do_dentry_open(struct file *f, · line 963

Quoted 2 lines
fs/open.c +1 -1
-     f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
+     f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | OPENAT2_REGULAR);

It's not clear to me why you dropped this, I didn't see a review mentioning it either. (General note: Ideally the cover letter changelog would mention who suggested a change in brackets after the changelog line so it's easier to track where a change might've come from.)

Thanks for the general note. I will keep that in mind.

The review was from Jeff Layton in v5
https://lore.kernel.org/linux-fsdevel/[email protected]/
" 1. OPENAT2_REGULAR leaks into f_flags - do_dentry_open() strips open-time-only flags (O_CREAT|O_EXCL|O_NOCTTY|O_TRUNC)
but does not strip OPENAT2_REGULAR. When a regular file is
successfully opened via openat2() with this flag, the bit
persists in file->f_flags and will be returned by fcntl(fd, F_GETFL)."

Quoted 2 lines

I think it makes sense to strip off as OPENAT2_REGULAR is an open time only flag (like O_CREAT and the others already), right?

Well, O_DIRECTORY isn't stripped so if we want to mirror that behaviour then it shouldn't be stripped either IMHO.

O_NOCTTY and O_TRUNC make sense to strip (they are not relevant to the file after it was opened -- truncation only happens at open time and you can always set your controlling TTY later).

The story with O_CREAT and O_EXCL is a bit more complicated. They are stripped but the history there is unclear -- the line was added in Linux 0.98.4(!) with no mention in the release note at the time. (Linus: I wonder if you remember why this was changed at the time? Sorry for the trip down memory lane...)

However, the existence of F_CREATED_QUERY kind of shows that these kinds of checks are stuff that userspace can find handy (though FMODE_CREATED is more useful than O_CREAT|O_EXCL anyway). O_EXCL is used internally for stuff so it can be re-exposed, I'm just not sure it's a good precedent to make a decision based on.

Then again, userspace can check with fstat(2) so it's not the end of the world, but I don't really see a strong reason to hide information from userspace. Since the mail was from Claude (and it tends to give silly nits like that) I'm not sure whether Jeff would agree with my view or not.

Signature · 2 lines
Aleksa Sarai
https://www.cyphar.com/
maintainer
#12 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 84 lines · Aleksa Sarai
Quoted 61 lines · Dorjoy Chowdhury
Quoted 21 lines · Aleksa Sarai
Quoted 19 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. This is a requested feature from the uapi-group[1].

A corresponding error code EFTYPE has been introduced. For example, if openat2 is called on path /dev/null with OPENAT2_REGULAR in the flag param, it will return -EFTYPE. EFTYPE is already used in BSD systems like FreeBSD, macOS.

When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -EFTYPE is returned.

When OPENAT2_REGULAR is combined with O_DIRECTORY, -EINVAL is returned as it doesn't make sense to open a path that is both a directory and a regular file.

Quoted 9 lines
Quoted 4 lines

[1]: https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files

---

Aside from the nit below, feel free to take a

Thanks for reviewing!

Quoted 8 lines
Quoted 8 lines
fs/open.c +0 -0
index 681d405bc61e..a6f445f72181 100644
@@ -960,7 +960,7 @@ static int do_dentry_open(struct file *f,
      if (f->f_mapping->a_ops && f->f_mapping->a_ops->direct_IO)
              f->f_mode |= FMODE_CAN_ODIRECT;
 
Quoted 8 lines

fs/open.c static int do_dentry_open(struct file *f, · line 963

Quoted 2 lines
fs/open.c +1 -1
-     f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
+     f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | OPENAT2_REGULAR);

It's not clear to me why you dropped this, I didn't see a review mentioning it either. (General note: Ideally the cover letter changelog would mention who suggested a change in brackets after the changelog line so it's easier to track where a change might've come from.)

Thanks for the general note. I will keep that in mind.

The review was from Jeff Layton in v5
https://lore.kernel.org/linux-fsdevel/[email protected]/
" 1. OPENAT2_REGULAR leaks into f_flags - do_dentry_open() strips open-time-only flags (O_CREAT|O_EXCL|O_NOCTTY|O_TRUNC)
but does not strip OPENAT2_REGULAR. When a regular file is
successfully opened via openat2() with this flag, the bit
persists in file->f_flags and will be returned by fcntl(fd, F_GETFL)."

Quoted 2 lines

I think it makes sense to strip off as OPENAT2_REGULAR is an open time only flag (like O_CREAT and the others already), right?

Well, O_DIRECTORY isn't stripped so if we want to mirror that behaviour then it shouldn't be stripped either IMHO.

O_NOCTTY and O_TRUNC make sense to strip (they are not relevant to the file after it was opened -- truncation only happens at open time and you can always set your controlling TTY later).

The story with O_CREAT and O_EXCL is a bit more complicated. They are stripped but the history there is unclear -- the line was added in Linux 0.98.4(!) with no mention in the release note at the time. (Linus: I wonder if you remember why this was changed at the time? Sorry for the trip down memory lane...)

However, the existence of F_CREATED_QUERY kind of shows that these kinds of checks are stuff that userspace can find handy (though FMODE_CREATED is more useful than O_CREAT|O_EXCL anyway). O_EXCL is used internally for stuff so it can be re-exposed, I'm just not sure it's a good precedent to make a decision based on.

Quoted 5 lines

Then again, userspace can check with fstat(2) so it's not the end of the world, but I don't really see a strong reason to hide information from userspace. Since the mail was from Claude (and it tends to give silly nits like that) I'm not sure whether Jeff would agree with my view or not.

I don't have a strong feeling either way, but it "feels" like O_REGULAR is not particularly useful to return in F_GETFL.

Once the file is open, then O_REGULAR really doesn't matter anymore. We _know_ it's a regular file at that point or the open wouldn't have happened. F_GETFL is more useful for showing flags that actually affect how the file description works (e.g. O_DIRECT, O_ASYNC, etc.).

Signature · 1 line
Jeff Layton <[email protected]>
#13 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

In reply to Dorjoy Chowdhury

arch/alpha/include/uapi/asm/fcntl.h line 38

Quoted 10 lines · Dorjoy Chowdhury
arch/alpha/include/uapi/asm/fcntl.h +1 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
 #define O_PATH		040000000
 #define __O_TMPFILE	0100000000
+#define OPENAT2_REGULAR	0200000000
 

I don't quite understand why we are adding OPENAT2_REGULAR inside the O_* flag range. Wasn't this supposed to be only supported for openat2()? If so, I don't see the need to waste an O_* flag bit. But maybe I am missing something.

Thanks,
Jori.

#14 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 8 lines · Jori Koolstra
Quoted 6 lines · Dorjoy Chowdhury
arch/alpha/include/uapi/asm/fcntl.h +0 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
Quoted 10 lines

arch/alpha/include/uapi/asm/fcntl.h line 38

Quoted 4 lines
arch/alpha/include/uapi/asm/fcntl.h +1 -0
 #define O_PATH               040000000
 #define __O_TMPFILE  0100000000
+#define OPENAT2_REGULAR      0200000000
 

I don't quite understand why we are adding OPENAT2_REGULAR inside the O_* flag range. Wasn't this supposed to be only supported for openat2()? If so, I don't see the need to waste an O_* flag bit. But maybe I am missing something.

Yes, OPENAT2_REGULAR is only supported for openat2. I am not sure if I got a specific review to not add OPENAT2_REGULAR in the O_* flag 32 bit range. But as far as I understand, for the old open system calls we can't easily add new O_* flags as the older codepaths don't strip off unknown bits which openat2 does. It's not easy to add new O_* flags for the old open system calls since that could break userspace programs. So I guess it's okay to add OPENAT2_REGULAR in the 32 bits range anyway? (Also lots of code paths take 32bit flags param right now and those would need changing to take uint64_t instead but this is of course not a reason to not add the new flag outside of the 32 bits).

Regards,
Dorjoy

#15 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 24 lines

Op 16-04-2026 16:21 CEST schreef Dorjoy Chowdhury <[email protected]>:

Quoted 8 lines · Jori Koolstra
Quoted 6 lines · Dorjoy Chowdhury
arch/alpha/include/uapi/asm/fcntl.h +0 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
Quoted 10 lines

arch/alpha/include/uapi/asm/fcntl.h line 38

Quoted 4 lines
arch/alpha/include/uapi/asm/fcntl.h +1 -0
 #define O_PATH               040000000
 #define __O_TMPFILE  0100000000
+#define OPENAT2_REGULAR      0200000000
 

I don't quite understand why we are adding OPENAT2_REGULAR inside the O_* flag range. Wasn't this supposed to be only supported for openat2()? If so, I don't see the need to waste an O_* flag bit. But maybe I am missing something.

Quoted 7 lines

Yes, OPENAT2_REGULAR is only supported for openat2. I am not sure if I got a specific review to not add OPENAT2_REGULAR in the O_* flag 32 bit range. But as far as I understand, for the old open system calls we can't easily add new O_* flags as the older codepaths don't strip off unknown bits which openat2 does. It's not easy to add new O_* flags for the old open system calls since that could break userspace programs.

If I recall correctly, Aleksa has suggested we might also want to add O_EMPTYPATH to openat() instead of only allowing this for openat2(). I am waiting to see what Christian thinks of this.

I guess in that case it is relatively harmless to change UAPI behavior because openat() with an empty path never works; so it would be silly if there are userspace programs that make this call, which always fails and does nothing, and somehow rely on that.

Quoted 8 lines

So I guess it's okay to add OPENAT2_REGULAR in the 32 bits range anyway? (Also lots of code paths take 32bit flags param right now and those would need changing to take uint64_t instead but this is of course not a reason to not add the new flag outside of the 32 bits).

Regards,
Dorjoy

Thanks,
Jori.

#16 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 32 lines · Jori Koolstra
Quoted 24 lines

Op 16-04-2026 16:21 CEST schreef Dorjoy Chowdhury <[email protected]>:

Quoted 8 lines · Jori Koolstra
Quoted 6 lines · Dorjoy Chowdhury
arch/alpha/include/uapi/asm/fcntl.h +0 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
Quoted 10 lines

arch/alpha/include/uapi/asm/fcntl.h line 38

Quoted 4 lines
arch/alpha/include/uapi/asm/fcntl.h +1 -0
 #define O_PATH               040000000
 #define __O_TMPFILE  0100000000
+#define OPENAT2_REGULAR      0200000000
 

I don't quite understand why we are adding OPENAT2_REGULAR inside the O_* flag range. Wasn't this supposed to be only supported for openat2()? If so, I don't see the need to waste an O_* flag bit. But maybe I am missing something.

Quoted 7 lines

Yes, OPENAT2_REGULAR is only supported for openat2. I am not sure if I got a specific review to not add OPENAT2_REGULAR in the O_* flag 32 bit range. But as far as I understand, for the old open system calls we can't easily add new O_* flags as the older codepaths don't strip off unknown bits which openat2 does. It's not easy to add new O_* flags for the old open system calls since that could break userspace programs.

Quoted 3 lines

If I recall correctly, Aleksa has suggested we might also want to add O_EMPTYPATH to openat() instead of only allowing this for openat2(). I am waiting to see what Christian thinks of this.

We can do that, yes. For O_EMPTYPATH that is workable.

I don't mind too much if we leave OPENAT2_REGUALR in the 32-bit flag space. It'll silently be ignored but the flag name should give it away.

#17 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 11 lines

Op 16-04-2026 17:15 CEST schreef Christian Brauner <[email protected]>:

Quoted 4 lines · Jori Koolstra

If I recall correctly, Aleksa has suggested we might also want to add O_EMPTYPATH to openat() instead of only allowing this for openat2(). I am waiting to see what Christian thinks of this.

We can do that, yes. For O_EMPTYPATH that is workable.

All right, then I'll update the patch this weekend.

Quoted 3 lines

I don't mind too much if we leave OPENAT2_REGUALR in the 32-bit flag space. It'll silently be ignored but the flag name should give it away.

I would also prefer to have the bits separated. Although it is unlikely that we will add so many O_* that we will ever run out of space, it just seems cleaner, and at no cost. But it's not too important.

Thanks,
Jori.

#18 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

In reply to Dorjoy Chowdhury
Quoted 20 lines · Dorjoy Chowdhury
Quoted 8 lines · Jori Koolstra
Quoted 6 lines · Dorjoy Chowdhury
arch/alpha/include/uapi/asm/fcntl.h +0 -0
index 50bdc8e8a271..fe488bf7c18e 100644
@@ -34,6 +34,7 @@
 
Quoted 10 lines

arch/alpha/include/uapi/asm/fcntl.h line 38

Quoted 4 lines
arch/alpha/include/uapi/asm/fcntl.h +1 -0
 #define O_PATH               040000000
 #define __O_TMPFILE  0100000000
+#define OPENAT2_REGULAR      0200000000
 

I don't quite understand why we are adding OPENAT2_REGULAR inside the O_* flag range. Wasn't this supposed to be only supported for openat2()? If so, I don't see the need to waste an O_* flag bit. But maybe I am missing something.

Quoted 11 lines

Yes, OPENAT2_REGULAR is only supported for openat2. I am not sure if I got a specific review to not add OPENAT2_REGULAR in the O_* flag 32 bit range. But as far as I understand, for the old open system calls we can't easily add new O_* flags as the older codepaths don't strip off unknown bits which openat2 does. It's not easy to add new O_* flags for the old open system calls since that could break userspace programs. So I guess it's okay to add OPENAT2_REGULAR in the 32 bits range anyway? (Also lots of code paths take 32bit flags param right now and those would need changing to take uint64_t instead but this is of course not a reason to not add the new flag outside of the 32 bits).

Oh, I didn't notice that this wasn't mentioned here, we had a separate discussion about it in a thread with Jori and I must've assumed we discussed it in both. (My brain is also really not wired up to read large octal values easily.)

While it is hard to add new O_* flags (hence OPENAT2_REGULAR), it's not /impossible/ (Jori has a patch for OPENAT2_EMPTY_PATH that is safe to add to O_* flags because of some fun historical coincidences).

I would have a slight preference towards segregating the bits, ideally at the top end but even 1<<31 would've been nice. Then again, I'm not too fussed either way to be honest...

Signature · 2 lines
Aleksa Sarai
https://www.cyphar.com/
#19 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 4 lines

Op 16-04-2026 17:15 CEST schreef Aleksa Sarai <[email protected]>:

Quoted 8 lines

Oh, I didn't notice that this wasn't mentioned here, we had a separate discussion about it in a thread with Jori and I must've assumed we discussed it in both. (My brain is also really not wired up to read large octal values easily.)

While it is hard to add new O_* flags (hence OPENAT2_REGULAR), it's not /impossible/ (Jori has a patch for OPENAT2_EMPTY_PATH that is safe to add to O_* flags because of some fun historical coincidences).

But it would change userspace, at least in theory, right? If anyone for some reason decided to set whatever the bit will be for O_EMPTYPATH in a call to openat(), and pass an empty string, relying on this to fail, that will no longer be the case. But that is just really silly. Or are you hinting on something else?

Thanks,
Jori.

#20 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 13 lines · Jori Koolstra
Quoted 4 lines

Op 16-04-2026 17:15 CEST schreef Aleksa Sarai <[email protected]>:

Quoted 8 lines

Oh, I didn't notice that this wasn't mentioned here, we had a separate discussion about it in a thread with Jori and I must've assumed we discussed it in both. (My brain is also really not wired up to read large octal values easily.)

While it is hard to add new O_* flags (hence OPENAT2_REGULAR), it's not /impossible/ (Jori has a patch for OPENAT2_EMPTY_PATH that is safe to add to O_* flags because of some fun historical coincidences).

Quoted 5 lines

But it would change userspace, at least in theory, right? If anyone for some reason decided to set whatever the bit will be for O_EMPTYPATH in a call to openat(), and pass an empty string, relying on this to fail, that will no longer be the case. But that is just really silly. Or are you hinting on something else?

Yes, such a program would break, but it is a fairly safe bet that no such program actually exists in the wild. There is a limit to "never break userspace" -- it actually needs to break a real userspace program for it to matter.

Even then there are limits -- in theory someone could write a program that would error out if any new flag is added to any syscall that returns -EINVAL for invalid flags (in fact, we have selftests for openat2(2) that would break because we test the error path) but it wouldn't make sense to not add features to any syscall because such a program could theoretically exist.

We change uAPI all the time, the trick is doing it so that userspace doesn't notice.

For O_EMPTYPATH the logic is that programs that pass regular paths would work the same way as they do now (i.e., LOOKUP_EMPTY semantics) and programs that used to pass "" would previously get ENOENT -- it seems quite unlikely anyone would depend on this for anything (they could check if the string was empty themselves, after all) and it seems astronomically unlikely that that they would pass garbage *and* depend on it for anything.

(It is a little funky that open("", O_EMPTYPATH) would give you an fd to "." but that makes more sense than the alternatives so let's just keep it consistent.)

Signature · 2 lines
Aleksa Sarai
https://www.cyphar.com/
#21 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

In reply to Dorjoy Chowdhury

* Dorjoy Chowdhury:

include/uapi/asm-generic/errno.h line 127

Quoted 11 lines
include/uapi/asm-generic/errno.h +2 -0
index 92e7ae493ee3..bd78e69e0a43 100644
@@ -122,4 +122,6 @@
 
 #define EHWPOISON	133	/* Memory page has hardware error */
 
+#define EFTYPE		134	/* Wrong file type for the intended operation */
+
 #endif

This is what POSIX says about EFTYPE, in the Rationale for System Interfaces:

“
[EFTYPE]
This error code was proposed in earlier proposals as "Inappropriate operation for file type", meaning that the operation requested is not appropriate for the file specified in the function call. This code was proposed, although the same idea was covered by [ENOTTY], because the connotations of the name would be misleading. It was pointed out that the fcntl() function uses the error code [EINVAL] for this notion, and hence all instances of [EFTYPE] were changed to this code.
”

So I'm not sure if reusing this name is a good idea.

Thanks,
Florian

#22 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 30 lines · Florian Weimer

* Dorjoy Chowdhury:

include/uapi/asm-generic/errno.h line 127

Quoted 11 lines
include/uapi/asm-generic/errno.h +2 -0
index 92e7ae493ee3..bd78e69e0a43 100644
@@ -122,4 +122,6 @@
 
 #define EHWPOISON    133     /* Memory page has hardware error */
 
+#define EFTYPE               134     /* Wrong file type for the intended operation */
+
 #endif

This is what POSIX says about EFTYPE, in the Rationale for System Interfaces:

“
[EFTYPE]
This error code was proposed in earlier proposals as "Inappropriate operation for file type", meaning that the operation requested is not appropriate for the file specified in the function call. This code was proposed, although the same idea was covered by [ENOTTY], because the connotations of the name would be misleading. It was pointed out that the fcntl() function uses the error code [EINVAL] for this notion, and hence all instances of [EFTYPE] were changed to this code.
”

Quoted 2 lines

So I'm not sure if reusing this name is a good idea.

Thanks for pointing this out. I had started out the patch series with ENOTREGULAR and it was discussed that EFTYPE was a better and more generic error code which is also used in BSD systems like FreeBSD[1] and MacOS[2]. I also agree that EFTYPE makes sense. We can of course change to something else if everyone agrees.

cc Christian Brauner who originally suggested EFTYPE for input on this.

[1]: https://man.freebsd.org/cgi/man.cgi?errno(2)
[2]: https://developer.apple.com/documentation/foundation/posixerror/eftype

Regards,
Dorjoy

#23 lore raw

Re: [PATCH v6 1/4] openat2: new OPENAT2_REGULAR flag support

Quoted 40 lines · Dorjoy Chowdhury
Quoted 30 lines · Florian Weimer

* Dorjoy Chowdhury:

include/uapi/asm-generic/errno.h line 127

Quoted 11 lines
include/uapi/asm-generic/errno.h +2 -0
index 92e7ae493ee3..bd78e69e0a43 100644
@@ -122,4 +122,6 @@
 
 #define EHWPOISON    133     /* Memory page has hardware error */
 
+#define EFTYPE               134     /* Wrong file type for the intended operation */
+
 #endif

This is what POSIX says about EFTYPE, in the Rationale for System Interfaces:

“
[EFTYPE]
This error code was proposed in earlier proposals as "Inappropriate operation for file type", meaning that the operation requested is not appropriate for the file specified in the function call. This code was proposed, although the same idea was covered by [ENOTTY], because the connotations of the name would be misleading. It was pointed out that the fcntl() function uses the error code [EINVAL] for this notion, and hence all instances of [EFTYPE] were changed to this code.
”

Quoted 2 lines

So I'm not sure if reusing this name is a good idea.

Thanks for pointing this out. I had started out the patch series with ENOTREGULAR and it was discussed that EFTYPE was a better and more generic error code which is also used in BSD systems like FreeBSD[1] and MacOS[2]. I also agree that EFTYPE makes sense. We can of course change to something else if everyone agrees.

Quoted 4 lines

cc Christian Brauner who originally suggested EFTYPE for input on this.

[1]: https://man.freebsd.org/cgi/man.cgi?errno(2)
[2]: https://developer.apple.com/documentation/foundation/posixerror/eftype

Given that both the bsds and macos already use that is there a good reason to return ENOTTY for this other than a standard we ignore most of the time anyway? I'm honestly asking.

#24 lore raw

PATCH v6 2/4 kselftest/openat2: test for OPENAT2_REGULAR flag

In reply to Dorjoy Chowdhury

Just a happy path test.

---
 .../testing/selftests/openat2/openat2_test.c  | 37 ++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
tools/testing/selftests/openat2/openat2_test.c +36 -1
index 0e161ef9e9e4..e8847f7d416c 100644
@@ -320,8 +320,42 @@ void test_openat2_flags(void)
 	}
 }
 
+#ifndef OPENAT2_REGULAR
+#define OPENAT2_REGULAR 040000000
+#endif
+
+#ifndef EFTYPE
+#define EFTYPE 134
+#endif
+
+void test_openat2_regular_flag(void)
+{
+	if (!openat2_supported) {
+		ksft_test_result_skip("Skipping %s as openat2 is not supported\n", __func__);
+		return;
+	}
+
+	struct open_how how = {
+		.flags = OPENAT2_REGULAR | O_RDONLY
+	};
+
+	int fd = sys_openat2(AT_FDCWD, "/dev/null", &how);
+
+	if (fd == -ENOENT) {
+		ksft_test_result_skip("Skipping %s as there is no /dev/null\n", __func__);
+		return;
+	}
+
+	if (fd != -EFTYPE) {
+		ksft_test_result_fail("openat2 should return EFTYPE\n");
+		return;
+	}
+
+	ksft_test_result_pass("%s succeeded\n", __func__);
+}
+
 #define NUM_TESTS (NUM_OPENAT2_STRUCT_VARIATIONS * NUM_OPENAT2_STRUCT_TESTS + \
-		   NUM_OPENAT2_FLAG_TESTS)
+		   NUM_OPENAT2_FLAG_TESTS + 1)
 
 int main(int argc, char **argv)
 {
@@ -330,6 +364,7 @@ int main(int argc, char **argv)
 
 	test_openat2_struct();
 	test_openat2_flags();
+	test_openat2_regular_flag();
 
 	if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0)
 		ksft_exit_fail();
Signature · 1 line
2.53.0
#25 lore raw

PATCH v6 3/4 sparc/fcntl.h: convert O_* flag macros from hex to octal

In reply to Dorjoy Chowdhury

Following the convention in include/uapi/asm-generic/fcntl.h and other architecture specific arch/*/include/uapi/asm/fcntl.h files.

---
 arch/sparc/include/uapi/asm/fcntl.h | 36 ++++++++++++++---------------
 1 file changed, 18 insertions(+), 18 deletions(-)
arch/sparc/include/uapi/asm/fcntl.h +18 -18
index bb6e9fa94bc9..33ce58ec57f6 100644
@@ -2,23 +2,23 @@
 #ifndef _SPARC_FCNTL_H
 #define _SPARC_FCNTL_H
 
-#define O_APPEND	0x0008
-#define FASYNC		0x0040	/* fcntl, for BSD compatibility */
-#define O_CREAT		0x0200	/* not fcntl */
-#define O_TRUNC		0x0400	/* not fcntl */
-#define O_EXCL		0x0800	/* not fcntl */
-#define O_DSYNC		0x2000	/* used to be O_SYNC, see below */
-#define O_NONBLOCK	0x4000
+#define O_APPEND	0000000010
+#define FASYNC		0000000100	/* fcntl, for BSD compatibility */
+#define O_CREAT		0000001000	/* not fcntl */
+#define O_TRUNC		0000002000	/* not fcntl */
+#define O_EXCL		0000004000	/* not fcntl */
+#define O_DSYNC		0000020000	/* used to be O_SYNC, see below */
+#define O_NONBLOCK	0000040000
 #if defined(__sparc__) && defined(__arch64__)
-#define O_NDELAY	0x0004
+#define O_NDELAY	0000000004
 #else
-#define O_NDELAY	(0x0004 | O_NONBLOCK)
+#define O_NDELAY	(0000000004 | O_NONBLOCK)
 #endif
-#define O_NOCTTY	0x8000	/* not fcntl */
-#define O_LARGEFILE	0x40000
-#define O_DIRECT        0x100000 /* direct disk access hint */
-#define O_NOATIME	0x200000
-#define O_CLOEXEC	0x400000
+#define O_NOCTTY	0000100000	/* not fcntl */
+#define O_LARGEFILE	0001000000
+#define O_DIRECT        0004000000 /* direct disk access hint */
+#define O_NOATIME	0010000000
+#define O_CLOEXEC	0020000000
 /*
  * Before Linux 2.6.33 only O_DSYNC semantics were implemented, but using
  * the O_SYNC flag.  We continue to use the existing numerical value
@@ -32,12 +32,12 @@
  *
  * Note: __O_SYNC must never be used directly.
  */
-#define __O_SYNC	0x800000
+#define __O_SYNC	0040000000
 #define O_SYNC		(__O_SYNC|O_DSYNC)
 
-#define O_PATH		0x1000000
-#define __O_TMPFILE	0x2000000
-#define OPENAT2_REGULAR	0x4000000
+#define O_PATH		0100000000
+#define __O_TMPFILE	0200000000
+#define OPENAT2_REGULAR	0400000000
 
 #define F_GETOWN	5	/*  for sockets. */
 #define F_SETOWN	6	/*  for sockets. */
Signature · 1 line
2.53.0
#26 lore raw

PATCH v6 4/4 mips/fcntl.h: convert O_* flag macros from hex to octal

In reply to Dorjoy Chowdhury

Following the convention in include/uapi/asm-generic/fcntl.h and other architecture specific arch/*/include/uapi/asm/fcntl.h files.

---
 arch/mips/include/uapi/asm/fcntl.h | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
arch/mips/include/uapi/asm/fcntl.h +11 -11
index 0369a38e3d4f..6aa3f49df17e 100644
@@ -11,15 +11,15 @@
 
 #include <asm/sgidefs.h>
 
-#define O_APPEND	0x0008
-#define O_DSYNC		0x0010	/* used to be O_SYNC, see below */
-#define O_NONBLOCK	0x0080
-#define O_CREAT		0x0100	/* not fcntl */
-#define O_TRUNC		0x0200	/* not fcntl */
-#define O_EXCL		0x0400	/* not fcntl */
-#define O_NOCTTY	0x0800	/* not fcntl */
-#define FASYNC		0x1000	/* fcntl, for BSD compatibility */
-#define O_LARGEFILE	0x2000	/* allow large file opens */
+#define O_APPEND	0000010
+#define O_DSYNC		0000020	/* used to be O_SYNC, see below */
+#define O_NONBLOCK	0000200
+#define O_CREAT		0000400	/* not fcntl */
+#define O_TRUNC		0001000	/* not fcntl */
+#define O_EXCL		0002000	/* not fcntl */
+#define O_NOCTTY	0004000	/* not fcntl */
+#define FASYNC		0010000	/* fcntl, for BSD compatibility */
+#define O_LARGEFILE	0020000	/* allow large file opens */
 /*
  * Before Linux 2.6.33 only O_DSYNC semantics were implemented, but using
  * the O_SYNC flag.  We continue to use the existing numerical value
@@ -33,9 +33,9 @@
  *
  * Note: __O_SYNC must never be used directly.
  */
-#define __O_SYNC	0x4000
+#define __O_SYNC	0040000
 #define O_SYNC		(__O_SYNC|O_DSYNC)
-#define O_DIRECT	0x8000	/* direct disk access hint */
+#define O_DIRECT	0100000	/* direct disk access hint */
 
 #define F_GETLK		14
 #define F_SETLK		6
Signature · 1 line
2.53.0
maintainer Applied
#27 lore raw
In reply to Dorjoy Chowdhury
Quoted 13 lines · Dorjoy Chowdhury

I came upon this "Ability to only open regular files" uapi feature suggestion from https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files
and thought it would be something I could do as a first patch and get to know the kernel code a bit better.

The following filesystems have been tested by building and booting the kernel x86 bzImage in a Fedora 43 VM in QEMU. I have tested with OPENAT2_REGULAR that regular files can be successfully opened and non-regular files (directory, fifo etc) return -EFTYPE.
- btrfs
- NFS (loopback)
- SMB (loopback)

Quoted 1 line

[...]

- I've added an explanation why OPENAT2_REGULAR is only needed for some ->atomic_open() implementers but not others. What I don't like is that we need all that custom handling in there but it's managable.

- I dropped the topmost style conversions. They really don't belong there and if we switch to something better we should use (1 << <nr>).

- I split the EFTYPE errno introduction into a separate patch.

---

Applied to the vfs-7.2.openat.regular branch of the vfs/vfs.git tree. Patches in the vfs-7.2.openat.regular branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: master

[1/4] openat2: new OPENAT2_REGULAR flag support

      https://git.kernel.org/vfs/vfs/c/0b649c4d70f7
[2/4] kselftest/openat2: test for OPENAT2_REGULAR flag
      https://git.kernel.org/vfs/vfs/c/d7dc36df8fa7

[3/4] sparc/fcntl.h: convert O_* flag macros from hex to octal
(dropped)
[4/4] mips/fcntl.h: convert O_* flag macros from hex to octal
(dropped)

#28 lore raw
Quoted 21 lines · Christian Brauner
Quoted 13 lines · Dorjoy Chowdhury

I came upon this "Ability to only open regular files" uapi feature suggestion from https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files
and thought it would be something I could do as a first patch and get to know the kernel code a bit better.

The following filesystems have been tested by building and booting the kernel x86 bzImage in a Fedora 43 VM in QEMU. I have tested with OPENAT2_REGULAR that regular files can be successfully opened and non-regular files (directory, fifo etc) return -EFTYPE.
- btrfs
- NFS (loopback)
- SMB (loopback)

Quoted 1 line

[...]

- I've added an explanation why OPENAT2_REGULAR is only needed for some ->atomic_open() implementers but not others. What I don't like is that we need all that custom handling in there but it's managable.

Quoted 6 lines

- I dropped the topmost style conversions. They really don't belong there and if we switch to something better we should use (1 << <nr>).

- I split the EFTYPE errno introduction into a separate patch.

---

Thanks for fixing up and picking this one up!

Quoted 4 lines

Applied to the vfs-7.2.openat.regular branch of the vfs/vfs.git tree. Patches in the vfs-7.2.openat.regular branch should appear in linux-next soon.

I don't see a vfs-7.2.openat.regular branch in vfs/vfs.git tree in git.kernel.org. Maybe this hasn't been pushed yet?

Quoted 12 lines

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: master

I guess you wanted to mean vfs-7.2.openat.regular here?

Regards,
Dorjoy

#29 lore raw
Quoted 32 lines · Dorjoy Chowdhury
Quoted 21 lines · Christian Brauner
Quoted 13 lines · Dorjoy Chowdhury

I came upon this "Ability to only open regular files" uapi feature suggestion from https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files
and thought it would be something I could do as a first patch and get to know the kernel code a bit better.

The following filesystems have been tested by building and booting the kernel x86 bzImage in a Fedora 43 VM in QEMU. I have tested with OPENAT2_REGULAR that regular files can be successfully opened and non-regular files (directory, fifo etc) return -EFTYPE.
- btrfs
- NFS (loopback)
- SMB (loopback)

Quoted 1 line

[...]

- I've added an explanation why OPENAT2_REGULAR is only needed for some ->atomic_open() implementers but not others. What I don't like is that we need all that custom handling in there but it's managable.

Quoted 6 lines

- I dropped the topmost style conversions. They really don't belong there and if we switch to something better we should use (1 << <nr>).

- I split the EFTYPE errno introduction into a separate patch.

---

Thanks for fixing up and picking this one up!

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Applied to the vfs-7.2.openat.regular branch of the vfs/vfs.git tree. Patches in the vfs-7.2.openat.regular branch should appear in linux-next soon.

I don't see a vfs-7.2.openat.regular branch in vfs/vfs.git tree in git.kernel.org. Maybe this hasn't been pushed yet?

Nothing will get pushed prior to -rc1 which is due this Sunday.

#30 lore raw
Quoted 34 lines · Christian Brauner
Quoted 32 lines · Dorjoy Chowdhury
Quoted 21 lines · Christian Brauner
Quoted 13 lines · Dorjoy Chowdhury

I came upon this "Ability to only open regular files" uapi feature suggestion from https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files
and thought it would be something I could do as a first patch and get to know the kernel code a bit better.

The following filesystems have been tested by building and booting the kernel x86 bzImage in a Fedora 43 VM in QEMU. I have tested with OPENAT2_REGULAR that regular files can be successfully opened and non-regular files (directory, fifo etc) return -EFTYPE.
- btrfs
- NFS (loopback)
- SMB (loopback)

Quoted 1 line

[...]

- I've added an explanation why OPENAT2_REGULAR is only needed for some ->atomic_open() implementers but not others. What I don't like is that we need all that custom handling in there but it's managable.

Quoted 6 lines

- I dropped the topmost style conversions. They really don't belong there and if we switch to something better we should use (1 << <nr>).

- I split the EFTYPE errno introduction into a separate patch.

---

Thanks for fixing up and picking this one up!

Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Applied to the vfs-7.2.openat.regular branch of the vfs/vfs.git tree. Patches in the vfs-7.2.openat.regular branch should appear in linux-next soon.

I don't see a vfs-7.2.openat.regular branch in vfs/vfs.git tree in git.kernel.org. Maybe this hasn't been pushed yet?

Nothing will get pushed prior to -rc1 which is due this Sunday.

Understood. Thanks!

Regards,
Dorjoy

#31 lore raw
In reply to Christian Brauner
Quoted 16 lines · Christian Brauner
Quoted 13 lines · Dorjoy Chowdhury

I came upon this "Ability to only open regular files" uapi feature suggestion from https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files
and thought it would be something I could do as a first patch and get to know the kernel code a bit better.

The following filesystems have been tested by building and booting the kernel x86 bzImage in a Fedora 43 VM in QEMU. I have tested with OPENAT2_REGULAR that regular files can be successfully opened and non-regular files (directory, fifo etc) return -EFTYPE.
- btrfs
- NFS (loopback)
- SMB (loopback)

Quoted 1 line

[...]

Quoted 8 lines

- I've added an explanation why OPENAT2_REGULAR is only needed for some ->atomic_open() implementers but not others. What I don't like is that we need all that custom handling in there but it's managable.

- I dropped the topmost style conversions. They really don't belong there and if we switch to something better we should use (1 << <nr>).

- I split the EFTYPE errno introduction into a separate patch.

So I've massaged this series a bit in that I moved OPENAT2_REGULAR into the upper 64-bit and internally use a __O_REGULAR bit. After having thought about it makes a lot more sense to move the openat2() only features into the upper 32-bit for the uapi space. I also ported the selftests to the TEST* framework to fit with Aleksa's recent rework.

#32 lore raw
Quoted 25 lines · Christian Brauner
Quoted 23 lines · Christian Brauner
Quoted 13 lines · Dorjoy Chowdhury

I came upon this "Ability to only open regular files" uapi feature suggestion from https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files
and thought it would be something I could do as a first patch and get to know the kernel code a bit better.

The following filesystems have been tested by building and booting the kernel x86 bzImage in a Fedora 43 VM in QEMU. I have tested with OPENAT2_REGULAR that regular files can be successfully opened and non-regular files (directory, fifo etc) return -EFTYPE.
- btrfs
- NFS (loopback)
- SMB (loopback)

Quoted 1 line

[...]

- I've added an explanation why OPENAT2_REGULAR is only needed for some ->atomic_open() implementers but not others. What I don't like is that we need all that custom handling in there but it's managable.

- I dropped the topmost style conversions. They really don't belong there and if we switch to something better we should use (1 << <nr>).

Quoted 7 lines
Quoted 1 line

- I split the EFTYPE errno introduction into a separate patch.

So I've massaged this series a bit in that I moved OPENAT2_REGULAR into the upper 64-bit and internally use a __O_REGULAR bit. After having thought about it makes a lot more sense to move the openat2() only features into the upper 32-bit for the uapi space. I also ported the selftests to the TEST* framework to fit with Aleksa's recent rework.

Thanks for fixing up!

Regards,
Dorjoy