plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 3 | * |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 8 | * |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 13 | * |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * Test Name: lchown02 |
| 21 | * |
| 22 | * Test Description: |
| 23 | * Verify that, |
| 24 | * 1) lchown(2) returns -1 and sets errno to EPERM if the effective user id |
| 25 | * of process does not match the owner of the file and the process is |
| 26 | * not super user. |
| 27 | * 2) lchown(2) returns -1 and sets errno to EACCES if search permission is |
| 28 | * denied on a component of the path prefix. |
| 29 | * 3) lchown(2) returns -1 and sets errno to EFAULT if pathname points |
| 30 | * outside user's accessible address space. |
| 31 | * 4) lchown(2) returns -1 and sets errno to ENAMETOOLONG if the pathname |
| 32 | * component is too long. |
| 33 | * 5) lchown(2) returns -1 and sets errno to ENOTDIR if the directory |
| 34 | * component in pathname is not a directory. |
| 35 | * 6) lchown(2) returns -1 and sets errno to ENOENT if the specified file |
| 36 | * does not exists. |
| 37 | * |
| 38 | * Expected Result: |
| 39 | * lchown() should fail with return value -1 and set expected errno. |
| 40 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | * HISTORY |
| 42 | * 07/2001 Ported by Wayne Boyer |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 43 | * 11/2010 Rewritten by Cyril Hrubis chrubis@suse.cz |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | */ |
| 45 | |
| 46 | #include <stdio.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <unistd.h> |
| 49 | #include <fcntl.h> |
| 50 | #include <errno.h> |
| 51 | #include <string.h> |
| 52 | #include <signal.h> |
| 53 | #include <grp.h> |
| 54 | #include <pwd.h> |
| 55 | #include <sys/types.h> |
| 56 | #include <sys/stat.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 57 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
| 59 | #include "test.h" |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 60 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 62 | #define TEST_USER "nobody" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | #define MODE_RWX S_IRWXU | S_IRWXG | S_IRWXO |
| 64 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 65 | #define DIR_TEMP "testdir_1" |
| 66 | #define TEST_FILE1 "tfile_1" |
| 67 | #define SFILE1 "sfile_1" |
| 68 | #define TEST_FILE2 "testdir_1/tfile_2" |
| 69 | #define SFILE2 "testdir_1/sfile_2" |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 70 | #define TFILE3 "t_file" |
| 71 | #define SFILE3 "t_file/sfile" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 73 | TCID_DEFINE(lchown02); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 74 | int TST_TOTAL = 7; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 76 | static void setup_eperm(int pos); |
| 77 | static void setup_eacces(int pos); |
| 78 | static void setup_enotdir(int pos); |
| 79 | static void setup_longpath(int pos); |
| 80 | static void setup_efault(int pos); |
| 81 | static void setup_highaddress(int pos); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 83 | static char path[PATH_MAX + 2]; |
| 84 | |
| 85 | struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | char *pathname; |
| 87 | char *desc; |
| 88 | int exp_errno; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | void (*setup) (int pos); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 92 | static struct test_case_t test_cases[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 93 | {SFILE1, "Process is not owner/root", EPERM, setup_eperm}, |
| 94 | {SFILE2, "Search permission denied", EACCES, setup_eacces}, |
| 95 | {NULL, "Address beyond address space", EFAULT, setup_highaddress}, |
| 96 | {NULL, "Unaccessible address space", EFAULT, setup_efault}, |
| 97 | {path, "Pathname too long", ENAMETOOLONG, setup_longpath}, |
| 98 | {SFILE3, "Path contains regular file", ENOTDIR, setup_enotdir}, |
| 99 | {"", "Pathname is empty", ENOENT, NULL}, |
| 100 | {NULL, NULL, 0, NULL} |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 101 | }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 103 | static struct passwd *ltpuser; |
robbiew | 5f2ac9f | 2001-08-31 18:29:29 +0000 | [diff] [blame] | 104 | |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 105 | static void setup(void); |
| 106 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 108 | int main(int argc, char *argv[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | { |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 110 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 111 | const char *msg; |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 112 | uid_t user_id; |
| 113 | gid_t group_id; |
| 114 | int i; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 115 | |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 116 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 121 | user_id = geteuid(); |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 122 | UID16_CHECK(user_id, lchown, cleanup); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 123 | group_id = getegid(); |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 124 | GID16_CHECK(group_id, lchown, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 127 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 129 | for (i = 0; test_cases[i].desc != NULL; i++) { |
| 130 | char *file_name = test_cases[i].pathname; |
| 131 | char *test_desc = test_cases[i].desc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * Call lchown(2) to test different test conditions. |
| 135 | * verify that it fails with -1 return value and |
| 136 | * sets appropriate errno. |
| 137 | */ |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 138 | TEST(LCHOWN(cleanup, file_name, user_id, group_id)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 139 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | /* Check return code from lchown(2) */ |
| 141 | if (TEST_RETURN == -1) { |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 142 | if (TEST_ERRNO == test_cases[i].exp_errno) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | tst_resm(TPASS, |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 144 | "lchown(2) fails, %s, errno:%d", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | test_desc, TEST_ERRNO); |
| 146 | } else { |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 147 | tst_resm(TFAIL, "lchown(2) fails, %s, " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | "errno:%d, expected errno:%d", |
| 149 | test_desc, TEST_ERRNO, |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 150 | test_cases[i].exp_errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | } |
| 152 | } else { |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 153 | tst_resm(TFAIL, "lchown(2) returned %ld, " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | "expected -1, errno:%d", TEST_RETURN, |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 155 | test_cases[i].exp_errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | } |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | cleanup(); |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 161 | tst_exit(); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 162 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 164 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | { |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 166 | int i; |
yaberauneya | 3dfbae9 | 2009-11-14 07:14:06 +0000 | [diff] [blame] | 167 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 169 | |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 170 | tst_require_root(NULL); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 171 | |
| 172 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 173 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 174 | /* change euid and gid to nobody */ |
| 175 | ltpuser = getpwnam(TEST_USER); |
| 176 | |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 177 | if (ltpuser == NULL) |
| 178 | tst_brkm(TBROK, cleanup, "getpwnam failed"); |
robbiew | 691a9c8 | 2001-09-12 16:30:29 +0000 | [diff] [blame] | 179 | |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 180 | if (setgid(ltpuser->pw_uid) == -1) |
| 181 | tst_resm(TBROK | TERRNO, "setgid failed"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 182 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | tst_tmpdir(); |
| 184 | |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 185 | for (i = 0; test_cases[i].desc != NULL; i++) |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 186 | if (test_cases[i].setup != NULL) |
| 187 | test_cases[i].setup(i); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 191 | * setup_eperm() - setup function for a test condition for which lchown(2) |
| 192 | * returns -1 and sets errno to EPERM. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 193 | * |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 194 | * Create test file and symlink with uid 0. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | */ |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 196 | static void setup_eperm(int pos LTP_ATTRIBUTE_UNUSED) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | { |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 198 | int fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 200 | /* create a testfile */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 201 | if ((fd = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1) |
| 202 | tst_brkm(TBROK | TERRNO, cleanup, "open failed"); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 203 | |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 204 | if (close(fd) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 205 | tst_brkm(TBROK | TERRNO, cleanup, "close failed"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 206 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 207 | /* become root once more */ |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 208 | if (seteuid(0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 209 | tst_resm(TBROK | TERRNO, "setuid(0) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 210 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 211 | /* create symling to testfile */ |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 212 | if (symlink(TEST_FILE1, SFILE1) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 213 | tst_brkm(TBROK | TERRNO, cleanup, "symlink failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 215 | /* back to the user nobody */ |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 216 | if (seteuid(ltpuser->pw_uid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 217 | tst_resm(TBROK | TERRNO, "seteuid(%d) failed", ltpuser->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 221 | * setup_eaccess() - setup function for a test condition for which lchown(2) |
| 222 | * returns -1 and sets errno to EACCES. |
| 223 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 224 | * Create a test directory under temporary directory and create a test file |
| 225 | * under this directory with mode "0666" permissions. |
| 226 | * Modify the mode permissions on test directory such that process will not |
| 227 | * have search permissions on test directory. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | */ |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 229 | static void setup_eacces(int pos LTP_ATTRIBUTE_UNUSED) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | { |
| 231 | int fd; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 232 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 233 | /* create a test directory */ |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 234 | if (mkdir(DIR_TEMP, MODE_RWX) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 235 | tst_brkm(TBROK | TERRNO, cleanup, "mkdir failed"); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 236 | |
| 237 | /* create a file under test directory */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 238 | if ((fd = open(TEST_FILE2, O_RDWR | O_CREAT, 0666)) == -1) |
| 239 | tst_brkm(TBROK | TERRNO, cleanup, "open failed"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 240 | |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 241 | if (close(fd) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 242 | tst_brkm(TBROK | TERRNO, cleanup, "close failed"); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 243 | |
| 244 | /* create a symlink of testfile */ |
| 245 | if (symlink(TEST_FILE2, SFILE2) < 0) { |
| 246 | tst_brkm(TBROK | TERRNO, cleanup, "symlink(2) %s to %s failed", |
| 247 | TEST_FILE2, SFILE2); |
| 248 | } |
| 249 | |
| 250 | /* modify mode permissions on test directory */ |
| 251 | if (chmod(DIR_TEMP, FILE_MODE) < 0) { |
| 252 | tst_brkm(TBROK | TERRNO, cleanup, "chmod(2) %s failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 253 | DIR_TEMP); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * setup_efault() -- setup for a test condition where lchown(2) returns -1 and |
| 259 | * sets errno to EFAULT. |
| 260 | * |
| 261 | * Create "bad address" by explicitly mmaping anonymous page that may not be |
| 262 | * accesed (see PROT_NONE). |
| 263 | */ |
| 264 | static void setup_efault(int pos) |
| 265 | { |
| 266 | char *bad_addr = 0; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 267 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 268 | bad_addr = mmap(NULL, 1, PROT_NONE, |
| 269 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, -1, 0); |
| 270 | |
| 271 | if (bad_addr == MAP_FAILED) |
| 272 | tst_brkm(TBROK | TERRNO, cleanup, "mmap failed"); |
| 273 | |
| 274 | test_cases[pos].pathname = bad_addr; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * setup_efault() -- setup for a test condition where lchown(2) returns -1 and |
| 279 | * sets errno to EFAULT. |
| 280 | * |
| 281 | * Use ltp function get_high_address() to compute high address. |
| 282 | */ |
| 283 | static void setup_highaddress(int pos) |
| 284 | { |
| 285 | test_cases[pos].pathname = get_high_address(); |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * setup_enotdir() - setup function for a test condition for which chown(2) |
| 290 | * returns -1 and sets errno to ENOTDIR. |
| 291 | * |
| 292 | * Create a regular file "t_file" to call lchown(2) on "t_file/sfile" later. |
| 293 | */ |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 294 | static void setup_enotdir(int pos LTP_ATTRIBUTE_UNUSED) |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 295 | { |
| 296 | int fd; |
| 297 | |
| 298 | /* create a testfile under temporary directory */ |
| 299 | if ((fd = open(TFILE3, O_RDWR | O_CREAT, MODE_RWX)) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 300 | tst_brkm(TBROK | TERRNO, cleanup, "open(2) %s failed", TFILE3); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | if (close(fd) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 304 | tst_brkm(TBROK | TERRNO, cleanup, "close(2) %s failed", TFILE3); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 305 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* |
| 309 | * longpath_setup() - setup to create a node with a name length exceeding |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 310 | * the length of PATH_MAX. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 311 | */ |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 312 | static void setup_longpath(int pos) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 313 | { |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 314 | memset(test_cases[pos].pathname, 'a', PATH_MAX + 1); |
| 315 | test_cases[pos].pathname[PATH_MAX + 1] = '\0'; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 318 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 319 | { |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 320 | if (seteuid(0) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 321 | tst_resm(TINFO | TERRNO, |
| 322 | "seteuid(2) failed to set the effective uid to 0"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 323 | } |
| 324 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 325 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 326 | } |