summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/exec/binfmt_misc_disabled.c
blob: 47c9e8a4ee42aad49ca803827e4f3fd6acef77ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// SPDX-License-Identifier: GPL-2.0
/*
 * Test the 'D' (register disabled) flag of binfmt_misc. An entry
 * registered with it exists but cannot be matched until userspace enables
 * it, which splits a registration into create and activate.
 *
 * Needs root for the registration; no bpf toolchain involved.
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

#include "binfmt_misc_common.h"
#include "kselftest_harness.h"

#define MAGIC		"#DISABLED-SELFTEST#"
#define TARGET_PATH	"/tmp/binfmt_disabled_target"
#define INTERP_PATH	"/tmp/binfmt_disabled_interp.sh"
#define ENTRY		"test_disabled"
#define RULE(flags)	":" ENTRY ":M:0:" MAGIC "::" INTERP_PATH ":" flags

/* The interpreter exits with a code the harness can recognise. */
#define EXIT_INTERP	7

/* The target only has to carry the magic; it is never actually loaded. */
static int create_target(void)
{
	char buf[128] = MAGIC "\n";
	int fd;

	unlink(TARGET_PATH);
	fd = open(TARGET_PATH, O_WRONLY | O_CREAT | O_EXCL, 0755);
	if (fd < 0)
		return -1;
	if (write(fd, buf, sizeof(buf)) != (ssize_t)sizeof(buf)) {
		close(fd);
		return -1;
	}
	close(fd);
	return 0;
}

static int create_interp(void)
{
	char buf[64];
	int fd;

	unlink(INTERP_PATH);
	fd = open(INTERP_PATH, O_WRONLY | O_CREAT | O_EXCL, 0755);
	if (fd < 0)
		return -1;
	snprintf(buf, sizeof(buf), "#!/bin/sh\nexit %d\n", EXIT_INTERP);
	if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
		close(fd);
		return -1;
	}
	return close(fd);
}

FIXTURE(disabled) {
};

FIXTURE_SETUP(disabled)
{
	if (getuid() != 0)
		SKIP(return, "test must be run as root");
	if (!binfmt_misc_available())
		SKIP(return, "no binfmt_misc");

	/* Skip the whole suite on a kernel that does not know 'D'. */
	if (!binfmt_flag_supported('D')) {
		ASSERT_EQ(errno, EINVAL);
		SKIP(return, "kernel without the 'D' flag");
	}

	ASSERT_EQ(create_interp(), 0);
	ASSERT_EQ(create_target(), 0);
}

FIXTURE_TEARDOWN(disabled)
{
	unregister(ENTRY);
	unlink(TARGET_PATH);
	unlink(INTERP_PATH);
}

/* The entry exists but does not dispatch until it is enabled. */
TEST_F(disabled, inert_until_enabled)
{
	ASSERT_EQ(write_reg(RULE("D")), 0);
	EXPECT_TRUE(entry_shows(ENTRY, "disabled"));

	/* Nothing matches it, so no binary format claims the target. */
	EXPECT_EQ(run_payload(TARGET_PATH), RUN_ENOEXEC);

	ASSERT_EQ(entry_command(ENTRY, "1\n"), 0);
	EXPECT_TRUE(entry_shows(ENTRY, "enabled"));
	EXPECT_EQ(run_payload(TARGET_PATH), EXIT_INTERP);
}

/* Without 'D' an entry is matchable the moment it is registered. */
TEST_F(disabled, enabled_without_the_flag)
{
	ASSERT_EQ(write_reg(RULE("")), 0);
	EXPECT_TRUE(entry_shows(ENTRY, "enabled"));
	EXPECT_EQ(run_payload(TARGET_PATH), EXIT_INTERP);
}

/* 'D' is spent on the registration: the entry does not report it back. */
TEST_F(disabled, flag_not_reported)
{
	ASSERT_EQ(write_reg(RULE("D")), 0);
	EXPECT_FALSE(entry_shows(ENTRY, "flags: D"));
	EXPECT_TRUE(entry_shows(ENTRY, "flags: "));
}

/* A disabled entry can be disabled and enabled like any other. */
TEST_F(disabled, toggles_like_any_entry)
{
	ASSERT_EQ(write_reg(RULE("D")), 0);

	ASSERT_EQ(entry_command(ENTRY, "1\n"), 0);
	ASSERT_EQ(run_payload(TARGET_PATH), EXIT_INTERP);
	ASSERT_EQ(entry_command(ENTRY, "0\n"), 0);
	EXPECT_EQ(run_payload(TARGET_PATH), RUN_ENOEXEC);
	ASSERT_EQ(entry_command(ENTRY, "1\n"), 0);
	EXPECT_EQ(run_payload(TARGET_PATH), EXIT_INTERP);
}

/* 'D' composes with the invocation flags a static entry can carry. */
TEST_F(disabled, composes_with_invocation_flags)
{
	ASSERT_EQ(write_reg(RULE("PD")), 0);
	EXPECT_TRUE(entry_shows(ENTRY, "disabled"));
	EXPECT_TRUE(entry_shows(ENTRY, "flags: P"));
}

/* '-1' to the status file sweeps a staged entry with everything else. */
TEST_F(disabled, removed_by_remove_all)
{
	int fd;

	ASSERT_EQ(write_reg(RULE("D")), 0);
	EXPECT_TRUE(entry_shows(ENTRY, "disabled"));

	fd = open(BINFMT_DIR "/status", O_WRONLY | O_CLOEXEC);
	ASSERT_GE(fd, 0);
	ASSERT_EQ(write(fd, "-1", 2), 2);
	close(fd);

	EXPECT_NE(access(BINFMT_DIR "/" ENTRY, F_OK), 0);
}

/* A file handle held across a removal cannot resurrect the entry. */
TEST_F(disabled, no_resurrection_after_remove)
{
	int fd;

	ASSERT_EQ(write_reg(RULE("D")), 0);
	fd = open(BINFMT_DIR "/" ENTRY, O_WRONLY | O_CLOEXEC);
	ASSERT_GE(fd, 0);

	ASSERT_EQ(write(fd, "-1", 2), 2);
	EXPECT_NE(access(BINFMT_DIR "/" ENTRY, F_OK), 0);

	/* Accepted like any toggle of a removed entry, but publishes nothing. */
	EXPECT_EQ(write(fd, "1", 1), 1);
	EXPECT_EQ(run_payload(TARGET_PATH), RUN_ENOEXEC);
	close(fd);
}

TEST_HARNESS_MAIN