| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <linux/unistd.h> | 6 #include <linux/unistd.h> |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <sys/prctl.h> | 8 #include <sys/prctl.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | 12 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
| 13 #include "sandbox/linux/seccomp-bpf/syscall.h" | 13 #include "sandbox/linux/seccomp-bpf/syscall.h" |
| 14 | 14 |
| 15 | |
| 16 namespace playground2 { | 15 namespace playground2 { |
| 17 | 16 |
| 18 void Die::ExitGroup() { | 17 void Die::ExitGroup() { |
| 19 // exit_group() should exit our program. After all, it is defined as a | 18 // exit_group() should exit our program. After all, it is defined as a |
| 20 // function that doesn't return. But things can theoretically go wrong. | 19 // function that doesn't return. But things can theoretically go wrong. |
| 21 // Especially, since we are dealing with system call filters. Continuing | 20 // Especially, since we are dealing with system call filters. Continuing |
| 22 // execution would be very bad in most cases where ExitGroup() gets called. | 21 // execution would be very bad in most cases where ExitGroup() gets called. |
| 23 // So, we'll try a few other strategies too. | 22 // So, we'll try a few other strategies too. |
| 24 SandboxSyscall(__NR_exit_group, 1); | 23 SandboxSyscall(__NR_exit_group, 1); |
| 25 | 24 |
| 26 // We have no idea what our run-time environment looks like. So, signal | 25 // We have no idea what our run-time environment looks like. So, signal |
| 27 // handlers might or might not do the right thing. Try to reset settings | 26 // handlers might or might not do the right thing. Try to reset settings |
| 28 // to a defined state; but we have not way to verify whether we actually | 27 // to a defined state; but we have not way to verify whether we actually |
| 29 // succeeded in doing so. Nonetheless, triggering a fatal signal could help | 28 // succeeded in doing so. Nonetheless, triggering a fatal signal could help |
| 30 // us terminate. | 29 // us terminate. |
| 31 signal(SIGSEGV, SIG_DFL); | 30 signal(SIGSEGV, SIG_DFL); |
| 32 SandboxSyscall(__NR_prctl, PR_SET_DUMPABLE, (void *)0, (void *)0, (void *)0); | 31 SandboxSyscall(__NR_prctl, PR_SET_DUMPABLE, (void*)0, (void*)0, (void*)0); |
| 33 if (*(volatile char *)0) { } | 32 if (*(volatile char*)0) { |
| 33 } |
| 34 | 34 |
| 35 // If there is no way for us to ask for the program to exit, the next | 35 // If there is no way for us to ask for the program to exit, the next |
| 36 // best thing we can do is to loop indefinitely. Maybe, somebody will notice | 36 // best thing we can do is to loop indefinitely. Maybe, somebody will notice |
| 37 // and file a bug... | 37 // and file a bug... |
| 38 // We in fact retry the system call inside of our loop so that it will | 38 // We in fact retry the system call inside of our loop so that it will |
| 39 // stand out when somebody tries to diagnose the problem by using "strace". | 39 // stand out when somebody tries to diagnose the problem by using "strace". |
| 40 for (;;) { | 40 for (;;) { |
| 41 SandboxSyscall(__NR_exit_group, 1); | 41 SandboxSyscall(__NR_exit_group, 1); |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 void Die::SandboxDie(const char *msg, const char *file, int line) { | 45 void Die::SandboxDie(const char* msg, const char* file, int line) { |
| 46 if (simple_exit_) { | 46 if (simple_exit_) { |
| 47 LogToStderr(msg, file, line); | 47 LogToStderr(msg, file, line); |
| 48 } else { | 48 } else { |
| 49 #if defined(SECCOMP_BPF_STANDALONE) | 49 #if defined(SECCOMP_BPF_STANDALONE) |
| 50 Die::LogToStderr(msg, file, line); | 50 Die::LogToStderr(msg, file, line); |
| 51 #else | 51 #else |
| 52 logging::LogMessage(file, line, logging::LOG_FATAL).stream() << msg; | 52 logging::LogMessage(file, line, logging::LOG_FATAL).stream() << msg; |
| 53 #endif | 53 #endif |
| 54 } | 54 } |
| 55 ExitGroup(); | 55 ExitGroup(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void Die::RawSandboxDie(const char *msg) { | 58 void Die::RawSandboxDie(const char* msg) { |
| 59 if (!msg) | 59 if (!msg) |
| 60 msg = ""; | 60 msg = ""; |
| 61 RAW_LOG(FATAL, msg); | 61 RAW_LOG(FATAL, msg); |
| 62 ExitGroup(); | 62 ExitGroup(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 void Die::SandboxInfo(const char *msg, const char *file, int line) { | 65 void Die::SandboxInfo(const char* msg, const char* file, int line) { |
| 66 if (!suppress_info_) { | 66 if (!suppress_info_) { |
| 67 #if defined(SECCOMP_BPF_STANDALONE) | 67 #if defined(SECCOMP_BPF_STANDALONE) |
| 68 Die::LogToStderr(msg, file, line); | 68 Die::LogToStderr(msg, file, line); |
| 69 #else | 69 #else |
| 70 logging::LogMessage(file, line, logging::LOG_INFO).stream() << msg; | 70 logging::LogMessage(file, line, logging::LOG_INFO).stream() << msg; |
| 71 #endif | 71 #endif |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 | 74 |
| 75 void Die::LogToStderr(const char *msg, const char *file, int line) { | 75 void Die::LogToStderr(const char* msg, const char* file, int line) { |
| 76 if (msg) { | 76 if (msg) { |
| 77 char buf[40]; | 77 char buf[40]; |
| 78 snprintf(buf, sizeof(buf), "%d", line); | 78 snprintf(buf, sizeof(buf), "%d", line); |
| 79 std::string s = std::string(file) + ":" + buf + ":" + msg + "\n"; | 79 std::string s = std::string(file) + ":" + buf + ":" + msg + "\n"; |
| 80 | 80 |
| 81 // No need to loop. Short write()s are unlikely and if they happen we | 81 // No need to loop. Short write()s are unlikely and if they happen we |
| 82 // probably prefer them over a loop that blocks. | 82 // probably prefer them over a loop that blocks. |
| 83 if (HANDLE_EINTR(SandboxSyscall(__NR_write, 2, s.c_str(), s.length()))) { } | 83 if (HANDLE_EINTR(SandboxSyscall(__NR_write, 2, s.c_str(), s.length()))) { |
| 84 } |
| 84 } | 85 } |
| 85 } | 86 } |
| 86 | 87 |
| 87 bool Die::simple_exit_ = false; | 88 bool Die::simple_exit_ = false; |
| 88 bool Die::suppress_info_ = false; | 89 bool Die::suppress_info_ = false; |
| 89 | 90 |
| 90 } // namespace | 91 } // namespace |
| OLD | NEW |