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