Chromium Code Reviews| 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <fcntl.h> | 6 #include <fcntl.h> |
| 7 #include <sys/ptrace.h> | 7 #include <sys/ptrace.h> |
| 8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
| 15 #include "sandbox/linux/services/scoped_process.h" | 15 #include "sandbox/linux/services/scoped_process.h" |
| 16 #include "sandbox/linux/services/yama.h" | 16 #include "sandbox/linux/services/yama.h" |
| 17 #include "sandbox/linux/tests/unit_tests.h" | 17 #include "sandbox/linux/tests/unit_tests.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 namespace sandbox { | 20 namespace sandbox { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 bool HasLinux32Bug() { | |
| 25 #if defined(__i386__) | |
| 26 // On 3.2 kernels, yama doesn't work for 32bit binaries on 64bit kernels. | |
|
Jorge Lucangeli Obes
2014/07/08 04:57:14
Yama, 32-bit, 64-bit
Nico
2014/07/08 05:00:47
I had fixed the 32bit/64bit thing (see patch set 3
| |
| 27 // This is fixed in 3.4. | |
| 28 bool is_kernel_64bit = | |
| 29 base::SysInfo::OperatingSystemArchitecture() == "x86_64"; | |
| 30 bool is_linux = base::SysInfo::OperatingSystemName() == "Linux"; | |
| 31 bool is_3_dot_2 = StartsWithASCII( | |
| 32 base::SysInfo::OperatingSystemVersion(), "3.2", /*case_sensitive=*/false); | |
| 33 if (is_kernel_64bit && is_linux && is_3_dot_2) | |
| 34 return true; | |
| 35 #endif // defined(__i386__) | |
| 36 return false; | |
| 37 } | |
| 38 | |
| 24 bool CanPtrace(pid_t pid) { | 39 bool CanPtrace(pid_t pid) { |
| 25 int ret; | 40 int ret; |
| 26 ret = ptrace(PTRACE_ATTACH, pid, NULL, NULL); | 41 ret = ptrace(PTRACE_ATTACH, pid, NULL, NULL); |
| 27 if (ret == -1) { | 42 if (ret == -1) { |
| 28 CHECK_EQ(EPERM, errno); | 43 CHECK_EQ(EPERM, errno); |
| 29 return false; | 44 return false; |
| 30 } | 45 } |
| 31 // Wait for the process to be stopped so that it can be detached. | 46 // Wait for the process to be stopped so that it can be detached. |
| 32 siginfo_t process_info; | 47 siginfo_t process_info; |
| 33 int wait_ret = HANDLE_EINTR(waitid(P_PID, pid, &process_info, WSTOPPED)); | 48 int wait_ret = HANDLE_EINTR(waitid(P_PID, pid, &process_info, WSTOPPED)); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 // Attempts to enable or disable Yama restrictions. | 120 // Attempts to enable or disable Yama restrictions. |
| 106 void SetYamaRestrictions(bool enable_restriction) { | 121 void SetYamaRestrictions(bool enable_restriction) { |
| 107 if (enable_restriction) { | 122 if (enable_restriction) { |
| 108 Yama::RestrictPtracersToAncestors(); | 123 Yama::RestrictPtracersToAncestors(); |
| 109 } else { | 124 } else { |
| 110 Yama::DisableYamaRestrictions(); | 125 Yama::DisableYamaRestrictions(); |
| 111 } | 126 } |
| 112 } | 127 } |
| 113 | 128 |
| 114 TEST(Yama, RestrictPtraceWorks) { | 129 TEST(Yama, RestrictPtraceWorks) { |
| 130 if (HasLinux32Bug()) | |
| 131 return; | |
| 132 | |
| 115 ScopedProcess process1(base::Bind(&SetYamaRestrictions, true)); | 133 ScopedProcess process1(base::Bind(&SetYamaRestrictions, true)); |
| 116 ASSERT_TRUE(process1.WaitForClosureToRun()); | 134 ASSERT_TRUE(process1.WaitForClosureToRun()); |
| 117 | 135 |
| 118 if (Yama::IsEnforcing()) { | 136 if (Yama::IsEnforcing()) { |
| 119 // A sibling process cannot ptrace process1. | 137 // A sibling process cannot ptrace process1. |
| 120 ASSERT_FALSE(CanSubProcessPtrace(process1.GetPid())); | 138 ASSERT_FALSE(CanSubProcessPtrace(process1.GetPid())); |
| 121 } | 139 } |
| 122 | 140 |
| 123 if (!(Yama::GetStatus() & Yama::STATUS_STRICT_ENFORCING)) { | 141 if (!(Yama::GetStatus() & Yama::STATUS_STRICT_ENFORCING)) { |
| 124 // However, parent can ptrace process1. | 142 // However, parent can ptrace process1. |
| 125 ASSERT_TRUE(CanPtrace(process1.GetPid())); | 143 ASSERT_TRUE(CanPtrace(process1.GetPid())); |
| 126 | 144 |
| 127 // A sibling can ptrace process2 which disables any Yama protection. | 145 // A sibling can ptrace process2 which disables any Yama protection. |
| 128 ScopedProcess process2(base::Bind(&SetYamaRestrictions, false)); | 146 ScopedProcess process2(base::Bind(&SetYamaRestrictions, false)); |
| 129 ASSERT_TRUE(process2.WaitForClosureToRun()); | 147 ASSERT_TRUE(process2.WaitForClosureToRun()); |
| 130 ASSERT_TRUE(CanSubProcessPtrace(process2.GetPid())); | 148 ASSERT_TRUE(CanSubProcessPtrace(process2.GetPid())); |
| 131 } | 149 } |
| 132 } | 150 } |
| 133 | 151 |
| 134 void DoNothing() {} | 152 void DoNothing() {} |
| 135 | 153 |
| 136 SANDBOX_TEST(Yama, RestrictPtraceIsDefault) { | 154 SANDBOX_TEST(Yama, RestrictPtraceIsDefault) { |
| 137 if (!Yama::IsPresent()) | 155 if (!Yama::IsPresent() || HasLinux32Bug()) |
| 138 return; | 156 return; |
| 139 | 157 |
| 140 CHECK(Yama::DisableYamaRestrictions()); | 158 CHECK(Yama::DisableYamaRestrictions()); |
| 141 ScopedProcess process1(base::Bind(&DoNothing)); | 159 ScopedProcess process1(base::Bind(&DoNothing)); |
| 142 | 160 |
| 143 if (Yama::IsEnforcing()) { | 161 if (Yama::IsEnforcing()) { |
| 144 // Check that process1 is protected by Yama, even though it has | 162 // Check that process1 is protected by Yama, even though it has |
| 145 // been created from a process that disabled Yama. | 163 // been created from a process that disabled Yama. |
| 146 CHECK(!CanSubProcessPtrace(process1.GetPid())); | 164 CHECK(!CanSubProcessPtrace(process1.GetPid())); |
| 147 } | 165 } |
| 148 } | 166 } |
| 149 | 167 |
| 150 } // namespace | 168 } // namespace |
| 151 | 169 |
| 152 } // namespace sandbox | 170 } // namespace sandbox |
| OLD | NEW |