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