xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/access/access04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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  * Copyright (c) Linux Test Project, 2003-2023
6  * Ported to LTP: Wayne Boyer
7  * 11/2013 Ported by Xiaoguang Wang <[email protected]>
8  * 11/2016 Modified by Guangwen Feng <[email protected]>
9  */
10 
11 /*\
12  * [Description]
13  *
14  * -  access() fails with -1 return value and sets errno to EINVAL
15  *    if the specified access mode argument is invalid.
16  * -  access() fails with -1 return value and sets errno to ENOENT
17  *    if the specified file doesn't exist (or pathname is NULL).
18  * -  access() fails with -1 return value and sets errno to ENAMETOOLONG
19  *    if the pathname size is > PATH_MAX characters.
20  * -  access() fails with -1 return value and sets errno to ENOTDIR
21  *    if a component used as a directory in pathname is not a directory.
22  * -  access() fails with -1 return value and sets errno to ELOOP
23  *    if too many symbolic links were encountered in resolving pathname.
24  * -  access() fails with -1 return value and sets errno to EROFS
25  *    if write permission was requested for files on a read-only file system.
26  */
27 
28 #include <errno.h>
29 #include <pwd.h>
30 #include <string.h>
31 #include <sys/mount.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 
35 #include "tst_test.h"
36 
37 #define FNAME1	"accessfile1"
38 #define FNAME2	"accessfile2/accessfile2"
39 #define DNAME	"accessfile2"
40 #define SNAME1	"symlink1"
41 #define SNAME2	"symlink2"
42 #define MNT_POINT	"mntpoint"
43 #define LONGPATHSIZE (PATH_MAX + 2)
44 
45 static uid_t uid;
46 static char *longpathname;
47 static char *fname1;
48 static char *fname2;
49 static char *sname1;
50 static char *empty_fname;
51 static char *mnt_point;
52 
53 static struct tcase {
54 	char **pathname;
55 	int mode;
56 	int exp_errno;
57 } tcases[] = {
58 	{&fname1, -1, EINVAL},
59 	{&empty_fname, W_OK, ENOENT},
60 	{&longpathname, R_OK, ENAMETOOLONG},
61 	{&fname2, R_OK, ENOTDIR},
62 	{&sname1, R_OK, ELOOP},
63 	{&mnt_point, W_OK, EROFS}
64 };
65 
access_test(struct tcase * tc,const char * user)66 static void access_test(struct tcase *tc, const char *user)
67 {
68 	TST_EXP_FAIL(access(*tc->pathname, tc->mode), tc->exp_errno,
69 	             "access as %s", user);
70 }
71 
verify_access(unsigned int n)72 static void verify_access(unsigned int n)
73 {
74 	struct tcase *tc = tcases + n;
75 	pid_t pid;
76 
77 	access_test(tc, "root");
78 
79 	pid = SAFE_FORK();
80 	if (pid) {
81 		SAFE_WAITPID(pid, NULL, 0);
82 	} else {
83 		SAFE_SETUID(uid);
84 		access_test(tc, "nobody");
85 	}
86 }
87 
setup(void)88 static void setup(void)
89 {
90 	struct passwd *pw;
91 
92 	pw = SAFE_GETPWNAM("nobody");
93 
94 	uid = pw->pw_uid;
95 
96 	memset(longpathname, 'a', LONGPATHSIZE - 1);
97 	longpathname[LONGPATHSIZE-1] = 0;
98 
99 	SAFE_TOUCH(FNAME1, 0333, NULL);
100 	SAFE_TOUCH(DNAME, 0644, NULL);
101 
102 	SAFE_SYMLINK(SNAME1, SNAME2);
103 	SAFE_SYMLINK(SNAME2, SNAME1);
104 }
105 
106 static struct tst_test test = {
107 	.tcnt = ARRAY_SIZE(tcases),
108 	.needs_root = 1,
109 	.forks_child = 1,
110 	.needs_rofs = 1,
111 	.mntpoint = MNT_POINT,
112 	.setup = setup,
113 	.test = verify_access,
114 	.bufs = (struct tst_buffers []) {
115 		{&fname1, .str = FNAME1},
116 		{&fname2, .str = FNAME2},
117 		{&sname1, .str = SNAME1},
118 		{&empty_fname, .str = ""},
119 		{&longpathname, .size = LONGPATHSIZE},
120 		{&mnt_point, .str = MNT_POINT},
121 		{}
122 	}
123 };
124