Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: base/files/protect_file_posix.cc

Issue 676873004: Intercept file Open/Close (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: gn Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/containers/hash_tables.h"
6 #include "base/files/file.h"
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
9 #include "base/posix/eintr_wrapper.h"
10 #include "base/synchronization/lock.h"
11
12 // These hooks provided for base::File perform additional sanity checks when
13 // files are closed.
14 //
15 // Background:
16 // 1. The browser process crashes if a call to close() provided by the C
17 // library (i.e. close(3)) fails. This is done for security purposes. See
18 // base/files/scoped_file.cc. When a crash like this happens, there is not
19 // enough context in the minidump to triage the problem.
20 // 2. base::File provides a good abstraction to prevent closing incorrect
21 // file descriptors or double-closing. Closing non-owned file descriptors
22 // would more likely happen from outside base::File and base::ScopedFD.
23 //
24 // These hooks intercept base::File operations to 'protect' file handles /
25 // descriptors from accidental close(3) by other portions of the code being
26 // linked into the browser. Also, this file provides an interceptor for the
27 // close(3) itself, and requires to be linked with cooperation of
28 // --Wl,--wrap=close (i.e. linker wrapping).
29 //
30 // Wrapping close(3) on all libraries can lead to confusion, particularly for
31 // the libraries that do not use ::base (I am also looking at you,
32 // crazy_linker). Instead two hooks are added to base::File, which are
33 // implemented as no-op by default. This file should be linked into the Chrome
34 // native binary(-ies) for a whitelist of targets where "file descriptor
35 // protection" is useful.
36
37 // With compilers other than GCC/Clang the wrapper is trivial. This is to avoid
38 // overexercising mechanisms for overriding weak symbols.
39 #if !defined(COMPILER_GCC)
40 extern "C" {
41
42 int __real_close(int fd);
43
44 BASE_EXPORT int __wrap_close(int fd) {
45 return __real_close(fd);
46 }
47
48 } // extern "C"
49
50 #else // defined(COMPILER_GCC)
51
52 namespace {
53
54 // Protects the |g_protected_fd_set|.
55 base::LazyInstance<base::Lock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER;
56
57 // Holds the set of all 'protected' file descriptors.
58 base::LazyInstance<base::hash_set<int> >::Leaky g_protected_fd_set =
59 LAZY_INSTANCE_INITIALIZER;
60
61 bool IsFileDescriptorProtected(int fd) {
62 base::AutoLock lock(*g_lock.Pointer());
63 return g_protected_fd_set.Get().count(fd) != 0;
64 }
65
66 } // namespace
67
68 namespace base {
69
70 BASE_EXPORT void ProtectFileDescriptor(int fd) {
71 base::AutoLock lock(g_lock.Get());
72 CHECK(!g_protected_fd_set.Get().count(fd)) << "fd: " << fd;
73 g_protected_fd_set.Get().insert(fd);
74 }
75
76 BASE_EXPORT void UnprotectFileDescriptor(int fd) {
77 base::AutoLock lock(*g_lock.Pointer());
78 CHECK(g_protected_fd_set.Get().erase(fd)) << "fd: " << fd;
79 }
80
81 } // namespace base
82
83 extern "C" {
84
85 int __real_close(int fd);
86
87 BASE_EXPORT int __wrap_close(int fd) {
88 // The crash happens here if a protected file descriptor was attempted to be
89 // closed without first being unprotected. Unprotection happens only in
90 // base::File. In other words this is an "early crash" as compared to the one
91 // happening in scoped_file.cc.
92 //
93 // Getting an earlier crash provides a more useful stack trace (minidump)
94 // allowing to debug deeper into the thread that freed the wrong resource.
95 CHECK(!IsFileDescriptorProtected(fd)) << "fd: " << fd;
96
97 // Crash by the same reason as in scoped_file.cc.
98 PCHECK(0 == IGNORE_EINTR(__real_close(fd)));
99 return 0;
100 }
101
102 } // extern "C"
103
104 #endif // defined(COMPILER_GCC)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698