• 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 	TST_EXP_FAIL(access(tc->pathname, tc->mode), tc->exp_errno,
62 	             "access as %s", user);
63 }
64 
verify_access(unsigned int n)65 static void verify_access(unsigned int n)
66 {
67 	struct tcase *tc = tcases + n;
68 	pid_t pid;
69 
70 	access_test(tc, "root");
71 
72 	pid = SAFE_FORK();
73 	if (pid) {
74 		SAFE_WAITPID(pid, NULL, 0);
75 	} else {
76 		SAFE_SETUID(uid);
77 		access_test(tc, "nobody");
78 	}
79 }
80 
setup(void)81 static void setup(void)
82 {
83 	struct passwd *pw;
84 
85 	pw = SAFE_GETPWNAM("nobody");
86 
87 	uid = pw->pw_uid;
88 
89 	memset(longpathname, 'a', sizeof(longpathname) - 1);
90 
91 	SAFE_TOUCH(FNAME1, 0333, NULL);
92 	SAFE_TOUCH(DNAME, 0644, NULL);
93 
94 	SAFE_SYMLINK(SNAME1, SNAME2);
95 	SAFE_SYMLINK(SNAME2, SNAME1);
96 }
97 
98 static struct tst_test test = {
99 	.tcnt = ARRAY_SIZE(tcases),
100 	.needs_root = 1,
101 	.forks_child = 1,
102 	.needs_rofs = 1,
103 	.mntpoint = MNT_POINT,
104 	.setup = setup,
105 	.test = verify_access,
106 };
107