Debian Codegraph @

testsuite/systemtap.server/server_privilege.exp

raw
1 # Let's try testing with optional users that only have certain
2 # privileges. This will allow us to do more privilege/signing
3 # testing.
4
5 set test "server_privilege"
6 if {! [installtest_p]} { untested $test; return }
7 if {! [nss_p]} { untested "$test (no NSS)"; return }
8
9 # Note we have to be root.
10 set effective_uid [exec /usr/bin/id -u]
11 if {$effective_uid != 0} { untested "$test (must be root)"; return }
12
13 proc compile_with_privilege { test_name privilege script } {
14 global server_spec srcdir
15
16 set cmd [concat stap -p4 $privilege --use-server=$server_spec $script ]
17 send_log "executing: $cmd\n"
18 set failed 1
19 set module_name ''
20 eval spawn $cmd
21 expect {
22 -timeout 150
23 -re {^stap_[^ \t\r\n]*\.ko\r\n} {
24 set module_name [string trim "$expect_out(0,string)" \r\n]
25 set failed 0
26 exp_continue
27 }
28 -re {^.*\r\n} { exp_continue }
29 timeout {
30 kill -INT -[exp_pid] 2
31 }
32 }
33 catch {close}
34 set rc [wait -i $spawn_id]
35 set rc [lindex $rc 3]
36 set module_exists 0
37 set subtest "module compile"
38 if {$rc == 0 && $failed == 0 && [string length $module_name]} {
39 pass "$test_name ($subtest)"
40
41 # Make sure that the module and module signature were
42 # returned. The module signature isn't required for 'stapdev'
43 # privilege.
44 if { $privilege eq "--privilege=stapdev" } {
45 set subtest "module exists"
46 if {[file exists $module_name]} {
47 set module_exists 1
48 pass "$test_name ($subtest)"
49 } else {
50 fail "$test_name ($subtest)"
51 }
52 } else {
53 set subtest "module and signature exist"
54 set module_sig "${module_name}.sgn"
55 if {[file exists $module_name] && [file exists $module_sig] } {
56 set module_exists 1
57 pass "$test_name ($subtest)"
58 } else {
59 fail "$test_name ($subtest)"
60 }
61 }
62 } else {
63 fail "$test_name ($subtest)"
64 }
65 return [list $module_exists $module_name]
66 }
67
68 # If we don't have any of the optional users, we could just quit. But,
69 # we'll go on and make sure the module compilation works.
70
71 # Now start our own server.
72 if {! [setup_server]} {
73 untested "$test (server start failure)"
74 return
75 }
76
77 ## We only want to change the system's trusted signer list in our mount
78 ## namespace, not in the real system.
79 set subtest "adding trusted signer"
80 set cmd "stap --trust-servers=signer,no-prompt --use-server=$server_spec"
81 lassign [server_ns_as_root $cmd] rc output
82 if {$rc == 0} {
83 pass "$test - $subtest"
84 } else {
85 fail "$test - $subtest"
86 }
87
88 # OK, now we've got a server. Use it to compile an unprivileged module
89 set subtest "unprivileged compile"
90 set full_test "$test - $subtest"
91 lassign [compile_with_privilege $full_test "--unprivileged" \
92 $srcdir/systemtap.server/hello.stp] module_exists module_name
93
94 # Make sure we can load the module. This will verify that the
95 # signature is correct and trusted.
96 if {$module_exists} {
97 set module_path [exec pwd]/$module_name
98 set unprivileged_module_path $module_path
99
100 # All the users should be able to load this module.
101 set subtest "stapusr unprivileged module run"
102 set cmd "staprun $module_path"
103 if {$systemtap_stapusr_user_exists} {
104 lassign [server_ns_as_user "stapusr" $cmd] rc output
105 if {$rc == 0} {
106 pass "$test - $subtest"
107 } else {
108 fail "$test - $subtest"
109 }
110 } else {
111 untested "$test - $subtest"
112 }
113 set subtest "stapsys unprivileged module run"
114 if {$systemtap_stapsys_user_exists} {
115 lassign [server_ns_as_user "stapsys" $cmd] rc output
116 if {$rc == 0} {
117 pass "$test - $subtest"
118 } else {
119 fail "$test - $subtest"
120 }
121 } else {
122 untested "$test - $subtest"
123 }
124 set subtest "stapdev unprivileged module run"
125 if {$systemtap_stapdev_user_exists} {
126 lassign [server_ns_as_user "stapdev" $cmd] rc output
127 if {$rc == 0} {
128 pass "$test - $subtest"
129 } else {
130 fail "$test - $subtest"
131 }
132 } else {
133 untested "$test - $subtest"
134 }
135 }
136
137 # Now compile a module using '--privilege=stapusr'. This should be the
138 # same thing as '--unprivileged'.
139 set subtest "stapusr privilege compile"
140 set full_test "$test - $subtest"
141 lassign [compile_with_privilege $full_test "--privilege=stapusr" \
142 $srcdir/systemtap.server/test.stp] module_exists module_name
143
144 # Make sure we can load the module. This will verify that the
145 # signature is correct and trusted.
146 if {$module_exists} {
147 set module_path [exec pwd]/$module_name
148 set stapusr_module_path $module_path
149
150 # All the users should be able to load this module.
151 set subtest "stapusr --privilege=stapusr module run"
152 set cmd "staprun $module_path"
153 if {$systemtap_stapusr_user_exists} {
154 lassign [server_ns_as_user "stapusr" $cmd] rc output
155 if {$rc == 0} {
156 pass "$test - $subtest"
157 } else {
158 fail "$test - $subtest"
159 }
160 } else {
161 untested "$test - $subtest"
162 }
163 set subtest "stapsys --privilege=stapusr module run"
164 if {$systemtap_stapsys_user_exists} {
165 lassign [server_ns_as_user "stapsys" $cmd] rc output
166 if {$rc == 0} {
167 pass "$test - $subtest"
168 } else {
169 fail "$test - $subtest"
170 }
171 } else {
172 untested "$test - $subtest"
173 }
174 set subtest "stapdev --privilege=stapusr module run"
175 if {$systemtap_stapdev_user_exists} {
176 lassign [server_ns_as_user "stapdev" $cmd] rc output
177 if {$rc == 0} {
178 pass "$test - $subtest"
179 } else {
180 fail "$test - $subtest"
181 }
182 } else {
183 untested "$test - $subtest"
184 }
185 }
186
187 # Now compile a module using '--privilege=stapsys'.
188 set subtest "stapsys privilege compile"
189 set full_test "$test - $subtest"
190 lassign [compile_with_privilege $full_test "--privilege=stapsys" \
191 $srcdir/systemtap.server/hello.stp ] module_exists module_name
192
193 # Make sure we can load the module. This will verify that the
194 # signature is correct and trusted.
195 if {$module_exists} {
196 set module_path [exec pwd]/$module_name
197 # Only the stapsys and stapdev users should be able to load this
198 # module.
199 set subtest "stapusr --privilege=stapsys module run"
200 set cmd "staprun $module_path"
201 if {$systemtap_stapusr_user_exists} {
202 lassign [server_ns_as_user "stapusr" $cmd] rc output
203 if {$rc == 0} {
204 fail "$test - $subtest"
205 } else {
206 pass "$test - $subtest"
207 }
208 } else {
209 untested "$test - $subtest"
210 }
211 set subtest "stapsys --privilege=stapsys module run"
212 if {$systemtap_stapsys_user_exists} {
213 lassign [server_ns_as_user "stapsys" $cmd] rc output
214 if {$rc == 0} {
215 pass "$test - $subtest"
216 } else {
217 fail "$test - $subtest"
218 }
219 } else {
220 untested "$test - $subtest"
221 }
222 set subtest "stapdev --privilege=stapsys module run"
223 if {$systemtap_stapdev_user_exists} {
224 lassign [server_ns_as_user "stapdev" $cmd] rc output
225 if {$rc == 0} {
226 pass "$test - $subtest"
227 } else {
228 fail "$test - $subtest"
229 }
230 } else {
231 untested "$test - $subtest"
232 }
233 }
234
235 # Now compile a module using '--privilege=stapdev'. Note that
236 # 'stapdev' compiles don't need a module signature.
237 set subtest "stapdev privilege compile"
238 set full_test "$test - $subtest"
239 lassign [compile_with_privilege $full_test "--privilege=stapdev" \
240 $srcdir/systemtap.server/hello.stp ] module_exists module_name
241
242 # Make sure we can load the module.
243 if {$module_exists} {
244 set module_path [exec pwd]/$module_name
245 # Only the stapdev user should be able to load this module.
246 set subtest "stapusr --privilege=stapdev module run"
247 set cmd "staprun $module_path"
248 if {$systemtap_stapusr_user_exists} {
249 lassign [server_ns_as_user "stapusr" $cmd] rc output
250 if {$rc == 0} {
251 fail "$test - $subtest"
252 } else {
253 pass "$test - $subtest"
254 }
255 } else {
256 untested "$test - $subtest"
257 }
258 set subtest "stapsys --privilege=stapdev module run"
259 if {$systemtap_stapsys_user_exists} {
260 lassign [server_ns_as_user "stapsys" $cmd] rc output
261 if {$rc == 0} {
262 fail "$test - $subtest"
263 } else {
264 pass "$test - $subtest"
265 }
266 } else {
267 untested "$test - $subtest"
268 }
269 set subtest "stapdev --privilege=stapdev module run"
270 if {$systemtap_stapdev_user_exists} {
271 lassign [server_ns_as_user "stapdev" $cmd] rc output
272 if {$rc == 0} {
273 pass "$test - $subtest"
274 } else {
275 fail "$test - $subtest"
276 }
277 } else {
278 untested "$test - $subtest"
279 }
280 }
281
282 # Let's try changing the module and signature file and make sure the
283 # verifcation fails. There isn't any point in trying this if we don't
284 # have the modules we need or the 'stapusr' or 'stapsys' optional
285 # users don't exist.
286 set subtest "module verification"
287 if {([info exists unprivileged_module_path]
288 && [info exists stapusr_module_path])
289 && ($systemtap_stapusr_user_exists || $systemtap_stapsys_user_exists)} {
290
291 # Use the shell to change the signature. This writes
292 # 0xdeadbeef at byte 10. (We could probably do this in tcl
293 # with 'binary format' and 'binary scan', but this was
294 # easier.
295 set rc 1
296 set unprivileged_module_sig_path "${unprivileged_module_path}.sgn"
297 if {$rc == 0} {
298 set cmd {printf '\\xdeadbeef' | dd of=${unprivileged_module_sig_path} bs=1 seek=10 conv=notrunc}
299 verbose -log "running: [subst $cmd]"
300 set rc 0
301 if {[catch {eval exec $cmd} output]} {
302 # non-zero exit status, get it:
303 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
304 set rc [lindex $::errorCode 2]
305 }
306 }
307 if {$rc != 0} {
308 verbose -log "$output"
309 fail "$test - $subtest (file change)"
310 set rc 1
311 } else {
312 pass "$test - $subtest (file change)"
313 set rc 0
314 }
315 }
316
317 if {$systemtap_stapusr_user_exists} {
318 set user "stapusr"
319 } else {
320 set user "stapsys"
321 }
322
323 # Now that we've changed the signature, try to run the module (which
324 # should fail). Note that we only need to run the module once with
325 # either stapusr or stapsys.
326 if {$rc == 0} {
327 set subtest "loading module with change signature"
328 set cmd "staprun $unprivileged_module_path"
329 lassign [server_ns_as_user $user $cmd] rc output
330 # This *should* fail, since we munged the signature.
331 if {$rc == 0} {
332 fail "$test - $subtest"
333 } else {
334 pass "$test - $subtest"
335 }
336 }
337
338 # Now lets change the module and see if we can load it. However,
339 # changing the module is tricky. If for some reason the module
340 # verifcation succeeds, if we've changed some random bits in the
341 # module, we could cause the kernel to crash. So, instead, we'll
342 # use the signature file for the 'stapusr' module with the
343 # unprivileged module.
344 set stapusr_module_sig_path "${stapusr_module_path}.sgn"
345 set subtest "module rename"
346 verbose -log "renaming $stapusr_module_sig_path $unprivileged_module_sig_path"
347 if {! [catch {file rename -force $stapusr_module_sig_path \
348 $unprivileged_module_sig_path}]} {
349 pass "$test - $subtest"
350
351 set subtest "loading module with mismatched signature"
352 set cmd "staprun $unprivileged_module_path"
353 lassign [server_ns_as_user $user $cmd] rc output
354 # This *should* fail, since the module and signature don't match.
355 if {$rc == 0} {
356 fail "$test - $subtest"
357 } else {
358 pass "$test - $subtest"
359 }
360 } else {
361 fail "$test - $subtest"
362 }
363 } else {
364 untested "$test - $subtest"
365 }
366
367 shutdown_server