plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
Cyril Hrubis | 572f0e0 | 2013-02-20 16:20:25 +0100 | [diff] [blame] | 3 | * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
plars | 0111e62 | 2001-09-06 13:04:23 +0000 | [diff] [blame] | 21 | * Test Name : readlink04 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 23 | * Test Description : |
| 24 | * Verify that, readlink call will succeed to read the contents of the |
| 25 | * symbolic link if invoked by non-root user who is not the owner of the |
| 26 | * symbolic link. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 27 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 28 | * Expected Result: |
| 29 | * readlink() should return the contents of symbolic link path in the |
| 30 | * specified buffer on success. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | */ |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 32 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | #include <stdlib.h> |
| 34 | #include <pwd.h> |
| 35 | #include <sys/types.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 36 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | #include <sys/fcntl.h> |
robbiew | dfa1a69 | 2002-07-09 15:32:36 +0000 | [diff] [blame] | 38 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | #include <errno.h> |
robbiew | a4f4a22 | 2001-09-12 21:36:09 +0000 | [diff] [blame] | 40 | #include <string.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 43 | char *TCID = "readlink04"; |
| 44 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | |
Cyril Hrubis | 116a8c0 | 2013-02-20 16:56:32 +0100 | [diff] [blame] | 46 | static char *TESTFILE = "./testfile"; |
| 47 | static char *SYMFILE = "slink_file"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
| 49 | #define MAX_SIZE 256 |
| 50 | |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 51 | static int exp_val; |
| 52 | static char buffer[MAX_SIZE]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 54 | static void setup(void); |
| 55 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 57 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 59 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 60 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 61 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 62 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 63 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | setup(); |
| 66 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 68 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 69 | tst_count = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 70 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 71 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | * Call readlink(2) to read the contents of |
| 73 | * symlink into a buffer. |
| 74 | */ |
Cyril Hrubis | 116a8c0 | 2013-02-20 16:56:32 +0100 | [diff] [blame] | 75 | TEST(readlink(SYMFILE, buffer, sizeof(buffer))); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 76 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | if (TEST_RETURN == -1) { |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 78 | tst_resm(TFAIL | TTERRNO, "readlink() on %s failed", |
Cyril Hrubis | 116a8c0 | 2013-02-20 16:56:32 +0100 | [diff] [blame] | 79 | SYMFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | continue; |
| 81 | } |
| 82 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 83 | if (TEST_RETURN == exp_val) { |
| 84 | /* Check for the contents of buffer */ |
| 85 | if (memcmp(buffer, TESTFILE, exp_val) != 0) { |
| 86 | tst_brkm(TFAIL, cleanup, "TESTFILE %s " |
| 87 | "and buffer contents %s " |
| 88 | "differ", TESTFILE, buffer); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 90 | tst_resm(TPASS, "readlink() " |
| 91 | "functionality on '%s' is " |
| 92 | "correct", SYMFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | } |
| 94 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 95 | tst_resm(TFAIL, "readlink() return value %ld " |
| 96 | "doesn't match, Expected %d", |
| 97 | TEST_RETURN, exp_val); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 99 | } |
| 100 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 102 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 103 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
Cyril Hrubis | 116a8c0 | 2013-02-20 16:56:32 +0100 | [diff] [blame] | 105 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 106 | |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 107 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | { |
Cyril Hrubis | 116a8c0 | 2013-02-20 16:56:32 +0100 | [diff] [blame] | 109 | int fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | char *tmp_dir = NULL; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | struct passwd *pwent; |
| 112 | |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 113 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | TEST_PAUSE; |
| 116 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | tst_tmpdir(); |
| 118 | |
| 119 | /* get the name of the temporary directory */ |
Cyril Hrubis | 116a8c0 | 2013-02-20 16:56:32 +0100 | [diff] [blame] | 120 | if ((tmp_dir = getcwd(tmp_dir, 0)) == NULL) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 121 | tst_brkm(TBROK, NULL, "getcwd failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
Cyril Hrubis | 116a8c0 | 2013-02-20 16:56:32 +0100 | [diff] [blame] | 123 | if ((pwent = getpwnam("bin")) == NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | tst_brkm(TBROK, cleanup, "getpwname() failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | |
plars | 248e9ed | 2001-08-31 18:14:12 +0000 | [diff] [blame] | 126 | /* make the tmp directory belong to bin */ |
Cyril Hrubis | 116a8c0 | 2013-02-20 16:56:32 +0100 | [diff] [blame] | 127 | if (chown(tmp_dir, pwent->pw_uid, pwent->pw_gid) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | tst_brkm(TBROK, cleanup, "chown() failed"); |
Cyril Hrubis | 116a8c0 | 2013-02-20 16:56:32 +0100 | [diff] [blame] | 129 | |
| 130 | if (chmod(tmp_dir, 0711) != 0) |
| 131 | tst_brkm(TBROK|TERRNO, cleanup, "chmod(%s) failed", tmp_dir); |
| 132 | |
| 133 | /* create test file and symlink */ |
| 134 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) |
| 135 | tst_brkm(TBROK|TERRNO, cleanup, "open(%s) failed", TESTFILE); |
| 136 | |
| 137 | if (close(fd)) |
| 138 | tst_brkm(TBROK|TERRNO, cleanup, "close(%s) failed", TESTFILE); |
| 139 | |
| 140 | if (symlink(TESTFILE, SYMFILE) < 0) { |
| 141 | tst_brkm(TBROK|TERRNO, cleanup, "symlink(%s, %s) failed", |
| 142 | TESTFILE, SYMFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | } |
| 144 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | /* set up the expected return value from the readlink() call */ |
| 146 | exp_val = strlen(TESTFILE); |
| 147 | |
| 148 | /* fill the buffer with a known value */ |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 149 | memset(buffer, 0, MAX_SIZE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | |
| 151 | /* finally, change the id of the parent process to "nobody" */ |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 152 | if ((pwent = getpwnam("nobody")) == NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | tst_brkm(TBROK, cleanup, "getpwname() failed for nobody"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 155 | if (seteuid(pwent->pw_uid) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | tst_brkm(TBROK, cleanup, "seteuid() failed for nobody"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 159 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | { |
Cyril Hrubis | 94bca9c | 2013-02-20 16:04:22 +0100 | [diff] [blame] | 161 | if (seteuid(0) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | tst_brkm(TBROK, NULL, "failed to set process id to root"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 165 | } |