• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <errno.h>
30 #include <signal.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 
36 #include <atomic>
37 #include <string>
38 
39 #include <memunreachable/memunreachable.h>
40 #include <platform/bionic/macros.h>
41 
42 #include "Config.h"
43 #include "Unreachable.h"
44 #include "debug_log.h"
45 
46 std::atomic_bool Unreachable::do_check_;
47 
EnableUnreachableCheck(int,struct siginfo *,void *)48 static void EnableUnreachableCheck(int, struct siginfo*, void*) {
49   Unreachable::EnableCheck();
50 }
51 
CheckIfRequested(const Config & config)52 void Unreachable::CheckIfRequested(const Config& config) {
53   if ((config.options() & CHECK_UNREACHABLE_ON_SIGNAL) && do_check_.exchange(false)) {
54     info_log("Starting to check for unreachable memory.");
55     if (!LogUnreachableMemory(false, 100)) {
56       error_log("Unreachable check failed, run setenforce 0 and try again.");
57     }
58   }
59 }
60 
Initialize(const Config & config)61 bool Unreachable::Initialize(const Config& config) {
62   if (!(config.options() & CHECK_UNREACHABLE_ON_SIGNAL)) {
63     return true;
64   }
65 
66   struct sigaction64 unreachable_act = {};
67   unreachable_act.sa_sigaction = EnableUnreachableCheck;
68   unreachable_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
69   if (sigaction64(config.check_unreachable_signal(), &unreachable_act, nullptr) != 0) {
70     error_log("Unable to set up check unreachable signal function: %s", strerror(errno));
71     return false;
72   }
73 
74   if (config.options() & VERBOSE) {
75     info_log("%s: Run: 'kill -%d %d' to check for unreachable memory.", getprogname(),
76              config.check_unreachable_signal(), getpid());
77   }
78 
79   return true;
80 }
81