205 Commits

Author SHA1 Message Date
Ryosuke Niwa
ef353b02b0
Introduce a new WebKit checker for a unchecked call arguments (#113708) (#114522)
This PR introduces alpha.webkit.UncheckedCallArgsChecker which detects a
function argument which is a raw reference or a raw pointer to a
CheckedPtr capable object.
2024-11-07 08:34:37 -08:00
Ryosuke Niwa
847d4eb427
Update clang static analyzers per rename of member functions in CanMakeCheckedPtr. (#114636)
The member functions that define CheckedPtr capable type is
incrementCheckedPtrCount and decrementCheckedPtrCount after the rename.
2024-11-04 22:26:02 -08:00
Ryosuke Niwa
61a6439f35
Introduce a new WebKit checker for a unchecked local variable (#113708)
This PR introduces alpha.webkit.UncheckedLocalVarsChecker which detects
a raw reference or a raw pointer local, static, or global variable to a
CheckedPtr capable object without a guardian variable in an outer scope.
2024-10-31 23:53:07 -07:00
vabridgers
3d6923dbac
RFC: [clang-tidy] [analyzer] Move nondeterministic pointer usage check to tidy (#110471)
This change moves the `alpha.nondeterministic.PointerSorting` and
`alpha.nondeterministic.PointerIteration` static analyzer checkers to a
single `clang-tidy` check. Those checkers were implemented as simple
`clang-tidy` check-like code, wrapped in the static analyzer framework.
The documentation was updated to describe what the checks can and cannot
do, and testing was completed on a broad set of open-source projects.

Co-authored-by: Vince Bridgers <vince.a.bridgers@ericsson.com>
2024-10-28 03:53:36 -05:00
Endre Fülöp
61a76f58eb
[clang][analyzer][doc] Migrate ClangSA www FAQ section (#112831)
The ClangSA documentation lives in RST format, and the FAQ section of
the old webpage is also migrated from HTML with this change.

---------

Co-authored-by: Donát Nagy <donat.nagy@ericsson.com>
2024-10-24 17:37:04 +02:00
BrnBlrg
5e81437f2b
[analyzer][doc] Fix typo in "translation unit" in analyzer doc CommandLineUsage.rst (#112966) 2024-10-19 11:06:12 +02:00
Balázs Kéri
9df8d8d05c
[clang][analyzer] Improve test and documentation in cstring NotNullTerminated checker (#112019)
CStringChecker has a sub-checker alpha.unix.cstring.NotNullTerminated
which checks for invalid objects passed to string functions. The checker
and its name are not exact and more functions could be checked, this
change only adds some tests and improves documentation.
2024-10-16 10:17:34 +02:00
Daniel Krupp
09b8dbfa80
[analyzer] Add optin.taint.TaintedDiv checker (#106389)
Tainted division operation is separated out from the core.DivideZero
checker into the optional optin.taint.TaintedDiv checker. The checker
warns when the denominator in a division operation is an attacker
controlled value.
2024-10-01 11:33:06 +02:00
Balázs Kéri
0d384fe978
[clang][analyzer] Move 'alpha.core.PointerSub' checker into 'security.PointerSub' (#107596) 2024-09-30 09:16:27 +02:00
Ryosuke Niwa
3c0984309e
[alpha.webkit.NoUncheckedPtrMemberChecker] Introduce member variable checker for CheckedPtr/CheckedRef (#108352)
This PR introduces new WebKit checker to warn a member variable that is
a raw reference or a raw pointer to an object, which is capable of
creating a CheckedRef/CheckedPtr.
2024-09-27 00:42:18 -07:00
Daniel Krupp
f82fb06cd1
[analyzer] Moving TaintPropagation checker out of alpha (#67352)
This commit moves the **alpha.security.taint.TaintPropagation** and
**alpha.security.taint.GenericTaint** checkers to the **optin.taint**
optional package.

These checkers were stabilized and improved by recent commits thus 
they are ready for production use.
2024-09-26 14:00:13 +02:00
Balázs Kéri
ffeb793f3a
[clang][analyzer][docs] Fix documentation of checker 'StackAddrAsyncEscape' (NFC) (#108586)
The checker was indicated as a 'C' language checker but is only applicable to 'ObjC' code.
2024-09-13 17:51:28 +02:00
Balázs Kéri
525ffd6262
[clang][analyzer] Bring alpha.security.MmapWriteExec checker out of alpha package (#102636) 2024-09-03 10:31:36 +02:00
Arseniy Zaostrovnykh
4f33e7c683
[analyzer] Report violations of the "returns_nonnull" attribute (#106048)
Make sure code respects the GNU-extension `__attribute__((returns_nonnull))`.

Extend the NullabilityChecker to check that a function returns_nonnull
does not return a nullptr.

This commit also reverts an old hack introduced by
49bd58f1ebe28d97e4949e9c757bc5dfd8b2d72f
because it is no longer needed

CPP-4741
2024-08-27 14:41:52 +02:00
Donát Nagy
340be6cb79
[analyzer] Delete alpha.security.MallocOverflow (#103059)
...because it is too noisy to be useful right now, and its architecture
is terrible, so it can't act a starting point of future development.

The main problem with this checker is that it tries to do (or at least
fake) path-sensitive analysis without actually using the established
path-sensitive analysis engine.

Instead of actually tracking the symbolic values and the known
constraints on them, this checker blindly gropes the AST and uses
heuristics like "this variable was seen in a comparison operator
expression that is not a loop condition, so it's probably not too large"
(which was improved in a separate commit to at least ignore comparison
operators that appear after the actual `malloc()` call).

This might have been acceptable in 2011 (when this checker was added),
but since then we developed a significantly better standard approach for
analysis and this old relic doesn't deserve to remain in the codebase.

Needless to say, this primitive approach causes lots of false positives
(and presumably false negatives as well), which ensures that this alpha
checker won't be missed by the users.

Moreover, the goals of this checker would be questionable even if it had
a perfect implementation. It's very aggressive to assume that the
argument of malloc can overflow by default (unless the checker sees a
bounds check); and this produces too many false positives -- perhaps
even for an optin checker. It may be possible to eventually create a
useful (and properly path-sensitive) optin checker for these kinds of
suspicious code, but this is a very low priority goal.

Also note that we already have `alpha.security.TaintedAlloc` which
provides more practical heuristics for detecting somewhat similar
"argument of malloc may be too large" vulnerabilities.
2024-08-14 15:07:42 +02:00
Balázs Kéri
e607360fcd
[clang][analyzer] Remove array bounds check from PointerSubChecker (#102580)
At pointer subtraction only pointers are allowed that point into an
array (or one after the end), this fact was checker by the checker. This
check is now removed because it is a special case of array indexing
error that is handled by different checkers (like ArrayBoundsV2).
2024-08-12 11:22:30 +02:00
Balázs Kéri
cab91ecffd
[clang][analyzer] Improve PointerSubChecker (#96501)
The checker could report false positives if pointer arithmetic was done
on pointers to non-array data before pointer subtraction. Another
problem is fixed that could cause false positive if members of the same
structure but in different memory objects are subtracted.
2024-08-01 12:56:25 +02:00
Balazs Benics
13d39cb6f7
[analyzer] Fix crash of StreamChecker when eval calling 'fopen' (#100990)
Actually, on the failure branch of `fopen`, the resulting pointer could
alias with `stdout` iff `stdout` is already known to be null.
We crashed in this case as the implementation assumed that the
state-split for creating the success and failure branches both should be
viable; thus dereferenced both of those states - leading to the crash.

To fix this, let's just only add this no-alias property for the success
path, and that's it :)

Fixes #100901
2024-07-29 14:15:02 +02:00
Pavel Skripkin
893a303962
[clang][analyzer] Support ownership_{returns,takes} attributes (#98941)
Add support for checking mismatched ownership_returns/ownership_takes attributes.

Closes #76861
2024-07-24 13:15:08 +02:00
Daniel Krupp
6002e2fd49
[analyzer] Split TaintPropagation checker into reporting and modeling checkers (#98157)
Taint propagation is a a generic modeling feature of the Clang Static
Analyzer which many other checkers depend on. Therefore
GenericTaintChecker is split into a TaintPropagation modeling checker
and a GenericTaint reporting checker.
2024-07-10 17:54:53 +02:00
Endre Fülöp
093aaca2b0
[clang][analyzer][doc] Migrate user-related docs from HTML to RST (#97034)
User documentation currently found at https://clang-analyzer.llvm.org is migrated to RST format.

This commit migrates all the relevant content, including suspicious or even clearly outdated parts. These issues will be cleaned up in separate follow-up commits (where the diff is not obscured by the format change). However, a few typos are fixed, and some parts (availability of binary releases, integration with XCode) are marked (with [Legacy] tags) to highlight that they are outdated.

The primary motivation for this change is to update the facts in the docs and make them discoverable from the RST-generated doc-tree as well (many subpages are not accessible at all, as the menu generation for the HTML-based page is not working at all).


This commit migrates all the relevant content, including parts that are
suspicious or even clearly outdated. These issues will be cleaned up in
separate follow-up commits (where the diff is not obscured by the format
change). However, a few typos are fixed and some parts (availability of
binary releases, integration with XCode) are marked (with [Legacy] tags)
to highlight that they are outdated.

The primary motivation for this change is to update the facts in the
docs and make them discoverable from the RST-generated doc-tree as well
(many subpages are not accessible **at all**, as the menu generation for
the HTML based page is not working at all).
2024-07-05 08:16:53 +02:00
Balázs Kéri
2341316929
[clang][analyzer] Improve documentation of checker 'cplusplus.Move' (NFC) (#96295) 2024-06-28 10:17:27 +02:00
Balázs Kéri
2e515ed60a
[clang] Move 'alpha.cplusplus.MisusedMovedObject' to 'cplusplus.Move' in documentation (NFC) (#95003)
The checker was renamed at some time ago but the documentation was not
updated. The section is now just moved and renamed. The documentation is
still very simple and needs improvement.
2024-06-20 16:41:51 +02:00
Donát Nagy
d9a508db55
[analyzer] Finish moving alpha.core.SizeofPtr to clang-tidy (#95118)
The checker `alpha.core.SizeofPtr` was a very simple checker that did
not rely on path sensitive analysis and was very similar to the (more
complex and refined) clang-tidy check `bugprone-sizeof-expression`.

As there is no reason to maintain two separate implementations for the
same goal (and clang-tidy is more lightweight and accessible than the
Analyzer) I decided to move this functionality from the Static Analyzer
to clang-tidy.

Recently my commit 546c816a529835a4cf89deecff957ea336a94fa2
reimplemented the advantageous parts of `alpha.core.SizeofPtr` within
clang-tidy; now this commit finishes the transfer by deleting
`alpha.core.SizeofPtr`.
2024-06-12 14:26:47 +02:00
Daniel Krupp
289725f11c
[analyzer] New optin.taint.TaintedAlloc checker for catching unbounded memory allocation calls (#92420)
A new optional checker (optin.taint.TaintedAlloc) will warn if a memory
allocation function (malloc, calloc, realloc, alloca, operator new[]) is
called with a tainted (attacker controlled) size parameter.
A large, maliciously set size value can trigger memory exhaustion. To
get this warning, the alpha.security.taint.TaintPropagation checker also
needs to be switched on.

The warning will only be emitted, if the analyzer cannot prove that the
size is below reasonable bounds (<SIZE_MAX/4).
2024-06-05 16:33:31 +02:00
Balázs Kéri
bc3baa93ce
[clang][analyzer] Move PutenvStackArrayChecker out of alpha package (#93980)
Checker alpha.security.PutenvStackArray is moved to
security.PutenvStackArray.
2024-06-04 10:02:38 +02:00
Endre Fülöp
6ef785c951
[clang][analyzer] Move unix.BlockInCriticalSection out of alpha (#93815)
After recent improvements (#80029) and testing on open-source projects,
the checker is ready to move out of the alpha package.
2024-06-03 14:23:58 +02:00
Endre Fülöp
46b3145b7c
[clang][analyzer][NFC] Add test for a limitation of alpha.unix.BlockInCriticalSection checker (#93799)
Updated the documentation in `checkers.rst` to include an example of how
`trylock` function is handled.
Added a new test for a scenario where `pthread_mutex_trylock` is used,
demonstrating the current limitation.
2024-05-31 12:51:14 +02:00
Endre Fülöp
196dca7561
[clang][analyzer][NFC] Improve docs of alpha.unix.BlockInCriticalSection (#93812)
- Enhanced descriptions for blocking and critical section functions
- Added an additional code sample highlighting interleaved C and C++
style mutexes
2024-05-31 12:50:04 +02:00
Balázs Kéri
76b9d38934
[clang][analyzer] PutenvStackArrayChecker: No warning from 'main' (#93299) 2024-05-27 09:55:10 +02:00
Balázs Kéri
3c23047413
[clang][analyzer] Move checker 'cert.pos.34c' (in alpha.security) into 'PutenvStackArray' (#92424)
The "cert" package looks not useful and the checker has not a meaningful name with the old naming scheme.
Additionally tests and documentation is updated.
2024-05-23 12:56:16 +02:00
Balázs Kéri
11b97da831
[clang][analyzer] Add checker 'security.SetgidSetuidOrder' (#91445) 2024-05-22 12:11:18 +02:00
Daniel Krupp
6ceb1c0ef9
[analyzer] Remove untrusted buffer size warning in the TaintPropagation checker (#68607)
Before this commit the the checker alpha.security.taint.TaintPropagation always reported warnings when the size argument of a memcpy-like or malloc-like function was tainted. However, this produced false positive reports in situations where the size was tainted, but correctly performed bound checks guaranteed the safety of the call.
 
This commit removes the rough "always warn if the size argument is tainted" heuristic; but it would be good to add a more refined "warns if the size argument is tainted and can be too large" heuristic in follow-up commits. That logic would belong to CStringChecker and MallocChecker, because those are the checkers responsible for the more detailed modeling of memcpy-like and malloc-like functions. To mark this plan, TODO comments are added in those two checkers.
 
There were several test cases that used these sinks to test generic properties of taint tracking; those were adapted to use different logic.
 
As a minor unrelated change, this commit ensures that strcat (and its wide variant, wcsncat) propagates taint from the first argument to the first argument, i.e. a tainted string remains tainted if we concatenate it with another string. This change was required because the adapted variant of multipleTaintedArgs is relying on strncat to compose a value that combines taint from two different sources.
2024-05-02 16:46:41 +02:00
Balázs Kéri
09f160c629
[clang][analyzer] Move StreamChecker out of the alpha package. (#89247) 2024-04-30 09:01:45 +02:00
Balázs Kéri
c2067c1f47
[clang][analyzer] Add "pedantic" mode to StreamChecker. (#87322)
The checker may create failure branches for all stream write operations
only if the new option "pedantic" is set to true.
Result of the write operations is often not checked in typical code. If
failure branches are created the checker will warn for unchecked write
operations and generate a lot of "false positives" (these are valid
warnings but the programmer does not care about this problem).
2024-04-08 12:19:03 +02:00
Balázs Kéri
8dcff10e9b
[clang][analyzer] Improve documentation of StreamChecker (NFC). (#83858) 2024-03-28 18:04:35 +01:00
komalverma04
b8cc838427
[analyzer][docs] Document the optin.performance.Padding checker (#86411)
Closes #73675

Co-authored-by: Balazs Benics <benicsbalazs@gmail.com>
Co-authored-by: NagyDonat <donat.nagy@ericsson.com>
2024-03-27 13:51:27 +01:00
Discookie
37785fedab
[clang][analyzer] Bring cplusplus.ArrayDelete out of alpha (#83985)
The checker finds a type of undefined behavior, where if the type of a
pointer to an object-array is different from the objects' underlying
type, calling `delete[]` is undefined, as the size of the two objects
might be different.

The checker has been in alpha for a while now, it is a simple checker
that causes no crashes, and considering the severity of the issue, it
has a low result-count on open-source projects (in my last test-run on
my usual projects, it had 0 results).

This commit cleans up the documentation and adds docs for the limitation
related to tracking through references, in addition to moving it to
`cplusplus`.

---------

Co-authored-by: Balazs Benics <benicsbalazs@gmail.com>
Co-authored-by: whisperity <whisperity@gmail.com>
2024-03-25 10:08:56 +00:00
Balázs Kéri
bbeb946652 [clang][analyzer] Change value of checker option in unix.StdCLibraryFunctions (second try). (#80457)
Default value of checker option `ModelPOSIX` is changed to `true`.
Documentation is updated.

This is a re-apply of commit 7af4e8bcc354d2bd7e46ecf547172b1f19ddde3e
that was reverted because a test failure (this is fixed now).
2024-03-04 15:28:20 +01:00
Balázs Kéri
da5966e0c1 Revert "[clang][analyzer] Change default value of checker option in unix.StdCLibraryFunctions. (#80457)"
This reverts commit 7af4e8bcc354d2bd7e46ecf547172b1f19ddde3e.
2024-03-04 09:50:36 +01:00
Balázs Kéri
7af4e8bcc3
[clang][analyzer] Change default value of checker option in unix.StdCLibraryFunctions. (#80457)
Default value of checker option `ModelPOSIX` is changed to `true`.
Documentation is updated.
2024-03-04 09:29:18 +01:00
Daniel Krupp
de04b7d44e
[analyzer] Fix core.VLASize checker false positive taint reports (#68140)
The checker reported a false positive on this code 

void testTaintedSanitizedVLASize(void) {
  int x;
  scanf("%d", &x);
  if (x<1)
    return;
  int vla[x]; // no-warning
}

After the fix, the checker only emits tainted warning if the vla size is
coming from a tainted source and it cannot prove that it is positive.
2024-02-23 11:44:34 +01:00
Balázs Kéri
2b5e4eeb26
[clang][analyzer] Remove 'alpha.core.CallAndMessageUnInitRefArg' from documentation (NFC). (#81138)
This checker does not exist (any more?) but appeared in the
documentation. No other references to CallAndMessageUnInitRefArg are
found in the full clang code.
2024-02-12 15:30:58 +01:00
Gábor Spaits
1d2fab74af
[analyzer] Add documentation for std::variant checker (#76501)
Add a short documentation for `StdVariantChecker`.

---------

Co-authored-by: Balazs Benics <benicsbalazs@gmail.com>
Co-authored-by: whisperity <whisperity@gmail.com>
Co-authored-by: DonatNagyE <donat.nagy@ericsson.com>
2023-12-28 22:19:51 +01:00
Balazs Benics
d488adb7c2
[analyzer][docs] Update the release notes for llvm-18 (#76446)
This PR prepares the release notes of the Clang Static Analyzer for the
llvm-18 release branch, due in about a week.
See the regular [release schedule](https://llvm.org/docs/HowToReleaseLLVM.html#annual-release-schedule).

This patch was written after examining the relevant Static Analyzer
commits since the last release.
Have a look at the commits, and provide feedback if I missed anything
interesting.
Note that the release notes is not meant to be an exhaustive list of the
changes, but rather a curated list of the relevant changes that might
interest our stakeholders, such as tool vendors based on top of CSA or
users with custom checkers.

See the relevant commits by using this command:
```
git log --oneline llvmorg-18-init..llvm/main   clang/{lib/StaticAnalyzer,include/clang/StaticAnalyzer} | grep -v NFC | grep -v -i revert
```
2023-12-28 15:48:59 +01:00
DonatNagyE
c873f77e87
[analyzer] Move alpha checker EnumCastOutOfRange to optin (#67157)
The checker EnumCastOutOfRange verifies the (helpful, but not
standard-mandated) design rule that integer to enum casts should not
produce values that don't have a corresponding enumerator. As it was
improved and cleaned up by recent changes, this commit renames it from
`alpha.cplusplus.EnumCastOutOfRange` to `optin.core.EnumCastOutOfRange`
to reflect that it's no longer alpha quality.

As this checker handles a basic language feature (which is also present
in plain C), I moved it to a "core" subpackage within "optin".

In addition to the renaming, this commit cleans up the documentation in
`checkers.rst` and adds the new example code to a test file to ensure
that it's indeed producing the behavior claimend in the documentation.
2023-12-12 16:29:37 +01:00
Endre Fülöp
b98a594977
[clang][analyzer] Move security.cert.env.InvalidPtr out of alpha (#71912)
Thanks to recent improvements in #67663, InvalidPtr checker does not
emit any false positives on the following OS projects: memcached, tmux,
curl, twin, vim, openssl, sqlite, ffmpeg, postgres, tinyxml2, libwebm,
xerces, bitcoin, protobuf, qtbase, contour, acid, openrct2. (Before the
changes mentioned above, there were 27 reports, catching the `getenv`
invalidates previous `getenv` results cases. That strict behaviour is
disabled by default)
2023-11-24 10:02:56 +01:00
Balázs Kéri
72d3bf2b87
[clang][Analyzer] Move checker 'alpha.unix.Errno' to 'unix.Errno'. (#69469) 2023-11-21 13:34:03 +01:00
Endre Fülöp
f7a46d700f
[analyzer][clangsa] Add new option to alpha.security.cert.InvalidPtrChecker (#67663)
Introduce 'InvalidatingGetEnv' checker option for 'getenv' calls.

- POSIX suggests consecutive 'getenv' calls may invalidate 
  pointer pointers. This is often too strict in real-world scenarios.
- New 'InvalidatingGetEnv' checker option provides a more 
  pragmatic default that doesn't treat consecutive 'getenv' 
  calls as invalidating.
- Now also handles main function specifications with an 
  environment pointer as the third parameter.

Original Phabricator review:
https://reviews.llvm.org/D154603
2023-10-24 13:59:54 +02:00
Balázs Kéri
c202a17d02
[clang][analyzer] Move checker alpha.unix.StdCLibraryFunctions out of alpha. (#66207) 2023-10-16 14:51:05 +02:00