1 /*
2 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 * DESCRIPTION
17 * Verify that mount(2) returns -1 and sets errno to EPERM if the user
18 * is not the super-user.
19 */
20
21 #include <errno.h>
22 #include <sys/mount.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <pwd.h>
27 #include "test.h"
28 #include "safe_macros.h"
29
30 static void setup(void);
31 static void cleanup(void);
32
33 char *TCID = "mount04";
34
35 #define DIR_MODE S_IRWXU | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP
36
37 static char *mntpoint = "mntpoint";
38 static const char *fs_type;
39 static const char *device;
40
41 int TST_TOTAL = 1;
42
verify_mount(void)43 static void verify_mount(void)
44 {
45
46 TEST(mount(device, mntpoint, fs_type, 0, NULL));
47
48 if (TEST_RETURN == -1) {
49 if (TEST_ERRNO == EPERM)
50 tst_resm(TPASS | TTERRNO, "mount() failed expectedly");
51 else
52 tst_resm(TFAIL | TTERRNO,
53 "mount() was expected to fail with EPERM");
54 return;
55 }
56
57 if (TEST_RETURN == 0) {
58 tst_resm(TFAIL, "mount() succeeded unexpectedly");
59 if (tst_umount(mntpoint))
60 tst_brkm(TBROK, cleanup, "umount() failed");
61 return;
62 }
63
64 tst_resm(TFAIL | TTERRNO, "mount() returned %li", TEST_RETURN);
65 }
66
main(int ac,char ** av)67 int main(int ac, char **av)
68 {
69 int lc;
70
71 tst_parse_opts(ac, av, NULL, NULL);
72
73 setup();
74
75 for (lc = 0; TEST_LOOPING(lc); lc++) {
76 tst_count = 0;
77 verify_mount();
78 }
79
80 cleanup();
81 tst_exit();
82 }
83
setup(void)84 static void setup(void)
85 {
86 char nobody_uid[] = "nobody";
87 struct passwd *ltpuser;
88
89 tst_sig(FORK, DEF_HANDLER, cleanup);
90
91 tst_require_root();
92
93 tst_tmpdir();
94
95 fs_type = tst_dev_fs_type();
96 device = tst_acquire_device(cleanup);
97
98 if (!device)
99 tst_brkm(TCONF, cleanup, "Failed to obtain block device");
100
101 tst_mkfs(cleanup, device, fs_type, NULL, NULL);
102
103 ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid);
104 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
105 SAFE_MKDIR(cleanup, mntpoint, DIR_MODE);
106
107 TEST_PAUSE;
108 }
109
cleanup(void)110 static void cleanup(void)
111 {
112 if (seteuid(0))
113 tst_resm(TWARN | TERRNO, "seteuid(0) failed");
114
115 if (device)
116 tst_release_device(device);
117
118 tst_rmdir();
119 }
120