Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mpm-ac: fix integer overflow on allocation #2546

Closed
wants to merge 1 commit into from

Conversation

satta
Copy link
Contributor

@satta satta commented Feb 8, 2017

The size of a memory buffer to be reallocated during state creation in the Aho-Corasick implementation was kept in a signed int instead of a size_t, leading to an overflow when large lists of long and diverse patterns cause the amount of AC states to blow up (>2GB). This case can be triggered for reproduction using a script like https://gist.github.com/satta/e47f7a6f0e44c9898804121e4d8c5f2e.

Addresses Redmine issues #1827 (https://redmine.openinfosecfoundation.org/issues/1827) and #1843 (https://redmine.openinfosecfoundation.org/issues/1843).

This PR adds the following functionality:

  • A new inline function SCACCheckSafeSizetMult() to check whether a size_t multiplication overflows, exit() ing in this case.
  • Type change of the size variable in SCACReallocState() to size_t.

I ran the Dockerized prscript, builds completed successfully:

The size of a memory buffer to be allocated was kept in a signed int
instead of a size_t, leading to an overflow when large lists of long
and diverse patterns cause the amount of AC states to blow up (>2GB).
Fixes Redmine issues OISF#1827 and OISF#1843.

Signed-off-by: Sascha Steinbiss <sascha@steinbiss.name>
@inliniac inliniac added this to the 3.2.1 milestone Feb 8, 2017

/* reallocate space in the goto table to include a new state */
size = cnt * ctx->single_state_size;
size = SCACCheckSafeSizetMult((size_t) cnt, (size_t) ctx->single_state_size);
ptmp = SCRealloc(ctx->goto_table, size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, scan-build-3.9 now warns that size could be 0:

util-mpm-ac.c:160:12: warning: Call to 'realloc' has an allocation size of 0 bytes
    ptmp = SCRealloc(ctx->goto_table, size);
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./util-mem.h:193:14: note: expanded from macro 'SCRealloc'
    ptrmem = realloc((x), (a)); \
             ^~~~~~~~~~~~~~~~~
1 warning generated.

I guess a simple size check would fix it. It is out goal to get to 0 issues in scan-build (and other checkers), even if they are sometimes far-fetched or even wrong. So we'll need a fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll look into it. I can totally understand the goal of static checker cleanness -- we have also used scan-build (as a successor of splint) in previous projects.

@satta satta closed this Feb 8, 2017
@satta satta deleted the fix-integer-overflow-1827-1843-v1 branch February 20, 2020 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants