plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 4 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 14 | * the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 17 | * along with this program; if not, write to the Free Software Foundation, |
| 18 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, |
| 25 | * 1) mknod(2) returns -1 and sets errno to EPERM if the process id of |
| 26 | * the caller is not super-user. |
| 27 | * 2) mknod(2) returns -1 and sets errno to EACCES if parent directory |
| 28 | * does not allow write permission to the process. |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 29 | * 3) mknod(2) returns -1 and sets errno to EROFS if pathname refers to |
| 30 | * a file on a read-only file system. |
| 31 | * 4) mknod(2) returns -1 and sets errno to ELOOP if too many symbolic |
| 32 | * links were encountered in resolving pathname. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <unistd.h> |
| 39 | #include <errno.h> |
| 40 | #include <string.h> |
| 41 | #include <signal.h> |
| 42 | #include <pwd.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/stat.h> |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 45 | #include <sys/mount.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | |
| 47 | #include "test.h" |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 48 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 50 | #define DIR_TEMP "testdir_1" |
| 51 | #define DIR_TEMP_MODE (S_IRUSR | S_IXUSR) |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 52 | #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \ |
| 53 | S_IXGRP|S_IROTH|S_IXOTH) |
| 54 | #define MNT_POINT "mntpoint" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 56 | #define FIFO_MODE (S_IFIFO | S_IRUSR | S_IRGRP | S_IROTH) |
| 57 | #define SOCKET_MODE (S_IFSOCK | S_IRWXU | S_IRWXG | S_IRWXO) |
| 58 | #define CHR_MODE (S_IFCHR | S_IRUSR | S_IWUSR) |
| 59 | #define BLK_MODE (S_IFBLK | S_IRUSR | S_IWUSR) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 61 | #define ELOPFILE "/test_eloop" |
| 62 | |
| 63 | static char elooppathname[sizeof(ELOPFILE) * 43] = "."; |
| 64 | |
Cyril Hrubis | 4102818 | 2014-06-19 17:27:33 +0200 | [diff] [blame] | 65 | static const char *device; |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 66 | static int mount_flag; |
| 67 | |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 68 | static struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | char *pathname; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | int mode; |
| 71 | int exp_errno; |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 72 | } test_cases[] = { |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 73 | { "testdir_1/tnode_1", SOCKET_MODE, EACCES }, |
| 74 | { "testdir_1/tnode_2", FIFO_MODE, EACCES }, |
| 75 | { "tnode_3", CHR_MODE, EPERM }, |
| 76 | { "tnode_4", BLK_MODE, EPERM }, |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 77 | { "mntpoint/tnode_5", SOCKET_MODE, EROFS }, |
| 78 | { elooppathname, FIFO_MODE, ELOOP }, |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 79 | }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 81 | char *TCID = "mknod07"; |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 82 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 84 | static void setup(void); |
| 85 | static void mknod_verify(const struct test_case_t *test_case); |
| 86 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 90 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 91 | const char *msg; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 92 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
Cyril Hrubis | 4102818 | 2014-06-19 17:27:33 +0200 | [diff] [blame] | 94 | msg = parse_opts(ac, av, NULL, NULL); |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 95 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | setup(); |
| 99 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 101 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 103 | for (i = 0; i < TST_TOTAL; i++) |
| 104 | mknod_verify(&test_cases[i]); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 105 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | cleanup(); |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 108 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 109 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 111 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | { |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 113 | int i; |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 114 | struct passwd *ltpuser; |
Cyril Hrubis | 4102818 | 2014-06-19 17:27:33 +0200 | [diff] [blame] | 115 | const char *fs_type; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 117 | tst_require_root(NULL); |
| 118 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | tst_tmpdir(); |
| 122 | |
Cyril Hrubis | 4102818 | 2014-06-19 17:27:33 +0200 | [diff] [blame] | 123 | fs_type = tst_dev_fs_type(); |
| 124 | device = tst_acquire_device(cleanup); |
| 125 | |
| 126 | if (!device) |
| 127 | tst_brkm(TCONF, cleanup, "Failed to acquire device"); |
| 128 | |
| 129 | tst_mkfs(cleanup, device, fs_type, NULL); |
| 130 | |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 131 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 133 | /* mount a read-only file system for EROFS test */ |
| 134 | SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE); |
Cyril Hrubis | 4102818 | 2014-06-19 17:27:33 +0200 | [diff] [blame] | 135 | if (mount(device, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) { |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 136 | tst_brkm(TBROK | TERRNO, cleanup, |
| 137 | "mount device:%s failed", device); |
| 138 | } |
| 139 | mount_flag = 1; |
| 140 | |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 141 | ltpuser = SAFE_GETPWNAM(cleanup, "nobody"); |
| 142 | SAFE_SETEUID(cleanup, ltpuser->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 144 | SAFE_MKDIR(cleanup, DIR_TEMP, DIR_TEMP_MODE); |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 145 | |
| 146 | /* |
| 147 | * NOTE: the ELOOP test is written based on that the consecutive |
| 148 | * symlinks limits in kernel is hardwired to 40. |
| 149 | */ |
| 150 | SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE); |
| 151 | SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop"); |
| 152 | for (i = 0; i < 43; i++) |
| 153 | strcat(elooppathname, ELOPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 156 | static void mknod_verify(const struct test_case_t *test_case) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | { |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 158 | TEST(mknod(test_case->pathname, test_case->mode, 0)); |
| 159 | |
| 160 | if (TEST_RETURN != -1) { |
| 161 | tst_resm(TFAIL, "mknod succeeded unexpectedly"); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | if (TEST_ERRNO == test_case->exp_errno) { |
| 166 | tst_resm(TPASS | TTERRNO, "mknod failed as expected"); |
| 167 | } else { |
| 168 | tst_resm(TFAIL | TTERRNO, |
| 169 | "mknod failed unexpectedly; expected: " |
| 170 | "%d - %s", test_case->exp_errno, |
| 171 | strerror(test_case->exp_errno)); |
| 172 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 175 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 177 | if (seteuid(0) == -1) |
Xiaoguang Wang | 47f7fa0 | 2014-02-26 20:32:25 +0800 | [diff] [blame] | 178 | tst_resm(TWARN | TERRNO, "seteuid(0) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 180 | if (mount_flag && tst_umount(MNT_POINT) < 0) |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 181 | tst_resm(TWARN | TERRNO, "umount device:%s failed", device); |
| 182 | |
Cyril Hrubis | 4102818 | 2014-06-19 17:27:33 +0200 | [diff] [blame] | 183 | if (device) |
| 184 | tst_release_device(NULL, device); |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 185 | |
Cyril Hrubis | 4102818 | 2014-06-19 17:27:33 +0200 | [diff] [blame] | 186 | tst_rmdir(); |
Xiaoguang Wang | 1e24e42 | 2014-02-26 20:32:26 +0800 | [diff] [blame] | 187 | } |