plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2000 |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 3 | * Copyright (c) 2010 Cyril Hrubis chrubis@suse.cz |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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) |
vapier | d319573 | 2008-04-28 03:22:29 +0000 | [diff] [blame] | 24 | * HISTORY : |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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. |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <string.h> |
robbiew | ddbe85a | 2003-04-15 16:40:46 +0000 | [diff] [blame] | 37 | #include <ctype.h> |
| 38 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <unistd.h> |
| 42 | #include <wait.h> |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 43 | #include <linux/limits.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | |
vapier | 64c3978 | 2008-05-06 15:57:10 +0000 | [diff] [blame] | 45 | #include "test.h" |
| 46 | |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 47 | #define TEST_FILE_NAME1 "./test.file1" |
| 48 | #define TEST_FILE_NAME2 "./test.file2" |
| 49 | |
vapier | 64c3978 | 2008-05-06 15:57:10 +0000 | [diff] [blame] | 50 | char *TCID = "fs_perms"; |
| 51 | int TST_TOTAL = 1; |
| 52 | |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 53 | static void cleanup(void) |
vapier | d319573 | 2008-04-28 03:22:29 +0000 | [diff] [blame] | 54 | { |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 55 | seteuid(0); |
| 56 | setegid(0); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 57 | |
subrata_modak | 7e67bd8 | 2009-10-18 18:09:26 +0000 | [diff] [blame] | 58 | tst_rmdir(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 59 | |
subrata_modak | 7e67bd8 | 2009-10-18 18:09:26 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 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 | */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 68 | static void testsetup(const char *file_name, int flag, mode_t mode, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 69 | int user_id, int group_id) |
vapier | d319573 | 2008-04-28 03:22:29 +0000 | [diff] [blame] | 70 | { |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 71 | FILE *file; |
| 72 | |
| 73 | file = fopen(file_name, "w"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 74 | |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 75 | if (file == NULL) |
| 76 | tst_brkm(TBROK | TERRNO, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | "Could not create test file %s.", file_name); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 78 | |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 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, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 85 | "Could not find path to sh in $PATH."); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 86 | |
| 87 | if (fprintf(file, "#!%s\n", buf) < 0) |
| 88 | tst_brkm(TBROK, cleanup, "Calling fprintf failed."); |
vapier | d319573 | 2008-04-28 03:22:29 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 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, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 96 | "Could not chmod test file %s.", file_name); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 97 | |
| 98 | if (chown(file_name, user_id, group_id)) |
| 99 | tst_brkm(TBROK | TERRNO, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 100 | "Could not chown test file %s.", file_name); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Test permissions. |
| 105 | */ |
| 106 | static int testfperm(const char *file_name, int flag, int user_id, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 107 | int group_id, char *fperm) |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 108 | { |
| 109 | FILE *file; |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 110 | int status; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 111 | |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 112 | switch (fork()) { |
| 113 | case 0: |
| 114 | if (setgid(group_id)) |
| 115 | tst_brkm(TBROK | TERRNO, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 116 | "Could not setgid to %d.", group_id); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 117 | |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 118 | if (setuid(user_id)) |
| 119 | tst_brkm(TBROK | TERRNO, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 120 | "Could not setuid to %d.", user_id); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 121 | |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 122 | switch (tolower(fperm[0])) { |
| 123 | case 'x': |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 124 | |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 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); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 133 | |
vapier | d2c75d0 | 2008-05-06 16:22:35 +0000 | [diff] [blame] | 134 | exit(1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 135 | break; |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 136 | default: |
| 137 | if ((file = fopen(file_name, fperm)) != NULL) { |
| 138 | fclose(file); |
| 139 | exit(0); |
| 140 | } |
| 141 | exit(1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 142 | break; |
vapier | d2c75d0 | 2008-05-06 16:22:35 +0000 | [diff] [blame] | 143 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 144 | break; |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 145 | case -1: |
| 146 | tst_brkm(TBROK | TERRNO, cleanup, "fork failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 147 | break; |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 148 | default: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 149 | break; |
vapier | d319573 | 2008-04-28 03:22:29 +0000 | [diff] [blame] | 150 | } |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 151 | |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 152 | wait(&status); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 153 | |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 154 | return WEXITSTATUS(status); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static void print_usage(const char *bname) |
| 158 | { |
| 159 | char *usage = "<file mode> <file UID> <file GID> " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 160 | "<tester UID> <tester GID> <permission " |
| 161 | "to test r|w|x> <expected result 0|1>"; |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 162 | |
| 163 | printf("Usage: %s %s\n", bname, usage); |
| 164 | } |
| 165 | |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 166 | static long str_to_l(const char *str, const char *name, int base) |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 167 | { |
| 168 | char *end; |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 169 | long i = strtol(str, &end, base); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 170 | |
| 171 | if (*end != '\0') |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 172 | tst_brkm(TBROK, NULL, "Invalid parameter '%s' passed. (%s)", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 173 | name, str); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 174 | |
| 175 | return i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | } |
| 177 | |
vapier | d319573 | 2008-04-28 03:22:29 +0000 | [diff] [blame] | 178 | int main(int argc, char *argv[]) |
| 179 | { |
| 180 | char *fperm; |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 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; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | |
Cyril Hrubis | 91bfa5b | 2014-02-12 17:28:17 +0100 | [diff] [blame] | 187 | tst_require_root(NULL); |
vapier | 64c3978 | 2008-05-06 15:57:10 +0000 | [diff] [blame] | 188 | |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 189 | if (argc != 8) { |
| 190 | print_usage(argv[0]); |
| 191 | tst_exit(); |
vapier | d319573 | 2008-04-28 03:22:29 +0000 | [diff] [blame] | 192 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 194 | if (strlen(argv[6]) > 1) { |
| 195 | print_usage(argv[0]); |
| 196 | tst_exit(); |
vapier | d319573 | 2008-04-28 03:22:29 +0000 | [diff] [blame] | 197 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 199 | fmode = str_to_l(argv[1], "file mode", 8); |
| 200 | fuser_id = str_to_l(argv[2], "file uid", 10); |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 201 | fgroup_id = str_to_l(argv[3], "file gid", 10); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 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); |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 206 | |
| 207 | tst_tmpdir(); |
| 208 | testsetup(TEST_FILE_NAME1, 0, fmode, fuser_id, fgroup_id); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 209 | |
Subrata Modak | ca758b7 | 2010-05-18 01:28:14 +0530 | [diff] [blame] | 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 | |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 223 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 225 | fperm[0], fmode, fuser_id, fgroup_id, user_id, group_id); |
Cyril Hrubis | 8779468 | 2011-02-25 17:21:47 +0100 | [diff] [blame] | 226 | } |