Skip to content

Detect signed integer wrap-around (overflow and underflow) #26

@kees

Description

@kees

Much like the array bounds checking (issue #25), the kernel should be detecting signed integer overflows using the Undefined Behavior Sanitizer (UBSan) compiler feature. There are some false positives that need to be fixed (e.g. commit a318f12), and there are some true positives that are expected (e.g. refcount_t overflow), and need to be marked.

There is, however, a complication with the kernel's use of -fno-strict-overflow which implies -fwrapv-pointer and -fwrapv (which is needed to keep the compiler from optimizing things away that are considered "undefined", when what is wanted is "expected" 2s-complement wrap-around on overflow). The former is for wrapped unsigned integer overflow (i.e. unsigned long pointer values), and the latter is needed for true positive signed overflow (i.e. refcount_t). However, this makes integer overflow no longer undefined behavior, making UBSan not catch overflows any more. :( To fix this, we need the "intentional overflow/wrap" helpers to DTRT in the face of -fno-wrapv so that UBSan will work correctly.

Language clarification:

  • "overflow" has a specific meaning related to undefined behavior
  • "wrap (around)" has a specific meaning related to the handling of signed overflow through wrap-around (i.e. defined behavior).

To avoid Undefined Behavior, the kernel must keep -fno-strict-overflow.

So, things to do:

  • make signed integer sanitizer work even with -fwrapv (and -fno-strict-overflow). done
  • create "expected signed overflow" helper inline functions marked with __attribute__((no_sanitize("signed-integer-overflow"))). done
  • add back signed integer overflow as a UBSan Kconfig done
  • add note to "deprecated.rst" with something like "open coded signed integer wrap around without a helper".
  • investigate need for -fsanitize=signed-integer-truncation and create new issue if needed
  • annote enough false positives that production workloads can run (e.g. Android, Ubuntu, etc).

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerNeeds compiler support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions