1 /* 2 * Copyright (c) International Business Machines Corp., 2001 3 * Copyright (c) 2013 Cyril Hrubis <chrubis (at) suse.cz> 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 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 18 */ 19 20 /* 21 * Test Name : readlink04 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. 27 * 28 * Expected Result: 29 * readlink() should return the contents of symbolic link path in the 30 * specified buffer on success. 31 */ 32 33 #include <stdlib.h> 34 #include <pwd.h> 35 #include <sys/types.h> 36 #include <sys/stat.h> 37 #include <sys/fcntl.h> 38 #include <sys/wait.h> 39 #include <errno.h> 40 #include <string.h> 41 #include "test.h" 42 43 char *TCID = "readlink04"; 44 int TST_TOTAL = 1; 45 46 static char *TESTFILE = "./testfile"; 47 static char *SYMFILE = "slink_file"; 48 49 #define MAX_SIZE 256 50 51 static int exp_val; 52 static char buffer[MAX_SIZE]; 53 54 static void setup(void); 55 static void cleanup(void); 56 57 int main(int ac, char **av) 58 { 59 int lc; 60 61 tst_parse_opts(ac, av, NULL, NULL); 62 63 setup(); 64 65 for (lc = 0; TEST_LOOPING(lc); lc++) { 66 67 tst_count = 0; 68 69 /* 70 * Call readlink(2) to read the contents of 71 * symlink into a buffer. 72 */ 73 TEST(readlink(SYMFILE, buffer, sizeof(buffer))); 74 75 if (TEST_RETURN == -1) { 76 tst_resm(TFAIL | TTERRNO, "readlink() on %s failed", 77 SYMFILE); 78 continue; 79 } 80 81 if (TEST_RETURN == exp_val) { 82 /* Check for the contents of buffer */ 83 if (memcmp(buffer, TESTFILE, exp_val) != 0) { 84 tst_brkm(TFAIL, cleanup, "TESTFILE %s " 85 "and buffer contents %s " 86 "differ", TESTFILE, buffer); 87 } else { 88 tst_resm(TPASS, "readlink() " 89 "functionality on '%s' is " 90 "correct", SYMFILE); 91 } 92 } else { 93 tst_resm(TFAIL, "readlink() return value %ld " 94 "doesn't match, Expected %d", 95 TEST_RETURN, exp_val); 96 } 97 } 98 99 cleanup(); 100 tst_exit(); 101 } 102 103 #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH 104 105 static void setup(void) 106 { 107 int fd; 108 char *tmp_dir = NULL; 109 struct passwd *pwent; 110 111 tst_require_root(); 112 113 TEST_PAUSE; 114 115 tst_tmpdir(); 116 117 /* get the name of the temporary directory */ 118 if ((tmp_dir = getcwd(tmp_dir, 0)) == NULL) 119 tst_brkm(TBROK, NULL, "getcwd failed"); 120 121 if ((pwent = getpwnam("bin")) == NULL) 122 tst_brkm(TBROK, cleanup, "getpwname() failed"); 123 124 /* make the tmp directory belong to bin */ 125 if (chown(tmp_dir, pwent->pw_uid, pwent->pw_gid) == -1) 126 tst_brkm(TBROK, cleanup, "chown() failed"); 127 128 if (chmod(tmp_dir, 0711) != 0) 129 tst_brkm(TBROK|TERRNO, cleanup, "chmod(%s) failed", tmp_dir); 130 131 /* create test file and symlink */ 132 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) 133 tst_brkm(TBROK|TERRNO, cleanup, "open(%s) failed", TESTFILE); 134 135 if (close(fd)) 136 tst_brkm(TBROK|TERRNO, cleanup, "close(%s) failed", TESTFILE); 137 138 if (symlink(TESTFILE, SYMFILE) < 0) { 139 tst_brkm(TBROK|TERRNO, cleanup, "symlink(%s, %s) failed", 140 TESTFILE, SYMFILE); 141 } 142 143 /* set up the expected return value from the readlink() call */ 144 exp_val = strlen(TESTFILE); 145 146 /* fill the buffer with a known value */ 147 memset(buffer, 0, MAX_SIZE); 148 149 /* finally, change the id of the parent process to "nobody" */ 150 if ((pwent = getpwnam("nobody")) == NULL) 151 tst_brkm(TBROK, cleanup, "getpwname() failed for nobody"); 152 153 if (seteuid(pwent->pw_uid) == -1) 154 tst_brkm(TBROK, cleanup, "seteuid() failed for nobody"); 155 } 156 157 static void cleanup(void) 158 { 159 if (seteuid(0) == -1) 160 tst_brkm(TBROK, NULL, "failed to set process id to root"); 161 162 tst_rmdir(); 163 } 164