https://github.com/balazske updated https://github.com/llvm/llvm-project/pull/102636
From 11e871ab17c119c71b8ef4bbcd3b186ec0e9a14f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <balazs.k...@ericsson.com> Date: Fri, 9 Aug 2024 17:41:46 +0200 Subject: [PATCH 1/2] [clang][analyzer] Bring checker alpha.security.MmapWriteExec out of alpha package --- clang/docs/analyzer/checkers.rst | 32 +++++++++---------- .../clang/StaticAnalyzer/Checkers/Checkers.td | 8 ++--- clang/test/Analysis/mmap-writeexec.c | 4 +-- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst index 55832d20bd27a1..b77defe35f9043 100644 --- a/clang/docs/analyzer/checkers.rst +++ b/clang/docs/analyzer/checkers.rst @@ -1277,6 +1277,22 @@ security.insecureAPI.DeprecatedOrUnsafeBufferHandling (C) strncpy(buf, "a", 1); // warn } +.. _security-MmapWriteExec: + +security.MmapWriteExec (C) +"""""""""""""""""""""""""" +Warn on ``mmap()`` calls with both writable and executable access. + +.. code-block:: c + + void test(int n) { + void *c = mmap(NULL, 32, PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_PRIVATE | MAP_ANON, -1, 0); + // warn: Both PROT_WRITE and PROT_EXEC flags are set. This can lead to + // exploitable memory regions, which could be overwritten with malicious + // code + } + .. _security-putenv-stack-array: security.PutenvStackArray (C) @@ -2998,22 +3014,6 @@ Limitations: - It is an AST-based checker, thus it does not make use of the path-sensitive taint-analysis. -.. _alpha-security-MmapWriteExec: - -alpha.security.MmapWriteExec (C) -"""""""""""""""""""""""""""""""" -Warn on mmap() calls that are both writable and executable. - -.. code-block:: c - - void test(int n) { - void *c = mmap(NULL, 32, PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_PRIVATE | MAP_ANON, -1, 0); - // warn: Both PROT_WRITE and PROT_EXEC flags are set. This can lead to - // exploitable memory regions, which could be overwritten with malicious - // code - } - .. _alpha-security-ReturnPtrRange: alpha.security.ReturnPtrRange (C) diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td index 38b55a0eb0a7b0..b5ed3e0ba16452 100644 --- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td +++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td @@ -1000,6 +1000,10 @@ def FloatLoopCounter : Checker<"FloatLoopCounter">, Dependencies<[SecuritySyntaxChecker]>, Documentation<HasDocumentation>; +def MmapWriteExecChecker : Checker<"MmapWriteExec">, + HelpText<"Warn on mmap() calls with both writable and executable access">, + Documentation<HasDocumentation>; + def PutenvStackArray : Checker<"PutenvStackArray">, HelpText<"Finds calls to the function 'putenv' which pass a pointer to " "an automatic (stack-allocated) array as the argument.">, @@ -1043,10 +1047,6 @@ def MallocOverflowSecurityChecker : Checker<"MallocOverflow">, HelpText<"Check for overflows in the arguments to malloc()">, Documentation<HasDocumentation>; -def MmapWriteExecChecker : Checker<"MmapWriteExec">, - HelpText<"Warn on mmap() calls that are both writable and executable">, - Documentation<HasDocumentation>; - def ReturnPointerRangeChecker : Checker<"ReturnPtrRange">, HelpText<"Check for an out-of-bound pointer being returned to callers">, Documentation<HasDocumentation>; diff --git a/clang/test/Analysis/mmap-writeexec.c b/clang/test/Analysis/mmap-writeexec.c index 579cc75069eec7..bca34d167fbc92 100644 --- a/clang/test/Analysis/mmap-writeexec.c +++ b/clang/test/Analysis/mmap-writeexec.c @@ -1,5 +1,5 @@ -// RUN: %clang_analyze_cc1 -triple i686-unknown-linux -analyzer-checker=alpha.security.MmapWriteExec -DUSE_ALTERNATIVE_PROT_EXEC_DEFINITION -verify %s -// RUN: %clang_analyze_cc1 -triple x86_64-unknown-apple-darwin10 -analyzer-checker=alpha.security.MmapWriteExec -verify %s +// RUN: %clang_analyze_cc1 -triple i686-unknown-linux -analyzer-checker=security.MmapWriteExec -DUSE_ALTERNATIVE_PROT_EXEC_DEFINITION -verify %s +// RUN: %clang_analyze_cc1 -triple x86_64-unknown-apple-darwin10 -analyzer-checker=security.MmapWriteExec -verify %s #ifndef USE_ALTERNATIVE_PROT_EXEC_DEFINITION #define PROT_EXEC 0x01 From 4d1a4c333bfb264af97b8f4263db6130ac6f2da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <balazs.k...@ericsson.com> Date: Mon, 2 Sep 2024 11:20:21 +0200 Subject: [PATCH 2/2] remove MallocOverflow checker after merge --- clang/docs/analyzer/checkers.rst | 43 ------------------- .../clang/StaticAnalyzer/Checkers/Checkers.td | 4 -- 2 files changed, 47 deletions(-) diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst index 558644791a4397..847bf4baf74887 100644 --- a/clang/docs/analyzer/checkers.rst +++ b/clang/docs/analyzer/checkers.rst @@ -2983,49 +2983,6 @@ Warn about buffer overflows (newer checker). char c = s[x]; // warn: index is tainted } -.. _alpha-security-MallocOverflow: - -alpha.security.MallocOverflow (C) -""""""""""""""""""""""""""""""""" -Check for overflows in the arguments to ``malloc()``. -It tries to catch ``malloc(n * c)`` patterns, where: - - - ``n``: a variable or member access of an object - - ``c``: a constant foldable integral - -This checker was designed for code audits, so expect false-positive reports. -One is supposed to silence this checker by ensuring proper bounds checking on -the variable in question using e.g. an ``assert()`` or a branch. - -.. code-block:: c - - void test(int n) { - void *p = malloc(n * sizeof(int)); // warn - } - - void test2(int n) { - if (n > 100) // gives an upper-bound - return; - void *p = malloc(n * sizeof(int)); // no warning - } - - void test3(int n) { - assert(n <= 100 && "Contract violated."); - void *p = malloc(n * sizeof(int)); // no warning - } - -Limitations: - - - The checker won't warn for variables involved in explicit casts, - since that might limit the variable's domain. - E.g.: ``(unsigned char)int x`` would limit the domain to ``[0,255]``. - The checker will miss the true-positive cases when the explicit cast would - not tighten the domain to prevent the overflow in the subsequent - multiplication operation. - - - It is an AST-based checker, thus it does not make use of the - path-sensitive taint-analysis. - .. _alpha-security-ReturnPtrRange: alpha.security.ReturnPtrRange (C) diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td index b5ed3e0ba16452..585246547b3dce 100644 --- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td +++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td @@ -1043,10 +1043,6 @@ def ArrayBoundCheckerV2 : Checker<"ArrayBoundV2">, HelpText<"Warn about buffer overflows (newer checker)">, Documentation<HasDocumentation>; -def MallocOverflowSecurityChecker : Checker<"MallocOverflow">, - HelpText<"Check for overflows in the arguments to malloc()">, - Documentation<HasDocumentation>; - def ReturnPointerRangeChecker : Checker<"ReturnPtrRange">, HelpText<"Check for an out-of-bound pointer being returned to callers">, Documentation<HasDocumentation>; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits