• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org>
4  *
5  * Basic openat2() test to check various failures.
6  */
7 #include "tst_test.h"
8 #include "lapi/openat2.h"
9 
10 #define TEST_FILE "test_file"
11 
12 static struct open_how *how;
13 static struct open_how_pad *phow;
14 
15 static struct tcase {
16 	const char *name;
17 	int dfd;
18 	const char *pathname;
19 	uint64_t flags;
20 	uint64_t mode;
21 	uint64_t resolve;
22 	struct open_how **how;
23 	size_t size;
24 	int exp_errno;
25 } tcases[] = {
26 	{"invalid-dfd", -1, TEST_FILE, O_RDWR | O_CREAT, S_IRWXU, 0, &how, sizeof(*how), EBADF},
27 	{"invalid-pathname", AT_FDCWD, NULL, O_RDONLY | O_CREAT, S_IRUSR, 0, &how, sizeof(*how), EFAULT},
28 	{"invalid-flags", AT_FDCWD, TEST_FILE, O_RDONLY, S_IWUSR, 0, &how, sizeof(*how), EINVAL},
29 	{"invalid-mode", AT_FDCWD, TEST_FILE, O_RDWR | O_CREAT, -1, 0, &how, sizeof(*how), EINVAL},
30 	{"invalid-resolve", AT_FDCWD, TEST_FILE, O_RDWR | O_CREAT, S_IRWXU, -1, &how, sizeof(*how), EINVAL},
31 	{"invalid-size-zero", AT_FDCWD, TEST_FILE, O_RDWR | O_CREAT, S_IRWXU, 0, &how, 0, EINVAL},
32 	{"invalid-size-small", AT_FDCWD, TEST_FILE, O_RDWR | O_CREAT, S_IRWXU, 0, &how, sizeof(*how) - 1, EINVAL},
33 	{"invalid-size-big", AT_FDCWD, TEST_FILE, O_RDWR | O_CREAT, S_IRWXU, 0, &how, sizeof(*how) + 1, EFAULT},
34 	{"invalid-size-big-with-pad", AT_FDCWD, TEST_FILE, O_RDWR | O_CREAT, S_IRWXU, 0, (struct open_how **)&phow, sizeof(*how) + 8, E2BIG},
35 };
36 
setup(void)37 static void setup(void)
38 {
39 	openat2_supported_by_kernel();
40 	phow->pad = 0xDEAD;
41 }
42 
run(unsigned int n)43 static void run(unsigned int n)
44 {
45 	struct tcase *tc = &tcases[n];
46 	struct open_how *myhow = *tc->how;
47 
48 	myhow->flags = tc->flags;
49 	myhow->mode = tc->mode;
50 	myhow->resolve = tc->resolve;
51 
52 	TEST(openat2(tc->dfd, tc->pathname, myhow, tc->size));
53 
54 	if (TST_RET >= 0) {
55 		SAFE_CLOSE(TST_RET);
56 		tst_res(TFAIL, "%s: openat2() passed unexpectedly",
57 			tc->name);
58 		return;
59 	}
60 
61 	if (tc->exp_errno != TST_ERR) {
62 		tst_res(TFAIL | TTERRNO, "%s: openat2() should fail with %s",
63 			tc->name, tst_strerrno(tc->exp_errno));
64 		return;
65 	}
66 
67 	tst_res(TPASS | TTERRNO, "%s: openat2() failed as expected", tc->name);
68 }
69 
70 static struct tst_test test = {
71 	.tcnt = ARRAY_SIZE(tcases),
72 	.test = run,
73 	.setup = setup,
74 	.needs_tmpdir = 1,
75 	.bufs = (struct tst_buffers []) {
76 		{&how, .size = sizeof(*how)},
77 		{&phow, .size = sizeof(*phow)},
78 		{},
79 	}
80 };
81