xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ioperm/ioperm02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  *  Copyright (c) Linux Test Project, 2020
4*49cdfc7eSAndroid Build Coastguard Worker  *  Copyright (c) Wipro Technologies Ltd, 2002
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * This is an error test for ioperm(2) system call.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Verify that
11*49cdfc7eSAndroid Build Coastguard Worker  * 1) ioperm(2) returns -1 and sets errno to EINVAL for I/O port
12*49cdfc7eSAndroid Build Coastguard Worker  *    address greater than 0x3ff.
13*49cdfc7eSAndroid Build Coastguard Worker  * 2) ioperm(2) returns -1 and sets errno to EPERM if the current
14*49cdfc7eSAndroid Build Coastguard Worker  *    user is not the super-user.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * Author: Subhab Biswas <[email protected]>
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #if defined __i386__ || defined(__x86_64__)
27*49cdfc7eSAndroid Build Coastguard Worker #include <sys/io.h>
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker #define NUM_BYTES 3
30*49cdfc7eSAndroid Build Coastguard Worker #ifndef IO_BITMAP_BITS
31*49cdfc7eSAndroid Build Coastguard Worker #define IO_BITMAP_BITS 1024	/* set to default value since some H/W may not support 0x10000 even with a 2.6.8 kernel */
32*49cdfc7eSAndroid Build Coastguard Worker #define IO_BITMAP_BITS_16 65536
33*49cdfc7eSAndroid Build Coastguard Worker #endif
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker static struct tcase_t {
36*49cdfc7eSAndroid Build Coastguard Worker 	long from;
37*49cdfc7eSAndroid Build Coastguard Worker 	long num;
38*49cdfc7eSAndroid Build Coastguard Worker 	int turn_on;
39*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
40*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
41*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
42*49cdfc7eSAndroid Build Coastguard Worker 	{0, NUM_BYTES, 1, "Invalid I/O address", EINVAL},
43*49cdfc7eSAndroid Build Coastguard Worker 	{0, NUM_BYTES, 1, "Non super-user", EPERM},
44*49cdfc7eSAndroid Build Coastguard Worker };
45*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)46*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	tcases[0].from = (IO_BITMAP_BITS_16 - NUM_BYTES) + 1;
49*49cdfc7eSAndroid Build Coastguard Worker 	tcases[1].from = IO_BITMAP_BITS_16 - NUM_BYTES;
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *pw;
52*49cdfc7eSAndroid Build Coastguard Worker 	pw = SAFE_GETPWNAM("nobody");
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(pw->pw_uid);
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)56*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(0);
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker 
verify_ioperm(unsigned int i)61*49cdfc7eSAndroid Build Coastguard Worker static void verify_ioperm(unsigned int i)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker 	TEST(ioperm(tcases[i].from, tcases[i].num, tcases[i].turn_on));
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	if ((TST_RET == -1) && (TST_ERR == tcases[i].exp_errno)) {
66*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "Expected failure for %s, "
67*49cdfc7eSAndroid Build Coastguard Worker 				"errno: %d", tcases[i].desc, TST_ERR);
68*49cdfc7eSAndroid Build Coastguard Worker 	} else {
69*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "Unexpected results for %s ; "
70*49cdfc7eSAndroid Build Coastguard Worker 				"returned %ld (expected -1), errno %d "
71*49cdfc7eSAndroid Build Coastguard Worker 				"(expected errno %d)", tcases[i].desc,
72*49cdfc7eSAndroid Build Coastguard Worker 				TST_RET, TST_ERR, tcases[i].exp_errno);
73*49cdfc7eSAndroid Build Coastguard Worker 	}
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
77*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
78*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_ioperm,
79*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
80*49cdfc7eSAndroid Build Coastguard Worker 	/* ioperm() is restricted under kernel lockdown. */
81*49cdfc7eSAndroid Build Coastguard Worker 	.skip_in_lockdown = 1,
82*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
83*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
84*49cdfc7eSAndroid Build Coastguard Worker };
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker #else
87*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("LSB v1.3 does not specify ioperm() for this architecture. (only for i386 or x86_64)");
88*49cdfc7eSAndroid Build Coastguard Worker #endif /* __i386_, __x86_64__*/
89