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 * Original POC by Daniel Jiang
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker * Test for CVE-2017-2671 faulty locking on ping socket
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * When sys_connect() is called with sockaddr.sin_family set to AF_UNSPEC on a
10*49cdfc7eSAndroid Build Coastguard Worker * ping socket; __udp_disconnect() gets called, which in turn calls the buggy
11*49cdfc7eSAndroid Build Coastguard Worker * function ping_unhashed(). This function does not obtain a rwlock before
12*49cdfc7eSAndroid Build Coastguard Worker * checking if the socket is hashed allowing the socket data to be pulled from
13*49cdfc7eSAndroid Build Coastguard Worker * underneath it in the time between calling sk_hashed() and gaining the write
14*49cdfc7eSAndroid Build Coastguard Worker * lock.
15*49cdfc7eSAndroid Build Coastguard Worker *
16*49cdfc7eSAndroid Build Coastguard Worker * Fixed in commit 43a6684519ab0a6c52024b5e25322476cabad893
17*49cdfc7eSAndroid Build Coastguard Worker *
18*49cdfc7eSAndroid Build Coastguard Worker * This test repeatedly 'connects' a ping socket correctly then calls
19*49cdfc7eSAndroid Build Coastguard Worker * connect() with AF_UNSPEC in two seperate threads to trigger the race
20*49cdfc7eSAndroid Build Coastguard Worker * condition. If the bug is present, then the test will most likely crash the
21*49cdfc7eSAndroid Build Coastguard Worker * system.
22*49cdfc7eSAndroid Build Coastguard Worker *
23*49cdfc7eSAndroid Build Coastguard Worker * The test requests root privileges so that it can ensure ping sockets are
24*49cdfc7eSAndroid Build Coastguard Worker * enabled. On distributions (including Android) where ping sockets are
25*49cdfc7eSAndroid Build Coastguard Worker * enabled by default, root privileges are not required.
26*49cdfc7eSAndroid Build Coastguard Worker */
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <arpa/inet.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
35*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_net.h"
36*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_pthread.h"
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker #include "tst_fuzzy_sync.h"
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker #define ATTEMPTS 0x80000
41*49cdfc7eSAndroid Build Coastguard Worker #define PING_SYSCTL_PATH "/proc/sys/net/ipv4/ping_group_range"
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker static int sockfd;
44*49cdfc7eSAndroid Build Coastguard Worker static unsigned int ping_min_grp, ping_max_grp;
45*49cdfc7eSAndroid Build Coastguard Worker static struct tst_fzsync_pair fzsync_pair;
46*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in iaddr, uaddr;
47*49cdfc7eSAndroid Build Coastguard Worker static void *connect_b(void *);
48*49cdfc7eSAndroid Build Coastguard Worker
setup(void)49*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker iaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
52*49cdfc7eSAndroid Build Coastguard Worker uaddr = iaddr;
53*49cdfc7eSAndroid Build Coastguard Worker iaddr.sin_family = AF_INET;
54*49cdfc7eSAndroid Build Coastguard Worker uaddr.sin_family = AF_UNSPEC;
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker if (access(PING_SYSCTL_PATH, F_OK))
57*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "socket() does not support IPPROTO_ICMP");
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF(PING_SYSCTL_PATH, "%u %u",
60*49cdfc7eSAndroid Build Coastguard Worker &ping_min_grp, &ping_max_grp);
61*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(PING_SYSCTL_PATH, "0 0");
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
64*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Created ping socket, attempting to race...");
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_init(&fzsync_pair);
67*49cdfc7eSAndroid Build Coastguard Worker }
68*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)69*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_cleanup(&fzsync_pair);
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker if (sockfd > 0)
74*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(sockfd);
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker if (ping_min_grp | ping_max_grp)
77*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(PING_SYSCTL_PATH, "%u %u",
78*49cdfc7eSAndroid Build Coastguard Worker ping_min_grp, ping_max_grp);
79*49cdfc7eSAndroid Build Coastguard Worker }
80*49cdfc7eSAndroid Build Coastguard Worker
connect_b(void * param LTP_ATTRIBUTE_UNUSED)81*49cdfc7eSAndroid Build Coastguard Worker static void *connect_b(void * param LTP_ATTRIBUTE_UNUSED)
82*49cdfc7eSAndroid Build Coastguard Worker {
83*49cdfc7eSAndroid Build Coastguard Worker while (tst_fzsync_run_b(&fzsync_pair)) {
84*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_start_race_b(&fzsync_pair);
85*49cdfc7eSAndroid Build Coastguard Worker connect(sockfd, (struct sockaddr *)&uaddr, sizeof(uaddr));
86*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_end_race_b(&fzsync_pair);
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker return 0;
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker
run(void)92*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
93*49cdfc7eSAndroid Build Coastguard Worker {
94*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_reset(&fzsync_pair, connect_b);
95*49cdfc7eSAndroid Build Coastguard Worker while (tst_fzsync_run_a(&fzsync_pair)) {
96*49cdfc7eSAndroid Build Coastguard Worker SAFE_CONNECT(sockfd,
97*49cdfc7eSAndroid Build Coastguard Worker (struct sockaddr *)&iaddr, sizeof(iaddr));
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_start_race_a(&fzsync_pair);
100*49cdfc7eSAndroid Build Coastguard Worker connect(sockfd, (struct sockaddr *)&uaddr, sizeof(uaddr));
101*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_end_race_a(&fzsync_pair);
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "We didn't crash");
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
108*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
109*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
110*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
111*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
112*49cdfc7eSAndroid Build Coastguard Worker .max_runtime = 40,
113*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
114*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "43a6684519ab"},
115*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "2017-2671"},
116*49cdfc7eSAndroid Build Coastguard Worker {}
117*49cdfc7eSAndroid Build Coastguard Worker }
118*49cdfc7eSAndroid Build Coastguard Worker };
119