1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 * 07/2001 Ported by Wayne Boyer
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
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
19 */
20
21 /*
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.
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.
33 *
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>
45 #include <sys/mount.h>
46 #include <sys/sysmacros.h>
47
48 #include "test.h"
49 #include "safe_macros.h"
50
51 #define DIR_TEMP "testdir_1"
52 #define DIR_TEMP_MODE (S_IRUSR | S_IXUSR)
53 #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
54 S_IXGRP|S_IROTH|S_IXOTH)
55 #define MNT_POINT "mntpoint"
56
57 #define FIFO_MODE (S_IFIFO | S_IRUSR | S_IRGRP | S_IROTH)
58 #define SOCKET_MODE (S_IFSOCK | S_IRWXU | S_IRWXG | S_IRWXO)
59 #define CHR_MODE (S_IFCHR | S_IRUSR | S_IWUSR)
60 #define BLK_MODE (S_IFBLK | S_IRUSR | S_IWUSR)
61
62 #define ELOPFILE "/test_eloop"
63
64 static char elooppathname[sizeof(ELOPFILE) * 43] = ".";
65
66 static const char *device;
67 static int mount_flag;
68
69 static struct test_case_t {
70 char *pathname;
71 int mode;
72 int exp_errno;
73 int major, minor;
74 } test_cases[] = {
75 { "testdir_1/tnode_1", SOCKET_MODE, EACCES, 0, 0 },
76 { "testdir_1/tnode_2", FIFO_MODE, EACCES, 0, 0 },
77 { "tnode_3", CHR_MODE, EPERM, 1, 3 },
78 { "tnode_4", BLK_MODE, EPERM, 0, 0 },
79 { "mntpoint/tnode_5", SOCKET_MODE, EROFS, 0, 0 },
80 { elooppathname, FIFO_MODE, ELOOP, 0, 0 },
81 };
82
83 char *TCID = "mknod07";
84 int TST_TOTAL = ARRAY_SIZE(test_cases);
85
86 static void setup(void);
87 static void mknod_verify(const struct test_case_t *test_case);
88 static void cleanup(void);
89
main(int ac,char ** av)90 int main(int ac, char **av)
91 {
92 int lc;
93 int i;
94
95 tst_parse_opts(ac, av, NULL, NULL);
96
97 setup();
98
99 for (lc = 0; TEST_LOOPING(lc); lc++) {
100 tst_count = 0;
101
102 for (i = 0; i < TST_TOTAL; i++)
103 mknod_verify(&test_cases[i]);
104 }
105
106 cleanup();
107 tst_exit();
108 }
109
setup(void)110 static void setup(void)
111 {
112 int i;
113 struct passwd *ltpuser;
114 const char *fs_type;
115
116 tst_require_root();
117
118 tst_sig(NOFORK, DEF_HANDLER, cleanup);
119
120 tst_tmpdir();
121
122 fs_type = tst_dev_fs_type();
123 device = tst_acquire_device(cleanup);
124
125 if (!device)
126 tst_brkm(TCONF, cleanup, "Failed to acquire device");
127
128 tst_mkfs(cleanup, device, fs_type, NULL, NULL);
129
130 TEST_PAUSE;
131
132 /* mount a read-only file system for EROFS test */
133 SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
134 SAFE_MOUNT(cleanup, device, MNT_POINT, fs_type, MS_RDONLY, NULL);
135 mount_flag = 1;
136
137 ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
138 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
139
140 SAFE_MKDIR(cleanup, DIR_TEMP, DIR_TEMP_MODE);
141
142 /*
143 * NOTE: the ELOOP test is written based on that the consecutive
144 * symlinks limits in kernel is hardwired to 40.
145 */
146 SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE);
147 SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
148 for (i = 0; i < 43; i++)
149 strcat(elooppathname, ELOPFILE);
150 }
151
mknod_verify(const struct test_case_t * test_case)152 static void mknod_verify(const struct test_case_t *test_case)
153 {
154 TEST(mknod(test_case->pathname, test_case->mode,
155 makedev(test_case->major, test_case->minor)));
156
157 if (TEST_RETURN != -1) {
158 tst_resm(TFAIL, "mknod succeeded unexpectedly");
159 return;
160 }
161
162 if (TEST_ERRNO == test_case->exp_errno) {
163 tst_resm(TPASS | TTERRNO, "mknod failed as expected");
164 } else {
165 tst_resm(TFAIL | TTERRNO,
166 "mknod failed unexpectedly; expected: "
167 "%d - %s", test_case->exp_errno,
168 strerror(test_case->exp_errno));
169 }
170 }
171
cleanup(void)172 static void cleanup(void)
173 {
174 if (seteuid(0) == -1)
175 tst_resm(TWARN | TERRNO, "seteuid(0) failed");
176
177 if (mount_flag && tst_umount(MNT_POINT) < 0)
178 tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
179
180 if (device)
181 tst_release_device(device);
182
183 tst_rmdir();
184 }
185