blob: 7a48d487a578ecd8212b98bf5f95b4c8c356aef7 [file] [log] [blame]
robbiew1d89ad52006-01-31 15:12:47 +00001/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiew1d89ad52006-01-31 15:12:47 +000015 *
Cyril Hrubis629a1ac2013-08-08 17:24:04 +020016 * DESCRIPTION
robbiew1d89ad52006-01-31 15:12:47 +000017 * Verify that mount(2) returns -1 and sets errno to EPERM if the user
18 * is not the super-user.
Cyril Hrubis629a1ac2013-08-08 17:24:04 +020019 */
robbiew1d89ad52006-01-31 15:12:47 +000020
21#include <errno.h>
22#include <sys/mount.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <sys/fcntl.h>
26#include <pwd.h>
27#include "test.h"
Cyril Hrubis1c9f5292014-06-30 14:00:31 +020028#include "safe_macros.h"
robbiew1d89ad52006-01-31 15:12:47 +000029
robbiew1d89ad52006-01-31 15:12:47 +000030static void setup(void);
31static void cleanup(void);
32
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020033char *TCID = "mount04";
robbiew1d89ad52006-01-31 15:12:47 +000034
robbiew1d89ad52006-01-31 15:12:47 +000035#define DIR_MODE S_IRWXU | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP
36
Cyril Hrubis629a1ac2013-08-08 17:24:04 +020037static char *mntpoint = "mntpoint";
Cyril Hrubis1c9f5292014-06-30 14:00:31 +020038static const char *fs_type;
39static const char *device;
robbiew1d89ad52006-01-31 15:12:47 +000040
Cyril Hrubis1c9f5292014-06-30 14:00:31 +020041int TST_TOTAL = 1;
robbiew1d89ad52006-01-31 15:12:47 +000042
Cyril Hrubis1c9f5292014-06-30 14:00:31 +020043static 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");
Cyril Hrubisc60a2302015-01-29 16:28:24 +010059 if (tst_umount(mntpoint))
Cyril Hrubis1c9f5292014-06-30 14:00:31 +020060 tst_brkm(TBROK, cleanup, "umount() failed");
61 return;
62 }
63
64 tst_resm(TFAIL | TTERRNO, "mount() returned %li", TEST_RETURN);
65}
robbiew1d89ad52006-01-31 15:12:47 +000066
subrata_modak56207ce2009-03-23 13:35:39 +000067int main(int ac, char **av)
robbiew1d89ad52006-01-31 15:12:47 +000068{
Cyril Hrubis1c9f5292014-06-30 14:00:31 +020069 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020070 const char *msg;
robbiew1d89ad52006-01-31 15:12:47 +000071
Cyril Hrubis1c9f5292014-06-30 14:00:31 +020072 if ((msg = parse_opts(ac, av, NULL, NULL)))
robbiew1d89ad52006-01-31 15:12:47 +000073 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew1d89ad52006-01-31 15:12:47 +000074
robbiew1d89ad52006-01-31 15:12:47 +000075 setup();
76
robbiew1d89ad52006-01-31 15:12:47 +000077 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080078 tst_count = 0;
Cyril Hrubis1c9f5292014-06-30 14:00:31 +020079 verify_mount();
Garrett Cooper2c282152010-12-16 00:55:50 -080080 }
robbiew1d89ad52006-01-31 15:12:47 +000081
robbiew1d89ad52006-01-31 15:12:47 +000082 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -080083 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -080084}
robbiew1d89ad52006-01-31 15:12:47 +000085
Cyril Hrubis629a1ac2013-08-08 17:24:04 +020086static void setup(void)
robbiew1d89ad52006-01-31 15:12:47 +000087{
subrata_modak56207ce2009-03-23 13:35:39 +000088 char nobody_uid[] = "nobody";
robbiew1d89ad52006-01-31 15:12:47 +000089 struct passwd *ltpuser;
90
robbiew1d89ad52006-01-31 15:12:47 +000091 tst_sig(FORK, DEF_HANDLER, cleanup);
92
Cyril Hrubis629a1ac2013-08-08 17:24:04 +020093 tst_require_root(NULL);
robbiew1d89ad52006-01-31 15:12:47 +000094
Cyril Hrubis629a1ac2013-08-08 17:24:04 +020095 tst_tmpdir();
robbiew1d89ad52006-01-31 15:12:47 +000096
Cyril Hrubis1c9f5292014-06-30 14:00:31 +020097 fs_type = tst_dev_fs_type();
98 device = tst_acquire_device(cleanup);
99
100 if (!device)
101 tst_brkm(TCONF, cleanup, "Failed to obtain block device");
102
103 tst_mkfs(cleanup, device, fs_type, NULL);
104
105 ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid);
106 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
107 SAFE_MKDIR(cleanup, mntpoint, DIR_MODE);
robbiew1d89ad52006-01-31 15:12:47 +0000108
robbiew1d89ad52006-01-31 15:12:47 +0000109 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800110}
robbiew1d89ad52006-01-31 15:12:47 +0000111
Cyril Hrubis629a1ac2013-08-08 17:24:04 +0200112static void cleanup(void)
robbiew1d89ad52006-01-31 15:12:47 +0000113{
Cyril Hrubis1c9f5292014-06-30 14:00:31 +0200114 if (seteuid(0))
115 tst_resm(TWARN | TERRNO, "seteuid(0) failed");
116
117 if (device)
118 tst_release_device(NULL, device);
119
robbiew1d89ad52006-01-31 15:12:47 +0000120 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700121}