diff --git a/lib/src/mmalloc/mmalloc.c b/lib/src/mmalloc/mmalloc.c index 0d50f75dc..1620007a9 100644 --- a/lib/src/mmalloc/mmalloc.c +++ b/lib/src/mmalloc/mmalloc.c @@ -60,6 +60,7 @@ #include #include #include +#include #include "mmalloc.h" #include "../coll/rbt.h" #include "ovis-test/test.h" @@ -104,6 +105,24 @@ static int compare_addr(void *node_key, const void *val_key) static mm_region_t mmr; +void mm_validate_access(void *addr, size_t size) +{ + struct rbn *rbn; + struct mm_prefix *pfx; + assert((uint64_t)addr >= (uint64_t)mmr->start); + assert(((uint64_t)addr + (uint64_t)size) < + ((uint64_t)mmr->start + (uint64_t)mmr->size)); + RBT_FOREACH(rbn, &mmr->addr_tree) { + pfx = container_of(rbn, struct mm_prefix, addr_node); + if ((uint64_t)addr < (uint64_t)pfx) { + assert((uint64_t)addr + (uint64_t)size < (uint64_t)pfx); + } else { + size_t size = pfx->count << mmr->grain_bits; + assert((uint64_t)addr > (uint64_t)pfx + (uint64_t)size); + } + } +} + void mm_get_info(struct mm_info *mmi) { mmi->grain = mmr->grain; diff --git a/lib/src/zap/sock/zap_sock.c b/lib/src/zap/sock/zap_sock.c index ced24cfaf..0e216941d 100644 --- a/lib/src/zap/sock/zap_sock.c +++ b/lib/src/zap/sock/zap_sock.c @@ -681,6 +681,7 @@ void __sock_io_free(struct z_sock_ep *sep, struct z_sock_io *io) */ static void process_sep_msg_read_resp(struct z_sock_ep *sep) { + void mm_validate_access(void *addr, size_t size); struct z_sock_io *io; struct sock_msg_read_resp *msg; uint32_t data_len; @@ -709,6 +710,9 @@ static void process_sep_msg_read_resp(struct z_sock_ep *sep) data_len, 0); switch (rc) { case 0: + /* Verify that the dst_ptr is not + reallocated/free in the mmr */ + mm_validate_access(io->dst_ptr, data_len); memcpy(io->dst_ptr, msg->data, data_len); break; case EACCES: