Merge pull request #29601 from yuwata/mmap-check-overflow

mmap: check offset and size more carefully
This commit is contained in:
Luca Boccassi
2023-10-24 12:20:04 +01:00
committed by GitHub
27 changed files with 365 additions and 82 deletions
+6
View File
@@ -81,6 +81,9 @@ static int update_argv(const char name[], size_t l) {
static int can_do = -1;
int r;
assert(name);
assert(l < SIZE_MAX);
if (can_do == 0)
return 0;
can_do = false; /* We'll set it to true only if the whole process works */
@@ -102,6 +105,9 @@ static int update_argv(const char name[], size_t l) {
char *nn;
nn_size = PAGE_ALIGN(l+1);
if (nn_size >= SIZE_MAX)
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "The requested argument is too long.");
nn = mmap(NULL, nn_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (nn == MAP_FAILED)
return log_debug_errno(errno, "mmap() failed: %m");
+1 -1
View File
@@ -284,7 +284,7 @@ ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) {
return -EIO;
}
if (lseek(fd, n, SEEK_CUR) == (off_t) -1)
if (lseek(fd, n, SEEK_CUR) < 0)
return -errno;
q += n;
+7
View File
@@ -51,6 +51,13 @@
#error "neither int nor long are four bytes long?!?"
#endif
static inline uint64_t u64_multiply_safe(uint64_t a, uint64_t b) {
if (_unlikely_(a != 0 && b > (UINT64_MAX / a)))
return 0; /* overflow */
return a * b;
}
/* align to next higher power-of-2 (except for: 0 => 0, overflow => 0) */
static inline unsigned long ALIGN_POWER2(unsigned long u) {
+6 -3
View File
@@ -12,9 +12,12 @@
#include "memory-util-fundamental.h"
size_t page_size(void) _pure_;
#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
#define PAGE_ALIGN_DOWN(l) ((l) & ~(page_size() - 1))
#define PAGE_OFFSET(l) ((l) & (page_size() - 1))
#define PAGE_ALIGN(l) ALIGN_TO(l, page_size())
#define PAGE_ALIGN_U64(l) ALIGN_TO_U64(l, page_size())
#define PAGE_ALIGN_DOWN(l) ALIGN_DOWN(l, page_size())
#define PAGE_ALIGN_DOWN_U64(l) ALIGN_DOWN_U64(l, page_size())
#define PAGE_OFFSET(l) ALIGN_OFFSET(l, page_size())
#define PAGE_OFFSET_U64(l) ALIGN_OFFSET_U64(l, page_size())
/* Normal memcpy() requires src to be nonnull. We do nothing if n is 0. */
static inline void *memcpy_safe(void *dst, const void *src, size_t n) {