| 1 |
#! /usr/bin/env stap |
| 2 |
# Copyright (C) 2016-2018 Red Hat, Inc. |
| 3 |
# Written by William Cohen <[email protected]> |
| 4 |
# |
| 5 |
# container_check.stp watches for use of |
| 6 |
# prohibited capabilities, use of prohibited syscalls, and |
| 7 |
# syscall failures) that would indicate that this application |
| 8 |
# would not operate properly in a restricted contiainer. |
| 9 |
# |
| 10 |
# By default this script monitors all systemcalls system-wide. |
| 11 |
# To limit to limit container_check.stp to monitoring a particular |
| 12 |
# process and it children use the systemtap -x <pid> option |
| 13 |
# or -c <command> option. |
| 14 |
# |
| 15 |
# By default this script lists all capabilities requested. |
| 16 |
# To limit it to a subset of capabilities use the following |
| 17 |
# option on the command line with a '-' separated list of |
| 18 |
# forbidden capabilites: |
| 19 |
# |
| 20 |
# -G forbidden_capabilities="badcap1-badcap2" |
| 21 |
# |
| 22 |
# By default this script allows all syscalls. |
| 23 |
# To mark syscalls as forbidden use a '-' separate list: |
| 24 |
# |
| 25 |
# -G forbidden_syscalls="syscall1-syscall2" |
| 26 |
# |
| 27 |
# control-c to exit data collection |
| 28 |
|
| 29 |
global forbidden_capabilities="" # '-' separated list of forbidden capabilities |
| 30 |
global forbidden_syscalls="" # '-' separated list of forbidden syscalls |
| 31 |
|
| 32 |
global capability, cap_use |
| 33 |
global badcaps = -1, cap_name |
| 34 |
global cap_syscall |
| 35 |
global badsyscall |
| 36 |
global problem_syscall |
| 37 |
global syscall_errno |
| 38 |
|
| 39 |
# Determine whether t is a ancestor of target() |
| 40 |
# returns 1 if ancestor of target() |
| 41 |
# returns 0 if not an ancestor of target() |
| 42 |
function child_of_target:long (t:long) |
| 43 |
{ |
| 44 |
if (!target()) return 1 |
| 45 |
while(t && t != task_parent(t)) { |
| 46 |
if (task_pid(t) == target()) return 1 |
| 47 |
t = task_parent(t) |
| 48 |
} |
| 49 |
return 0 |
| 50 |
} |
| 51 |
|
| 52 |
function init_cap_name2num() |
| 53 |
{ |
| 54 |
/* set up the names */ |
| 55 |
cap_name[0]="cap_chown" |
| 56 |
cap_name[1]="cap_dac_override" |
| 57 |
cap_name[2]="cap_dac_read_search" |
| 58 |
cap_name[3]="cap_fowner" |
| 59 |
cap_name[4]="cap_fsetid" |
| 60 |
cap_name[5]="cap_kill" |
| 61 |
cap_name[6]="cap_setgid" |
| 62 |
cap_name[7]="cap_setuid" |
| 63 |
cap_name[8]="cap_setpcap" |
| 64 |
cap_name[9]="cap_linux_immutable" |
| 65 |
cap_name[10]="cap_net_bind_service" |
| 66 |
cap_name[11]="cap_net_broadcast" |
| 67 |
cap_name[12]="cap_net_admin" |
| 68 |
cap_name[13]="cap_net_raw" |
| 69 |
cap_name[14]="cap_ipc_lock" |
| 70 |
cap_name[15]="cap_ipc_owner" |
| 71 |
cap_name[16]="cap_sys_module" |
| 72 |
cap_name[17]="cap_sys_rawio" |
| 73 |
cap_name[18]="cap_sys_chroot" |
| 74 |
cap_name[19]="cap_sys_ptrace" |
| 75 |
cap_name[20]="cap_sys_pacct" |
| 76 |
cap_name[21]="cap_sys_admin" |
| 77 |
cap_name[22]="cap_sys_boot" |
| 78 |
cap_name[23]="cap_sys_nice" |
| 79 |
cap_name[24]="cap_sys_resource" |
| 80 |
cap_name[25]="cap_sys_time" |
| 81 |
cap_name[26]="cap_sys_tty_config" |
| 82 |
cap_name[27]="cap_mknod" |
| 83 |
cap_name[28]="cap_lease" |
| 84 |
cap_name[29]="cap_audit_write" |
| 85 |
cap_name[30]="cap_audit_control" |
| 86 |
cap_name[31]="cap_setfcap" |
| 87 |
cap_name[32]="cap_mac_override" |
| 88 |
cap_name[33]="cap_mac_admin" |
| 89 |
cap_name[34]="cap_syslog" |
| 90 |
cap_name[35]="cap_wake_alarm" |
| 91 |
cap_name[36]="cap_block_suspend" |
| 92 |
} |
| 93 |
|
| 94 |
function parse_capabilities() { |
| 95 |
/* convert optional list of forbidden capabilities into a bitmask */ |
| 96 |
caps = 0 |
| 97 |
cname = tokenize(forbidden_capabilities, "-") |
| 98 |
while (cname != "") { |
| 99 |
i =36 |
| 100 |
while(i>0) { |
| 101 |
if(cname == cap_name[i]) { |
| 102 |
caps |= 1<<i |
| 103 |
i = -1 |
| 104 |
} |
| 105 |
i -= 1 |
| 106 |
} |
| 107 |
cname = tokenize("", "-") |
| 108 |
} |
| 109 |
if (caps) badcaps = caps |
| 110 |
} |
| 111 |
|
| 112 |
function parse_syscalls() { |
| 113 |
/* The following assignment is to ensure that badsyscall has typeinfo. */ |
| 114 |
badsyscall["no_a_syscall"]=1 |
| 115 |
/* Put in optional list of bad syscalls. */ |
| 116 |
sysname = tokenize(forbidden_syscalls, "-") |
| 117 |
while (sysname != "") { |
| 118 |
badsyscall[sysname] = 1 |
| 119 |
sysname = tokenize("", "-") |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
probe begin { |
| 124 |
init_cap_name2num() |
| 125 |
parse_capabilities() |
| 126 |
parse_syscalls() |
| 127 |
|
| 128 |
printf ("starting container_check.stp. monitoring %d\n", target()) |
| 129 |
} |
| 130 |
|
| 131 |
# bool ns_capable(struct user_namespace *ns, int cap) |
| 132 |
probe ns_capable = kprobe.function("ns_capable") |
| 133 |
{ |
| 134 |
cap = 1 << int_arg(2) |
| 135 |
} |
| 136 |
|
| 137 |
#bool ns_capable_noaudit(struct user_namespace *ns, int cap) |
| 138 |
probe ns_capable_noaudit = kprobe.function("ns_capable_noaudit") |
| 139 |
{ |
| 140 |
cap = 1 << int_arg(2) |
| 141 |
} |
| 142 |
|
| 143 |
#bool ns_capable_setid(struct user_namespace *ns, int cap) |
| 144 |
probe ns_capable_setid = kprobe.function("ns_capable_setid") |
| 145 |
{ |
| 146 |
cap = 1 << int_arg(2) |
| 147 |
} |
| 148 |
|
| 149 |
# bool capable(int cap) |
| 150 |
probe capable = kprobe.function("capable") |
| 151 |
{ |
| 152 |
cap = 1 << int_arg(1) |
| 153 |
} |
| 154 |
|
| 155 |
#bool file_ns_capable(const struct file *file, struct user_namespace *ns, |
| 156 |
# int cap) |
| 157 |
probe file_ns_capable = kprobe.function("file_ns_capable") |
| 158 |
{ |
| 159 |
cap = 1 << int_arg(3) |
| 160 |
} |
| 161 |
|
| 162 |
probe ns_capable?, ns_capable_noaudit?, ns_capable_setid?, |
| 163 |
capable?, file_ns_capable? |
| 164 |
{ |
| 165 |
if ((cap & badcaps) && child_of_target(task_current())) |
| 166 |
cap_use[tid()] |= cap |
| 167 |
} |
| 168 |
|
| 169 |
probe syscall_any.return { |
| 170 |
# note any problem capabilities use during syscall |
| 171 |
cap = cap_use[tid()] |
| 172 |
if (cap && child_of_target(task_current())) { |
| 173 |
capability[execname()] |= cap |
| 174 |
cap_syscall[execname(), name, cap] <<< 1 |
| 175 |
delete cap_use[tid()] |
| 176 |
} |
| 177 |
|
| 178 |
# note any prohibited systemcalls |
| 179 |
if (name in badsyscall && child_of_target(task_current())) { |
| 180 |
problem_syscall[execname(), name] <<< 1 |
| 181 |
} |
| 182 |
|
| 183 |
# note any syscalls returning errors |
| 184 |
if (retval < 0 && child_of_target(task_current())) { |
| 185 |
syscall_errno[execname(), name, retval] <<< 1 |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
probe end { |
| 190 |
printf("\n\ncapabilities used by executables\n"); |
| 191 |
printf("%16s: %20s\n\n", "executable", "prob capability") |
| 192 |
foreach(e+ in capability) { |
| 193 |
cap = capability[e] |
| 194 |
i=0 |
| 195 |
while (cap) { |
| 196 |
if (cap & 1) |
| 197 |
printf("%16s: %20s\n", e, cap_name[i] ); |
| 198 |
cap = cap >> 1 |
| 199 |
i += 1 |
| 200 |
} |
| 201 |
printf("\n") |
| 202 |
} |
| 203 |
|
| 204 |
printf("\n\ncapabilities used by syscalls\n"); |
| 205 |
printf("%16s, %20s ( %16s ) : %16s\n", "executable", "syscall", "capability", "count") |
| 206 |
foreach([e+,s,c] in cap_syscall){ |
| 207 |
printf("%16s, %20s ( ", e, s); |
| 208 |
cap = c |
| 209 |
i=0 |
| 210 |
while (cap) { |
| 211 |
if (cap & 1) |
| 212 |
printf("%16s ", cap_name[i] ); |
| 213 |
cap = cap >> 1 |
| 214 |
i += 1 |
| 215 |
} |
| 216 |
printf(") : %16d\n", @count(cap_syscall[e,s,c]) ); |
| 217 |
} |
| 218 |
|
| 219 |
printf("\n\nforbidden syscalls\n"); |
| 220 |
printf("%16s, %20s: %16s\n", "executable", "syscall", "count") |
| 221 |
foreach([e+,s] in problem_syscall){ |
| 222 |
printf("%16s, %20s: %16d\n", e, s, @count(problem_syscall[e,s]) ); |
| 223 |
} |
| 224 |
|
| 225 |
printf("\n\nfailed syscalls\n"); |
| 226 |
printf("%16s, %20s = %16s: %16s\n", "executable", "syscall", "errno", "count") |
| 227 |
foreach([e+,s,v] in syscall_errno){ |
| 228 |
printf("%16s, %20s = %16s: %16d\n", e, s, errno_str(v), |
| 229 |
@count(syscall_errno[e,s,v]) ); |
| 230 |
} |
| 231 |
} |