summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/prog_tests/sock_create.c
blob: 072910c05c995893b6e9ad2615ae1c5747fbeea1 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// SPDX-License-Identifier: GPL-2.0
#include <linux/bpf.h>
#include <test_progs.h>
#include "cgroup_helpers.h"

static char bpf_log_buf[4096];
static bool verbose;

static struct sock_create_test {
	const char			*descr;
	const struct bpf_insn		insns[64];
	enum bpf_attach_type		attach_type;
	enum bpf_attach_type		expected_attach_type;

	int				domain;
	int				type;

	int				optname;
	int				optval;
} tests[] = {
	{
		.descr = "AF_INET set priority",
		.insns = {
			/* r3 = 123 (priority) */
			BPF_MOV64_IMM(BPF_REG_3, 123),
			BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3,
				    offsetof(struct bpf_sock, priority)),

			/* return 1 */
			BPF_MOV64_IMM(BPF_REG_0, 1),
			BPF_EXIT_INSN(),
		},
		.expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE,
		.attach_type = BPF_CGROUP_INET_SOCK_CREATE,

		.domain = AF_INET,
		.type = SOCK_DGRAM,

		.optname = SO_PRIORITY,
		.optval = 123,
	},
	{
		.descr = "AF_INET6 set priority",
		.insns = {
			/* r3 = 123 (priority) */
			BPF_MOV64_IMM(BPF_REG_3, 123),
			BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3,
				    offsetof(struct bpf_sock, priority)),

			/* return 1 */
			BPF_MOV64_IMM(BPF_REG_0, 1),
			BPF_EXIT_INSN(),
		},
		.expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE,
		.attach_type = BPF_CGROUP_INET_SOCK_CREATE,

		.domain = AF_INET6,
		.type = SOCK_DGRAM,

		.optname = SO_PRIORITY,
		.optval = 123,
	},
	{
		.descr = "AF_INET set mark",
		.insns = {
			BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),

			/* get uid of process */
			BPF_EMIT_CALL(BPF_FUNC_get_current_uid_gid),
			BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0xffffffff),

			/* if uid is 0, use given mark(666), else use uid as the mark */
			BPF_MOV64_REG(BPF_REG_3, BPF_REG_0),
			BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
			BPF_MOV64_IMM(BPF_REG_3, 666),

			BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
			BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3,
				    offsetof(struct bpf_sock, mark)),

			/* return 1 */
			BPF_MOV64_IMM(BPF_REG_0, 1),
			BPF_EXIT_INSN(),
		},
		.expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE,
		.attach_type = BPF_CGROUP_INET_SOCK_CREATE,

		.domain = AF_INET,
		.type = SOCK_DGRAM,

		.optname = SO_MARK,
		.optval = 666,
	},
	{
		.descr = "AF_INET6 set mark",
		.insns = {
			BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),

			/* get uid of process */
			BPF_EMIT_CALL(BPF_FUNC_get_current_uid_gid),
			BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0xffffffff),

			/* if uid is 0, use given mark(666), else use uid as the mark */
			BPF_MOV64_REG(BPF_REG_3, BPF_REG_0),
			BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
			BPF_MOV64_IMM(BPF_REG_3, 666),

			BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
			BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3,
				    offsetof(struct bpf_sock, mark)),

			/* return 1 */
			BPF_MOV64_IMM(BPF_REG_0, 1),
			BPF_EXIT_INSN(),
		},
		.expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE,
		.attach_type = BPF_CGROUP_INET_SOCK_CREATE,

		.domain = AF_INET6,
		.type = SOCK_DGRAM,

		.optname = SO_MARK,
		.optval = 666,
	},
	{
		.descr = "AF_INET bound to iface",
		.insns = {
			/* r3 = 1 (lo interface) */
			BPF_MOV64_IMM(BPF_REG_3, 1),
			BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3,
				    offsetof(struct bpf_sock, bound_dev_if)),

			/* return 1 */
			BPF_MOV64_IMM(BPF_REG_0, 1),
			BPF_EXIT_INSN(),
		},
		.expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE,
		.attach_type = BPF_CGROUP_INET_SOCK_CREATE,

		.domain = AF_INET,
		.type = SOCK_DGRAM,

		.optname = SO_BINDTOIFINDEX,
		.optval = 1,
	},
	{
		.descr = "AF_INET6 bound to iface",
		.insns = {
			/* r3 = 1 (lo interface) */
			BPF_MOV64_IMM(BPF_REG_3, 1),
			BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3,
				    offsetof(struct bpf_sock, bound_dev_if)),

			/* return 1 */
			BPF_MOV64_IMM(BPF_REG_0, 1),
			BPF_EXIT_INSN(),
		},
		.expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE,
		.attach_type = BPF_CGROUP_INET_SOCK_CREATE,

		.domain = AF_INET6,
		.type = SOCK_DGRAM,

		.optname = SO_BINDTOIFINDEX,
		.optval = 1,
	},
};

static int load_prog(const struct bpf_insn *insns,
		     enum bpf_attach_type expected_attach_type)
{
	LIBBPF_OPTS(bpf_prog_load_opts, opts,
		    .expected_attach_type = expected_attach_type,
		    .log_level = 2,
		    .log_buf = bpf_log_buf,
		    .log_size = sizeof(bpf_log_buf),
	);
	int fd, insns_cnt = 0;

	for (;
	     insns[insns_cnt].code != (BPF_JMP | BPF_EXIT);
	     insns_cnt++) {
	}
	insns_cnt++;

	fd = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, NULL, "GPL", insns,
			   insns_cnt, &opts);
	if (verbose && fd < 0)
		fprintf(stderr, "%s\n", bpf_log_buf);

	return fd;
}

static int run_test(int cgroup_fd, struct sock_create_test *test)
{
	int sock_fd, err, prog_fd, optval, ret = -1;
	socklen_t optlen = sizeof(optval);

	prog_fd = load_prog(test->insns, test->expected_attach_type);
	if (prog_fd < 0) {
		log_err("Failed to load BPF program");
		return -1;
	}

	err = bpf_prog_attach(prog_fd, cgroup_fd, test->attach_type, 0);
	if (err < 0) {
		log_err("Failed to attach BPF program");
		goto close_prog_fd;
	}

	sock_fd = socket(test->domain, test->type, 0);
	if (sock_fd < 0) {
		log_err("Failed to create socket");
		goto detach_prog;
	}

	err = getsockopt(sock_fd, SOL_SOCKET, test->optname, &optval, &optlen);
	if (err) {
		log_err("Failed to call getsockopt");
		goto cleanup;
	}

	if (optval != test->optval) {
		errno = 0;
		log_err("getsockopt returned unexpected optval");
		goto cleanup;
	}

	ret = 0;

cleanup:
	close(sock_fd);
detach_prog:
	bpf_prog_detach2(prog_fd, cgroup_fd, test->attach_type);
close_prog_fd:
	close(prog_fd);
	return ret;
}

void test_sock_create(void)
{
	int cgroup_fd, i;

	cgroup_fd = test__join_cgroup("/sock_create");
	if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
		return;

	for (i = 0; i < ARRAY_SIZE(tests); i++) {
		if (!test__start_subtest(tests[i].descr))
			continue;

		ASSERT_OK(run_test(cgroup_fd, &tests[i]), tests[i].descr);
	}

	close(cgroup_fd);
}