[clang-tools-extra] r311027 - [clang-tidy] Add a close-on-exec check on accept4() in Android module.
Chih-Hung Hsieh via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 16 10:46:18 PDT 2017
Author: chh
Date: Wed Aug 16 10:46:18 2017
New Revision: 311027
URL: http://llvm.org/viewvc/llvm-project?rev=311027&view=rev
Log:
[clang-tidy] Add a close-on-exec check on accept4() in Android module.
Summary:
accept4() is better to set SOCK_CLOEXEC flag to avoid file descriptor leakage.
Differential Revision: https://reviews.llvm.org/D35363
Added:
clang-tools-extra/trunk/clang-tidy/android/CloexecAccept4Check.cpp
clang-tools-extra/trunk/clang-tidy/android/CloexecAccept4Check.h
clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-accept4.rst
clang-tools-extra/trunk/test/clang-tidy/android-cloexec-accept4.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=311027&r1=311026&r2=311027&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:46:18 2017
@@ -10,6 +10,7 @@
#include "../ClangTidy.h"
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
+#include "CloexecAccept4Check.h"
#include "CloexecAcceptCheck.h"
#include "CloexecCreatCheck.h"
#include "CloexecDupCheck.h"
@@ -30,6 +31,7 @@ namespace android {
class AndroidModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
+ CheckFactories.registerCheck<CloexecAccept4Check>("android-cloexec-accept4");
CheckFactories.registerCheck<CloexecAcceptCheck>("android-cloexec-accept");
CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat");
CheckFactories.registerCheck<CloexecDupCheck>("android-cloexec-dup");
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=311027&r1=311026&r2=311027&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:46:18 2017
@@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyAndroidModule
AndroidTidyModule.cpp
+ CloexecAccept4Check.cpp
CloexecAcceptCheck.cpp
CloexecCheck.cpp
CloexecCreatCheck.cpp
Added: clang-tools-extra/trunk/clang-tidy/android/CloexecAccept4Check.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecAccept4Check.cpp?rev=311027&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecAccept4Check.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecAccept4Check.cpp Wed Aug 16 10:46:18 2017
@@ -0,0 +1,40 @@
+//===--- CloexecAccept4Check.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 "CloexecAccept4Check.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 CloexecAccept4Check::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("accept4"),
+ hasParameter(0, hasType(isInteger())),
+ hasParameter(1, SockAddrPointerType),
+ hasParameter(2, SockLenPointerType),
+ hasParameter(3, hasType(isInteger()))));
+}
+
+void CloexecAccept4Check::check(const MatchFinder::MatchResult &Result) {
+ insertMacroFlag(Result, /*MarcoFlag=*/"SOCK_CLOEXEC", /*ArgPos=*/3);
+}
+
+} // namespace android
+} // namespace tidy
+} // namespace clang
Added: clang-tools-extra/trunk/clang-tidy/android/CloexecAccept4Check.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecAccept4Check.h?rev=311027&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecAccept4Check.h (added)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecAccept4Check.h Wed Aug 16 10:46:18 2017
@@ -0,0 +1,35 @@
+//===--- CloexecAccept4Check.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_ACCEPT4_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_ACCEPT4_H
+
+#include "CloexecCheck.h"
+
+namespace clang {
+namespace tidy {
+namespace android {
+
+/// Finds code that uses accept4() without using the SOCK_CLOEXEC flag.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-accept4.html
+class CloexecAccept4Check : public CloexecCheck {
+public:
+ CloexecAccept4Check(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_ACCEPT4_H
Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=311027&r1=311026&r2=311027&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Wed Aug 16 10:46:18 2017
@@ -75,6 +75,12 @@ Improvements to clang-tidy
Detects usage of ``accept()``.
+- New `android-cloexec-accept4
+ <http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-accept4.html>`_ check
+
+ Checks if the required file flag ``SOCK_CLOEXEC`` is present in the argument of
+ ``accept4()``.
+
- 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-accept4.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-accept4.rst?rev=311027&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-accept4.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-accept4.rst Wed Aug 16 10:46:18 2017
@@ -0,0 +1,18 @@
+.. title:: clang-tidy - android-cloexec-accept4
+
+android-cloexec-accept4
+=======================
+
+``accept4()`` should include ``SOCK_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++
+
+ accept4(sockfd, addr, addrlen, SOCK_NONBLOCK);
+
+ // becomes
+
+ accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | 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=311027&r1=311026&r2=311027&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:46:18 2017
@@ -5,6 +5,7 @@ Clang-Tidy Checks
.. toctree::
android-cloexec-accept
+ android-cloexec-accept4
android-cloexec-creat
android-cloexec-dup
android-cloexec-fopen
Added: clang-tools-extra/trunk/test/clang-tidy/android-cloexec-accept4.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/android-cloexec-accept4.cpp?rev=311027&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/android-cloexec-accept4.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/android-cloexec-accept4.cpp Wed Aug 16 10:46:18 2017
@@ -0,0 +1,66 @@
+// RUN: %check_clang_tidy %s android-cloexec-accept4 %t
+
+typedef int socklen_t;
+struct sockaddr {};
+
+#define SOCK_NONBLOCK 1
+#define __O_CLOEXEC 3
+#define SOCK_CLOEXEC __O_CLOEXEC
+#define TEMP_FAILURE_RETRY(exp) \
+ ({ \
+ int _rc; \
+ do { \
+ _rc = (exp); \
+ } while (_rc == -1); \
+ })
+#define NULL 0
+
+extern "C" int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
+
+void a() {
+ accept4(0, NULL, NULL, SOCK_NONBLOCK);
+ // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: 'accept4' should use SOCK_CLOEXEC where possible [android-cloexec-accept4]
+ // CHECK-FIXES: accept4(0, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
+ TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, SOCK_NONBLOCK));
+ // CHECK-MESSAGES: :[[@LINE-1]]:58: warning: 'accept4'
+ // CHECK-FIXES: TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC));
+}
+
+void f() {
+ accept4(0, NULL, NULL, 3);
+ // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 'accept4'
+ // CHECK-FIXES: accept4(0, NULL, NULL, 3 | SOCK_CLOEXEC);
+ TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, 3));
+ // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: 'accept4'
+ // CHECK-FIXES: TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, 3 | SOCK_CLOEXEC));
+
+ int flag = SOCK_NONBLOCK;
+ accept4(0, NULL, NULL, flag);
+ TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, flag));
+}
+
+namespace i {
+int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
+
+void d() {
+ accept4(0, NULL, NULL, SOCK_NONBLOCK);
+ TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, SOCK_NONBLOCK));
+}
+
+} // namespace i
+
+void e() {
+ accept4(0, NULL, NULL, SOCK_CLOEXEC);
+ TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, SOCK_CLOEXEC));
+ accept4(0, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
+ TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC));
+}
+
+class G {
+public:
+ int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
+ void d() {
+ accept4(0, NULL, NULL, SOCK_NONBLOCK);
+ TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, SOCK_NONBLOCK));
+ }
+};
More information about the cfe-commits
mailing list