xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setsockopt/setsockopt04.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 /* Test for CVE-2016-9793
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * With kernels between version 3.11 and 4.8 missing commit b98b0bc8 it
9*49cdfc7eSAndroid Build Coastguard Worker  * is possible to pass a very high unsigned integer as send buffer size
10*49cdfc7eSAndroid Build Coastguard Worker  * to a socket which is then interpreted as a negative value.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * This can be used to escalate privileges by every user that has the
13*49cdfc7eSAndroid Build Coastguard Worker  * CAP_NET_ADMIN capability.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * For additional information about this CVE see:
16*49cdfc7eSAndroid Build Coastguard Worker  * https://www.suse.com/security/cve/CVE-2016-9793/
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_net.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #define SNDBUF	(0xffffff00)
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static int sockfd;
26*49cdfc7eSAndroid Build Coastguard Worker 
run(void)27*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int sndbuf, rec_sndbuf;
30*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t optlen;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	sndbuf = SNDBUF;
33*49cdfc7eSAndroid Build Coastguard Worker 	rec_sndbuf = 0;
34*49cdfc7eSAndroid Build Coastguard Worker 	optlen = sizeof(sndbuf);
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETSOCKOPT(sockfd, SOL_SOCKET, SO_SNDBUFFORCE, &sndbuf, optlen);
37*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETSOCKOPT(sockfd, SOL_SOCKET, SO_SNDBUF, &rec_sndbuf, &optlen);
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Try to set send buffer size to: %u", sndbuf);
40*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Send buffer size was set to: %d", rec_sndbuf);
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	if ((int)rec_sndbuf < 0)
43*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Was able to set negative send buffer size!");
44*49cdfc7eSAndroid Build Coastguard Worker 	else
45*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Was unable to set negative send buffer size!");
46*49cdfc7eSAndroid Build Coastguard Worker }
47*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)48*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
49*49cdfc7eSAndroid Build Coastguard Worker {
50*49cdfc7eSAndroid Build Coastguard Worker 	sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)53*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
54*49cdfc7eSAndroid Build Coastguard Worker {
55*49cdfc7eSAndroid Build Coastguard Worker 	if (sockfd > 0)
56*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sockfd);
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
60*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
61*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
62*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
63*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
64*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
65*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "b98b0bc8c431"},
66*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2016-9793"},
67*49cdfc7eSAndroid Build Coastguard Worker 		{}
68*49cdfc7eSAndroid Build Coastguard Worker 	}
69*49cdfc7eSAndroid Build Coastguard Worker };
70