xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/vma/vma03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2012 Red Hat, Inc.
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or
5*49cdfc7eSAndroid Build Coastguard Worker  * modify it under the terms of version 2 of the GNU General Public
6*49cdfc7eSAndroid Build Coastguard Worker  * License as published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful,
9*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Further, this software is distributed without any warranty that it
13*49cdfc7eSAndroid Build Coastguard Worker  * is free of the rightful claim of any third person regarding
14*49cdfc7eSAndroid Build Coastguard Worker  * infringement or the like.  Any license provided herein, whether
15*49cdfc7eSAndroid Build Coastguard Worker  * implied or otherwise, applies only to this software file.  Patent
16*49cdfc7eSAndroid Build Coastguard Worker  * licenses, if any, provided herein do not apply to combinations of
17*49cdfc7eSAndroid Build Coastguard Worker  * this program with other software, or any other product whatsoever.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
20*49cdfc7eSAndroid Build Coastguard Worker  * along with this program; if not, write the Free Software
21*49cdfc7eSAndroid Build Coastguard Worker  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22*49cdfc7eSAndroid Build Coastguard Worker  * 02110-1301, USA.
23*49cdfc7eSAndroid Build Coastguard Worker  */
24*49cdfc7eSAndroid Build Coastguard Worker /*
25*49cdfc7eSAndroid Build Coastguard Worker  * This is a reproducer for CVE-2011-2496.
26*49cdfc7eSAndroid Build Coastguard Worker  *
27*49cdfc7eSAndroid Build Coastguard Worker  * The normal mmap paths all avoid creating a mapping where the pgoff
28*49cdfc7eSAndroid Build Coastguard Worker  * inside the mapping could wrap around due to overflow.  However, an
29*49cdfc7eSAndroid Build Coastguard Worker  * expanding mremap() can take such a non-wrapping mapping and make it
30*49cdfc7eSAndroid Build Coastguard Worker  * bigger and cause a wrapping condition. There is also another case
31*49cdfc7eSAndroid Build Coastguard Worker  * where we expand mappings hiding in plain sight: the automatic stack
32*49cdfc7eSAndroid Build Coastguard Worker  * expansion.
33*49cdfc7eSAndroid Build Coastguard Worker  *
34*49cdfc7eSAndroid Build Coastguard Worker  * This program tries to remap a mapping with a new size that would
35*49cdfc7eSAndroid Build Coastguard Worker  * wrap pgoff. Notice that it only works on 32-bit arch for now.
36*49cdfc7eSAndroid Build Coastguard Worker  */
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
39*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
40*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
52*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
53*49cdfc7eSAndroid Build Coastguard Worker #include "tst_kernel.h"
54*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/abisize.h"
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "vma03";
57*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker #ifdef __NR_mmap2
60*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE "testfile"
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker static size_t pgsz;
63*49cdfc7eSAndroid Build Coastguard Worker static int fd;
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker static void *mmap2(void *addr, size_t length, int prot,
66*49cdfc7eSAndroid Build Coastguard Worker 		   int flags, int fd, off_t pgoffset);
67*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
68*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
69*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])70*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
73*49cdfc7eSAndroid Build Coastguard Worker 	void *map, *remap;
74*49cdfc7eSAndroid Build Coastguard Worker 	off_t pgoff;
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ABI != 32 || tst_kernel_bits() != 32) {
77*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TCONF, NULL,
78*49cdfc7eSAndroid Build Coastguard Worker 			 "test is designed for 32-bit system only.");
79*49cdfc7eSAndroid Build Coastguard Worker 	}
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(argc, argv, NULL, NULL);
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	pgsz = sysconf(_SC_PAGE_SIZE);
84*49cdfc7eSAndroid Build Coastguard Worker 	setup();
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
87*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 		fd = SAFE_OPEN(NULL, TESTFILE, O_RDWR);
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 		/*
92*49cdfc7eSAndroid Build Coastguard Worker 		 * The pgoff is counted in 4K units and must be page-aligned,
93*49cdfc7eSAndroid Build Coastguard Worker 		 * hence we must align it down to page_size/4096 in a case that
94*49cdfc7eSAndroid Build Coastguard Worker 		 * the system has page_size > 4K.
95*49cdfc7eSAndroid Build Coastguard Worker 		 */
96*49cdfc7eSAndroid Build Coastguard Worker 		pgoff = (ULONG_MAX - 1)&(~((pgsz-1)>>12));
97*49cdfc7eSAndroid Build Coastguard Worker 		map = mmap2(NULL, pgsz, PROT_READ | PROT_WRITE, MAP_PRIVATE,
98*49cdfc7eSAndroid Build Coastguard Worker 			    fd, pgoff);
99*49cdfc7eSAndroid Build Coastguard Worker 		if (map == MAP_FAILED)
100*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup, "mmap2");
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 		remap = mremap(map, pgsz, 2 * pgsz, 0);
103*49cdfc7eSAndroid Build Coastguard Worker 		if (remap == MAP_FAILED) {
104*49cdfc7eSAndroid Build Coastguard Worker 			if (errno == EINVAL)
105*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS, "mremap failed as expected.");
106*49cdfc7eSAndroid Build Coastguard Worker 			else
107*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL | TERRNO, "mremap");
108*49cdfc7eSAndroid Build Coastguard Worker 			munmap(map, pgsz);
109*49cdfc7eSAndroid Build Coastguard Worker 		} else {
110*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "mremap succeeded unexpectedly.");
111*49cdfc7eSAndroid Build Coastguard Worker 			munmap(remap, 2 * pgsz);
112*49cdfc7eSAndroid Build Coastguard Worker 		}
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 		close(fd);
115*49cdfc7eSAndroid Build Coastguard Worker 	}
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
118*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker 
mmap2(void * addr,size_t length,int prot,int flags,int fd,off_t pgoffset)121*49cdfc7eSAndroid Build Coastguard Worker static void *mmap2(void *addr, size_t length, int prot,
122*49cdfc7eSAndroid Build Coastguard Worker 		   int flags, int fd, off_t pgoffset)
123*49cdfc7eSAndroid Build Coastguard Worker {
124*49cdfc7eSAndroid Build Coastguard Worker 	return (void *)syscall(SYS_mmap2, addr, length, prot,
125*49cdfc7eSAndroid Build Coastguard Worker 			       flags, fd, pgoffset);
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)128*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
129*49cdfc7eSAndroid Build Coastguard Worker {
130*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_CREAT(NULL, TESTFILE, 0644);
135*49cdfc7eSAndroid Build Coastguard Worker 	close(fd);
136*49cdfc7eSAndroid Build Coastguard Worker 
137*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
138*49cdfc7eSAndroid Build Coastguard Worker }
139*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)140*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
141*49cdfc7eSAndroid Build Coastguard Worker {
142*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
143*49cdfc7eSAndroid Build Coastguard Worker }
144*49cdfc7eSAndroid Build Coastguard Worker #else /* __NR_mmap2 */
main(int argc,char * argv[])145*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
146*49cdfc7eSAndroid Build Coastguard Worker {
147*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TCONF, NULL, "__NR_mmap2 is not defined on your system");
148*49cdfc7eSAndroid Build Coastguard Worker }
149*49cdfc7eSAndroid Build Coastguard Worker #endif
150