[clang-tools-extra] r311024 - [clang-tidy] Add a close-on-exec check on accept() in Android module.
Chih-Hung Hsieh via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 16 10:18:16 PDT 2017
Author: chh
Date: Wed Aug 16 10:18:16 2017
New Revision: 311024
URL: http://llvm.org/viewvc/llvm-project?rev=311024&view=rev
Log:
[clang-tidy] Add a close-on-exec check on accept() in Android module.
Summary:
accept() is better to be replaced by accept4() with SOCK_CLOEXEC
flag to avoid file descriptor leakage.
Differential Revision: https://reviews.llvm.org/D35362
Added:
clang-tools-extra/trunk/clang-tidy/android/CloexecAcceptCheck.cpp
clang-tools-extra/trunk/clang-tidy/android/CloexecAcceptCheck.h
clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-accept.rst
clang-tools-extra/trunk/test/clang-tidy/android-cloexec-accept.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=311024&r1=311023&r2=311024&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp Wed Aug 16 10:18:16 2017
@@ -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"
@@ -29,6 +30,7 @@ namespace android {
class AndroidModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
+ CheckFactories.registerCheck<CloexecAcceptCheck>("android-cloexec-accept");
CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat");
CheckFactories.registerCheck<CloexecDupCheck>("android-cloexec-dup");
CheckFactories.registerCheck<CloexecFopenCheck>("android-cloexec-fopen");
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=311024&r1=311023&r2=311024&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt Wed Aug 16 10:18:16 2017
@@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyAndroidModule
AndroidTidyModule.cpp
+ CloexecAcceptCheck.cpp
CloexecCheck.cpp
CloexecCreatCheck.cpp
CloexecDupCheck.cpp
Added: clang-tools-extra/trunk/clang-tidy/android/CloexecAcceptCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecAcceptCheck.cpp?rev=311024&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecAcceptCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecAcceptCheck.cpp Wed Aug 16 10:18:16 2017
@@ -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,
+ "prefer accept4() to accept() because accept4() allows SOCK_CLOEXEC",
+ ReplacementText);
+}
+
+} // namespace android
+} // namespace tidy
+} // namespace clang
Added: clang-tools-extra/trunk/clang-tidy/android/CloexecAcceptCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecAcceptCheck.h?rev=311024&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecAcceptCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecAcceptCheck.h Wed Aug 16 10:18:16 2017
@@ -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
Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=311024&r1=311023&r2=311024&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Wed Aug 16 10:18:16 2017
@@ -70,6 +70,11 @@ Improvements to clang-tidy
``AllowConditionalIntegerCasts`` -> ``AllowIntegerConditions``,
``AllowConditionalPointerCasts`` -> ``AllowPointerConditions``.
+- New `android-cloexec-accept
+ <http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-accept.html>`_ check
+
+ Detects usage of ``accept()``.
+
- New `android-cloexec-dup
<http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-dup.html>`_ check
Added: clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-accept.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-accept.rst?rev=311024&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-accept.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-accept.rst Wed Aug 16 10:18:16 2017
@@ -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);
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=311024&r1=311023&r2=311024&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Wed Aug 16 10:18:16 2017
@@ -4,6 +4,7 @@ Clang-Tidy Checks
=================
.. toctree::
+ android-cloexec-accept
android-cloexec-creat
android-cloexec-dup
android-cloexec-fopen
Added: clang-tools-extra/trunk/test/clang-tidy/android-cloexec-accept.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/android-cloexec-accept.cpp?rev=311024&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/android-cloexec-accept.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/android-cloexec-accept.cpp Wed Aug 16 10:18:16 2017
@@ -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);
+ }
+};
More information about the cfe-commits
mailing list