tree-wide: check results of PAGE_ALIGN()

Fixes CID#1491282, CID#1491283, CID#1491285, CID#1491288.
This commit is contained in:
Yu Watanabe
2023-10-18 14:32:17 +09:00
parent c886f2d26e
commit cbdac0c33a
6 changed files with 22 additions and 6 deletions

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");

View File

@@ -405,6 +405,7 @@ void server_process_native_file(
/* The file is sealed, we can just map it and use it. */
ps = PAGE_ALIGN(st.st_size);
assert(ps < SIZE_MAX);
p = mmap(NULL, ps, PROT_READ, MAP_PRIVATE, fd, 0);
if (p == MAP_FAILED) {
log_ratelimit_error_errno(errno, JOURNAL_LOG_RATELIMIT,

View File

@@ -27,8 +27,11 @@
#include "memory-util.h"
void close_and_munmap(int fd, void *address, size_t size) {
if (size > 0)
assert_se(munmap(address, PAGE_ALIGN(size)) >= 0);
if (size > 0) {
size = PAGE_ALIGN(size);
assert(size < SIZE_MAX);
assert_se(munmap(address, size) >= 0);
}
safe_close(fd);
}

View File

@@ -2490,6 +2490,8 @@ int bus_body_part_map(struct bus_body_part *part) {
shift = PAGE_OFFSET(part->memfd_offset);
psz = PAGE_ALIGN(part->size + shift);
if (psz >= SIZE_MAX)
return -EFBIG;
if (part->memfd >= 0)
p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE, part->memfd, part->memfd_offset - shift);

View File

@@ -379,7 +379,9 @@ int journal_file_fss_load(JournalFile *f) {
if (le64toh(header->start_usec) <= 0 || le64toh(header->interval_usec) <= 0)
return -EBADMSG;
f->fss_file = mmap(NULL, PAGE_ALIGN(f->fss_file_size), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
size_t sz = PAGE_ALIGN(f->fss_file_size);
assert(sz < SIZE_MAX);
f->fss_file = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (f->fss_file == MAP_FAILED) {
f->fss_file = NULL;
return -errno;

View File

@@ -302,9 +302,11 @@ JournalFile* journal_file_close(JournalFile *f) {
#endif
#if HAVE_GCRYPT
if (f->fss_file)
munmap(f->fss_file, PAGE_ALIGN(f->fss_file_size));
else
if (f->fss_file) {
size_t sz = PAGE_ALIGN(f->fss_file_size);
assert(sz < SIZE_MAX);
munmap(f->fss_file, sz);
} else
free(f->fsprg_state);
free(f->fsprg_seed);