blob: 71e760c88ef1b54442ba421cf677e2133a2ef618 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis94bca9c2013-02-20 16:04:22 +01002 * Copyright (c) International Business Machines Corp., 2001
Cyril Hrubis572f0e02013-02-20 16:20:25 +01003 * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubis94bca9c2013-02-20 16:04:22 +01005 * 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.
plars865695b2001-08-27 22:15:12 +00009 *
Cyril Hrubis94bca9c2013-02-20 16:04:22 +010010 * 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.
plars865695b2001-08-27 22:15:12 +000014 *
Cyril Hrubis94bca9c2013-02-20 16:04:22 +010015 * 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
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
plars0111e622001-09-06 13:04:23 +000021 * Test Name : readlink04
plars865695b2001-08-27 22:15:12 +000022 *
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_modak4bb656a2009-02-26 12:02:09 +000027 *
plars865695b2001-08-27 22:15:12 +000028 * Expected Result:
29 * readlink() should return the contents of symbolic link path in the
30 * specified buffer on success.
plars865695b2001-08-27 22:15:12 +000031 */
Cyril Hrubis94bca9c2013-02-20 16:04:22 +010032
plars865695b2001-08-27 22:15:12 +000033#include <stdlib.h>
34#include <pwd.h>
35#include <sys/types.h>
plars74948ad2002-11-14 16:16:14 +000036#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000037#include <sys/fcntl.h>
robbiewdfa1a692002-07-09 15:32:36 +000038#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000039#include <errno.h>
robbiewa4f4a222001-09-12 21:36:09 +000040#include <string.h>
plars865695b2001-08-27 22:15:12 +000041#include "test.h"
plars865695b2001-08-27 22:15:12 +000042
Cyril Hrubis94bca9c2013-02-20 16:04:22 +010043char *TCID = "readlink04";
44int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000045
Cyril Hrubis116a8c02013-02-20 16:56:32 +010046static char *TESTFILE = "./testfile";
47static char *SYMFILE = "slink_file";
plars865695b2001-08-27 22:15:12 +000048
49#define MAX_SIZE 256
50
Cyril Hrubis94bca9c2013-02-20 16:04:22 +010051static int exp_val;
52static char buffer[MAX_SIZE];
plars865695b2001-08-27 22:15:12 +000053
Cyril Hrubis94bca9c2013-02-20 16:04:22 +010054static void setup(void);
55static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000056
subrata_modak56207ce2009-03-23 13:35:39 +000057int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000058{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020059 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020060 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000061
Garrett Coopera9e49f12010-12-16 10:54:03 -080062 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080063 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000064
plars865695b2001-08-27 22:15:12 +000065 setup();
66
plars865695b2001-08-27 22:15:12 +000067 for (lc = 0; TEST_LOOPING(lc); lc++) {
68
Caspar Zhangd59a6592013-03-07 14:59:12 +080069 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +000070
subrata_modak4bb656a2009-02-26 12:02:09 +000071 /*
plars865695b2001-08-27 22:15:12 +000072 * Call readlink(2) to read the contents of
73 * symlink into a buffer.
74 */
Cyril Hrubis116a8c02013-02-20 16:56:32 +010075 TEST(readlink(SYMFILE, buffer, sizeof(buffer)));
subrata_modakbdbaec52009-02-26 12:14:51 +000076
plars865695b2001-08-27 22:15:12 +000077 if (TEST_RETURN == -1) {
Cyril Hrubis94bca9c2013-02-20 16:04:22 +010078 tst_resm(TFAIL | TTERRNO, "readlink() on %s failed",
Cyril Hrubis116a8c02013-02-20 16:56:32 +010079 SYMFILE);
plars865695b2001-08-27 22:15:12 +000080 continue;
81 }
82
Cyril Hrubise38b9612014-06-02 17:20:57 +020083 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);
plars865695b2001-08-27 22:15:12 +000089 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +020090 tst_resm(TPASS, "readlink() "
91 "functionality on '%s' is "
92 "correct", SYMFILE);
plars865695b2001-08-27 22:15:12 +000093 }
94 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +020095 tst_resm(TFAIL, "readlink() return value %ld "
96 "doesn't match, Expected %d",
97 TEST_RETURN, exp_val);
plars865695b2001-08-27 22:15:12 +000098 }
Garrett Cooper2c282152010-12-16 00:55:50 -080099 }
100
plars865695b2001-08-27 22:15:12 +0000101 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800102 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800103}
plars865695b2001-08-27 22:15:12 +0000104
Cyril Hrubis116a8c02013-02-20 16:56:32 +0100105#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
106
Cyril Hrubis94bca9c2013-02-20 16:04:22 +0100107static void setup(void)
plars865695b2001-08-27 22:15:12 +0000108{
Cyril Hrubis116a8c02013-02-20 16:56:32 +0100109 int fd;
plars865695b2001-08-27 22:15:12 +0000110 char *tmp_dir = NULL;
plars865695b2001-08-27 22:15:12 +0000111 struct passwd *pwent;
112
Cyril Hrubis94bca9c2013-02-20 16:04:22 +0100113 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000114
plars865695b2001-08-27 22:15:12 +0000115 TEST_PAUSE;
116
plars865695b2001-08-27 22:15:12 +0000117 tst_tmpdir();
118
119 /* get the name of the temporary directory */
Cyril Hrubis116a8c02013-02-20 16:56:32 +0100120 if ((tmp_dir = getcwd(tmp_dir, 0)) == NULL)
Garrett Cooper53740502010-12-16 00:04:01 -0800121 tst_brkm(TBROK, NULL, "getcwd failed");
plars865695b2001-08-27 22:15:12 +0000122
Cyril Hrubis116a8c02013-02-20 16:56:32 +0100123 if ((pwent = getpwnam("bin")) == NULL)
plars865695b2001-08-27 22:15:12 +0000124 tst_brkm(TBROK, cleanup, "getpwname() failed");
plars865695b2001-08-27 22:15:12 +0000125
plars248e9ed2001-08-31 18:14:12 +0000126 /* make the tmp directory belong to bin */
Cyril Hrubis116a8c02013-02-20 16:56:32 +0100127 if (chown(tmp_dir, pwent->pw_uid, pwent->pw_gid) == -1)
plars865695b2001-08-27 22:15:12 +0000128 tst_brkm(TBROK, cleanup, "chown() failed");
Cyril Hrubis116a8c02013-02-20 16:56:32 +0100129
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);
plars865695b2001-08-27 22:15:12 +0000143 }
144
plars865695b2001-08-27 22:15:12 +0000145 /* 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 Hrubis94bca9c2013-02-20 16:04:22 +0100149 memset(buffer, 0, MAX_SIZE);
plars865695b2001-08-27 22:15:12 +0000150
151 /* finally, change the id of the parent process to "nobody" */
Cyril Hrubis94bca9c2013-02-20 16:04:22 +0100152 if ((pwent = getpwnam("nobody")) == NULL)
plars865695b2001-08-27 22:15:12 +0000153 tst_brkm(TBROK, cleanup, "getpwname() failed for nobody");
plars865695b2001-08-27 22:15:12 +0000154
Cyril Hrubis94bca9c2013-02-20 16:04:22 +0100155 if (seteuid(pwent->pw_uid) == -1)
plars865695b2001-08-27 22:15:12 +0000156 tst_brkm(TBROK, cleanup, "seteuid() failed for nobody");
plars865695b2001-08-27 22:15:12 +0000157}
158
Cyril Hrubis94bca9c2013-02-20 16:04:22 +0100159static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000160{
Cyril Hrubis94bca9c2013-02-20 16:04:22 +0100161 if (seteuid(0) == -1)
plars865695b2001-08-27 22:15:12 +0000162 tst_brkm(TBROK, NULL, "failed to set process id to root");
plars865695b2001-08-27 22:15:12 +0000163
plars865695b2001-08-27 22:15:12 +0000164 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700165}