aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/ring-leak.c
blob: 6b8237fdc49d193c042cb63b89d5bed838367af3 (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
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
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Based on description from Al Viro - this demonstrates a leak of the
 * io_uring instance, by sending the io_uring fd over a UNIX socket.
 *
 * See:
 *
 * https://lore.kernel.org/linux-block/20190129192702.3605-1-axboe@kernel.dk/T/#m6c87fc64e4d063786af6ec6fadce3ac1e95d3184
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <signal.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <linux/fs.h>

#include "liburing.h"
#include "helpers.h"
#include "../src/syscall.h"

static int __io_uring_register_files(int ring_fd, int fd1, int fd2)
{
	__s32 fds[2] = { fd1, fd2 };

	return __sys_io_uring_register(ring_fd, IORING_REGISTER_FILES, fds, 2);
}

static int get_ring_fd(void)
{
	struct io_uring_params p;
	int fd;

	memset(&p, 0, sizeof(p));

	fd = __sys_io_uring_setup(2, &p);
	if (fd < 0) {
		perror("io_uring_setup");
		return -1;
	}

	return fd;
}

static int send_fd(int socket, int fd)
{
	char buf[CMSG_SPACE(sizeof(fd))];
	struct cmsghdr *cmsg;
	struct msghdr msg;

	memset(buf, 0, sizeof(buf));
	memset(&msg, 0, sizeof(msg));

	msg.msg_control = buf;
	msg.msg_controllen = sizeof(buf);

	cmsg = CMSG_FIRSTHDR(&msg);
	cmsg->cmsg_level = SOL_SOCKET;
	cmsg->cmsg_type = SCM_RIGHTS;
	cmsg->cmsg_len = CMSG_LEN(sizeof(fd));

	memmove(CMSG_DATA(cmsg), &fd, sizeof(fd));

	msg.msg_controllen = CMSG_SPACE(sizeof(fd));

	if (sendmsg(socket, &msg, 0) < 0) {
		if (errno == EINVAL)
			return T_EXIT_SKIP;
		perror("sendmsg");
		return T_EXIT_FAIL;
	}

	return T_EXIT_PASS;
}

static int test_iowq_request_cancel(void)
{
	char buffer[128];
	struct io_uring ring;
	struct io_uring_sqe *sqe;
	int ret, fds[2];

	ret = io_uring_queue_init(8, &ring, 0);
	if (ret < 0) {
		fprintf(stderr, "failed to init io_uring: %s\n", strerror(-ret));
		return ret;
	}
	if (pipe(fds)) {
		perror("pipe");
		return -1;
	}
	ret = io_uring_register_files(&ring, fds, 2);
	if (ret) {
		fprintf(stderr, "file_register: %d\n", ret);
		return ret;
	}
	close(fds[1]);

	sqe = io_uring_get_sqe(&ring);
	if (!sqe) {
		fprintf(stderr, "%s: failed to get sqe\n", __FUNCTION__);
		return 1;
	}
	/* potentially sitting in internal polling */
	io_uring_prep_read(sqe, 0, buffer, 10, 0);
	sqe->flags |= IOSQE_FIXED_FILE;

	sqe = io_uring_get_sqe(&ring);
	if (!sqe) {
		fprintf(stderr, "%s: failed to get sqe\n", __FUNCTION__);
		return 1;
	}
	/* staying in io-wq */
	io_uring_prep_read(sqe, 0, buffer, 10, 0);
	sqe->flags |= IOSQE_FIXED_FILE | IOSQE_ASYNC;

	ret = io_uring_submit(&ring);
	if (ret != 2) {
		fprintf(stderr, "%s: got %d, wanted 1\n", __FUNCTION__, ret);
		return 1;
	}

	/* should unregister files and close the write fd */
	io_uring_queue_exit(&ring);

	/*
	 * We're trying to wait for the ring to "really" exit, that will be
	 * done async. For that rely on the registered write end to be closed
	 * after ring quiesce, so failing read from the other pipe end.
	 */
	ret = read(fds[0], buffer, 10);
	if (ret < 0)
		perror("read");
	close(fds[0]);
	return 0;
}

static void trigger_unix_gc(void)
{
	int fd;

	fd = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (fd < 0)
		perror("socket dgram");
	else
		close(fd);
}

static int test_scm_cycles(bool update)
{
	char buffer[128];
	struct io_uring ring;
	int i, ret;
	int sp[2], fds[2], reg_fds[4];

	if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sp) != 0) {
		perror("Failed to create Unix-domain socket pair\n");
		return 1;
	}
	ret = io_uring_queue_init(8, &ring, 0);
	if (ret < 0) {
		fprintf(stderr, "failed to init io_uring: %s\n", strerror(-ret));
		return ret;
	}
	if (pipe(fds)) {
		perror("pipe");
		return -1;
	}
	ret = send_fd(sp[0], ring.ring_fd);
	if (ret != T_EXIT_PASS)
		return ret;

	/* register an empty set for updates */
	if (update) {
		for (i = 0; i < 4; i++)
			reg_fds[i] = -1;
		ret = io_uring_register_files(&ring, reg_fds, 4);
		if (ret) {
			fprintf(stderr, "file_register: %d\n", ret);
			return ret;
		}
	}

	reg_fds[0] = fds[0];
	reg_fds[1] = fds[1];
	reg_fds[2] = sp[0];
	reg_fds[3] = sp[1];
	if (update) {
		ret = io_uring_register_files_update(&ring, 0, reg_fds, 4);
		if (ret != 4) {
			fprintf(stderr, "file_register: %d\n", ret);
			return ret;
		}
	} else {
		ret = io_uring_register_files(&ring, reg_fds, 4);
		if (ret) {
			fprintf(stderr, "file_register: %d\n", ret);
			return ret;
		}
	}

	close(fds[1]);
	close(sp[0]);
	close(sp[1]);

	/* should unregister files and close the write fd */
	io_uring_queue_exit(&ring);

	trigger_unix_gc();

	/*
	 * We're trying to wait for the ring to "really" exit, that will be
	 * done async. For that rely on the registered write end to be closed
	 * after ring quiesce, so failing read from the other pipe end.
	 */
	ret = read(fds[0], buffer, 10);
	if (ret < 0)
		perror("read");
	close(fds[0]);
	return 0;
}

int main(int argc, char *argv[])
{
	int sp[2], pid, ring_fd, ret;
	int i;

	if (argc > 1)
		return 0;

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

	for (i = 0; i < 2; i++) {
		bool update = !!(i & 1);

		ret = test_scm_cycles(update);
		if (ret == T_EXIT_SKIP)
			return T_EXIT_SKIP;
		if (ret) {
			fprintf(stderr, "test_scm_cycles() failed %i\n",
				update);
			return 1;
		}
	}

	if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sp) != 0) {
		perror("Failed to create Unix-domain socket pair\n");
		return 1;
	}

	ring_fd = get_ring_fd();
	if (ring_fd < 0)
		return 1;

	ret = __io_uring_register_files(ring_fd, sp[0], sp[1]);
	if (ret < 0) {
		perror("register files");
		return 1;
	}

	pid = fork();
	if (pid) {
		ret = send_fd(sp[0], ring_fd);
		if (ret != T_EXIT_PASS)
			return ret;
	}

	close(ring_fd);
	close(sp[0]);
	close(sp[1]);
	return 0;
}