lib/asn1_decoder: add KUnit test suite
Subsystems (2)
RFC PATCH 1/1 lib/asn1_decoder: add KUnit test suite
Add a KUnit test suite for the ASN.1 BER/DER decoder in lib/asn1_decoder.c. A tiny two-field grammar (SEQUENCE { INTEGER, OCTET STRING }) drives asn1_ber_decoder() with well-formed and malformed byte strings and checks that valid input is parsed and each class of malformed input is rejected with the documented error code rather than over-read.
The suite has 13 cases: one well-formed decode; eight malformed-input rejections (truncation, length overrun, long tag, wrong tag, oversize, short header, missing and malformed end-of-contents); three success-path cases (indefinite length with end-of-contents, long-form length, zero-length string); and one bounded deterministic random-stress case whose callbacks assert that every slice handed back lies within the input buffer (KASAN additionally catches any over-read).
Tested with KUnit on UML:
$ ./tools/testing/kunit/kunit.py run asn1_decoder ================ asn1_decoder (13 subtests) ================ [PASSED] asn1_test_decode_valid ... ================== [PASSED] asn1_decoder =================== Testing complete. Ran 13 tests: passed: 13
This suite was developed with the assistance of an AI coding assistant (Claude Opus 4.8): the test vectors and C were drafted with the assistant and reviewed by hand, and this changelog was written with its help. Beyond the KUnit run above, the decoder was checked against the suite with an independent verification kit -- exhaustive enumeration and randomized, grammar-biased and coverage-guided (libFuzzer) fuzzing under a guard page and KASAN/UBSan, CBMC bounded model checking of the real decoder, and Coq and Lean models of the no-over-read invariant; no over-read was found. That kit is maintained separately and is not part of this patch. I understand the code and take responsibility for it.
--- lib/Kconfig.debug | 17 ++ lib/tests/Makefile | 4 + lib/tests/asn1_decoder_kunit.c | 360 +++++++++++++++++++++++++++++++++ lib/tests/asn1_test.asn1 | 11 + 4 files changed, 392 insertions(+) create mode 100644 lib/tests/asn1_decoder_kunit.c create mode 100644 lib/tests/asn1_test.asn1
lib/Kconfig.debug +17 -0
@@ -2998,6 +2998,23 @@ config BASE64_KUNIT If unsure, say N. +config ASN1_DECODER_KUNIT_TEST + tristate "KUnit tests for the ASN.1 BER/DER decoder" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + select ASN1 + help + This builds the KUnit tests for the ASN.1 BER/DER decoder in + lib/asn1_decoder.c. A tiny grammar drives asn1_ber_decoder() with + well-formed and malformed input and checks that valid input is + parsed and each class of malformed input is rejected rather than + over-read. + + For more information on KUnit and unit tests in general please refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. + config BITS_TEST tristate "KUnit test for bit functions and macros" if !KUNIT_ALL_TESTS depends on KUNIT
lib/tests/Makefile +4 -0
@@ -60,3 +60,7 @@ obj-$(CONFIG_RATELIMIT_KUNIT_TEST) += test_ratelimit.o obj-$(CONFIG_UUID_KUNIT_TEST) += uuid_kunit.o obj-$(CONFIG_TEST_RUNTIME_MODULE) += module/ + +obj-$(CONFIG_ASN1_DECODER_KUNIT_TEST) += asn1_decoder_kunit_test.o +asn1_decoder_kunit_test-y := asn1_decoder_kunit.o asn1_test.asn1.o +$(obj)/asn1_decoder_kunit.o: $(obj)/asn1_test.asn1.h
lib/tests/asn1_decoder_kunit.c new file +360 -0
360 lines not on this page · raw message
lib/tests/asn1_test.asn1 new file +11 -0
@@ -0,0 +1,11 @@ +-- SPDX-License-Identifier: GPL-2.0-or-later +-- +-- Minimal grammar used only by the ASN.1 decoder KUnit tests +-- (lib/tests/asn1_decoder_kunit.c). It exercises a constructed type, a +-- primitive INTEGER and a primitive OCTET STRING, each with an action, which +-- is enough to drive the decoder's tag/length handling and action dispatch. + +TestData ::= SEQUENCE { + num INTEGER ({ asn1_test_note_int }), + str OCTET STRING ({ asn1_test_note_str }) + }
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.43.0
Jarkko Sakkinen
maintainerQuoted 6 lines · William Chuang
Add a KUnit test suite for the ASN.1 BER/DER decoder in
lib/asn1_decoder.c. A tiny two-field grammar (SEQUENCE { INTEGER, OCTET STRING }) drivesasn1_ber_decoder()with well-formed and malformed byte strings and checks that valid input is parsed and each class of malformed input is rejected with the documented error code rather than over-read.
1. I'm not convinced. Why overall?
2. Why KUnit?
3. Why host the test in kernel?
Br, Jarkko
Fair questions.
Why at all: asn1_ber_decoder() parses untrusted ASN.1 BER/DER input. X.509 certificates (keyrings) and PKCS#7 signatures (module signing) both go through it, and it has had a length-validation bug of this kind before — CVE-2016-0758, in asn1_find_indefinite_length().
Scope: the suite checks that malformed and truncated input is rejected rather than over-read, in the cases it exercises. It does not reproduce that CVE or prove the decoder memory-safe. That it isn’t vacuous: I weakened one bounds check in asn1_decoder.c to allow a short over-read. asn1_test_truncated_content then fails its return-value check — the decoder accepts a truncated SEQUENCE (returns 0 instead of -EBADMSG). The other 12 cases pass; reverting restores 13/13.
Why KUnit: it exercises the in-kernel decoder directly, with no userspace harness, and the cases can also run under KASAN/UBSAN (verified in my config). It is deterministic and is not built into production kernels.
Why in-kernel: the coverage travels with the code. An out-of-tree test does not run when someone changes lib/asn1_decoder.c.
I agree the maintenance has to be justified by the coverage. If the current cases are too thin to carry, I’m happy to strengthen or narrow them on your feedback.
Thanks,
William
Quoted 14 lines · Jarkko Sakkinen
Quoted 6 lines · William Chuang
Add a KUnit test suite for the ASN.1 BER/DER decoder in
lib/asn1_decoder.c. A tiny two-field grammar (SEQUENCE { INTEGER, OCTET STRING }) drivesasn1_ber_decoder()with well-formed and malformed byte strings and checks that valid input is parsed and each class of malformed input is rejected with the documented error code rather than over-read.1. I'm not convinced. Why overall?
2. Why KUnit?
3. Why host the test in kernel?Br, Jarkko
Jarkko Sakkinen
maintainerQuoted 9 lines · Founder – Logarcheon
Fair questions.
Why at all:
asn1_ber_decoder()parses untrusted ASN.1 BER/DER input. X.509 certificates (keyrings) and PKCS#7 signatures (module signing) both go through it, and it has had a length-validation bug of this kind before — CVE-2016-0758, inasn1_find_indefinite_length().
Scope: the suite checks that malformed and truncated input is rejected rather than over-read, in the cases it exercises. It does not reproduce that CVE or prove the decoder memory-safe. That it isn’t vacuous: I weakened one bounds check inasn1_decoder.cto allow a short over-read.asn1_test_truncated_contentthen fails its return-value check — the decoder accepts a truncated SEQUENCE (returns 0 instead of-EBADMSG). The other 12 cases pass; reverting restores 13/13.Why KUnit: it exercises the in-kernel decoder directly, with no userspace harness, and the cases can also run under KASAN/UBSAN (verified in my config). It is deterministic and is not built into production kernels.
Quoted 6 lines
Why in-kernel: the coverage travels with the code. An out-of-tree test does not run when someone changes
lib/asn1_decoder.c.I agree the maintenance has to be justified by the coverage. If the current cases are too thin to carry, I’m happy to strengthen or narrow them on your feedback.
Thanks,
William
I'd start by learning to use the email client properly at first:
https://docs.kernel.org/process/email-clients.html
Br, Jarkko
Jarkko Sakkinen
maintainerQuoted 12 lines · Jarkko Sakkinen
Quoted 11 lines · Founder – Logarcheon
Fair questions.
Why at all:
asn1_ber_decoder()parses untrusted ASN.1 BER/DER input. X.509 certificates (keyrings) and PKCS#7 signatures (module signing) both go through it, and it has had a length-validation bug of this kind before — CVE-2016-0758, inasn1_find_indefinite_length().
Scope: the suite checks that malformed and truncated input is rejected rather than over-read, in the cases it exercises. It does not reproduce that CVE or prove the decoder memory-safe. That it isn’t vacuous: I weakened one bounds check inasn1_decoder.cto allow a short over-read.asn1_test_truncated_contentthen fails its return-value check — the decoder accepts a truncated SEQUENCE (returns 0 instead of-EBADMSG). The other 12 cases pass; reverting restores 13/13.Why KUnit: it exercises the in-kernel decoder directly, with no userspace harness, and the cases can also run under KASAN/UBSAN (verified in my config). It is deterministic and is not built into production kernels.
Why in-kernel: the coverage travels with the code. An out-of-tree test does not run when someone changes
lib/asn1_decoder.c.
Quoted 8 lines
Quoted 4 lines
I agree the maintenance has to be justified by the coverage. If the current cases are too thin to carry, I’m happy to strengthen or narrow them on your feedback.
Thanks,
WilliamI'd start by learning to use the email client properly at first:
Within past 17 years the total number of commits is 22. This includes all the possible. Of these total six carry a Fixes tag.
That makes about 0.35 bug fixes annually. How do you explain this?
For me this looks whole a lot like LLM lottery i.e., sending random and useless patches in the hopes that something might happen to get in.
Your previous kunit supports this point of view given that it is a possible "LLM remix" of a patch (kunit for assoc_array) that I sent a month ago but we discarded the idea in favor of a better solution.
Stop doing this.
Br, Jarkko
Quoted 4 lines · Jarkko Sakkinen
Within past 17 years the total number of commits is 22. This includes all the possible. Of these total six carry a Fixes tag.
That makes about 0.35 bug fixes annually. How do you explain this
That is a fair measure. With a change to the decoder every nine months or so, a regression suite has little to catch, and its upkeep would fall on you and the other maintainers. I'm withdrawing this RFC.
Quoted 4 lines
Your previous kunit supports this point of view given that it is a possible "LLM remix" of a patch (kunit for
assoc_array) that I sent a month ago but we discarded the idea in favor of a better solution.
I had not seen your October patch when I sent mine. I should have searched lore first, and I will before sending anything further.
Quoted 1 line
Stop doing this.
Understood: no more test-only patches for this code from me. Sorry as well for the badly formatted reply earlier.
Thanks,
William