xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setsockopt/setsockopt09.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) 2022 SUSE LLC
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Marcos Paulo de Souza <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * LTP port: Martin Doucha <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Check for possible double free of rx_owner_map after switching packet
12*49cdfc7eSAndroid Build Coastguard Worker  * interface versions aka CVE-2021-22600.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * Kernel crash fixed in:
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  *  commit ec6af094ea28f0f2dda1a6a33b14cd57e36a9755
17*49cdfc7eSAndroid Build Coastguard Worker  *  Author: Willem de Bruijn <[email protected]>
18*49cdfc7eSAndroid Build Coastguard Worker  *  Date:   Wed Dec 15 09:39:37 2021 -0500
19*49cdfc7eSAndroid Build Coastguard Worker  *
20*49cdfc7eSAndroid Build Coastguard Worker  *  net/packet: rx_owner_map depends on pg_vec
21*49cdfc7eSAndroid Build Coastguard Worker  *
22*49cdfc7eSAndroid Build Coastguard Worker  *  commit c800aaf8d869f2b9b47b10c5c312fe19f0a94042
23*49cdfc7eSAndroid Build Coastguard Worker  *  Author: WANG Cong <[email protected]>
24*49cdfc7eSAndroid Build Coastguard Worker  *  Date:   Mon Jul 24 10:07:32 2017 -0700
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  *  packet: fix use-after-free in prb_retire_rx_blk_timer_expired()
27*49cdfc7eSAndroid Build Coastguard Worker  */
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
34*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/if_packet.h"
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker static int sock = -1;
37*49cdfc7eSAndroid Build Coastguard Worker static unsigned int pagesize;
38*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)39*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker 	pagesize = SAFE_SYSCONF(_SC_PAGESIZE);
42*49cdfc7eSAndroid Build Coastguard Worker 	tst_setup_netns();
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker 
run(void)45*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
46*49cdfc7eSAndroid Build Coastguard Worker {
47*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i, version = TPACKET_V3;
48*49cdfc7eSAndroid Build Coastguard Worker 	struct tpacket_req3 req = {
49*49cdfc7eSAndroid Build Coastguard Worker 		.tp_block_size = 4 * pagesize,
50*49cdfc7eSAndroid Build Coastguard Worker 		.tp_frame_size = TPACKET_ALIGNMENT << 7,
51*49cdfc7eSAndroid Build Coastguard Worker 		.tp_retire_blk_tov = 64,
52*49cdfc7eSAndroid Build Coastguard Worker 		.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH
53*49cdfc7eSAndroid Build Coastguard Worker 	};
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 5; i++) {
56*49cdfc7eSAndroid Build Coastguard Worker 		req.tp_block_nr = 256;
57*49cdfc7eSAndroid Build Coastguard Worker 		req.tp_frame_nr = req.tp_block_size * req.tp_block_nr;
58*49cdfc7eSAndroid Build Coastguard Worker 		req.tp_frame_nr /= req.tp_frame_size;
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 		sock = SAFE_SOCKET(AF_PACKET, SOCK_RAW, 0);
61*49cdfc7eSAndroid Build Coastguard Worker 		TEST(setsockopt(sock, SOL_PACKET, PACKET_VERSION, &version,
62*49cdfc7eSAndroid Build Coastguard Worker 			sizeof(version)));
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_RET == -1 && TST_ERR == EINVAL)
65*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TCONF | TTERRNO, "TPACKET_V3 not supported");
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_RET) {
68*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK | TTERRNO,
69*49cdfc7eSAndroid Build Coastguard Worker 				"setsockopt(PACKET_VERSION, TPACKET_V3)");
70*49cdfc7eSAndroid Build Coastguard Worker 		}
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 		/* Allocate owner map and then free it again */
73*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETSOCKOPT(sock, SOL_PACKET, PACKET_RX_RING, &req,
74*49cdfc7eSAndroid Build Coastguard Worker 			sizeof(req));
75*49cdfc7eSAndroid Build Coastguard Worker 		req.tp_block_nr = 0;
76*49cdfc7eSAndroid Build Coastguard Worker 		req.tp_frame_nr = 0;
77*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETSOCKOPT(sock, SOL_PACKET, PACKET_RX_RING, &req,
78*49cdfc7eSAndroid Build Coastguard Worker 			sizeof(req));
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 		/* Switch packet version and trigger double free of owner map */
81*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETSOCKOPT_INT(sock, SOL_PACKET, PACKET_VERSION,
82*49cdfc7eSAndroid Build Coastguard Worker 			TPACKET_V2);
83*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETSOCKOPT(sock, SOL_PACKET, PACKET_RX_RING, &req,
84*49cdfc7eSAndroid Build Coastguard Worker 			sizeof(req));
85*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sock);
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 		/* Wait for socket timer to expire just in case */
88*49cdfc7eSAndroid Build Coastguard Worker 		usleep(req.tp_retire_blk_tov * 3000);
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 		if (tst_taint_check()) {
91*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "Kernel is vulnerable");
92*49cdfc7eSAndroid Build Coastguard Worker 			return;
93*49cdfc7eSAndroid Build Coastguard Worker 		}
94*49cdfc7eSAndroid Build Coastguard Worker 	}
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Nothing bad happened, probably");
97*49cdfc7eSAndroid Build Coastguard Worker }
98*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)99*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
100*49cdfc7eSAndroid Build Coastguard Worker {
101*49cdfc7eSAndroid Build Coastguard Worker 	if (sock >= 0)
102*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sock);
103*49cdfc7eSAndroid Build Coastguard Worker }
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
106*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
107*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
108*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
109*49cdfc7eSAndroid Build Coastguard Worker 	.taint_check = TST_TAINT_W | TST_TAINT_D,
110*49cdfc7eSAndroid Build Coastguard Worker 	.needs_kconfigs = (const char *[]) {
111*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_USER_NS=y",
112*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_NET_NS=y",
113*49cdfc7eSAndroid Build Coastguard Worker 		NULL
114*49cdfc7eSAndroid Build Coastguard Worker 	},
115*49cdfc7eSAndroid Build Coastguard Worker 	.save_restore = (const struct tst_path_val[]) {
116*49cdfc7eSAndroid Build Coastguard Worker 		{"/proc/sys/user/max_user_namespaces", "1024", TST_SR_SKIP},
117*49cdfc7eSAndroid Build Coastguard Worker 		{}
118*49cdfc7eSAndroid Build Coastguard Worker 	},
119*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
120*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "ec6af094ea28"},
121*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "c800aaf8d869"},
122*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2021-22600"},
123*49cdfc7eSAndroid Build Coastguard Worker 		{}
124*49cdfc7eSAndroid Build Coastguard Worker 	}
125*49cdfc7eSAndroid Build Coastguard Worker };
126