blob: 3ba7af3bdb1d4d2026fca958ba6264ef395fe251 [file] [log] [blame]
subrata_modakcbfac502008-08-19 07:23:42 +00001/******************************************************************************/
2/* Copyright (c) Kerlabs 2008. */
3/* Copyright (c) International Business Machines Corp., 2008 */
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 */
subrata_modakcbfac502008-08-19 07:23:42 +000018/* */
19/******************************************************************************/
20/*
21 * NAME
22 * setresuid04.c
23 *
24 * DESCRIPTION
25 * Check if setresuid behaves correctly with file permissions.
26 * The test creates a file as ROOT with permissions 0644, does a setresuid
27 * and then tries to open the file with RDWR permissions.
28 * The same test is done in a fork to check if new UIDs are correctly
29 * passed to the son.
30 *
31 * USAGE: <for command-line>
32 * setresuid04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
33 * where, -c n : Run n copies concurrently.
34 * -e : Turn on errno logging.
35 * -i n : Execute test n times.
36 * -I x : Execute test for x seconds.
37 * -P x : Pause for x seconds between iterations.
38 * -t : Turn on syscall timing.
39 *
40 * HISTORY
41 * 07/2001 Created by Renaud Lottiaux
42 *
43 * RESTRICTIONS
44 * Must be run as root.
45 */
46#define _GNU_SOURCE 1
47#include <errno.h>
48#include <sys/types.h>
49#include <sys/stat.h>
50#include <sys/wait.h>
51#include <fcntl.h>
52#include <unistd.h>
53#include "test.h"
54#include "usctest.h"
55#include <pwd.h>
Han Pingtian75fb0572014-11-28 16:33:41 +080056#include "compat_16.h"
subrata_modakcbfac502008-08-19 07:23:42 +000057
Han Pingtian75fb0572014-11-28 16:33:41 +080058TCID_DEFINE(setresuid04);
subrata_modakcbfac502008-08-19 07:23:42 +000059int TST_TOTAL = 1;
subrata_modakcbfac502008-08-19 07:23:42 +000060char nobody_uid[] = "nobody";
61char testfile[256] = "";
62struct passwd *ltpuser;
63
subrata_modak56207ce2009-03-23 13:35:39 +000064int exp_enos[] = { EACCES, 0 };
Wanlong Gao354ebb42012-12-07 10:10:04 +080065
subrata_modakcbfac502008-08-19 07:23:42 +000066int fd = -1;
67
68void setup(void);
69void cleanup(void);
70void do_master_child();
71
72int main(int ac, char **av)
73{
74 pid_t pid;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020075 const char *msg;
subrata_modakcbfac502008-08-19 07:23:42 +000076 int status;
77
Garrett Cooper1da938b2011-02-23 21:21:15 -080078 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080079 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakcbfac502008-08-19 07:23:42 +000080 setup();
81
82 TEST_EXP_ENOS(exp_enos);
83
84 pid = FORK_OR_VFORK();
85 if (pid < 0)
86 tst_brkm(TBROK, cleanup, "Fork failed");
subrata_modakbdbaec52009-02-26 12:14:51 +000087
Garrett Cooper1da938b2011-02-23 21:21:15 -080088 if (pid == 0)
subrata_modakcbfac502008-08-19 07:23:42 +000089 do_master_child();
Garrett Cooper2c282152010-12-16 00:55:50 -080090
Garrett Cooper1da938b2011-02-23 21:21:15 -080091 if (waitpid(pid, &status, 0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080092 tst_resm(TBROK | TERRNO, "waitpid failed");
Garrett Cooper1da938b2011-02-23 21:21:15 -080093 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
94 tst_resm(TFAIL, "child process terminated abnormally");
subrata_modakcbfac502008-08-19 07:23:42 +000095
96 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080097 tst_exit();
subrata_modakcbfac502008-08-19 07:23:42 +000098}
99
100/*
101 * do_master_child()
102 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400103void do_master_child(void)
subrata_modakcbfac502008-08-19 07:23:42 +0000104{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200105 int lc;
subrata_modakcbfac502008-08-19 07:23:42 +0000106 int pid;
107 int status;
108
subrata_modakcbfac502008-08-19 07:23:42 +0000109 for (lc = 0; TEST_LOOPING(lc); lc++) {
110 int tst_fd;
111
Caspar Zhangd59a6592013-03-07 14:59:12 +0800112 /* Reset tst_count in case we are looping */
113 tst_count = 0;
subrata_modakcbfac502008-08-19 07:23:42 +0000114
Han Pingtian75fb0572014-11-28 16:33:41 +0800115 if (SETRESUID(cleanup, 0, ltpuser->pw_uid, 0) == -1) {
116 perror("setresuid failed");
Garrett Cooper1da938b2011-02-23 21:21:15 -0800117 exit(1);
subrata_modakcbfac502008-08-19 07:23:42 +0000118 }
119
120 /* Test 1: Check the process with new uid cannot open the file
121 * with RDWR permissions.
122 */
123 TEST(tst_fd = open(testfile, O_RDWR));
124
125 if (TEST_RETURN != -1) {
Garrett Cooper1da938b2011-02-23 21:21:15 -0800126 printf("open succeeded unexpectedly\n");
subrata_modakcbfac502008-08-19 07:23:42 +0000127 close(tst_fd);
Garrett Cooper1da938b2011-02-23 21:21:15 -0800128 exit(1);
subrata_modakcbfac502008-08-19 07:23:42 +0000129 }
130
131 if (TEST_ERRNO == EACCES) {
Garrett Cooper1da938b2011-02-23 21:21:15 -0800132 printf("open failed with EACCES as expected\n");
subrata_modakcbfac502008-08-19 07:23:42 +0000133 } else {
Garrett Cooper1da938b2011-02-23 21:21:15 -0800134 perror("open failed unexpectedly");
135 exit(1);
subrata_modakcbfac502008-08-19 07:23:42 +0000136 }
137
138 /* Test 2: Check a son process cannot open the file
139 * with RDWR permissions.
140 */
141 pid = FORK_OR_VFORK();
142 if (pid < 0)
143 tst_brkm(TBROK, cleanup, "Fork failed");
144
145 if (pid == 0) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000146 int tst_fd2;
subrata_modakcbfac502008-08-19 07:23:42 +0000147
148 /* Test to open the file in son process */
149 TEST(tst_fd2 = open(testfile, O_RDWR));
150
151 if (TEST_RETURN != -1) {
Garrett Cooper1da938b2011-02-23 21:21:15 -0800152 printf("call succeeded unexpectedly\n");
subrata_modakcbfac502008-08-19 07:23:42 +0000153 close(tst_fd2);
Garrett Cooper1da938b2011-02-23 21:21:15 -0800154 exit(1);
subrata_modakcbfac502008-08-19 07:23:42 +0000155 }
156
157 TEST_ERROR_LOG(TEST_ERRNO);
subrata_modak56207ce2009-03-23 13:35:39 +0000158
subrata_modakcbfac502008-08-19 07:23:42 +0000159 if (TEST_ERRNO == EACCES) {
Garrett Cooper1da938b2011-02-23 21:21:15 -0800160 printf("open failed with EACCES as expected\n");
161 exit(0);
subrata_modakcbfac502008-08-19 07:23:42 +0000162 } else {
Garrett Cooper1da938b2011-02-23 21:21:15 -0800163 printf("open failed unexpectedly\n");
164 exit(1);
subrata_modakcbfac502008-08-19 07:23:42 +0000165 }
subrata_modak56207ce2009-03-23 13:35:39 +0000166 } else {
subrata_modakcbfac502008-08-19 07:23:42 +0000167 /* Wait for son completion */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 if (waitpid(pid, &status, 0) == -1) {
Garrett Cooper1da938b2011-02-23 21:21:15 -0800169 perror("waitpid failed");
170 exit(1);
171 }
subrata_modakcbfac502008-08-19 07:23:42 +0000172 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
173 exit(WEXITSTATUS(status));
174 }
175
176 /* Test 3: Fallback to initial uid and check we can again open
177 * the file with RDWR permissions.
178 */
Caspar Zhangd59a6592013-03-07 14:59:12 +0800179 tst_count++;
Han Pingtian75fb0572014-11-28 16:33:41 +0800180 if (SETRESUID(cleanup, 0, 0, 0) == -1) {
181 perror("setresuid failed");
Garrett Cooper1da938b2011-02-23 21:21:15 -0800182 exit(1);
subrata_modakcbfac502008-08-19 07:23:42 +0000183 }
184
185 TEST(tst_fd = open(testfile, O_RDWR));
186
187 if (TEST_RETURN == -1) {
Garrett Cooper1da938b2011-02-23 21:21:15 -0800188 perror("open failed unexpectedly");
189 exit(1);
subrata_modak56207ce2009-03-23 13:35:39 +0000190 } else {
Garrett Cooper1da938b2011-02-23 21:21:15 -0800191 printf("open call succeeded\n");
subrata_modak56207ce2009-03-23 13:35:39 +0000192 close(tst_fd);
subrata_modakcbfac502008-08-19 07:23:42 +0000193 }
194 }
Garrett Cooper1da938b2011-02-23 21:21:15 -0800195 exit(0);
subrata_modakcbfac502008-08-19 07:23:42 +0000196}
197
198/*
199 * setup() - performs all ONE TIME setup for this test
200 */
subrata_modak56207ce2009-03-23 13:35:39 +0000201void setup(void)
subrata_modakcbfac502008-08-19 07:23:42 +0000202{
Garrett Cooper1da938b2011-02-23 21:21:15 -0800203 tst_require_root(NULL);
subrata_modakcbfac502008-08-19 07:23:42 +0000204
subrata_modakbdbaec52009-02-26 12:14:51 +0000205 ltpuser = getpwnam(nobody_uid);
subrata_modakcbfac502008-08-19 07:23:42 +0000206
Han Pingtian75fb0572014-11-28 16:33:41 +0800207 UID16_CHECK(ltpuser->pw_uid, "setresuid", cleanup)
208
subrata_modak9e89d9a2008-09-08 08:52:52 +0000209 tst_tmpdir();
210
subrata_modakcbfac502008-08-19 07:23:42 +0000211 sprintf(testfile, "setresuid04file%d.tst", getpid());
212
213 /* Create test file */
214 fd = open(testfile, O_CREAT | O_RDWR, 0644);
215 if (fd < 0)
216 tst_brkm(TBROK, cleanup, "cannot creat test file");
217
subrata_modakcbfac502008-08-19 07:23:42 +0000218 tst_sig(FORK, DEF_HANDLER, cleanup);
219
subrata_modakcbfac502008-08-19 07:23:42 +0000220 TEST_PAUSE;
221}
222
223/*
224 * cleanup() - performs all the ONE TIME cleanup for this test at completion
225 * or premature exit
226 */
subrata_modak56207ce2009-03-23 13:35:39 +0000227void cleanup(void)
subrata_modakcbfac502008-08-19 07:23:42 +0000228{
subrata_modak56207ce2009-03-23 13:35:39 +0000229 close(fd);
subrata_modakcbfac502008-08-19 07:23:42 +0000230
231 /*
232 * print timing status if that option was specified
233 * print errno log if that option was specified
234 */
235 TEST_CLEANUP;
236
subrata_modak9e89d9a2008-09-08 08:52:52 +0000237 tst_rmdir();
238
Garrett Cooper1da938b2011-02-23 21:21:15 -0800239}