xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setsockopt/setsockopt05.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-2017-1000112
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Check that UDP fragmentation offload doesn't cause memory corruption
10*49cdfc7eSAndroid Build Coastguard Worker  * if the userspace process turns off UFO in between two send() calls.
11*49cdfc7eSAndroid Build Coastguard Worker  * Kernel crash fixed in:
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  *  commit 85f1bd9a7b5a79d5baa8bf44af19658f7bf77bfa
14*49cdfc7eSAndroid Build Coastguard Worker  *  Author: Willem de Bruijn <[email protected]>
15*49cdfc7eSAndroid Build Coastguard Worker  *  Date:   Thu Aug 10 12:29:19 2017 -0400
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  *  udp: consistently apply ufo or fragmentation
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ioctl.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <net/if.h>
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_net.h"
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker #define BUFSIZE 4000
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in addr;
32*49cdfc7eSAndroid Build Coastguard Worker static int dst_sock = -1;
33*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)34*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker 	struct ifreq ifr;
37*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t addrlen = sizeof(addr);
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	tst_setup_netns();
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, 0);
42*49cdfc7eSAndroid Build Coastguard Worker 	dst_sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(ifr.ifr_name, "lo");
45*49cdfc7eSAndroid Build Coastguard Worker 	ifr.ifr_mtu = 1500;
46*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(dst_sock, SIOCSIFMTU, &ifr);
47*49cdfc7eSAndroid Build Coastguard Worker 	ifr.ifr_flags = IFF_UP;
48*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(dst_sock, SIOCSIFFLAGS, &ifr);
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(dst_sock, (struct sockaddr *)&addr, addrlen);
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETSOCKNAME(dst_sock, (struct sockaddr*)&addr, &addrlen);
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)54*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
55*49cdfc7eSAndroid Build Coastguard Worker {
56*49cdfc7eSAndroid Build Coastguard Worker 	if (dst_sock != -1)
57*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(dst_sock);
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker 
run(void)60*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
61*49cdfc7eSAndroid Build Coastguard Worker {
62*49cdfc7eSAndroid Build Coastguard Worker 	int sock, i;
63*49cdfc7eSAndroid Build Coastguard Worker 	char buf[BUFSIZE];
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	memset(buf, 0x42, BUFSIZE);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 1000; i++) {
68*49cdfc7eSAndroid Build Coastguard Worker 		sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
69*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CONNECT(sock, (struct sockaddr *)&addr, sizeof(addr));
70*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SEND(1, sock, buf, BUFSIZE, MSG_MORE);
71*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETSOCKOPT_INT(sock, SOL_SOCKET, SO_NO_CHECK, 1);
72*49cdfc7eSAndroid Build Coastguard Worker 		send(sock, buf, 1, 0);
73*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sock);
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 		if (tst_taint_check()) {
76*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "Kernel is vulnerable");
77*49cdfc7eSAndroid Build Coastguard Worker 			return;
78*49cdfc7eSAndroid Build Coastguard Worker 		}
79*49cdfc7eSAndroid Build Coastguard Worker 	}
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Nothing bad happened, probably");
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
85*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
86*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
87*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
88*49cdfc7eSAndroid Build Coastguard Worker 	.taint_check = TST_TAINT_W | TST_TAINT_D,
89*49cdfc7eSAndroid Build Coastguard Worker 	.needs_kconfigs = (const char *[]) {
90*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_USER_NS=y",
91*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_NET_NS=y",
92*49cdfc7eSAndroid Build Coastguard Worker 		NULL
93*49cdfc7eSAndroid Build Coastguard Worker 	},
94*49cdfc7eSAndroid Build Coastguard Worker 	.save_restore = (const struct tst_path_val[]) {
95*49cdfc7eSAndroid Build Coastguard Worker 		{"/proc/sys/user/max_user_namespaces", "1024", TST_SR_SKIP},
96*49cdfc7eSAndroid Build Coastguard Worker 		{}
97*49cdfc7eSAndroid Build Coastguard Worker 	},
98*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
99*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "85f1bd9a7b5a"},
100*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2017-1000112"},
101*49cdfc7eSAndroid Build Coastguard Worker 		{}
102*49cdfc7eSAndroid Build Coastguard Worker 	}
103*49cdfc7eSAndroid Build Coastguard Worker };
104