[clang-tools-extra] r310858 - [clang-tidy] Add a close-on-exec check on dup() in Android module.
Chih-Hung Hsieh via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 14 10:04:16 PDT 2017
Author: chh
Date: Mon Aug 14 10:04:16 2017
New Revision: 310858
URL: http://llvm.org/viewvc/llvm-project?rev=310858&view=rev
Log:
[clang-tidy] Add a close-on-exec check on dup() in Android module.
Summary:
dup() is better to be replaced by fcntl() to avoid file descriptor leakage.
Differential Revision: https://reviews.llvm.org/D35364
Added:
clang-tools-extra/trunk/clang-tidy/android/CloexecDupCheck.cpp
clang-tools-extra/trunk/clang-tidy/android/CloexecDupCheck.h
clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-dup.rst
clang-tools-extra/trunk/test/clang-tidy/android-cloexec-dup.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp
clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/android/CloexecCheck.cpp
clang-tools-extra/trunk/clang-tidy/android/CloexecCheck.h
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=310858&r1=310857&r2=310858&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp Mon Aug 14 10:04:16 2017
@@ -11,6 +11,7 @@
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
#include "CloexecCreatCheck.h"
+#include "CloexecDupCheck.h"
#include "CloexecFopenCheck.h"
#include "CloexecMemfdCreateCheck.h"
#include "CloexecOpenCheck.h"
@@ -27,6 +28,7 @@ class AndroidModule : public ClangTidyMo
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat");
+ CheckFactories.registerCheck<CloexecDupCheck>("android-cloexec-dup");
CheckFactories.registerCheck<CloexecFopenCheck>("android-cloexec-fopen");
CheckFactories.registerCheck<CloexecMemfdCreateCheck>(
"android-cloexec-memfd-create");
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=310858&r1=310857&r2=310858&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt Mon Aug 14 10:04:16 2017
@@ -4,6 +4,7 @@ add_clang_library(clangTidyAndroidModule
AndroidTidyModule.cpp
CloexecCheck.cpp
CloexecCreatCheck.cpp
+ CloexecDupCheck.cpp
CloexecFopenCheck.cpp
CloexecMemfdCreateCheck.cpp
CloexecOpenCheck.cpp
Modified: clang-tools-extra/trunk/clang-tidy/android/CloexecCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecCheck.cpp?rev=310858&r1=310857&r2=310858&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecCheck.cpp Mon Aug 14 10:04:16 2017
@@ -100,6 +100,15 @@ void CloexecCheck::insertStringFlag(
ReplacementText);
}
+StringRef CloexecCheck::getSpellingArg(const MatchFinder::MatchResult &Result,
+ int N) const {
+ const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
+ const SourceManager &SM = *Result.SourceManager;
+ return Lexer::getSourceText(
+ CharSourceRange::getTokenRange(MatchedCall->getArg(N)->getSourceRange()),
+ SM, Result.Context->getLangOpts());
+}
+
} // namespace android
} // namespace tidy
} // namespace clang
Modified: clang-tools-extra/trunk/clang-tidy/android/CloexecCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecCheck.h?rev=310858&r1=310857&r2=310858&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecCheck.h Mon Aug 14 10:04:16 2017
@@ -86,6 +86,10 @@ protected:
/// \param ArgPos The 0-based position of the flag argument.
void insertStringFlag(const ast_matchers::MatchFinder::MatchResult &Result,
const char Mode, const int ArgPos);
+
+ /// Helper function to get the spelling of a particular argument.
+ StringRef getSpellingArg(const ast_matchers::MatchFinder::MatchResult &Result,
+ int N) const;
};
} // namespace android
Added: clang-tools-extra/trunk/clang-tidy/android/CloexecDupCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecDupCheck.cpp?rev=310858&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecDupCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecDupCheck.cpp Mon Aug 14 10:04:16 2017
@@ -0,0 +1,38 @@
+//===--- CloexecDupCheck.cpp - clang-tidy----------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "CloexecDupCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace android {
+
+void CloexecDupCheck::registerMatchers(MatchFinder *Finder) {
+ registerMatchersImpl(Finder,
+ functionDecl(returns(isInteger()), hasName("dup"),
+ hasParameter(0, hasType(isInteger()))));
+}
+
+void CloexecDupCheck::check(const MatchFinder::MatchResult &Result) {
+ const std::string &ReplacementText =
+ (Twine("fcntl(") + getSpellingArg(Result, 0) + ", F_DUPFD_CLOEXEC)")
+ .str();
+
+ replaceFunc(Result,
+ "prefer fcntl() to dup() because fcntl() allows F_DUPFD_CLOEXEC",
+ ReplacementText);
+}
+
+} // namespace android
+} // namespace tidy
+} // namespace clang
Added: clang-tools-extra/trunk/clang-tidy/android/CloexecDupCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecDupCheck.h?rev=310858&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecDupCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecDupCheck.h Mon Aug 14 10:04:16 2017
@@ -0,0 +1,36 @@
+//===--- CloexecDupCheck.h - clang-tidy-------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_DUP_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_DUP_H
+
+#include "CloexecCheck.h"
+
+namespace clang {
+namespace tidy {
+namespace android {
+
+/// dup() is better to be replaced by fcntl(), which has close-on-exec flag.
+/// Find the usage of dup() and redirect user to use fcntl().
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-dup.html
+class CloexecDupCheck : public CloexecCheck {
+public:
+ CloexecDupCheck(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_DUP_H
Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=310858&r1=310857&r2=310858&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Mon Aug 14 10:04:16 2017
@@ -70,6 +70,11 @@ Improvements to clang-tidy
``AllowConditionalIntegerCasts`` -> ``AllowIntegerConditions``,
``AllowConditionalPointerCasts`` -> ``AllowPointerConditions``.
+- New `android-cloexec-dup
+ <http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-dup.html>`_ check
+
+ Detects usage of ``dup()``.
+
- New `android-cloexec-memfd_create
<http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-memfd_create.html>`_ check
Added: clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-dup.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-dup.rst?rev=310858&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-dup.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-dup.rst Mon Aug 14 10:04:16 2017
@@ -0,0 +1,18 @@
+.. title:: clang-tidy - android-cloexec-dup
+
+android-cloexec-dup
+===================
+
+The usage of ``dup()`` is not recommended, it's better to use ``fcntl()``,
+which can set the close-on-exec flag. Otherwise, an opened sensitive file would
+remain open across a fork+exec to a lower-privileged SELinux domain.
+
+Examples:
+
+.. code-block:: c++
+
+ int fd = dup(oldfd);
+
+ // becomes
+
+ int fd = fcntl(oldfd, F_DUPFD_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=310858&r1=310857&r2=310858&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Mon Aug 14 10:04:16 2017
@@ -5,6 +5,7 @@ Clang-Tidy Checks
.. toctree::
android-cloexec-creat
+ android-cloexec-dup
android-cloexec-fopen
android-cloexec-memfd-create
android-cloexec-open
Added: clang-tools-extra/trunk/test/clang-tidy/android-cloexec-dup.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/android-cloexec-dup.cpp?rev=310858&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/android-cloexec-dup.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/android-cloexec-dup.cpp Mon Aug 14 10:04:16 2017
@@ -0,0 +1,31 @@
+// RUN: %check_clang_tidy %s android-cloexec-dup %t
+
+extern "C" int dup(int oldfd);
+void f() {
+ dup(1);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer fcntl() to dup() because fcntl() allows F_DUPFD_CLOEXEC [android-cloexec-dup]
+ // CHECK-FIXES: fcntl(1, F_DUPFD_CLOEXEC);
+ int oldfd = 0;
+ dup(oldfd);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer
+ // CHECK-FIXES: fcntl(oldfd, F_DUPFD_CLOEXEC);
+}
+
+namespace i {
+int dup(int oldfd);
+void g() {
+ dup(0);
+ int oldfd = 1;
+ dup(oldfd);
+}
+} // namespace i
+
+class C {
+public:
+ int dup(int oldfd);
+ void h() {
+ dup(0);
+ int oldfd = 1;
+ dup(oldfd);
+ }
+};
More information about the cfe-commits
mailing list