blob: 9fad79cdb569d26e5dd0dbb85659158abe092f9d [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: fchmod05
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
plars865695b2001-08-27 22:15:12 +000023 * Test Description:
24 * Verify that, fchmod(2) will succeed to change the mode of a directory
25 * but fails to set the setgid bit on it if invoked by non-root (uid != 0)
26 * process with the following constraints,
27 * - the process is the owner of the directory.
28 * - the effective group ID or one of the supplementary group ID's of the
29 * process is not equal to the group ID of the directory.
30 *
31 * Expected Result:
32 * fchmod() should return value 0 on success and though succeeds to change
33 * the mode of a directory but fails to set setgid bit on it.
34 *
35 * Algorithm:
36 * Setup:
37 * Setup signal handling.
38 * Create temporary directory.
39 * Pause for SIGUSR1 if option specified.
40 *
41 * Test:
42 * Loop if the proper options are given.
43 * Execute system call
44 * Check return code, if system call failed (return=-1)
subrata_modak945234b2009-04-25 17:52:44 +000045 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000046 * Otherwise,
subrata_modak945234b2009-04-25 17:52:44 +000047 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000048 * if successful,
subrata_modak945234b2009-04-25 17:52:44 +000049 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000050 * Otherwise,
51 * Issue Functionality-Fail message.
52 * Cleanup:
53 * Print errno log and/or timing stats if options given
54 * Delete the temporary directory created.
55 *
56 * Usage: <for command-line>
57 * fchmod05 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
58 * where, -c n : Run n copies concurrently.
59 * -f : Turn off functionality Testing.
60 * -i n : Execute test n times.
61 * -I x : Execute test for x seconds.
62 * -P x : Pause for x seconds between iterations.
63 * -t : Turn on syscall timing.
64 *
65 * HISTORY
66 * 07/2001 Ported by Wayne Boyer
67 *
68 * RESTRICTIONS:
69 * This test should be run by 'non-super-user' only.
70 *
71 */
72
vapier5b31d1b2006-08-21 06:46:26 +000073#ifndef _GNU_SOURCE
Wanlong Gao354ebb42012-12-07 10:10:04 +080074#define _GNU_SOURCE
vapier5b31d1b2006-08-21 06:46:26 +000075#endif
76
plars865695b2001-08-27 22:15:12 +000077#include <stdio.h>
78#include <stdlib.h>
79#include <sys/types.h>
80#include <sys/stat.h>
81#include <sys/fcntl.h>
82#include <errno.h>
83#include <string.h>
84#include <signal.h>
robbiew15226cd2002-04-10 16:10:40 +000085#include <unistd.h>
plars865695b2001-08-27 22:15:12 +000086#include <grp.h>
87#include <pwd.h>
88
89#include "test.h"
plars865695b2001-08-27 22:15:12 +000090
subrata_modak945234b2009-04-25 17:52:44 +000091#define MODE_RWX (S_IRWXU | S_IRWXG | S_IRWXO)
robbiewcd501262002-06-10 17:42:53 +000092#define PERMS 043777
plars865695b2001-08-27 22:15:12 +000093#define TESTDIR "testdir"
94
95int fd; /* file descriptor for test directory */
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020096char *TCID = "fchmod05";
97int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000098
99void setup(); /* Main setup function for test */
100void cleanup(); /* Main cleanup function for test */
101
subrata_modak56207ce2009-03-23 13:35:39 +0000102int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000103{
104 struct stat stat_buf; /* stat struct */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200105 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200106 const char *msg;
plars865695b2001-08-27 22:15:12 +0000107 mode_t dir_mode; /* mode permissions set on test directory */
subrata_modak56207ce2009-03-23 13:35:39 +0000108
Garrett Cooper45e285d2010-11-22 12:19:25 -0800109 msg = parse_opts(ac, av, NULL, NULL);
110 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000111 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800112
plars865695b2001-08-27 22:15:12 +0000113 }
114
plars865695b2001-08-27 22:15:12 +0000115 setup();
116
plars865695b2001-08-27 22:15:12 +0000117 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800118
Caspar Zhangd59a6592013-03-07 14:59:12 +0800119 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000120
subrata_modak4bb656a2009-02-26 12:02:09 +0000121 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000122 * Call fchmod(2) with mode argument
plars865695b2001-08-27 22:15:12 +0000123 * to set setgid bit on TESTDIR.
subrata_modak56207ce2009-03-23 13:35:39 +0000124 */
robbiew48988622001-09-10 18:33:12 +0000125
plars865695b2001-08-27 22:15:12 +0000126 TEST(fchmod(fd, PERMS));
subrata_modakbdbaec52009-02-26 12:14:51 +0000127
plars865695b2001-08-27 22:15:12 +0000128 if (TEST_RETURN == -1) {
129 tst_resm(TFAIL, "fchmod(%d, %#o) Failed, errno=%d : %s",
130 fd, PERMS, TEST_ERRNO, strerror(TEST_ERRNO));
131 continue;
132 }
133 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200134 * Get the directory information using
135 * fstat(2).
plars865695b2001-08-27 22:15:12 +0000136 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200137 if (fstat(fd, &stat_buf) < 0) {
138 tst_brkm(TFAIL, cleanup,
139 "fstat(2) of %s failed, errno:%d",
140 TESTDIR, TEST_ERRNO);
141 }
142 dir_mode = stat_buf.st_mode;
143 if ((PERMS & ~S_ISGID) != dir_mode) {
144 tst_resm(TFAIL, "%s: Incorrect modes 0%03o, "
145 "Expected 0%03o",
146 TESTDIR, dir_mode, PERMS & ~S_ISGID);
plars865695b2001-08-27 22:15:12 +0000147 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200148 tst_resm(TPASS, "Functionality of fchmod(%d, "
149 "%#o) successful", fd,
150 PERMS & ~S_ISGID);
plars865695b2001-08-27 22:15:12 +0000151 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800152 }
plars865695b2001-08-27 22:15:12 +0000153
plars865695b2001-08-27 22:15:12 +0000154 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800155 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800156}
plars865695b2001-08-27 22:15:12 +0000157
158/*
159 * void
160 * setup() - performs all ONE TIME setup for this test.
161 * Create a temporary directory and cd to it.
162 * Create a test directory under temporary directory.
subrata_modak4bb656a2009-02-26 12:02:09 +0000163 * Invoke setuid to root program to modify group ownership
plars865695b2001-08-27 22:15:12 +0000164 * on test directory.
165 * Open the test directory for reading.
166 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400167void setup(void)
plars865695b2001-08-27 22:15:12 +0000168{
subrata_modak945234b2009-04-25 17:52:44 +0000169 struct passwd *nobody_u;
170 struct group *bin_group;
plars865695b2001-08-27 22:15:12 +0000171
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200172 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000173
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200174 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000175
plars865695b2001-08-27 22:15:12 +0000176 TEST_PAUSE;
177
plars865695b2001-08-27 22:15:12 +0000178 tst_tmpdir();
179
subrata_modak945234b2009-04-25 17:52:44 +0000180 nobody_u = getpwnam("nobody");
181 if (!nobody_u)
182 tst_brkm(TBROK, cleanup,
183 "Couldn't find uid of nobody: %s", strerror(errno));
184
185 bin_group = getgrnam("bin");
186 if (!bin_group)
187 tst_brkm(TBROK, cleanup,
188 "Couldn't find gid of bin: %s", strerror(errno));
189
plars865695b2001-08-27 22:15:12 +0000190 /*
191 * Create a test directory under temporary directory with specified
192 * mode permissions and change the gid of test directory to that of
193 * guest user.
194 */
195 if (mkdir(TESTDIR, MODE_RWX) < 0) {
196 tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR);
197 }
198
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800199 if (setgroups(1, &nobody_u->pw_gid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800200 tst_brkm(TBROK, cleanup,
201 "Couldn't change supplementary group Id: %s",
202 strerror(errno));
subrata_modak624a96e2009-09-16 14:43:43 +0000203
204 if (chown(TESTDIR, nobody_u->pw_uid, bin_group->gr_gid) == -1)
subrata_modak945234b2009-04-25 17:52:44 +0000205 tst_brkm(TBROK, cleanup, "Couldn't change owner of testdir: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800206 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000207
subrata_modak624a96e2009-09-16 14:43:43 +0000208 /* change to nobody:nobody */
209 if (setegid(nobody_u->pw_gid) == -1 || seteuid(nobody_u->pw_uid) == -1)
subrata_modak945234b2009-04-25 17:52:44 +0000210 tst_brkm(TBROK, cleanup, "Couldn't switch to nobody:nobody: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800211 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000212
213 /* Open the test directory for reading */
subrata_modak945234b2009-04-25 17:52:44 +0000214 fd = open(TESTDIR, O_RDONLY);
215 if (fd == -1) {
plars865695b2001-08-27 22:15:12 +0000216 tst_brkm(TBROK, cleanup,
217 "open(%s, O_RDONLY) failed, errno=%d : %s",
218 TESTDIR, errno, strerror(errno));
219 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800220}
plars865695b2001-08-27 22:15:12 +0000221
222/*
223 * void
224 * cleanup() - performs all ONE TIME cleanup for this test at
225 * completion or premature exit.
226 * Close the test directory opened in the setup().
227 * Remove the test directory and temporary directory created in
228 * in the setup().
229 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400230void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000231{
plars865695b2001-08-27 22:15:12 +0000232
233 /* Close the test directory opened in the setup() */
234 if (close(fd) == -1) {
235 tst_brkm(TBROK, NULL,
236 "close(%s) Failed, errno=%d : %s",
237 TESTDIR, errno, strerror(errno));
238 }
239
subrata_modak0e9fc312009-06-15 18:57:19 +0000240 setegid(0);
241 seteuid(0);
242
plars865695b2001-08-27 22:15:12 +0000243 tst_rmdir();
244
Chris Dearmanec6edca2012-10-17 19:54:01 -0700245}