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