1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * Ported to LTP: Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2014-2018 Cyril Hrubis <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*
9*49cdfc7eSAndroid Build Coastguard Worker * Test Name: chmod06
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
12*49cdfc7eSAndroid Build Coastguard Worker * Verify that,
13*49cdfc7eSAndroid Build Coastguard Worker * 1) chmod(2) returns -1 and sets errno to EPERM if the effective user id
14*49cdfc7eSAndroid Build Coastguard Worker * of process does not match the owner of the file and the process is
15*49cdfc7eSAndroid Build Coastguard Worker * not super user.
16*49cdfc7eSAndroid Build Coastguard Worker * 2) chmod(2) returns -1 and sets errno to EACCES if search permission is
17*49cdfc7eSAndroid Build Coastguard Worker * denied on a component of the path prefix.
18*49cdfc7eSAndroid Build Coastguard Worker * 3) chmod(2) returns -1 and sets errno to EFAULT if pathname points
19*49cdfc7eSAndroid Build Coastguard Worker * outside user's accessible address space.
20*49cdfc7eSAndroid Build Coastguard Worker * 4) chmod(2) returns -1 and sets errno to ENAMETOOLONG if the pathname
21*49cdfc7eSAndroid Build Coastguard Worker * component is too long.
22*49cdfc7eSAndroid Build Coastguard Worker * 5) chmod(2) returns -1 and sets errno to ENOTDIR if the directory
23*49cdfc7eSAndroid Build Coastguard Worker * component in pathname is not a directory.
24*49cdfc7eSAndroid Build Coastguard Worker * 6) chmod(2) returns -1 and sets errno to ENOENT if the specified file
25*49cdfc7eSAndroid Build Coastguard Worker * does not exists.
26*49cdfc7eSAndroid Build Coastguard Worker */
27*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker #define MODE_RWX (S_IRWXU|S_IRWXG|S_IRWXO)
32*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
33*49cdfc7eSAndroid Build Coastguard Worker #define DIR_TEMP "testdir_1"
34*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE1 "tfile_1"
35*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE2 "testdir_1/tfile_2"
36*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE3 "t_file/tfile_3"
37*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE4 "test_file4"
38*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT "mntpoint"
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
41*49cdfc7eSAndroid Build Coastguard Worker S_IXGRP|S_IROTH|S_IXOTH)
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker static char long_path[PATH_MAX + 2];
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker static uid_t nobody_uid;
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker static void set_root(void);
48*49cdfc7eSAndroid Build Coastguard Worker static void set_nobody(void);
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
51*49cdfc7eSAndroid Build Coastguard Worker char *pathname;
52*49cdfc7eSAndroid Build Coastguard Worker mode_t mode;
53*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
54*49cdfc7eSAndroid Build Coastguard Worker void (*setup)(void);
55*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup)(void);
56*49cdfc7eSAndroid Build Coastguard Worker } tc[] = {
57*49cdfc7eSAndroid Build Coastguard Worker {TEST_FILE1, FILE_MODE, EPERM, set_nobody, set_root},
58*49cdfc7eSAndroid Build Coastguard Worker {TEST_FILE2, FILE_MODE, EACCES, set_nobody, set_root},
59*49cdfc7eSAndroid Build Coastguard Worker {(char *)-1, FILE_MODE, EFAULT, NULL, NULL},
60*49cdfc7eSAndroid Build Coastguard Worker {NULL, FILE_MODE, EFAULT, NULL, NULL},
61*49cdfc7eSAndroid Build Coastguard Worker {long_path, FILE_MODE, ENAMETOOLONG, NULL, NULL},
62*49cdfc7eSAndroid Build Coastguard Worker {"", FILE_MODE, ENOENT, NULL, NULL},
63*49cdfc7eSAndroid Build Coastguard Worker {TEST_FILE3, FILE_MODE, ENOTDIR, NULL, NULL},
64*49cdfc7eSAndroid Build Coastguard Worker {MNT_POINT, FILE_MODE, EROFS, NULL, NULL},
65*49cdfc7eSAndroid Build Coastguard Worker {TEST_FILE4, FILE_MODE, ELOOP, NULL, NULL},
66*49cdfc7eSAndroid Build Coastguard Worker };
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker static char *bad_addr;
69*49cdfc7eSAndroid Build Coastguard Worker
run(unsigned int i)70*49cdfc7eSAndroid Build Coastguard Worker void run(unsigned int i)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker if (tc[i].setup)
73*49cdfc7eSAndroid Build Coastguard Worker tc[i].setup();
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker TEST(chmod(tc[i].pathname, tc[i].mode));
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker if (tc[i].cleanup)
78*49cdfc7eSAndroid Build Coastguard Worker tc[i].cleanup();
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != -1) {
81*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "chmod succeeded unexpectedly");
82*49cdfc7eSAndroid Build Coastguard Worker return;
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == tc[i].exp_errno) {
86*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "chmod failed as expected");
87*49cdfc7eSAndroid Build Coastguard Worker } else {
88*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "chmod failed unexpectedly; "
89*49cdfc7eSAndroid Build Coastguard Worker "expected %d - %s", tc[i].exp_errno,
90*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(tc[i].exp_errno));
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker
set_root(void)94*49cdfc7eSAndroid Build Coastguard Worker void set_root(void)
95*49cdfc7eSAndroid Build Coastguard Worker {
96*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(0);
97*49cdfc7eSAndroid Build Coastguard Worker }
98*49cdfc7eSAndroid Build Coastguard Worker
set_nobody(void)99*49cdfc7eSAndroid Build Coastguard Worker void set_nobody(void)
100*49cdfc7eSAndroid Build Coastguard Worker {
101*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(nobody_uid);
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker
setup(void)104*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
105*49cdfc7eSAndroid Build Coastguard Worker {
106*49cdfc7eSAndroid Build Coastguard Worker struct passwd *nobody;
107*49cdfc7eSAndroid Build Coastguard Worker unsigned int i;
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker nobody = SAFE_GETPWNAM("nobody");
110*49cdfc7eSAndroid Build Coastguard Worker nobody_uid = nobody->pw_uid;
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker bad_addr = SAFE_MMAP(0, 1, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(tc); i++) {
115*49cdfc7eSAndroid Build Coastguard Worker if (!tc[i].pathname)
116*49cdfc7eSAndroid Build Coastguard Worker tc[i].pathname = bad_addr;
117*49cdfc7eSAndroid Build Coastguard Worker }
118*49cdfc7eSAndroid Build Coastguard Worker
119*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(TEST_FILE1, 0666, NULL);
120*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(DIR_TEMP, MODE_RWX);
121*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(TEST_FILE2, 0666, NULL);
122*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHMOD(DIR_TEMP, FILE_MODE);
123*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH("t_file", MODE_RWX, NULL);
124*49cdfc7eSAndroid Build Coastguard Worker
125*49cdfc7eSAndroid Build Coastguard Worker memset(long_path, 'a', PATH_MAX+1);
126*49cdfc7eSAndroid Build Coastguard Worker
127*49cdfc7eSAndroid Build Coastguard Worker /*
128*49cdfc7eSAndroid Build Coastguard Worker * create two symbolic links who point to each other for
129*49cdfc7eSAndroid Build Coastguard Worker * test ELOOP.
130*49cdfc7eSAndroid Build Coastguard Worker */
131*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK("test_file4", "test_file5");
132*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK("test_file5", "test_file4");
133*49cdfc7eSAndroid Build Coastguard Worker }
134*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)135*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
136*49cdfc7eSAndroid Build Coastguard Worker {
137*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHMOD(DIR_TEMP, MODE_RWX);
138*49cdfc7eSAndroid Build Coastguard Worker }
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
141*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
142*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
143*49cdfc7eSAndroid Build Coastguard Worker .test = run,
144*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tc),
145*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
146*49cdfc7eSAndroid Build Coastguard Worker .needs_rofs = 1,
147*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MNT_POINT,
148*49cdfc7eSAndroid Build Coastguard Worker };
149