1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2017 Richard Palethorpe <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker /*
6*49cdfc7eSAndroid Build Coastguard Worker * CVE-2016-7117
7*49cdfc7eSAndroid Build Coastguard Worker *
8*49cdfc7eSAndroid Build Coastguard Worker * This tests for a use after free caused by a race between recvmmsg() and
9*49cdfc7eSAndroid Build Coastguard Worker * close(). The exit path for recvmmsg() in (a2e2725541f: net: Introduce
10*49cdfc7eSAndroid Build Coastguard Worker * recvmmsg socket syscall) called fput() on the active file descriptor before
11*49cdfc7eSAndroid Build Coastguard Worker * checking the error state and setting the socket's error field.
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * If one or more messages are received by recvmmsg() followed by one which
14*49cdfc7eSAndroid Build Coastguard Worker * fails, the socket's error field will be set. If just after recvmmsg() calls
15*49cdfc7eSAndroid Build Coastguard Worker * fput(), a call to close() is made on the same file descriptor there is a
16*49cdfc7eSAndroid Build Coastguard Worker * race between close() releasing the socket object and recvmmsg() setting its
17*49cdfc7eSAndroid Build Coastguard Worker * error field.
18*49cdfc7eSAndroid Build Coastguard Worker *
19*49cdfc7eSAndroid Build Coastguard Worker * fput() does not release a file descriptor's resources (e.g. a socket)
20*49cdfc7eSAndroid Build Coastguard Worker * immediatly, it queues them to be released just before a system call returns
21*49cdfc7eSAndroid Build Coastguard Worker * to user land. So the close() system call must call fput() after it is
22*49cdfc7eSAndroid Build Coastguard Worker * called in recvmmsg(), exit and release the resources all before the socket
23*49cdfc7eSAndroid Build Coastguard Worker * error is set.
24*49cdfc7eSAndroid Build Coastguard Worker *
25*49cdfc7eSAndroid Build Coastguard Worker * Usually if the vulnerability is present the test will be killed with a
26*49cdfc7eSAndroid Build Coastguard Worker * kernel null pointer exception. However this is not guaranteed to happen
27*49cdfc7eSAndroid Build Coastguard Worker * every time.
28*49cdfc7eSAndroid Build Coastguard Worker *
29*49cdfc7eSAndroid Build Coastguard Worker * The following was used for reference
30*49cdfc7eSAndroid Build Coastguard Worker * https://blog.lizzie.io/notes-about-cve-2016-7117.html
31*49cdfc7eSAndroid Build Coastguard Worker */
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
43*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_net.h"
44*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer.h"
45*49cdfc7eSAndroid Build Coastguard Worker #include "tst_fuzzy_sync.h"
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker /* The bug was present in the kernel before recvmmsg was exposed by glibc */
48*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
49*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/socket.h"
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker #define MSG "abcdefghijklmnop"
52*49cdfc7eSAndroid Build Coastguard Worker #define RECV_TIMEOUT 1
53*49cdfc7eSAndroid Build Coastguard Worker #define ATTEMPTS 0x1FFFFF
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker static volatile int socket_fds[2];
56*49cdfc7eSAndroid Build Coastguard Worker static struct mmsghdr msghdrs[2] = {
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker .msg_hdr = {
59*49cdfc7eSAndroid Build Coastguard Worker .msg_iov = &(struct iovec) {
60*49cdfc7eSAndroid Build Coastguard Worker .iov_len = sizeof(MSG),
61*49cdfc7eSAndroid Build Coastguard Worker },
62*49cdfc7eSAndroid Build Coastguard Worker .msg_iovlen = 1
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker },
65*49cdfc7eSAndroid Build Coastguard Worker {
66*49cdfc7eSAndroid Build Coastguard Worker .msg_hdr = {
67*49cdfc7eSAndroid Build Coastguard Worker .msg_iov = &(struct iovec) {
68*49cdfc7eSAndroid Build Coastguard Worker .iov_base = (void *)(0xbadadd),
69*49cdfc7eSAndroid Build Coastguard Worker .iov_len = ~0,
70*49cdfc7eSAndroid Build Coastguard Worker },
71*49cdfc7eSAndroid Build Coastguard Worker .msg_iovlen = 1
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker };
75*49cdfc7eSAndroid Build Coastguard Worker static char rbuf[sizeof(MSG)];
76*49cdfc7eSAndroid Build Coastguard Worker static struct timespec timeout = { .tv_sec = RECV_TIMEOUT };
77*49cdfc7eSAndroid Build Coastguard Worker static struct tst_fzsync_pair fzsync_pair;
78*49cdfc7eSAndroid Build Coastguard Worker static void *send_and_close(void *);
79*49cdfc7eSAndroid Build Coastguard Worker
setup(void)80*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker fzsync_pair.min_samples = 10000;
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker tst_syscall(__NR_recvmmsg, 0, 0, 0, 0, 0);
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_init(&fzsync_pair);
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)89*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker close(socket_fds[0]);
92*49cdfc7eSAndroid Build Coastguard Worker close(socket_fds[1]);
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_cleanup(&fzsync_pair);
95*49cdfc7eSAndroid Build Coastguard Worker }
96*49cdfc7eSAndroid Build Coastguard Worker
send_and_close(void * arg)97*49cdfc7eSAndroid Build Coastguard Worker static void *send_and_close(void *arg)
98*49cdfc7eSAndroid Build Coastguard Worker {
99*49cdfc7eSAndroid Build Coastguard Worker while (tst_fzsync_run_b(&fzsync_pair)) {
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_wait_b(&fzsync_pair);
102*49cdfc7eSAndroid Build Coastguard Worker send(socket_fds[0], MSG, sizeof(MSG), 0);
103*49cdfc7eSAndroid Build Coastguard Worker send(socket_fds[0], MSG, sizeof(MSG), 0);
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker close(socket_fds[0]);
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_start_race_b(&fzsync_pair);
108*49cdfc7eSAndroid Build Coastguard Worker close(socket_fds[1]);
109*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_end_race_b(&fzsync_pair);
110*49cdfc7eSAndroid Build Coastguard Worker }
111*49cdfc7eSAndroid Build Coastguard Worker return arg;
112*49cdfc7eSAndroid Build Coastguard Worker }
113*49cdfc7eSAndroid Build Coastguard Worker
run(void)114*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
115*49cdfc7eSAndroid Build Coastguard Worker {
116*49cdfc7eSAndroid Build Coastguard Worker int stat, too_early_count = 0;
117*49cdfc7eSAndroid Build Coastguard Worker
118*49cdfc7eSAndroid Build Coastguard Worker msghdrs[0].msg_hdr.msg_iov->iov_base = (void *)&rbuf;
119*49cdfc7eSAndroid Build Coastguard Worker
120*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_reset(&fzsync_pair, send_and_close);
121*49cdfc7eSAndroid Build Coastguard Worker while (tst_fzsync_run_a(&fzsync_pair)) {
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Worker SAFE_SOCKETPAIR(AF_LOCAL, SOCK_DGRAM, 0, (int *)socket_fds);
124*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_wait_a(&fzsync_pair);
125*49cdfc7eSAndroid Build Coastguard Worker
126*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_start_race_a(&fzsync_pair);
127*49cdfc7eSAndroid Build Coastguard Worker stat = tst_syscall(__NR_recvmmsg,
128*49cdfc7eSAndroid Build Coastguard Worker socket_fds[1], msghdrs, 2, 0, &timeout);
129*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_end_race_a(&fzsync_pair);
130*49cdfc7eSAndroid Build Coastguard Worker
131*49cdfc7eSAndroid Build Coastguard Worker if (stat == 0)
132*49cdfc7eSAndroid Build Coastguard Worker tst_res(TWARN, "No messages received, should be one");
133*49cdfc7eSAndroid Build Coastguard Worker else if (stat < 0) {
134*49cdfc7eSAndroid Build Coastguard Worker if (errno != EBADF) {
135*49cdfc7eSAndroid Build Coastguard Worker tst_res(TWARN | TERRNO,
136*49cdfc7eSAndroid Build Coastguard Worker "recvmmsg failed unexpectedly");
137*49cdfc7eSAndroid Build Coastguard Worker } else {
138*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_add_bias(&fzsync_pair, 1);
139*49cdfc7eSAndroid Build Coastguard Worker too_early_count++;
140*49cdfc7eSAndroid Build Coastguard Worker }
141*49cdfc7eSAndroid Build Coastguard Worker }
142*49cdfc7eSAndroid Build Coastguard Worker }
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Nothing bad happened, probably");
145*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Socket was closed too early %d times", too_early_count);
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
149*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
150*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
151*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
152*49cdfc7eSAndroid Build Coastguard Worker .max_runtime = 60,
153*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
154*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "a2e2725541fa"},
155*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "2016-7117"},
156*49cdfc7eSAndroid Build Coastguard Worker {}
157*49cdfc7eSAndroid Build Coastguard Worker }
158*49cdfc7eSAndroid Build Coastguard Worker };
159