summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorJean Delvare <jdelvare@suse.de>2015-10-14 14:37:04 +0200
committerJean Delvare <jdelvare@suse.de>2015-10-14 14:37:04 +0200
commitc081fa410e7c466df4b3b257e7b974b71fb7f250 (patch)
tree22a8f512604afc053ab56433abe9db362112358c /util.c
parent33b5aafc6ee6b5de9f2526fb1cf4b14d1e16e4f0 (diff)
downloaddmidecode-git-c081fa410e7c466df4b3b257e7b974b71fb7f250.tar.gz
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
Diffstat (limited to 'util.c')
-rw-r--r--util.c21
1 files changed, 21 insertions, 0 deletions
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