| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | package provide porttrace 1.0 |
|---|
| 36 | package require Pextlib 1.0 |
|---|
| 37 | |
|---|
| 38 | proc trace_start {workpath} { |
|---|
| 39 | global os.platform |
|---|
| 40 | if {${os.platform} == "darwin"} { |
|---|
| 41 | if {[catch {package require Thread} error]} { |
|---|
| 42 | ui_warn "trace requires Tcl Thread package ($error)" |
|---|
| 43 | } else { |
|---|
| 44 | global env trace_fifo trace_sandboxbounds portpath |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | set trace_fifo "/tmp/macports/[pid]_[expr {int(rand()*1000)}]" |
|---|
| 49 | file mkdir "/tmp/macports" |
|---|
| 50 | file delete -force $trace_fifo |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | create_slave $workpath $trace_fifo |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | set tracelib_path [file join ${portutil::autoconf::prefix} share macports Tcl darwintrace1.0 darwintrace.dylib] |
|---|
| 58 | |
|---|
| 59 | if {[info exists env(DYLD_INSERT_LIBRARIES)] && [string length "$env(DYLD_INSERT_LIBRARIES)"] > 0} { |
|---|
| 60 | set env(DYLD_INSERT_LIBRARIES) "${env(DYLD_INSERT_LIBRARIES)}:${tracelib_path}" |
|---|
| 61 | } else { |
|---|
| 62 | set env(DYLD_INSERT_LIBRARIES) ${tracelib_path} |
|---|
| 63 | } |
|---|
| 64 | set env(DYLD_FORCE_FLAT_NAMESPACE) 1 |
|---|
| 65 | set env(DARWINTRACE_LOG) "$trace_fifo" |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | set trace_sandboxbounds "/tmp:/private/tmp:/var/tmp:/private/var/tmp:/dev/:/etc/passwd:/etc/groups:/etc/localtime:/Library/Caches/com.apple.Xcode:$env(HOME)/.ccache:${workpath}:$portpath" |
|---|
| 79 | if {[info exists env(TMPDIR)]} { |
|---|
| 80 | set trace_sandboxbounds "${trace_sandboxbounds}:$env(TMPDIR)" |
|---|
| 81 | } |
|---|
| 82 | if {[info exists env(CCACHE_DIR)]} { |
|---|
| 83 | set trace_sandboxbounds "${trace_sandboxbounds}:$env(CCACHE_DIR)" |
|---|
| 84 | } |
|---|
| 85 | tracelib setsandbox $trace_sandboxbounds |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | proc trace_enable_fence {} { |
|---|
| 93 | global env trace_sandboxbounds |
|---|
| 94 | set env(DARWINTRACE_SANDBOX_BOUNDS) $trace_sandboxbounds |
|---|
| 95 | tracelib enablefence |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | proc trace_disable_fence {} { |
|---|
| 101 | global env |
|---|
| 102 | if [info exists env(DARWINTRACE_SANDBOX_BOUNDS)] { |
|---|
| 103 | unset env(DARWINTRACE_SANDBOX_BOUNDS) |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | proc trace_check_deps {target portslist} { |
|---|
| 112 | |
|---|
| 113 | set ports [slave_send slave_get_ports] |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | set portslist [lsort $portslist] |
|---|
| 117 | foreach port $ports { |
|---|
| 118 | if {[lsearch -sorted -exact $portslist $port] == -1} { |
|---|
| 119 | ui_warn "Target $target has an undeclared dependency on $port" |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | foreach port $portslist { |
|---|
| 123 | if {[lsearch -sorted -exact $ports $port] == -1} { |
|---|
| 124 | ui_debug "Target $target has no traceable dependency on $port" |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | proc trace_check_violations {} { |
|---|
| 133 | |
|---|
| 134 | set violations [slave_send slave_get_sandbox_violations] |
|---|
| 135 | |
|---|
| 136 | foreach violation [lsort $violations] { |
|---|
| 137 | ui_warn "An activity was attempted outside sandbox: $violation" |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | proc trace_stop {} { |
|---|
| 144 | global os.platform |
|---|
| 145 | if {${os.platform} == "darwin"} { |
|---|
| 146 | global env trace_fifo |
|---|
| 147 | unset env(DYLD_INSERT_LIBRARIES) |
|---|
| 148 | unset env(DYLD_FORCE_FLAT_NAMESPACE) |
|---|
| 149 | unset env(DARWINTRACE_LOG) |
|---|
| 150 | if [info exists env(DARWINTRACE_SANDBOX_BOUNDS)] { |
|---|
| 151 | unset env(DARWINTRACE_SANDBOX_BOUNDS) |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | tracelib clean |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | slave_send slave_stop |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | delete_slave |
|---|
| 162 | |
|---|
| 163 | file delete -force $trace_fifo |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | proc create_slave {workpath trace_fifo} { |
|---|
| 170 | global trace_thread |
|---|
| 171 | |
|---|
| 172 | set trace_thread [macports_create_thread] |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | thread::send -async $trace_thread "package require registry 1.0" |
|---|
| 176 | |
|---|
| 177 | thread::send -async $trace_thread "package require porttrace 1.0" |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | thread::send -async $trace_thread "slave_start $trace_fifo $workpath" |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | proc slave_send_async {command} { |
|---|
| 186 | global trace_thread |
|---|
| 187 | |
|---|
| 188 | thread::send -async $trace_thread "$command" |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | proc slave_send {command} { |
|---|
| 194 | global trace_thread |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | thread::send $trace_thread "$command" result |
|---|
| 199 | return $result |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | proc delete_slave {} { |
|---|
| 205 | global trace_thread |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | thread::release $trace_thread |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | proc slave_read_line {chan} { |
|---|
| 214 | global ports_list trace_filemap sandbox_violation_list workpath |
|---|
| 215 | global env |
|---|
| 216 | |
|---|
| 217 | while 1 { |
|---|
| 218 | |
|---|
| 219 | if {[eof $chan]} { |
|---|
| 220 | break |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | set theline [gets $chan] |
|---|
| 226 | |
|---|
| 227 | if {[fblocked $chan]} { |
|---|
| 228 | |
|---|
| 229 | break |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | set line_length [string length $theline] |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | if {$line_length > 0} { |
|---|
| 236 | set path_start [expr [string first "\t" $theline] + 1] |
|---|
| 237 | set op [string range $theline 0 [expr $path_start - 2]] |
|---|
| 238 | set path [string range $theline $path_start [expr $line_length - 1]] |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | if {$op == "open" || $op == "execve"} { |
|---|
| 242 | |
|---|
| 243 | if {[file isfile $path]} { |
|---|
| 244 | |
|---|
| 245 | if {![filemap exists trace_filemap $path]} { |
|---|
| 246 | |
|---|
| 247 | set port [registry::file_registered $path] |
|---|
| 248 | if { $port != 0 } { |
|---|
| 249 | |
|---|
| 250 | if {[lsearch -sorted -exact $ports_list $port] == -1} { |
|---|
| 251 | lappend ports_list $port |
|---|
| 252 | set ports_list [lsort $ports_list] |
|---|
| 253 | |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | catch {filemap set trace_filemap $path $port} |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | } elseif {$op == "sandbox_violation"} { |
|---|
| 266 | lappend sandbox_violation_list $path |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | proc slave_start {fifo p_workpath} { |
|---|
| 275 | global ports_list trace_filemap sandbox_violation_list |
|---|
| 276 | |
|---|
| 277 | set workpath $p_workpath |
|---|
| 278 | |
|---|
| 279 | filemap create trace_filemap |
|---|
| 280 | set ports_list {} |
|---|
| 281 | set sandbox_violation_list {} |
|---|
| 282 | tracelib setname $fifo |
|---|
| 283 | tracelib run |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | proc slave_stop {} { |
|---|
| 289 | global trace_filemap trace_fifo_r_chan trace_fifo_w_chan |
|---|
| 290 | |
|---|
| 291 | filemap close trace_filemap |
|---|
| 292 | |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | proc slave_get_ports {} { |
|---|
| 298 | global ports_list |
|---|
| 299 | return $ports_list |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | proc slave_get_sandbox_violations {} { |
|---|
| 305 | global sandbox_violation_list |
|---|
| 306 | return $sandbox_violation_list |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | proc slave_add_sandbox_violation {path} { |
|---|
| 310 | global sandbox_violation_list |
|---|
| 311 | lappend sandbox_violation_list $path |
|---|
| 312 | } |
|---|