dcavalca / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone
17aa40
From f9b19c9d4caaf870b30cce8a3d6be79eda099c4e Mon Sep 17 00:00:00 2001
17aa40
From: Frantisek Sumsal <frantisek@sumsal.cz>
17aa40
Date: Sun, 5 Dec 2021 16:11:35 +0100
17aa40
Subject: [PATCH] lgtm: detect more possible problematic scenarios
17aa40
17aa40
1) don't ignore stack-allocated variables, since they may hide
17aa40
   heap-allocated stuff (compound types)
17aa40
2) check if there's a return between the variable declaration and its
17aa40
   initialization; if so, treat the variable as uninitialized
17aa40
3) introduction of 2) increased the query runtime exponentially, so
17aa40
   introduce some optimizations to bring it back to some reasonable
17aa40
   values
17aa40
17aa40
(cherry picked from commit c8fec8bf9b086f9fc7638db0f1a613a00d7c63a3)
17aa40
17aa40
Related: #2017033
17aa40
---
17aa40
 .../UninitializedVariableWithCleanup.ql       | 48 ++++++++++---------
17aa40
 1 file changed, 25 insertions(+), 23 deletions(-)
17aa40
17aa40
diff --git a/.lgtm/cpp-queries/UninitializedVariableWithCleanup.ql b/.lgtm/cpp-queries/UninitializedVariableWithCleanup.ql
17aa40
index 8c24b6d8f1..6b3b62f8bc 100644
17aa40
--- a/.lgtm/cpp-queries/UninitializedVariableWithCleanup.ql
17aa40
+++ b/.lgtm/cpp-queries/UninitializedVariableWithCleanup.ql
17aa40
@@ -16,24 +16,6 @@
17aa40
 import cpp
17aa40
 import semmle.code.cpp.controlflow.StackVariableReachability
17aa40
 
17aa40
-/**
17aa40
- * Auxiliary predicate: Types that don't require initialization
17aa40
- * before they are used, since they're stack-allocated.
17aa40
- */
17aa40
-predicate allocatedType(Type t) {
17aa40
-  /* Arrays: "int foo[1]; foo[0] = 42;" is ok. */
17aa40
-  t instanceof ArrayType
17aa40
-  or
17aa40
-  /* Structs: "struct foo bar; bar.baz = 42" is ok. */
17aa40
-  t instanceof Class
17aa40
-  or
17aa40
-  /* Typedefs to other allocated types are fine. */
17aa40
-  allocatedType(t.(TypedefType).getUnderlyingType())
17aa40
-  or
17aa40
-  /* Type specifiers don't affect whether or not a type is allocated. */
17aa40
-  allocatedType(t.getUnspecifiedType())
17aa40
-}
17aa40
-
17aa40
 /** Auxiliary predicate: List cleanup functions we want to explicitly ignore
17aa40
   * since they don't do anything illegal even when the variable is uninitialized
17aa40
   */
17aa40
@@ -47,13 +29,11 @@ predicate cleanupFunctionDenyList(string fun) {
17aa40
  */
17aa40
 DeclStmt declWithNoInit(LocalVariable v) {
17aa40
   result.getADeclaration() = v and
17aa40
-  not exists(v.getInitializer()) and
17aa40
+  not v.hasInitializer() and
17aa40
   /* The variable has __attribute__((__cleanup__(...))) set */
17aa40
   v.getAnAttribute().hasName("cleanup") and
17aa40
   /* Check if the cleanup function is not on a deny list */
17aa40
-  not exists(Attribute a | a = v.getAnAttribute() and a.getName() = "cleanup" | cleanupFunctionDenyList(a.getAnArgument().getValueText())) and
17aa40
-  /* The type of the variable is not stack-allocated. */
17aa40
-  exists(Type t | t = v.getType() | not allocatedType(t))
17aa40
+  not cleanupFunctionDenyList(v.getAnAttribute().getAnArgument().getValueText())
17aa40
 }
17aa40
 
17aa40
 class UninitialisedLocalReachability extends StackVariableReachability {
17aa40
@@ -78,7 +58,29 @@ class UninitialisedLocalReachability extends StackVariableReachability {
17aa40
   override predicate isBarrier(ControlFlowNode node, StackVariable v) {
17aa40
     // only report the _first_ possibly uninitialized use
17aa40
     useOfVar(v, node) or
17aa40
-    definitionBarrier(v, node)
17aa40
+    (
17aa40
+      /* If there's an return statement somewhere between the variable declaration
17aa40
+       * and a possible definition, don't accept is as a valid initialization.
17aa40
+       *
17aa40
+       * E.g.:
17aa40
+       * _cleanup_free_ char *x;
17aa40
+       * ...
17aa40
+       * if (...)
17aa40
+       *    return;
17aa40
+       * ...
17aa40
+       * x = malloc(...);
17aa40
+       *
17aa40
+       * is not a valid initialization, since we might return from the function
17aa40
+       * _before_ the actual iniitialization (emphasis on _might_, since we
17aa40
+       * don't know if the return statement might ever evaluate to true).
17aa40
+       */
17aa40
+      definitionBarrier(v, node) and
17aa40
+      not exists(ReturnStmt rs |
17aa40
+                 /* The attribute check is "just" a complexity optimization */
17aa40
+                 v.getFunction() = rs.getEnclosingFunction() and v.getAnAttribute().hasName("cleanup") |
17aa40
+                 rs.getLocation().isBefore(node.getLocation())
17aa40
+      )
17aa40
+    )
17aa40
   }
17aa40
 }
17aa40