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 "CloexecEpollCreate1Check.h" #include "CloexecFopenCheck.h" #include "CloexecOpenCheck.h" #include "CloexecSocketCheck.h" @@ -26,6 +27,8 @@ public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck("android-cloexec-creat"); + CheckFactories.registerCheck( + "android-cloexec-epoll-create1"); 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 + CloexecEpollCreate1Check.cpp CloexecFopenCheck.cpp CloexecOpenCheck.cpp CloexecSocketCheck.cpp Index: clang-tidy/android/CloexecEpollCreate1Check.h =================================================================== --- /dev/null +++ clang-tidy/android/CloexecEpollCreate1Check.h @@ -0,0 +1,35 @@ +//===--- CloexecEpollCreate1Check.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_EPOLL_CREATE1_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_EPOLL_CREATE1_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace android { + +/// Finds code that uses epoll_create1() without using the EPOLL_CLOEXEC flag. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-epoll-create1.html +class CloexecEpollCreate1Check : public ClangTidyCheck { +public: + CloexecEpollCreate1Check(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_EPOLL_CREATE1_H Index: clang-tidy/android/CloexecEpollCreate1Check.cpp =================================================================== --- /dev/null +++ clang-tidy/android/CloexecEpollCreate1Check.cpp @@ -0,0 +1,56 @@ +//===--- CloexecEpollCreate1Check.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 "CloexecEpollCreate1Check.h" +#include "../utils/ASTUtils.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace android { + +static constexpr const char *EPOLL_CLOEXEC = "EPOLL_CLOEXEC"; + +void CloexecEpollCreate1Check::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + callExpr(callee(functionDecl(isExternC(), returns(isInteger()), + hasName("epoll_create1"), + hasParameter(0, hasType(isInteger()))) + .bind("funcDecl"))) + .bind("epollCreate1Fn"), + this); +} + +void CloexecEpollCreate1Check::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedCall = Result.Nodes.getNodeAs("epollCreate1Fn"); + const auto *FD = Result.Nodes.getNodeAs("funcDecl"); + const Expr *FlagArg = MatchedCall->getArg(0); + SourceManager &SM = *Result.SourceManager; + + if (utils::exprHasBitFlagWithSpelling(FlagArg->IgnoreParenCasts(), SM, + Result.Context->getLangOpts(), + EPOLL_CLOEXEC)) + return; + + SourceLocation EndLoc = + Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getLocEnd()), 0, SM, + Result.Context->getLangOpts()); + + diag(EndLoc, "%0 should use %1 where possible") + << FD << EPOLL_CLOEXEC + << FixItHint::CreateInsertion(EndLoc, + (Twine(" | ") + EPOLL_CLOEXEC).str()); +} + +} // namespace android +} // namespace tidy +} // namespace clang Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -57,6 +57,12 @@ Improvements to clang-tidy -------------------------- +- New `android-cloexec-epoll-create1 + `_ check + + Checks if the required file flag ``EPOLL_CLOEXEC`` is present in the argument of + ``epoll_create1()``. + - New `android-cloexec-creat `_ check Index: docs/clang-tidy/checks/android-cloexec-epoll-create1.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/android-cloexec-epoll-create1.rst @@ -0,0 +1,18 @@ +.. title:: clang-tidy - android-cloexec-epoll-create1 + +android-cloexec-epoll-create1 +============================= + +``epoll_create1()`` should include ``EPOLL_CLOEXEC`` in its type argument to +avoid the file descriptor leakage. Without this flag, an opened sensitive file +would remain open across a fork+exec to a lower-privileged SELinux domain. + +Examples: + +.. code-block:: c++ + + epoll_create1(0); + + // becomes + + epoll_create1(EPOLL_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-epoll-create1 android-cloexec-fopen android-cloexec-open android-cloexec-socket Index: test/clang-tidy/android-cloexec-epoll-create1.cpp =================================================================== --- /dev/null +++ test/clang-tidy/android-cloexec-epoll-create1.cpp @@ -0,0 +1,59 @@ +// RUN: %check_clang_tidy %s android-cloexec-epoll-create1 %t + +#define __O_CLOEXEC 3 +#define EPOLL_CLOEXEC __O_CLOEXEC +#define TEMP_FAILURE_RETRY(exp) \ + ({ \ + int _rc; \ + do { \ + _rc = (exp); \ + } while (_rc == -1); \ + }) + +extern "C" int epoll_create1(int flags); + +void a() { + epoll_create1(0); + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'epoll_create1' should use EPOLL_CLOEXEC where possible [android-cloexec-epoll-create1] + // CHECK-FIXES: epoll_create1(EPOLL_CLOEXEC) + TEMP_FAILURE_RETRY(epoll_create1(0)); + // CHECK-MESSAGES: :[[@LINE-1]]:37: warning: 'epoll_create1' + // CHECK-FIXES: TEMP_FAILURE_RETRY(epoll_create1(EPOLL_CLOEXEC)) +} + +void f() { + epoll_create1(3); + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'epoll_create1' + // CHECK-FIXES: epoll_create1(EPOLL_CLOEXEC) + TEMP_FAILURE_RETRY(epoll_create1(3)); + // CHECK-MESSAGES: :[[@LINE-1]]:37: warning: 'epoll_create1' + // CHECK-FIXES: TEMP_FAILURE_RETRY(epoll_create1(EPOLL_CLOEXEC)) + + int flag = 0; + epoll_create1(EPOLL_CLOEXEC); + TEMP_FAILURE_RETRY(epoll_create1(EPOLL_CLOEXEC)); +} + +namespace i { +int epoll_create1(int flags); + +void d() { + epoll_create1(0); + TEMP_FAILURE_RETRY(epoll_create1(0)); +} + +} // namespace i + +void e() { + epoll_create1(EPOLL_CLOEXEC); + TEMP_FAILURE_RETRY(epoll_create1(EPOLL_CLOEXEC)); +} + +class G { +public: + int epoll_create1(int flags); + void d() { + epoll_create1(EPOLL_CLOEXEC); + TEMP_FAILURE_RETRY(epoll_create1(EPOLL_CLOEXEC)); + } +};