xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setsockopt/setsockopt07.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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) 2020 SUSE LLC <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker  * CVE-2017-1000111
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Check for race condition between packet_set_ring() and tp_reserve.
10*49cdfc7eSAndroid Build Coastguard Worker  * The race allows you to set tp_reserve bigger than ring buffer size.
11*49cdfc7eSAndroid Build Coastguard Worker  * While this will cause truncation of all incoming packets to 0 bytes,
12*49cdfc7eSAndroid Build Coastguard Worker  * sanity checks in tpacket_rcv() prevent any exploitable buffer overflows.
13*49cdfc7eSAndroid Build Coastguard Worker  * Race fixed in:
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *  commit c27927e372f0785f3303e8fad94b85945e2c97b7 (HEAD)
16*49cdfc7eSAndroid Build Coastguard Worker  *  Author: Willem de Bruijn <[email protected]>
17*49cdfc7eSAndroid Build Coastguard Worker  *  Date:   Thu Aug 10 12:41:58 2017 -0400
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  *  packet: fix tp_reserve race in packet_set_ring
20*49cdfc7eSAndroid Build Coastguard Worker  */
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_fuzzy_sync.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/if_packet.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/if_ether.h"
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker static int sock = -1;
32*49cdfc7eSAndroid Build Coastguard Worker static unsigned int pagesize;
33*49cdfc7eSAndroid Build Coastguard Worker static struct tst_fzsync_pair fzsync_pair;
34*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)35*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	pagesize = SAFE_SYSCONF(_SC_PAGESIZE);
38*49cdfc7eSAndroid Build Coastguard Worker 	tst_setup_netns();
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	/*
41*49cdfc7eSAndroid Build Coastguard Worker 	 * Reproducing the bug on unpatched system takes <15 loops. The test
42*49cdfc7eSAndroid Build Coastguard Worker 	 * is slow and the bug is mostly harmless so don't waste too much
43*49cdfc7eSAndroid Build Coastguard Worker 	 * time.
44*49cdfc7eSAndroid Build Coastguard Worker 	 */
45*49cdfc7eSAndroid Build Coastguard Worker 	fzsync_pair.exec_loops = 500;
46*49cdfc7eSAndroid Build Coastguard Worker 	tst_fzsync_pair_init(&fzsync_pair);
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
thread_run(void * arg)49*49cdfc7eSAndroid Build Coastguard Worker static void *thread_run(void *arg)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int val = 1 << 30;
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	while (tst_fzsync_run_b(&fzsync_pair)) {
54*49cdfc7eSAndroid Build Coastguard Worker 		tst_fzsync_start_race_b(&fzsync_pair);
55*49cdfc7eSAndroid Build Coastguard Worker 		setsockopt(sock, SOL_PACKET, PACKET_RESERVE, &val, sizeof(val));
56*49cdfc7eSAndroid Build Coastguard Worker 		tst_fzsync_end_race_b(&fzsync_pair);
57*49cdfc7eSAndroid Build Coastguard Worker 	}
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	return arg;
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker 
run(void)62*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int val, version = TPACKET_V3;
65*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t vsize = sizeof(val);
66*49cdfc7eSAndroid Build Coastguard Worker 	struct tpacket_req3 req = {
67*49cdfc7eSAndroid Build Coastguard Worker 		.tp_block_size = pagesize,
68*49cdfc7eSAndroid Build Coastguard Worker 		.tp_block_nr = 1,
69*49cdfc7eSAndroid Build Coastguard Worker 		.tp_frame_size = pagesize,
70*49cdfc7eSAndroid Build Coastguard Worker 		.tp_frame_nr = 1,
71*49cdfc7eSAndroid Build Coastguard Worker 		.tp_retire_blk_tov = 100
72*49cdfc7eSAndroid Build Coastguard Worker 	};
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	tst_fzsync_pair_reset(&fzsync_pair, thread_run);
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	while (tst_fzsync_run_a(&fzsync_pair)) {
77*49cdfc7eSAndroid Build Coastguard Worker 		sock = SAFE_SOCKET(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
78*49cdfc7eSAndroid Build Coastguard Worker 		TEST(setsockopt(sock, SOL_PACKET, PACKET_VERSION, &version,
79*49cdfc7eSAndroid Build Coastguard Worker 			sizeof(version)));
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_RET == -1 && TST_ERR == EINVAL)
82*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TCONF | TTERRNO, "TPACKET_V3 not supported");
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_RET) {
85*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK | TTERRNO,
86*49cdfc7eSAndroid Build Coastguard Worker 				"setsockopt(PACKET_VERSION, TPACKET_V3");
87*49cdfc7eSAndroid Build Coastguard Worker 		}
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 		tst_fzsync_start_race_a(&fzsync_pair);
90*49cdfc7eSAndroid Build Coastguard Worker 		TEST(setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &req,
91*49cdfc7eSAndroid Build Coastguard Worker 			sizeof(req)));
92*49cdfc7eSAndroid Build Coastguard Worker 		tst_fzsync_end_race_a(&fzsync_pair);
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_GETSOCKOPT(sock, SOL_PACKET, PACKET_RESERVE, &val, &vsize);
95*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sock);
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_RET == -1 && TST_ERR == EINVAL) {
98*49cdfc7eSAndroid Build Coastguard Worker 			tst_fzsync_pair_add_bias(&fzsync_pair, 1);
99*49cdfc7eSAndroid Build Coastguard Worker 			continue;
100*49cdfc7eSAndroid Build Coastguard Worker 		}
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_RET) {
103*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK | TTERRNO,
104*49cdfc7eSAndroid Build Coastguard Worker 				"Invalid setsockopt() return value");
105*49cdfc7eSAndroid Build Coastguard Worker 		}
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 		if (val > req.tp_block_size) {
108*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "PACKET_RESERVE checks bypassed");
109*49cdfc7eSAndroid Build Coastguard Worker 			return;
110*49cdfc7eSAndroid Build Coastguard Worker 		}
111*49cdfc7eSAndroid Build Coastguard Worker 	}
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Cannot reproduce bug");
114*49cdfc7eSAndroid Build Coastguard Worker }
115*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)116*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
117*49cdfc7eSAndroid Build Coastguard Worker {
118*49cdfc7eSAndroid Build Coastguard Worker 	tst_fzsync_pair_cleanup(&fzsync_pair);
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	if (sock >= 0)
121*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sock);
122*49cdfc7eSAndroid Build Coastguard Worker }
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
125*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
126*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
127*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
128*49cdfc7eSAndroid Build Coastguard Worker 	.max_runtime = 150,
129*49cdfc7eSAndroid Build Coastguard Worker 	.needs_kconfigs = (const char *[]) {
130*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_USER_NS=y",
131*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_NET_NS=y",
132*49cdfc7eSAndroid Build Coastguard Worker 		NULL
133*49cdfc7eSAndroid Build Coastguard Worker 	},
134*49cdfc7eSAndroid Build Coastguard Worker 	.save_restore = (const struct tst_path_val[]) {
135*49cdfc7eSAndroid Build Coastguard Worker 		{"/proc/sys/user/max_user_namespaces", "1024", TST_SR_SKIP},
136*49cdfc7eSAndroid Build Coastguard Worker 		{}
137*49cdfc7eSAndroid Build Coastguard Worker 	},
138*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
139*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "c27927e372f0"},
140*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2017-1000111"},
141*49cdfc7eSAndroid Build Coastguard Worker 		{}
142*49cdfc7eSAndroid Build Coastguard Worker 	}
143*49cdfc7eSAndroid Build Coastguard Worker };
144