1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /*
20 * Test Name: chmod03
21 *
22 * Test Description:
23 * Verify that, chmod(2) will succeed to change the mode of a file
24 * and set the sticky bit on it if invoked by non-root (uid != 0)
25 * process with the following constraints,
26 * - the process is the owner of the file.
27 * - the effective group ID or one of the supplementary group ID's of the
28 * process is equal to the group ID of the file.
29 *
30 * Expected Result:
31 * chmod() should return value 0 on success and succeeds to change
32 * the mode of specified file with sticky bit set on it.
33 *
34 * Algorithm:
35 * Setup:
36 * Setup signal handling.
37 * Create temporary directory.
38 * Pause for SIGUSR1 if option specified.
39 *
40 * Test:
41 * Loop if the proper options are given.
42 * Execute system call
43 * Check return code, if system call failed (return=-1)
44 * Log the errno and Issue a FAIL message.
45 * Otherwise,
46 * Verify the Functionality of system call
47 * if successful,
48 * Issue Functionality-Pass message.
49 * Otherwise,
50 * Issue Functionality-Fail message.
51 * Cleanup:
52 * Print errno log and/or timing stats if options given
53 * Delete the temporary directory created.
54 *
55 * Usage: <for command-line>
56 * chmod03 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
57 * where, -c n : Run n copies concurrently.
58 * -e : Turn on errno logging.
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
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 <pwd.h>
81
82 #include "test.h"
83
84 #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
85 #define PERMS 01777 /*
86 * Mode permissions of test file with sticky
87 * bit set.
88 */
89 #define TESTFILE "testfile"
90
91 char *TCID = "chmod03";
92 int TST_TOTAL = 1;
93 char nobody_uid[] = "nobody";
94 struct passwd *ltpuser;
95
96 void setup(); /* Main setup function for the test */
97 void cleanup(); /* Main cleanup function for the test */
98
main(int ac,char ** av)99 int main(int ac, char **av)
100 {
101 struct stat stat_buf;
102 int lc;
103 mode_t file_mode;
104
105 tst_parse_opts(ac, av, NULL, NULL);
106
107 setup();
108
109 for (lc = 0; TEST_LOOPING(lc); lc++) {
110
111 tst_count = 0;
112
113 TEST(chmod(TESTFILE, PERMS));
114
115 if (TEST_RETURN == -1) {
116 tst_resm(TFAIL | TTERRNO, "chmod(%s, %#o) failed",
117 TESTFILE, PERMS);
118 continue;
119 }
120 if (stat(TESTFILE, &stat_buf) < 0) {
121 tst_brkm(TFAIL | TERRNO, cleanup,
122 "stat(%s) failed", TESTFILE);
123 }
124 file_mode = stat_buf.st_mode;
125
126 /* Verify STICKY BIT set on testfile */
127 if ((file_mode & PERMS) != PERMS) {
128 tst_resm(TFAIL, "%s: Incorrect modes 0%3o, "
129 "Expected 0777", TESTFILE, file_mode);
130 } else {
131 tst_resm(TPASS, "Functionality of "
132 "chmod(%s, %#o) successful",
133 TESTFILE, PERMS);
134 }
135 }
136
137 cleanup();
138 tst_exit();
139 }
140
setup(void)141 void setup(void)
142 {
143 int fd;
144
145 tst_sig(NOFORK, DEF_HANDLER, cleanup);
146
147 tst_require_root();
148 ltpuser = getpwnam(nobody_uid);
149 if (ltpuser == NULL)
150 tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
151 if (setuid(ltpuser->pw_uid) == -1)
152 tst_brkm(TBROK | TERRNO, NULL, "setuid(%u) failed",
153 ltpuser->pw_uid);
154
155 TEST_PAUSE;
156
157 tst_tmpdir();
158
159 /*
160 * Create a test file under temporary directory with specified
161 * mode permissios and set the ownership of the test file to the
162 * uid/gid of guest user.
163 */
164 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
165 tst_brkm(TBROK | TERRNO, cleanup,
166 "open(%s, O_RDWR|O_CREAT, %#o) failed",
167 TESTFILE, FILE_MODE);
168 }
169
170 if (close(fd) == -1) {
171 tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", TESTFILE);
172 }
173 }
174
175 /*
176 * void
177 * cleanup() - performs all ONE TIME cleanup for this test at
178 * completion or premature exit.
179 * Delete the testfile and temporary directory created in setup().
180 */
cleanup(void)181 void cleanup(void)
182 {
183
184 tst_rmdir();
185
186 }
187