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/yama.h" | 5 #include "sandbox/linux/services/yama.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <sys/prctl.h> | 8 #include <sys/prctl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
18 #include "base/strings/string_util.h" | |
19 #include "base/sys_info.h" | |
18 | 20 |
19 #if !defined(PR_SET_PTRACER_ANY) | 21 #if !defined(PR_SET_PTRACER_ANY) |
20 #define PR_SET_PTRACER_ANY ((unsigned long)-1) | 22 #define PR_SET_PTRACER_ANY ((unsigned long)-1) |
21 #endif | 23 #endif |
22 | 24 |
23 #if !defined(PR_SET_PTRACER) | 25 #if !defined(PR_SET_PTRACER) |
24 #define PR_SET_PTRACER 0x59616d61 | 26 #define PR_SET_PTRACER 0x59616d61 |
25 #endif | 27 #endif |
26 | 28 |
27 namespace sandbox { | 29 namespace sandbox { |
28 | 30 |
29 namespace { | 31 namespace { |
30 | 32 |
31 // Enable or disable the Yama ptracers restrictions. | 33 // Enable or disable the Yama ptracers restrictions. |
32 // Return false if Yama is not present on this kernel. | 34 // Return false if Yama is not present on this kernel. |
33 bool SetYamaPtracersRestriction(bool enable_restrictions) { | 35 bool SetYamaPtracersRestriction(bool enable_restrictions) { |
36 if (enable_restrictions && Yama::HasLinux32Bug()) { | |
Jorge Lucangeli Obes
2014/07/08 04:14:28
This doesn't really make sense. The problem is whe
| |
37 return false; | |
38 } | |
39 | |
34 unsigned long set_ptracer_arg; | 40 unsigned long set_ptracer_arg; |
35 if (enable_restrictions) { | 41 if (enable_restrictions) { |
36 set_ptracer_arg = 0; | 42 set_ptracer_arg = 0; |
37 } else { | 43 } else { |
38 set_ptracer_arg = PR_SET_PTRACER_ANY; | 44 set_ptracer_arg = PR_SET_PTRACER_ANY; |
39 } | 45 } |
40 | 46 |
41 const int ret = prctl(PR_SET_PTRACER, set_ptracer_arg); | 47 const int ret = prctl(PR_SET_PTRACER, set_ptracer_arg); |
42 const int prctl_errno = errno; | 48 const int prctl_errno = errno; |
43 | 49 |
(...skipping 26 matching lines...) Expand all Loading... | |
70 bool Yama::DisableYamaRestrictions() { | 76 bool Yama::DisableYamaRestrictions() { |
71 return SetYamaPtracersRestriction(false /* enable_restrictions */); | 77 return SetYamaPtracersRestriction(false /* enable_restrictions */); |
72 } | 78 } |
73 | 79 |
74 // static | 80 // static |
75 int Yama::GetStatus() { | 81 int Yama::GetStatus() { |
76 if (!CanAccessProcFS()) { | 82 if (!CanAccessProcFS()) { |
77 return 0; | 83 return 0; |
78 } | 84 } |
79 | 85 |
86 if (HasLinux32Bug()) { | |
87 return 0; | |
Jorge Lucangeli Obes
2014/07/08 04:14:28
As above.
| |
88 } | |
89 | |
80 static const char kPtraceScopePath[] = "/proc/sys/kernel/yama/ptrace_scope"; | 90 static const char kPtraceScopePath[] = "/proc/sys/kernel/yama/ptrace_scope"; |
81 | 91 |
82 base::ScopedFD yama_scope(HANDLE_EINTR(open(kPtraceScopePath, O_RDONLY))); | 92 base::ScopedFD yama_scope(HANDLE_EINTR(open(kPtraceScopePath, O_RDONLY))); |
83 | 93 |
84 if (!yama_scope.is_valid()) { | 94 if (!yama_scope.is_valid()) { |
85 const int open_errno = errno; | 95 const int open_errno = errno; |
86 DCHECK(ENOENT == open_errno); | 96 DCHECK(ENOENT == open_errno); |
87 // The status is known, yama is not present. | 97 // The status is known, yama is not present. |
88 return STATUS_KNOWN; | 98 return STATUS_KNOWN; |
89 } | 99 } |
(...skipping 16 matching lines...) Expand all Loading... | |
106 return 0; | 116 return 0; |
107 } | 117 } |
108 } | 118 } |
109 | 119 |
110 // static | 120 // static |
111 bool Yama::IsPresent() { return GetStatus() & STATUS_PRESENT; } | 121 bool Yama::IsPresent() { return GetStatus() & STATUS_PRESENT; } |
112 | 122 |
113 // static | 123 // static |
114 bool Yama::IsEnforcing() { return GetStatus() & STATUS_ENFORCING; } | 124 bool Yama::IsEnforcing() { return GetStatus() & STATUS_ENFORCING; } |
115 | 125 |
126 // static | |
127 bool Yama::HasLinux32Bug() { | |
128 #if defined(__i386__) | |
129 // On 3.2 kernels, yama doesn't work for 32bit binaries on 64bit kernels. | |
Jorge Lucangeli Obes
2014/07/08 04:14:28
32-bit, 64-bit
| |
130 // This is fixed in 3.4. | |
131 bool is_kernel_64bit = | |
132 base::SysInfo::OperatingSystemArchitecture() == "x86_64"; | |
133 bool is_linux = base::SysInfo::OperatingSystemName() == "Linux"; | |
134 bool is_3_dot_2 = StartsWithASCII( | |
135 base::SysInfo::OperatingSystemVersion(), "3.2", /*case_sensitive=*/false); | |
Jorge Lucangeli Obes
2014/07/08 04:14:28
'false /* case_sensitive */'
Nico
2014/07/08 04:24:43
This is somewhat common style: https://code.google
| |
136 if (is_kernel_64bit && is_linux && is_3_dot_2) | |
137 return true; | |
138 #endif // defined(__i386__) | |
139 return false; | |
140 } | |
141 | |
116 } // namespace sandbox | 142 } // namespace sandbox |
OLD | NEW |