plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 3 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 16 | * along with this program; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | * Test Description: |
| 21 | * Verify that, |
| 22 | * 1) lstat(2) returns -1 and sets errno to EACCES if search permission is |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 23 | * denied on a component of the path prefix. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 24 | * 2) lstat(2) returns -1 and sets errno to ENOENT if the specified file |
| 25 | * does not exists or empty string. |
| 26 | * 3) lstat(2) returns -1 and sets errno to EFAULT if pathname points |
| 27 | * outside user's accessible address space. |
| 28 | * 4) lstat(2) returns -1 and sets errno to ENAMETOOLONG if the pathname |
| 29 | * component is too long. |
| 30 | * 5) lstat(2) returns -1 and sets errno to ENOTDIR if the directory |
| 31 | * component in pathname is not a directory. |
Zeng Linggang | 2f1458a | 2014-03-03 15:58:33 +0800 | [diff] [blame] | 32 | * 6) lstat(2) returns -1 and sets errno to ELOOP if the pathname has too |
| 33 | * many symbolic links encountered while traversing. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <unistd.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <errno.h> |
| 41 | #include <string.h> |
| 42 | #include <signal.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/stat.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 45 | #include <sys/mman.h> |
robbiew | 7bea895 | 2001-09-04 20:05:43 +0000 | [diff] [blame] | 46 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | |
| 48 | #include "test.h" |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 49 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | |
| 51 | #define MODE_RWX S_IRWXU | S_IRWXG | S_IRWXO |
| 52 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 53 | #define TEST_DIR "test_dir" |
| 54 | #define TEST_EACCES TEST_DIR"/test_eacces" |
| 55 | #define TEST_ENOENT "" |
| 56 | #define TEST_ENOTDIR "test_file/test_enotdir" |
Zeng Linggang | 2f1458a | 2014-03-03 15:58:33 +0800 | [diff] [blame] | 57 | #define TEST_ELOOP "/test_eloop" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 59 | static char longpathname[PATH_MAX + 2]; |
Zeng Linggang | 2f1458a | 2014-03-03 15:58:33 +0800 | [diff] [blame] | 60 | static char elooppathname[sizeof(TEST_ELOOP) * 43] = "."; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 61 | |
| 62 | #if !defined(UCLINUX) |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 63 | static void bad_addr_setup(int); |
| 64 | static void high_address_setup(int); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 65 | #endif |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 66 | |
| 67 | static struct test_case_t { |
| 68 | char *pathname; |
| 69 | int exp_errno; |
| 70 | void (*setup) (); |
| 71 | } test_cases[] = { |
| 72 | {TEST_EACCES, EACCES, NULL}, |
| 73 | {TEST_ENOENT, ENOENT, NULL}, |
| 74 | #if !defined(UCLINUX) |
| 75 | {NULL, EFAULT, bad_addr_setup}, |
| 76 | {NULL, EFAULT, high_address_setup}, |
| 77 | #endif |
| 78 | {longpathname, ENAMETOOLONG, NULL}, |
| 79 | {TEST_ENOTDIR, ENOTDIR, NULL}, |
Zeng Linggang | 2f1458a | 2014-03-03 15:58:33 +0800 | [diff] [blame] | 80 | {elooppathname, ELOOP, NULL}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 83 | char *TCID = "lstat02"; |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 84 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 86 | static void setup(void); |
| 87 | static void lstat_verify(int); |
| 88 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 92 | int lc; |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 93 | int i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 94 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 96 | msg = parse_opts(ac, av, NULL, NULL); |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 97 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 99 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | setup(); |
| 101 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 103 | tst_count = 0; |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 104 | for (i = 0; i < TST_TOTAL; i++) |
| 105 | lstat_verify(i); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 106 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 109 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 110 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 112 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | { |
Zeng Linggang | 2f1458a | 2014-03-03 15:58:33 +0800 | [diff] [blame] | 114 | int i; |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 115 | struct passwd *ltpuser; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 117 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 119 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 120 | |
| 121 | ltpuser = SAFE_GETPWNAM(cleanup, "nobody"); |
| 122 | SAFE_SETEUID(cleanup, ltpuser->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | TEST_PAUSE; |
| 125 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | tst_tmpdir(); |
| 127 | |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 128 | SAFE_MKDIR(cleanup, TEST_DIR, MODE_RWX); |
| 129 | SAFE_TOUCH(cleanup, TEST_EACCES, 0666, NULL); |
| 130 | if (chmod(TEST_DIR, FILE_MODE) < 0) |
| 131 | tst_brkm(TBROK, cleanup, "chmod(2) of %s failed", TEST_DIR); |
| 132 | |
| 133 | SAFE_TOUCH(cleanup, "test_file", MODE_RWX, NULL); |
| 134 | |
| 135 | memset(longpathname, 'a', PATH_MAX+1); |
Zeng Linggang | 2f1458a | 2014-03-03 15:58:33 +0800 | [diff] [blame] | 136 | |
| 137 | SAFE_MKDIR(cleanup, "test_eloop", MODE_RWX); |
| 138 | SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop"); |
| 139 | /* |
| 140 | * NOTE: the ELOOP test is written based on that the consecutive |
| 141 | * symlinks limits in kernel is hardwired to 40. |
| 142 | */ |
| 143 | for (i = 0; i < 43; i++) |
| 144 | strcat(elooppathname, TEST_ELOOP); |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 145 | } |
| 146 | |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 147 | #if !defined(UCLINUX) |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 148 | static void bad_addr_setup(int i) |
| 149 | { |
| 150 | test_cases[i].pathname = SAFE_MMAP(cleanup, 0, 1, PROT_NONE, |
| 151 | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
| 152 | } |
| 153 | |
| 154 | static void high_address_setup(int i) |
| 155 | { |
| 156 | test_cases[i].pathname = (char *)get_high_address(); |
| 157 | } |
mreed10 | 81534c3 | 2006-08-03 05:21:24 +0000 | [diff] [blame] | 158 | #endif |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 159 | |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 160 | static void lstat_verify(int i) |
| 161 | { |
| 162 | struct stat stat_buf; |
| 163 | |
| 164 | if (test_cases[i].setup != NULL) |
| 165 | test_cases[i].setup(i); |
| 166 | |
| 167 | TEST(lstat(test_cases[i].pathname, &stat_buf)); |
| 168 | |
| 169 | if (TEST_RETURN != -1) { |
| 170 | tst_resm(TFAIL, "lstat() returned %ld, expected -1, errno=%d", |
| 171 | TEST_RETURN, test_cases[i].exp_errno); |
| 172 | return; |
| 173 | } |
| 174 | |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 175 | if (TEST_ERRNO == test_cases[i].exp_errno) { |
| 176 | tst_resm(TPASS | TTERRNO, "lstat() failed as expected"); |
| 177 | } else { |
| 178 | tst_resm(TFAIL | TTERRNO, |
| 179 | "lstat() failed unexpectedly; expected: %d - %s", |
| 180 | test_cases[i].exp_errno, |
| 181 | strerror(test_cases[i].exp_errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 185 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | { |
Zeng Linggang | d1043c9 | 2014-02-18 16:09:59 +0800 | [diff] [blame] | 187 | if (seteuid(0)) |
| 188 | tst_resm(TINFO | TERRNO, "Failet to seteuid(0) before cleanup"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 191 | } |