• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   Copyright (c) International Business Machines  Corp., 2000
3  *   Copyright (c) 2010 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  *  FILE(s)     : fs_perms.c simpletest.sh textx.o Makefile README
22  *  DESCRIPTION : Regression test for Linux filesystem permissions.
23  *  AUTHOR      : Jeff Martin (martinjn@us.ibm.com)
24  *  HISTORY     :
25  *     (04/12/01)v.99  First attempt at using C for fs-regression test.  Only tests read and write bits.
26  *     (04/19/01)v1.0  Added test for execute bit.
27  *     (05/23/01)v1.1  Added command line parameter to specify test file.
28  *     (07/12/01)v1.2  Removed conf file and went to command line parameters.
29  *     (10/19/04)      Rewritten to fit ltp test interface.
30  *                     Also now we try to run two different files, one is executed by execl,
31  *                     has shebang and should end up executed by kernel, other one is empty
32  *                     is executed by execlp and should end up executed by libc.
33  */
34 
35 #include <stdio.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <sys/wait.h>
43 #include <linux/limits.h>
44 
45 #include "test.h"
46 
47 #define TEST_FILE_NAME1 "./test.file1"
48 #define TEST_FILE_NAME2 "./test.file2"
49 
50 char *TCID = "fs_perms";
51 int TST_TOTAL = 1;
52 
cleanup(void)53 static void cleanup(void)
54 {
55 	seteuid(0);
56 	setegid(0);
57 
58 	tst_rmdir();
59 
60 }
61 
62 /*
63  * Create file and set permissions, user id, group id.
64  *
65  * If flag is non zero, the file contains #!/PATH/sh shebang otherwise it's
66  * empty.
67  */
testsetup(const char * file_name,int flag,mode_t mode,int user_id,int group_id)68 static void testsetup(const char *file_name, int flag, mode_t mode,
69 		      int user_id, int group_id)
70 {
71 	FILE *file;
72 
73 	file = fopen(file_name, "w");
74 
75 	if (file == NULL)
76 		tst_brkm(TBROK | TERRNO, cleanup,
77 			 "Could not create test file %s.", file_name);
78 
79 	/* create file with shebang */
80 	if (flag) {
81 		char buf[PATH_MAX];
82 
83 		if (tst_get_path("sh", buf, PATH_MAX))
84 			tst_brkm(TBROK, cleanup,
85 				 "Could not find path to sh in $PATH.");
86 
87 		if (fprintf(file, "#!%s\n", buf) < 0)
88 			tst_brkm(TBROK, cleanup, "Calling fprintf failed.");
89 	}
90 
91 	if (fclose(file))
92 		tst_brkm(TBROK | TERRNO, cleanup, "Calling fclose failed.");
93 
94 	if (chmod(file_name, mode))
95 		tst_brkm(TBROK | TERRNO, cleanup,
96 			 "Could not chmod test file %s.", file_name);
97 
98 	if (chown(file_name, user_id, group_id))
99 		tst_brkm(TBROK | TERRNO, cleanup,
100 			 "Could not chown test file %s.", file_name);
101 }
102 
103 /*
104  * Test permissions.
105  */
testfperm(const char * file_name,int flag,int user_id,int group_id,char * fperm)106 static int testfperm(const char *file_name, int flag, int user_id,
107 		     int group_id, char *fperm)
108 {
109 	FILE *file;
110 	int status;
111 
112 	switch (fork()) {
113 	case 0:
114 		if (setgid(group_id))
115 			tst_brkm(TBROK | TERRNO, cleanup,
116 				 "Could not setgid to %d.", group_id);
117 
118 		if (setuid(user_id))
119 			tst_brkm(TBROK | TERRNO, cleanup,
120 				 "Could not setuid to %d.", user_id);
121 
122 		switch (tolower(fperm[0])) {
123 		case 'x':
124 
125 			/*
126 			 * execlp runs file with sh in case kernel has
127 			 * no binmft handler for it, execl does not.
128 			 */
129 			if (flag)
130 				execl(file_name, file_name, NULL);
131 			else
132 				execlp(file_name, "test", NULL);
133 
134 			exit(1);
135 			break;
136 		default:
137 			if ((file = fopen(file_name, fperm)) != NULL) {
138 				fclose(file);
139 				exit(0);
140 			}
141 			exit(1);
142 			break;
143 		}
144 		break;
145 	case -1:
146 		tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
147 		break;
148 	default:
149 		break;
150 	}
151 
152 	wait(&status);
153 
154 	return WEXITSTATUS(status);
155 }
156 
print_usage(const char * bname)157 static void print_usage(const char *bname)
158 {
159 	char *usage = "<file mode> <file UID> <file GID> "
160 	    "<tester UID> <tester GID> <permission "
161 	    "to test r|w|x> <expected result 0|1>";
162 
163 	printf("Usage: %s %s\n", bname, usage);
164 }
165 
str_to_l(const char * str,const char * name,int base)166 static long str_to_l(const char *str, const char *name, int base)
167 {
168 	char *end;
169 	long i = strtol(str, &end, base);
170 
171 	if (*end != '\0')
172 		tst_brkm(TBROK, NULL, "Invalid parameter '%s' passed. (%s)",
173 			 name, str);
174 
175 	return i;
176 }
177 
main(int argc,char * argv[])178 int main(int argc, char *argv[])
179 {
180 	char *fperm;
181 	gid_t fgroup_id, group_id;
182 	uid_t fuser_id, user_id;
183 	mode_t fmode;
184 	int exp_res;
185 	int res1, res2 = 1;
186 
187 	tst_require_root();
188 
189 	if (argc != 8) {
190 		print_usage(argv[0]);
191 		tst_exit();
192 	}
193 
194 	if (strlen(argv[6]) > 1) {
195 		print_usage(argv[0]);
196 		tst_exit();
197 	}
198 
199 	fmode = str_to_l(argv[1], "file mode", 8);
200 	fuser_id = str_to_l(argv[2], "file uid", 10);
201 	fgroup_id = str_to_l(argv[3], "file gid", 10);
202 	user_id = str_to_l(argv[4], "tester uid", 10);
203 	group_id = str_to_l(argv[5], "tester gid", 10);
204 	fperm = argv[6];
205 	exp_res = str_to_l(argv[7], "expected result", 10);
206 
207 	tst_tmpdir();
208 	testsetup(TEST_FILE_NAME1, 0, fmode, fuser_id, fgroup_id);
209 
210 	/* more tests for 'x' flag */
211 	if (tolower(fperm[0]) == 'x') {
212 		testsetup(TEST_FILE_NAME2, 1, fmode, fuser_id, fgroup_id);
213 		res2 = testfperm(TEST_FILE_NAME2, 1, user_id, group_id, fperm);
214 
215 		if (res2 == exp_res)
216 			res2 = 1;
217 		else
218 			res2 = 0;
219 	}
220 
221 	res1 = testfperm(TEST_FILE_NAME1, 0, user_id, group_id, fperm);
222 
223 	tst_resm((exp_res == res1) && res2 ? TPASS : TFAIL,
224 		 "%c a %03o file owned by (%d/%d) as user/group (%d/%d)",
225 		 fperm[0], fmode, fuser_id, fgroup_id, user_id, group_id);
226 	tst_rmdir();
227 	tst_exit();
228 }
229