xref: /aosp_15_r20/external/ltp/testcases/cve/cve-2017-2618.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 FUJITSU LIMITED
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Guangwen Feng <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * Test for CVE-2017-2618, this regression test can crash
9*49cdfc7eSAndroid Build Coastguard Worker  * the buggy kernel, and the bug was fixed in:
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  *  commit 0c461cb727d146c9ef2d3e86214f498b78b7d125
12*49cdfc7eSAndroid Build Coastguard Worker  *  Author: Stephen Smalley <[email protected]>
13*49cdfc7eSAndroid Build Coastguard Worker  *  Date:   Tue Jan 31 11:54:04 2017 -0500
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *  selinux: fix off-by-one in setprocattr
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #define LOOPS	100
24*49cdfc7eSAndroid Build Coastguard Worker #define PATH_ATTRFS	"/proc/self/attr/fscreate"
25*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)26*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
27*49cdfc7eSAndroid Build Coastguard Worker {
28*49cdfc7eSAndroid Build Coastguard Worker 	if (access(PATH_ATTRFS, F_OK))
29*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "%s does not exist", PATH_ATTRFS);
30*49cdfc7eSAndroid Build Coastguard Worker }
31*49cdfc7eSAndroid Build Coastguard Worker 
do_test(void)32*49cdfc7eSAndroid Build Coastguard Worker static void do_test(void)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker 	int i, fd;
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < LOOPS; i++) {
37*49cdfc7eSAndroid Build Coastguard Worker 		if (!SAFE_FORK()) {
38*49cdfc7eSAndroid Build Coastguard Worker 			fd = SAFE_OPEN(PATH_ATTRFS, O_WRONLY);
39*49cdfc7eSAndroid Build Coastguard Worker 			write(fd, "\n", 1);
40*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_CLOSE(fd);
41*49cdfc7eSAndroid Build Coastguard Worker 			exit(0);
42*49cdfc7eSAndroid Build Coastguard Worker 		}
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_reap_children();
45*49cdfc7eSAndroid Build Coastguard Worker 	}
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Bug not reproduced");
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
51*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
52*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
53*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = do_test,
54*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
55*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "0c461cb727d1"},
56*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2017-2618"},
57*49cdfc7eSAndroid Build Coastguard Worker 		{}
58*49cdfc7eSAndroid Build Coastguard Worker 	}
59*49cdfc7eSAndroid Build Coastguard Worker };
60