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