1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Author: Aleksa Sarai <cyphar@cyphar.com>
4 * Copyright (C) 2018-2019 SUSE LLC.
11 #include <sys/types.h>
12 #include <sys/mount.h>
17 #include "../kselftest.h"
21 * O_LARGEFILE is set to 0 by glibc.
22 * XXX: This is wrong on {mips, parisc, powerpc, sparc}.
26 #define O_LARGEFILE 0x20000
28 #define O_LARGEFILE 0x8000
32 struct open_how inner;
42 struct open_how_ext arg;
47 #define NUM_OPENAT2_STRUCT_TESTS 7
48 #define NUM_OPENAT2_STRUCT_VARIATIONS 13
50 void test_openat2_struct(void)
52 int misalignments[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 17, 87 };
54 struct struct_test tests[] = {
56 { .name = "normal struct",
57 .arg.inner.flags = O_RDONLY,
58 .size = sizeof(struct open_how) },
59 /* Bigger struct, with zeroed out end. */
60 { .name = "bigger struct (zeroed out)",
61 .arg.inner.flags = O_RDONLY,
62 .size = sizeof(struct open_how_ext) },
64 /* TODO: Once expanded, check zero-padding. */
66 /* Smaller than version-0 struct. */
67 { .name = "zero-sized 'struct'",
68 .arg.inner.flags = O_RDONLY, .size = 0, .err = -EINVAL },
69 { .name = "smaller-than-v0 struct",
70 .arg.inner.flags = O_RDONLY,
71 .size = OPEN_HOW_SIZE_VER0 - 1, .err = -EINVAL },
73 /* Bigger struct, with non-zero trailing bytes. */
74 { .name = "bigger struct (non-zero data in first 'future field')",
75 .arg.inner.flags = O_RDONLY, .arg.extra1 = 0xdeadbeef,
76 .size = sizeof(struct open_how_ext), .err = -E2BIG },
77 { .name = "bigger struct (non-zero data in middle of 'future fields')",
78 .arg.inner.flags = O_RDONLY, .arg.extra2 = 0xfeedcafe,
79 .size = sizeof(struct open_how_ext), .err = -E2BIG },
80 { .name = "bigger struct (non-zero data at end of 'future fields')",
81 .arg.inner.flags = O_RDONLY, .arg.extra3 = 0xabad1dea,
82 .size = sizeof(struct open_how_ext), .err = -E2BIG },
85 BUILD_BUG_ON(ARRAY_LEN(misalignments) != NUM_OPENAT2_STRUCT_VARIATIONS);
86 BUILD_BUG_ON(ARRAY_LEN(tests) != NUM_OPENAT2_STRUCT_TESTS);
88 for (int i = 0; i < ARRAY_LEN(tests); i++) {
89 struct struct_test *test = &tests[i];
90 struct open_how_ext how_ext = test->arg;
92 for (int j = 0; j < ARRAY_LEN(misalignments); j++) {
93 int fd, misalign = misalignments[j];
96 void (*resultfn)(const char *msg, ...) = ksft_test_result_pass;
98 void *copy = NULL, *how_copy = &how_ext;
100 if (!openat2_supported) {
101 ksft_print_msg("openat2(2) unsupported\n");
102 resultfn = ksft_test_result_skip;
108 * Explicitly misalign the structure copying it with the given
109 * (mis)alignment offset. The other data is set to be non-zero to
110 * make sure that non-zero bytes outside the struct aren't checked
112 * This is effectively to check that is_zeroed_user() works.
114 copy = malloc(misalign + sizeof(how_ext));
115 how_copy = copy + misalign;
116 memset(copy, 0xff, misalign);
117 memcpy(how_copy, &how_ext, sizeof(how_ext));
120 fd = raw_openat2(AT_FDCWD, ".", how_copy, test->size);
124 failed = (fd != test->err);
126 fdpath = fdreadlink(fd);
131 resultfn = ksft_test_result_fail;
133 ksft_print_msg("openat2 unexpectedly returned ");
135 ksft_print_msg("%d['%s']\n", fd, fdpath);
137 ksft_print_msg("%d (%s)\n", fd, strerror(-fd));
142 resultfn("openat2 with %s argument [misalign=%d] succeeds\n",
143 test->name, misalign);
145 resultfn("openat2 with %s argument [misalign=%d] fails with %d (%s)\n",
146 test->name, misalign, test->err,
147 strerror(-test->err));
162 #define NUM_OPENAT2_FLAG_TESTS 25
164 void test_openat2_flags(void)
166 struct flag_test tests[] = {
167 /* O_TMPFILE is incompatible with O_PATH and O_CREAT. */
168 { .name = "incompatible flags (O_TMPFILE | O_PATH)",
169 .how.flags = O_TMPFILE | O_PATH | O_RDWR, .err = -EINVAL },
170 { .name = "incompatible flags (O_TMPFILE | O_CREAT)",
171 .how.flags = O_TMPFILE | O_CREAT | O_RDWR, .err = -EINVAL },
173 /* O_PATH only permits certain other flags to be set ... */
174 { .name = "compatible flags (O_PATH | O_CLOEXEC)",
175 .how.flags = O_PATH | O_CLOEXEC },
176 { .name = "compatible flags (O_PATH | O_DIRECTORY)",
177 .how.flags = O_PATH | O_DIRECTORY },
178 { .name = "compatible flags (O_PATH | O_NOFOLLOW)",
179 .how.flags = O_PATH | O_NOFOLLOW },
180 /* ... and others are absolutely not permitted. */
181 { .name = "incompatible flags (O_PATH | O_RDWR)",
182 .how.flags = O_PATH | O_RDWR, .err = -EINVAL },
183 { .name = "incompatible flags (O_PATH | O_CREAT)",
184 .how.flags = O_PATH | O_CREAT, .err = -EINVAL },
185 { .name = "incompatible flags (O_PATH | O_EXCL)",
186 .how.flags = O_PATH | O_EXCL, .err = -EINVAL },
187 { .name = "incompatible flags (O_PATH | O_NOCTTY)",
188 .how.flags = O_PATH | O_NOCTTY, .err = -EINVAL },
189 { .name = "incompatible flags (O_PATH | O_DIRECT)",
190 .how.flags = O_PATH | O_DIRECT, .err = -EINVAL },
191 { .name = "incompatible flags (O_PATH | O_LARGEFILE)",
192 .how.flags = O_PATH | O_LARGEFILE, .err = -EINVAL },
194 /* ->mode must only be set with O_{CREAT,TMPFILE}. */
195 { .name = "non-zero how.mode and O_RDONLY",
196 .how.flags = O_RDONLY, .how.mode = 0600, .err = -EINVAL },
197 { .name = "non-zero how.mode and O_PATH",
198 .how.flags = O_PATH, .how.mode = 0600, .err = -EINVAL },
199 { .name = "valid how.mode and O_CREAT",
200 .how.flags = O_CREAT, .how.mode = 0600 },
201 { .name = "valid how.mode and O_TMPFILE",
202 .how.flags = O_TMPFILE | O_RDWR, .how.mode = 0600 },
203 /* ->mode must only contain 0777 bits. */
204 { .name = "invalid how.mode and O_CREAT",
205 .how.flags = O_CREAT,
206 .how.mode = 0xFFFF, .err = -EINVAL },
207 { .name = "invalid (very large) how.mode and O_CREAT",
208 .how.flags = O_CREAT,
209 .how.mode = 0xC000000000000000ULL, .err = -EINVAL },
210 { .name = "invalid how.mode and O_TMPFILE",
211 .how.flags = O_TMPFILE | O_RDWR,
212 .how.mode = 0x1337, .err = -EINVAL },
213 { .name = "invalid (very large) how.mode and O_TMPFILE",
214 .how.flags = O_TMPFILE | O_RDWR,
215 .how.mode = 0x0000A00000000000ULL, .err = -EINVAL },
217 /* ->resolve flags must not conflict. */
218 { .name = "incompatible resolve flags (BENEATH | IN_ROOT)",
219 .how.flags = O_RDONLY,
220 .how.resolve = RESOLVE_BENEATH | RESOLVE_IN_ROOT,
223 /* ->resolve must only contain RESOLVE_* flags. */
224 { .name = "invalid how.resolve and O_RDONLY",
225 .how.flags = O_RDONLY,
226 .how.resolve = 0x1337, .err = -EINVAL },
227 { .name = "invalid how.resolve and O_CREAT",
228 .how.flags = O_CREAT,
229 .how.resolve = 0x1337, .err = -EINVAL },
230 { .name = "invalid how.resolve and O_TMPFILE",
231 .how.flags = O_TMPFILE | O_RDWR,
232 .how.resolve = 0x1337, .err = -EINVAL },
233 { .name = "invalid how.resolve and O_PATH",
235 .how.resolve = 0x1337, .err = -EINVAL },
237 /* currently unknown upper 32 bit rejected. */
238 { .name = "currently unknown bit (1 << 63)",
239 .how.flags = O_RDONLY | (1ULL << 63),
240 .how.resolve = 0, .err = -EINVAL },
243 BUILD_BUG_ON(ARRAY_LEN(tests) != NUM_OPENAT2_FLAG_TESTS);
245 for (int i = 0; i < ARRAY_LEN(tests); i++) {
246 int fd, fdflags = -1;
247 char *path, *fdpath = NULL;
249 struct flag_test *test = &tests[i];
250 void (*resultfn)(const char *msg, ...) = ksft_test_result_pass;
252 if (!openat2_supported) {
253 ksft_print_msg("openat2(2) unsupported\n");
254 resultfn = ksft_test_result_skip;
258 path = (test->how.flags & O_CREAT) ? "/tmp/ksft.openat2_tmpfile" : ".";
261 fd = sys_openat2(AT_FDCWD, path, &test->how);
265 failed = (fd != test->err);
269 fdpath = fdreadlink(fd);
270 fdflags = fcntl(fd, F_GETFL);
271 otherflags = fcntl(fd, F_GETFD);
274 E_assert(fdflags >= 0, "fcntl F_GETFL of new fd");
275 E_assert(otherflags >= 0, "fcntl F_GETFD of new fd");
277 /* O_CLOEXEC isn't shown in F_GETFL. */
278 if (otherflags & FD_CLOEXEC)
279 fdflags |= O_CLOEXEC;
280 /* O_CREAT is hidden from F_GETFL. */
281 if (test->how.flags & O_CREAT)
283 if (!(test->how.flags & O_LARGEFILE))
284 fdflags &= ~O_LARGEFILE;
285 failed |= (fdflags != test->how.flags);
289 resultfn = ksft_test_result_fail;
291 ksft_print_msg("openat2 unexpectedly returned ");
293 ksft_print_msg("%d['%s'] with %X (!= %X)\n",
297 ksft_print_msg("%d (%s)\n", fd, strerror(-fd));
302 resultfn("openat2 with %s succeeds\n", test->name);
304 resultfn("openat2 with %s fails with %d (%s)\n",
305 test->name, test->err, strerror(-test->err));
312 #define NUM_TESTS (NUM_OPENAT2_STRUCT_VARIATIONS * NUM_OPENAT2_STRUCT_TESTS + \
313 NUM_OPENAT2_FLAG_TESTS)
315 int main(int argc, char **argv)
318 ksft_set_plan(NUM_TESTS);
320 test_openat2_struct();
321 test_openat2_flags();
323 if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0)