plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 21 | * Test to check the error and trivial conditions in setuid |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | */ |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 23 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 24 | #include <errno.h> |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 25 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | #include <sys/types.h> |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 27 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 28 | #include <unistd.h> |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 29 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | #include "test.h" |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 31 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | |
| 33 | #define ROOT_USER 0 |
| 34 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 35 | char *TCID = "setuid03"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 38 | static char nobody_uid[] = "nobody"; |
| 39 | static struct passwd *ltpuser; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 41 | static void setup(void); |
| 42 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 44 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 46 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 47 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 49 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 50 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | setup(); |
| 53 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 55 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 57 | TEST(SETUID(cleanup, ROOT_USER)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
| 59 | if (TEST_RETURN != -1) { |
| 60 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 61 | continue; |
| 62 | } |
| 63 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | if (TEST_ERRNO == EPERM) { |
| 65 | tst_resm(TPASS, "setuid returned errno EPERM"); |
| 66 | } else { |
| 67 | tst_resm(TFAIL, "setuid returned unexpected errno - %d", |
| 68 | TEST_ERRNO); |
| 69 | } |
| 70 | } |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 71 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 73 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 76 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | { |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 78 | tst_require_root(NULL); |
| 79 | |
robbiew | 5901ff4 | 2001-08-28 15:11:41 +0000 | [diff] [blame] | 80 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | ltpuser = getpwnam(nobody_uid); |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 82 | if (ltpuser == NULL) |
| 83 | tst_brkm(TBROK, cleanup, "getpwnam failed for user id %s", |
| 84 | nobody_uid); |
| 85 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | if (setuid(ltpuser->pw_uid) == -1) { |
| 87 | tst_resm(TINFO, "setuid failed to " |
| 88 | "to set the effective uid to %d", ltpuser->pw_uid); |
| 89 | perror("setuid"); |
| 90 | } |
robbiew | 5901ff4 | 2001-08-28 15:11:41 +0000 | [diff] [blame] | 91 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 93 | |
| 94 | umask(0); |
| 95 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | TEST_PAUSE; |
| 97 | } |
| 98 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 99 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 101 | } |