• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
access_test(struct tcase * tc,const char * user)29 static void access_test(struct tcase *tc, const char *user)
30 {
31 	TEST(access(tc->addr, tc->mode));
32 
33 	if (TST_RET != -1) {
34 		tst_res(TFAIL, "access(%p, %s) as %s succeeded unexpectedly",
35 			tc->addr, tc->name, user);
36 		return;
37 	}
38 
39 	if (TST_ERR != EFAULT) {
40 		tst_res(TFAIL | TTERRNO,
41 			"access(%p, %s) as %s should fail with EFAULT",
42 			tc->addr, tc->name, user);
43 		return;
44 	}
45 
46 	tst_res(TPASS | TTERRNO, "access(%p, %s) as %s",
47 		tc->addr, tc->name, user);
48 }
49 
verify_access(unsigned int n)50 static void verify_access(unsigned int n)
51 {
52 	struct tcase *tc = &tcases[n];
53 	pid_t pid;
54 
55 	/* test as root */
56 	access_test(tc, "root");
57 
58 	/* test as nobody */
59 	pid = SAFE_FORK();
60 	if (pid) {
61 		SAFE_WAITPID(pid, NULL, 0);
62 	} else {
63 		SAFE_SETUID(uid);
64 		access_test(tc, "nobody");
65 	}
66 }
67 
setup(void)68 static void setup(void)
69 {
70 	struct passwd *pw;
71 
72 	pw = SAFE_GETPWNAM("nobody");
73 
74 	uid = pw->pw_uid;
75 }
76 
77 static struct tst_test test = {
78 	.tcnt = ARRAY_SIZE(tcases),
79 	.needs_root = 1,
80 	.forks_child = 1,
81 	.setup = setup,
82 	.test = verify_access,
83 };
84