summaryrefslogtreecommitdiff
path: root/source/ap/dmidecode/patches/0002-Avoid-SIGBUS-on-mmap-failure.patch
blob: 924a2ffefc968e6367262bfcdf0a58450497fa8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
From c081fa410e7c466df4b3b257e7b974b71fb7f250 Mon Sep 17 00:00:00 2001
From: Jean Delvare <jdelvare@suse.de>
Date: Wed, 14 Oct 2015 14:37:04 +0200
Subject: [PATCH 2/9] Avoid SIGBUS on mmap failure

mmap will fail with SIGBUS if trying to map a non-existent portion of
a file. While this should never happen with /dev/mem, it can happen if
passing a regular file with option -d. While people should no longer
do that, failure gracefully seems better than crashing. So check for
the file size before calling mmap.

This closes bug #46066:
http://savannah.nongnu.org/bugs/?46066
---
 CHANGELOG |  6 ++++++
 util.c    | 21 +++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/CHANGELOG b/CHANGELOG
index 42d815c..aa1c28f 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,9 @@
+2015-10-14  Jean Delvare  <jdelvare@suse.de>
+
+	* util.c: Avoid SIGBUS on mmap failure.
+	  This fixes Savannah bug #46066:
+	  https://savannah.nongnu.org/bugs/?46066
+
 2015-10-01  Roy Franz  <roy.franz@linaro.org>
 
 	* dmiopt.c: Add "--no-sysfs" option description to -h output.
diff --git a/util.c b/util.c
index 8cafe5c..5795d02 100644
--- a/util.c
+++ b/util.c
@@ -152,6 +152,7 @@ void *mem_chunk(off_t base, size_t len, const char *devmem)
 	void *p;
 	int fd;
 #ifdef USE_MMAP
+	struct stat statbuf;
 	off_t mmoffset;
 	void *mmp;
 #endif
@@ -169,6 +170,26 @@ void *mem_chunk(off_t base, size_t len, const char *devmem)
 	}
 
 #ifdef USE_MMAP
+	if (fstat(fd, &statbuf) == -1)
+	{
+		fprintf(stderr, "%s: ", devmem);
+		perror("stat");
+		free(p);
+		return NULL;
+	}
+
+	/*
+	 * mmap() will fail with SIGBUS if trying to map beyond the end of
+	 * the file.
+	 */
+	if (S_ISREG(statbuf.st_mode) && base + (off_t)len > statbuf.st_size)
+	{
+		fprintf(stderr, "mmap: Can't map beyond end of file %s\n",
+			devmem);
+		free(p);
+		return NULL;
+	}
+
 #ifdef _SC_PAGESIZE
 	mmoffset = base % sysconf(_SC_PAGESIZE);
 #else
-- 
2.6.4