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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
* Description: Test that a provided buffer ring descriptor's len is not
* persistently corrupted by a non-incremental bundle operation
* that truncates the head buffer.
*
* 1) A bundle recv that fails with -EAGAIN (no data available)
* must NOT leave the head buffer's len shrunk to the
* requested size. A later operation using the same buffer
* group must still see the full buffer length.
*
* 2) A bundle recv that succeeds while truncating the buffer
* (sqe->len < buf->len) must post a completion that matches
* the actual transfer length, not the original buffer len,
* and must consume the buffer correctly.
*
* Based on the reproducer from Federico Brasili:
* https://lore.kernel.org/io-uring/CAAEr8jbY60noGj1fw_k91UJRBkyiRVoS6=nLhZ7Svwidjn4CAA@mail.gmail.com/
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include "liburing.h"
#include "helpers.h"
#define BGID 1
#define NR_BUFS 2
#define BUF_SIZE 4096
#define SHORT_LEN 32
static int no_buf_ring, no_bundle;
struct test_ctx {
struct io_uring ring;
struct io_uring_buf_ring *br;
void *buf_mem;
};
static void *buf_addr(struct test_ctx *t, int bid)
{
return (char *) t->buf_mem + bid * BUF_SIZE;
}
/*
* Set up a ring + a non-incremental provided buffer ring with NR_BUFS
* buffers of BUF_SIZE each, fully published to the kernel.
*/
static int setup(struct test_ctx *t)
{
struct io_uring_params p = { };
int ret, i, mask;
ret = io_uring_queue_init_params(8, &t->ring, &p);
if (ret < 0) {
fprintf(stderr, "queue init: %d\n", ret);
return T_EXIT_FAIL;
}
if (!(p.features & IORING_FEAT_RECVSEND_BUNDLE)) {
no_bundle = 1;
io_uring_queue_exit(&t->ring);
return T_EXIT_SKIP;
}
if (posix_memalign(&t->buf_mem, 4096, NR_BUFS * BUF_SIZE)) {
perror("posix_memalign");
io_uring_queue_exit(&t->ring);
return T_EXIT_FAIL;
}
t->br = io_uring_setup_buf_ring(&t->ring, NR_BUFS, BGID, 0, &ret);
if (!t->br) {
if (ret == -EINVAL) {
no_buf_ring = 1;
free(t->buf_mem);
io_uring_queue_exit(&t->ring);
return T_EXIT_SKIP;
}
fprintf(stderr, "buf ring setup: %d\n", ret);
free(t->buf_mem);
io_uring_queue_exit(&t->ring);
return T_EXIT_FAIL;
}
mask = io_uring_buf_ring_mask(NR_BUFS);
for (i = 0; i < NR_BUFS; i++)
io_uring_buf_ring_add(t->br, buf_addr(t, i), BUF_SIZE, i, mask, i);
io_uring_buf_ring_advance(t->br, NR_BUFS);
return T_EXIT_PASS;
}
static void teardown(struct test_ctx *t)
{
io_uring_free_buf_ring(&t->ring, t->br, NR_BUFS, BGID);
io_uring_queue_exit(&t->ring);
free(t->buf_mem);
}
static int submit_wait(struct test_ctx *t, struct io_uring_cqe **cqe)
{
int ret;
ret = io_uring_submit(&t->ring);
if (ret != 1) {
fprintf(stderr, "submit: %d\n", ret);
return -1;
}
ret = io_uring_wait_cqe(&t->ring, cqe);
if (ret < 0) {
fprintf(stderr, "wait_cqe: %d\n", ret);
return -1;
}
return 0;
}
/*
* Case 1: a failed (-EAGAIN) bundle recv on an empty socket must not shrink
* the head buffer's len. We verify both the raw descriptor and that a later
* unrelated READ using the same group sees the full buffer.
*/
static int test_eagain_no_corrupt(void)
{
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
struct test_ctx t;
int ret, sv[2], pfd[2], bid;
char *pipe_buf;
ret = setup(&t);
if (ret != T_EXIT_PASS)
return ret;
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sv) < 0) {
perror("socketpair");
teardown(&t);
return T_EXIT_FAIL;
}
/* empty socket, MSG_DONTWAIT -> -EAGAIN, bundle, len=1 */
sqe = io_uring_get_sqe(&t.ring);
io_uring_prep_recv(sqe, sv[0], NULL, 1, MSG_DONTWAIT);
sqe->ioprio |= IORING_RECVSEND_BUNDLE;
sqe->flags |= IOSQE_BUFFER_SELECT;
sqe->buf_group = BGID;
if (submit_wait(&t, &cqe))
goto fail;
if (cqe->res == -EINVAL) {
no_bundle = 1;
io_uring_cqe_seen(&t.ring, cqe);
goto skip;
}
if (cqe->res != -EAGAIN) {
fprintf(stderr, "case1: expected -EAGAIN, got %d\n", cqe->res);
io_uring_cqe_seen(&t.ring, cqe);
goto fail;
}
io_uring_cqe_seen(&t.ring, cqe);
/* the failed recv must not have consumed or shrunk buffer 0 */
if (t.br->bufs[0].len != BUF_SIZE) {
fprintf(stderr, "case1: head buf len corrupted: %u (want %u)\n",
t.br->bufs[0].len, BUF_SIZE);
goto fail;
}
/*
* User-visible impact: an unrelated READ from a pipe using the same
* buffer group must consume the full buffer, not the poisoned len.
*/
if (pipe(pfd) < 0) {
perror("pipe");
goto fail;
}
pipe_buf = malloc(BUF_SIZE);
memset(pipe_buf, 0x5a, BUF_SIZE);
if (write(pfd[1], pipe_buf, BUF_SIZE) != BUF_SIZE) {
perror("write pipe");
free(pipe_buf);
close(pfd[0]);
close(pfd[1]);
goto fail;
}
free(pipe_buf);
sqe = io_uring_get_sqe(&t.ring);
io_uring_prep_read(sqe, pfd[0], NULL, BUF_SIZE, 0);
sqe->flags |= IOSQE_BUFFER_SELECT;
sqe->buf_group = BGID;
ret = submit_wait(&t, &cqe);
close(pfd[0]);
close(pfd[1]);
if (ret)
goto fail;
if (cqe->res != BUF_SIZE) {
fprintf(stderr, "case1: read consumed poisoned len: res=%d "
"(want %d)\n", cqe->res, BUF_SIZE);
io_uring_cqe_seen(&t.ring, cqe);
goto fail;
}
if (!(cqe->flags & IORING_CQE_F_BUFFER)) {
fprintf(stderr, "case1: read did not select a buffer\n");
io_uring_cqe_seen(&t.ring, cqe);
goto fail;
}
bid = cqe->flags >> IORING_CQE_BUFFER_SHIFT;
if (bid != 0) {
fprintf(stderr, "case1: read used wrong bid %d\n", bid);
io_uring_cqe_seen(&t.ring, cqe);
goto fail;
}
io_uring_cqe_seen(&t.ring, cqe);
close(sv[0]);
close(sv[1]);
teardown(&t);
return T_EXIT_PASS;
skip:
close(sv[0]);
close(sv[1]);
teardown(&t);
return T_EXIT_SKIP;
fail:
close(sv[0]);
close(sv[1]);
teardown(&t);
return T_EXIT_FAIL;
}
/*
* Case 2: a successful bundle recv that truncates the buffer (len=32 on a
* 4096 buffer) must post a completion matching the actual transfer length
* (32), not the buffer's original len, and must read only the truncated
* amount even though more data is queued.
*/
static int test_success_trim(void)
{
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
struct test_ctx t;
char snd[64], *got;
int ret, sv[2], bid, i;
ret = setup(&t);
if (ret != T_EXIT_PASS)
return ret;
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
perror("socketpair");
teardown(&t);
return T_EXIT_FAIL;
}
for (i = 0; i < (int) sizeof(snd); i++)
snd[i] = 'A' + (i % 26);
if (write(sv[1], snd, sizeof(snd)) != sizeof(snd)) {
perror("write sock");
goto fail;
}
/* 64 bytes available, ask for 32: trim to 32, expect res == 32 */
sqe = io_uring_get_sqe(&t.ring);
io_uring_prep_recv(sqe, sv[0], NULL, SHORT_LEN, MSG_DONTWAIT);
sqe->ioprio |= IORING_RECVSEND_BUNDLE;
sqe->flags |= IOSQE_BUFFER_SELECT;
sqe->buf_group = BGID;
if (submit_wait(&t, &cqe))
goto fail;
if (cqe->res == -EINVAL) {
no_bundle = 1;
io_uring_cqe_seen(&t.ring, cqe);
goto skip;
}
if (cqe->res != SHORT_LEN) {
fprintf(stderr, "case2: expected res=%d, got %d\n",
SHORT_LEN, cqe->res);
io_uring_cqe_seen(&t.ring, cqe);
goto fail;
}
if (!(cqe->flags & IORING_CQE_F_BUFFER)) {
fprintf(stderr, "case2: recv did not select a buffer\n");
io_uring_cqe_seen(&t.ring, cqe);
goto fail;
}
bid = cqe->flags >> IORING_CQE_BUFFER_SHIFT;
if (bid != 0) {
fprintf(stderr, "case2: recv used wrong bid %d\n", bid);
io_uring_cqe_seen(&t.ring, cqe);
goto fail;
}
io_uring_cqe_seen(&t.ring, cqe);
/* the 32 bytes landed in buffer 0 and match the head of the stream */
got = buf_addr(&t, 0);
if (memcmp(got, snd, SHORT_LEN)) {
fprintf(stderr, "case2: received data mismatch\n");
goto fail;
}
close(sv[0]);
close(sv[1]);
teardown(&t);
return T_EXIT_PASS;
skip:
close(sv[0]);
close(sv[1]);
teardown(&t);
return T_EXIT_SKIP;
fail:
close(sv[0]);
close(sv[1]);
teardown(&t);
return T_EXIT_FAIL;
}
int main(int argc, char *argv[])
{
int ret;
if (argc > 1)
return T_EXIT_SKIP;
ret = test_eagain_no_corrupt();
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "test_eagain_no_corrupt failed\n");
return T_EXIT_FAIL;
}
if (no_buf_ring || no_bundle)
return T_EXIT_SKIP;
ret = test_success_trim();
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "test_success_trim failed\n");
return T_EXIT_FAIL;
}
return T_EXIT_PASS;
}
|