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