1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 */
5
6 /*
7 * access(2) test for errno(s) EFAULT as root and nobody respectively.
8 */
9
10 #include <errno.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <pwd.h>
14 #include "tst_test.h"
15
16 static uid_t uid;
17
18 static struct tcase {
19 void *addr;
20 int mode;
21 char *name;
22 } tcases[] = {
23 {(void *)-1, F_OK, "F_OK"},
24 {(void *)-1, R_OK, "R_OK"},
25 {(void *)-1, W_OK, "W_OK"},
26 {(void *)-1, X_OK, "X_OK"},
27 };
28
verify_access(unsigned int n)29 static void verify_access(unsigned int n)
30 {
31 struct tcase *tc = &tcases[n];
32 pid_t pid;
33
34 TST_EXP_FAIL(access(tc->addr, tc->mode), EFAULT,
35 "invalid address as root");
36
37 /* test as nobody */
38 pid = SAFE_FORK();
39 if (pid) {
40 SAFE_WAITPID(pid, NULL, 0);
41 } else {
42 SAFE_SETUID(uid);
43 TST_EXP_FAIL(access(tc->addr, tc->mode), EFAULT,
44 "invalid address as nobody");
45 }
46 }
47
setup(void)48 static void setup(void)
49 {
50 struct passwd *pw;
51
52 pw = SAFE_GETPWNAM("nobody");
53
54 uid = pw->pw_uid;
55 }
56
57 static struct tst_test test = {
58 .tcnt = ARRAY_SIZE(tcases),
59 .needs_root = 1,
60 .forks_child = 1,
61 .setup = setup,
62 .test = verify_access,
63 };
64