xref: /aosp_15_r20/external/ltp/testcases/network/packet/fanout01.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) 2018 Richard Palethorpe <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  * Original reproducer: https://blogs.securiteam.com/index.php/archives/3484
5*49cdfc7eSAndroid Build Coastguard Worker  * Other copyrights may apply.
6*49cdfc7eSAndroid Build Coastguard Worker  *
7*49cdfc7eSAndroid Build Coastguard Worker  * CVE-2017-15649
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Fixed by the following commits:
10*49cdfc7eSAndroid Build Coastguard Worker  * 4971613c "packet: in packet_do_bind, test fanout with bind_lock held"
11*49cdfc7eSAndroid Build Coastguard Worker  * 008ba2a1 "packet: hold bind lock when rebinding to fanout hook"
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * See blogpost in copyright notice for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <net/if.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <linux/if_packet.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_fuzzy_sync.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/if_packet.h"
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static struct tst_fzsync_pair pair;
26*49cdfc7eSAndroid Build Coastguard Worker static int fd;
27*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_ll addr;
28*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)29*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker 	tst_setup_netns();
32*49cdfc7eSAndroid Build Coastguard Worker 	tst_fzsync_pair_init(&pair);
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)35*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	tst_fzsync_pair_cleanup(&pair);
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker 
binder(void * unused)40*49cdfc7eSAndroid Build Coastguard Worker void *binder(void *unused)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	while (tst_fzsync_run_b(&pair)) {
43*49cdfc7eSAndroid Build Coastguard Worker 		tst_fzsync_start_race_b(&pair);
44*49cdfc7eSAndroid Build Coastguard Worker 		bind(fd, (struct sockaddr *)&addr, sizeof(addr));
45*49cdfc7eSAndroid Build Coastguard Worker 		tst_fzsync_end_race_b(&pair);
46*49cdfc7eSAndroid Build Coastguard Worker 	}
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	return unused;
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker 
run(void)51*49cdfc7eSAndroid Build Coastguard Worker void run(void)
52*49cdfc7eSAndroid Build Coastguard Worker {
53*49cdfc7eSAndroid Build Coastguard Worker 	int fanout_val = PACKET_FANOUT_ROLLOVER, index;
54*49cdfc7eSAndroid Build Coastguard Worker 	struct ifreq ifr;
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	memset(&ifr, 0, sizeof(struct ifreq));
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	tst_fzsync_pair_reset(&pair, binder);
59*49cdfc7eSAndroid Build Coastguard Worker 	while (tst_fzsync_run_a(&pair)) {
60*49cdfc7eSAndroid Build Coastguard Worker 		fd = SAFE_SOCKET(AF_PACKET, SOCK_RAW, PF_PACKET);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 		strcpy((char *)&ifr.ifr_name, "lo");
63*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_IOCTL(fd, SIOCGIFINDEX, &ifr);
64*49cdfc7eSAndroid Build Coastguard Worker 		index = ifr.ifr_ifindex;
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_IOCTL(fd, SIOCGIFFLAGS, &ifr);
67*49cdfc7eSAndroid Build Coastguard Worker 		ifr.ifr_flags &= ~(short)IFF_UP;
68*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_IOCTL(fd, SIOCSIFFLAGS, &ifr);
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 		addr.sll_family = AF_PACKET;
71*49cdfc7eSAndroid Build Coastguard Worker 		/* need something different to rehook && 0 to skip register_prot_hook */
72*49cdfc7eSAndroid Build Coastguard Worker 		addr.sll_protocol = 0x0;
73*49cdfc7eSAndroid Build Coastguard Worker 		addr.sll_ifindex = index;
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 		tst_fzsync_start_race_a(&pair);
76*49cdfc7eSAndroid Build Coastguard Worker 		setsockopt(fd, SOL_PACKET, PACKET_FANOUT,
77*49cdfc7eSAndroid Build Coastguard Worker 			   &fanout_val, sizeof(fanout_val));
78*49cdfc7eSAndroid Build Coastguard Worker 		tst_fzsync_end_race_a(&pair);
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 		/* UAF */
81*49cdfc7eSAndroid Build Coastguard Worker 		close(fd);
82*49cdfc7eSAndroid Build Coastguard Worker 	}
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Nothing bad happened, probably...");
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
88*49cdfc7eSAndroid Build Coastguard Worker 	.min_kver = "3.19",
89*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
90*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
91*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
92*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
93*49cdfc7eSAndroid Build Coastguard Worker 	.max_runtime = 180,
94*49cdfc7eSAndroid Build Coastguard Worker 	.needs_kconfigs = (const char *[]) {
95*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_USER_NS=y",
96*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_NET_NS=y",
97*49cdfc7eSAndroid Build Coastguard Worker 		NULL
98*49cdfc7eSAndroid Build Coastguard Worker 	},
99*49cdfc7eSAndroid Build Coastguard Worker 	.save_restore = (const struct tst_path_val[]) {
100*49cdfc7eSAndroid Build Coastguard Worker 		{"/proc/sys/user/max_user_namespaces", "1024", TST_SR_SKIP},
101*49cdfc7eSAndroid Build Coastguard Worker 		{}
102*49cdfc7eSAndroid Build Coastguard Worker 	},
103*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
104*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2017-15649"},
105*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "4971613c1639"},
106*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "008ba2a13f2d"},
107*49cdfc7eSAndroid Build Coastguard Worker 		{}
108*49cdfc7eSAndroid Build Coastguard Worker 	}
109*49cdfc7eSAndroid Build Coastguard Worker };
110