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