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