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 */
6
7 /*
8 * This is an error test for ioperm(2) system call.
9 *
10 * Verify that
11 * 1) ioperm(2) returns -1 and sets errno to EINVAL for I/O port
12 * address greater than 0x3ff.
13 * 2) ioperm(2) returns -1 and sets errno to EPERM if the current
14 * user is not the super-user.
15 *
16 * Author: Subhab Biswas <subhabrata.biswas@wipro.com>
17 */
18
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <pwd.h>
23 #include "tst_test.h"
24 #include "tst_safe_macros.h"
25
26 #if defined __i386__ || defined(__x86_64__)
27 #include <sys/io.h>
28
29 #define NUM_BYTES 3
30 #ifndef IO_BITMAP_BITS
31 #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 #define IO_BITMAP_BITS_16 65536
33 #endif
34
35 static struct tcase_t {
36 long from;
37 long num;
38 int turn_on;
39 char *desc;
40 int exp_errno;
41 } tcases[] = {
42 {0, NUM_BYTES, 1, "Invalid I/O address", EINVAL},
43 {0, NUM_BYTES, 1, "Non super-user", EPERM},
44 };
45
setup(void)46 static void setup(void)
47 {
48 /*
49 * The value of IO_BITMAP_BITS (include/asm-i386/processor.h) changed
50 * from kernel 2.6.8 to permit 16-bits (65536) ioperm
51 *
52 * Ricky Ng-Adam, rngadam@yahoo.com
53 */
54 if ((tst_kvercmp(2, 6, 8) < 0) || (tst_kvercmp(2, 6, 9) == 0)) {
55 tcases[0].from = (IO_BITMAP_BITS - NUM_BYTES) + 1;
56 tcases[1].from = IO_BITMAP_BITS - NUM_BYTES;
57 } else {
58 tcases[0].from = (IO_BITMAP_BITS_16 - NUM_BYTES) + 1;
59 tcases[1].from = IO_BITMAP_BITS_16 - NUM_BYTES;
60 }
61
62 struct passwd *pw;
63 pw = SAFE_GETPWNAM("nobody");
64 SAFE_SETEUID(pw->pw_uid);
65 }
66
cleanup(void)67 static void cleanup(void)
68 {
69 SAFE_SETEUID(0);
70 }
71
verify_ioperm(unsigned int i)72 static void verify_ioperm(unsigned int i)
73 {
74 TEST(ioperm(tcases[i].from, tcases[i].num, tcases[i].turn_on));
75
76 if ((TST_RET == -1) && (TST_ERR == tcases[i].exp_errno)) {
77 tst_res(TPASS | TTERRNO, "Expected failure for %s, "
78 "errno: %d", tcases[i].desc, TST_ERR);
79 } else {
80 tst_res(TFAIL | TTERRNO, "Unexpected results for %s ; "
81 "returned %ld (expected -1), errno %d "
82 "(expected errno %d)", tcases[i].desc,
83 TST_RET, TST_ERR, tcases[i].exp_errno);
84 }
85 }
86
87 static struct tst_test test = {
88 .tcnt = ARRAY_SIZE(tcases),
89 .test = verify_ioperm,
90 .needs_root = 1,
91 /* ioperm() is restricted under kernel lockdown. */
92 .skip_in_lockdown = 1,
93 .setup = setup,
94 .cleanup = cleanup,
95 };
96
97 #else
98 TST_TEST_TCONF("LSB v1.3 does not specify ioperm() for this architecture. (only for i386 or x86_64)");
99 #endif /* __i386_, __x86_64__*/
100