[clang-tools-extra] r362673 - android: add a close-on-exec check on pipe()

George Burgess IV via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 5 22:21:46 PDT 2019


Author: gbiv
Date: Wed Jun  5 22:21:45 2019
New Revision: 362673

URL: http://llvm.org/viewvc/llvm-project?rev=362673&view=rev
Log:
android: add a close-on-exec check on pipe()

On Android, pipe() is better to be replaced by pipe2() with O_CLOEXEC
flag to avoid file descriptor leakage.

Patch by Jian Cai!

Differential Revision: https://reviews.llvm.org/D61967

Added:
    clang-tools-extra/trunk/clang-tidy/android/CloexecPipeCheck.cpp
    clang-tools-extra/trunk/clang-tidy/android/CloexecPipeCheck.h
    clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-pipe.rst
    clang-tools-extra/trunk/test/clang-tidy/android-cloexec-pipe.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp
    clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt
    clang-tools-extra/trunk/docs/ReleaseNotes.rst
    clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst

Modified: clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp?rev=362673&r1=362672&r2=362673&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp Wed Jun  5 22:21:45 2019
@@ -20,6 +20,7 @@
 #include "CloexecInotifyInitCheck.h"
 #include "CloexecMemfdCreateCheck.h"
 #include "CloexecOpenCheck.h"
+#include "CloexecPipeCheck.h"
 #include "CloexecPipe2Check.h"
 #include "CloexecSocketCheck.h"
 #include "ComparisonInTempFailureRetryCheck.h"
@@ -50,6 +51,7 @@ public:
     CheckFactories.registerCheck<CloexecMemfdCreateCheck>(
         "android-cloexec-memfd-create");
     CheckFactories.registerCheck<CloexecOpenCheck>("android-cloexec-open");
+    CheckFactories.registerCheck<CloexecPipeCheck>("android-cloexec-pipe");
     CheckFactories.registerCheck<CloexecPipe2Check>("android-cloexec-pipe2");
     CheckFactories.registerCheck<CloexecSocketCheck>("android-cloexec-socket");
     CheckFactories.registerCheck<ComparisonInTempFailureRetryCheck>(

Modified: clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt?rev=362673&r1=362672&r2=362673&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt Wed Jun  5 22:21:45 2019
@@ -14,6 +14,7 @@ add_clang_library(clangTidyAndroidModule
   CloexecInotifyInitCheck.cpp
   CloexecMemfdCreateCheck.cpp
   CloexecOpenCheck.cpp
+  CloexecPipeCheck.cpp
   CloexecPipe2Check.cpp
   CloexecSocketCheck.cpp
   ComparisonInTempFailureRetryCheck.cpp

Added: clang-tools-extra/trunk/clang-tidy/android/CloexecPipeCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecPipeCheck.cpp?rev=362673&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecPipeCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecPipeCheck.cpp Wed Jun  5 22:21:45 2019
@@ -0,0 +1,37 @@
+//===--- CloexecPipeCheck.cpp - clang-tidy---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "CloexecPipeCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace android {
+
+void CloexecPipeCheck::registerMatchers(MatchFinder *Finder) {
+  registerMatchersImpl(Finder,
+                       functionDecl(returns(isInteger()), hasName("pipe"),
+                                    hasParameter(0, hasType(pointsTo(isInteger())))));
+}
+
+void CloexecPipeCheck::check(const MatchFinder::MatchResult &Result) {
+  std::string ReplacementText =
+      (Twine("pipe2(") + getSpellingArg(Result, 0) + ", O_CLOEXEC)").str();
+
+  replaceFunc(
+      Result,
+      "prefer pipe2() with O_CLOEXEC to avoid leaking file descriptors to child processes",
+      ReplacementText);
+}
+
+} // namespace android
+} // namespace tidy
+} // namespace clang

Added: clang-tools-extra/trunk/clang-tidy/android/CloexecPipeCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecPipeCheck.h?rev=362673&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecPipeCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecPipeCheck.h Wed Jun  5 22:21:45 2019
@@ -0,0 +1,34 @@
+//===--- CloexecPipeCheck.h - clang-tidy-------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_PIPE_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_PIPE_H
+
+#include "CloexecCheck.h"
+
+namespace clang {
+namespace tidy {
+namespace android {
+
+/// Suggests to replace calls to pipe() with calls to pipe2().
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-pipe.html
+class CloexecPipeCheck : public CloexecCheck {
+public:
+  CloexecPipeCheck(StringRef Name, ClangTidyContext *Context)
+      : CloexecCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace android
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_PIPE_H

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=362673&r1=362672&r2=362673&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Wed Jun  5 22:21:45 2019
@@ -101,6 +101,11 @@ Improvements to clang-tidy
   Finds and fixes ``absl::Time`` subtraction expressions to do subtraction
   in the Time domain instead of the numeric domain.
 
+- New :doc:`android-cloexec-pipe
+  <clang-tidy/checks/android-cloexec-pipe>` check.
+
+  This check detects usage of ``pipe()``.
+
 - New :doc:`android-cloexec-pipe2
   <clang-tidy/checks/android-cloexec-pipe2>` check.
 

Added: clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-pipe.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-pipe.rst?rev=362673&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-pipe.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-pipe.rst Wed Jun  5 22:21:45 2019
@@ -0,0 +1,20 @@
+.. title:: clang-tidy - android-cloexec-pipe
+
+android-cloexec-pipe
+====================
+
+This check detects usage of ``pipe()``. Using ``pipe()`` is not recommended, ``pipe2()`` is the
+suggested replacement. The check also adds the O_CLOEXEC flag that marks the file descriptor to
+be closed in child processes. Without this flag a sensitive file descriptor can be leaked to a
+child process, potentially into a lower-privileged SELinux domain.
+
+Examples:
+
+.. code-block:: c++
+
+  pipe(pipefd);
+
+Suggested replacement:
+
+.. code-block:: c++
+  pipe2(pipefd, O_CLOEXEC);

Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst?rev=362673&r1=362672&r2=362673&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Wed Jun  5 22:21:45 2019
@@ -32,6 +32,7 @@ Clang-Tidy Checks
    android-cloexec-inotify-init1
    android-cloexec-memfd-create
    android-cloexec-open
+   android-cloexec-pipe
    android-cloexec-pipe2
    android-cloexec-socket
    android-comparison-in-temp-failure-retry

Added: clang-tools-extra/trunk/test/clang-tidy/android-cloexec-pipe.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/android-cloexec-pipe.cpp?rev=362673&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/android-cloexec-pipe.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/android-cloexec-pipe.cpp Wed Jun  5 22:21:45 2019
@@ -0,0 +1,27 @@
+// RUN: %check_clang_tidy %s android-cloexec-pipe %t
+
+extern "C" int pipe(int pipefd[2]);
+
+void warning() {
+  int pipefd[2];
+  pipe(pipefd);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer pipe2() with O_CLOEXEC to avoid leaking file descriptors to child processes [android-cloexec-pipe]
+  // CHECK-FIXES: pipe2(pipefd, O_CLOEXEC);
+}
+
+namespace i {
+int pipe(int pipefd[2]);
+void noWarningInNamespace() {
+  int pipefd[2];
+  pipe(pipefd);
+}
+} // namespace i
+
+class C {
+public:
+  int pipe(int pipefd[2]);
+  void noWarningForMemberFunction() {
+    int pipefd[2];
+    pipe(pipefd);
+  }
+};




More information about the cfe-commits mailing list