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