xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/accept/accept02.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
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Christian Amann <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Test for CVE-2017-8890
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * In Kernels up to 4.10.15 missing commit 657831ff the multicast
12*49cdfc7eSAndroid Build Coastguard Worker  * group information of a socket gets copied over to a newly created
13*49cdfc7eSAndroid Build Coastguard Worker  * socket when using the accept() syscall. This will cause a double free
14*49cdfc7eSAndroid Build Coastguard Worker  * when closing the original and the cloned socket.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * WARNING:
17*49cdfc7eSAndroid Build Coastguard Worker  * There is a high chance that this test will cause an unstable system
18*49cdfc7eSAndroid Build Coastguard Worker  * if it does not succeed!
19*49cdfc7eSAndroid Build Coastguard Worker  *
20*49cdfc7eSAndroid Build Coastguard Worker  * For more information about this CVE see:
21*49cdfc7eSAndroid Build Coastguard Worker  * https://www.suse.com/security/cve/CVE-2017-8890/
22*49cdfc7eSAndroid Build Coastguard Worker  */
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_net.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_pthread.h"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #define MULTICASTIP "224.0.0.0"
31*49cdfc7eSAndroid Build Coastguard Worker #define LOCALHOSTIP "127.0.0.1"
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker static int server_sockfd;
34*49cdfc7eSAndroid Build Coastguard Worker static int clone_server_sockfd;
35*49cdfc7eSAndroid Build Coastguard Worker static int client_sockfd;
36*49cdfc7eSAndroid Build Coastguard Worker static int server_port;
37*49cdfc7eSAndroid Build Coastguard Worker static socklen_t addr_len;
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in *server_addr;
40*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in *client_addr;
41*49cdfc7eSAndroid Build Coastguard Worker static struct group_req *mc_group;
42*49cdfc7eSAndroid Build Coastguard Worker 
server_thread(void * arg)43*49cdfc7eSAndroid Build Coastguard Worker static void *server_thread(void *arg)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker 	int op, op_len, mc_group_len;
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	op = 1;
48*49cdfc7eSAndroid Build Coastguard Worker 	op_len = sizeof(op);
49*49cdfc7eSAndroid Build Coastguard Worker 	mc_group_len = sizeof(*mc_group);
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	server_sockfd = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETSOCKOPT(server_sockfd, SOL_SOCKET, SO_REUSEADDR, &op, op_len);
54*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETSOCKOPT(server_sockfd, SOL_IP, MCAST_JOIN_GROUP,
55*49cdfc7eSAndroid Build Coastguard Worker 			mc_group, mc_group_len);
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(server_sockfd, (struct sockaddr *)server_addr, addr_len);
58*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LISTEN(server_sockfd, 1);
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE(0);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	TEST(accept(server_sockfd, (struct sockaddr *)client_addr, &addr_len));
63*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
64*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "Could not accept connection");
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	clone_server_sockfd = TST_RET;
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	TEST(setsockopt(clone_server_sockfd, SOL_IP, MCAST_LEAVE_GROUP,
69*49cdfc7eSAndroid Build Coastguard Worker 			mc_group, mc_group_len));
70*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(clone_server_sockfd);
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1)
73*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Multicast group was copied!");
74*49cdfc7eSAndroid Build Coastguard Worker 	else if (TST_ERR == EADDRNOTAVAIL)
75*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "Multicast group was not copied");
76*49cdfc7eSAndroid Build Coastguard Worker 	else
77*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "setsockopt() failed unexpectedly");
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(server_sockfd);
80*49cdfc7eSAndroid Build Coastguard Worker 	return arg;
81*49cdfc7eSAndroid Build Coastguard Worker }
82*49cdfc7eSAndroid Build Coastguard Worker 
client_thread(void * arg)83*49cdfc7eSAndroid Build Coastguard Worker static void *client_thread(void *arg)
84*49cdfc7eSAndroid Build Coastguard Worker {
85*49cdfc7eSAndroid Build Coastguard Worker 	client_sockfd = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
86*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(client_sockfd, (struct sockaddr *)client_addr, addr_len);
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CONNECT(client_sockfd, (struct sockaddr *)server_addr, addr_len);
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(client_sockfd);
91*49cdfc7eSAndroid Build Coastguard Worker 	return arg;
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker 
run(void)94*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
95*49cdfc7eSAndroid Build Coastguard Worker {
96*49cdfc7eSAndroid Build Coastguard Worker 	pthread_t server_thr, client_thr;
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	server_addr->sin_port = server_port;
99*49cdfc7eSAndroid Build Coastguard Worker 	client_addr->sin_port = htons(0);
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_CREATE(&server_thr, NULL, server_thread, NULL);
102*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAIT(0);
103*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_CREATE(&client_thr, NULL, client_thread, NULL);
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_JOIN(server_thr, NULL);
106*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_JOIN(client_thr, NULL);
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)109*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
110*49cdfc7eSAndroid Build Coastguard Worker {
111*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in *mc_group_addr;
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	server_addr = tst_alloc(sizeof(*server_addr));
114*49cdfc7eSAndroid Build Coastguard Worker 	client_addr = tst_alloc(sizeof(*client_addr));
115*49cdfc7eSAndroid Build Coastguard Worker 	mc_group = tst_alloc(sizeof(*mc_group));
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	mc_group->gr_interface = 0;
118*49cdfc7eSAndroid Build Coastguard Worker 	mc_group_addr = (struct sockaddr_in *) &mc_group->gr_group;
119*49cdfc7eSAndroid Build Coastguard Worker 	mc_group_addr->sin_family = AF_INET;
120*49cdfc7eSAndroid Build Coastguard Worker 	inet_aton(MULTICASTIP, &mc_group_addr->sin_addr);
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker 	server_addr->sin_family = AF_INET;
123*49cdfc7eSAndroid Build Coastguard Worker 	inet_aton(LOCALHOSTIP, &server_addr->sin_addr);
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker 	client_addr->sin_family = AF_INET;
126*49cdfc7eSAndroid Build Coastguard Worker 	client_addr->sin_addr.s_addr = htons(INADDR_ANY);
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker 	addr_len = sizeof(struct sockaddr_in);
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 	server_port = TST_GET_UNUSED_PORT(AF_INET, SOCK_STREAM);
131*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Starting listener on port: %d", ntohs(server_port));
132*49cdfc7eSAndroid Build Coastguard Worker }
133*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)134*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
135*49cdfc7eSAndroid Build Coastguard Worker {
136*49cdfc7eSAndroid Build Coastguard Worker 	if (clone_server_sockfd > 0)
137*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(clone_server_sockfd);
138*49cdfc7eSAndroid Build Coastguard Worker 	if (client_sockfd > 0)
139*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(client_sockfd);
140*49cdfc7eSAndroid Build Coastguard Worker 	if (server_sockfd > 0)
141*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(server_sockfd);
142*49cdfc7eSAndroid Build Coastguard Worker }
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
145*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
146*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
147*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
148*49cdfc7eSAndroid Build Coastguard Worker 	.needs_checkpoints = 1,
149*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
150*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2017-8890"},
151*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "657831ff"},
152*49cdfc7eSAndroid Build Coastguard Worker 		{},
153*49cdfc7eSAndroid Build Coastguard Worker 	}
154*49cdfc7eSAndroid Build Coastguard Worker };
155