History

vfs: add O_EMPTYPATH to openat(2)/openat2(2)

To get an operable version of an O_PATH file descriptor, it is possible to use openat(fd, ".", O_DIRECTORY) for directories, but other files currently require going through open("/proc/<pid>/fd/<nr>"), which depends on a functioning procfs. This patch adds the O_EMPTYPATH flag to openat(2)/openat2(2). If passed, LOOKUP_EMPTY is set at path resolution time. Note: This implies that you cannot rely anymore on disabling procfs from being mounted (e.g. inside a container without procfs mounted and with CAP_SYS_ADMIN dropped) to prevent O_PATH fds from being re-opened read-write.

Jori Koolstra committed 6 months ago

Commit 31cf44e1 parent 3e0be8c4 files · +10 −4Browse files

Unified · Split

4 files
  1. fs/fcntl.c +1 −1
  2. fs/open.c +4 −2
  3. include/linux/fcntl.h +1 −1
  4. include/uapi/asm-generic/fcntl.h +4 −0
fs/fcntl.c+1−1
@@ -1169,7 +1169,7 @@ static int __init fcntl_init(void) · 1168 unmodified lines
11691169 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
11701170 * is defined as O_NONBLOCK on some platforms and not on others.
11711171 */
1172− BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ !=
1172+ BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
11731173 HWEIGHT32(
11741174 (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
11751175 __FMODE_EXEC));
fs/open.c+4−2
@@ -1158,7 +1158,7 @@ struct file *kernel_file_open(const struct path *path, int flags, · 1157 unmodified lines
11581158 EXPORT_SYMBOL_GPL(kernel_file_open);
11591159
11601160 #define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE))
1161−#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC)
1161+#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC | O_EMPTYPATH)
11621162
11631163 inline struct open_how build_open_how(int flags, umode_t mode)
11641164 {
@@ -1279,6 +1279,8 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op) · 114 unmodified lines
12791279 lookup_flags |= LOOKUP_DIRECTORY;
12801280 if (!(flags & O_NOFOLLOW))
12811281 lookup_flags |= LOOKUP_FOLLOW;
1282+ if (flags & O_EMPTYPATH)
1283+ lookup_flags |= LOOKUP_EMPTY;
12821284
12831285 if (how->resolve & RESOLVE_NO_XDEV)
12841286 lookup_flags |= LOOKUP_NO_XDEV;
@@ -1360,7 +1362,7 @@ static int do_sys_openat2(int dfd, const char __user *filename, · 75 unmodified lines
13601362 if (unlikely(err))
13611363 return err;
13621364
1363− CLASS(filename, name)(filename);
1365+ CLASS(filename_flags, name)(filename, op.lookup_flags);
13641366 return FD_ADD(how->flags, do_file_open(dfd, name, &op));
13651367 }
13661368
include/linux/fcntl.h+1−1
@@ -10,7 +10,7 @@ · 9 unmodified lines
1010 (O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | \
1111 O_APPEND | O_NDELAY | O_NONBLOCK | __O_SYNC | O_DSYNC | \
1212 FASYNC | O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \
13− O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE)
13+ O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE | O_EMPTYPATH)
1414
1515 /* List of all valid flags for the how->resolve argument: */
1616 #define VALID_RESOLVE_FLAGS \
include/uapi/asm-generic/fcntl.h+4−0
@@ -88,6 +88,10 @@ · 87 unmodified lines
8888 #define __O_TMPFILE 020000000
8989 #endif
9090
91+#ifndef O_EMPTYPATH
92+#define O_EMPTYPATH (1 << 26) /* allow empty path */
93+#endif
94+
9195 /* a horrid kludge trying to make sure that this will fail on old kernels */
9296 #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
9397