plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 4 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 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 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * Test Description : |
| 22 | * Verify that, |
| 23 | * 1) readlink(2) returns -1 and sets errno to EACCES if search/write |
| 24 | * permission is denied in the directory where the symbolic link |
| 25 | * resides. |
| 26 | * 2) readlink(2) returns -1 and sets errno to EINVAL if the buffer size |
| 27 | * is not positive. |
| 28 | * 3) readlink(2) returns -1 and sets errno to EINVAL if the specified |
| 29 | * file is not a symbolic link file. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 30 | * 4) readlink(2) returns -1 and sets errno to ENAMETOOLONG if the |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | * pathname component of symbolic link is too long (ie, > PATH_MAX). |
| 32 | * 5) readlink(2) returns -1 and sets errno to ENOENT if the component of |
| 33 | * symbolic link points to an empty string. |
Xing Gu | 7493169 | 2014-03-31 18:26:25 +0800 | [diff] [blame] | 34 | * 6) readlink(2) returns -1 and sets errno to ENOTDIR if a component of |
| 35 | * the path prefix is not a directory. |
| 36 | * 7) readlink(2) returns -1 and sets errno to ELOOP if too many symbolic |
| 37 | * links were encountered in translating the pathname. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | */ |
| 39 | |
| 40 | #include <stdio.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/fcntl.h> |
| 43 | #include <errno.h> |
| 44 | #include <string.h> |
| 45 | #include <signal.h> |
| 46 | #include <sys/stat.h> |
robbiew | 20e8ec6 | 2001-09-10 21:15:02 +0000 | [diff] [blame] | 47 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
| 49 | #include "test.h" |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 50 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 52 | #define MODE_RWX (S_IRWXU | S_IRWXG | S_IRWXO) |
| 53 | #define FILE_MODE (S_IRUSR | S_IRGRP | S_IROTH) |
| 54 | #define DIR_TEMP "testdir_1" |
| 55 | #define TEST_FILE1 "testdir_1/tfile_1" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | #define SYM_FILE1 "testdir_1/sfile_1" |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 57 | #define TEST_FILE2 "tfile_2" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | #define SYM_FILE2 "sfile_2" |
Xing Gu | 7493169 | 2014-03-31 18:26:25 +0800 | [diff] [blame] | 59 | #define TEST_FILE3 "tfile_3" |
| 60 | #define SYM_FILE3 "tfile_3/sfile_3" |
| 61 | #define ELOOPFILE "/test_eloop" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | #define MAX_SIZE 256 |
| 63 | |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 64 | static char longpathname[PATH_MAX + 2]; |
Xing Gu | 7493169 | 2014-03-31 18:26:25 +0800 | [diff] [blame] | 65 | static char elooppathname[sizeof(ELOOPFILE) * 43] = "."; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 67 | static struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | char *link; |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 69 | size_t buf_size; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | int exp_errno; |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 71 | } test_cases[] = { |
| 72 | {SYM_FILE1, 1, EACCES}, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | /* Don't test with bufsize -1, since this cause a fortify-check-fail when |
| 74 | using glibc and -D_FORITY_SOURCE=2 |
subrata_modak | 9855946 | 2008-11-18 10:18:41 +0000 | [diff] [blame] | 75 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | Discussion: http://lkml.org/lkml/2008/10/23/229 |
| 77 | Conclusion: Only test with 0 as non-positive bufsize. |
| 78 | |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 79 | { SYM_FILE2, -1, EINVAL, NULL }, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | */ |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 81 | {SYM_FILE2, 0, EINVAL}, |
| 82 | {TEST_FILE2, 1, EINVAL}, |
| 83 | {longpathname, 1, ENAMETOOLONG}, |
| 84 | {"", 1, ENOENT}, |
Xing Gu | 7493169 | 2014-03-31 18:26:25 +0800 | [diff] [blame] | 85 | {SYM_FILE3, 1, ENOTDIR}, |
| 86 | {elooppathname, 1, ELOOP}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 89 | static void setup(void); |
| 90 | static void readlink_verify(struct test_case_t *); |
| 91 | static void cleanup(void); |
| 92 | |
| 93 | char *TCID = "readlink03"; |
| 94 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
plars | 865695b | 2001-08-27 22:15:12 +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 | { |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 98 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 99 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 101 | msg = parse_opts(ac, av, NULL, NULL); |
| 102 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 103 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | setup(); |
| 106 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 108 | tst_count = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 109 | |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 110 | for (i = 0; i < TST_TOTAL; i++) |
| 111 | readlink_verify(&test_cases[i]); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 112 | } |
| 113 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 115 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 116 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 118 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | { |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 120 | struct passwd *ltpuser; |
Xing Gu | 7493169 | 2014-03-31 18:26:25 +0800 | [diff] [blame] | 121 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 123 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 126 | |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 127 | ltpuser = SAFE_GETPWNAM(cleanup, "nobody"); |
| 128 | SAFE_SETEUID(cleanup, ltpuser->pw_uid); |
| 129 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | TEST_PAUSE; |
| 131 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | tst_tmpdir(); |
| 133 | |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 134 | SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX); |
| 135 | SAFE_TOUCH(cleanup, TEST_FILE1, 0666, NULL); |
| 136 | SAFE_SYMLINK(cleanup, TEST_FILE1, SYM_FILE1); |
| 137 | SAFE_CHMOD(cleanup, DIR_TEMP, FILE_MODE); |
| 138 | |
| 139 | SAFE_TOUCH(cleanup, TEST_FILE2, 0666, NULL); |
| 140 | SAFE_SYMLINK(cleanup, TEST_FILE2, SYM_FILE2); |
| 141 | |
| 142 | memset(longpathname, 'a', PATH_MAX + 1); |
Xing Gu | 7493169 | 2014-03-31 18:26:25 +0800 | [diff] [blame] | 143 | |
| 144 | SAFE_TOUCH(cleanup, TEST_FILE3, 0666, NULL); |
| 145 | |
| 146 | /* |
| 147 | * NOTE: the ELOOP test is written based on that the consecutive |
| 148 | * symlinks limit in kernel is hardwired to 40. |
| 149 | */ |
| 150 | SAFE_MKDIR(cleanup, "test_eloop", MODE_RWX); |
| 151 | SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop"); |
| 152 | for (i = 0; i < 43; i++) |
| 153 | strcat(elooppathname, ELOOPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 156 | void readlink_verify(struct test_case_t *tc) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | { |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 158 | char buffer[MAX_SIZE]; |
| 159 | |
| 160 | if (tc->buf_size == 1) |
| 161 | tc->buf_size = sizeof(buffer); |
| 162 | |
| 163 | TEST(readlink(tc->link, buffer, tc->buf_size)); |
| 164 | |
| 165 | if (TEST_RETURN != -1) { |
| 166 | tst_resm(TFAIL, "readlink() returned %ld, " |
| 167 | "expected -1, errno:%d", TEST_RETURN, |
| 168 | tc->exp_errno); |
| 169 | return; |
| 170 | } |
| 171 | |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 172 | if (TEST_ERRNO == tc->exp_errno) { |
| 173 | tst_resm(TPASS | TTERRNO, "readlink() failed as expected"); |
| 174 | } else { |
| 175 | tst_resm(TFAIL | TTERRNO, |
| 176 | "readlink() failed unexpectedly; expected: %d - %s", |
| 177 | tc->exp_errno, strerror(tc->exp_errno)); |
| 178 | if (tc->exp_errno == ENOENT && TEST_ERRNO == EINVAL) { |
| 179 | tst_resm(TWARN | TTERRNO, |
| 180 | "It may be a Kernel Bug, see the patch:" |
| 181 | "http://git.kernel.org/linus/1fa1e7f6"); |
| 182 | } |
| 183 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 186 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | { |
Cyril Hrubis | ae02b42 | 2014-05-05 17:57:54 +0200 | [diff] [blame] | 188 | if (seteuid(0) == -1) |
| 189 | tst_resm(TWARN | TERRNO, "seteuid(0) failed"); |
| 190 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | tst_rmdir(); |
Nageswara R Sastry | 0cc7514 | 2011-09-12 17:17:27 +0530 | [diff] [blame] | 192 | } |