summaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/sqe-mixed-boundary.c
blob: 8819f1e3c7f417d3b3831ba47fab0fe35a43563c (plain) (blame)
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
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Description: test SQE_MIXED physical SQE boundary validation with sq_array
 *
 * Verify that 128-byte operations are correctly rejected when sq_array
 * remaps them to the last physical SQE slot, preventing a 64-byte OOB
 * read past the SQE array.
 */
#include <stdio.h>
#include <string.h>

#include "liburing.h"
#include "helpers.h"
#include "test.h"

#define NENTRIES	4

/*
 * Positive test: NOP128 at a valid physical position should succeed.
 */
static int test_valid_position(void)
{
	struct io_uring ring;
	struct io_uring_params p = { .flags = IORING_SETUP_SQE_MIXED };
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	int ret;

	ret = t_io_uring_init_sqarray(NENTRIES, &ring, &p);
	if (ret) {
		if (ret == -EINVAL)
			return T_EXIT_SKIP;
		fprintf(stderr, "ring init: %d\n", ret);
		return T_EXIT_FAIL;
	}

	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_nop(sqe);
	sqe->user_data = 1;

	sqe = io_uring_get_sqe128(&ring);
	if (!sqe) {
		fprintf(stderr, "get_sqe128 failed\n");
		goto fail;
	}
	io_uring_prep_nop128(sqe);
	sqe->user_data = 2;

	ret = io_uring_submit(&ring);
	if (ret < 0) {
		fprintf(stderr, "submit: %d\n", ret);
		goto fail;
	}

	ret = io_uring_wait_cqe(&ring, &cqe);
	if (ret) {
		fprintf(stderr, "wait_cqe: %d\n", ret);
		goto fail;
	}
	io_uring_cqe_seen(&ring, cqe);

	ret = io_uring_wait_cqe(&ring, &cqe);
	if (ret) {
		fprintf(stderr, "wait_cqe: %d\n", ret);
		goto fail;
	}
	if (cqe->user_data == 2 && cqe->res != 0) {
		fprintf(stderr, "NOP128 at valid position failed: %d\n",
			cqe->res);
		io_uring_cqe_seen(&ring, cqe);
		goto fail;
	}
	io_uring_cqe_seen(&ring, cqe);

	io_uring_queue_exit(&ring);
	return T_EXIT_PASS;
fail:
	io_uring_queue_exit(&ring);
	return T_EXIT_FAIL;
}

/*
 * Negative test: NOP128 at the last physical SQE slot via sq_array remap
 * must be rejected. Without the kernel fix, this triggers a 64-byte OOB
 * read in io_uring_cmd_sqe_copy().
 */
static int test_oob_boundary(void)
{
	struct io_uring ring;
	struct io_uring_params p = { .flags = IORING_SETUP_SQE_MIXED };
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	unsigned mask;
	int ret, i, found;

	ret = t_io_uring_init_sqarray(NENTRIES, &ring, &p);
	if (ret) {
		if (ret == -EINVAL)
			return T_EXIT_SKIP;
		fprintf(stderr, "ring init: %d\n", ret);
		return T_EXIT_FAIL;
	}

	mask = *ring.sq.kring_entries - 1;

	/* Advance internal tail: NOP (1) + NOP128 (2) = 3 slots */
	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_nop(sqe);
	sqe->user_data = 1;

	sqe = io_uring_get_sqe128(&ring);
	if (!sqe) {
		fprintf(stderr, "get_sqe128 failed\n");
		goto fail;
	}

	/*
	 * Override: remap logical position 1 to last physical slot.
	 * Prep NOP128 there instead of the position get_sqe128 returned.
	 */
	ring.sq.array[1] = mask;
	memset(&ring.sq.sqes[mask], 0, sizeof(struct io_uring_sqe));
	io_uring_prep_nop128(&ring.sq.sqes[mask]);
	ring.sq.sqes[mask].user_data = 2;

	ret = io_uring_submit(&ring);
	if (ret < 0) {
		fprintf(stderr, "submit: %d\n", ret);
		goto fail;
	}

	found = 0;
	for (i = 0; i < 2; i++) {
		ret = io_uring_wait_cqe(&ring, &cqe);
		if (ret)
			break;
		if (cqe->user_data == 2) {
			if (cqe->res != -EINVAL) {
				fprintf(stderr,
					"NOP128 at last slot: expected -EINVAL, got %d\n",
					cqe->res);
				io_uring_cqe_seen(&ring, cqe);
				goto fail;
			}
			found = 1;
		}
		io_uring_cqe_seen(&ring, cqe);
	}

	if (!found) {
		fprintf(stderr, "no CQE for NOP128 boundary test\n");
		goto fail;
	}

	io_uring_queue_exit(&ring);
	return T_EXIT_PASS;
fail:
	io_uring_queue_exit(&ring);
	return T_EXIT_FAIL;
}

int main(int argc, char *argv[])
{
	int ret;

	if (argc > 1)
		return T_EXIT_SKIP;

	ret = test_valid_position();
	if (ret == T_EXIT_SKIP)
		return T_EXIT_SKIP;
	if (ret) {
		fprintf(stderr, "test_valid_position failed\n");
		return T_EXIT_FAIL;
	}

	ret = test_oob_boundary();
	if (ret) {
		fprintf(stderr, "test_oob_boundary failed\n");
		return ret;
	}

	return T_EXIT_PASS;
}