|
0 |
Description: Avoid SIGBUS on mmap failure
|
|
1 |
mmap will fail with SIGBUS if trying to map a non-existent portion of
|
|
2 |
a file. While this should never happen with /dev/mem, it can happen if
|
|
3 |
passing a regular file with option -d. While people should no longer
|
|
4 |
do that, failure gracefully seems better than crashing. So check for
|
|
5 |
the file size before calling mmap.
|
|
6 |
Author: Jean Delvare <jdelvare@suse.de>
|
|
7 |
Origin: https://savannah.nongnu.org/bugs/download.php?file_id=35008
|
|
8 |
Bug: https://savannah.nongnu.org/bugs/index.php?46066
|
|
9 |
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=796963
|
|
10 |
Last-Update: 2015-10-01
|
|
11 |
----
|
|
12 |
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
|
|
13 |
--- dmidecode.orig/util.c 2015-09-29 11:27:02.136566009 +0200
|
|
14 |
+++ dmidecode/util.c 2015-09-29 11:37:24.746191083 +0200
|
|
15 |
@@ -152,6 +152,7 @@ void *mem_chunk(off_t base, size_t len,
|
|
16 |
void *p;
|
|
17 |
int fd;
|
|
18 |
#ifdef USE_MMAP
|
|
19 |
+ struct stat statbuf;
|
|
20 |
off_t mmoffset;
|
|
21 |
void *mmp;
|
|
22 |
#endif
|
|
23 |
@@ -169,6 +170,26 @@ void *mem_chunk(off_t base, size_t len,
|
|
24 |
}
|
|
25 |
|
|
26 |
#ifdef USE_MMAP
|
|
27 |
+ if (fstat(fd, &statbuf) == -1)
|
|
28 |
+ {
|
|
29 |
+ fprintf(stderr, "%s: ", devmem);
|
|
30 |
+ perror("stat");
|
|
31 |
+ free(p);
|
|
32 |
+ return NULL;
|
|
33 |
+ }
|
|
34 |
+
|
|
35 |
+ /*
|
|
36 |
+ * mmap() will fail with SIGBUS if trying to map beyond the end of
|
|
37 |
+ * the file.
|
|
38 |
+ */
|
|
39 |
+ if (S_ISREG(statbuf.st_mode) && base + (off_t)len > statbuf.st_size)
|
|
40 |
+ {
|
|
41 |
+ fprintf(stderr, "mmap: Can't map beyond end of file %s\n",
|
|
42 |
+ devmem);
|
|
43 |
+ free(p);
|
|
44 |
+ return NULL;
|
|
45 |
+ }
|
|
46 |
+
|
|
47 |
#ifdef _SC_PAGESIZE
|
|
48 |
mmoffset = base % sysconf(_SC_PAGESIZE);
|
|
49 |
#else
|