plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +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 | 369bb1c | 2014-06-04 16:00:01 +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 | 369bb1c | 2014-06-04 16:00:01 +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 Foundation, |
| 17 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
vapier | 37cf54a | 2007-04-13 20:58:17 +0000 | [diff] [blame] | 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * Test Description: |
| 22 | * Verify that, fchown(2) succeeds to change the group of a file specified |
| 23 | * by path when called by non-root user with the following constraints, |
vapier | 37cf54a | 2007-04-13 20:58:17 +0000 | [diff] [blame] | 24 | * - euid of the process is equal to the owner of the file. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 25 | * - the intended gid is either egid, or one of the supplementary gids |
| 26 | * of the process. |
| 27 | * Also, verify that fchown() clears the setuid/setgid bits set on the file. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/fcntl.h> |
| 35 | #include <errno.h> |
| 36 | #include <string.h> |
| 37 | #include <signal.h> |
| 38 | #include <grp.h> |
| 39 | #include <pwd.h> |
| 40 | |
| 41 | #include "test.h" |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 42 | #include "safe_macros.h" |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 43 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | |
subrata_modak | 945234b | 2009-04-25 17:52:44 +0000 | [diff] [blame] | 45 | #define FILE_MODE (mode_t)(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) |
| 46 | #define NEW_PERMS (mode_t)(S_IFREG | S_IRWXU | S_IRWXG | S_ISUID | S_ISGID) |
| 47 | #define FCHOWN_PERMS (mode_t)(NEW_PERMS & ~(S_ISUID | S_ISGID)) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | #define TESTFILE "testfile" |
| 49 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 50 | TCID_DEFINE(fchown03); |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 51 | int TST_TOTAL = 1; |
robbiew | d3faba0 | 2001-08-31 17:21:08 +0000 | [diff] [blame] | 52 | |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 53 | static int fildes; |
| 54 | char nobody_uid[] = "nobody"; |
| 55 | static struct passwd *ltpuser; |
| 56 | |
| 57 | static void setup(void); |
| 58 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
vapier | 37cf54a | 2007-04-13 20:58:17 +0000 | [diff] [blame] | 60 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | { |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 62 | struct stat stat_buf; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 63 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 64 | const char *msg; |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 65 | uid_t user_id; |
| 66 | gid_t group_id; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 68 | msg = parse_opts(ac, av, NULL, NULL); |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 69 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 71 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | setup(); |
| 73 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 75 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 77 | user_id = geteuid(); |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 78 | GID16_CHECK((group_id = getegid()), "fchown", cleanup) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 80 | TEST(FCHOWN(cleanup, fildes, -1, group_id)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | if (TEST_RETURN == -1) { |
| 83 | tst_resm(TFAIL, "fchown() on %s Fails, errno=%d", |
| 84 | TESTFILE, TEST_ERRNO); |
| 85 | continue; |
| 86 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 87 | |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 88 | SAFE_FSTAT(cleanup, fildes, &stat_buf); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 90 | if ((stat_buf.st_uid != user_id) || |
| 91 | (stat_buf.st_gid != group_id)) { |
| 92 | tst_resm(TFAIL, "%s: Incorrect " |
| 93 | "ownership set, Expected %d %d", |
| 94 | TESTFILE, user_id, group_id); |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 95 | continue; |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 96 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 98 | if (stat_buf.st_mode != FCHOWN_PERMS) { |
| 99 | tst_resm(TFAIL, "%s: Incorrect mode permissions" |
| 100 | " %#o, Expected %#o", TESTFILE, |
| 101 | stat_buf.st_mode, FCHOWN_PERMS); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 103 | tst_resm(TPASS, "fchown() on %s succeeds: " |
| 104 | "Setuid/gid bits cleared", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 106 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | cleanup(); |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 109 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 110 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 112 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 115 | |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 116 | tst_require_root(NULL); |
| 117 | |
| 118 | ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid); |
| 119 | SAFE_SETEUID(NULL, ltpuser->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | TEST_PAUSE; |
| 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | tst_tmpdir(); |
| 124 | |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 125 | fildes = SAFE_OPEN(cleanup, TESTFILE, O_RDWR | O_CREAT, FILE_MODE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 127 | SAFE_SETEUID(cleanup, 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 129 | SAFE_FCHOWN(cleanup, fildes, -1, 0); |
| 130 | SAFE_FCHMOD(cleanup, fildes, NEW_PERMS); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 132 | SAFE_SETEGID(cleanup, ltpuser->pw_gid); |
| 133 | SAFE_SETEUID(cleanup, ltpuser->pw_uid); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 134 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 136 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | { |
Cyril Hrubis | 369bb1c | 2014-06-04 16:00:01 +0200 | [diff] [blame] | 138 | if (fildes > 0 && close(fildes)) |
| 139 | tst_resm(TWARN | TERRNO, "close(%s) Failed", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 142 | } |