xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sendto/sendto03.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) 2019 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-2020-14386
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Check for vulnerability in tpacket_rcv() which allows an unprivileged user
10*49cdfc7eSAndroid Build Coastguard Worker  * to write arbitrary data to a memory area outside the allocated packet
11*49cdfc7eSAndroid Build Coastguard Worker  * buffer. Kernel crash fixed in:
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  *  commit acf69c946233259ab4d64f8869d4037a198c7f06
14*49cdfc7eSAndroid Build Coastguard Worker  *  Author: Or Cohen <[email protected]>
15*49cdfc7eSAndroid Build Coastguard Worker  *  Date:   Thu Sep 3 21:05:28 2020 -0700
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  *  net/packet: fix overflow in tpacket_rcv
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ioctl.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <net/if.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <net/ethernet.h>
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "tst_net.h"
30*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/if_packet.h"
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #define BUFSIZE 1024
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker static int dst_sock = -1, sock = -1;
35*49cdfc7eSAndroid Build Coastguard Worker static unsigned char buf[BUFSIZE];
36*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_ll bind_addr, addr;
37*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)38*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker 	struct ifreq ifr;
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	tst_setup_netns();
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	sock = SAFE_SOCKET(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
45*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(ifr.ifr_name, "lo");
46*49cdfc7eSAndroid Build Coastguard Worker 	ifr.ifr_flags = IFF_UP;
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(sock, SIOCSIFFLAGS, &ifr);
48*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(sock, SIOCGIFINDEX, &ifr);
49*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(sock);
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	memset(buf, 0x42, BUFSIZE);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	bind_addr.sll_family = AF_PACKET;
54*49cdfc7eSAndroid Build Coastguard Worker 	bind_addr.sll_protocol = htons(ETH_P_ALL);
55*49cdfc7eSAndroid Build Coastguard Worker 	bind_addr.sll_ifindex = ifr.ifr_ifindex;
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	addr.sll_family = AF_PACKET;
58*49cdfc7eSAndroid Build Coastguard Worker 	addr.sll_ifindex = ifr.ifr_ifindex;
59*49cdfc7eSAndroid Build Coastguard Worker 	addr.sll_halen = ETH_ALEN;
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker /* Test for commit bcc5364bdcfe (cap PACKET_RESERVE to INT_MAX) */
check_tiny_frame(void)63*49cdfc7eSAndroid Build Coastguard Worker static int check_tiny_frame(void)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int val = (UINT_MAX - TPACKET2_HDRLEN) + 1;
66*49cdfc7eSAndroid Build Coastguard Worker 	struct tpacket_req tpreq;
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	tpreq.tp_block_size = SAFE_SYSCONF(_SC_PAGESIZE);
69*49cdfc7eSAndroid Build Coastguard Worker 	tpreq.tp_frame_size = TPACKET_ALIGNMENT;
70*49cdfc7eSAndroid Build Coastguard Worker 	tpreq.tp_block_nr = 1;
71*49cdfc7eSAndroid Build Coastguard Worker 	tpreq.tp_frame_nr = (tpreq.tp_block_size * tpreq.tp_block_nr) /
72*49cdfc7eSAndroid Build Coastguard Worker 		tpreq.tp_frame_size;
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	dst_sock = SAFE_SOCKET(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
75*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETSOCKOPT_INT(dst_sock, SOL_PACKET, PACKET_VERSION, TPACKET_V2);
76*49cdfc7eSAndroid Build Coastguard Worker 	TEST(setsockopt(dst_sock, SOL_PACKET, PACKET_RESERVE, &val,
77*49cdfc7eSAndroid Build Coastguard Worker 		sizeof(val)));
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1 && TST_ERR == EINVAL) {
80*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(dst_sock);
81*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO,
82*49cdfc7eSAndroid Build Coastguard Worker 			"setsockopt(PACKET_RESERVE) value is capped");
83*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
84*49cdfc7eSAndroid Build Coastguard Worker 	}
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
87*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO,
88*49cdfc7eSAndroid Build Coastguard Worker 			"setsockopt(PACKET_RESERVE): unexpected error");
89*49cdfc7eSAndroid Build Coastguard Worker 	}
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET) {
92*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO,
93*49cdfc7eSAndroid Build Coastguard Worker 			"Invalid setsockopt(PACKET_RESERVE) return value");
94*49cdfc7eSAndroid Build Coastguard Worker 	}
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "setsockopt(PACKET_RESERVE) accepted too large value");
97*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Checking whether this will cause integer overflow...");
98*49cdfc7eSAndroid Build Coastguard Worker 	TEST(setsockopt(dst_sock, SOL_PACKET, PACKET_RX_RING, &tpreq,
99*49cdfc7eSAndroid Build Coastguard Worker 		sizeof(tpreq)));
100*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(dst_sock);
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	if (!TST_RET) {
103*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "setsockopt(PACKET_RX_RING) accepted frame "
104*49cdfc7eSAndroid Build Coastguard Worker 			"size smaller than packet header");
105*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
106*49cdfc7eSAndroid Build Coastguard Worker 	}
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
109*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO,
110*49cdfc7eSAndroid Build Coastguard Worker 			"Invalid setsockopt(PACKET_RX_RING) return value");
111*49cdfc7eSAndroid Build Coastguard Worker 	}
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR != EINVAL) {
114*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO,
115*49cdfc7eSAndroid Build Coastguard Worker 			"setsockopt(PACKET_RX_RING): unexpeced error");
116*49cdfc7eSAndroid Build Coastguard Worker 	}
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS | TTERRNO, "setsockopt(PACKET_RX_RING) frame size check "
119*49cdfc7eSAndroid Build Coastguard Worker 		"rejects values smaller than packet header");
120*49cdfc7eSAndroid Build Coastguard Worker 	/* This test case should not cause kernel taint, skip taint check */
121*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
122*49cdfc7eSAndroid Build Coastguard Worker }
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker /* Test for commit acf69c946233 (drop packet if netoff overflows) */
check_vnet_hdr(void)125*49cdfc7eSAndroid Build Coastguard Worker static int check_vnet_hdr(void)
126*49cdfc7eSAndroid Build Coastguard Worker {
127*49cdfc7eSAndroid Build Coastguard Worker 	struct tpacket_req tpreq;
128*49cdfc7eSAndroid Build Coastguard Worker 	size_t blocksize = 0x800000, pagesize = SAFE_SYSCONF(_SC_PAGESIZE);
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 	/* Make sure blocksize is big enough and pagesize aligned */
131*49cdfc7eSAndroid Build Coastguard Worker 	if (blocksize % pagesize)
132*49cdfc7eSAndroid Build Coastguard Worker 		blocksize += pagesize - (blocksize % pagesize);
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker 	tpreq.tp_block_size = blocksize;
135*49cdfc7eSAndroid Build Coastguard Worker 	tpreq.tp_frame_size = 0x11000;
136*49cdfc7eSAndroid Build Coastguard Worker 	tpreq.tp_block_nr = 1;
137*49cdfc7eSAndroid Build Coastguard Worker 	tpreq.tp_frame_nr = (tpreq.tp_block_size * tpreq.tp_block_nr) /
138*49cdfc7eSAndroid Build Coastguard Worker 		tpreq.tp_frame_size;
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 	dst_sock = SAFE_SOCKET(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
141*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETSOCKOPT_INT(dst_sock, SOL_PACKET, PACKET_VERSION, TPACKET_V2);
142*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETSOCKOPT_INT(dst_sock, SOL_PACKET, PACKET_VNET_HDR, 1);
143*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETSOCKOPT_INT(dst_sock, SOL_PACKET, PACKET_RESERVE, 0xffff - 75);
144*49cdfc7eSAndroid Build Coastguard Worker 	TEST(setsockopt(dst_sock, SOL_PACKET, PACKET_RX_RING, &tpreq,
145*49cdfc7eSAndroid Build Coastguard Worker 		sizeof(tpreq)));
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1 && TST_ERR == EINVAL) {
148*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(dst_sock);
149*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TCONF, "PACKET_VNET_HDR and PACKET_RX_RING not "
150*49cdfc7eSAndroid Build Coastguard Worker 			"supported together");
151*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
152*49cdfc7eSAndroid Build Coastguard Worker 	}
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
155*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO,
156*49cdfc7eSAndroid Build Coastguard Worker 			"setsockopt(PACKET_RX_RING): unexpected error");
157*49cdfc7eSAndroid Build Coastguard Worker 	}
158*49cdfc7eSAndroid Build Coastguard Worker 
159*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET) {
160*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO,
161*49cdfc7eSAndroid Build Coastguard Worker 			"Invalid setsockopt(PACKET_RX_RING) return value");
162*49cdfc7eSAndroid Build Coastguard Worker 	}
163*49cdfc7eSAndroid Build Coastguard Worker 
164*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(dst_sock, (struct sockaddr *)&bind_addr, sizeof(addr));
165*49cdfc7eSAndroid Build Coastguard Worker 
166*49cdfc7eSAndroid Build Coastguard Worker 	sock = SAFE_SOCKET(AF_PACKET, SOCK_RAW, IPPROTO_RAW);
167*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SENDTO(1, sock, buf, BUFSIZE, 0, (struct sockaddr *)&addr,
168*49cdfc7eSAndroid Build Coastguard Worker 		sizeof(addr));
169*49cdfc7eSAndroid Build Coastguard Worker 
170*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(sock);
171*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(dst_sock);
172*49cdfc7eSAndroid Build Coastguard Worker 	return 1; /* Continue to taint check */
173*49cdfc7eSAndroid Build Coastguard Worker }
174*49cdfc7eSAndroid Build Coastguard Worker 
175*49cdfc7eSAndroid Build Coastguard Worker static int (*testcase_list[])(void) = {check_tiny_frame, check_vnet_hdr};
176*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int n)177*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
178*49cdfc7eSAndroid Build Coastguard Worker {
179*49cdfc7eSAndroid Build Coastguard Worker 	if (!testcase_list[n]())
180*49cdfc7eSAndroid Build Coastguard Worker 		return;
181*49cdfc7eSAndroid Build Coastguard Worker 
182*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_taint_check()) {
183*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Kernel is vulnerable");
184*49cdfc7eSAndroid Build Coastguard Worker 		return;
185*49cdfc7eSAndroid Build Coastguard Worker 	}
186*49cdfc7eSAndroid Build Coastguard Worker 
187*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Nothing bad happened, probably");
188*49cdfc7eSAndroid Build Coastguard Worker }
189*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)190*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
191*49cdfc7eSAndroid Build Coastguard Worker {
192*49cdfc7eSAndroid Build Coastguard Worker 	if (sock != -1)
193*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sock);
194*49cdfc7eSAndroid Build Coastguard Worker 
195*49cdfc7eSAndroid Build Coastguard Worker 	if (dst_sock != -1)
196*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(dst_sock);
197*49cdfc7eSAndroid Build Coastguard Worker }
198*49cdfc7eSAndroid Build Coastguard Worker 
199*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
200*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
201*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(testcase_list),
202*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
203*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
204*49cdfc7eSAndroid Build Coastguard Worker 	.taint_check = TST_TAINT_W | TST_TAINT_D,
205*49cdfc7eSAndroid Build Coastguard Worker 	.needs_kconfigs = (const char *[]) {
206*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_USER_NS=y",
207*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_NET_NS=y",
208*49cdfc7eSAndroid Build Coastguard Worker 		NULL
209*49cdfc7eSAndroid Build Coastguard Worker 	},
210*49cdfc7eSAndroid Build Coastguard Worker 	.save_restore = (const struct tst_path_val[]) {
211*49cdfc7eSAndroid Build Coastguard Worker 		{"/proc/sys/user/max_user_namespaces", "1024", TST_SR_SKIP},
212*49cdfc7eSAndroid Build Coastguard Worker 		{}
213*49cdfc7eSAndroid Build Coastguard Worker 	},
214*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
215*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "bcc5364bdcfe"},
216*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "acf69c946233"},
217*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2020-14386"},
218*49cdfc7eSAndroid Build Coastguard Worker 		{}
219*49cdfc7eSAndroid Build Coastguard Worker 	}
220*49cdfc7eSAndroid Build Coastguard Worker };
221