blob: 20be13649c04df3ee1dace8ea4c3b79447859428 (
plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
#!/bin/sh -u
# Copyright 2010 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Test the chromeos TPM recovery script by faking the entire execution
# environment.
rm -rf tpm-recovery-test-workdir
mkdir tpm-recovery-test-workdir
cd tpm-recovery-test-workdir
test_kind=
if [ $# -ge 1 ]; then
test_kind="$1"
fi
if [ "$test_kind" != "" -a "$test_kind" != "fake" ]; then
echo "$0: usage: $0 [fake]"
echo "With fake as the argument, use a simulated TPM instead of the real one"
fi
if [ "$test_kind" = "fake" ]; then
export USR_BIN=.
export USR_SBIN=.
export USR_LOCAL_BIN=.
export USR_LOCAL_SBIN=.
export DOT_RECOVERY=.recovery
export ACPI_DIR=.
ctr=../chromeos-tpm-recovery
tpmc=./tpmc
else
ctr=chromeos-tpm-recovery
tpmc=tpmc
fi
# For simplicity, build the permanent environment as if we prepared to run the
# fake test, even if we're running the test on a real TPM.
echo > .recovery
echo 3 > BINF.0
echo 0 > CHSW
export NVRAM_SPACE_OVERHEAD=200
space_overhead=$NVRAM_SPACE_OVERHEAD
# build tpmc
cat > tpmc <<"EOF"
#!/bin/sh -u
# Fake tpmc program
definespace () {
index=$2
size=$3
permissions=$4
space_overhead=$NVRAM_SPACE_OVERHEAD
if [ -e space.$index.data -a -e tpm-owned ]; then
echo "cannot redefine space without auth"
fi
totalsize=$(( $size + $space_overhead ))
free=$(cat nvram.freespace)
if [ $totalsize -gt $free ]; then
return 17 # NO_SPACE
fi
if [ $index != 0xf004 ]; then
echo $size > space.$index.size
echo $permissions > space.$index.perm
for i in $(seq 1 $(($size))); do
echo -n "ff " >> space.$index.data
done
echo $(( $free - $totalsize )) > nvram.freespace
fi
return 0
}
case $1 in
clear)
rm -f tpm-owned
;;
enable)
# boring
;;
activate)
# boring
;;
definespace)
definespace $*
;;
getp)
echo space blah has permissions $(cat space.$2.perm)
;;
read)
index=$2
size=$3
maxsize=$(cat space.$index.size)
if [ $(($size > $maxsize)) -eq 1 ]; then
echo "size $size too large for space (max is $maxsize)"
exit 1
fi
dd if=space.$index.data bs=1 count=$(($3 * 3)) 2> /dev/null
;;
write)
args="$@"
index=$2
bytes="$(echo $args | sed 's/[^ ]* [^ ]* //')"
size=$(echo $bytes | wc -w)
maxsize=$(cat space.$index.size)
if [ $(($size > $maxsize)) -eq 1 ]; then
echo "size $size too large for space (max is $(($maxsize)))"
exit 1
fi
re=$(echo "$bytes " | sed 's/././g')
sed "s/$re/$bytes /" < space.$index.data > _tmp_
mv _tmp_ space.$index.data
;;
getpf)
echo "disable 0"
echo "deactivated 0"
echo "nvLocked 1"
echo "physicalPresenceLifetimeLock 1"
echo "physicalPresenceHWEnable 0"
echo "physicalPresenceCMDEnable 1"
;;
getvf)
echo "bGlobalLock 1"
echo "physicalPresence 1"
echo "physicalPresenceLock 0"
;;
ppfin)
# boring
;;
ppon)
# boring
;;
*)
echo "tpmc: invalid command $1"
exit 1
;;
esac
EOF
# build nvtool
cat > tpm-nvtool <<"EOF"
#!/bin/sh -u
space_overhead=$NVRAM_SPACE_OVERHEAD
print_space () {
local index=$1
printf "# NV Index 0x%08x" $(( $index ))
echo " uninteresting random garbage"
echo " further random garbage"
echo ""
}
if [ "$1" = "--release" ]; then
if [ "$2" != "--index" -o \
"$4" != "--owner_password" ]; then
echo "sorry, picky tpm-nvtool"
exit 1
fi
index=$3
if [ ! -f tpm-owned ]; then
echo "tpm is unowned"
exit 1
fi
size=$(cat space.$index.size)
free=$(cat nvram.freespace)
rm space.$index.*
echo $(( $size + $space_overhead + $free )) > nvram.freespace
elif [ "$1" = "--list" ]; then
for s in space.*.data; do
print_space $(echo $s | sed -e "s/[^.]*\.//" -e "s/\..*//")
done
fi
EOF
# build tpm_takeownership
cat > tpm_takeownership <<"EOF"
#!/bin/sh -u
if [ -f tpm-owned ]; then
echo "tpm is already owned"
exit 1
fi
echo > tpm-owned
EOF
# build tcsd
cat > tcsd <<"EOF"
#!/bin/sh -u
trap "{ rm tcsd_is_running; }" EXIT
echo > tcsd_is_running
sleep 365d
EOF
tcsd_pid=0
start_tcsd () {
if [ $tcsd_pid -ne 0 ]; then
echo TCSD is already started
exit 1
fi
tcsd -f &
tcsd_pid=$!
sleep 2
}
stop_tcsd () {
if [ $tcsd_pid -eq 0 ]; then
echo TCSD is already stopped
exit 1
fi
kill $tcsd_pid
sleep 0.5
kill $tcsd_pid > /dev/null 2>&1
sleep 0.5
wait $tcsd_pid > /dev/null 2>&1 # we trust that tcsd will agree to die
tcsd_pid=0
}
tpm_clear_and_reenable () {
tpmc clear
tpmc enable
tpmc activate
}
takeownership () {
if [ "$test_kind" = "fake" ]; then
touch tpm_owned
else
tpm_clear_and_reenable
start_tcsd
tpm_takeownership -y -z
stop_tcsd
fi
}
remove_chromeos_spaces () {
if [ "$test_kind" = "fake" ]; then
rm -f space.*
echo 1500 > nvram.freespace
else
takeownership
start_tcsd
tpm-nvtool --release --index 0x1007 --owner_password ""
tpm-nvtool --release --index 0x1008 --owner_password ""
stop_tcsd
tpm_clear_and_reenable
fi
}
chmod 755 tpmc tpm-nvtool tpm_takeownership tcsd
echo "starting test, results in $(pwd)/log"
echo "starting TPM recovery test" > log
if ps ax | grep "tcs[d]"; then
echo "a tcsd is process appears to be running, please kill it first"
exit 1
fi
# normal run
test_normal_run () {
echo "TEST: normal run" >> log
remove_chromeos_spaces
$tpmc definespace 0x1007 0xa 0x8001
$tpmc definespace 0x1008 0xd 0x1
$tpmc write 0x1008 01 4c 57 52 47
takeownership
$ctr log
}
# Kernel space with wrong ID
test_wrong_id () {
echo "TEST: bad kernel space ID" >> log
remove_chromeos_spaces
$tpmc definespace 0x1007 0xa 0x8001
$tpmc definespace 0x1008 0xd 0x1
takeownership
$ctr log
}
# Kernel space with wrong size
test_wrong_size () {
echo "TEST: bad kernel space size" >> log
remove_chromeos_spaces
$tpmc definespace 0x1007 0xa 0x8001
$tpmc definespace 0x1008 0xc 0x1
takeownership
$ctr log
}
# Kernel space with wrong size AND bogus space to exhaust nvram
test_wrong_size_hog () {
echo "TEST: bad kernel space size and no room" >> log
remove_chromeos_spaces
$tpmc definespace 0x1007 0xa 0x8001
$tpmc definespace 0x1008 0x1 0x1
if [ "$test_kind" = "fake" ]; then
space_hog_size=$(( $(cat nvram.freespace) - $space_overhead - 1 ))
echo "remaining $(cat nvram.freespace) bytes" >> log
else
space_hog_size=$(( $(tpm-nvsize) - 2 ))
fi
echo "hogging $(( $space_hog_size )) bytes" >> log
$tpmc definespace 0xcafe $(printf "%#x" $space_hog_size) 0x1 \
|| echo "hogging failed!" >> log
takeownership
$ctr log
}
test_normal_run
test_wrong_id
test_wrong_size
test_wrong_size_hog
echo "test completed" >> log
echo "test completed"
|