xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setsockopt/setsockopt08.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) 2021 SUSE LLC <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  * Based on reproducer by Nicolai Stange based on PoC Andy Nguyen
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  * This will reproduce the bug on x86_64 in 32bit compatibility
10*49cdfc7eSAndroid Build Coastguard Worker  * mode. It is most reliable with KASAN enabled. Otherwise it relies
11*49cdfc7eSAndroid Build Coastguard Worker  * on the out-of-bounds write corrupting something which leads to a
12*49cdfc7eSAndroid Build Coastguard Worker  * crash. It will run in other scenarious, but is not a test for the
13*49cdfc7eSAndroid Build Coastguard Worker  * CVE.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * See https://google.github.io/security-research/pocs/linux/cve-2021-22555/writeup.html
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  * Also below is Nicolai's detailed description of the bug itself.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * The problem underlying CVE-2021-22555 fixed by upstream commit
20*49cdfc7eSAndroid Build Coastguard Worker  * b29c457a6511 ("netfilter: x_tables: fix compat match/target pad
21*49cdfc7eSAndroid Build Coastguard Worker  * out-of-bound write") is that the (now removed) padding zeroing code
22*49cdfc7eSAndroid Build Coastguard Worker  * in xt_compat_target_from_user() had been based on the premise that
23*49cdfc7eSAndroid Build Coastguard Worker  * the user specified ->u.user.target_size, which will be considered
24*49cdfc7eSAndroid Build Coastguard Worker  * for the target buffer allocation size, is greater or equal than
25*49cdfc7eSAndroid Build Coastguard Worker  * what's needed to fit the corresponding xt_target instance's
26*49cdfc7eSAndroid Build Coastguard Worker  * ->targetsize: if OTOH the user specified ->u.user.target_size is
27*49cdfc7eSAndroid Build Coastguard Worker  * too small, then the memset() destination address calculated by
28*49cdfc7eSAndroid Build Coastguard Worker  * adding ->targetsize to the payload start will not point at, but
29*49cdfc7eSAndroid Build Coastguard Worker  * into or even past the padding.
30*49cdfc7eSAndroid Build Coastguard Worker  *
31*49cdfc7eSAndroid Build Coastguard Worker  * For the table's last entry's target record, this will result in an
32*49cdfc7eSAndroid Build Coastguard Worker  * out-of-bounds write past the destination buffer allocated for the converted
33*49cdfc7eSAndroid Build Coastguard Worker  * table. The code below will create a (compat) table such that the converted
34*49cdfc7eSAndroid Build Coastguard Worker  * table's calculated size will fit exactly into a slab size of 1024 bytes and
35*49cdfc7eSAndroid Build Coastguard Worker  * that the memset() in xt_compat_target_from_user() will write past this slab.
36*49cdfc7eSAndroid Build Coastguard Worker  *
37*49cdfc7eSAndroid Build Coastguard Worker  * The table will consist of
38*49cdfc7eSAndroid Build Coastguard Worker  *
39*49cdfc7eSAndroid Build Coastguard Worker  *  * the mandatory struct compat_ipt_replace header,
40*49cdfc7eSAndroid Build Coastguard Worker  *  * a single entry consisting of
41*49cdfc7eSAndroid Build Coastguard Worker  *    ** the mandatory compat_ipt_entry header
42*49cdfc7eSAndroid Build Coastguard Worker  *    ** a single 'state' match entry of appropriate size for
43*49cdfc7eSAndroid Build Coastguard Worker  *      controlling the out-of-bounds write when converting
44*49cdfc7eSAndroid Build Coastguard Worker  *      the target entry following next,
45*49cdfc7eSAndroid Build Coastguard Worker  *    ** a single 'REJECT' target entry.
46*49cdfc7eSAndroid Build Coastguard Worker  *
47*49cdfc7eSAndroid Build Coastguard Worker  * The kernel will transform this into a buffer containing (in
48*49cdfc7eSAndroid Build Coastguard Worker  * this order)
49*49cdfc7eSAndroid Build Coastguard Worker  *
50*49cdfc7eSAndroid Build Coastguard Worker  * * a xt_table_info
51*49cdfc7eSAndroid Build Coastguard Worker  * * a single entry consisting of
52*49cdfc7eSAndroid Build Coastguard Worker  *   ** its ipt_entry header
53*49cdfc7eSAndroid Build Coastguard Worker  *   ** a single 'state' match entry
54*49cdfc7eSAndroid Build Coastguard Worker  *   ** followed by a single 'REJECT' target entry.
55*49cdfc7eSAndroid Build Coastguard Worker  *
56*49cdfc7eSAndroid Build Coastguard Worker  * The expected sizes for the 'state' match entries as well as the
57*49cdfc7eSAndroid Build Coastguard Worker  * 'REJECT' target are the size of the base header struct (32 bytes)
58*49cdfc7eSAndroid Build Coastguard Worker  * plus the size of an unsigned int (4 bytes) each.
59*49cdfc7eSAndroid Build Coastguard Worker  *
60*49cdfc7eSAndroid Build Coastguard Worker  * In the course of the compat => non-compat conversion, the kernel will insert
61*49cdfc7eSAndroid Build Coastguard Worker  * four bytes of padding after the unsigned int payload (c.f. 'off' adjustments
62*49cdfc7eSAndroid Build Coastguard Worker  * via xt_compat_match_offset() and xt_compat_target_offset() in
63*49cdfc7eSAndroid Build Coastguard Worker  * xt_compat_match_from_user() and xt_compat_target_from_user() resp.).
64*49cdfc7eSAndroid Build Coastguard Worker  *
65*49cdfc7eSAndroid Build Coastguard Worker  * This code is based on the premise that the user sets the given
66*49cdfc7eSAndroid Build Coastguard Worker  * ->u.user.match_size or ->u.user.target_size consistent to the
67*49cdfc7eSAndroid Build Coastguard Worker  * COMPAT_XT_ALIGN()ed payload size as specified by the corresponding xt_match
68*49cdfc7eSAndroid Build Coastguard Worker  * instance's ->matchsize or xt_target instance's ->targetsize.
69*49cdfc7eSAndroid Build Coastguard Worker  *
70*49cdfc7eSAndroid Build Coastguard Worker  * That is, the padding gets inserted unconditionally during the transformation,
71*49cdfc7eSAndroid Build Coastguard Worker  * independent of the actual values of ->u.user.match_size or
72*49cdfc7eSAndroid Build Coastguard Worker  * ->u.user.target_size and the result ends up getting layed out with proper
73*49cdfc7eSAndroid Build Coastguard Worker  *  alignment only if said values match the expectations.
74*49cdfc7eSAndroid Build Coastguard Worker  *
75*49cdfc7eSAndroid Build Coastguard Worker  * That's not a problem in itself, but this unconditional insertion of padding
76*49cdfc7eSAndroid Build Coastguard Worker  * must be taken into account in the match_size calculation below.
77*49cdfc7eSAndroid Build Coastguard Worker  *
78*49cdfc7eSAndroid Build Coastguard Worker  * For the match_size calculation below, note that the chosen
79*49cdfc7eSAndroid Build Coastguard Worker  * target slab size is 1024 and that
80*49cdfc7eSAndroid Build Coastguard Worker  *
81*49cdfc7eSAndroid Build Coastguard Worker  *  * sizeof(xt_table_info) = 64
82*49cdfc7eSAndroid Build Coastguard Worker  *  * sizeof(ipt_entry) = 112
83*49cdfc7eSAndroid Build Coastguard Worker  *  * the kernel will insert four bytes of padding
84*49cdfc7eSAndroid Build Coastguard Worker  *    after the match and target entries each.
85*49cdfc7eSAndroid Build Coastguard Worker  *  * sizeof(struct xt_entry_target) = 32
86*49cdfc7eSAndroid Build Coastguard Worker  */
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
91*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_net.h"
92*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/ip_tables.h"
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker static void *buffer;
95*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)96*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
97*49cdfc7eSAndroid Build Coastguard Worker {
98*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_kernel_bits() == 32 || sizeof(long) > 4) {
99*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO,
100*49cdfc7eSAndroid Build Coastguard Worker 			"The vulnerability was only present in 32-bit compat mode");
101*49cdfc7eSAndroid Build Coastguard Worker 	}
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	tst_setup_netns();
104*49cdfc7eSAndroid Build Coastguard Worker }
105*49cdfc7eSAndroid Build Coastguard Worker 
run(void)106*49cdfc7eSAndroid Build Coastguard Worker void run(void)
107*49cdfc7eSAndroid Build Coastguard Worker {
108*49cdfc7eSAndroid Build Coastguard Worker 	const char *const res_fmt_str =
109*49cdfc7eSAndroid Build Coastguard Worker 		"setsockopt(%d, IPPROTO_IP, IPT_SO_SET_REPLACE, %p, 1)";
110*49cdfc7eSAndroid Build Coastguard Worker 	struct ipt_replace *ipt_replace = buffer;
111*49cdfc7eSAndroid Build Coastguard Worker 	struct ipt_entry *ipt_entry = &ipt_replace->entries[0];
112*49cdfc7eSAndroid Build Coastguard Worker 	struct xt_entry_match *xt_entry_match =
113*49cdfc7eSAndroid Build Coastguard Worker 		(struct xt_entry_match *)&ipt_entry->elems[0];
114*49cdfc7eSAndroid Build Coastguard Worker 	const size_t tgt_size = 32;
115*49cdfc7eSAndroid Build Coastguard Worker 	const size_t match_size = 1024 - 64 - 112 - 4 - tgt_size - 4;
116*49cdfc7eSAndroid Build Coastguard Worker 	struct xt_entry_target *xt_entry_tgt =
117*49cdfc7eSAndroid Build Coastguard Worker 		((struct xt_entry_target *) (&ipt_entry->elems[0] + match_size));
118*49cdfc7eSAndroid Build Coastguard Worker 	int fd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
119*49cdfc7eSAndroid Build Coastguard Worker 	int result;
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 	xt_entry_match->u.user.match_size = (u_int16_t)match_size;
122*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(xt_entry_match->u.user.name, "state");
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	xt_entry_tgt->u.user.target_size = (u_int16_t)tgt_size;
125*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(xt_entry_tgt->u.user.name, "REJECT");
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker 	ipt_entry->target_offset =
128*49cdfc7eSAndroid Build Coastguard Worker 		(__builtin_offsetof(struct ipt_entry, elems) + match_size);
129*49cdfc7eSAndroid Build Coastguard Worker 	ipt_entry->next_offset = ipt_entry->target_offset + tgt_size;
130*49cdfc7eSAndroid Build Coastguard Worker 
131*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(ipt_replace->name, "filter");
132*49cdfc7eSAndroid Build Coastguard Worker 	ipt_replace->num_entries = 1;
133*49cdfc7eSAndroid Build Coastguard Worker 	ipt_replace->num_counters = 1;
134*49cdfc7eSAndroid Build Coastguard Worker 	ipt_replace->size = ipt_entry->next_offset;
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 	TEST(setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, buffer, 1));
137*49cdfc7eSAndroid Build Coastguard Worker 
138*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1 && TST_ERR == ENOPROTOOPT)
139*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF | TTERRNO, res_fmt_str, fd, buffer);
140*49cdfc7eSAndroid Build Coastguard Worker 
141*49cdfc7eSAndroid Build Coastguard Worker 	result = (TST_RET == -1 && TST_ERR == EINVAL) ? TPASS : TFAIL;
142*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(result | TTERRNO, res_fmt_str, fd, buffer);
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
145*49cdfc7eSAndroid Build Coastguard Worker }
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
148*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
149*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
150*49cdfc7eSAndroid Build Coastguard Worker 	.taint_check = TST_TAINT_W | TST_TAINT_D,
151*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
152*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers []) {
153*49cdfc7eSAndroid Build Coastguard Worker 		{&buffer, .size = 2048},
154*49cdfc7eSAndroid Build Coastguard Worker 		{},
155*49cdfc7eSAndroid Build Coastguard Worker 	},
156*49cdfc7eSAndroid Build Coastguard Worker 	.needs_kconfigs = (const char *[]) {
157*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_NETFILTER_XT_MATCH_STATE",
158*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_IP_NF_TARGET_REJECT",
159*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_USER_NS=y",
160*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_NET_NS=y",
161*49cdfc7eSAndroid Build Coastguard Worker 		NULL
162*49cdfc7eSAndroid Build Coastguard Worker 	},
163*49cdfc7eSAndroid Build Coastguard Worker 	.save_restore = (const struct tst_path_val[]) {
164*49cdfc7eSAndroid Build Coastguard Worker 		{"/proc/sys/user/max_user_namespaces", "1024", TST_SR_SKIP},
165*49cdfc7eSAndroid Build Coastguard Worker 		{}
166*49cdfc7eSAndroid Build Coastguard Worker 	},
167*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
168*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "b29c457a6511"},
169*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2021-22555"},
170*49cdfc7eSAndroid Build Coastguard Worker 		{}
171*49cdfc7eSAndroid Build Coastguard Worker 	}
172*49cdfc7eSAndroid Build Coastguard Worker };
173