blob: 9ce56bb7052a293bbaa72a49254c9d7e247f4dc3 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
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.
9 *
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.
14 *
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
subrata_modak4bb656a2009-02-26 12:02:09 +000020/*
plars865695b2001-08-27 22:15:12 +000021 * Test Name: chmod05
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
plars865695b2001-08-27 22:15:12 +000023 * Test Description:
robbiewec91fc42002-06-10 17:32:41 +000024//wjh
25//This test seems to be invalid see comments below
26//The man page for chmod doesn't seem to indicate that the setgid bit can
27//only be set by root
plars865695b2001-08-27 22:15:12 +000028 * Verify that, chmod(2) will succeed to change the mode of a directory
29 * but fails to set the setgid bit on it if invoked by non-root (uid != 0)
30 * process with the following constraints,
31 * - the process is the owner of the directory.
32 * - the effective group ID or one of the supplementary group ID's of the
33 * process is not equal to the group ID of the directory.
34 *
35 * Expected Result:
36 * chmod() should return value 0 on success and though succeeds to change
37 * the mode of a directory but fails to set setgid bit on it.
38 *
39 * Algorithm:
40 * Setup:
41 * Setup signal handling.
42 * Create temporary directory.
43 * Pause for SIGUSR1 if option specified.
44 *
45 * Test:
46 * Loop if the proper options are given.
47 * Execute system call
48 * Check return code, if system call failed (return=-1)
subrata_modak945234b2009-04-25 17:52:44 +000049 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000050 * Otherwise,
subrata_modak945234b2009-04-25 17:52:44 +000051 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000052 * if successful,
subrata_modak945234b2009-04-25 17:52:44 +000053 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000054 * Otherwise,
55 * Issue Functionality-Fail message.
56 * Cleanup:
57 * Print errno log and/or timing stats if options given
58 * Delete the temporary directory created.
59 *
60 * Usage: <for command-line>
61 * chmod05 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t]
62 * where, -c n : Run n copies concurrently.
63 * -e : Turn on errno logging.
64 * -f : Turn off functionality Testing.
65 * -i n : Execute test n times.
66 * -I x : Execute test for x seconds.
67 * -P x : Pause for x seconds between iterations.
68 * -t : Turn on syscall timing.
69 *
70 * HISTORY
71 * 07/2001 Ported by Wayne Boyer
robbiewec91fc42002-06-10 17:32:41 +000072 * 05/2002 Fixes by wjhuie and Ferhan Shareef
73 * changed conditional to check for valid tests only and
74 * in a more logically understandable way
75 * 07/2002 Additional fixes to #defines to allow comparisions to work.
76 * -Robbie Williamson
plars865695b2001-08-27 22:15:12 +000077 *
78 * RESTRICTIONS:
Zeng Linggang52d294a2014-02-18 23:22:51 +080079 * This test should be run by root.
plars865695b2001-08-27 22:15:12 +000080 *
81 */
82
vapier5b31d1b2006-08-21 06:46:26 +000083#ifndef _GNU_SOURCE
Wanlong Gao354ebb42012-12-07 10:10:04 +080084#define _GNU_SOURCE
vapier5b31d1b2006-08-21 06:46:26 +000085#endif
86
plars865695b2001-08-27 22:15:12 +000087#include <stdio.h>
88#include <stdlib.h>
89#include <sys/types.h>
90#include <sys/stat.h>
91#include <sys/fcntl.h>
92#include <errno.h>
93#include <string.h>
94#include <signal.h>
robbiew15226cd2002-04-10 16:10:40 +000095#include <unistd.h>
plars865695b2001-08-27 22:15:12 +000096#include <grp.h>
97#include <pwd.h>
98
99#include "test.h"
plars865695b2001-08-27 22:15:12 +0000100
vapier7b7103c2006-02-11 04:34:36 +0000101#define DEBUG 0
102
subrata_modak945234b2009-04-25 17:52:44 +0000103#define MODE_RWX (mode_t)(S_IRWXU | S_IRWXG | S_IRWXO)
robbiewec91fc42002-06-10 17:32:41 +0000104#define DIR_MODE (mode_t)(S_ISVTX | S_ISGID | S_IFDIR)
105#define PERMS (mode_t)(MODE_RWX | DIR_MODE)
plars865695b2001-08-27 22:15:12 +0000106#define TESTDIR "testdir"
107
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200108char *TCID = "chmod05";
109int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +0000110
111void setup(); /* Main setup function for test */
112void cleanup(); /* Main cleanup function for test */
113
robbiewec91fc42002-06-10 17:32:41 +0000114int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000115{
subrata_modak56207ce2009-03-23 13:35:39 +0000116 struct stat stat_buf; /* stat struct */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200117 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200118 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000119 mode_t dir_mode; /* mode permissions set on test directory */
plars865695b2001-08-27 22:15:12 +0000120
Garrett Cooperc5f40232010-12-17 12:26:46 -0800121 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +0000122 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800123
subrata_modak56207ce2009-03-23 13:35:39 +0000124 setup();
plars865695b2001-08-27 22:15:12 +0000125
subrata_modak56207ce2009-03-23 13:35:39 +0000126 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800127
Caspar Zhangd59a6592013-03-07 14:59:12 +0800128 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000129
subrata_modak56207ce2009-03-23 13:35:39 +0000130 TEST(chmod(TESTDIR, PERMS));
131
subrata_modak56207ce2009-03-23 13:35:39 +0000132 if (TEST_RETURN == -1) {
vapier45996092009-08-28 11:49:07 +0000133 tst_resm(TFAIL, "chmod(%s, %#o) failed",
134 TESTDIR, PERMS);
subrata_modak56207ce2009-03-23 13:35:39 +0000135 continue;
136 }
subrata_modak56207ce2009-03-23 13:35:39 +0000137 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200138 * Get the directory information using
139 * stat(2).
subrata_modak56207ce2009-03-23 13:35:39 +0000140 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200141 if (stat(TESTDIR, &stat_buf) < 0) {
142 tst_brkm(TFAIL | TERRNO, cleanup,
143 "stat(%s) failed", TESTDIR);
144 }
145 dir_mode = stat_buf.st_mode;
vapier7b7103c2006-02-11 04:34:36 +0000146#if DEBUG
Cyril Hrubise38b9612014-06-02 17:20:57 +0200147 printf("DIR_MODE = 0%03o\n", DIR_MODE);
148 printf("MODE_RWX = 0%03o\n", MODE_RWX);
149 printf("PERMS = 0%03o\n", PERMS);
150 printf("dir_mode = 0%03o\n", dir_mode);
vapier7b7103c2006-02-11 04:34:36 +0000151#endif
Cyril Hrubise38b9612014-06-02 17:20:57 +0200152 if ((PERMS & ~S_ISGID) != dir_mode)
153 tst_resm(TFAIL, "%s: Incorrect modes 0%03o, "
154 "Expected 0%03o", TESTDIR, dir_mode,
155 PERMS & ~S_ISGID);
156 else
157 tst_resm(TPASS,
158 "Functionality of chmod(%s, %#o) successful",
subrata_modak56207ce2009-03-23 13:35:39 +0000159 TESTDIR, PERMS);
Garrett Cooper2c282152010-12-16 00:55:50 -0800160 }
plars865695b2001-08-27 22:15:12 +0000161
subrata_modak56207ce2009-03-23 13:35:39 +0000162 cleanup();
Garrett Cooperc5f40232010-12-17 12:26:46 -0800163 tst_exit();
Garrett Cooperc5f40232010-12-17 12:26:46 -0800164}
plars865695b2001-08-27 22:15:12 +0000165
166/*
167 * void
168 * setup() - performs all ONE TIME setup for this test.
169 * Create a temporary directory and cd to it.
170 * Create a test directory under temporary directory.
robbiewec91fc42002-06-10 17:32:41 +0000171//wjh we are root so do we really need this kluged helper program?
subrata_modak4bb656a2009-02-26 12:02:09 +0000172 * Invoke setuid to root program to modify group ownership
plars865695b2001-08-27 22:15:12 +0000173 * on test directory.
174 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400175void setup(void)
plars865695b2001-08-27 22:15:12 +0000176{
subrata_modak945234b2009-04-25 17:52:44 +0000177 struct passwd *nobody_u;
178 struct group *bin_group;
subrata_modak56207ce2009-03-23 13:35:39 +0000179
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200180 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000181
subrata_modak56207ce2009-03-23 13:35:39 +0000182 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000183
subrata_modak56207ce2009-03-23 13:35:39 +0000184 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000185
subrata_modak945234b2009-04-25 17:52:44 +0000186 nobody_u = getpwnam("nobody");
Garrett Cooperc5f40232010-12-17 12:26:46 -0800187 if (nobody_u == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188 tst_brkm(TBROK | TERRNO, cleanup,
189 "getpwnam(\"nobody\") failed");
subrata_modak945234b2009-04-25 17:52:44 +0000190
191 bin_group = getgrnam("bin");
Garrett Cooperc5f40232010-12-17 12:26:46 -0800192 if (bin_group == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800193 tst_brkm(TBROK | TERRNO, cleanup, "getgrnam(\"bin\") failed");
subrata_modak945234b2009-04-25 17:52:44 +0000194
subrata_modak56207ce2009-03-23 13:35:39 +0000195 /*
196 * Create a test directory under temporary directory with specified
Garrett Cooperc5f40232010-12-17 12:26:46 -0800197 * mode permissions and change the gid of test directory to nobody's
198 * gid.
subrata_modak56207ce2009-03-23 13:35:39 +0000199 */
vapier45996092009-08-28 11:49:07 +0000200 if (mkdir(TESTDIR, MODE_RWX) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800201 tst_brkm(TBROK | TERRNO, cleanup, "mkdir(%s) failed", TESTDIR);
subrata_modak4bb656a2009-02-26 12:02:09 +0000202
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800203 if (setgroups(1, &nobody_u->pw_gid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800204 tst_brkm(TBROK | TERRNO, cleanup,
205 "setgroups to nobody's gid failed");
subrata_modak624a96e2009-09-16 14:43:43 +0000206
207 if (chown(TESTDIR, nobody_u->pw_uid, bin_group->gr_gid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800208 tst_brkm(TBROK | TERRNO, cleanup,
209 "chowning testdir to nobody:bin failed");
plars865695b2001-08-27 22:15:12 +0000210
subrata_modak624a96e2009-09-16 14:43:43 +0000211 /* change to nobody:nobody */
Garrett Cooperc5f40232010-12-17 12:26:46 -0800212 if (setegid(nobody_u->pw_gid) == -1 || seteuid(nobody_u->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800213 tst_brkm(TBROK | TERRNO, cleanup,
214 "Couldn't switch to nobody:nobody");
Garrett Cooper2c282152010-12-16 00:55:50 -0800215}
plars865695b2001-08-27 22:15:12 +0000216
Mike Frysingerc57fba52014-04-09 18:56:30 -0400217void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000218{
Garrett Cooperc5f40232010-12-17 12:26:46 -0800219 if (setegid(0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800220 tst_resm(TWARN | TERRNO, "setegid(0) failed");
Garrett Cooperc5f40232010-12-17 12:26:46 -0800221 if (seteuid(0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222 tst_resm(TWARN | TERRNO, "seteuid(0) failed");
subrata_modak96a65202009-06-09 16:54:50 +0000223
subrata_modak56207ce2009-03-23 13:35:39 +0000224 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700225}