blob: 02e7735a3bfbc8992dadd8593dd5716c210926e6 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
plars865695b2001-08-27 22:15:12 +00002 * Copyright (c) International Business Machines Corp., 2000
Subrata Modakca758b72010-05-18 01:28:14 +05303 * Copyright (c) 2010 Cyril Hrubis chrubis@suse.cz
plars865695b2001-08-27 22:15:12 +00004 *
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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)
vapierd3195732008-04-28 03:22:29 +000024 * HISTORY :
plars865695b2001-08-27 22:15:12 +000025 * (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.
Subrata Modakca758b72010-05-18 01:28:14 +053029 * (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.
plars865695b2001-08-27 22:15:12 +000033 */
34
35#include <stdio.h>
36#include <string.h>
robbiewddbe85a2003-04-15 16:40:46 +000037#include <ctype.h>
38#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000039#include <sys/types.h>
40#include <stdlib.h>
41#include <unistd.h>
42#include <wait.h>
Subrata Modakca758b72010-05-18 01:28:14 +053043#include <linux/limits.h>
plars865695b2001-08-27 22:15:12 +000044
vapier64c39782008-05-06 15:57:10 +000045#include "test.h"
46
Subrata Modakca758b72010-05-18 01:28:14 +053047#define TEST_FILE_NAME1 "./test.file1"
48#define TEST_FILE_NAME2 "./test.file2"
49
vapier64c39782008-05-06 15:57:10 +000050char *TCID = "fs_perms";
51int TST_TOTAL = 1;
52
Subrata Modakca758b72010-05-18 01:28:14 +053053static void cleanup(void)
vapierd3195732008-04-28 03:22:29 +000054{
Subrata Modakca758b72010-05-18 01:28:14 +053055 seteuid(0);
56 setegid(0);
Garrett Cooper2c282152010-12-16 00:55:50 -080057
subrata_modak7e67bd82009-10-18 18:09:26 +000058 tst_rmdir();
Garrett Cooper2c282152010-12-16 00:55:50 -080059
subrata_modak7e67bd82009-10-18 18:09:26 +000060}
61
Subrata Modakca758b72010-05-18 01:28:14 +053062/*
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 */
Garrett Cooper2c282152010-12-16 00:55:50 -080068static void testsetup(const char *file_name, int flag, mode_t mode,
Wanlong Gao354ebb42012-12-07 10:10:04 +080069 int user_id, int group_id)
vapierd3195732008-04-28 03:22:29 +000070{
Subrata Modakca758b72010-05-18 01:28:14 +053071 FILE *file;
72
73 file = fopen(file_name, "w");
Garrett Cooper2c282152010-12-16 00:55:50 -080074
Subrata Modakca758b72010-05-18 01:28:14 +053075 if (file == NULL)
76 tst_brkm(TBROK | TERRNO, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 "Could not create test file %s.", file_name);
Garrett Cooper2c282152010-12-16 00:55:50 -080078
Subrata Modakca758b72010-05-18 01:28:14 +053079 /* 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,
Wanlong Gao354ebb42012-12-07 10:10:04 +080085 "Could not find path to sh in $PATH.");
Subrata Modakca758b72010-05-18 01:28:14 +053086
87 if (fprintf(file, "#!%s\n", buf) < 0)
88 tst_brkm(TBROK, cleanup, "Calling fprintf failed.");
vapierd3195732008-04-28 03:22:29 +000089 }
90
Subrata Modakca758b72010-05-18 01:28:14 +053091 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,
Wanlong Gao354ebb42012-12-07 10:10:04 +080096 "Could not chmod test file %s.", file_name);
Subrata Modakca758b72010-05-18 01:28:14 +053097
98 if (chown(file_name, user_id, group_id))
99 tst_brkm(TBROK | TERRNO, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800100 "Could not chown test file %s.", file_name);
Subrata Modakca758b72010-05-18 01:28:14 +0530101}
102
103/*
104 * Test permissions.
105 */
106static int testfperm(const char *file_name, int flag, int user_id,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800107 int group_id, char *fperm)
Subrata Modakca758b72010-05-18 01:28:14 +0530108{
109 FILE *file;
Cyril Hrubis87794682011-02-25 17:21:47 +0100110 int status;
Garrett Cooper2c282152010-12-16 00:55:50 -0800111
Cyril Hrubis87794682011-02-25 17:21:47 +0100112 switch (fork()) {
113 case 0:
114 if (setgid(group_id))
115 tst_brkm(TBROK | TERRNO, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800116 "Could not setgid to %d.", group_id);
Subrata Modakca758b72010-05-18 01:28:14 +0530117
Cyril Hrubis87794682011-02-25 17:21:47 +0100118 if (setuid(user_id))
119 tst_brkm(TBROK | TERRNO, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120 "Could not setuid to %d.", user_id);
Subrata Modakca758b72010-05-18 01:28:14 +0530121
Cyril Hrubis87794682011-02-25 17:21:47 +0100122 switch (tolower(fperm[0])) {
123 case 'x':
Garrett Cooper2c282152010-12-16 00:55:50 -0800124
Subrata Modakca758b72010-05-18 01:28:14 +0530125 /*
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);
Garrett Cooper2c282152010-12-16 00:55:50 -0800133
vapierd2c75d02008-05-06 16:22:35 +0000134 exit(1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135 break;
Cyril Hrubis87794682011-02-25 17:21:47 +0100136 default:
137 if ((file = fopen(file_name, fperm)) != NULL) {
138 fclose(file);
139 exit(0);
140 }
141 exit(1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 break;
vapierd2c75d02008-05-06 16:22:35 +0000143 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 break;
Cyril Hrubis87794682011-02-25 17:21:47 +0100145 case -1:
146 tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800147 break;
Cyril Hrubis87794682011-02-25 17:21:47 +0100148 default:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800149 break;
vapierd3195732008-04-28 03:22:29 +0000150 }
Chris Dearman37550cf2012-10-17 19:54:01 -0700151
Cyril Hrubis87794682011-02-25 17:21:47 +0100152 wait(&status);
Subrata Modakca758b72010-05-18 01:28:14 +0530153
Cyril Hrubis87794682011-02-25 17:21:47 +0100154 return WEXITSTATUS(status);
Subrata Modakca758b72010-05-18 01:28:14 +0530155}
156
157static void print_usage(const char *bname)
158{
159 char *usage = "<file mode> <file UID> <file GID> "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800160 "<tester UID> <tester GID> <permission "
161 "to test r|w|x> <expected result 0|1>";
Subrata Modakca758b72010-05-18 01:28:14 +0530162
163 printf("Usage: %s %s\n", bname, usage);
164}
165
Cyril Hrubis87794682011-02-25 17:21:47 +0100166static long str_to_l(const char *str, const char *name, int base)
Subrata Modakca758b72010-05-18 01:28:14 +0530167{
168 char *end;
Cyril Hrubis87794682011-02-25 17:21:47 +0100169 long i = strtol(str, &end, base);
Subrata Modakca758b72010-05-18 01:28:14 +0530170
171 if (*end != '\0')
Garrett Cooper53740502010-12-16 00:04:01 -0800172 tst_brkm(TBROK, NULL, "Invalid parameter '%s' passed. (%s)",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800173 name, str);
Subrata Modakca758b72010-05-18 01:28:14 +0530174
175 return i;
plars865695b2001-08-27 22:15:12 +0000176}
177
vapierd3195732008-04-28 03:22:29 +0000178int main(int argc, char *argv[])
179{
180 char *fperm;
Subrata Modakca758b72010-05-18 01:28:14 +0530181 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;
plars865695b2001-08-27 22:15:12 +0000186
Cyril Hrubis91bfa5b2014-02-12 17:28:17 +0100187 tst_require_root(NULL);
vapier64c39782008-05-06 15:57:10 +0000188
Subrata Modakca758b72010-05-18 01:28:14 +0530189 if (argc != 8) {
190 print_usage(argv[0]);
191 tst_exit();
vapierd3195732008-04-28 03:22:29 +0000192 }
plars865695b2001-08-27 22:15:12 +0000193
Subrata Modakca758b72010-05-18 01:28:14 +0530194 if (strlen(argv[6]) > 1) {
195 print_usage(argv[0]);
196 tst_exit();
vapierd3195732008-04-28 03:22:29 +0000197 }
plars865695b2001-08-27 22:15:12 +0000198
Wanlong Gao354ebb42012-12-07 10:10:04 +0800199 fmode = str_to_l(argv[1], "file mode", 8);
200 fuser_id = str_to_l(argv[2], "file uid", 10);
Cyril Hrubis87794682011-02-25 17:21:47 +0100201 fgroup_id = str_to_l(argv[3], "file gid", 10);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800202 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);
Subrata Modakca758b72010-05-18 01:28:14 +0530206
207 tst_tmpdir();
208 testsetup(TEST_FILE_NAME1, 0, fmode, fuser_id, fgroup_id);
Garrett Cooper2c282152010-12-16 00:55:50 -0800209
Subrata Modakca758b72010-05-18 01:28:14 +0530210 /* 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
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100223 tst_brkm((exp_res == res1) && res2 ? TPASS : TFAIL,
224 tst_rmdir, "%c a %03o file owned by (%d/%d) as user/group (%d/%d)",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800225 fperm[0], fmode, fuser_id, fgroup_id, user_id, group_id);
Cyril Hrubis87794682011-02-25 17:21:47 +0100226}