• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  * Copyright (c) 2013 Cyril Hrubis <chrubis@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 <fcntl.h>
38 #include <sys/wait.h>
39 #include <errno.h>
40 #include <string.h>
41 #include "test.h"
42 #include "safe_macros.h"
43 
44 char *TCID = "readlink04";
45 int TST_TOTAL = 1;
46 
47 static char *TESTFILE = "./testfile";
48 static char *SYMFILE = "slink_file";
49 
50 #define MAX_SIZE	256
51 
52 static int exp_val;
53 static char buffer[MAX_SIZE];
54 
55 static void setup(void);
56 static void cleanup(void);
57 
main(int ac,char ** av)58 int main(int ac, char **av)
59 {
60 	int lc;
61 
62 	tst_parse_opts(ac, av, NULL, NULL);
63 
64 	setup();
65 
66 	for (lc = 0; TEST_LOOPING(lc); lc++) {
67 
68 		tst_count = 0;
69 
70 		/*
71 		 * Call readlink(2) to read the contents of
72 		 * symlink into a buffer.
73 		 */
74 		TEST(readlink(SYMFILE, buffer, sizeof(buffer)));
75 
76 		if (TEST_RETURN == -1) {
77 			tst_resm(TFAIL | TTERRNO, "readlink() on %s failed",
78 			         SYMFILE);
79 			continue;
80 		}
81 
82 		if (TEST_RETURN == exp_val) {
83 			/* Check for the contents of buffer */
84 			if (memcmp(buffer, TESTFILE, exp_val) != 0) {
85 				tst_brkm(TFAIL, cleanup, "TESTFILE %s "
86 					 "and buffer contents %s "
87 					 "differ", TESTFILE, buffer);
88 			} else {
89 				tst_resm(TPASS, "readlink() "
90 					 "functionality on '%s' is "
91 					 "correct", SYMFILE);
92 			}
93 		} else {
94 			tst_resm(TFAIL, "readlink() return value %ld "
95 				 "doesn't match, Expected %d",
96 				 TEST_RETURN, exp_val);
97 		}
98 	}
99 
100 	cleanup();
101 	tst_exit();
102 }
103 
104 #define FILE_MODE  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
105 
setup(void)106 static void setup(void)
107 {
108 	int fd;
109 	char *tmp_dir = NULL;
110 	struct passwd *pwent;
111 
112 	tst_require_root();
113 
114 	TEST_PAUSE;
115 
116 	tst_tmpdir();
117 
118 	/* get the name of the temporary directory */
119 	if ((tmp_dir = getcwd(tmp_dir, 0)) == NULL)
120 		tst_brkm(TBROK, NULL, "getcwd failed");
121 
122 	if ((pwent = getpwnam("bin")) == NULL)
123 		tst_brkm(TBROK, cleanup, "getpwname() failed");
124 
125 	/* make the tmp directory belong to bin */
126 	SAFE_CHOWN(cleanup, tmp_dir, pwent->pw_uid, pwent->pw_gid);
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 	SAFE_SYMLINK(cleanup, TESTFILE, SYMFILE);
139 
140 	/* set up the expected return value from the readlink() call */
141 	exp_val = strlen(TESTFILE);
142 
143 	/* fill the buffer with a known value */
144 	memset(buffer, 0, MAX_SIZE);
145 
146 	/* finally, change the id of the parent process to "nobody" */
147 	if ((pwent = getpwnam("nobody")) == NULL)
148 		tst_brkm(TBROK, cleanup, "getpwname() failed for nobody");
149 
150 	SAFE_SETEUID(cleanup, pwent->pw_uid);
151 }
152 
cleanup(void)153 static void cleanup(void)
154 {
155 	SAFE_SETEUID(NULL, 0);
156 
157 	tst_rmdir();
158 }
159