1/*
2 * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like.  Any license provided herein, whether implied or
15 * otherwise, applies only to this software file.  Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25/*
26 * access(2) test for errno(s) EFAULT as root and nobody respectively.
27 */
28
29#include <errno.h>
30#include <unistd.h>
31#include <sys/types.h>
32#include <pwd.h>
33#include "tst_test.h"
34
35static uid_t uid;
36
37static struct tcase {
38	void *addr;
39	int mode;
40	char *name;
41} tcases[] = {
42	{(void *)-1, F_OK, "F_OK"},
43	{(void *)-1, R_OK, "R_OK"},
44	{(void *)-1, W_OK, "W_OK"},
45	{(void *)-1, X_OK, "X_OK"},
46};
47
48static void access_test(struct tcase *tc, const char *user)
49{
50	TEST(access(tc->addr, tc->mode));
51
52	if (TEST_RETURN != -1) {
53		tst_res(TFAIL, "access(%p, %s) as %s succeeded unexpectedly",
54			tc->addr, tc->name, user);
55		return;
56	}
57
58	if (TEST_ERRNO != EFAULT) {
59		tst_res(TFAIL | TTERRNO,
60			"access(%p, %s) as %s should fail with EFAULT",
61			tc->addr, tc->name, user);
62		return;
63	}
64
65	tst_res(TPASS | TTERRNO, "access(%p, %s) as %s",
66		tc->addr, tc->name, user);
67}
68
69static void verify_access(unsigned int n)
70{
71	struct tcase *tc = &tcases[n];
72	pid_t pid;
73
74	/* test as root */
75	access_test(tc, "root");
76
77	/* test as nobody */
78	pid = SAFE_FORK();
79	if (pid) {
80		SAFE_WAITPID(pid, NULL, 0);
81	} else {
82		SAFE_SETUID(uid);
83		access_test(tc, "nobody");
84	}
85}
86
87static void setup(void)
88{
89	struct passwd *pw;
90
91	pw = SAFE_GETPWNAM("nobody");
92
93	uid = pw->pw_uid;
94}
95
96static struct tst_test test = {
97	.tcnt = ARRAY_SIZE(tcases),
98	.needs_root = 1,
99	.forks_child = 1,
100	.setup = setup,
101	.test = verify_access,
102};
103