Skip to content

Commit b97902b

Browse files
committed
Merge tag 'fs.openat2.unknown_flags.v5.14' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux
Pull openat2 fixes from Christian Brauner: - Remove the unused VALID_UPGRADE_FLAGS define we carried from an extension to openat2() that we haven't merged. Aleksa might be getting back to it at some point but just not right now. - openat2() used to accidently ignore unknown flag values in the upper 32 bits. The new openat2() syscall verifies that no unknown O-flag values are set and returns an error to userspace if they are while the older open syscalls like open() and openat() simply ignore unknown flag values: #define O_FLAG_CURRENTLY_INVALID (1 << 31) struct open_how how = { .flags = O_RDONLY | O_FLAG_CURRENTLY_INVALID, .resolve = 0, }; /* fails */ fd = openat2(-EBADF, "/dev/null", &how, sizeof(how)); /* succeeds */ fd = openat(-EBADF, "/dev/null", O_RDONLY | O_FLAG_CURRENTLY_INVALID); However, openat2() silently truncates the upper 32 bits meaning: #define O_FLAG_CURRENTLY_INVALID_LOWER32 (1 << 31) #define O_FLAG_CURRENTLY_INVALID_UPPER32 (1 << 40) struct open_how how_lowe32 = { .flags = O_RDONLY | O_FLAG_CURRENTLY_INVALID_LOWER32, }; struct open_how how_upper32 = { .flags = O_RDONLY | O_FLAG_CURRENTLY_INVALID_UPPER32, }; /* fails */ fd = openat2(-EBADF, "/dev/null", &how_lower32, sizeof(how_lower32)); /* succeeds */ fd = openat2(-EBADF, "/dev/null", &how_upper32, sizeof(how_upper32)); Fix this by preventing the immediate truncation in build_open_flags() and add a compile-time check to catch when we add flags in the upper 32 bit range. * tag 'fs.openat2.unknown_flags.v5.14' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux: test: add openat2() test for invalid upper 32 bit flag value open: don't silently ignore unknown O-flags in openat2() fcntl: remove unused VALID_UPGRADE_FLAGS
2 parents 30d1a55 + 15845cb commit b97902b

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

fs/open.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,12 +1002,20 @@ inline struct open_how build_open_how(int flags, umode_t mode)
10021002

10031003
inline int build_open_flags(const struct open_how *how, struct open_flags *op)
10041004
{
1005-
int flags = how->flags;
1005+
u64 flags = how->flags;
1006+
u64 strip = FMODE_NONOTIFY | O_CLOEXEC;
10061007
int lookup_flags = 0;
10071008
int acc_mode = ACC_MODE(flags);
10081009

1009-
/* Must never be set by userspace */
1010-
flags &= ~(FMODE_NONOTIFY | O_CLOEXEC);
1010+
BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPEN_FLAGS),
1011+
"struct open_flags doesn't yet handle flags > 32 bits");
1012+
1013+
/*
1014+
* Strip flags that either shouldn't be set by userspace like
1015+
* FMODE_NONOTIFY or that aren't relevant in determining struct
1016+
* open_flags like O_CLOEXEC.
1017+
*/
1018+
flags &= ~strip;
10111019

10121020
/*
10131021
* Older syscalls implicitly clear all of the invalid flags or argument

include/linux/fcntl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
FASYNC | O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \
1313
O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE)
1414

15-
/* List of all valid flags for the how->upgrade_mask argument: */
16-
#define VALID_UPGRADE_FLAGS \
17-
(UPGRADE_NOWRITE | UPGRADE_NOREAD)
18-
1915
/* List of all valid flags for the how->resolve argument: */
2016
#define VALID_RESOLVE_FLAGS \
2117
(RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS | \

tools/testing/selftests/openat2/openat2_test.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ struct flag_test {
155155
int err;
156156
};
157157

158-
#define NUM_OPENAT2_FLAG_TESTS 24
158+
#define NUM_OPENAT2_FLAG_TESTS 25
159159

160160
void test_openat2_flags(void)
161161
{
@@ -229,6 +229,11 @@ void test_openat2_flags(void)
229229
{ .name = "invalid how.resolve and O_PATH",
230230
.how.flags = O_PATH,
231231
.how.resolve = 0x1337, .err = -EINVAL },
232+
233+
/* currently unknown upper 32 bit rejected. */
234+
{ .name = "currently unknown bit (1 << 63)",
235+
.how.flags = O_RDONLY | (1ULL << 63),
236+
.how.resolve = 0, .err = -EINVAL },
232237
};
233238

234239
BUILD_BUG_ON(ARRAY_LEN(tests) != NUM_OPENAT2_FLAG_TESTS);

0 commit comments

Comments
 (0)