Index: clang-tidy/android/AndroidTidyModule.cpp =================================================================== --- clang-tidy/android/AndroidTidyModule.cpp +++ clang-tidy/android/AndroidTidyModule.cpp @@ -12,6 +12,7 @@ #include "../ClangTidyModuleRegistry.h" #include "CloexecCreatCheck.h" #include "CloexecFopenCheck.h" +#include "CloexecInotifyInit1Check.h" #include "CloexecOpenCheck.h" #include "CloexecSocketCheck.h" @@ -27,6 +28,8 @@ void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck("android-cloexec-creat"); CheckFactories.registerCheck("android-cloexec-fopen"); + CheckFactories.registerCheck( + "android-cloexec-inotify-init1"); 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 @@ -4,6 +4,7 @@ AndroidTidyModule.cpp CloexecCreatCheck.cpp CloexecFopenCheck.cpp + CloexecInotifyInit1Check.cpp CloexecOpenCheck.cpp CloexecSocketCheck.cpp Index: clang-tidy/android/CloexecInotifyInit1Check.h =================================================================== --- /dev/null +++ clang-tidy/android/CloexecInotifyInit1Check.h @@ -0,0 +1,35 @@ +//===--- CloexecInotifyInit1Check.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_INOTIFY_INIT1_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_INOTIFY_INIT1_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace android { + +/// Finds code that uses inotify_init1() without using the IN_CLOEXEC flag. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-inotify-init1.html +class CloexecInotifyInit1Check : public ClangTidyCheck { +public: + CloexecInotifyInit1Check(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_INOTIFY_INIT1_H Index: clang-tidy/android/CloexecInotifyInit1Check.cpp =================================================================== --- /dev/null +++ clang-tidy/android/CloexecInotifyInit1Check.cpp @@ -0,0 +1,55 @@ +//===--- CloexecInotifyInit1Check.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 "CloexecInotifyInit1Check.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 *IN_CLOEXEC = "IN_CLOEXEC"; + +void CloexecInotifyInit1Check::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + callExpr(callee(functionDecl(isExternC(), returns(isInteger()), + hasName("inotify_init1"), + hasParameter(0, hasType(isInteger()))) + .bind("funcDecl"))) + .bind("inotifyInit1Fn"), + this); +} + +void CloexecInotifyInit1Check::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedCall = Result.Nodes.getNodeAs("inotifyInit1Fn"); + 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(), + IN_CLOEXEC)) + return; + + SourceLocation EndLoc = + Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getLocEnd()), 0, SM, + Result.Context->getLangOpts()); + + diag(EndLoc, "%0 should use %1 where possible") + << FD << IN_CLOEXEC + << FixItHint::CreateInsertion(EndLoc, (Twine(" | ") + IN_CLOEXEC).str()); +} + +} // namespace android +} // namespace tidy +} // namespace clang Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -73,6 +73,12 @@ Checks if the required mode ``e`` exists in the mode argument of ``fopen()``. +- New `android-cloexec-inotify-init1 + `_ check + + Checks if the required file flag ``IN_CLOEXEC`` is present in the argument of + ``inotify_init1()``. + - New `android-cloexec-socket `_ check Index: docs/clang-tidy/checks/android-cloexec-inotify-init1.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/android-cloexec-inotify-init1.rst @@ -0,0 +1,18 @@ +.. title:: clang-tidy - android-cloexec-inotify-init1 + +android-cloexec-inotify-init1 +============================= + +``inotify_init1()`` should include ``IN_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++ + + inotify_init1(IN_NONBLOCK); + + // becomes + + inotify_init1(IN_NONBLOCK | IN_CLOEXEC); Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -6,6 +6,7 @@ .. toctree:: android-cloexec-creat android-cloexec-fopen + android-cloexec-inotify-init1 android-cloexec-open android-cloexec-socket boost-use-to-string Index: test/clang-tidy/android-cloexec-inotify-init1.cpp =================================================================== --- /dev/null +++ test/clang-tidy/android-cloexec-inotify-init1.cpp @@ -0,0 +1,64 @@ +// RUN: %check_clang_tidy %s android-cloexec-inotify-init1 %t + +#define IN_NONBLOCK 1 +#define __O_CLOEXEC 3 +#define IN_CLOEXEC __O_CLOEXEC +#define TEMP_FAILURE_RETRY(exp) \ + ({ \ + int _rc; \ + do { \ + _rc = (exp); \ + } while (_rc == -1); \ + }) + +extern "C" int inotify_init1(int flags); + +void a() { + inotify_init1(IN_NONBLOCK); + // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: 'inotify_init1' should use IN_CLOEXEC where possible [android-cloexec-inotify-init1] + // CHECK-FIXES: inotify_init1(IN_NONBLOCK | IN_CLOEXEC) + TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK)); + // CHECK-MESSAGES: :[[@LINE-1]]:47: warning: 'inotify_init1' + // CHECK-FIXES: TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)) +} + +void f() { + inotify_init1(0); + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'inotify_init1' + // CHECK-FIXES: inotify_init1(IN_CLOEXEC) + TEMP_FAILURE_RETRY(inotify_init1(0)); + // CHECK-MESSAGES: :[[@LINE-1]]:37: warning: 'inotify_init1' + // CHECK-FIXES: TEMP_FAILURE_RETRY(inotify_init1(IN_CLOEXEC)) + + int flag = 1; + inotify_init1(flag); + TEMP_FAILURE_RETRY(inotify_init1(flag)); +} + +namespace i { +int inotify_init1(int flags); + +void d() { + inotify_init1(IN_NONBLOCK); + TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK)); +} + +} // namespace i + +void e() { + inotify_init1(IN_CLOEXEC); + TEMP_FAILURE_RETRY(inotify_init1(IN_CLOEXEC)); + inotify_init1(IN_NONBLOCK | IN_CLOEXEC); + TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); +} + +class G { +public: + int inotify_init1(int flags); + void d() { + inotify_init1(IN_CLOEXEC); + TEMP_FAILURE_RETRY(inotify_init1(IN_CLOEXEC)); + inotify_init1(IN_NONBLOCK | IN_CLOEXEC); + TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); + } +};