blob: 6498ad19d8abd117b5ee5f8152c9df40a60c59ac [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis369bb1c2014-06-04 16:00:01 +02002 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubis369bb1c2014-06-04 16:00:01 +02005 * 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.
plars865695b2001-08-27 22:15:12 +00009 *
Cyril Hrubis369bb1c2014-06-04 16:00:01 +020010 * 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.
plars865695b2001-08-27 22:15:12 +000014 *
Cyril Hrubis369bb1c2014-06-04 16:00:01 +020015 * 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
plars865695b2001-08-27 22:15:12 +000018 */
19
vapier37cf54a2007-04-13 20:58:17 +000020/*
plars865695b2001-08-27 22:15:12 +000021 * 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,
vapier37cf54a2007-04-13 20:58:17 +000024 * - euid of the process is equal to the owner of the file.
plars865695b2001-08-27 22:15:12 +000025 * - 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.
plars865695b2001-08-27 22:15:12 +000028 */
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 Hrubis369bb1c2014-06-04 16:00:01 +020042#include "safe_macros.h"
Han Pingtian75fb0572014-11-28 16:33:41 +080043#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000044
subrata_modak945234b2009-04-25 17:52:44 +000045#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))
plars865695b2001-08-27 22:15:12 +000048#define TESTFILE "testfile"
49
Han Pingtian75fb0572014-11-28 16:33:41 +080050TCID_DEFINE(fchown03);
Cyril Hrubis369bb1c2014-06-04 16:00:01 +020051int TST_TOTAL = 1;
robbiewd3faba02001-08-31 17:21:08 +000052
Cyril Hrubis369bb1c2014-06-04 16:00:01 +020053static int fildes;
54char nobody_uid[] = "nobody";
55static struct passwd *ltpuser;
56
57static void setup(void);
58static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000059
vapier37cf54a2007-04-13 20:58:17 +000060int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000061{
Cyril Hrubis369bb1c2014-06-04 16:00:01 +020062 struct stat stat_buf;
Cyril Hrubis89af32a2012-10-24 16:39:11 +020063 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020064 const char *msg;
Cyril Hrubis369bb1c2014-06-04 16:00:01 +020065 uid_t user_id;
66 gid_t group_id;
plars865695b2001-08-27 22:15:12 +000067
Garrett Cooper45e285d2010-11-22 12:19:25 -080068 msg = parse_opts(ac, av, NULL, NULL);
Cyril Hrubis369bb1c2014-06-04 16:00:01 +020069 if (msg != NULL)
plars865695b2001-08-27 22:15:12 +000070 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080071
plars865695b2001-08-27 22:15:12 +000072 setup();
73
plars865695b2001-08-27 22:15:12 +000074 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080075 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000076
Garrett Cooper53740502010-12-16 00:04:01 -080077 user_id = geteuid();
Han Pingtian75fb0572014-11-28 16:33:41 +080078 GID16_CHECK((group_id = getegid()), "fchown", cleanup)
plars865695b2001-08-27 22:15:12 +000079
Han Pingtian75fb0572014-11-28 16:33:41 +080080 TEST(FCHOWN(cleanup, fildes, -1, group_id));
plars865695b2001-08-27 22:15:12 +000081
plars865695b2001-08-27 22:15:12 +000082 if (TEST_RETURN == -1) {
83 tst_resm(TFAIL, "fchown() on %s Fails, errno=%d",
84 TESTFILE, TEST_ERRNO);
85 continue;
86 }
Cyril Hrubise38b9612014-06-02 17:20:57 +020087
Cyril Hrubis369bb1c2014-06-04 16:00:01 +020088 SAFE_FSTAT(cleanup, fildes, &stat_buf);
plars865695b2001-08-27 22:15:12 +000089
Cyril Hrubise38b9612014-06-02 17:20:57 +020090 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 Hrubis369bb1c2014-06-04 16:00:01 +020095 continue;
Cyril Hrubise38b9612014-06-02 17:20:57 +020096 }
plars865695b2001-08-27 22:15:12 +000097
Cyril Hrubise38b9612014-06-02 17:20:57 +020098 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);
plars865695b2001-08-27 22:15:12 +0000102 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200103 tst_resm(TPASS, "fchown() on %s succeeds: "
104 "Setuid/gid bits cleared", TESTFILE);
plars865695b2001-08-27 22:15:12 +0000105 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800106 }
plars865695b2001-08-27 22:15:12 +0000107
plars865695b2001-08-27 22:15:12 +0000108 cleanup();
Cyril Hrubis369bb1c2014-06-04 16:00:01 +0200109 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800110}
plars865695b2001-08-27 22:15:12 +0000111
Cyril Hrubis369bb1c2014-06-04 16:00:01 +0200112static void setup(void)
plars865695b2001-08-27 22:15:12 +0000113{
plars865695b2001-08-27 22:15:12 +0000114 tst_sig(FORK, DEF_HANDLER, cleanup);
115
Cyril Hrubis369bb1c2014-06-04 16:00:01 +0200116 tst_require_root(NULL);
117
118 ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid);
119 SAFE_SETEUID(NULL, ltpuser->pw_uid);
plars865695b2001-08-27 22:15:12 +0000120
plars865695b2001-08-27 22:15:12 +0000121 TEST_PAUSE;
122
plars865695b2001-08-27 22:15:12 +0000123 tst_tmpdir();
124
Cyril Hrubis369bb1c2014-06-04 16:00:01 +0200125 fildes = SAFE_OPEN(cleanup, TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
plars865695b2001-08-27 22:15:12 +0000126
Cyril Hrubis369bb1c2014-06-04 16:00:01 +0200127 SAFE_SETEUID(cleanup, 0);
plars865695b2001-08-27 22:15:12 +0000128
Cyril Hrubis369bb1c2014-06-04 16:00:01 +0200129 SAFE_FCHOWN(cleanup, fildes, -1, 0);
130 SAFE_FCHMOD(cleanup, fildes, NEW_PERMS);
plars865695b2001-08-27 22:15:12 +0000131
Cyril Hrubis369bb1c2014-06-04 16:00:01 +0200132 SAFE_SETEGID(cleanup, ltpuser->pw_gid);
133 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
Garrett Cooper2c282152010-12-16 00:55:50 -0800134}
plars865695b2001-08-27 22:15:12 +0000135
Cyril Hrubis369bb1c2014-06-04 16:00:01 +0200136static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000137{
Cyril Hrubis369bb1c2014-06-04 16:00:01 +0200138 if (fildes > 0 && close(fildes))
139 tst_resm(TWARN | TERRNO, "close(%s) Failed", TESTFILE);
plars865695b2001-08-27 22:15:12 +0000140
plars865695b2001-08-27 22:15:12 +0000141 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700142}