Debian Codegraph @

testsuite/systemtap.http_server/server_trust.exp

raw
1 # In this test, we are using arrays as sets. The key itself does not matter,
2 # only the value
3
4 set test "server_trust"
5 # exp_internal 1
6
7 if {! [installtest_p]} { untested "client"; return }
8 if {! [nss_p]} { untested "client (no nss)"; return }
9
10 # Note we have to be root (to get the namespace to work correctly).
11 set effective_uid [exec /usr/bin/id -u]
12 if {$effective_uid != 0} { untested "client (must be root)"; return }
13
14 # Let's start with a clean slate in terms of trust.
15 exec rm -fr $env(SYSTEMTAP_DIR)/ssl
16
17 # arr given as list (e.g. [array_has [array get myarr] elem])
18 proc array_has {arr elem} {
19
20 array set foo $arr
21 if {[array size foo] == 0} {
22 return 0
23 }
24
25 foreach {key val} [array get foo] {
26 if {[string equal $val $elem]} {
27 return 1
28 }
29 }
30
31 return 0
32 }
33
34 # Check if array1 is a subset of array2. Returns 1 if yes, otherwise 0.
35 # (Modified from http://wiki.tcl.tk/1032.)
36 proc array_in {array1 array2} {
37 upvar 1 $array1 sub $array2 super
38
39 if {![array exists sub]} {
40 return -code error "$array1 is not an array"
41 }
42 if {![array exists super]} {
43 return -code error "$array2 is not an array"
44 }
45 if {[array size sub] > [array size super]} {
46 return 0
47 }
48 if {[array size sub] == 0} {
49 return 1
50 }
51
52 foreach key [array names sub] {
53 if {![array_has [array get super] $sub($key)]} {
54 return 0
55 }
56 }
57 return 1
58 }
59
60 # Check if array1 and array2 have all the same elements
61 proc array_equal {array1 array2} {
62 upvar 1 $array1 foo $array2 bar
63 return [expr [array_in foo bar] && [array_in bar foo]]
64 }
65
66 # Returns the union of the three arrays
67 proc array_union {array1 array2 array3} {
68 upvar 1 $array1 foo $array2 bar $array3 baz
69 array unset ::union
70
71 if {![array exists foo]} {
72 return -code error "$array1 is not an array"
73 }
74 if {![array exists bar]} {
75 return -code error "$array2 is not an array"
76 }
77 if {![array exists baz]} {
78 return -code error "$array3 is not an array"
79 }
80
81 set n 0
82 foreach key [array names foo] {
83 set ::union($n) $foo($key)
84 incr n
85 }
86 foreach key [array names bar] {
87 if {![array_has [array get ::union] $bar($key)]} {
88 set ::union($n) $bar($key)
89 incr n
90 }
91 }
92 foreach key [array names baz] {
93 if {![array_has [array get ::union] $baz($key)]} {
94 set ::union($n) $baz($key)
95 incr n
96 }
97 }
98 }
99
100 # Returns all the elements in array_new not in array_old
101 proc array_diff {array_new array_old} {
102 upvar 1 $array_new anew $array_old aold
103 array unset ::diff
104
105 if {![array exists anew]} {
106 return -code error "$array_new is not an array"
107 }
108 if {![array exists aold]} {
109 return -code error "$array_old is not an array"
110 }
111
112 if {[array size anew] == 0} {
113 verbose -log "array_diff $array_new size = 0"
114 }
115 foreach {index value} [array get anew] {
116 verbose -log "array_diff $array_new index: $index, value: $value"
117 }
118 if {[array size aold] == 0} {
119 verbose -log "array_diff $array_old size = 0"
120 }
121 foreach {index value} [array get aold] {
122 verbose -log "array_diff $array_old index: $index, value: $value"
123 }
124
125 set n 0
126 foreach key [array names anew] {
127 if {![array_has [array get aold] $anew($key)]} {
128 set ::diff($n) $anew($key)
129 incr n
130 }
131 }
132 }
133
134 # Test the --list-servers option and return an array of the servers found.
135 proc list_servers { TEST_NAME SERVER_SPEC args } {
136 global systemtap_http_server_spec
137 set failed 0
138 set n 0
139 array unset ::servers
140 set cmd [concat stap --list-servers=$SERVER_SPEC $args --use-http-server=$systemtap_http_server_spec]
141 send_log "executing: $cmd\n"
142 eval spawn $cmd
143 expect {
144 -timeout 150
145 -re "^Systemtap Compile Server Status for '${SERVER_SPEC}'\r\n" {
146 exp_continue
147 }
148 -re {^No servers found\r\n} {
149 }
150 -re {^ host=[^\r]*\r\n} {
151 set ::servers($n) "$expect_out(0,string)"
152 incr n
153 exp_continue
154 }
155 -re {^No certificate found in database [^\r]*\r\n} {
156 exp_continue
157 }
158 -re {^[^\r]*\r\n} {
159 verbose -log "unexpected output: $expect_out(0,string)"
160 set failed 1
161 }
162 timeout {
163 verbose -log "timeout"
164 kill -INT -[exp_pid] 2
165 set failed 1
166 }
167 }
168 catch {close}; catch {wait}
169
170 if {$failed != 0} {
171 fail "$TEST_NAME"
172 } else {
173 pass "$TEST_NAME"
174 }
175 }
176
177 # Test the --list-servers option and return an array of the servers
178 # found in the custom namespace.
179 proc ns_list_servers { TEST_NAME SERVER_SPEC args } {
180 global systemtap_http_server_spec
181 set failed 0
182 set n 0
183 array unset ::servers
184 set cmd "stap --list-servers=$SERVER_SPEC $args --use-http-server=$systemtap_http_server_spec"
185 send_log "executing: $cmd\n"
186 lassign [server_ns_as_root $cmd] rc output
187 if {$rc == 0} {
188 # The command succeeded. Search the output for the servers.
189 foreach line [split $output \n] {
190 if {[regexp {^ host=(.*)$} $line match server]} {
191 set ::servers($n) $server
192 incr n
193 } elseif {[regexp "^Systemtap Compile Server Status for '${SERVER_SPEC}'$" $line]
194 || [regexp {^No servers found$} $line]
195 || [regexp {^No certificate found in database .*$} $line]} {
196 # Ignore this output
197 } else {
198 verbose -log "unexpected output: $line"
199 }
200 }
201 pass "$TEST_NAME"
202 } else {
203 fail "$TEST_NAME"
204 }
205 }
206
207 array set existing_online_servers {}
208 array set existing_trusted_servers {}
209 # array set existing_signing_servers {}
210
211 # Now start our own server and make sure we can work with it.
212 if {[http_start_server] == 0} {
213 pass "$test server start"
214 } else {
215 untested "$test server start"
216 return
217 }
218
219 # There may be existing trusted signers. Keep track of them.
220 list_servers "List existing signing servers" signer
221 array unset existing_signing_servers
222 array set existing_signing_servers [array get servers]
223
224 # Our server should now appear online, separate from the previously
225 # discovered online servers. Note that our server could generate
226 # serveral listings because it could appear at more than one ip
227 # address,
228 list_servers "List current online servers" online
229 array unset current_online_servers
230 array set current_online_servers [array get servers]
231
232 # array_diff will give us all the servers in current not in existing
233 array_diff current_online_servers existing_online_servers
234 array unset new_online_servers
235 array set new_online_servers [array get diff]
236
237 set test "New online servers"
238 if {[array size new_online_servers] > 0} {
239 pass "$test"
240 } else {
241 fail "$test"
242 }
243
244 # Our server should now be trusted, separate from the previously
245 # discovered trusted servers.
246 list_servers "List current trusted servers" online,trusted
247 array unset current_trusted_servers
248 array set current_trusted_servers [array get servers]
249
250 # array_diff will give us all the servers in current not in existing
251 array_diff current_trusted_servers existing_trusted_servers
252 array unset new_trusted_servers
253 array set new_trusted_servers [array get diff]
254
255 set test "New trusted servers"
256 if {[array size new_trusted_servers] > 0} {
257 pass "$test"
258 } else {
259 fail "$test"
260 }
261
262 # The new servers should automatically be trusted, so the
263 # new_trusted_servers array should be a subset of the
264 # new_online_servers array, but not necessarily vice-versa, since new
265 # servers may have come online independently of our testing.
266 set test "Verify new trusted server list"
267 if {[array_in new_trusted_servers new_online_servers]} {
268 pass "$test"
269 } else {
270 fail "$test"
271 }
272
273 # The newly trusted servers represent the server we just started.
274 array unset our_servers
275 array set our_servers [array get new_trusted_servers]
276
277 # The new servers should not be trusted as signers so there should be
278 # no new signing servers.
279 list_servers "List current signing servers" signer
280 array unset current_signing_servers
281 array set current_signing_servers [array get servers]
282
283 set test "No new signing servers"
284 if {[array_equal current_signing_servers existing_signing_servers]} {
285 pass "$test"
286 } else {
287 fail "$test"
288 }
289
290 # Revoke trust in our server. Specify the server by host name.
291 set test "Server has host name"
292 if {[array size our_servers] > 0} {
293 if {[regexp {^ host=([^ ]*).*} $our_servers(0) match host_name]} {
294 pass $test
295 } else {
296 fail $test
297 }
298 } else {
299 fail "$test no servers in list"
300 }
301
302 # Compile and run a simple hello test, selecting the server automatically
303 set test "Hello from server"
304 set rc [stap_run_batch $srcdir/systemtap.server/hello.stp --use-http-server=$systemtap_http_server_spec]
305 if {$rc == 0} { pass $test } else { fail $test }
306
307 set cmd [concat stap --trust-servers=ssl,revoke,no-prompt --use-http-server=$systemtap_http_server_spec]
308 send_log "executing: $cmd\n"
309 eval spawn $cmd
310 expect {
311 -timeout 150
312 -re {^.*\r\n} { exp_continue }
313 timeout {
314 kill -INT -[exp_pid] 2
315 set failed 1
316 }
317 }
318 catch {close}; catch {wait}
319
320 # Our server should no longer be trusted.
321 list_servers "List current trusted servers after revocation by host name" trusted
322 array unset current_trusted_servers
323 array set current_trusted_servers [array get servers]
324
325 set test "No longer trusted after revocation by host name"
326 if {[array_equal current_trusted_servers existing_trusted_servers]} {
327 pass "$test"
328 } else {
329 fail "$test"
330 }
331
332 # Reinstate trust in our server. Specify the server by ip address.
333 # The default for --trusted servers is 'ssl'.
334 set test "Server has ip address"
335 if {[array size our_servers] > 0} {
336 if {[regexp {^.*address=([^ ]*).*} $our_servers(0) match ip_address]} {
337 pass $test
338 } else {
339 fail $test
340 }
341 } else {
342 fail "$test no servers in list"
343 }
344 set cmd [concat stap --trust-servers=no-prompt --use-http-server=$systemtap_http_server_spec]
345 send_log "executing: $cmd\n"
346 eval spawn $cmd
347 expect {
348 -timeout 150
349 -re {^.*\r\n} { exp_continue }
350 timeout {
351 kill -INT -[exp_pid] 2
352 set failed 1
353 }
354 }
355 catch {close}; catch {wait}
356
357 # Our server should be trusted again, separate from the previously discovered
358 # trusted servers.
359 list_servers "List current trusted servers after reinstatement by ip address" online,trusted
360 array unset current_trusted_servers
361 array set current_trusted_servers [array get servers]
362
363 array_diff current_trusted_servers existing_trusted_servers
364 array unset new_trusted_servers
365 array set new_trusted_servers [array get diff]
366
367 set test "New trusted servers after reinstatement by ip address"
368 if {[array size new_trusted_servers] > 0} {
369 pass "$test"
370 } else {
371 fail "$test"
372 }
373
374 # The new_trusted_servers array should now match the our_servers
375 # array, since the our_servers array is a copy of the original
376 # new_trusted_servers array.
377 set test "New trusted servers matches after reinstatement by ip address"
378 if {[array size our_servers] > 0} {
379 if {[array_equal new_trusted_servers our_servers]} {
380 pass "$test"
381 } else {
382 fail "$test"
383 verbose -log "new_trusted_servers:"
384 foreach {index value} [array get new_trusted_servers] {
385 verbose -log " index: $index, value: $value"
386 }
387 verbose -log "our_servers:"
388 foreach {index value} [array get our_servers] {
389 verbose -log " index: $index, value: $value"
390 }
391 }
392 } else {
393 fail "$test no servers in list"
394 }
395
396
397 # Trust our server as a module signer. This must be done as root and
398 # in the namespace. Specify the server by certificate serial number.
399 set test "Server has certificate serial number"
400 if {[array size our_servers] > 0} {
401 if {[regexp {^.*certinfo="([^ ]*)".*} $our_servers(0) match cert_info]} {
402 pass $test
403 } else {
404 fail $test
405 }
406 } else {
407 fail "$test no servers in list"
408 }
409
410 # Grab the existing trusted signers in the namespace. There should be
411 # none (since we're starting with a clean slate).
412 set test "Namespace existing signer servers"
413 ns_list_servers "List namespace existing signing servers" signer
414 array unset ns_existing_signing_servers
415 array set ns_existing_signing_servers [array get servers]
416 if {[array size ns_existing_signing_servers] == 0} {
417 pass $test
418 } else {
419 fail $test
420 }
421
422 # We only want to change the system's trusted signer list in our mount
423 # namespace, not in the real system.
424 set test "Adding trusted signer (in a namespace)"
425 set cmd "stap --trust-servers=signer,no-prompt --use-http-server=$systemtap_http_server_spec"
426 lassign [server_ns_as_root $cmd] rc output
427 if {$rc == 0} {
428 pass "$test"
429 } else {
430 fail "$test"
431 }
432
433 # Our server should now be trusted as a signer (but only in our namespace).
434 ns_list_servers "List namespace current online signing servers" online,signer
435 array unset ns_current_signing_servers
436 array set ns_current_signing_servers [array get servers]
437
438 # The current list of signing servers in the namespace should have
439 # changed.
440 array_diff ns_current_signing_servers ns_existing_signing_servers
441 array unset ns_new_signing_servers
442 array set ns_new_signing_servers [array get diff]
443 set test "New namespace signing servers"
444 if {[array size ns_new_signing_servers] > 0} {
445 pass "$test"
446 } else {
447 fail "$test"
448 }
449
450 # The current list of signing servers shouldn't have changed in the
451 # real system (since the list of servers in the real system and in the
452 # namespace should be separate).
453 list_servers "List current online signing servers" online,signer
454 array unset current_signing_servers
455 array set current_signing_servers [array get servers]
456
457 array_diff current_signing_servers existing_signing_servers
458 array unset new_signing_servers
459 array set new_signing_servers [array get diff]
460 set test "New signing servers"
461 if {[array size new_signing_servers] == 0} {
462 pass "$test"
463 } else {
464 fail "$test"
465 }
466
467 set test "Server has port number"
468 if {[array size ns_new_signing_servers] > 0} {
469 if {[regexp {^.*port=([^ ]*).*} $ns_new_signing_servers(0) match port_num]} {
470 pass $test
471 } else {
472 fail $test
473 }
474 } else {
475 fail "$test no new signing servers"
476 }
477
478 foreach privilege {"--unprivileged" "--privilege=stapusr" "--privilege=stapsys"} {
479 # Compile a simple test using an unprivileged setting. This will
480 # ask the server to check and sign the module. Specify the server
481 # using host name and port.
482 #
483 # Notice we can use the server directly, and don't need to be in
484 # the namespace. We'll need to be in the namespace when trying to
485 # run the compiled module.
486 set test "Compile module using server with $privilege"
487 set failed 1
488 set module_name ""
489 set cmd [concat stap -p4 $privilege $srcdir/systemtap.server/hello.stp --use-http-server=$systemtap_http_server_spec]
490 send_log "executing: $cmd\n"
491 eval spawn $cmd
492 expect {
493 -timeout 150
494 -re {^stap_[^ \t\r\n]*\.ko\r\n} {
495 set module_name [string trim "$expect_out(0,string)" \r\n]
496 set failed 0
497 exp_continue
498 }
499 -re {^.*\r\n} { exp_continue }
500 timeout {
501 kill -INT -[exp_pid] 2
502 }
503 }
504 catch {close}; catch {wait}
505 if {$failed == 0} {
506 pass "$test"
507 }
508 send_log "'$module_name'\n"
509
510 # Make sure that the module was returned
511 set no_module 0
512 set test "Module was created with $privilege"
513 catch {exec /bin/ls $module_name $module_name.sgn} result
514 send_log "$result\n"
515 if {[file exists $module_name]} {
516 pass "$test"
517 } else {
518 fail "$test"
519 set no_module 1
520 }
521
522 # Make sure that the module was signed
523 set no_signature 0
524 set test "Module was signed with $privilege"
525 if {[file exists $module_name.sgn]} {
526 pass "$test"
527 } else {
528 fail "$test"
529 set no_signature 1
530 }
531
532 # Make sure we can load the module. This will verify that the
533 # signature is correct and trusted. There must be a module to
534 # load.
535 #
536 # Note that we must do this in the namespace, since only there can
537 # the signature be verified.
538 #
539 # We're going to use the optional 'stapusr' and 'stapsys' users,
540 # if they exist.
541 set test "Load and run signed module when trusted with $privilege"
542 set user ""
543 if {$privilege == "--privilege=stapsys"} {
544 if {$systemtap_stapsys_user_exists} {
545 set user "stapsys"
546 } elseif {$systemtap_stapusr_user_exists} {
547 set user "stapusr"
548 setup_xfail *-*-*
549 }
550 } else {
551 if {$systemtap_stapusr_user_exists} {
552 set user "stapusr"
553 }
554 }
555
556 if {$no_module == 1 || $no_signature == 1 || [string length $user] == 0} {
557 untested "$test"
558 } else {
559 set failed 1
560 set module_path [exec pwd]/$module_name
561 set cmd [concat staprun $module_path]
562 lassign [server_ns_as_user $user $cmd] rc output
563 if {$rc == 0} {
564 foreach line [split $output \n] {
565 if {[regexp {^Hello From Server$} $line]} {
566 set failed 0
567 }
568 }
569 }
570 if {$failed == 0} {
571 pass "$test"
572 } else {
573 fail "$test"
574 }
575 }
576 }
577
578 # Revoke trust in our server as a module signer in the namespace.
579 # Specify the server by certificate serial number to ensure we're
580 # revoking trust in the correct server.
581 set test "Revoking trusted signer (in a namespace)"
582 if {[info exists cert_info]} {
583 set cmd [concat stap --trust-servers=revoke,signer,no-prompt --use-http-server=$cert_info]
584 lassign [server_ns_as_root $cmd] rc output
585 if {$rc == 0} {
586 pass "$test"
587 } else {
588 fail "$test"
589 }
590 } else {
591 fail "$test no cert_info"
592 }
593
594 # Our server should no longer be trusted as a signer in the namespace.
595 ns_list_servers "List namespace current signing servers after revocation " signer
596 array unset ns_current_signing_servers
597 array set ns_current_signing_servers [array get servers]
598
599 set test "No longer trusted as a signer after revocation"
600 foreach {index value} [array get ns_current_signing_servers] {
601 verbose -log "ns_current_signing_servers: $index, value: $value"
602 }
603 foreach {index value} [array get ns_existing_signing_servers] {
604 verbose -log "ns_existing_signing_servers: $index, value: $value"
605 }
606 if {[array_in ns_current_signing_servers ns_existing_signing_servers]} {
607 pass "$test"
608 } else {
609 fail "$test"
610 }
611
612 # Since our server is no longer a trusted signer, attempting to load
613 # and run the module now should fail (when run as 'stapsys' in the
614 # namespace). We have to use the 'stapsys' user since the last module
615 # compiled used "--privilege=stapsys".
616 set test "Load and run signed module when not trusted"
617 if {$no_module == 1 || !$systemtap_stapsys_user_exists} {
618 untested "$test"
619 } else {
620 setup_xfail *-*-*
621 set failed 1
622 set module_path [exec pwd]/$module_name
623 set cmd [concat staprun $module_path]
624 lassign [server_ns_as_user "stapsys" $cmd] rc output
625 if {$rc == 0} {
626 foreach line [split $output \n] {
627 if {[regexp {^Hello From Server$} $line]} {
628 set failed 0
629 }
630 }
631 }
632 if {$failed == 0} {
633 pass "$test"
634 } else {
635 fail "$test"
636 }
637 }
638
639 # Shutdown the server we started
640 http_shutdown_server