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
|
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
* repro-CVE-2020-29373 -- Reproducer for CVE-2020-29373.
*
* Copyright (c) 2021 SUSE
* Author: Nicolai Stange <nstange@suse.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "liburing.h"
/*
* This attempts to make the kernel issue a sendmsg() to
* path from io_uring's async io_sq_wq_submit_work().
*
* Unfortunately, IOSQE_ASYNC is available only from kernel version
* 5.6 onwards. To still force io_uring to process the request
* asynchronously from io_sq_wq_submit_work(), queue a couple of
* auxiliary requests all failing with EAGAIN before. This is
* implemented by writing repeatedly to an auxiliary O_NONBLOCK
* AF_UNIX socketpair with a small SO_SNDBUF.
*/
static int try_sendmsg_async(const char * const path)
{
int snd_sock, r;
struct io_uring ring;
char sbuf[16] = {};
struct iovec siov = { .iov_base = &sbuf, .iov_len = sizeof(sbuf) };
struct sockaddr_un addr = {};
struct msghdr msg = {
.msg_name = &addr,
.msg_namelen = sizeof(addr),
.msg_iov = &siov,
.msg_iovlen = 1,
};
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
snd_sock = socket(AF_UNIX, SOCK_DGRAM, 0);
if (snd_sock < 0) {
perror("socket(AF_UNIX)");
return -1;
}
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, path);
r = io_uring_queue_init(512, &ring, 0);
if (r < 0) {
fprintf(stderr, "ring setup failed: %d\n", r);
goto close_iour;
}
sqe = io_uring_get_sqe(&ring);
if (!sqe) {
fprintf(stderr, "get sqe failed\n");
r = -EFAULT;
goto close_iour;
}
/* the actual one supposed to fail with -ENOENT. */
io_uring_prep_sendmsg(sqe, snd_sock, &msg, 0);
sqe->flags = IOSQE_ASYNC;
sqe->user_data = 255;
r = io_uring_submit(&ring);
if (r != 1) {
fprintf(stderr, "sqe submit failed: %d\n", r);
r = -EFAULT;
goto close_iour;
}
r = io_uring_wait_cqe(&ring, &cqe);
if (r < 0) {
fprintf(stderr, "wait completion %d\n", r);
r = -EFAULT;
goto close_iour;
}
if (cqe->user_data != 255) {
fprintf(stderr, "user data %d\n", r);
r = -EFAULT;
goto close_iour;
}
if (cqe->res != -ENOENT) {
r = 3;
fprintf(stderr,
"error: cqe %i: res=%i, but expected -ENOENT\n",
(int)cqe->user_data, (int)cqe->res);
}
io_uring_cqe_seen(&ring, cqe);
close_iour:
io_uring_queue_exit(&ring);
close(snd_sock);
return r;
}
int main(int argc, char *argv[])
{
int r;
char tmpdir[] = "/tmp/tmp.XXXXXX";
int rcv_sock;
struct sockaddr_un addr = {};
pid_t c;
int wstatus;
if (!mkdtemp(tmpdir)) {
perror("mkdtemp()");
return 1;
}
rcv_sock = socket(AF_UNIX, SOCK_DGRAM, 0);
if (rcv_sock < 0) {
perror("socket(AF_UNIX)");
r = 1;
goto rmtmpdir;
}
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/sock", tmpdir);
r = bind(rcv_sock, (struct sockaddr *)&addr,
sizeof(addr));
if (r < 0) {
perror("bind()");
close(rcv_sock);
r = 1;
goto rmtmpdir;
}
c = fork();
if (!c) {
close(rcv_sock);
r = chroot(tmpdir);
if (r) {
if (errno == EPERM) {
fprintf(stderr, "chroot not allowed, skip\n");
return 0;
}
perror("chroot()");
return 1;
}
r = try_sendmsg_async(addr.sun_path);
if (r < 0) {
/* system call failure */
r = 1;
} else if (r) {
/* test case failure */
r += 1;
}
return r;
}
if (waitpid(c, &wstatus, 0) == (pid_t)-1) {
perror("waitpid()");
r = 1;
goto rmsock;
}
if (!WIFEXITED(wstatus)) {
fprintf(stderr, "child got terminated\n");
r = 1;
goto rmsock;
}
r = WEXITSTATUS(wstatus);
if (r)
fprintf(stderr, "error: Test failed\n");
rmsock:
close(rcv_sock);
unlink(addr.sun_path);
rmtmpdir:
rmdir(tmpdir);
return r;
}
|