Skip to content

Commit

Permalink
extstore: fix segfault if page_count is too high.
Browse files Browse the repository at this point in the history
leaked into a runtime bug :(

fixes memcached#482
  • Loading branch information
dormando committed Apr 27, 2019
1 parent 8caa414 commit f8dc1ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
13 changes: 12 additions & 1 deletion extstore.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ const char *extstore_err(enum extstore_res res) {
case EXTSTORE_INIT_PAGE_WBUF_ALIGNMENT:
rv = "page_size and wbuf_size must be divisible by 1024*1024*2";
break;
case EXTSTORE_INIT_TOO_MANY_PAGES:
rv = "page_count must total to < 65536. Increase page_size or lower path sizes";
break;
case EXTSTORE_INIT_OOM:
rv = "failed calloc for engine";
break;
Expand Down Expand Up @@ -247,6 +250,7 @@ void *extstore_init(struct extstore_conf_file *fh, struct extstore_conf *cf,
}

e->page_size = cf->page_size;
uint64_t temp_page_count = 0;
for (f = fh; f != NULL; f = f->next) {
f->fd = open(f->file, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (f->fd < 0) {
Expand All @@ -257,10 +261,17 @@ void *extstore_init(struct extstore_conf_file *fh, struct extstore_conf *cf,
free(e);
return NULL;
}
e->page_count += f->page_count;
temp_page_count += f->page_count;
f->offset = 0;
}

if (temp_page_count >= UINT16_MAX) {
*res = EXTSTORE_INIT_TOO_MANY_PAGES;
free(e);
return NULL;
}
e->page_count = temp_page_count;

e->pages = calloc(e->page_count, sizeof(store_page));
if (e->pages == NULL) {
*res = EXTSTORE_INIT_OOM;
Expand Down
1 change: 1 addition & 0 deletions extstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ enum extstore_res {
EXTSTORE_INIT_NEED_MORE_WBUF,
EXTSTORE_INIT_NEED_MORE_BUCKETS,
EXTSTORE_INIT_PAGE_WBUF_ALIGNMENT,
EXTSTORE_INIT_TOO_MANY_PAGES,
EXTSTORE_INIT_OOM,
EXTSTORE_INIT_OPEN_FAIL,
EXTSTORE_INIT_THREAD_FAIL
Expand Down

0 comments on commit f8dc1ab

Please sign in to comment.