[cfe-dev] [analyzer] Constrain the size of unknown memory regions

Csaba Dabis via cfe-dev cfe-dev at lists.llvm.org
Thu Mar 12 21:13:00 PDT 2020


*How To Ask The Analyzer The Smart Way*

I hope the following writing could help checker developers to make sure
they understand the problem by asking the Analyzer what could be the
problem.

-------------

> How to constrain the size of unknown memory regions, eg. pointed by 'raw'
char pointers?

Well, the secret is: you do not. The Analyzer tries to give you the best
approximation of your program.

-------------

> How should tell the analyzer that the array which is pointed by the
pointer holds at least/most N elements?

The Analyzer denote a pointer with a symbolic value and during the analysis
it applies constraints on such symbols on their own will using the core
facilities and existing API modeling checkers. If your checker models a
function call/nullability, so that you need to explicitly create
assumptions you can `assume()` values using the ProgramState, like:
`std::tie(StateTrue, StateFalse) = State->assume(Value);`. With that you
can force out a state split and if your new assumption of `Value` does not
contradict with previous assumptions `StateTrue` holds the assumption, and
`StateFalse` holds the assumption negated. If such contradiction happens
the return value is a `nullptr`.

-------------

> What would be the right approach to guide (to constrain the size of a
memory region) the analyzer?

Use [1] checkers and know them well, they guide the Analyzer. Otherwise
create your own API modeling checkers.

If you can't solve a problem, then there is an easier problem you can't
> solve: find it.

- George Polya.

First, let me simplify your problem/example and make sure we can work with
it:
```
// RUN: %clang_analyze_cc1 \
// RUN:  -analyzer-checker=core,unix,debug.ExprInspection \
// RUN:  -verify %s

typedef __SIZE_TYPE__ size_t;
size_t strlen(const char *);

void clang_analyzer_printState();

void test(const char *src) {
  if (strlen(src) < 10)
    return;

  clang_analyzer_printState();
  *(void)src;*
}
```

It is a *runnable* test using `llvm-lit` which dumps out the state of the
program:
- The `core` package models the analysis.
- The `unix` package models memory and string manipulation.
- The [2] `debug.ExprInspection` checker / `debug` package defines function
calls which could be used to make human-readable what the Analyzer thinks
of your program at the moment of the function call.
- `(void)src` makes sure when we print out the state the Analyzer does not
throw away the information of `src`. Without that extra call we would print
out the state where dead symbols, such as `src` would be purged out from
the analysis.

It dumps out the following:
```
"program_state": {
  ...
  "constraints": [
    { "symbol": "reg_$0<const char * src>", "range": "{ [1,
18446744073709551615] }" },
    { *"symbol"*: "meta_$1{SymRegion{reg_$0<const char * *src*>},unsigned
long}", *"range"*: "{ [10, 4611686018427387903] }" }
  ],
  ...
}
```

To continue dig into your example, we need to predefine function
signatures, which you can find easily using Microsoft Docs [3]:
```
// RUN: %clang_analyze_cc1 \
// RUN:  -analyzer-checker=core,unix,debug.ExprInspection \
// RUN:  -verify %s

typedef __SIZE_TYPE__ size_t;
size_t strlen(const char *);
*char *strncpy(char *dst, const char *src, size_t size);*

void clang_analyzer_printState();

void test(char *dst, const char *src, size_t size) {
  if (strlen(src) < 10)
    return;

  *strncpy*(dst, src, size);
  clang_analyzer_printState();

  (void)dst; (void)src; (void)size;
}
```

With that you could see if the Analyzer cannot model something enough
precisely for your needs. With `grep` and GitHub's blame system usually you
can find out how such API modeling introduced by an LLVM Phabricator
review's link. If you want to learn more about symbolic values please visit
section 5 from Artem Dergachev's [4] booklet.

-------------

> How can the analyzer inference such constraint?

Like magic and unicorns

and we name it ConstraintManager.

-------------

> assert(&src[n] - &src[-1]);

I do not get what you are trying to do with this expression. Back in the
days I have learnt [5] how to ask questions the smart way, which sounds
rude for the first time, but this is the best reading on the web:
http://www.catb.org/esr/faqs/smart-questions.html

-------------

[1] Available Analyzer checkers:
https://clang.llvm.org/docs/analyzer/checkers.html

[2] Debug checkers - ExprInspection:
https://clang.llvm.org/docs/analyzer/developer-docs/DebugChecks.html#exprinspection-checks

[3] Microsoft Docs:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strncpy-strncpy-l-wcsncpy-wcsncpy-l-mbsncpy-mbsncpy-l

[4] Artem Dergachev's booklet: A Checker Developer's Guide:
https://github.com/haoNoQ/clang-analyzer-guide/releases

[5] How To Ask Questions The Smart Way:
http://www.catb.org/esr/faqs/smart-questions.html

On Thu, Mar 12, 2020 at 1:20 PM Balázs Benics via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hi, checker devs
>
> TLDR:
> How to constrain the size of unknown memory regions, eg. pointed by 'raw'
> char pointers?
>
> longer version:
> Working on taint analysis I'm facing with the following problem:
>
> void strncpy_bounded_tainted_buffer(char *src, char *dst) {
>>   // assert(strlen(src) >= 10 && "src must have at leas 10 elements");
>>   int n;
>>   scanf("%d", &n);
>>   if (0 < n && n < 10) {
>>     strncpy(dst, src, n); // Should we warn or not?
>>   }
>> }
>>
>
>
> In this example we analyze a function taking two raw pointers, so we don't
> know how big those arrays are.
> We will check the `strncpy` call, whether it will access *(read and
> write)* only valid memory.
> We will check the pointers (src and dst) separately by checking if *`*&src[0]`
> and `&src[n-1]` would be in bound of the memory region pointed by the
> pointer. Since the analyzer don't know (both states are non-null), we
> should check if the `length` parameter is tainted, and if so, we should
> still issue a warning telling that "String copy function might overflow the
> given buffer since untrusted data is used to specify the buffer size."
> Obviously, if the `length` parameter is not tainted, we will assume
> (conservatively) that the access would be valid.
>
>
> How should tell the analyzer that the array which is pointed by the
> pointer holds at least/most N elements?
> For example in the code above, express something similar via an assertion,
> like saying that `src` points to a c-string, which has at least 10 + 1
> element underlying storage.
> Although this assertion using `strlen` seems like a solution,
> unfortunately not applicable for example to the `dst` buffer, which is
> most likely uninitialized - so not a c-string, in other words calling `
> strlen` would be undefined behavior.
>
> The only (hacky) option which came in my mind was to abuse the standard
> regarding pointer arithmetic.
>
>> assert(&src[n] - &src[-1]);
>>
> The standard is clear about that pointer arithmetic is only applicable for
> pointers pointing to elements of the same array OR to a hypothetical ONE
> past/before element of that array.
> http://eel.is/c++draft/expr.add#4.2
>
> This assertion would be undefined behavior if the size of the array
> pointed by `src` would be smaller than `n`.
>
> IMO this looks really ugly.
> I think that no '*annotations*' should introduce UB even if that
> assumption expressed via an annotation is turned out to be *invalid*.
>
>
> What would be the right approach to guide (to constrain the size of a
> memory region) the analyzer?
> How can the analyzer inference such constraint?
>
> Thanks Balazs.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200313/0dd9c8f8/attachment.html>


More information about the cfe-dev mailing list