Author: chh Date: Mon Aug 14 10:45:48 2017 New Revision: 310863 URL: http://llvm.org/viewvc/llvm-project?rev=310863&view=rev Log: [clang-tidy] Add a close-on-exec check on inotify_init1() in Android module.
Summary: inotify_init1() is better to set IN_CLOEXEC flag to avoid file descriptor leakage. Differential Revision: https://reviews.llvm.org/D35368 Added: clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInit1Check.cpp clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInit1Check.h clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-inotify-init1.rst clang-tools-extra/trunk/test/clang-tidy/android-cloexec-inotify-init1.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=310863&r1=310862&r2=310863&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:45:48 2017 @@ -13,6 +13,7 @@ #include "CloexecCreatCheck.h" #include "CloexecDupCheck.h" #include "CloexecFopenCheck.h" +#include "CloexecInotifyInit1Check.h" #include "CloexecInotifyInitCheck.h" #include "CloexecMemfdCreateCheck.h" #include "CloexecOpenCheck.h" @@ -33,6 +34,8 @@ public: CheckFactories.registerCheck<CloexecFopenCheck>("android-cloexec-fopen"); CheckFactories.registerCheck<CloexecInotifyInitCheck>( "android-cloexec-inotify-init"); + CheckFactories.registerCheck<CloexecInotifyInit1Check>( + "android-cloexec-inotify-init1"); CheckFactories.registerCheck<CloexecMemfdCreateCheck>( "android-cloexec-memfd-create"); CheckFactories.registerCheck<CloexecOpenCheck>("android-cloexec-open"); 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=310863&r1=310862&r2=310863&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:45:48 2017 @@ -6,6 +6,7 @@ add_clang_library(clangTidyAndroidModule CloexecCreatCheck.cpp CloexecDupCheck.cpp CloexecFopenCheck.cpp + CloexecInotifyInit1Check.cpp CloexecInotifyInitCheck.cpp CloexecMemfdCreateCheck.cpp CloexecOpenCheck.cpp Added: clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInit1Check.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInit1Check.cpp?rev=310863&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInit1Check.cpp (added) +++ clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInit1Check.cpp Mon Aug 14 10:45:48 2017 @@ -0,0 +1,33 @@ +//===--- 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 { + +void CloexecInotifyInit1Check::registerMatchers(MatchFinder *Finder) { + registerMatchersImpl( + Finder, functionDecl(returns(isInteger()), hasName("inotify_init1"), + hasParameter(0, hasType(isInteger())))); +} + +void CloexecInotifyInit1Check::check(const MatchFinder::MatchResult &Result) { + insertMacroFlag(Result, /*MarcoFlag=*/"IN_CLOEXEC", /*ArgPos=*/0); +} + +} // namespace android +} // namespace tidy +} // namespace clang Added: clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInit1Check.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInit1Check.h?rev=310863&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInit1Check.h (added) +++ clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInit1Check.h Mon Aug 14 10:45:48 2017 @@ -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 "CloexecCheck.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 CloexecCheck { +public: + CloexecInotifyInit1Check(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_INOTIFY_INIT1_H Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=310863&r1=310862&r2=310863&view=diff ============================================================================== --- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original) +++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Mon Aug 14 10:45:48 2017 @@ -98,6 +98,12 @@ Improvements to clang-tidy Ensures that all exception will be instances of ``std::exception`` and classes that are derived from it. +- New `android-cloexec-inotify-init1 + <http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-inotify-init1.html>`_ check + + Checks if the required file flag ``IN_CLOEXEC`` is present in the argument of + ``inotify_init1()``. + - New `readability-static-accessed-through-instance <http://clang.llvm.org/extra/clang-tidy/checks/readability-static-accessed-through-instance.html>`_ check Added: clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-inotify-init1.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-inotify-init1.rst?rev=310863&view=auto ============================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-inotify-init1.rst (added) +++ clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-inotify-init1.rst Mon Aug 14 10:45:48 2017 @@ -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); 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=310863&r1=310862&r2=310863&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:45:48 2017 @@ -8,6 +8,7 @@ Clang-Tidy Checks android-cloexec-dup android-cloexec-fopen android-cloexec-inotify-init + android-cloexec-inotify-init1 android-cloexec-memfd-create android-cloexec-open android-cloexec-socket Added: clang-tools-extra/trunk/test/clang-tidy/android-cloexec-inotify-init1.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/android-cloexec-inotify-init1.cpp?rev=310863&view=auto ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/android-cloexec-inotify-init1.cpp (added) +++ clang-tools-extra/trunk/test/clang-tidy/android-cloexec-inotify-init1.cpp Mon Aug 14 10:45:48 2017 @@ -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)); + } +}; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits