| 1 |
# When testing the systemtap server, we don't want to modify the real |
| 2 |
# system configuration (like adding trusted signers). So, instead |
| 3 |
# we'll use a mount namespace. This will allow us to mount a directory |
| 4 |
# on top of the real system cert database directory and only processes |
| 5 |
# that use this mount namespace will see our temporary system cert |
| 6 |
# database directory. See unshare(1) and mount_namespaces(7) for more |
| 7 |
# details. |
| 8 |
# |
| 9 |
# One note. Fedora 27 (the current Fedora at the time of this writeup) |
| 10 |
# has the capability of creating persistent namespaces, which would |
| 11 |
# simplify the following logic a bit. However, neither the RHEL6 or |
| 12 |
# RHEL7 versions of 'unshare' support this. So, we'll do this the |
| 13 |
# harder way. |
| 14 |
# |
| 15 |
# There are two main functions provided by this file: |
| 16 |
# 'server_ns_as_root' and 'server_ns_as_user'. These allow the caller |
| 17 |
# to run commands (as either 'root' or a specified user) in the custom |
| 18 |
# mount namespace. |
| 19 |
# |
| 20 |
# Note that you have to be root to call these functions. Why? It might |
| 21 |
# be possible to call them as non-root using 'sudo'. But, the quoting |
| 22 |
# is really tricky now, and that would just complicate matters |
| 23 |
# even further. So, we're taking the easy way out. |
| 24 |
|
| 25 |
# Setup the server_ns configuration. This function is called by |
| 26 |
# 'server_ns_as_root' and 'server_ns_as_user' (so the setup is only |
| 27 |
# done if those routines that need it are called). |
| 28 |
proc server_ns_setup {} { |
| 29 |
global env systemtap_orig_cert_db_dir systemtap_new_cert_db_dir |
| 30 |
|
| 31 |
# We need to be root so that the temporary directory is owned by |
| 32 |
# root. |
| 33 |
set effective_uid [exec /usr/bin/id -u] |
| 34 |
if {$effective_uid != 0} { |
| 35 |
verbose -log "server_ns_setup: non-root user" |
| 36 |
return 1 |
| 37 |
} |
| 38 |
|
| 39 |
# If we've already created the directory, just return. |
| 40 |
if {[info exists systemtap_orig_cert_db_dir] |
| 41 |
&& [string length $systemtap_orig_cert_db_dir] |
| 42 |
&& [info exists systemtap_new_cert_db_dir] |
| 43 |
&& [string length $systemtap_orig_cert_db_dir]} { |
| 44 |
return 0 |
| 45 |
} |
| 46 |
|
| 47 |
# Create a new directory to bind mount on top of |
| 48 |
# ${SYSCONFDIR}/systemtap. |
| 49 |
if {! [info exists env(SYSCONFDIR)] |
| 50 |
|| [string length $env(SYSCONFDIR)] == 0} { |
| 51 |
verbose -log "server_ns_setup: no SYSCONFDIR!" |
| 52 |
return 1 |
| 53 |
} |
| 54 |
|
| 55 |
# Note that the db path actually ends up under |
| 56 |
# SYSCONFDIR/systemtap/staprun, but for some reason stap doesn't |
| 57 |
# like us bind mounting that directory. So, we'll use the parent |
| 58 |
# directory. |
| 59 |
set systemtap_orig_cert_db_dir "$env(SYSCONFDIR)/systemtap" |
| 60 |
|
| 61 |
# Create a temporary cert dtatabase path. This directory must be |
| 62 |
# owned by root. We delete this when shutting down the server. |
| 63 |
if {[catch {exec mktemp -d} systemtap_new_cert_db_dir]} { |
| 64 |
verbose -log "server_ns_setup - Failed to create temporary cert db directory: $systemtap_new_cert_db_dir" |
| 65 |
unset systemtap_new_cert_db_dir |
| 66 |
return 1 |
| 67 |
} |
| 68 |
verbose -log "server_ns_setup - orig cert db dir: $systemtap_orig_cert_db_dir" |
| 69 |
verbose -log "server_ns_setup - new cert db dir: $systemtap_new_cert_db_dir" |
| 70 |
return 0 |
| 71 |
} |
| 72 |
|
| 73 |
# Clean up the server_ns configuration. This function is called by |
| 74 |
# 'shutdown_server' (and is careful not to do anyting if |
| 75 |
# server_ns_setup hasn't been called). |
| 76 |
proc server_ns_cleanup {} { |
| 77 |
global systemtap_new_cert_db_dir |
| 78 |
if {[info exists systemtap_new_cert_db_dir] |
| 79 |
&& [string length $systemtap_new_cert_db_dir] |
| 80 |
&& [file exists $systemtap_new_cert_db_dir]} { |
| 81 |
catch {exec rm -rf $systemtap_new_cert_db_dir} |
| 82 |
unset systemtap_new_cert_db_dir |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
# server_ns_as_root: run 'command' in a custom mount |
| 87 |
# namespace as root. Returns a list of [ RETURN_STATUS OUTPUT ]. |
| 88 |
proc server_ns_as_root { command } { |
| 89 |
global systemtap_orig_cert_db_dir systemtap_new_cert_db_dir |
| 90 |
|
| 91 |
if {[server_ns_setup]} { return [list 1 "server_ns_setup failure"] } |
| 92 |
|
| 93 |
# We're root (otherwise server_ns_setup would have failed). We're going |
| 94 |
# to call "unshare" to create a new mount namespace. This will |
| 95 |
# allow us to mount our temporary directory on top of the real |
| 96 |
# system cert db. |
| 97 |
set cmd {unshare --mount bash -c "mount --bind $systemtap_new_cert_db_dir $systemtap_orig_cert_db_dir && $command"} |
| 98 |
verbose -log "running: [subst $cmd]" |
| 99 |
set rc 0 |
| 100 |
if {[catch {eval exec $cmd} output]} { |
| 101 |
# non-zero exit status, get it: |
| 102 |
if {[lindex $::errorCode 0] eq "CHILDSTATUS"} { |
| 103 |
set rc [lindex $::errorCode 2] |
| 104 |
} |
| 105 |
} |
| 106 |
verbose -log "RC: $rc" |
| 107 |
verbose -log "$output" |
| 108 |
|
| 109 |
return [list $rc $output] |
| 110 |
} |
| 111 |
|
| 112 |
# server_ns_as_user: run 'command' in a custom mount namespace as user |
| 113 |
# 'user'. Returns a list of [ RETURN_STATUS OUTPUT ]. |
| 114 |
proc server_ns_as_user { user command } { |
| 115 |
global systemtap_orig_cert_db_dir systemtap_new_cert_db_dir |
| 116 |
|
| 117 |
if {[server_ns_setup]} { return [list 1 "server_ns_setup failure"] } |
| 118 |
|
| 119 |
# We're root (otherwise server_ns_setup would have failed). We're going |
| 120 |
# to call "unshare" to create a new mount namespace. This will |
| 121 |
# allow us to mount our temporary directory on top of the real |
| 122 |
# system cert db. We're also going to run the caller's command as |
| 123 |
# the specified user (using 'su'). |
| 124 |
set cmd {unshare --mount bash -c "mount --bind $systemtap_new_cert_db_dir $systemtap_orig_cert_db_dir && su -l -s /bin/sh $user -c '$command'"} |
| 125 |
verbose -log "running: [subst $cmd]" |
| 126 |
set rc 0 |
| 127 |
if {[catch {eval exec $cmd} output]} { |
| 128 |
# non-zero exit status, get it: |
| 129 |
if {[lindex $::errorCode 0] eq "CHILDSTATUS"} { |
| 130 |
set rc [lindex $::errorCode 2] |
| 131 |
} |
| 132 |
} |
| 133 |
verbose -log "RC: $rc" |
| 134 |
verbose -log "$output" |
| 135 |
|
| 136 |
return [list $rc $output] |
| 137 |
} |