- add sources.
[platform/framework/web/crosswalk.git] / src / sandbox / linux / seccomp-bpf / die.cc
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
3 // found in the LICENSE file.
4
5 #include <errno.h>
6 #include <linux/unistd.h>
7 #include <stdio.h>
8 #include <sys/prctl.h>
9
10 #include <string>
11
12 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
13 #include "sandbox/linux/seccomp-bpf/syscall.h"
14
15
16 namespace playground2 {
17
18 void Die::ExitGroup() {
19   // 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.
21   // Especially, since we are dealing with system call filters. Continuing
22   // execution would be very bad in most cases where ExitGroup() gets called.
23   // So, we'll try a few other strategies too.
24   SandboxSyscall(__NR_exit_group, 1);
25
26   // 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
28   // 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
30   // us terminate.
31   signal(SIGSEGV, SIG_DFL);
32   SandboxSyscall(__NR_prctl, PR_SET_DUMPABLE, (void *)0, (void *)0, (void *)0);
33   if (*(volatile char *)0) { }
34
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
37   // and file a bug...
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".
40   for (;;) {
41     SandboxSyscall(__NR_exit_group, 1);
42   }
43 }
44
45 void Die::SandboxDie(const char *msg, const char *file, int line) {
46   if (simple_exit_) {
47     LogToStderr(msg, file, line);
48   } else {
49     #if defined(SECCOMP_BPF_STANDALONE)
50       Die::LogToStderr(msg, file, line);
51     #else
52       logging::LogMessage(file, line, logging::LOG_FATAL).stream() << msg;
53     #endif
54   }
55   ExitGroup();
56 }
57
58 void Die::RawSandboxDie(const char *msg) {
59   if (!msg)
60     msg = "";
61   RAW_LOG(FATAL, msg);
62   ExitGroup();
63 }
64
65 void Die::SandboxInfo(const char *msg, const char *file, int line) {
66   if (!suppress_info_) {
67   #if defined(SECCOMP_BPF_STANDALONE)
68     Die::LogToStderr(msg, file, line);
69   #else
70     logging::LogMessage(file, line, logging::LOG_INFO).stream() << msg;
71   #endif
72   }
73 }
74
75 void Die::LogToStderr(const char *msg, const char *file, int line) {
76   if (msg) {
77     char buf[40];
78     snprintf(buf, sizeof(buf), "%d", line);
79     std::string s = std::string(file) + ":" + buf + ":" + msg + "\n";
80
81     // No need to loop. Short write()s are unlikely and if they happen we
82     // probably prefer them over a loop that blocks.
83     if (HANDLE_EINTR(SandboxSyscall(__NR_write, 2, s.c_str(), s.length()))) { }
84   }
85 }
86
87 bool Die::simple_exit_   = false;
88 bool Die::suppress_info_ = false;
89
90 }  // namespace