dcavalca / rpms / systemd

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