• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Ported by John George
19  */
20 
21 /*
22  * Test that EPERM is set when setreuid is given an invalid user id.
23  */
24 
25 #include <sys/wait.h>
26 #include <limits.h>
27 #include <signal.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <pwd.h>
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 
35 #include "test.h"
36 #include "safe_macros.h"
37 #include "compat_16.h"
38 
39 #define INVAL_USER		 (USHRT_MAX-2)
40 
41 TCID_DEFINE(setreuid06);
42 int TST_TOTAL = 1;
43 
44 static struct passwd *ltpuser;
45 
46 static void setup(void);
47 static void cleanup(void);
48 
main(int argc,char ** argv)49 int main(int argc, char **argv)
50 {
51 	int lc;
52 
53 	tst_parse_opts(argc, argv, NULL, NULL);
54 
55 	setup();
56 
57 	for (lc = 0; TEST_LOOPING(lc); lc++) {
58 		tst_count = 0;
59 
60 		TEST(SETREUID(cleanup, -1, INVAL_USER));
61 		if (TEST_RETURN != -1) {
62 			tst_resm(TFAIL, "%s did not fail as expected", TCID);
63 		} else if (TEST_ERRNO == EPERM) {
64 			tst_resm(TPASS, "setreuid set errno to EPERM as "
65 				 "expected");
66 		} else {
67 			tst_resm(TFAIL, "setreuid FAILED, expected 1 but "
68 				 "returned %d", TEST_ERRNO);
69 		}
70 
71 	}
72 	cleanup();
73 	tst_exit();
74 }
75 
setup(void)76 static void setup(void)
77 {
78 	tst_require_root();
79 
80 	tst_sig(FORK, DEF_HANDLER, cleanup);
81 
82 	umask(0);
83 
84 	ltpuser = getpwnam("nobody");
85 	if (ltpuser == NULL)
86 		tst_brkm(TBROK, NULL, "nobody must be a valid user.");
87 
88 	SAFE_SETUID(NULL, ltpuser->pw_uid);
89 
90 	TEST_PAUSE;
91 }
92 
cleanup(void)93 static void cleanup(void)
94 {
95 }
96