Index: clang-tidy/android/AndroidTidyModule.cpp =================================================================== --- clang-tidy/android/AndroidTidyModule.cpp +++ clang-tidy/android/AndroidTidyModule.cpp @@ -11,6 +11,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "CloexecCreatCheck.h" +#include "CloexecDupCheck.h" #include "CloexecFopenCheck.h" #include "CloexecOpenCheck.h" #include "CloexecSocketCheck.h" @@ -26,6 +27,7 @@ public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck("android-cloexec-creat"); + CheckFactories.registerCheck("android-cloexec-dup"); CheckFactories.registerCheck("android-cloexec-fopen"); CheckFactories.registerCheck("android-cloexec-open"); CheckFactories.registerCheck("android-cloexec-socket"); Index: clang-tidy/android/CMakeLists.txt =================================================================== --- clang-tidy/android/CMakeLists.txt +++ clang-tidy/android/CMakeLists.txt @@ -3,6 +3,7 @@ add_clang_library(clangTidyAndroidModule AndroidTidyModule.cpp CloexecCreatCheck.cpp + CloexecDupCheck.cpp CloexecFopenCheck.cpp CloexecOpenCheck.cpp CloexecSocketCheck.cpp Index: clang-tidy/android/CloexecDupCheck.h =================================================================== --- /dev/null +++ clang-tidy/android/CloexecDupCheck.h @@ -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 "../ClangTidy.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 ClangTidyCheck { +public: + CloexecDupCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(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 Index: clang-tidy/android/CloexecDupCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/android/CloexecDupCheck.cpp @@ -0,0 +1,50 @@ +//===--- 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) { + Finder->addMatcher( + callExpr( + callee(functionDecl(isExternC(), returns(isInteger()), hasName("dup"), + hasParameter(0, hasType(isInteger()))) + .bind("funcDecl"))) + .bind("dupFn"), + this); +} + +void CloexecDupCheck::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedCall = Result.Nodes.getNodeAs("dupFn"); + const SourceManager &SM = *Result.SourceManager; + + const std::string &ReplacementText = + (Twine("fcntl(") + + Lexer::getSourceText(CharSourceRange::getTokenRange( + MatchedCall->getArg(0)->getSourceRange()), + SM, Result.Context->getLangOpts()) + + ", F_DUPFD_CLOEXEC)") + .str(); + + diag(MatchedCall->getLocStart(), + "prefer fcntl() to dup() because fcntl() allows F_DUPFD_CLOEXEC") + << FixItHint::CreateReplacement(MatchedCall->getSourceRange(), + ReplacementText); +} + +} // namespace android +} // namespace tidy +} // namespace clang Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -62,6 +62,11 @@ Detect usage of ``creat()``. +- New `android-cloexec-dup + `_ check + + Detects usage of ``dup()``. + - New `android-cloexec-open `_ check Index: docs/clang-tidy/checks/android-cloexec-dup.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/android-cloexec-dup.rst @@ -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); Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -5,6 +5,7 @@ .. toctree:: android-cloexec-creat + android-cloexec-dup android-cloexec-fopen android-cloexec-open android-cloexec-socket Index: test/clang-tidy/android-cloexec-dup.cpp =================================================================== --- /dev/null +++ test/clang-tidy/android-cloexec-dup.cpp @@ -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); + } +};