Skip to content

Commit 885d503

Browse files
yonghuahwenlingz
authored andcommitted
dm: refine 'assert' in hugetlb.c and mem.c
cleanup 'assert' usage to avoid possible software vulnerabilities. Tracked-On: #3252 Signed-off-by: Yonghua Huang <yonghua.huang@intel.com> Reviewed-by: Shuo A Liu <shuo.a.liu@intel.com>
1 parent 65d7d83 commit 885d503

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

devicemodel/core/hugetlb.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include <sys/types.h>
3737
#include <fcntl.h>
3838
#include <errno.h>
39-
#include <assert.h>
4039

4140
#include "vmmapi.h"
4241

@@ -267,7 +266,6 @@ static int mmap_hugetlbfs(struct vmctx *ctx, size_t offset,
267266
pg_size = hugetlb_priv[level].pg_size;
268267

269268
while (len > 0) {
270-
assert((offset & (pg_size - 1)) == 0);
271269
ret = mmap_hugetlbfs_from_level(ctx, level, len, offset, skip);
272270

273271
if (ret < 0 && level > HUGETLB_LV1) {
@@ -297,7 +295,6 @@ static void get_lowmem_param(struct hugetlb_info *htlb,
297295
static size_t adj_lowmem_param(struct hugetlb_info *htlb,
298296
struct hugetlb_info *htlb_prev, int adj_size)
299297
{
300-
assert(htlb->lowmem >= adj_size);
301298
htlb->lowmem -= adj_size;
302299
htlb_prev->lowmem += adj_size;
303300

@@ -314,7 +311,6 @@ static void get_highmem_param(struct hugetlb_info *htlb,
314311
static size_t adj_highmem_param(struct hugetlb_info *htlb,
315312
struct hugetlb_info *htlb_prev, int adj_size)
316313
{
317-
assert(htlb->highmem >= adj_size);
318314
htlb->highmem -= adj_size;
319315
htlb_prev->highmem += adj_size;
320316

@@ -331,7 +327,6 @@ static void get_biosmem_param(struct hugetlb_info *htlb,
331327
static size_t adj_biosmem_param(struct hugetlb_info *htlb,
332328
struct hugetlb_info *htlb_prev, int adj_size)
333329
{
334-
assert(htlb->biosmem >= adj_size);
335330
htlb->biosmem -= adj_size;
336331
htlb_prev->biosmem += adj_size;
337332

devicemodel/core/mem.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <errno.h>
3636
#include <stdio.h>
3737
#include <stdlib.h>
38-
#include <assert.h>
3938
#include <string.h>
4039
#include <pthread.h>
4140

@@ -178,7 +177,8 @@ emulate_mem(struct vmctx *ctx, struct mmio_request *mmio_req)
178177

179178
pthread_rwlock_unlock(&mmio_rwlock);
180179

181-
assert(entry != NULL);
180+
if (entry == NULL)
181+
return -EINVAL;
182182

183183
if (mmio_req->direction == REQUEST_READ)
184184
err = mem_read(ctx, 0, paddr, (uint64_t *)&mmio_req->value,
@@ -238,11 +238,11 @@ unregister_mem_int(struct mmio_rb_tree *rbt, struct mem_range *memp)
238238
err = mmio_rb_lookup(rbt, memp->base, &entry);
239239
if (err == 0) {
240240
mr = &entry->mr_param;
241-
if (strncmp(mr->name, memp->name, MEMNAMESZ)) {
241+
if (strncmp(mr->name, memp->name, MEMNAMESZ)
242+
|| (mr->base != memp->base) || (mr->size != memp->size)
243+
|| ((mr->flags & MEM_F_IMMUTABLE) != 0)) {
242244
err = -1;
243245
} else {
244-
assert(mr->base == memp->base && mr->size == memp->size);
245-
assert((mr->flags & MEM_F_IMMUTABLE) == 0);
246246
RB_REMOVE(mmio_rb_tree, rbt, entry);
247247

248248
/* flush Per-VM cache */

0 commit comments

Comments
 (0)