Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * Copyright (c) Kerlabs 2008. * |
| 3 | * Copyright (c) International Business Machines Corp., 2008 * |
| 4 | * Created by Renaud Lottiaux * |
| 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 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 21 | /* |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 22 | * Check if setuid behaves correctly with file permissions. The test creates a |
| 23 | * file as ROOT with permissions 0644, does a setuid and then tries to open the |
| 24 | * file with RDWR permissions. The same test is done in a fork to check if new |
| 25 | * UIDs are correctly passed to the son. |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 26 | */ |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 27 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 28 | #include <errno.h> |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 29 | #include <pwd.h> |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/wait.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <unistd.h> |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 35 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 36 | #include "test.h" |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 37 | #include "compat_16.h" |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 38 | |
| 39 | char *TCID = "setuid04"; |
| 40 | int TST_TOTAL = 1; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 41 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 42 | static char nobody_uid[] = "nobody"; |
Cyril Hrubis | 3875344 | 2013-09-12 14:58:41 +0200 | [diff] [blame] | 43 | static char testfile[] = "setuid04_testfile"; |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 44 | static struct passwd *ltpuser; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 45 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 46 | static int fd = -1; |
| 47 | |
| 48 | static void setup(void); |
| 49 | static void cleanup(void); |
| 50 | static void do_master_child(void); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 51 | |
| 52 | int main(int ac, char **av) |
| 53 | { |
| 54 | pid_t pid; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 55 | const char *msg; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 56 | int status; |
| 57 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 58 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 59 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 60 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 61 | setup(); |
| 62 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 63 | pid = FORK_OR_VFORK(); |
| 64 | if (pid < 0) |
| 65 | tst_brkm(TBROK, cleanup, "Fork failed"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 66 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 67 | if (pid == 0) { |
| 68 | do_master_child(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | } else { |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 70 | waitpid(pid, &status, 0); |
| 71 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 72 | tst_resm(WEXITSTATUS(status), |
| 73 | "son process exits with error"); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 77 | tst_exit(); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 80 | static void do_master_child(void) |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 81 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 82 | int lc; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 83 | int pid; |
| 84 | int status; |
| 85 | |
MaShimiao | 7f5bfb8 | 2013-09-26 17:42:02 +0800 | [diff] [blame] | 86 | if (SETUID(NULL, ltpuser->pw_uid) == -1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 87 | tst_brkm(TBROK, NULL, |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 88 | "setuid failed to set the effective uid to %d", |
| 89 | ltpuser->pw_uid); |
| 90 | } |
| 91 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 92 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 93 | int tst_fd; |
| 94 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 95 | tst_count = 0; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 96 | |
| 97 | TEST(tst_fd = open(testfile, O_RDWR)); |
| 98 | |
| 99 | if (TEST_RETURN != -1) { |
| 100 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 101 | close(tst_fd); |
| 102 | } |
| 103 | |
| 104 | if (TEST_ERRNO == EACCES) { |
| 105 | tst_resm(TPASS, "open returned errno EACCES"); |
| 106 | } else { |
| 107 | tst_resm(TFAIL, "open returned unexpected errno - %d", |
| 108 | TEST_ERRNO); |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | pid = FORK_OR_VFORK(); |
| 113 | if (pid < 0) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 114 | tst_brkm(TBROK, NULL, "Fork failed"); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 115 | |
| 116 | if (pid == 0) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 117 | int tst_fd2; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 118 | |
| 119 | /* Test to open the file in son process */ |
| 120 | TEST(tst_fd2 = open(testfile, O_RDWR)); |
| 121 | |
| 122 | if (TEST_RETURN != -1) { |
| 123 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 124 | close(tst_fd2); |
| 125 | } |
| 126 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 127 | if (TEST_ERRNO == EACCES) { |
| 128 | tst_resm(TPASS, "open returned errno EACCES"); |
| 129 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | tst_resm(TFAIL, |
| 131 | "open returned unexpected errno - %d", |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 132 | TEST_ERRNO); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 133 | } |
subrata_modak | c891597 | 2009-06-23 14:07:59 +0000 | [diff] [blame] | 134 | tst_exit(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | } else { |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 136 | /* Wait for son completion */ |
| 137 | waitpid(pid, &status, 0); |
| 138 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) |
| 139 | exit(WEXITSTATUS(status)); |
| 140 | } |
| 141 | } |
subrata_modak | 80e4194 | 2009-06-25 08:16:54 +0000 | [diff] [blame] | 142 | tst_exit(); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 145 | static void setup(void) |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 146 | { |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 147 | tst_require_root(NULL); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 148 | |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 149 | ltpuser = getpwnam(nobody_uid); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 150 | |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 151 | if (ltpuser == NULL) |
| 152 | tst_brkm(TBROK, cleanup, "getpwnam failed for user id %s", |
| 153 | nobody_uid); |
| 154 | |
| 155 | UID16_CHECK(ltpuser->pw_uid, setuid, cleanup); |
| 156 | |
Cyril Hrubis | 3875344 | 2013-09-12 14:58:41 +0200 | [diff] [blame] | 157 | tst_tmpdir(); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 158 | |
| 159 | /* Create test file */ |
| 160 | fd = open(testfile, O_CREAT | O_RDWR, 0644); |
| 161 | if (fd < 0) |
| 162 | tst_brkm(TBROK, cleanup, "cannot creat test file"); |
| 163 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 164 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 165 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 166 | TEST_PAUSE; |
| 167 | } |
| 168 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 169 | static void cleanup(void) |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 170 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | close(fd); |
Cyril Hrubis | 3875344 | 2013-09-12 14:58:41 +0200 | [diff] [blame] | 172 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 173 | } |