teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0681-lgtm-replace-the-query-used-for-looking-for-fgets-wi.patch

be0c12
From c4a34b71d4f51f071f7a722059e36388b41d30e4 Mon Sep 17 00:00:00 2001
be0c12
From: Evgeny Vereshchagin <evvers@ya.ru>
be0c12
Date: Mon, 11 Mar 2019 21:05:13 +0100
be0c12
Subject: [PATCH] lgtm: replace the query used for looking for fgets with a
be0c12
 more general query
be0c12
be0c12
to make it easier to comlain about `strtok` :-)
be0c12
be0c12
Inspired by https://github.com/systemd/systemd/pull/11963, which, in turn,
be0c12
was prompted by https://github.com/systemd/systemd/pull/11555.
be0c12
be0c12
(cherry picked from commit 7ba5ded9dbd7737bc368521f5ea7c90e5b06ab3e)
be0c12
be0c12
Related: #2017033
be0c12
---
be0c12
 .../PotentiallyDangerousFunction.ql           | 30 +++++++++++++++++++
be0c12
 .lgtm/cpp-queries/fgets.ql                    | 21 -------------
be0c12
 2 files changed, 30 insertions(+), 21 deletions(-)
be0c12
 create mode 100644 .lgtm/cpp-queries/PotentiallyDangerousFunction.ql
be0c12
 delete mode 100644 .lgtm/cpp-queries/fgets.ql
be0c12
be0c12
diff --git a/.lgtm/cpp-queries/PotentiallyDangerousFunction.ql b/.lgtm/cpp-queries/PotentiallyDangerousFunction.ql
be0c12
new file mode 100644
be0c12
index 0000000000..ba80f4ad8c
be0c12
--- /dev/null
be0c12
+++ b/.lgtm/cpp-queries/PotentiallyDangerousFunction.ql
be0c12
@@ -0,0 +1,30 @@
be0c12
+/**
be0c12
+ * @name Use of potentially dangerous function
be0c12
+ * @description Certain standard library functions are dangerous to call.
be0c12
+ * @kind problem
be0c12
+ * @problem.severity error
be0c12
+ * @precision high
be0c12
+ * @id cpp/potentially-dangerous-function
be0c12
+ * @tags reliability
be0c12
+ *       security
be0c12
+ *
be0c12
+ * Borrowed from
be0c12
+ * https://github.com/Semmle/ql/blob/master/cpp/ql/src/Security/CWE/CWE-676/PotentiallyDangerousFunction.ql
be0c12
+ */
be0c12
+import cpp
be0c12
+
be0c12
+predicate potentiallyDangerousFunction(Function f, string message) {
be0c12
+  (
be0c12
+    f.getQualifiedName() = "fgets" and
be0c12
+    message = "Call to fgets is potentially dangerous. Use read_line() instead."
be0c12
+  ) or (
be0c12
+    f.getQualifiedName() = "strtok" and
be0c12
+    message = "Call to strtok is potentially dangerous. Use extract_first_word() instead."
be0c12
+  )
be0c12
+}
be0c12
+
be0c12
+from FunctionCall call, Function target, string message
be0c12
+where
be0c12
+  call.getTarget() = target and
be0c12
+  potentiallyDangerousFunction(target, message)
be0c12
+select call, message
be0c12
diff --git a/.lgtm/cpp-queries/fgets.ql b/.lgtm/cpp-queries/fgets.ql
be0c12
deleted file mode 100644
be0c12
index a4181e4f3d..0000000000
be0c12
--- a/.lgtm/cpp-queries/fgets.ql
be0c12
+++ /dev/null
be0c12
@@ -1,21 +0,0 @@
be0c12
-/**
be0c12
- * @name Use of fgets()
be0c12
- * @description fgets() is dangerous to call. Use read_line() instead.
be0c12
- * @kind problem
be0c12
- * @problem.severity error
be0c12
- * @precision high
be0c12
- * @id cpp/fgets
be0c12
- * @tags reliability
be0c12
- *       security
be0c12
- */
be0c12
-import cpp
be0c12
-
be0c12
-predicate dangerousFunction(Function function) {
be0c12
-  exists (string name | name = function.getQualifiedName() |
be0c12
-    name = "fgets")
be0c12
-}
be0c12
-
be0c12
-from FunctionCall call, Function target
be0c12
-where call.getTarget() = target
be0c12
-  and dangerousFunction(target)
be0c12
-select call, target.getQualifiedName() + " is potentially dangerous"