Skip to contentSkip to conversation

O_REGULAR flag support for open

PATCH Superseded Dorjoy Chowdhury 4 participants 8 messages Jan 26

Subsystems (8)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover O_REGULAR flag support for open

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/

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.

I am not quite sure if the semantics that I baked into the code for this O_REGULAR flag's behavior when combined with other flags like O_CREAT look good and if there are other places that need the checks. I can fixup my patch according to suggestions for improvement. I did some happy path testing and the O_REGULAR flag seems to work as intended.

Thanks.

Regards,
Dorjoy

Dorjoy Chowdhury (2):
open: new O_REGULAR flag support
kselftest/openat2: test for O_REGULAR flag

 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/fcntl.c                                    |  2 +-
 fs/namei.c                                    |  6 +++
 fs/open.c                                     |  4 +-
 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 ++++++++++++++++++-
 19 files changed, 74 insertions(+), 4 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v2 1/2 open: new O_REGULAR flag support

This flag indicates the path should be opened if it's a regular file. A relevant error code ENOTREGULAR(35) has been introduced. For example, if open is called on path /dev/null with O_REGULAR in the flag param, it will return -ENOTREGULAR.

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, -ENOTREGULAR is returned.

-EINVAL is returned when O_REGULAR is combined with O_DIRECTORY (not part of O_TMPFILE) because it doesn't make sense to open a path that is both a directory and a regular file.

---
 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/fcntl.c                                 | 2 +-
 fs/namei.c                                 | 6 ++++++
 fs/open.c                                  | 4 +++-
 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 ++
 18 files changed, 38 insertions(+), 3 deletions(-)
arch/alpha/include/uapi/asm/errno.h +2 -0
index 6791f6508632..8bbcaa9024f9 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON	139	/* Memory page has hardware error */
 
+#define ENOTREG		140	/* Not a regular file */
+
 #endif
arch/alpha/include/uapi/asm/fcntl.h +1 -0
index 50bdc8e8a271..4da5a64c23bd 100644
@@ -34,6 +34,7 @@
 
 #define O_PATH		040000000
 #define __O_TMPFILE	0100000000
+#define O_REGULAR	0200000000
 
 #define F_GETLK		7
 #define F_SETLK		8
arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..293c78777254 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON	168	/* Memory page has hardware error */
 
+#define ENOTREG		169	/* Not a regular file */
+
 #define EDQUOT		1133	/* Quota exceeded */
 
 
arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..442917484f99 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON	257	/* Memory page has hardware error */
 
+#define ENOTREG		258	/* Not a regular file */
+
 #endif
arch/parisc/include/uapi/asm/fcntl.h +1 -0
index 03dee816cb13..efd763335ff7 100644
@@ -19,6 +19,7 @@
 
 #define O_PATH		020000000
 #define __O_TMPFILE	040000000
+#define O_REGULAR	060000000
 
 #define F_GETLK64	8
 #define F_SETLK64	9
arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..8dce0bfeab74 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON	135	/* Memory page has hardware error */
 
+#define ENOTREG		136	/* Not a regular file */
+
 #endif
arch/sparc/include/uapi/asm/fcntl.h +1 -0
index 67dae75e5274..a93d18d2c23e 100644
@@ -37,6 +37,7 @@
 
 #define O_PATH		0x1000000
 #define __O_TMPFILE	0x2000000
+#define O_REGULAR	0x4000000
 
 #define F_GETOWN	5	/*  for sockets. */
 #define F_SETOWN	6	/*  for sockets. */
fs/fcntl.c +1 -1
index f93dbca08435..62ab4ad2b6f5 100644
@@ -1169,7 +1169,7 @@ 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)) |
 			__FMODE_EXEC));
fs/namei.c +6 -0
index b28ecb699f32..f5504ae4b03c 100644
@@ -4616,6 +4616,10 @@ static int do_open(struct nameidata *nd,
 		if (unlikely(error))
 			return error;
 	}
+
+	if ((open_flag & O_REGULAR) && !d_is_reg(nd->path.dentry))
+		return -ENOTREG;
+
 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
 		return -ENOTDIR;
 
@@ -4765,6 +4769,8 @@ static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
 	struct path path;
 	int error = path_lookupat(nd, flags, &path);
 	if (!error) {
+		if ((file->f_flags & O_REGULAR) && !d_is_reg(path.dentry))
+			return -ENOTREG;
 		audit_inode(nd->name, path.dentry, 0);
 		error = vfs_open(&path, file);
 		path_put(&path);
fs/open.c +3 -1
index 74c4c1462b3e..82153e21907e 100644
@@ -1173,7 +1173,7 @@ struct file *kernel_file_open(const struct path *path, int flags,
 EXPORT_SYMBOL_GPL(kernel_file_open);
 
 #define WILL_CREATE(flags)	(flags & (O_CREAT | __O_TMPFILE))
-#define O_PATH_FLAGS		(O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC)
+#define O_PATH_FLAGS		(O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC | O_REGULAR)
 
 inline struct open_how build_open_how(int flags, umode_t mode)
 {
@@ -1250,6 +1250,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 & O_REGULAR)) {
+		return -EINVAL;
 	}
 	if (flags & O_PATH) {
 		/* O_PATH only permits certain other flags to be set. */
include/linux/fcntl.h +1 -1
index a332e79b3207..4fd07b0e0a17 100644
@@ -10,7 +10,7 @@
 	(O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | \
 	 O_APPEND | O_NDELAY | O_NONBLOCK | __O_SYNC | O_DSYNC | \
 	 FASYNC	| O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \
-	 O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE)
+	 O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE | O_REGULAR)
 
 /* List of all valid flags for the how->resolve argument: */
 #define VALID_RESOLVE_FLAGS \
include/uapi/asm-generic/errno.h +2 -0
index 92e7ae493ee3..2216ab9aa32e 100644
@@ -122,4 +122,6 @@
 
 #define EHWPOISON	133	/* Memory page has hardware error */
 
+#define ENOTREG		134	/* Not a regular file */
+
 #endif
include/uapi/asm-generic/fcntl.h +4 -0
index 613475285643..3468b352a575 100644
@@ -88,6 +88,10 @@
 #define __O_TMPFILE	020000000
 #endif
 
+#ifndef O_REGULAR
+#define O_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..8bbcaa9024f9 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON	139	/* Memory page has hardware error */
 
+#define ENOTREG		140	/* Not a regular file */
+
 #endif
tools/arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..293c78777254 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON	168	/* Memory page has hardware error */
 
+#define ENOTREG		169	/* Not a regular file */
+
 #define EDQUOT		1133	/* Quota exceeded */
 
 
tools/arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..442917484f99 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON	257	/* Memory page has hardware error */
 
+#define ENOTREG		258	/* Not a regular file */
+
 #endif
tools/arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..8dce0bfeab74 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON	135	/* Memory page has hardware error */
 
+#define ENOTREG		136	/* Not a regular file */
+
 #endif
tools/include/uapi/asm-generic/errno.h +2 -0
index 92e7ae493ee3..2216ab9aa32e 100644
@@ -122,4 +122,6 @@
 
 #define EHWPOISON	133	/* Memory page has hardware error */
 
+#define ENOTREG		134	/* Not a regular file */
+
 #endif
Signature · 1 line
2.52.0
maintainer
#2 lore raw

Re: [PATCH v2 1/2] open: new O_REGULAR flag support

arch/parisc/include/uapi/asm/fcntl.h line 22

Quoted 10 lines · Dorjoy Chowdhury
arch/parisc/include/uapi/asm/fcntl.h +1 -0
index 03dee816cb13..efd763335ff7 100644
@@ -19,6 +19,7 @@
 
 #define O_PATH		020000000
 #define __O_TMPFILE	040000000
+#define O_REGULAR	060000000

This is two bits, not one, and it overlaps with O_PATH|__O_TMPFILE.

The other ones look like they are fine in this regard, but I'm still unsure if we should be using the next available bit, or reusing an unused lower bit, e.g. these bits removed in commit 41f5a81c07cd ("parisc: Drop HP-UX specific fcntl and signal flags"):

-#define O_BLKSEEK 000000100 /* HPUX only */
-#define O_RSYNC 002000000 /* HPUX only */
-#define O_INVISIBLE 004000000 /* invisible I/O, for DMAPI/XDSM */

Arnd

#3 lore raw

Re: [PATCH v2 1/2] open: new O_REGULAR flag support

Quoted 10 lines · Arnd Bergmann
Quoted 7 lines · Dorjoy Chowdhury
arch/parisc/include/uapi/asm/fcntl.h +0 -0
index 03dee816cb13..efd763335ff7 100644
@@ -19,6 +19,7 @@
 
Quoted 6 lines

arch/parisc/include/uapi/asm/fcntl.h line 22

Quoted 3 lines
arch/parisc/include/uapi/asm/fcntl.h +1 -0
 #define O_PATH               020000000
 #define __O_TMPFILE  040000000
+#define O_REGULAR    060000000

This is two bits, not one, and it overlaps with O_PATH|__O_TMPFILE.

oh right! Thanks for catching that.

Review

Quoted 9 lines

The other ones look like they are fine in this regard, but I'm still unsure if we should be using the next available bit, or reusing an unused lower bit, e.g. these bits removed in commit 41f5a81c07cd ("parisc: Drop HP-UX specific fcntl and signal flags"):

(quoted diff) +0 -3
-#define O_BLKSEEK      000000100 /* HPUX only */
-#define O_RSYNC                002000000 /* HPUX only */
-#define O_INVISIBLE    004000000 /* invisible I/O, for DMAPI/XDSM */

So should I use 000000100 or 0100000000 for parisc/*/fcntl.h (other files are fine I guess) in v3? I am not sure if there's any objective reason to use one over the other.

Regards,
Dorjoy

reviewer
#4 lore raw

Re: [PATCH v2 1/2] open: new O_REGULAR flag support

In reply to Dorjoy Chowdhury
Quoted 9 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. A relevant error code ENOTREGULAR(35) has been introduced. For example, if open is called on path /dev/null with O_REGULAR in the flag param, it will return -ENOTREGULAR.

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, -ENOTREGULAR is returned.

Quoted 5 lines

-EINVAL is returned when O_REGULAR is combined with O_DIRECTORY (not part of O_TMPFILE) because it doesn't make sense to open a path that is both a directory and a regular file.

The feature looks useful to me (but the justification with the danger of being tricked into opening some device nodes would be nice to have here in the changelog).

arch/parisc/include/uapi/asm/fcntl.h line 22

Quoted 9 lines
arch/parisc/include/uapi/asm/fcntl.h +1 -0
index 03dee816cb13..efd763335ff7 100644
@@ -19,6 +19,7 @@
 
 #define O_PATH	020000000
 #define __O_TMPFILE	040000000
+#define O_REGULAR	060000000

This looks wrong? O_REGULAR is overlapping with O_PATH and __O_TMPFILE???

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw

Re: [PATCH v2 1/2] open: new O_REGULAR flag support

Quoted 15 lines · Jan Kara
Quoted 13 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. A relevant error code ENOTREGULAR(35) has been introduced. For example, if open is called on path /dev/null with O_REGULAR in the flag param, it will return -ENOTREGULAR.

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, -ENOTREGULAR is returned.

-EINVAL is returned when O_REGULAR is combined with O_DIRECTORY (not part of O_TMPFILE) because it doesn't make sense to open a path that is both a directory and a regular file.

Quoted 6 lines
Quoted 1 line

The feature looks useful to me (but the justification with the danger of being tricked into opening some device nodes would be nice to have here in the changelog).

Good suggestion. Will put something related to this in v3 submission.

Quoted 6 lines
Quoted 6 lines
arch/parisc/include/uapi/asm/fcntl.h +0 -0
index 03dee816cb13..efd763335ff7 100644
@@ -19,6 +19,7 @@
 
Quoted 6 lines

arch/parisc/include/uapi/asm/fcntl.h line 22

Quoted 3 lines
arch/parisc/include/uapi/asm/fcntl.h +1 -0
 #define O_PATH       020000000
 #define __O_TMPFILE  040000000
+#define O_REGULAR    060000000

This looks wrong? O_REGULAR is overlapping with O_PATH and __O_TMPFILE???

Yes, this is wrong (pointed out by Arnd as well -
https://lore.kernel.org/linux-fsdevel/[email protected]/).
Arnd also pointed out if we should use an earlier bit (e.g., 000000100) or use a new one (e.g., 0100000000) which I asked about in the thread. Please let us know in the thread if you have any suggestions on this so that we can all be on the same page. Thanks!

Regards,
Dorjoy

#6 lore raw

Re: [PATCH v2 1/2] open: new O_REGULAR flag support

In reply to Dorjoy Chowdhury
Quoted 5 lines · Dorjoy Chowdhury

This flag indicates the path should be opened if it's a regular file. A relevant error code ENOTREGULAR(35) has been introduced. For example, if open is called on path /dev/null with O_REGULAR in the flag param, it will return -ENOTREGULAR.

This appears to be ENOTREG in the actual patch?

It seems possible to use ENOTREG=140 to avoid collisions for _most_ of the architectures, leaving only parisc and mips with different values (which are mostly obsolete anyway).

Similarly, it seems possible to use O_REGULAR 0400000000 to avoid conflicts for most (all?) architectures as well, instead of having a different value for several of them.

While it isn't _required_ to have the same values across architectures, it doesn't hurt and makes human identification of these values easier.

Cheers, Andreas

Quoted 271 lines

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, -ENOTREGULAR is returned.

-EINVAL is returned when O_REGULAR is combined with O_DIRECTORY (not part of O_TMPFILE) because it doesn't make sense to open a path that is both a directory and a regular file.

---

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/fcntl.c                                 | 2 +-
fs/namei.c                                 | 6 ++++++
fs/open.c                                  | 4 +++-
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 ++
18 files changed, 38 insertions(+), 3 deletions(-)
arch/alpha/include/uapi/asm/errno.h +2 -0
index 6791f6508632..8bbcaa9024f9 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON 139 /* Memory page has hardware error */
 
+#define ENOTREG 140 /* Not a regular file */
+
 #endif
arch/alpha/include/uapi/asm/fcntl.h +1 -0
index 50bdc8e8a271..4da5a64c23bd 100644
@@ -34,6 +34,7 @@
 
 #define O_PATH 040000000
 #define __O_TMPFILE 0100000000
+#define O_REGULAR 0200000000
 
 #define F_GETLK 7
 #define F_SETLK 8
arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..293c78777254 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON 168 /* Memory page has hardware error */
 
+#define ENOTREG 169 /* Not a regular file */
+
 #define EDQUOT 1133 /* Quota exceeded */
 
 
arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..442917484f99 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON 257 /* Memory page has hardware error */
 
+#define ENOTREG 258 /* Not a regular file */
+
 #endif
arch/parisc/include/uapi/asm/fcntl.h +1 -0
index 03dee816cb13..efd763335ff7 100644
@@ -19,6 +19,7 @@
 
 #define O_PATH 020000000
 #define __O_TMPFILE 040000000
+#define O_REGULAR 060000000
 
 #define F_GETLK64 8
 #define F_SETLK64 9
arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..8dce0bfeab74 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON 135 /* Memory page has hardware error */
 
+#define ENOTREG 136 /* Not a regular file */
+
 #endif
arch/sparc/include/uapi/asm/fcntl.h +1 -0
index 67dae75e5274..a93d18d2c23e 100644
@@ -37,6 +37,7 @@
 
 #define O_PATH 0x1000000
 #define __O_TMPFILE 0x2000000
+#define O_REGULAR 0x4000000
 
 #define F_GETOWN 5 /*  for sockets. */
 #define F_SETOWN 6 /*  for sockets. */
fs/fcntl.c +1 -1
index f93dbca08435..62ab4ad2b6f5 100644
@@ -1169,7 +1169,7 @@ 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)) |
__FMODE_EXEC));

fs/namei.c +6 -0
index b28ecb699f32..f5504ae4b03c 100644
@@ -4616,6 +4616,10 @@ static int do_open(struct nameidata *nd,
 if (unlikely(error))
 return error;
 }
+
+ if ((open_flag & O_REGULAR) && !d_is_reg(nd->path.dentry))
+ return -ENOTREG;
+
 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
 return -ENOTDIR;
 
@@ -4765,6 +4769,8 @@ static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
 struct path path;
 int error = path_lookupat(nd, flags, &path);
 if (!error) {
+ if ((file->f_flags & O_REGULAR) && !d_is_reg(path.dentry))
+ return -ENOTREG;
 audit_inode(nd->name, path.dentry, 0);
 error = vfs_open(&path, file);
 path_put(&path);
fs/open.c +3 -1
index 74c4c1462b3e..82153e21907e 100644
@@ -1173,7 +1173,7 @@ struct file *kernel_file_open(const struct path *path, int flags,
 EXPORT_SYMBOL_GPL(kernel_file_open);
 
 #define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE))
-#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC)
+#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC | O_REGULAR)
 
 inline struct open_how build_open_how(int flags, umode_t mode)
 {
@@ -1250,6 +1250,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 & O_REGULAR)) {
+ return -EINVAL;
 }
 if (flags & O_PATH) {
 /* O_PATH only permits certain other flags to be set. */
include/linux/fcntl.h +1 -1
index a332e79b3207..4fd07b0e0a17 100644
@@ -10,7 +10,7 @@
 (O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | \
 O_APPEND | O_NDELAY | O_NONBLOCK | __O_SYNC | O_DSYNC | \
 FASYNC | O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \
- O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE)
+ O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE | O_REGULAR)
 
 /* List of all valid flags for the how->resolve argument: */
 #define VALID_RESOLVE_FLAGS \
include/uapi/asm-generic/errno.h +2 -0
index 92e7ae493ee3..2216ab9aa32e 100644
@@ -122,4 +122,6 @@
 
 #define EHWPOISON 133 /* Memory page has hardware error */
 
+#define ENOTREG 134 /* Not a regular file */
+
 #endif
include/uapi/asm-generic/fcntl.h +4 -0
index 613475285643..3468b352a575 100644
@@ -88,6 +88,10 @@
 #define __O_TMPFILE 020000000
 #endif
 
+#ifndef O_REGULAR
+#define O_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..8bbcaa9024f9 100644
@@ -127,4 +127,6 @@
 
 #define EHWPOISON 139 /* Memory page has hardware error */
 
+#define ENOTREG 140 /* Not a regular file */
+
 #endif
tools/arch/mips/include/uapi/asm/errno.h +2 -0
index c01ed91b1ef4..293c78777254 100644
@@ -126,6 +126,8 @@
 
 #define EHWPOISON 168 /* Memory page has hardware error */
 
+#define ENOTREG 169 /* Not a regular file */
+
 #define EDQUOT 1133 /* Quota exceeded */
 
 
tools/arch/parisc/include/uapi/asm/errno.h +2 -0
index 8cbc07c1903e..442917484f99 100644
@@ -124,4 +124,6 @@
 
 #define EHWPOISON 257 /* Memory page has hardware error */
 
+#define ENOTREG 258 /* Not a regular file */
+
 #endif
tools/arch/sparc/include/uapi/asm/errno.h +2 -0
index 4a41e7835fd5..8dce0bfeab74 100644
@@ -117,4 +117,6 @@
 
 #define EHWPOISON 135 /* Memory page has hardware error */
 
+#define ENOTREG 136 /* Not a regular file */
+
 #endif
tools/include/uapi/asm-generic/errno.h +2 -0
index 92e7ae493ee3..2216ab9aa32e 100644
@@ -122,4 +122,6 @@
 
 #define EHWPOISON 133 /* Memory page has hardware error */
 
+#define ENOTREG 134 /* Not a regular file */
+
 #endif
Signature · 1 line
2.52.0

Cheers, Andreas

#7 lore raw

PATCH v2 2/2 kselftest/openat2: test for O_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..011ebc9af4e5 100644
@@ -320,8 +320,42 @@ void test_openat2_flags(void)
 	}
 }
 
+#ifndef O_REGULAR
+#define O_REGULAR 040000000
+#endif
+
+#ifndef ENOTREG
+#define ENOTREG 134
+#endif
+
+void test_openat2_o_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 = O_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 != -ENOTREG) {
+		ksft_test_result_fail("openat2 should return ENOTREG\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_o_regular_flag();
 
 	if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0)
 		ksft_exit_fail();
Signature · 1 line
2.52.0