blob: 5558d918810642d0b0dbabfd92bb628684fcb151 [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: fchmod02
22 *
23 * Test Description:
24 * Verify that, fchmod(2) will succeed to change the mode of a file/directory
25 * set the sticky bit on it if invoked by root (uid = 0) process with
26 * the following constraints,
27 * - the process is not the owner of the file/directory.
28 * - the effective group ID or one of the supplementary group ID's of the
29 * process is equal to the group ID of the file/directory.
subrata_modakbdbaec52009-02-26 12:14:51 +000030 *
plars865695b2001-08-27 22:15:12 +000031 * Expected Result:
32 * fchmod() should return value 0 on success and succeeds to set sticky bit
33 * on the specified file.
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)
45 * Log the errno and Issue a FAIL message.
46 * Otherwise,
subrata_modakbdbaec52009-02-26 12:14:51 +000047 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000048 * if successful,
49 * Issue Functionality-Pass message.
50 * 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 * fchmod02 [-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.
Garrett Cooper2c282152010-12-16 00:55:50 -080064 *
plars865695b2001-08-27 22:15:12 +000065 * HISTORY
66 * 07/2001 Ported by Wayne Boyer
67 *
68 * RESTRICTIONS:
69 * This test should be run by 'super-user' (root) only.
70 *
71 */
72
73#include <stdio.h>
74#include <sys/types.h>
75#include <sys/stat.h>
76#include <sys/fcntl.h>
77#include <errno.h>
78#include <string.h>
79#include <signal.h>
80#include <grp.h>
81#include <pwd.h>
82
83#include "test.h"
84#include "usctest.h"
85
86#define LTPUSER "nobody"
robbiew2dbfc402001-08-31 16:22:34 +000087#define LTPGRP "users"
plars865695b2001-08-27 22:15:12 +000088#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
89#define PERMS 01777 /*
90 * Mode permissions of test file with sticky
91 * bit set.
92 */
93#define TESTFILE "testfile"
94
95int fd; /* file descriptor variable */
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020096char *TCID = "fchmod02";
97int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000098
99void setup(); /* Main setup function for the test */
100void cleanup(); /* Main cleanup function for the 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(2) struct contents */
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
Garrett Cooper45e285d2010-11-22 12:19:25 -0800108 msg = parse_opts(ac, av, NULL, NULL);
109 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000110 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800111
plars865695b2001-08-27 22:15:12 +0000112 }
113
plars865695b2001-08-27 22:15:12 +0000114 setup();
115
plars865695b2001-08-27 22:15:12 +0000116 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800117
Caspar Zhangd59a6592013-03-07 14:59:12 +0800118 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000119
subrata_modak4bb656a2009-02-26 12:02:09 +0000120 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000121 * Call fchmod(2) with specified mode argument
plars865695b2001-08-27 22:15:12 +0000122 * (sticky-bit set) on testfile.
subrata_modak56207ce2009-03-23 13:35:39 +0000123 */
plars865695b2001-08-27 22:15:12 +0000124 TEST(fchmod(fd, PERMS));
subrata_modakbdbaec52009-02-26 12:14:51 +0000125
plars865695b2001-08-27 22:15:12 +0000126 if (TEST_RETURN == -1) {
127 tst_resm(TFAIL, "fchmod(%d, %#o) Failed, errno=%d : %s",
128 fd, PERMS, TEST_ERRNO, strerror(TEST_ERRNO));
129 continue;
130 }
131 /*
132 * Perform functional verification if test
133 * executed without (-f) option.
134 */
135 if (STD_FUNCTIONAL_TEST) {
136 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000137 * Get the testfile information using
plars865695b2001-08-27 22:15:12 +0000138 * fstat(2).
139 */
140 if (fstat(fd, &stat_buf) < 0) {
141 tst_brkm(TFAIL, cleanup, "fstat(2) of %s "
142 "failed, errno:%d", TESTFILE,
143 TEST_ERRNO);
144 }
145
146 /* Check for expected mode permissions */
147 if ((stat_buf.st_mode & PERMS) == PERMS) {
148 tst_resm(TPASS, "Functionality of fchmod(%d, "
149 "%#o) Successful", fd, PERMS);
150 } else {
151 tst_resm(TFAIL, "%s: Incorrect modes 0%03o, "
152 "Expected 0%03o", TESTFILE,
153 stat_buf.st_mode, PERMS);
154 }
155 } else {
156 tst_resm(TPASS, "call succeeded");
157 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800158 }
plars865695b2001-08-27 22:15:12 +0000159
plars865695b2001-08-27 22:15:12 +0000160 cleanup();
161
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800162 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800163}
plars865695b2001-08-27 22:15:12 +0000164
165/*
166 * void
167 * setup() - performs all ONE TIME setup for this test.
168 * Create a temporary directory and change directory to it.
169 * Create a test file under temporary directory.
170 * Change the ownership of test file to that of "ltpuser1" user.
171 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400172void setup(void)
plars865695b2001-08-27 22:15:12 +0000173{
subrata_modak56207ce2009-03-23 13:35:39 +0000174 struct passwd *ltpuser; /* password struct for ltpuser1 */
175 struct group *ltpgroup; /* group struct for ltpuser1 */
176 gid_t group1_gid; /* user and process group id's */
plars865695b2001-08-27 22:15:12 +0000177 uid_t user1_uid;
178
plars865695b2001-08-27 22:15:12 +0000179 tst_sig(FORK, DEF_HANDLER, cleanup);
180
plars865695b2001-08-27 22:15:12 +0000181 TEST_PAUSE;
182
183 /* Check that the test process id is super/root */
184 if (geteuid() != 0) {
185 tst_brkm(TBROK, NULL, "Must be super/root for this test!");
186 tst_exit();
187 }
188
plars865695b2001-08-27 22:15:12 +0000189 tst_tmpdir();
190
191 /* Get the uid of guest user - ltpuser1 */
192 if ((ltpuser = getpwnam(LTPUSER)) == NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000193 tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER);
plars865695b2001-08-27 22:15:12 +0000194 }
195 user1_uid = ltpuser->pw_uid;
196
197 /* Get the group id of guest user - ltpuser1 */
198 if ((ltpgroup = getgrnam(LTPGRP)) == NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000199 tst_brkm(TBROK, cleanup, "%s not in /etc/group", LTPGRP);
plars865695b2001-08-27 22:15:12 +0000200 }
201 group1_gid = ltpgroup->gr_gid;
202
203 /*
204 * Create a test file under temporary directory with specified
205 * mode permissios and set the ownership of the test file to the
206 * uid/gid of guest user user1.
207 */
subrata_modak56207ce2009-03-23 13:35:39 +0000208 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
plars865695b2001-08-27 22:15:12 +0000209 tst_brkm(TBROK, cleanup,
210 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s",
211 TESTFILE, FILE_MODE, errno, strerror(errno));
subrata_modak4bb656a2009-02-26 12:02:09 +0000212 }
plars865695b2001-08-27 22:15:12 +0000213
214 if (chown(TESTFILE, user1_uid, group1_gid) < 0) {
215 tst_brkm(TBROK, cleanup, "chown(2) of %s failed", TESTFILE);
216 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000217
plars865695b2001-08-27 22:15:12 +0000218 /* Set the effective gid of the process to that of user */
219 if (setgid(group1_gid) < 0) {
220 tst_brkm(TBROK, cleanup, "setgid(2) to %d failed", group1_gid);
221 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800222}
plars865695b2001-08-27 22:15:12 +0000223
224/*
225 * void
226 * cleanup() - performs all ONE TIME cleanup for this test at
227 * completion or premature exit.
228 * Close the testfile created in the setup.
229 * Remove the test directory and testfile created in the setup.
230 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400231void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000232{
233 /*
234 * print timing stats if that option was specified.
235 */
236 TEST_CLEANUP;
237
238 /* Close the testfile created in the setup() */
239 if (close(fd) == -1) {
240 tst_brkm(TBROK, NULL, "close(%s) Failed, errno=%d : %s",
241 TESTFILE, errno, strerror(errno));
242 }
243
plars865695b2001-08-27 22:15:12 +0000244 tst_rmdir();
245
Chris Dearmanec6edca2012-10-17 19:54:01 -0700246}