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