| 1 |
// stapdyn utility functions |
| 2 |
// Copyright (C) 2014 Red Hat Inc. |
| 3 |
// |
| 4 |
// This file is part of systemtap, and is free software. You can |
| 5 |
// redistribute it and/or modify it under the terms of the GNU General |
| 6 |
// Public License (GPL); either version 2, or (at your option) any |
| 7 |
// later version. |
| 8 |
|
| 9 |
#include "config.h" |
| 10 |
|
| 11 |
#include <iostream> |
| 12 |
#include <string> |
| 13 |
#include <cstdlib> |
| 14 |
|
| 15 |
extern "C" { |
| 16 |
#include <dlfcn.h> |
| 17 |
#include <err.h> |
| 18 |
#include <link.h> |
| 19 |
} |
| 20 |
|
| 21 |
#ifdef HAVE_SELINUX |
| 22 |
#include <selinux/selinux.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include "dynutil.h" |
| 26 |
#include "../staputil.h" |
| 27 |
|
| 28 |
using namespace std; |
| 29 |
|
| 30 |
|
| 31 |
// Callback for dl_iterate_phdr to look for libdyninstAPI.so |
| 32 |
static int |
| 33 |
guess_dyninst_rt_callback(struct dl_phdr_info *info, |
| 34 |
size_t size __attribute__ ((unused)), |
| 35 |
void *data) |
| 36 |
{ |
| 37 |
string& libdyninstAPI = *static_cast<string*>(data); |
| 38 |
|
| 39 |
const string name = info->dlpi_name ?: "(null)"; |
| 40 |
if (name.find("libdyninstAPI.so") != string::npos) |
| 41 |
libdyninstAPI = name; |
| 42 |
|
| 43 |
return 0; |
| 44 |
} |
| 45 |
|
| 46 |
// Look for libdyninstAPI.so in our own process, and use that |
| 47 |
// to guess the path for libdyninstAPI_RT.so |
| 48 |
static const string |
| 49 |
guess_dyninst_rt(void) |
| 50 |
{ |
| 51 |
string libdyninstAPI; |
| 52 |
dl_iterate_phdr(guess_dyninst_rt_callback, &libdyninstAPI); |
| 53 |
|
| 54 |
string libdyninstAPI_RT; |
| 55 |
size_t so = libdyninstAPI.rfind(".so"); |
| 56 |
if (so != string::npos) |
| 57 |
{ |
| 58 |
libdyninstAPI_RT = libdyninstAPI; |
| 59 |
libdyninstAPI_RT.insert(so, "_RT"); |
| 60 |
} |
| 61 |
return libdyninstAPI_RT; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
// Check that environment DYNINSTAPI_RT_LIB exists and is a valid file. |
| 66 |
// If not, try to guess a good value and set it. |
| 67 |
bool |
| 68 |
check_dyninst_rt(void) |
| 69 |
{ |
| 70 |
static const char rt_env_name[] = "DYNINSTAPI_RT_LIB"; |
| 71 |
static const char dyn_rw_env_name[] = "DYNINST_REWRITER_PATHS"; |
| 72 |
|
| 73 |
char* rt_env = getenv(rt_env_name); |
| 74 |
if (rt_env) |
| 75 |
{ |
| 76 |
if (file_exists(rt_env)) |
| 77 |
{ |
| 78 |
if (appendenv(dyn_rw_env_name, rt_env) != 0) |
| 79 |
{ |
| 80 |
int olderrno = errno; |
| 81 |
staperror() << "Can't set " << dyn_rw_env_name << ": " << strerror(olderrno); |
| 82 |
return false; |
| 83 |
} |
| 84 |
return true; |
| 85 |
} |
| 86 |
staperror() << "Invalid " << rt_env_name << ": \"" << rt_env << "\"" << endl; |
| 87 |
} |
| 88 |
|
| 89 |
const string rt = guess_dyninst_rt(); |
| 90 |
if (rt.empty() || !file_exists(rt)) |
| 91 |
{ |
| 92 |
staperror() << "Can't find libdyninstAPI_RT.so; try setting " << rt_env_name << endl; |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
if (appendenv(dyn_rw_env_name, rt) != 0) |
| 97 |
{ |
| 98 |
int olderrno = errno; |
| 99 |
staperror() << "Can't set " << rt_env_name << ": " << strerror(olderrno); |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
// Check that SELinux settings are ok for Dyninst operation. |
| 108 |
bool |
| 109 |
check_dyninst_sebools(bool attach) |
| 110 |
{ |
| 111 |
#ifdef HAVE_SELINUX |
| 112 |
// For all these checks, we could examine errno on failure to act differently |
| 113 |
// for e.g. ENOENT vs. EPERM. But since these are just early diagnostices, |
| 114 |
// I'm only going worry about successful bools for now. |
| 115 |
|
| 116 |
// deny_ptrace is definitely a blocker for us to attach at all |
| 117 |
if (security_get_boolean_active("deny_ptrace") > 0) |
| 118 |
{ |
| 119 |
staperror() << "SELinux boolean 'deny_ptrace' is active, " |
| 120 |
"which blocks Dyninst" << endl; |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
// We might have to get more nuanced about allow_execstack, especially if |
| 125 |
// Dyninst is later enhanced to work around this restriction. But for now, |
| 126 |
// this is also a blocker. |
| 127 |
if (security_get_boolean_active("allow_execstack") == 0) |
| 128 |
{ |
| 129 |
staperror() << "SELinux boolean 'allow_execstack' is disabled, " |
| 130 |
"which blocks Dyninst" << endl; |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
// In process-attach mode, SELinux will trigger "avc: denied { execmod }" |
| 135 |
// on ld.so, when the mutator is injecting the dlopen for libdyninstAPI_RT.so. |
| 136 |
if (attach && security_get_boolean_active("allow_execmod") == 0) |
| 137 |
{ |
| 138 |
staperror() << "SELinux boolean 'allow_execmod' is disabled, " |
| 139 |
"which blocks Dyninst" << endl; |
| 140 |
return false; |
| 141 |
} |
| 142 |
#else |
| 143 |
(void)attach; // unused |
| 144 |
#endif |
| 145 |
|
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
// Check whether a process exited cleanly |
| 151 |
bool |
| 152 |
check_dyninst_exit(BPatch_process *process) |
| 153 |
{ |
| 154 |
int code; |
| 155 |
switch (process->terminationStatus()) |
| 156 |
{ |
| 157 |
case ExitedNormally: |
| 158 |
code = process->getExitCode(); |
| 159 |
if (code == EXIT_SUCCESS) |
| 160 |
return true; |
| 161 |
stapwarn() << "Child process exited with status " << code << endl; |
| 162 |
return false; |
| 163 |
|
| 164 |
case ExitedViaSignal: |
| 165 |
code = process->getExitSignal(); |
| 166 |
stapwarn() << "Child process exited with signal " << code |
| 167 |
<< " (" << strsignal(code) << ")" << endl; |
| 168 |
return false; |
| 169 |
|
| 170 |
case NoExit: |
| 171 |
if (process->isTerminated()) |
| 172 |
stapwarn() << "Child process exited in an unknown manner" << endl; |
| 173 |
else |
| 174 |
stapwarn() << "Child process has not exited" << endl; |
| 175 |
return false; |
| 176 |
|
| 177 |
default: |
| 178 |
return false; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
// Get an untyped symbol from a dlopened module. |
| 184 |
// If flagged as 'required', throw an exception if missing or NULL. |
| 185 |
void * |
| 186 |
get_dlsym(void* handle, const char* symbol, bool required) |
| 187 |
{ |
| 188 |
const char* err = dlerror(); // clear previous errors |
| 189 |
void *pointer = dlsym(handle, symbol); |
| 190 |
if (required) |
| 191 |
{ |
| 192 |
if ((err = dlerror())) |
| 193 |
throw std::runtime_error("dlsym " + std::string(err)); |
| 194 |
if (pointer == NULL) |
| 195 |
throw std::runtime_error("dlsym " + std::string(symbol) + " is NULL"); |
| 196 |
} |
| 197 |
return pointer; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
// |
| 202 |
// Logging, warnings, and errors, oh my! |
| 203 |
// |
| 204 |
|
| 205 |
// A null-sink output stream, similar to /dev/null |
| 206 |
// (no buffer -> badbit -> quietly suppressed output) |
| 207 |
static ostream nullstream(NULL); |
| 208 |
|
| 209 |
// verbosity, increased by -v |
| 210 |
unsigned stapdyn_log_level = 0; |
| 211 |
|
| 212 |
// Whether to suppress warnings, set by -w |
| 213 |
bool stapdyn_suppress_warnings = false; |
| 214 |
|
| 215 |
// Output file name, set by -o |
| 216 |
char *stapdyn_outfile_name = NULL; |
| 217 |
|
| 218 |
// Return a stream for logging at the given verbosity level. |
| 219 |
ostream& |
| 220 |
staplog(unsigned level) |
| 221 |
{ |
| 222 |
if (level > stapdyn_log_level) |
| 223 |
return nullstream; |
| 224 |
return clog << program_invocation_short_name << ": "; |
| 225 |
} |
| 226 |
|
| 227 |
// Return a stream for warning messages. |
| 228 |
ostream& |
| 229 |
stapwarn(void) |
| 230 |
{ |
| 231 |
if (stapdyn_suppress_warnings) |
| 232 |
return nullstream; |
| 233 |
return clog << program_invocation_short_name << ": " |
| 234 |
<< colorize("WARNING:", "warning") << " "; |
| 235 |
} |
| 236 |
|
| 237 |
// Return a stream for error messages. |
| 238 |
ostream& |
| 239 |
staperror(void) |
| 240 |
{ |
| 241 |
return clog << program_invocation_short_name << ": " |
| 242 |
<< colorize("ERROR:", "error") << " "; |
| 243 |
} |
| 244 |
|
| 245 |
// Whether to color error and warning messages |
| 246 |
bool color_errors; // Initialized in main() |
| 247 |
|
| 248 |
// Adds coloring to strings |
| 249 |
std::string |
| 250 |
colorize(std::string str, std::string type) |
| 251 |
{ |
| 252 |
if (str.empty() || !color_errors) |
| 253 |
return str; |
| 254 |
else { |
| 255 |
// Check if this type is defined in SYSTEMTAP_COLORS |
| 256 |
std::string color = parse_stap_color(type); |
| 257 |
if (!color.empty()) // no need to pollute terminal if not necessary |
| 258 |
return "\033[" + color + "m\033[K" + str + "\033[m\033[K"; |
| 259 |
else |
| 260 |
return str; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/* Parse SYSTEMTAP_COLORS and returns the SGR parameter(s) for the given |
| 265 |
type. The env var SYSTEMTAP_COLORS must be in the following format: |
| 266 |
'key1=val1:key2=val2:' etc... where valid keys are 'error', 'warning', |
| 267 |
'source', 'caret', 'token' and valid values constitute SGR parameter(s). |
| 268 |
For example, the default setting would be: |
| 269 |
'error=01;31:warning=00;33:source=00;34:caret=01:token=01' |
| 270 |
*/ |
| 271 |
std::string |
| 272 |
parse_stap_color(std::string type) |
| 273 |
{ |
| 274 |
const char *key, *col, *eq; |
| 275 |
int n = type.size(); |
| 276 |
int done = 0; |
| 277 |
|
| 278 |
key = getenv("SYSTEMTAP_COLORS"); |
| 279 |
if (key == NULL) |
| 280 |
key = "error=01;31:warning=00;33:source=00;34:caret=01:token=01"; |
| 281 |
else if (*key == '\0') |
| 282 |
return ""; // disable colors if set but empty |
| 283 |
|
| 284 |
while (!done) { |
| 285 |
if (!(col = strchr(key, ':'))) { |
| 286 |
col = strchr(key, '\0'); |
| 287 |
done = 1; |
| 288 |
} |
| 289 |
if (!((eq = strchr(key, '=')) && eq < col)) |
| 290 |
return ""; /* invalid syntax: no = in range */ |
| 291 |
if (!(key < eq && eq < col-1)) |
| 292 |
return ""; /* invalid syntax: key or val empty */ |
| 293 |
if (strspn(eq+1, "0123456789;") < (size_t)(col-eq-1)) |
| 294 |
return ""; /* invalid syntax: invalid char in val */ |
| 295 |
if (eq-key == n && type.compare(0, n, key, n) == 0) |
| 296 |
return string(eq+1, col-eq-1); |
| 297 |
if (!done) key = col+1; /* advance to next key */ |
| 298 |
} |
| 299 |
|
| 300 |
// Could not find the key |
| 301 |
return ""; |
| 302 |
} |
| 303 |
|
| 304 |
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |