summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff.email>2023-03-15 15:11:53 +0100
committerJörg Frings-Fürst <debian@jff.email>2023-03-15 15:11:53 +0100
commitdb1a6f51c433843eb5d59a564d080b043ad4c46b (patch)
treefd5af76ae152493f02afeb360c6cf206b81333bc /util.c
parentabf98f6ec0ce25c284fbe71bdd56571ea7df5672 (diff)
parent69e8c5b5339d256e8973c536926d2ecc9b1b86fc (diff)
Merge branch 'feature/upstream' into develop
Diffstat (limited to 'util.c')
-rw-r--r--util.c69
1 files changed, 16 insertions, 53 deletions
diff --git a/util.c b/util.c
index 04aaadd..2770b1d 100644
--- a/util.c
+++ b/util.c
@@ -155,6 +155,7 @@ out:
return p;
}
+#ifdef USE_MMAP
static void safe_memcpy(void *dest, const void *src, size_t n)
{
#ifdef USE_SLOW_MEMCPY
@@ -164,8 +165,9 @@ static void safe_memcpy(void *dest, const void *src, size_t n)
*((u8 *)dest + i) = *((const u8 *)src + i);
#else
memcpy(dest, src, n);
-#endif
+#endif /* USE_SLOW_MEMCPY */
}
+#endif /* USE_MMAP */
/*
* Copy a physical memory chunk into a memory buffer.
@@ -173,18 +175,26 @@ static void safe_memcpy(void *dest, const void *src, size_t n)
*/
void *mem_chunk(off_t base, size_t len, const char *devmem)
{
- void *p;
+ struct stat statbuf;
+ void *p = NULL;
int fd;
#ifdef USE_MMAP
- struct stat statbuf;
off_t mmoffset;
void *mmp;
#endif
- if ((fd = open(devmem, O_RDONLY)) == -1)
+ /*
+ * Safety check: if running as root, devmem is expected to be a
+ * character device file.
+ */
+ if ((fd = open(devmem, O_RDONLY)) == -1
+ || fstat(fd, &statbuf) == -1
+ || (geteuid() == 0 && !S_ISCHR(statbuf.st_mode)))
{
- perror(devmem);
- return NULL;
+ fprintf(stderr, "Can't read memory from %s\n", devmem);
+ if (fd == -1)
+ return NULL;
+ goto out;
}
if ((p = malloc(len)) == NULL)
@@ -194,13 +204,6 @@ 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");
- goto err_free;
- }
-
/*
* mmap() will fail with SIGBUS if trying to map beyond the end of
* the file.
@@ -259,46 +262,6 @@ out:
return p;
}
-int write_dump(size_t base, size_t len, const void *data, const char *dumpfile, int add)
-{
- FILE *f;
-
- f = fopen(dumpfile, add ? "r+b" : "wb");
- if (!f)
- {
- fprintf(stderr, "%s: ", dumpfile);
- perror("fopen");
- return -1;
- }
-
- if (fseek(f, base, SEEK_SET) != 0)
- {
- fprintf(stderr, "%s: ", dumpfile);
- perror("fseek");
- goto err_close;
- }
-
- if (fwrite(data, len, 1, f) != 1)
- {
- fprintf(stderr, "%s: ", dumpfile);
- perror("fwrite");
- goto err_close;
- }
-
- if (fclose(f))
- {
- fprintf(stderr, "%s: ", dumpfile);
- perror("fclose");
- return -1;
- }
-
- return 0;
-
-err_close:
- fclose(f);
- return -1;
-}
-
/* Returns end - start + 1, assuming start < end */
u64 u64_range(u64 start, u64 end)
{