blob: 742d90106c2e674799775785f2b992d1c76ff571 [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: chown03
22 *
23 * Test Description:
24 * Verify that, chown(2) succeeds to change the group of a file specified
25 * by path when called by non-root user with the following constraints,
subrata_modakbdbaec52009-02-26 12:14:51 +000026 * - euid of the process is equal to the owner of the file.
plars865695b2001-08-27 22:15:12 +000027 * - the intended gid is either egid, or one of the supplementary gids
28 * of the process.
29 * Also, verify that chown() clears the setuid/setgid bits set on the file.
30 *
31 * Expected Result:
subrata_modak4bb656a2009-02-26 12:02:09 +000032 * chown(2) should return 0 and the ownership set on the file should match
plars865695b2001-08-27 22:15:12 +000033 * the numeric values contained in owner and group respectively.
subrata_modakbdbaec52009-02-26 12:14:51 +000034 *
plars865695b2001-08-27 22:15:12 +000035 * 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 * chown03 [-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:
plars865695b2001-08-27 22:15:12 +000069 *
70 */
71
72#include <stdio.h>
73#include <stdlib.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"
Han Pingtian75fb0572014-11-28 16:33:41 +080084#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000085
Garrett Cooper4f00dbe2010-12-17 16:05:52 -080086#define FILE_MODE (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
87#define NEW_PERMS (S_IFREG|S_IRWXU|S_IRWXG|S_ISUID|S_ISGID)
plars865695b2001-08-27 22:15:12 +000088#define TESTFILE "testfile"
89
Han Pingtian75fb0572014-11-28 16:33:41 +080090TCID_DEFINE(chown03);
subrata_modak56207ce2009-03-23 13:35:39 +000091int TST_TOTAL = 1; /* Total number of test conditions */
robbiew6eabc142001-08-31 15:23:29 +000092char nobody_uid[] = "nobody";
93struct passwd *ltpuser;
plars865695b2001-08-27 22:15:12 +000094
95void setup(); /* setup function for the test */
96void cleanup(); /* cleanup function for the test */
97
subrata_modak56207ce2009-03-23 13:35:39 +000098int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000099{
100 struct stat stat_buf; /* stat(2) struct contents */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200101 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200102 const char *msg;
Garrett Cooper53740502010-12-16 00:04:01 -0800103 uid_t user_id; /* Owner id of the test file. */
104 gid_t group_id; /* Group id of the test file. */
robbiew6eabc142001-08-31 15:23:29 +0000105
Garrett Cooper4f00dbe2010-12-17 16:05:52 -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
Han Pingtian75fb0572014-11-28 16:33:41 +0800115 UID16_CHECK((user_id = geteuid()), "chown", cleanup)
116 GID16_CHECK((group_id = getegid()), "chown", cleanup)
plars865695b2001-08-27 22:15:12 +0000117
Han Pingtian75fb0572014-11-28 16:33:41 +0800118 TEST(CHOWN(cleanup, TESTFILE, -1, group_id));
plars865695b2001-08-27 22:15:12 +0000119
plars865695b2001-08-27 22:15:12 +0000120 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121 tst_resm(TFAIL | TTERRNO, "chown(%s, ..) failed",
122 TESTFILE);
plars865695b2001-08-27 22:15:12 +0000123 continue;
124 }
125
Cyril Hrubise38b9612014-06-02 17:20:57 +0200126 if (stat(TESTFILE, &stat_buf) == -1)
127 tst_brkm(TFAIL | TERRNO, cleanup,
128 "stat failed");
plars865695b2001-08-27 22:15:12 +0000129
Cyril Hrubise38b9612014-06-02 17:20:57 +0200130 if (stat_buf.st_uid != user_id ||
131 stat_buf.st_gid != group_id)
132 tst_resm(TFAIL, "%s: Incorrect ownership"
133 "set to %d %d, Expected %d %d",
134 TESTFILE, stat_buf.st_uid,
135 stat_buf.st_gid, user_id, group_id);
plars865695b2001-08-27 22:15:12 +0000136
Cyril Hrubise38b9612014-06-02 17:20:57 +0200137 if (stat_buf.st_mode !=
138 (NEW_PERMS & ~(S_ISUID | S_ISGID)))
139 tst_resm(TFAIL, "%s: incorrect mode permissions"
140 " %#o, Expected %#o", TESTFILE,
141 stat_buf.st_mode,
142 NEW_PERMS & ~(S_ISUID | S_ISGID));
143 else
144 tst_resm(TPASS, "chown(%s, ..) was successful",
145 TESTFILE);
Garrett Cooper2c282152010-12-16 00:55:50 -0800146 }
plars865695b2001-08-27 22:15:12 +0000147
plars865695b2001-08-27 22:15:12 +0000148 cleanup();
Garrett Cooper4f00dbe2010-12-17 16:05:52 -0800149 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800150}
plars865695b2001-08-27 22:15:12 +0000151
152/*
153 * void
154 * setup() - performs all ONE TIME setup for this test.
155 * Create a temporary directory and change directory to it.
156 * Create a test file under temporary directory and close it
157 * Change the group ownership on testfile.
158 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400159void setup(void)
plars865695b2001-08-27 22:15:12 +0000160{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800161 int fd; /* file handler for testfile */
plars865695b2001-08-27 22:15:12 +0000162
plars725c2802004-09-21 21:22:34 +0000163 TEST_PAUSE;
164
Garrett Cooper4f00dbe2010-12-17 16:05:52 -0800165 tst_require_root(NULL);
166
Caspar Zhangde923072011-05-11 17:39:54 +0800167 tst_sig(FORK, DEF_HANDLER, cleanup);
168
169 tst_tmpdir();
170
Garrett Cooper4f00dbe2010-12-17 16:05:52 -0800171 ltpuser = getpwnam(nobody_uid);
172 if (ltpuser == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800173 tst_brkm(TBROK | TERRNO, NULL, "getpwnam(\"nobody\") failed");
Garrett Cooper4f00dbe2010-12-17 16:05:52 -0800174 if (setegid(ltpuser->pw_gid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 tst_brkm(TBROK | TERRNO, NULL, "setegid(%d) failed",
176 ltpuser->pw_gid);
Garrett Cooper4f00dbe2010-12-17 16:05:52 -0800177 if (seteuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800178 tst_brkm(TBROK | TERRNO, NULL, "seteuid(%d) failed",
179 ltpuser->pw_uid);
plars725c2802004-09-21 21:22:34 +0000180
plars865695b2001-08-27 22:15:12 +0000181 /* Create a test file under temporary directory */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800182 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1)
183 tst_brkm(TBROK | TERRNO, cleanup,
184 "open(%s, O_RDWR|O_CREAT, %o) failed", TESTFILE,
185 FILE_MODE);
subrata_modak945234b2009-04-25 17:52:44 +0000186
187 if (seteuid(0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188 tst_brkm(TBROK | TERRNO, cleanup, "seteuid(0) failed");
subrata_modak945234b2009-04-25 17:52:44 +0000189
190 if (fchown(fd, -1, 0) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800191 tst_brkm(TBROK | TERRNO, cleanup, "fchown failed");
subrata_modak945234b2009-04-25 17:52:44 +0000192
193 if (fchmod(fd, NEW_PERMS) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800194 tst_brkm(TBROK | TERRNO, cleanup, "fchmod failed");
subrata_modak945234b2009-04-25 17:52:44 +0000195
196 if (seteuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800197 tst_brkm(TBROK | TERRNO, cleanup, "seteuid to nobody failed");
subrata_modak945234b2009-04-25 17:52:44 +0000198
vapier3fdf88a2009-08-28 11:59:17 +0000199 if (close(fd) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800200 tst_brkm(TBROK | TERRNO, cleanup, "closing %s failed",
201 TESTFILE);
Garrett Cooper2c282152010-12-16 00:55:50 -0800202}
plars865695b2001-08-27 22:15:12 +0000203
Mike Frysingerc57fba52014-04-09 18:56:30 -0400204void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000205{
Garrett Cooper4f00dbe2010-12-17 16:05:52 -0800206 if (setegid(0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800207 tst_resm(TWARN | TERRNO, "setegid(0) failed");
Garrett Cooper4f00dbe2010-12-17 16:05:52 -0800208 if (seteuid(0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800209 tst_resm(TWARN | TERRNO, "seteuid(0) failed");
subrata_modaka0a3e412009-06-15 18:58:25 +0000210
plars865695b2001-08-27 22:15:12 +0000211 tst_rmdir();
212
Caspar Zhangde923072011-05-11 17:39:54 +0800213}