Zeng Linggang | b8e13bc | 2014-04-25 10:51:05 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Fujitsu Ltd. |
| 3 | * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2 of the GNU General Public License as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | /* |
| 18 | * DESCRIPTION |
| 19 | * The calling process is not privileged and euid is not appropriate, |
| 20 | * EPERM should return. |
| 21 | */ |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <pwd.h> |
| 25 | #include "test.h" |
Zeng Linggang | b8e13bc | 2014-04-25 10:51:05 +0800 | [diff] [blame] | 26 | #include "safe_macros.h" |
| 27 | |
| 28 | char *TCID = "setegid02"; |
| 29 | int TST_TOTAL = 1; |
Zeng Linggang | b8e13bc | 2014-04-25 10:51:05 +0800 | [diff] [blame] | 30 | static void setup(void); |
| 31 | static void setegid_verify(void); |
| 32 | static void cleanup(void); |
| 33 | |
| 34 | static struct passwd *ltpuser; |
| 35 | |
| 36 | int main(int argc, char *argv[]) |
| 37 | { |
| 38 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 39 | const char *msg; |
Zeng Linggang | b8e13bc | 2014-04-25 10:51:05 +0800 | [diff] [blame] | 40 | |
| 41 | msg = parse_opts(argc, argv, NULL, NULL); |
| 42 | if (msg != NULL) |
| 43 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 44 | |
| 45 | setup(); |
| 46 | |
| 47 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 48 | tst_count = 0; |
| 49 | setegid_verify(); |
| 50 | } |
| 51 | |
| 52 | cleanup(); |
| 53 | tst_exit(); |
| 54 | } |
| 55 | |
| 56 | static void setup(void) |
| 57 | { |
| 58 | tst_require_root(NULL); |
| 59 | |
| 60 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 61 | |
| 62 | TEST_PAUSE; |
| 63 | |
| 64 | ltpuser = SAFE_GETPWNAM(cleanup, "nobody"); |
| 65 | |
| 66 | SAFE_SETEUID(cleanup, ltpuser->pw_uid); |
Zeng Linggang | b8e13bc | 2014-04-25 10:51:05 +0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static void setegid_verify(void) |
| 70 | { |
| 71 | TEST(setegid(ltpuser->pw_gid)); |
| 72 | |
| 73 | if (TEST_RETURN != -1) { |
| 74 | tst_resm(TFAIL, "setegid(%d) succeeded unexpectedly", |
| 75 | ltpuser->pw_gid); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | if (TEST_ERRNO == EPERM) { |
| 80 | tst_resm(TPASS | TTERRNO, "setegid failed as expected"); |
| 81 | } else { |
| 82 | tst_resm(TFAIL | TTERRNO, |
| 83 | "setegid failed unexpectedly; expected: %d - %s", |
| 84 | EPERM, strerror(EPERM)); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static void cleanup(void) |
| 89 | { |
Zeng Linggang | b8e13bc | 2014-04-25 10:51:05 +0800 | [diff] [blame] | 90 | } |