1 /*
2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write the Free Software Foundation, Inc.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  */
17 /**********************************************************
18  *
19  *    TEST IDENTIFIER	: ioperm02
20  *
21  *    EXECUTED BY	: superuser
22  *
23  *    TEST TITLE	: Tests for error conditions
24  *
25  *    TEST CASE TOTAL	: 2
26  *
27  *    AUTHOR		: Subhab Biwas <subhabrata.biswas@wipro.com>
28  *
29  *    SIGNALS
30  * 	Uses SIGUSR1 to pause before test if option set.
31  * 	(See the parse_opts(3) man page).
32  *
33  *    DESCRIPTION
34  *	Verify that
35  *	1) ioperm(2) returns -1 and sets errno to EINVAL for I/O port
36  *	   address greater than 0x3ff.
37  *	2) ioperm(2) returns -1 and sets errno to EPERM if the current
38  *	   user is not the super-user.
39  *
40  * 	Setup:
41  * 	  Setup signal handling.
42  *	  Set expected errnos for logging
43  *	  Pause for SIGUSR1 if option specified.
44  *
45  * 	Test:
46  *	 Loop if the proper options are given.
47  * 	  Execute system call
48  *	  Check return code and error number, if matching,
49  *		     Issue PASS message
50  *	  Otherwise,
51  *		     Issue FAIL message
52  *	  Perform testcase specific cleanup (if needed)
53  *
54  * 	Cleanup:
55  * 	  Print errno log and/or timing stats if options given
56  *
57  * USAGE:  <for command-line>
58  * ioperm02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
59  *			where,  -c n : Run n copies concurrently.
60  *				-e   : Turn on errno logging.
61  *				-h   : Show help screen
62  *				-f   : Turn off functional testing
63  *				-i n : Execute test n times.
64  *				-I x : Execute test for x seconds.
65  *				-p   : Pause for SIGUSR1 before starting
66  *				-P x : Pause for x seconds between iterations.
67  *				-t   : Turn on syscall timing.
68  *
69  ****************************************************************/
70 
71 char *TCID = "ioperm02";
72 
73 #if defined __i386__ || defined(__x86_64__)
74 
75 #include <errno.h>
76 #include <unistd.h>
77 #include <sys/io.h>
78 #include <pwd.h>
79 #include "test.h"
80 #include "safe_macros.h"
81 
82 #define NUM_BYTES 3
83 #define TURN_ON 1
84 #define TURN_OFF 0
85 #define EXP_RET_VAL -1
86 #ifndef IO_BITMAP_BITS
87 #define IO_BITMAP_BITS 1024	/* set to default value since some H/W may not support 0x10000 even with a 2.6.8 kernel */
88 #define IO_BITMAP_BITS_16 65536
89 #endif
90 
91 static void setup();
92 static int setup1(void);
93 static void cleanup1();
94 static void cleanup();
95 
96 static char nobody_uid[] = "nobody";
97 struct passwd *ltpuser;
98 
99 struct test_cases_t {
100 	long from;		/* starting port address */
101 	long num;		/* no. of bytes from starting address */
102 	int turn_on;
103 	char *desc;		/* test case description */
104 	int exp_errno;		/* expected error number */
105 };
106 
107 int TST_TOTAL = 2;
108 struct test_cases_t *test_cases;
109 
main(int ac,char ** av)110 int main(int ac, char **av)
111 {
112 	int lc, i;
113 
114 	tst_parse_opts(ac, av, NULL, NULL);
115 
116 	setup();
117 
118 	for (lc = 0; TEST_LOOPING(lc); lc++) {
119 
120 		tst_count = 0;
121 
122 		for (i = 0; i < TST_TOTAL; ++i) {
123 
124 			if (i == 1) {
125 				/* setup Non super-user for second test */
126 				if (setup1()) {
127 					/* setup1() failed, skip this test */
128 					continue;
129 				}
130 			}
131 
132 			/* Test the system call */
133 
134 			TEST(ioperm(test_cases[i].from,
135 				    test_cases[i].num, test_cases[i].turn_on));
136 
137 			if ((TEST_RETURN == EXP_RET_VAL) &&
138 			    (TEST_ERRNO == test_cases[i].exp_errno)) {
139 				tst_resm(TPASS, "Expected failure for %s, "
140 					 "errno: %d", test_cases[i].desc,
141 					 TEST_ERRNO);
142 			} else {
143 				tst_resm(TFAIL, "Unexpected results for %s ; "
144 					 "returned %ld (expected %d), errno %d "
145 					 "(expected errno  %d)",
146 					 test_cases[i].desc,
147 					 TEST_RETURN, EXP_RET_VAL,
148 					 TEST_ERRNO, test_cases[i].exp_errno);
149 			}
150 
151 			if (i == 1) {
152 				/* revert back to super user */
153 				cleanup1();
154 			} else {
155 			}
156 		}
157 
158 	}
159 
160 	/* cleanup and exit */
161 	cleanup();
162 
163 	tst_exit();
164 
165 }
166 
167 /* setup1() - set up non-super user for second test case */
setup1(void)168 int setup1(void)
169 {
170 	/* switch to "nobody" user */
171 	if (seteuid(ltpuser->pw_uid) == -1) {
172 		tst_resm(TWARN, "Failed to set effective"
173 			 "uid to %d", ltpuser->pw_uid);
174 		return 1;
175 	}
176 	return 0;
177 }
178 
179 /* cleanup1() - reset to super user for second test case */
cleanup1(void)180 void cleanup1(void)
181 {
182 	/* reset user as root */
183 	SAFE_SETEUID(NULL, 0);
184 }
185 
186 /* setup() - performs all ONE TIME setup for this test */
setup(void)187 void setup(void)
188 {
189 	tst_require_root();
190 
191 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
192 
193 	/* Check if "nobody" user id exists */
194 	if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
195 		tst_brkm(TBROK, NULL, "\"nobody\" user id doesn't exist");
196 	}
197 
198 	/*
199 	 * The value of IO_BITMAP_BITS (include/asm-i386/processor.h) changed
200 	 * from kernel 2.6.8 to permit 16-bits (65536) ioperm
201 	 *
202 	 * Ricky Ng-Adam, rngadam@yahoo.com
203 	 * */
204 	test_cases = malloc(sizeof(struct test_cases_t) * 2);
205 	test_cases[0].num = NUM_BYTES;
206 	test_cases[0].turn_on = TURN_ON;
207 	test_cases[0].desc = "Invalid I/O address";
208 	test_cases[0].exp_errno = EINVAL;
209 	test_cases[1].num = NUM_BYTES;
210 	test_cases[1].turn_on = TURN_ON;
211 	test_cases[1].desc = "Non super-user";
212 	test_cases[1].exp_errno = EPERM;
213 	if ((tst_kvercmp(2, 6, 8) < 0) || (tst_kvercmp(2, 6, 9) == 0)) {
214 		/*try invalid ioperm on 1022, 1023, 1024 */
215 		test_cases[0].from = (IO_BITMAP_BITS - NUM_BYTES) + 1;
216 
217 		/*try get valid ioperm on 1021, 1022, 1023 */
218 		test_cases[1].from = IO_BITMAP_BITS - NUM_BYTES;
219 	} else {
220 		/*try invalid ioperm on 65534, 65535, 65536 */
221 		test_cases[0].from = (IO_BITMAP_BITS_16 - NUM_BYTES) + 1;
222 
223 		/*try valid ioperm on 65533, 65534, 65535 */
224 		test_cases[1].from = IO_BITMAP_BITS_16 - NUM_BYTES;
225 	}
226 
227 	TEST_PAUSE;
228 
229 }
230 
cleanup(void)231 void cleanup(void)
232 {
233 
234 }
235 
236 #else /* __i386__ */
237 
238 #include "test.h"
239 #include "safe_macros.h"
240 
241 int TST_TOTAL = 0;
242 
main(void)243 int main(void)
244 {
245 	tst_resm(TPASS,
246 		 "LSB v1.3 does not specify ioperm() for this architecture.");
247 	tst_exit();
248 }
249 
250 #endif /* __i386__ */
251