OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sandbox/linux/services/proc_util.h" | 5 #include "sandbox/linux/services/proc_util.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 int proc_self_fd = HANDLE_EINTR( | 44 int proc_self_fd = HANDLE_EINTR( |
45 openat(proc_fd, "self/fd/", O_DIRECTORY | O_RDONLY | O_CLOEXEC)); | 45 openat(proc_fd, "self/fd/", O_DIRECTORY | O_RDONLY | O_CLOEXEC)); |
46 PCHECK(0 <= proc_self_fd); | 46 PCHECK(0 <= proc_self_fd); |
47 | 47 |
48 // Ownership of proc_self_fd is transferred here, it must not be closed | 48 // Ownership of proc_self_fd is transferred here, it must not be closed |
49 // or modified afterwards except via dir. | 49 // or modified afterwards except via dir. |
50 ScopedDIR dir(fdopendir(proc_self_fd)); | 50 ScopedDIR dir(fdopendir(proc_self_fd)); |
51 CHECK(dir); | 51 CHECK(dir); |
52 | 52 |
53 int count = 0; | 53 int count = 0; |
54 struct dirent e; | 54 for (struct dirent* e = readdir(dir.get()); e; e = readdir(dir.get())) { |
Jorge Lucangeli Obes
2016/10/19 15:50:16
Are these calls to readdir guaranteed to not race
dcheng
2016/10/19 22:11:39
My understanding (and the condition on which I LGT
| |
55 struct dirent* de; | 55 if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) { |
56 while (!readdir_r(dir.get(), &e, &de) && de) { | |
57 if (strcmp(e.d_name, ".") == 0 || strcmp(e.d_name, "..") == 0) { | |
58 continue; | 56 continue; |
59 } | 57 } |
60 | 58 |
61 int fd_num; | 59 int fd_num; |
62 CHECK(base::StringToInt(e.d_name, &fd_num)); | 60 CHECK(base::StringToInt(e->d_name, &fd_num)); |
63 if (fd_num == proc_fd || fd_num == proc_self_fd) { | 61 if (fd_num == proc_fd || fd_num == proc_self_fd) { |
64 continue; | 62 continue; |
65 } | 63 } |
66 | 64 |
67 ++count; | 65 ++count; |
68 } | 66 } |
69 return count; | 67 return count; |
70 } | 68 } |
71 | 69 |
72 bool ProcUtil::HasOpenDirectory(int proc_fd) { | 70 bool ProcUtil::HasOpenDirectory(int proc_fd) { |
73 DCHECK_LE(0, proc_fd); | 71 DCHECK_LE(0, proc_fd); |
74 int proc_self_fd = | 72 int proc_self_fd = |
75 openat(proc_fd, "self/fd/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); | 73 openat(proc_fd, "self/fd/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
76 | 74 |
77 PCHECK(0 <= proc_self_fd); | 75 PCHECK(0 <= proc_self_fd); |
78 | 76 |
79 // Ownership of proc_self_fd is transferred here, it must not be closed | 77 // Ownership of proc_self_fd is transferred here, it must not be closed |
80 // or modified afterwards except via dir. | 78 // or modified afterwards except via dir. |
81 ScopedDIR dir(fdopendir(proc_self_fd)); | 79 ScopedDIR dir(fdopendir(proc_self_fd)); |
82 CHECK(dir); | 80 CHECK(dir); |
83 | 81 |
84 struct dirent e; | 82 for (struct dirent *e = readdir(dir.get()); e; e = readdir(dir.get())) { |
85 struct dirent* de; | 83 if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) { |
86 while (!readdir_r(dir.get(), &e, &de) && de) { | |
87 if (strcmp(e.d_name, ".") == 0 || strcmp(e.d_name, "..") == 0) { | |
88 continue; | 84 continue; |
89 } | 85 } |
90 | 86 |
91 int fd_num; | 87 int fd_num; |
92 CHECK(base::StringToInt(e.d_name, &fd_num)); | 88 CHECK(base::StringToInt(e->d_name, &fd_num)); |
93 if (fd_num == proc_fd || fd_num == proc_self_fd) { | 89 if (fd_num == proc_fd || fd_num == proc_self_fd) { |
94 continue; | 90 continue; |
95 } | 91 } |
96 | 92 |
97 struct stat s; | 93 struct stat s; |
98 // It's OK to use proc_self_fd here, fstatat won't modify it. | 94 // It's OK to use proc_self_fd here, fstatat won't modify it. |
99 CHECK(fstatat(proc_self_fd, e.d_name, &s, 0) == 0); | 95 CHECK(fstatat(proc_self_fd, e->d_name, &s, 0) == 0); |
100 if (S_ISDIR(s.st_mode)) { | 96 if (S_ISDIR(s.st_mode)) { |
101 return true; | 97 return true; |
102 } | 98 } |
103 } | 99 } |
104 | 100 |
105 // No open unmanaged directories found. | 101 // No open unmanaged directories found. |
106 return false; | 102 return false; |
107 } | 103 } |
108 | 104 |
109 bool ProcUtil::HasOpenDirectory() { | 105 bool ProcUtil::HasOpenDirectory() { |
110 base::ScopedFD proc_fd( | 106 base::ScopedFD proc_fd( |
111 HANDLE_EINTR(open("/proc/", O_DIRECTORY | O_RDONLY | O_CLOEXEC))); | 107 HANDLE_EINTR(open("/proc/", O_DIRECTORY | O_RDONLY | O_CLOEXEC))); |
112 return HasOpenDirectory(proc_fd.get()); | 108 return HasOpenDirectory(proc_fd.get()); |
113 } | 109 } |
114 | 110 |
115 // static | 111 // static |
116 base::ScopedFD ProcUtil::OpenProc() { | 112 base::ScopedFD ProcUtil::OpenProc() { |
117 return OpenDirectory("/proc/"); | 113 return OpenDirectory("/proc/"); |
118 } | 114 } |
119 | 115 |
120 } // namespace sandbox | 116 } // namespace sandbox |
OLD | NEW |