OLD | NEW |
(Empty) | |
| 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 <setjmp.h> |
| 6 #include <signal.h> |
| 7 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
| 8 #include "sandbox/linux/services/android_ucontext.h" |
| 9 #include "sandbox/linux/tests/unit_tests.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace sandbox { |
| 13 const int kExpectedValueParm2 = 0xec; |
| 14 const int kExpectedValueParm3 = 0xed; |
| 15 const int kExpectedValueParm4 = 0xee; |
| 16 const int kExpectedValueParm5 = 0x00; |
| 17 |
| 18 void SigAction(int n, siginfo_t *SigInfo, void* SigContext) { |
| 19 const ucontext_t *Context = static_cast<ucontext_t*>(SigContext); |
| 20 sigset_t set = 0; |
| 21 |
| 22 sigaddset(&set, SIGPIPE); |
| 23 sigaddset(&set, SIGFPE); |
| 24 |
| 25 // ARM define registers array as unsigned while x86 define it as signed. |
| 26 SANDBOX_ASSERT(static_cast<int>(SECCOMP_PARM2(Context)) == |
| 27 static_cast<int>(kExpectedValueParm2)); |
| 28 SANDBOX_ASSERT(static_cast<int>(SECCOMP_PARM3(Context)) == |
| 29 static_cast<int>(kExpectedValueParm3)); |
| 30 SANDBOX_ASSERT(static_cast<int>(SECCOMP_PARM4(Context)) == |
| 31 static_cast<int>(kExpectedValueParm4)); |
| 32 SANDBOX_ASSERT(static_cast<int>(SECCOMP_PARM5(Context)) == |
| 33 static_cast<int>(kExpectedValueParm5)); |
| 34 SANDBOX_ASSERT(Context->uc_sigmask == set); |
| 35 } |
| 36 |
| 37 SANDBOX_DEATH_TEST(UcontextTest, TestUcontext, DEATH_BY_SIGNAL(SIGSEGV)) { |
| 38 int ret; |
| 39 struct sigaction NewAct; |
| 40 struct sigaction OldAct; |
| 41 sigset_t NewSet, OldSet; |
| 42 |
| 43 memset(&NewAct, 0, sizeof(NewAct)); |
| 44 NewAct.sa_sigaction = SigAction; |
| 45 // We need SA_RESETHAND here to make sure the process is terminated |
| 46 // after return from signal handler. Otherwise, there will be dead loop. |
| 47 NewAct.sa_flags = SA_RESETHAND | SA_SIGINFO; |
| 48 sigemptyset(&NewAct.sa_mask); |
| 49 |
| 50 sigemptyset(&NewSet); |
| 51 sigaddset(&NewSet, SIGPIPE); |
| 52 sigaddset(&NewSet, SIGFPE); |
| 53 |
| 54 sigprocmask(SIG_SETMASK, &NewSet, &OldSet); |
| 55 ret = sigaction(SIGSEGV, &NewAct, &OldAct); |
| 56 SANDBOX_ASSERT(ret == 0); |
| 57 |
| 58 // We don't use *((int*) 0x0) = 0xdeadbeaf here because toolchain |
| 59 // may allocate registers (Android ARM gcc uses r2/r3). Which makes |
| 60 // registers checked in signal handler are not easy to choice. |
| 61 // The following code is suppose to make process terminated. So we don't |
| 62 // care about the which reigsters are clobbered here. |
| 63 #if defined(__i386__) |
| 64 asm __volatile__ ( |
| 65 "movl $0xec, %ecx\n\t" |
| 66 "movl $0xed, %edx\n\t" |
| 67 "movl $0xee, %esi\n\t" |
| 68 "movl $0x00, %edi\n\t" |
| 69 "movl $0x00, (%edi)\n\t" |
| 70 ); |
| 71 #elif defined(__arm__) |
| 72 asm __volatile__ ( |
| 73 "mov r1, #0xec\n\t" |
| 74 "mov r2, #0xed\n\t" |
| 75 "mov r3, #0xee\n\t" |
| 76 "mov r4, #0x00\n\t" |
| 77 "str r3, [r4]\n\t" |
| 78 ); |
| 79 #endif |
| 80 |
| 81 /* Shouldn't be here */ |
| 82 SANDBOX_ASSERT(1 == 0); |
| 83 sigprocmask(SIG_SETMASK, &OldSet, NULL); |
| 84 sigaction(SIGSEGV, &OldAct, NULL); |
| 85 } |
| 86 |
| 87 } // namespace sandbox |
OLD | NEW |