-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.py
449 lines (399 loc) · 16.4 KB
/
test.py
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
from tempfile import mkdtemp
from shutil import rmtree, copy
import os
import os.path
import subprocess
import sys
import unittest
import rpm_head_signing
import rpm_head_signing.fix_signatures
import rpm_head_signing.verify_rpm
class TestRpmHeadSigning(unittest.TestCase):
pkg_numbers = ["1", "2"]
@classmethod
def setUpClass(cls):
cls.asset_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"test_assets",
)
def setUp(self):
self.tmpdir = mkdtemp(prefix="test-rpm_head_signing-", dir=os.path.abspath("."))
def tearDown(self):
rmtree(self.tmpdir)
self.tmpdir = None
def compare_files(self, asset_name, tmp_name):
with open(os.path.join(self.asset_dir, asset_name), "rb") as asset_file:
with open(os.path.join(self.tmpdir, tmp_name), "rb") as tmp_file:
self.assertEqual(
asset_file.read().strip(),
tmp_file.read().strip(),
"Asset file %s is different from tmp file %s"
% (asset_name, tmp_name),
)
def test_extract(self):
copy(
os.path.join(self.asset_dir, "testpkg-1.rpm"),
os.path.join(self.tmpdir, "testpkg-1.input.rpm"),
)
copy(
os.path.join(self.asset_dir, "testpkg-2.rpm"),
os.path.join(self.tmpdir, "testpkg-2.input.rpm"),
)
rpm_head_signing.extract_header(
os.path.join(self.tmpdir, "testpkg-1.input.rpm"),
os.path.join(self.tmpdir, "testpkg-1.rpm.hdr.tmp"),
os.path.join(self.tmpdir, "digests.out.tmp"),
)
rpm_head_signing.extract_header(
os.path.join(self.tmpdir, "testpkg-2.input.rpm"),
os.path.join(self.tmpdir, "testpkg-2.rpm.hdr.tmp"),
os.path.join(self.tmpdir, "digests.out.tmp"),
)
self.compare_files("testpkg-1.rpm", "testpkg-1.input.rpm")
self.compare_files("testpkg-2.rpm", "testpkg-2.input.rpm")
self.compare_files("testpkg-1.rpm.hdr", "testpkg-1.rpm.hdr.tmp")
self.compare_files("testpkg-2.rpm.hdr", "testpkg-2.rpm.hdr.tmp")
self.compare_files("digests.out", "digests.out.tmp")
def test_extract_no_digests(self):
rpm_head_signing.extract_header(
os.path.join(self.asset_dir, "testpkg-1.rpm"),
os.path.join(self.tmpdir, "testpkg-1.rpm.hdr.tmp"),
digest_out_path=None,
)
rpm_head_signing.extract_header(
os.path.join(self.asset_dir, "testpkg-2.rpm"),
os.path.join(self.tmpdir, "testpkg-2.rpm.hdr.tmp"),
digest_out_path=None,
)
self.compare_files("testpkg-1.rpm.hdr", "testpkg-1.rpm.hdr.tmp")
self.compare_files("testpkg-2.rpm.hdr", "testpkg-2.rpm.hdr.tmp")
def test_extract_non_hdrsign_able(self):
with self.assertRaises(rpm_head_signing.NonHeaderSignablePackage):
rpm_head_signing.extract_header(
os.path.join(
self.asset_dir, "sblim-cim-client-javadoc-1.3.9.1-1.el6.noarch.rpm"
),
os.path.join(
self.tmpdir,
"sblim-cim-client-javadoc-1.3.9.1-1.el6.noarch.rpm.hdr.tmp",
),
os.path.join(self.tmpdir, "digests.out.tmp"),
)
self.assertFalse(os.path.exists(os.path.join(self.tmpdir, "digests.out.tmp")))
result = rpm_head_signing.determine_rpm_status(
os.path.join(
self.asset_dir, "sblim-cim-client-javadoc-1.3.9.1-1.el6.noarch.rpm"
)
)
self.assertFalse(result.is_head_only_signable)
self.assertFalse(result.is_head_only_signed)
self.assertTrue(result.is_signed)
self.assertFalse(result.is_ima_signed)
def test_insert_no_ima(self):
self._add_gpg_key("gpgkey.asc")
for pkg in self.pkg_numbers:
copy(
os.path.join(self.asset_dir, "testpkg-%s.rpm" % pkg),
os.path.join(self.tmpdir, "testpkg-%s.rpm" % pkg),
)
res = subprocess.check_output(
[
"rpm",
"--dbpath",
self.tmpdir,
"-Kv",
os.path.join(self.tmpdir, "testpkg-%s.rpm" % pkg),
],
)
self.assertTrue(b"SHA1 digest: OK" in res)
self.assertFalse(b"Header V3 RSA" in res)
rpm_head_signing.insert_signature(
os.path.join(self.tmpdir, "testpkg-%s.rpm" % pkg),
os.path.join(self.asset_dir, "testpkg-%s.rpm.hdr.sig" % pkg),
)
res = subprocess.check_output(
[
"rpm",
"--dbpath",
self.tmpdir,
"-Kv",
os.path.join(self.tmpdir, "testpkg-%s.rpm" % pkg),
],
)
self.assertTrue(b"SHA1 digest: OK" in res)
self.assertTrue(b"Header V3 RSA" in res)
self.assertTrue(b"15f712be: ok" in res.lower())
result = rpm_head_signing.determine_rpm_status(
os.path.join(self.tmpdir, "testpkg-%s.rpm" % pkg)
)
self.assertTrue(result.is_head_only_signable)
self.assertTrue(result.is_head_only_signed)
self.assertTrue(result.is_signed)
self.assertFalse(result.is_ima_signed)
def test_insert_ima_presigned(self):
def insert_cb(pkg):
rpm_head_signing.insert_signature(
os.path.join(self.tmpdir, "testpkg-%s.rpm" % pkg),
os.path.join(self.asset_dir, "testpkg-%s.rpm.hdr.sig" % pkg),
ima_presigned_path=os.path.join(self.asset_dir, "digests.out.signed"),
)
self._ima_insertion_test(insert_cb, "15f712be")
def test_insert_ima_presigned_nonhdrsigned(self):
def insert_cb(pkg):
rpm_head_signing.insert_signature(
os.path.join(self.tmpdir, "testpkg-%s.signed.rpm" % pkg),
None,
ima_presigned_path=os.path.join(self.asset_dir, "digests.out.signed"),
)
self._ima_insertion_test(insert_cb, "9ab51e50", nonhdrsigned=True)
def test_insert_ima_byteorder_wrong(self):
copy(
os.path.join(self.asset_dir, "centos-stream-release-9.0-1.c9s.noarch.rpm"),
os.path.join(self.tmpdir, "centos-stream-release-9.0-1.c9s.noarch.rpm"),
)
rpm_head_signing.insert_signature(
os.path.join(self.tmpdir, "centos-stream-release-9.0-1.c9s.noarch.rpm"),
None,
os.path.join(self.asset_dir, "centos-stream.ima.digests.signed"),
)
if os.environ.get("SKIP_BYTEORDER_CHECK"):
raise unittest.SkipTest(
"Skipping remainer of byteorder test due to RPM bug"
)
siginfos = rpm_head_signing.get_rpm_ima_signature_info(
os.path.join(self.tmpdir, "centos-stream-release-9.0-1.c9s.noarch.rpm"),
)
for path in siginfos:
sig = siginfos[path]
self.assertEqual(
sig["error"],
None,
"Signature for %s had an error: %s" % (path, sig["error"]),
)
def test_verify_rpm(self):
# This is a negative test (on the unsigned RPM) to ensure verify_rpm is
# working as expected.
args = self._get_verify_rpm_args()
args.extend([os.path.join(self.asset_dir, "testpkg-1.rpm")])
args = rpm_head_signing.verify_rpm.get_args().parse_args(args)
self.assertFalse(rpm_head_signing.verify_rpm.main(args))
def test_fix_signatures(self):
return True
pkgname = "readline-8.1-4.el9.i686.rpm"
copy(
os.path.join(self.asset_dir, pkgname),
os.path.join(self.tmpdir, pkgname),
)
result = rpm_head_signing.fix_signatures.fix_ima_signatures(
os.path.join(self.tmpdir, pkgname),
dry_run=True,
)
self.assertEqual(result, rpm_head_signing.fix_signatures.CHANGED_IMA_SIG_LENGTH)
# We did a dry-run, this should not have changed the RPM
self.compare_files(
os.path.join(self.asset_dir, pkgname),
os.path.join(self.tmpdir, pkgname),
)
result = rpm_head_signing.fix_signatures.fix_ima_signatures(
os.path.join(self.tmpdir, pkgname),
dry_run=False,
)
self.assertEqual(result, rpm_head_signing.fix_signatures.CHANGED_IMA_SIG_LENGTH)
# Now we did an actual fix, so re-running the fixer should return no changes
result = rpm_head_signing.fix_signatures.fix_ima_signatures(
os.path.join(self.tmpdir, pkgname),
dry_run=True,
)
self.assertEqual(result, rpm_head_signing.fix_signatures.CHANGED_NONE)
res = subprocess.Popen(
[
"rpm",
"--dbpath",
self.tmpdir,
"-Kv",
os.path.join(self.tmpdir, pkgname),
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
res = res.communicate()[0]
self.assertTrue(b"SHA1 digest: OK" in res)
self.assertTrue(b"Header V3 RSA" in res)
rpm_head_signing.get_rpm_ima_signature_info(
os.path.join(self.tmpdir, pkgname),
)
def test_fix_signatures_valgrind(self):
return True
pkgname = "readline-8.1-4.el9.i686.rpm"
copy(
os.path.join(self.asset_dir, pkgname),
os.path.join(self.tmpdir, pkgname),
)
cmd = [sys.executable, "test_fix.py", os.path.join(self.tmpdir, pkgname)]
result = rpm_head_signing.fix_signatures.fix_ima_signatures(
os.path.join(self.tmpdir, pkgname),
dry_run=True,
)
self.assertEqual(result, rpm_head_signing.fix_signatures.CHANGED_IMA_SIG_LENGTH)
# We did a dry-run, this should not have changed the RPM
self.compare_files(
os.path.join(self.asset_dir, pkgname),
os.path.join(self.tmpdir, pkgname),
)
self._run_with_valgrind(cmd + ["active"])
self._check_valgrind_log()
# Now we did an actual fix, so re-running the fixer should return no changes
result = rpm_head_signing.fix_signatures.fix_ima_signatures(
os.path.join(self.tmpdir, pkgname),
dry_run=True,
)
self.assertEqual(result, rpm_head_signing.fix_signatures.CHANGED_NONE)
def test_insert_ima_valgrind_normal(self):
self._test_insert_ima_valgrind("normal", "15f712be")
def test_insert_ima_valgrind_normal_nonhdrsigned(self):
self._test_insert_ima_valgrind("normal", "9ab51e50", nonhdrsigned=True)
def test_insert_ima_valgrind_splice_header(self):
self._test_insert_ima_valgrind("splice_header", "15f712be")
def test_insert_ima_valgrind_splice_header_nonhdrsigned(self):
self._test_insert_ima_valgrind("splice_header", "9ab51e50", nonhdrsigned=True)
def _run_with_valgrind(self, command):
if os.environ.get("SKIP_VALGRIND"):
raise unittest.SkipTest("Valgrind tests are disabled")
self.valgrind_logfile = os.environ.get(
"VALGRIND_LOG_FILE",
"%s/valgrind.log" % self.tmpdir,
)
cmd = [
"valgrind",
"--tool=memcheck",
"--track-fds=yes",
"--leak-check=full",
"--track-origins=yes",
"--log-file=%s" % self.valgrind_logfile,
"--",
] + command
subprocess.check_call(cmd)
def _check_valgrind_log(self):
with open(self.valgrind_logfile, "r") as logfile:
log = logfile.read()
if os.environ.get("PRINT_VALGRIND_LOG"):
print("---- START OF VALGRIND LOG ----")
print(log)
print("---- END OF VALGRIND LOG ----")
if "insertlib.c" in log:
if os.environ.get("VALGRIND_ALLOW_INSERTLIB"):
print(
"Would normally have failed the test due to insertlib.c found in valgrind log"
)
else:
raise Exception("insertlib.c found in the Valgrind log")
def _test_insert_ima_valgrind(self, insert_mode, rpm_keyid, nonhdrsigned=False):
def insert_cb(pkg):
insert_command = [
sys.executable,
"test_insert.py",
insert_mode,
]
if nonhdrsigned:
rpm_path = os.path.join(self.tmpdir, "testpkg-%s.signed.rpm" % pkg)
sig_path = "none"
else:
rpm_path = os.path.join(self.tmpdir, "testpkg-%s.rpm" % pkg)
sig_path = os.path.join(self.asset_dir, "testpkg-%s.rpm.hdr.sig" % pkg)
self._run_with_valgrind(
insert_command
+ [
rpm_path,
sig_path,
os.path.join(self.asset_dir, "digests.out.signed"),
]
)
self._ima_insertion_test(insert_cb, rpm_keyid, nonhdrsigned=nonhdrsigned)
self._check_valgrind_log()
def _add_gpg_key(self, key_file_name):
subprocess.check_call(
[
"rpm",
"--dbpath",
self.tmpdir,
"--import",
os.path.join(self.asset_dir, key_file_name),
]
)
def _ima_insertion_test(self, insert_command, rpm_keyid, nonhdrsigned=False):
if nonhdrsigned:
self._add_gpg_key("puiterwijk.gpgkey.asc")
else:
self._add_gpg_key("gpgkey.asc")
rpm_paths = []
for pkg in self.pkg_numbers:
if nonhdrsigned:
rpm_filename = "testpkg-%s.signed.rpm" % pkg
else:
rpm_filename = "testpkg-%s.rpm" % pkg
copy(
os.path.join(self.asset_dir, rpm_filename),
os.path.join(self.tmpdir, rpm_filename),
)
rpm_paths.append(os.path.join(self.tmpdir, rpm_filename))
res = subprocess.check_output(
[
"rpm",
"--dbpath",
self.tmpdir,
"-Kv",
os.path.join(self.tmpdir, rpm_filename),
],
)
self.assertTrue(b"SHA1 digest: OK" in res)
self.assertFalse(b"Header V3 RSA" in res)
insert_command(pkg)
res = subprocess.check_output(
[
"rpm",
"--dbpath",
self.tmpdir,
"-Kv",
os.path.join(self.tmpdir, rpm_filename),
],
)
self.assertTrue(b"SHA1 digest: OK" in res)
msg = ("%s: ok" % rpm_keyid).encode("utf8")
self.assertTrue(msg in res.lower())
result = rpm_head_signing.determine_rpm_status(
os.path.join(self.tmpdir, rpm_filename)
)
self.assertTrue(result.is_head_only_signable)
self.assertTrue(result.is_head_only_signed)
self.assertTrue(result.is_signed)
self.assertTrue(result.is_ima_signed)
# Construct the arguments to verify_rpm
args = self._get_verify_rpm_args()
args.extend(rpm_paths)
args = rpm_head_signing.verify_rpm.get_args().parse_args(args)
self.assertTrue(rpm_head_signing.verify_rpm.main(args))
def _get_verify_rpm_args(self):
args = []
args.extend(
[
"--tmp-path-dir",
self.tmpdir,
"--cert-path",
os.path.join(self.asset_dir, "imacert.der"),
],
)
if sys.version_info.major != 3:
# X962 is only supported on Cryptography 2.5+
# We are a bit lazy and just check for py3 instead of checking
# this more carefully
args.append("--skip-keyid-check")
if os.environ.get("SKIP_IMA_LIVE_CHECK"):
args.append("--lite-verify")
evmctl_help = subprocess.check_output(["evmctl", "--help"])
if b"--xattr-user" not in evmctl_help:
if not os.environ.get("ONLY_ALTERNATIVE_EVMCTL_CHECK"):
raise Exception("Can't test evmctl")
args.append("--skip-evmctl")
return args
if __name__ == "__main__":
unittest.main()