plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * 07/2001 Ported by Wayne Boyer |
| 4 | * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 5 | * |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 10 | * |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 14 | * the GNU General Public License for more details. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 15 | * |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software Foundation, |
| 18 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Test Name: chmod06 |
| 23 | * |
| 24 | * Test Description: |
| 25 | * Verify that, |
| 26 | * 1) chmod(2) returns -1 and sets errno to EPERM if the effective user id |
| 27 | * of process does not match the owner of the file and the process is |
| 28 | * not super user. |
| 29 | * 2) chmod(2) returns -1 and sets errno to EACCES if search permission is |
| 30 | * denied on a component of the path prefix. |
| 31 | * 3) chmod(2) returns -1 and sets errno to EFAULT if pathname points |
| 32 | * outside user's accessible address space. |
| 33 | * 4) chmod(2) returns -1 and sets errno to ENAMETOOLONG if the pathname |
| 34 | * component is too long. |
| 35 | * 5) chmod(2) returns -1 and sets errno to ENOTDIR if the directory |
| 36 | * component in pathname is not a directory. |
| 37 | * 6) chmod(2) returns -1 and sets errno to ENOENT if the specified file |
| 38 | * does not exists. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | */ |
| 40 | |
vapier | 5b31d1b | 2006-08-21 06:46:26 +0000 | [diff] [blame] | 41 | #ifndef _GNU_SOURCE |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 42 | # define _GNU_SOURCE |
vapier | 5b31d1b | 2006-08-21 06:46:26 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | #include <stdio.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <unistd.h> |
| 48 | #include <fcntl.h> |
| 49 | #include <errno.h> |
| 50 | #include <string.h> |
| 51 | #include <signal.h> |
| 52 | #include <grp.h> |
| 53 | #include <pwd.h> |
| 54 | #include <sys/types.h> |
| 55 | #include <sys/stat.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 56 | #include <sys/mman.h> |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 57 | #include <sys/mount.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
| 59 | #include "test.h" |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 60 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 62 | #define MODE_RWX (S_IRWXU|S_IRWXG|S_IRWXO) |
| 63 | #define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | #define DIR_TEMP "testdir_1" |
| 65 | #define TEST_FILE1 "tfile_1" |
| 66 | #define TEST_FILE2 "testdir_1/tfile_2" |
| 67 | #define TEST_FILE3 "t_file/tfile_3" |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 68 | #define TEST_FILE4 "test_file4" |
| 69 | #define MNT_POINT "mntpoint" |
| 70 | |
| 71 | #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \ |
| 72 | S_IXGRP|S_IROTH|S_IXOTH) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 74 | static char long_path[PATH_MAX + 2]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
Cyril Hrubis | b111871 | 2014-06-19 16:50:57 +0200 | [diff] [blame] | 76 | static const char *device; |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 77 | static int mount_flag; |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 78 | static uid_t nobody_uid; |
| 79 | |
| 80 | static void set_root(void); |
| 81 | static void set_nobody(void); |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 82 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 83 | struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | char *pathname; |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 85 | mode_t mode; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | int exp_errno; |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 87 | void (*setup)(void); |
| 88 | void (*cleanup)(void); |
| 89 | } tc[] = { |
| 90 | {TEST_FILE1, FILE_MODE, EPERM, set_nobody, set_root}, |
| 91 | {TEST_FILE2, FILE_MODE, EACCES, set_nobody, set_root}, |
| 92 | {(char *)-1, FILE_MODE, EFAULT, NULL, NULL}, |
| 93 | {(char *)-2, FILE_MODE, EFAULT, NULL, NULL}, |
| 94 | {long_path, FILE_MODE, ENAMETOOLONG, NULL, NULL}, |
| 95 | {"", FILE_MODE, ENOENT, NULL, NULL}, |
| 96 | {TEST_FILE3, FILE_MODE, ENOTDIR, NULL, NULL}, |
| 97 | {MNT_POINT, FILE_MODE, EROFS, NULL, NULL}, |
| 98 | {TEST_FILE4, FILE_MODE, ELOOP, NULL, NULL}, |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 99 | }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 101 | char *TCID = "chmod06"; |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 102 | int TST_TOTAL = ARRAY_SIZE(tc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 104 | static char *bad_addr = 0; |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 105 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 106 | static void setup(void); |
| 107 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | { |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 111 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 112 | const char *msg; |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 113 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
Cyril Hrubis | b111871 | 2014-06-19 16:50:57 +0200 | [diff] [blame] | 115 | msg = parse_opts(ac, av, NULL, NULL); |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 116 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 118 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | setup(); |
| 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 122 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 124 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 126 | if (tc[i].setup) |
| 127 | tc[i].setup(); |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 128 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 129 | TEST(chmod(tc[i].pathname, tc[i].mode)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 131 | if (tc[i].cleanup) |
| 132 | tc[i].cleanup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 133 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | if (TEST_RETURN != -1) { |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 135 | tst_resm(TFAIL, "chmod succeeded unexpectedly"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | continue; |
| 137 | } |
| 138 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 139 | if (TEST_ERRNO == tc[i].exp_errno) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | tst_resm(TPASS | TTERRNO, |
| 141 | "chmod failed as expected"); |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 142 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 143 | tst_resm(TFAIL | TTERRNO, |
| 144 | "chmod failed unexpectedly; " |
| 145 | "expected %d - %s", |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 146 | tc[i].exp_errno, |
| 147 | tst_strerrno(tc[i].exp_errno)); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 148 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 150 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | cleanup(); |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 153 | tst_exit(); |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 154 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 156 | void set_root(void) |
| 157 | { |
| 158 | SAFE_SETEUID(cleanup, 0); |
| 159 | } |
| 160 | |
| 161 | void set_nobody(void) |
| 162 | { |
| 163 | SAFE_SETEUID(cleanup, nobody_uid); |
| 164 | } |
| 165 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 166 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | { |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 168 | struct passwd *nobody; |
Cyril Hrubis | b111871 | 2014-06-19 16:50:57 +0200 | [diff] [blame] | 169 | const char *fs_type; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 172 | |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 173 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | TEST_PAUSE; |
| 176 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 177 | nobody = SAFE_GETPWNAM(NULL, "nobody"); |
| 178 | nobody_uid = nobody->pw_uid; |
| 179 | |
| 180 | bad_addr = SAFE_MMAP(NULL, 0, 1, PROT_NONE, |
| 181 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
| 182 | tc[3].pathname = bad_addr; |
| 183 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | tst_tmpdir(); |
| 185 | |
Cyril Hrubis | b111871 | 2014-06-19 16:50:57 +0200 | [diff] [blame] | 186 | fs_type = tst_dev_fs_type(); |
| 187 | device = tst_acquire_device(cleanup); |
| 188 | |
| 189 | if (!device) |
| 190 | tst_brkm(TCONF, cleanup, "Failed to obtain block device"); |
| 191 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 192 | SAFE_TOUCH(cleanup, TEST_FILE1, 0666, NULL); |
| 193 | SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX); |
| 194 | SAFE_TOUCH(cleanup, TEST_FILE2, 0666, NULL); |
| 195 | SAFE_CHMOD(cleanup, DIR_TEMP, FILE_MODE); |
| 196 | SAFE_TOUCH(cleanup, "t_file", MODE_RWX, NULL); |
| 197 | |
Cyril Hrubis | b111871 | 2014-06-19 16:50:57 +0200 | [diff] [blame] | 198 | tst_mkfs(cleanup, device, fs_type, NULL); |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 199 | |
| 200 | SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE); |
| 201 | |
| 202 | /* |
| 203 | * mount a read-only file system for test EROFS |
| 204 | */ |
Cyril Hrubis | b111871 | 2014-06-19 16:50:57 +0200 | [diff] [blame] | 205 | if (mount(device, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) { |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 206 | tst_brkm(TBROK | TERRNO, cleanup, |
| 207 | "mount device:%s failed", device); |
| 208 | } |
| 209 | mount_flag = 1; |
| 210 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 211 | memset(long_path, 'a', PATH_MAX+1); |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * create two symbolic links who point to each other for |
| 215 | * test ELOOP. |
| 216 | */ |
| 217 | SAFE_SYMLINK(cleanup, "test_file4", "test_file5"); |
| 218 | SAFE_SYMLINK(cleanup, "test_file5", "test_file4"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Cyril Hrubis | cd3beec | 2014-06-19 15:20:12 +0200 | [diff] [blame] | 221 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | { |
Garrett Cooper | c5f4023 | 2010-12-17 12:26:46 -0800 | [diff] [blame] | 223 | if (chmod(DIR_TEMP, MODE_RWX) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 224 | tst_resm(TBROK | TERRNO, "chmod(%s) failed", DIR_TEMP); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 226 | if (mount_flag && tst_umount(MNT_POINT) < 0) { |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 227 | tst_brkm(TBROK | TERRNO, NULL, |
| 228 | "umount device:%s failed", device); |
| 229 | } |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 230 | |
Cyril Hrubis | b111871 | 2014-06-19 16:50:57 +0200 | [diff] [blame] | 231 | if (device) |
| 232 | tst_release_device(NULL, device); |
| 233 | |
| 234 | tst_rmdir(); |
Wang, Xiaoguang | 3aa27d9 | 2013-10-25 16:13:40 +0800 | [diff] [blame] | 235 | } |