xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/iopl/iopl02.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  * Author: Subhab Biswas <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Test for iopl(2) system call error.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * - iopl(2) fail with EINVAL when privilege level greater than 3.
14*49cdfc7eSAndroid Build Coastguard Worker  * - iopl(2) fail with EPERM when the current user is not the super-user.
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #if defined __i386__ || defined(__x86_64__)
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/io.h>
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
27*49cdfc7eSAndroid Build Coastguard Worker 	int level;
28*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
29*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
30*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
31*49cdfc7eSAndroid Build Coastguard Worker 	{4, "Invalid privilege level", EINVAL},
32*49cdfc7eSAndroid Build Coastguard Worker 	{1, "Non super-user", EPERM}
33*49cdfc7eSAndroid Build Coastguard Worker };
34*49cdfc7eSAndroid Build Coastguard Worker 
verify_iopl(unsigned int i)35*49cdfc7eSAndroid Build Coastguard Worker static void verify_iopl(unsigned int i)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	TEST(iopl(tcases[i].level));
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	if ((TST_RET == -1) && (TST_ERR == tcases[i].exp_errno)) {
40*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO,
41*49cdfc7eSAndroid Build Coastguard Worker 			"Expected failure for %s, errno: %d",
42*49cdfc7eSAndroid Build Coastguard Worker 			tcases[i].desc, TST_ERR);
43*49cdfc7eSAndroid Build Coastguard Worker 	} else {
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
45*49cdfc7eSAndroid Build Coastguard Worker 			"%s returned %ld expected -1, expected %s got ",
46*49cdfc7eSAndroid Build Coastguard Worker 			tcases[i].desc, TST_RET, tst_strerrno(tcases[i].exp_errno));
47*49cdfc7eSAndroid Build Coastguard Worker 	}
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)50*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *pw;
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	pw = SAFE_GETPWNAM("nobody");
55*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(pw->pw_uid);
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)58*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(0);
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
64*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
65*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_iopl,
66*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
67*49cdfc7eSAndroid Build Coastguard Worker 	/* iopl() is restricted under kernel lockdown. */
68*49cdfc7eSAndroid Build Coastguard Worker 	.skip_in_lockdown = 1,
69*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
70*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
71*49cdfc7eSAndroid Build Coastguard Worker };
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker #else
74*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("LSB v1.3 does not specify iopl() for this architecture. (only for i386 or x86_64)");
75*49cdfc7eSAndroid Build Coastguard Worker #endif /* __i386_, __x86_64__*/
76