Index: clang-tidy/android/AndroidTidyModule.cpp =================================================================== --- clang-tidy/android/AndroidTidyModule.cpp +++ clang-tidy/android/AndroidTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidy.h" #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" +#include "CloexecAcceptCheck.h" #include "CloexecCreatCheck.h" #include "CloexecDupCheck.h" #include "CloexecFopenCheck.h" @@ -27,6 +28,7 @@ class AndroidModule : public ClangTidyModule { public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { + CheckFactories.registerCheck("android-cloexec-accept"); CheckFactories.registerCheck("android-cloexec-creat"); CheckFactories.registerCheck("android-cloexec-dup"); CheckFactories.registerCheck("android-cloexec-fopen"); Index: clang-tidy/android/CMakeLists.txt =================================================================== --- clang-tidy/android/CMakeLists.txt +++ clang-tidy/android/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyAndroidModule AndroidTidyModule.cpp + CloexecAcceptCheck.cpp CloexecCheck.cpp CloexecCreatCheck.cpp CloexecDupCheck.cpp Index: clang-tidy/android/CloexecAcceptCheck.h =================================================================== --- /dev/null +++ clang-tidy/android/CloexecAcceptCheck.h @@ -0,0 +1,35 @@ +//===--- CloexecAcceptCheck.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_ACCEPT_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_ACCEPT_H + +#include "CloexecCheck.h" + +namespace clang { +namespace tidy { +namespace android { + +/// accept() is better to be replaced by accept4(). +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-accept.html +class CloexecAcceptCheck : public CloexecCheck { +public: + CloexecAcceptCheck(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_ACCEPT_H Index: clang-tidy/android/CloexecAcceptCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/android/CloexecAcceptCheck.cpp @@ -0,0 +1,47 @@ +//===--- CloexecAcceptCheck.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 "CloexecAcceptCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace android { + +void CloexecAcceptCheck::registerMatchers(MatchFinder *Finder) { + auto SockAddrPointerType = + hasType(pointsTo(recordDecl(isStruct(), hasName("sockaddr")))); + auto SockLenPointerType = hasType(pointsTo(namedDecl(hasName("socklen_t")))); + + registerMatchersImpl(Finder, + functionDecl(returns(isInteger()), hasName("accept"), + hasParameter(0, hasType(isInteger())), + hasParameter(1, SockAddrPointerType), + hasParameter(2, SockLenPointerType))); +} + +void CloexecAcceptCheck::check(const MatchFinder::MatchResult &Result) { + const std::string &ReplacementText = + (Twine("accept4(") + getSpellingArg(Result, 0) + ", " + + getSpellingArg(Result, 1) + ", " + getSpellingArg(Result, 2) + + ", SOCK_CLOEXEC)") + .str(); + + replaceFunc( + Result, /*WarningMsg=*/ + "prefer accept4() to accept() because accept4() allows SOCK_CLOEXEC", + /*FixMsg=*/ReplacementText); +} + +} // namespace android +} // namespace tidy +} // namespace clang Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -70,6 +70,11 @@ ``AllowConditionalIntegerCasts`` -> ``AllowIntegerConditions``, ``AllowConditionalPointerCasts`` -> ``AllowPointerConditions``. +- New `android-cloexec-accept + `_ check + + Detects usage of ``accept()``. + - New `android-cloexec-dup `_ check Index: docs/clang-tidy/checks/android-cloexec-accept.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/android-cloexec-accept.rst @@ -0,0 +1,18 @@ +.. title:: clang-tidy - android-cloexec-accept + +android-cloexec-accept +====================== + +The usage of ``accept()`` is not recommended, it's better to use ``accept4()``. +Without this flag, an opened sensitive file descriptor would remain open across +a fork+exec to a lower-privileged SELinux domain. + +Examples: + +.. code-block:: c++ + + accept(sockfd, addr, addrlen); + + // becomes + + accept4(sockfd, addr, addrlen, SOCK_CLOEXEC); Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -4,6 +4,7 @@ ================= .. toctree:: + android-cloexec-accept android-cloexec-creat android-cloexec-dup android-cloexec-fopen Index: test/clang-tidy/android-cloexec-accept.cpp =================================================================== --- /dev/null +++ test/clang-tidy/android-cloexec-accept.cpp @@ -0,0 +1,28 @@ +// RUN: %check_clang_tidy %s android-cloexec-accept %t + +struct sockaddr {}; +typedef int socklen_t; +#define NULL 0 + +extern "C" int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); + +void f() { + accept(0, NULL, NULL); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer accept4() to accept() because accept4() allows SOCK_CLOEXEC [android-cloexec-accept] + // CHECK-FIXES: accept4(0, NULL, NULL, SOCK_CLOEXEC); +} + +namespace i { +int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); +void g() { + accept(0, NULL, NULL); +} +} // namespace i + +class C { +public: + int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); + void h() { + accept(0, NULL, NULL); + } +};