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