Skip to content

Commit 9b4c267

Browse files
author
Sean Hefty
committed
util/bufpool: Check against accessing outside of buffers
Add magic values to verify that users do not access buffers outside of the exposed areas. Signed-off-by: Sean Hefty <sean.hefty@intel.com>
1 parent f0049e3 commit 9b4c267

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

include/ofi_mem.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,20 @@ struct ofi_bufpool_region {
333333
OFI_DBG_VAR(size_t, use_cnt)
334334
};
335335

336+
struct ofi_bufpool_ftr {
337+
size_t magic;
338+
};
339+
336340
struct ofi_bufpool_hdr {
337341
union {
338342
struct slist_entry slist;
339343
struct dlist_entry dlist;
340344
} entry;
341345
struct ofi_bufpool_region *region;
342346
size_t index;
347+
348+
OFI_DBG_VAR(struct ofi_bufpool_ftr *, ftr)
349+
OFI_DBG_VAR(size_t, magic)
343350
};
344351

345352
int ofi_bufpool_create_attr(struct ofi_bufpool_attr *attr,
@@ -391,6 +398,9 @@ static inline void ofi_buf_free(void *buf)
391398
{
392399
assert(ofi_buf_region(buf)->use_cnt--);
393400
assert(!(ofi_buf_pool(buf)->attr.flags & OFI_BUFPOOL_INDEXED));
401+
assert(ofi_buf_hdr(buf)->magic == OFI_MAGIC_SIZE_T);
402+
assert(ofi_buf_hdr(buf)->ftr->magic == OFI_MAGIC_SIZE_T);
403+
394404
slist_insert_head(&ofi_buf_hdr(buf)->entry.slist,
395405
&ofi_buf_pool(buf)->free_list.entries);
396406
}
@@ -402,13 +412,15 @@ static inline void ofi_ibuf_free(void *buf)
402412
{
403413
struct ofi_bufpool_hdr *buf_hdr;
404414

405-
assert(ofi_buf_pool(buf)->attr.flags & OFI_BUFPOOL_INDEXED);
406-
assert(ofi_buf_region(buf)->use_cnt--);
407415
buf_hdr = ofi_buf_hdr(buf);
408416

417+
assert(ofi_buf_region(buf)->use_cnt--);
418+
assert(ofi_buf_pool(buf)->attr.flags & OFI_BUFPOOL_INDEXED);
419+
assert(buf_hdr->magic == OFI_MAGIC_SIZE_T);
420+
assert(buf_hdr->ftr->magic == OFI_MAGIC_SIZE_T);
421+
409422
dlist_insert_order(&buf_hdr->region->free_list,
410423
ofi_ibuf_is_lower, &buf_hdr->entry.dlist);
411-
412424
if (dlist_empty(&buf_hdr->region->entry)) {
413425
dlist_insert_order(&buf_hdr->region->pool->free_list.regions,
414426
ofi_ibufpool_region_is_lower,

prov/util/src/util_buf.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ int ofi_bufpool_grow(struct ofi_bufpool *pool)
114114
buf_hdr = ofi_buf_hdr(buf);
115115
buf_hdr->region = buf_region;
116116
buf_hdr->index = pool->entry_cnt + i;
117+
OFI_DBG_SET(buf_hdr->magic, OFI_MAGIC_SIZE_T);
118+
OFI_DBG_SET(buf_hdr->ftr,
119+
(struct ofi_bufpool_ftr *) ((char *) buf +
120+
pool->entry_size - sizeof(struct ofi_bufpool_ftr)));
121+
OFI_DBG_SET(buf_hdr->ftr->magic, OFI_MAGIC_SIZE_T);
122+
117123
if (pool->attr.init_fn) {
118124
OFI_DBG_SET(buf_hdr->entry.dlist.next, OFI_MAGIC_PTR);
119125
OFI_DBG_SET(buf_hdr->entry.dlist.prev, OFI_MAGIC_PTR);
@@ -163,6 +169,7 @@ int ofi_bufpool_create_attr(struct ofi_bufpool_attr *attr,
163169
pool->attr = *attr;
164170

165171
entry_sz = (attr->size + sizeof(struct ofi_bufpool_hdr));
172+
OFI_DBG_ADD(entry_sz, sizeof(struct ofi_bufpool_ftr));
166173
pool->entry_size = ofi_get_aligned_size(entry_sz, attr->alignment);
167174

168175
if (!attr->chunk_cnt) {

0 commit comments

Comments
 (0)