• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  * Copyright (c) 2013 Fujitsu Ltd.
5  */
6 
7 /*
8  * Verify that,
9  *  1) access() fails with -1 return value and sets errno to EINVAL
10  *     if the specified access mode argument is invalid.
11  *  2) access() fails with -1 return value and sets errno to ENOENT
12  *     if the specified file doesn't exist (or pathname is NULL).
13  *  3) access() fails with -1 return value and sets errno to ENAMETOOLONG
14  *     if the pathname size is > PATH_MAX characters.
15  *  4) access() fails with -1 return value and sets errno to ENOTDIR
16  *     if a component used as a directory in pathname is not a directory.
17  *  5) access() fails with -1 return value and sets errno to ELOOP
18  *     if too many symbolic links were encountered in resolving pathname.
19  *  6) access() fails with -1 return value and sets errno to EROFS
20  *     if write permission was requested for files on a read-only file system.
21  *
22  * Ported to LTP: Wayne Boyer
23  *  11/2013 Ported by Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
24  *  11/2016 Modified by Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
25  */
26 
27 #include <errno.h>
28 #include <pwd.h>
29 #include <string.h>
30 #include <sys/mount.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 
34 #include "tst_test.h"
35 
36 #define FNAME1	"accessfile1"
37 #define FNAME2	"accessfile2/accessfile2"
38 #define DNAME	"accessfile2"
39 #define SNAME1	"symlink1"
40 #define SNAME2	"symlink2"
41 #define MNT_POINT	"mntpoint"
42 
43 static uid_t uid;
44 static char longpathname[PATH_MAX + 2];
45 
46 static struct tcase {
47 	const char *pathname;
48 	int mode;
49 	int exp_errno;
50 } tcases[] = {
51 	{FNAME1, -1, EINVAL},
52 	{"", W_OK, ENOENT},
53 	{longpathname, R_OK, ENAMETOOLONG},
54 	{FNAME2, R_OK, ENOTDIR},
55 	{SNAME1, R_OK, ELOOP},
56 	{MNT_POINT, W_OK, EROFS}
57 };
58 
access_test(struct tcase * tc,const char * user)59 static void access_test(struct tcase *tc, const char *user)
60 {
61 	TEST(access(tc->pathname, tc->mode));
62 
63 	if (TST_RET != -1) {
64 		tst_res(TFAIL, "access as %s succeeded unexpectedly", user);
65 		return;
66 	}
67 
68 	if (tc->exp_errno != TST_ERR) {
69 		tst_res(TFAIL | TTERRNO,
70 			"access as %s should fail with %s",
71 			user, tst_strerrno(tc->exp_errno));
72 		return;
73 	}
74 
75 	tst_res(TPASS | TTERRNO, "access as %s failed expectedly", user);
76 }
77 
verify_access(unsigned int n)78 static void verify_access(unsigned int n)
79 {
80 	struct tcase *tc = tcases + n;
81 	pid_t pid;
82 
83 	access_test(tc, "root");
84 
85 	pid = SAFE_FORK();
86 	if (pid) {
87 		SAFE_WAITPID(pid, NULL, 0);
88 	} else {
89 		SAFE_SETUID(uid);
90 		access_test(tc, "nobody");
91 	}
92 }
93 
setup(void)94 static void setup(void)
95 {
96 	struct passwd *pw;
97 
98 	pw = SAFE_GETPWNAM("nobody");
99 
100 	uid = pw->pw_uid;
101 
102 	memset(longpathname, 'a', sizeof(longpathname) - 1);
103 
104 	SAFE_TOUCH(FNAME1, 0333, NULL);
105 	SAFE_TOUCH(DNAME, 0644, NULL);
106 
107 	SAFE_SYMLINK(SNAME1, SNAME2);
108 	SAFE_SYMLINK(SNAME2, SNAME1);
109 }
110 
111 static struct tst_test test = {
112 	.tcnt = ARRAY_SIZE(tcases),
113 	.needs_root = 1,
114 	.forks_child = 1,
115 	.needs_rofs = 1,
116 	.mntpoint = MNT_POINT,
117 	.setup = setup,
118 	.test = verify_access,
119 };
120