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

clif: After incrementing pos, make sure it is still in bounds #15945

Merged
merged 1 commit into from
Feb 10, 2021

Conversation

nmeum
Copy link
Member

@nmeum nmeum commented Feb 8, 2021

Contribution description

While the for-loop condition does contain a bounds check, the pointer is independently increment in the for-loop body. This increment therefore requires a separate bounds check. Otherwise, the parsing loop may access data outside the given buffer boundaries.

Testing procedure

Sample application code:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <clif.h>

static uint8_t buf[] = { 59, 196, 198, 196, 196, 196, 196, 196, 196, 61 };

int main(void)
{
        char *input = (char *)&buf[0];
	clif_attr_t attr;
	clif_get_attr(input, sizeof(buf), &attr);
	return 0;
}

Minimal Makefile:

APPLICATION = clif

BOARD = native

USEMODULE += clif

RIOTBASE ?= $(CURDIR)/../..
include $(RIOTBASE)/Makefile.include

Invoke as:

$ make -C examples/clif all-asan
$ make -C examples/clif term
main(): This is RIOT! (Version: 2021.04-devel-461-g50cf93)
=================================================================
==6533==ERROR: AddressSanitizer: global-buffer-overflow on address 0x565cb0ea at pc 0x565bb00a bp 0x565d08d8 sp 0x565d08cc
READ of size 1 at 0x565cb0ea thread T0
    #0 0x565bb009 in clif_get_attr /root/RIOT/sys/clif/clif.c:281
    #1 0x565bab5b in main /root/RIOT/examples/clif/main.c:12
    #2 0x565bb501 in main_trampoline /root/RIOT/core/init.c:58
    #3 0xf76ca53a in makecontext (/lib/i386-linux-gnu/libc.so.6+0x4153a)

0x565cb0ea is located 0 bytes to the right of global variable 'buf' defined in '/root/RIOT/examples/clif/main.c:6:16' (0x565cb0e0) of size 10
SUMMARY: AddressSanitizer: global-buffer-overflow /root/RIOT/sys/clif/clif.c:281 in clif_get_attr
Shadow bytes around the buggy address:
  0x2acb95c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x2acb95d0: f9 f9 f9 f9 00 00 00 00 00 00 00 00 00 00 00 00
  0x2acb95e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x2acb95f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x2acb9600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x2acb9610: 00 00 00 00 00 00 00 00 00 00 00 00 00[02]f9 f9
  0x2acb9620: f9 f9 f9 f9 00 00 00 00 f9 f9 f9 f9 f9 f9 f9 f9
  0x2acb9630: 00 00 00 00 00 00 f9 f9 f9 f9 f9 f9 00 00 00 00
  0x2acb9640: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x2acb9650: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x2acb9660: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==6533==ABORTING

With this patch applied, no error is reported.

Issues/PRs references

None.

While the for-loop condition does contain a bounds check, the pointer is
independently increment in the for-loop body. This increment therefore
requires a separate bounds check. Otherwise, the parsing loop may access
data outside the given buffer boundaries.
@leandrolanzieri leandrolanzieri added Area: sys Area: System Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) labels Feb 8, 2021
Copy link
Contributor

@leandrolanzieri leandrolanzieri left a comment

Choose a reason for hiding this comment

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

Thanks @nmeum for the fix! Just a couple of comments

sys/clif/clif.c Outdated Show resolved Hide resolved
@@ -278,7 +278,9 @@ ssize_t clif_get_attr(const char *input, size_t input_len, clif_attr_t *attr)
attr->key_len = pos - attr->key;
/* check if the value is quoted and prepare pointer for value scan */
pos++;
if (*pos == '"') {
if (pos == end)
break;
Copy link
Contributor

Choose a reason for hiding this comment

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

If it gets to this condition, it would be a malformed attribute right? (e.g. foo=)

Suggested change
break;
return CLIF_NOT_FOUND;

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, but what about foo="? That doesn't seem to return CLIF_NOT_FOUND either at the moment or am I missing something here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we should check for that, and also a possible foo="ba I guess, and return CLIF_NOT_FOUND

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, maybe it makes sense to implement this separately? The changes I propose here handle foo= in the same way as foo=" is handled presently.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. Could you add your proposed test to the unit test application?

Copy link
Member Author

@nmeum nmeum Feb 9, 2021

Choose a reason for hiding this comment

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

Done. Keep in mind though that the tests are not run with ASAN enabled. So even if this bug would be reintroduced they would not necessarily detect it.

@nmeum nmeum requested a review from miri64 as a code owner February 9, 2021 08:48
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Feb 9, 2021
@fjmolinas fjmolinas added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Feb 9, 2021
@leandrolanzieri leandrolanzieri added Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 3-testing The PR was tested according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines labels Feb 10, 2021
@leandrolanzieri leandrolanzieri added this to the Release 2021.04 milestone Feb 10, 2021
Copy link
Contributor

@leandrolanzieri leandrolanzieri left a comment

Choose a reason for hiding this comment

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

ACK.

@leandrolanzieri leandrolanzieri merged commit 609c9ad into RIOT-OS:master Feb 10, 2021
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: sys Area: System CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 3-testing The PR was tested according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants