xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/connect/connect02.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) 2017 Christoph Paasch <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2020 SUSE LLC <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  * CVE-2018-9568
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * Test that connect() to AF_UNSPEC address correctly converts IPV6 socket
9*49cdfc7eSAndroid Build Coastguard Worker  * to IPV4 listen socket when IPV6_ADDRFORM is set to AF_INET.
10*49cdfc7eSAndroid Build Coastguard Worker  * Kernel memory corruption fixed in:
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  *  commit 9d538fa60bad4f7b23193c89e843797a1cf71ef3
13*49cdfc7eSAndroid Build Coastguard Worker  *  Author: Christoph Paasch <[email protected]>
14*49cdfc7eSAndroid Build Coastguard Worker  *  Date:   Tue Sep 26 17:38:50 2017 -0700
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  *  net: Set sk_prot_creator when cloning sockets to the right proto
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * Note: This test also detects setsockopt(IP_ADDRFORM) breakage caused by
20*49cdfc7eSAndroid Build Coastguard Worker  * kernel commit b6f6118901d1. This bug is unrelated to CVE-2018-9568.
21*49cdfc7eSAndroid Build Coastguard Worker  * Fixed in:
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  *  commit 82c9ae440857840c56e05d4fb1427ee032531346
24*49cdfc7eSAndroid Build Coastguard Worker  *  Author: John Haxby <[email protected]>
25*49cdfc7eSAndroid Build Coastguard Worker  *  Date:   Sat Apr 18 16:30:49 2020 +0100
26*49cdfc7eSAndroid Build Coastguard Worker  *
27*49cdfc7eSAndroid Build Coastguard Worker  *  ipv6: fix restrict IPV6_ADDRFORM operation
28*49cdfc7eSAndroid Build Coastguard Worker  */
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/tcp.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <arpa/inet.h>
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
37*49cdfc7eSAndroid Build Coastguard Worker #include "tst_net.h"
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker static int listenfd = -1, fd = -1, confd1 = -1, confd2 = -1, confd3 = -1;
40*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in6 bind_addr;
41*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in bind_addr4, client_addr;
42*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr reset_addr;
43*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)44*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t size = sizeof(bind_addr);
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	tst_init_sockaddr_inet6_bin(&bind_addr, &in6addr_any, 0);
49*49cdfc7eSAndroid Build Coastguard Worker 	tst_init_sockaddr_inet_bin(&bind_addr4, INADDR_ANY, 0);
50*49cdfc7eSAndroid Build Coastguard Worker 	memset(&reset_addr, 0, sizeof(reset_addr));
51*49cdfc7eSAndroid Build Coastguard Worker 	reset_addr.sa_family = AF_UNSPEC;
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	listenfd = SAFE_SOCKET(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
54*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(listenfd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
55*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LISTEN(listenfd, 5);
56*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETSOCKNAME(listenfd, (struct sockaddr *)&bind_addr, &size);
57*49cdfc7eSAndroid Build Coastguard Worker 	tst_init_sockaddr_inet(&client_addr, "127.0.0.1",
58*49cdfc7eSAndroid Build Coastguard Worker 		htons(bind_addr.sin6_port));
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)61*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker 	if (confd3 >= 0)
64*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(confd3);
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	if (confd2 >= 0)
67*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(confd2);
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	if (confd1 >= 0)
70*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(confd1);
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	if (fd >= 0)
73*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	if (listenfd >= 0)
76*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(listenfd);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker 
run(void)79*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
80*49cdfc7eSAndroid Build Coastguard Worker {
81*49cdfc7eSAndroid Build Coastguard Worker 	int i, addrlen, optval = AF_INET;
82*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_storage client_addr2;
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 1000; i++) {
85*49cdfc7eSAndroid Build Coastguard Worker 		confd1 = SAFE_SOCKET(AF_INET, SOCK_STREAM, IPPROTO_TCP);
86*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CONNECT(confd1, (struct sockaddr *)&client_addr,
87*49cdfc7eSAndroid Build Coastguard Worker 			sizeof(client_addr));
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 		fd = SAFE_ACCEPT(listenfd, NULL, NULL);
90*49cdfc7eSAndroid Build Coastguard Worker 		TEST(setsockopt(fd, SOL_IPV6, IPV6_ADDRFORM, &optval,
91*49cdfc7eSAndroid Build Coastguard Worker 			sizeof(optval)));
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_RET == -1) {
94*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL | TTERRNO,
95*49cdfc7eSAndroid Build Coastguard Worker 				"setsockopt(IPV6_ADDRFORM) failed");
96*49cdfc7eSAndroid Build Coastguard Worker 			return;
97*49cdfc7eSAndroid Build Coastguard Worker 		}
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_RET != 0)
100*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK | TTERRNO, "setsockopt(IPV6_ADDRFORM) "
101*49cdfc7eSAndroid Build Coastguard Worker 				"returned invalid value");
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CONNECT(fd, (struct sockaddr *)&reset_addr,
104*49cdfc7eSAndroid Build Coastguard Worker 			sizeof(reset_addr));
105*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_BIND(fd, (struct sockaddr *)&bind_addr4,
106*49cdfc7eSAndroid Build Coastguard Worker 			sizeof(bind_addr4));
107*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_LISTEN(fd, 5);
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 		addrlen = tst_get_connect_address(fd, &client_addr2);
110*49cdfc7eSAndroid Build Coastguard Worker 		confd2 = SAFE_SOCKET(AF_INET, SOCK_STREAM, IPPROTO_TCP);
111*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CONNECT(confd2, (struct sockaddr *)&client_addr2, addrlen);
112*49cdfc7eSAndroid Build Coastguard Worker 		confd3 = SAFE_ACCEPT(fd, NULL, NULL);
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(confd3);
115*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(confd2);
116*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(confd1);
117*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 		if (tst_taint_check()) {
120*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "Kernel is vulnerable");
121*49cdfc7eSAndroid Build Coastguard Worker 			return;
122*49cdfc7eSAndroid Build Coastguard Worker 		}
123*49cdfc7eSAndroid Build Coastguard Worker 	}
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Nothing bad happened, probably");
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
129*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
130*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
131*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
132*49cdfc7eSAndroid Build Coastguard Worker 	.taint_check = TST_TAINT_W | TST_TAINT_D,
133*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
134*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "9d538fa60bad"},
135*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "82c9ae440857"},
136*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2018-9568"},
137*49cdfc7eSAndroid Build Coastguard Worker 		{}
138*49cdfc7eSAndroid Build Coastguard Worker 	}
139*49cdfc7eSAndroid Build Coastguard Worker };
140