blob: 44b8e1749bc9ef5738156adaf23743c548e9dfc2 [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/*
nstrazfa31d552002-05-14 16:50:06 +000021 * Test Name: chmod07
plars865695b2001-08-27 22:15:12 +000022 *
23 * Test Description:
24 * Verify that, chmod(2) will succeed to change the mode of a file/directory
25 * and sets 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 * chmod() 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>
nstrazfa31d552002-05-14 16:50:06 +000057 * chmod07 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000058 * 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 '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"
plars865695b2001-08-27 22:15:12 +000084
85#define LTPUSER "nobody"
robbiew48b0c352001-08-31 16:03:43 +000086#define LTPGRP "users"
Garrett Cooperc5f40232010-12-17 12:26:46 -080087#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
plars865695b2001-08-27 22:15:12 +000088#define PERMS 01777 /*
89 * Mode permissions of test file with sticky
90 * bit set.
91 */
92#define TESTFILE "testfile"
93
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020094char *TCID = "chmod07";
95int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000096
97void setup(); /* Main setup function for the test */
98void cleanup(); /* Main cleanup function for the test */
99
subrata_modak56207ce2009-03-23 13:35:39 +0000100int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000101{
102 struct stat stat_buf; /* stat(2) struct contents */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200103 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200104 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000105
Garrett Cooperc5f40232010-12-17 12:26:46 -0800106 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000107 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800108
plars865695b2001-08-27 22:15:12 +0000109 setup();
110
plars865695b2001-08-27 22:15:12 +0000111 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800112
Caspar Zhangd59a6592013-03-07 14:59:12 +0800113 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000114
subrata_modak4bb656a2009-02-26 12:02:09 +0000115 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000116 * Call chmod(2) with specified mode argument
plars865695b2001-08-27 22:15:12 +0000117 * (sticky-bit set) on testfile.
subrata_modak56207ce2009-03-23 13:35:39 +0000118 */
plars865695b2001-08-27 22:15:12 +0000119 TEST(chmod(TESTFILE, PERMS));
subrata_modakbdbaec52009-02-26 12:14:51 +0000120
plars865695b2001-08-27 22:15:12 +0000121 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 tst_resm(TFAIL | TTERRNO, "chmod(%s, %#o) failed",
vapier45996092009-08-28 11:49:07 +0000123 TESTFILE, PERMS);
plars865695b2001-08-27 22:15:12 +0000124 continue;
125 }
126 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200127 * Get the testfile information using
128 * stat(2).
plars865695b2001-08-27 22:15:12 +0000129 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200130 if (stat(TESTFILE, &stat_buf) == -1)
131 tst_brkm(TFAIL | TTERRNO, cleanup,
132 "stat failed");
plars865695b2001-08-27 22:15:12 +0000133
Cyril Hrubise38b9612014-06-02 17:20:57 +0200134 /* Check for expected mode permissions */
135 if ((stat_buf.st_mode & PERMS) == PERMS)
136 tst_resm(TPASS, "Functionality of "
137 "chmod(%s, %#o) successful",
138 TESTFILE, PERMS);
139 else
140 tst_resm(TFAIL, "%s: Incorrect modes 0%03o; "
141 "expected 0%03o", TESTFILE,
142 stat_buf.st_mode, PERMS);
Garrett Cooper2c282152010-12-16 00:55:50 -0800143 }
plars865695b2001-08-27 22:15:12 +0000144
plars865695b2001-08-27 22:15:12 +0000145 cleanup();
Garrett Cooperc5f40232010-12-17 12:26:46 -0800146 tst_exit();
147}
plars865695b2001-08-27 22:15:12 +0000148
149/*
150 * void
151 * setup() - performs all ONE TIME setup for this test.
152 * Create a temporary directory and change directory to it.
153 * Create a test file under temporary directory and close it
154 * Change the ownership of test file to that of "ltpuser1" user.
155 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400156void setup(void)
plars865695b2001-08-27 22:15:12 +0000157{
subrata_modak56207ce2009-03-23 13:35:39 +0000158 struct passwd *ltpuser; /* password struct for ltpuser1 */
159 struct group *ltpgroup; /* group struct for ltpuser1 */
160 int fd; /* file descriptor variable */
161 gid_t group1_gid; /* user and process group id's */
plars865695b2001-08-27 22:15:12 +0000162 uid_t user1_uid;
163
plars865695b2001-08-27 22:15:12 +0000164 tst_sig(FORK, DEF_HANDLER, cleanup);
165
subrata_modak56207ce2009-03-23 13:35:39 +0000166 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000167
Garrett Cooperc5f40232010-12-17 12:26:46 -0800168 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000169
plars865695b2001-08-27 22:15:12 +0000170 tst_tmpdir();
171
172 /* Get the uid of guest user - ltpuser1 */
Garrett Cooperc5f40232010-12-17 12:26:46 -0800173 if ((ltpuser = getpwnam(LTPUSER)) == NULL)
174 tst_brkm(TBROK, cleanup, "getpwnam failed");
plars865695b2001-08-27 22:15:12 +0000175 user1_uid = ltpuser->pw_uid;
176
177 /* Get the group id of guest user - ltpuser1 */
Garrett Cooperc5f40232010-12-17 12:26:46 -0800178 if ((ltpgroup = getgrnam(LTPGRP)) == NULL)
179 tst_brkm(TBROK, cleanup, "getgrnam failed");
plars865695b2001-08-27 22:15:12 +0000180 group1_gid = ltpgroup->gr_gid;
181
Wanlong Gao354ebb42012-12-07 10:10:04 +0800182 fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
vapier45996092009-08-28 11:49:07 +0000183 if (fd == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800184 tst_brkm(TBROK | TERRNO, cleanup,
vapier45996092009-08-28 11:49:07 +0000185 "open(%s, O_RDWR|O_CREAT, %#o) failed",
186 TESTFILE, FILE_MODE);
187 if (close(fd) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188 tst_brkm(TBROK, cleanup, "close(%s) failed", TESTFILE);
Garrett Cooperc5f40232010-12-17 12:26:46 -0800189 if (chown(TESTFILE, user1_uid, group1_gid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190 tst_brkm(TBROK | TERRNO, cleanup, "chown(%s) failed", TESTFILE);
subrata_modakbdbaec52009-02-26 12:14:51 +0000191
Garrett Cooperc5f40232010-12-17 12:26:46 -0800192 if (setgid(group1_gid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800193 tst_brkm(TBROK | TERRNO, cleanup, "setgid(%d) failed",
194 group1_gid);
Garrett Cooper2c282152010-12-16 00:55:50 -0800195}
plars865695b2001-08-27 22:15:12 +0000196
Mike Frysingerc57fba52014-04-09 18:56:30 -0400197void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000198{
plars865695b2001-08-27 22:15:12 +0000199 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700200}