plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +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 | 75b30b6 | 2014-06-19 16:22:27 +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 | 75b30b6 | 2014-06-19 16:22:27 +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 | 75b30b6 | 2014-06-19 16:22:27 +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: chown04 |
| 23 | * |
| 24 | * Test Description: |
| 25 | * Verify that, |
| 26 | * 1) chown(2) returns -1 and sets errno to EPERM if the effective user id |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 27 | * of process does not match the owner of the file and the process |
| 28 | * is not super user. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | * 2) chown(2) returns -1 and sets errno to EACCES if search permission is |
robbiew | 35e4906 | 2002-08-26 14:30:59 +0000 | [diff] [blame] | 30 | * denied on a component of the path prefix. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | * 3) chown(2) returns -1 and sets errno to EFAULT if pathname points |
robbiew | 35e4906 | 2002-08-26 14:30:59 +0000 | [diff] [blame] | 32 | * outside user's accessible address space. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | * 4) chown(2) returns -1 and sets errno to ENAMETOOLONG if the pathname |
robbiew | 35e4906 | 2002-08-26 14:30:59 +0000 | [diff] [blame] | 34 | * component is too long. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | * 5) chown(2) returns -1 and sets errno to ENOTDIR if the directory |
robbiew | 35e4906 | 2002-08-26 14:30:59 +0000 | [diff] [blame] | 36 | * component in pathname is not a directory. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | * 6) chown(2) returns -1 and sets errno to ENOENT if the specified file |
robbiew | 35e4906 | 2002-08-26 14:30:59 +0000 | [diff] [blame] | 38 | * does not exists. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | */ |
| 40 | |
| 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <unistd.h> |
| 44 | #include <fcntl.h> |
| 45 | #include <errno.h> |
| 46 | #include <string.h> |
| 47 | #include <signal.h> |
| 48 | #include <grp.h> |
| 49 | #include <pwd.h> |
| 50 | #include <sys/types.h> |
| 51 | #include <sys/stat.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 52 | #include <sys/mman.h> |
Zeng Linggang | 2d127a3 | 2013-10-25 18:43:16 +0800 | [diff] [blame] | 53 | #include <sys/mount.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
| 55 | #include "test.h" |
Zeng Linggang | 2d127a3 | 2013-10-25 18:43:16 +0800 | [diff] [blame] | 56 | #include "safe_macros.h" |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 57 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
Garrett Cooper | c0a39bb | 2010-12-17 16:06:03 -0800 | [diff] [blame] | 59 | #define MODE_RWX (S_IRWXU|S_IRWXG|S_IRWXO) |
| 60 | #define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) |
Zeng Linggang | 2d127a3 | 2013-10-25 18:43:16 +0800 | [diff] [blame] | 61 | #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \ |
| 62 | S_IXGRP|S_IROTH|S_IXOTH) |
robbiew | 35e4906 | 2002-08-26 14:30:59 +0000 | [diff] [blame] | 63 | #define DIR_TEMP "testdir_1" |
| 64 | #define TEST_FILE1 "tfile_1" |
Garrett Cooper | c0a39bb | 2010-12-17 16:06:03 -0800 | [diff] [blame] | 65 | #define TEST_FILE2 (DIR_TEMP "/tfile_2") |
robbiew | 35e4906 | 2002-08-26 14:30:59 +0000 | [diff] [blame] | 66 | #define TEST_FILE3 "t_file/tfile_3" |
Zeng Linggang | 2d127a3 | 2013-10-25 18:43:16 +0800 | [diff] [blame] | 67 | #define TEST_FILE4 "test_eloop1" |
| 68 | #define TEST_FILE5 "mntpoint" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 70 | static char long_path[PATH_MAX + 2]; |
Cyril Hrubis | c0b8cc1 | 2014-06-19 16:52:32 +0200 | [diff] [blame] | 71 | static const char *device; |
Zeng Linggang | 2d127a3 | 2013-10-25 18:43:16 +0800 | [diff] [blame] | 72 | static int mount_flag; |
| 73 | |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 74 | static struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | char *pathname; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | int exp_errno; |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 77 | } tc[] = { |
| 78 | {TEST_FILE1, EPERM}, |
| 79 | {TEST_FILE2, EACCES}, |
| 80 | {(char *)-1, EFAULT}, |
| 81 | {long_path, ENAMETOOLONG}, |
| 82 | {"", ENOENT}, |
| 83 | {TEST_FILE3, ENOTDIR}, |
| 84 | {TEST_FILE4, ELOOP}, |
| 85 | {TEST_FILE5, EROFS} |
| 86 | }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 88 | TCID_DEFINE(chown04); |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 89 | int TST_TOTAL = ARRAY_SIZE(tc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 91 | static char *bad_addr; |
robbiew | 6d8c9dc | 2001-08-31 18:34:37 +0000 | [diff] [blame] | 92 | |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 93 | static void setup(void); |
| 94 | static void cleanup(void); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 95 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | { |
Garrett Cooper | c0a39bb | 2010-12-17 16:06:03 -0800 | [diff] [blame] | 98 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 99 | const char *msg; |
Garrett Cooper | c0a39bb | 2010-12-17 16:06:03 -0800 | [diff] [blame] | 100 | int i; |
| 101 | uid_t user_id; |
| 102 | gid_t group_id; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
Cyril Hrubis | c0b8cc1 | 2014-06-19 16:52:32 +0200 | [diff] [blame] | 104 | msg = parse_opts(ac, av, NULL, NULL); |
Zeng Linggang | 2d127a3 | 2013-10-25 18:43:16 +0800 | [diff] [blame] | 105 | if (msg != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 107 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 110 | UID16_CHECK((user_id = geteuid()), "chown", cleanup) |
| 111 | GID16_CHECK((group_id = getegid()), "chown", cleanup) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 114 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
Garrett Cooper | c0a39bb | 2010-12-17 16:06:03 -0800 | [diff] [blame] | 116 | for (i = 0; i < TST_TOTAL; i++) { |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 117 | TEST(CHOWN(cleanup, tc[i].pathname, user_id, group_id)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | |
Garrett Cooper | c0a39bb | 2010-12-17 16:06:03 -0800 | [diff] [blame] | 119 | if (TEST_RETURN == 0) { |
| 120 | tst_resm(TFAIL, "chown succeeded unexpectedly"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | continue; |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | if (TEST_ERRNO == tc[i].exp_errno) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | tst_resm(TPASS | TTERRNO, "chown failed"); |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 126 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | tst_resm(TFAIL | TTERRNO, |
| 128 | "chown failed; expected: %d - %s", |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 129 | tc[i].exp_errno, |
| 130 | tst_strerrno(tc[i].exp_errno)); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 132 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | } |
| 134 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | cleanup(); |
Garrett Cooper | c0a39bb | 2010-12-17 16:06:03 -0800 | [diff] [blame] | 136 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 137 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 139 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | { |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 141 | struct passwd *ltpuser; |
Cyril Hrubis | c0b8cc1 | 2014-06-19 16:52:32 +0200 | [diff] [blame] | 142 | const char *fs_type; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | |
Garrett Cooper | c0a39bb | 2010-12-17 16:06:03 -0800 | [diff] [blame] | 144 | tst_require_root(NULL); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | tst_sig(FORK, DEF_HANDLER, cleanup); |
Cyril Hrubis | c0b8cc1 | 2014-06-19 16:52:32 +0200 | [diff] [blame] | 146 | ltpuser = SAFE_GETPWNAM(NULL, "nobody"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | tst_tmpdir(); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 149 | |
Cyril Hrubis | c0b8cc1 | 2014-06-19 16:52:32 +0200 | [diff] [blame] | 150 | fs_type = tst_dev_fs_type(); |
| 151 | device = tst_acquire_device(cleanup); |
| 152 | if (!device) |
| 153 | tst_brkm(TCONF, cleanup, "Failed to obtain block device"); |
| 154 | |
| 155 | tst_mkfs(cleanup, device, fs_type, NULL); |
| 156 | |
| 157 | TEST_PAUSE; |
| 158 | |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 159 | memset(long_path, 'a', PATH_MAX - 1); |
| 160 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 161 | bad_addr = mmap(0, 1, PROT_NONE, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 162 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
Garrett Cooper | c0a39bb | 2010-12-17 16:06:03 -0800 | [diff] [blame] | 163 | if (bad_addr == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | tst_brkm(TBROK | TERRNO, cleanup, "mmap failed"); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 165 | |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 166 | tc[2].pathname = bad_addr; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | |
Zeng Linggang | 2d127a3 | 2013-10-25 18:43:16 +0800 | [diff] [blame] | 168 | SAFE_SYMLINK(cleanup, "test_eloop1", "test_eloop2"); |
| 169 | SAFE_SYMLINK(cleanup, "test_eloop2", "test_eloop1"); |
| 170 | |
| 171 | SAFE_SETEUID(cleanup, 0); |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 172 | SAFE_TOUCH(cleanup, "t_file", MODE_RWX, NULL); |
| 173 | SAFE_TOUCH(cleanup, TEST_FILE1, 0666, NULL); |
| 174 | SAFE_MKDIR(cleanup, DIR_TEMP, S_IRWXU); |
| 175 | SAFE_TOUCH(cleanup, TEST_FILE2, 0666, NULL); |
| 176 | |
Zeng Linggang | 2d127a3 | 2013-10-25 18:43:16 +0800 | [diff] [blame] | 177 | SAFE_MKDIR(cleanup, "mntpoint", DIR_MODE); |
Cyril Hrubis | c0b8cc1 | 2014-06-19 16:52:32 +0200 | [diff] [blame] | 178 | if (mount(device, "mntpoint", fs_type, MS_RDONLY, NULL) < 0) { |
Zeng Linggang | 2d127a3 | 2013-10-25 18:43:16 +0800 | [diff] [blame] | 179 | tst_brkm(TBROK | TERRNO, cleanup, |
| 180 | "mount device:%s failed", device); |
| 181 | } |
| 182 | mount_flag = 1; |
| 183 | |
| 184 | SAFE_SETEUID(cleanup, ltpuser->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 187 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | { |
Garrett Cooper | c0a39bb | 2010-12-17 16:06:03 -0800 | [diff] [blame] | 189 | if (seteuid(0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 190 | tst_resm(TWARN | TERRNO, "seteuid(0) failed"); |
Cyril Hrubis | 75b30b6 | 2014-06-19 16:22:27 +0200 | [diff] [blame] | 191 | |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 192 | if (mount_flag && tst_umount("mntpoint") < 0) { |
Zeng Linggang | 2d127a3 | 2013-10-25 18:43:16 +0800 | [diff] [blame] | 193 | tst_brkm(TBROK | TERRNO, NULL, |
| 194 | "umount device:%s failed", device); |
| 195 | } |
robbiew | 35e4906 | 2002-08-26 14:30:59 +0000 | [diff] [blame] | 196 | |
Cyril Hrubis | c0b8cc1 | 2014-06-19 16:52:32 +0200 | [diff] [blame] | 197 | if (device) |
| 198 | tst_release_device(NULL, device); |
Zeng Linggang | 2d127a3 | 2013-10-25 18:43:16 +0800 | [diff] [blame] | 199 | |
Cyril Hrubis | c0b8cc1 | 2014-06-19 16:52:32 +0200 | [diff] [blame] | 200 | tst_rmdir(); |
Zeng Linggang | 2d127a3 | 2013-10-25 18:43:16 +0800 | [diff] [blame] | 201 | } |