robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 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 |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 13 | * with this program; if not, write the Free Software Foundation, Inc., |
| 14 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 15 | * |
Cyril Hrubis | 629a1ac | 2013-08-08 17:24:04 +0200 | [diff] [blame] | 16 | * DESCRIPTION |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 17 | * Verify that mount(2) returns -1 and sets errno to EPERM if the user |
| 18 | * is not the super-user. |
Cyril Hrubis | 629a1ac | 2013-08-08 17:24:04 +0200 | [diff] [blame] | 19 | */ |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 20 | |
| 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 Hrubis | 1c9f529 | 2014-06-30 14:00:31 +0200 | [diff] [blame] | 28 | #include "safe_macros.h" |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 29 | |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 30 | static void setup(void); |
| 31 | static void cleanup(void); |
| 32 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 33 | char *TCID = "mount04"; |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 34 | |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 35 | #define DIR_MODE S_IRWXU | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
| 36 | |
Cyril Hrubis | 629a1ac | 2013-08-08 17:24:04 +0200 | [diff] [blame] | 37 | static char *mntpoint = "mntpoint"; |
Cyril Hrubis | 1c9f529 | 2014-06-30 14:00:31 +0200 | [diff] [blame] | 38 | static const char *fs_type; |
| 39 | static const char *device; |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 40 | |
Cyril Hrubis | 1c9f529 | 2014-06-30 14:00:31 +0200 | [diff] [blame] | 41 | int TST_TOTAL = 1; |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 42 | |
Cyril Hrubis | 1c9f529 | 2014-06-30 14:00:31 +0200 | [diff] [blame] | 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"); |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 59 | if (tst_umount(mntpoint)) |
Cyril Hrubis | 1c9f529 | 2014-06-30 14:00:31 +0200 | [diff] [blame] | 60 | tst_brkm(TBROK, cleanup, "umount() failed"); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | tst_resm(TFAIL | TTERRNO, "mount() returned %li", TEST_RETURN); |
| 65 | } |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 66 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 67 | int main(int ac, char **av) |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 68 | { |
Cyril Hrubis | 1c9f529 | 2014-06-30 14:00:31 +0200 | [diff] [blame] | 69 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 70 | const char *msg; |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 71 | |
Cyril Hrubis | 1c9f529 | 2014-06-30 14:00:31 +0200 | [diff] [blame] | 72 | if ((msg = parse_opts(ac, av, NULL, NULL))) |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 73 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 74 | |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 75 | setup(); |
| 76 | |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 77 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 78 | tst_count = 0; |
Cyril Hrubis | 1c9f529 | 2014-06-30 14:00:31 +0200 | [diff] [blame] | 79 | verify_mount(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 80 | } |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 81 | |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 82 | cleanup(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 83 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 84 | } |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 85 | |
Cyril Hrubis | 629a1ac | 2013-08-08 17:24:04 +0200 | [diff] [blame] | 86 | static void setup(void) |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 87 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | char nobody_uid[] = "nobody"; |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 89 | struct passwd *ltpuser; |
| 90 | |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 91 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 92 | |
Cyril Hrubis | 629a1ac | 2013-08-08 17:24:04 +0200 | [diff] [blame] | 93 | tst_require_root(NULL); |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 94 | |
Cyril Hrubis | 629a1ac | 2013-08-08 17:24:04 +0200 | [diff] [blame] | 95 | tst_tmpdir(); |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 96 | |
Cyril Hrubis | 1c9f529 | 2014-06-30 14:00:31 +0200 | [diff] [blame] | 97 | 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); |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 108 | |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 109 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 110 | } |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 111 | |
Cyril Hrubis | 629a1ac | 2013-08-08 17:24:04 +0200 | [diff] [blame] | 112 | static void cleanup(void) |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 113 | { |
Cyril Hrubis | 1c9f529 | 2014-06-30 14:00:31 +0200 | [diff] [blame] | 114 | if (seteuid(0)) |
| 115 | tst_resm(TWARN | TERRNO, "seteuid(0) failed"); |
| 116 | |
| 117 | if (device) |
| 118 | tst_release_device(NULL, device); |
| 119 | |
robbiew | 1d89ad5 | 2006-01-31 15:12:47 +0000 | [diff] [blame] | 120 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 121 | } |