All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib/asn1_encoder: fix encoding of zero OID components
@ 2026-11-20 13:00 zzz
  2026-11-24  6:03 ` Andrew Morton
  2026-11-24  6:03 ` + lib-asn1_encoder-fix-encoding-of-zero-oid-components.patch added to mm-nonmm-unstable branch Andrew Morton
  0 siblings, 2 replies; 7+ messages in thread
From: zzz @ 2026-11-20 13:00 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel, James.Bottomley

asn1_encode_oid_digit() encodes a zero component as 0x80, setting the
continuation bit on its only octet. A trailing zero therefore produces
an unterminated subidentifier, while an interior zero produces an
overlong encoding of the following component.

For example, 1.2.0 is encoded as 06 02 2a 80 instead of 06 02 2a 00.
Emit zero without the continuation bit, as required for the final
octet of a base-128 subidentifier.

OpenAI Codex was used to inspect the encoder, implement the fix, and
prepare the reproducer, regression tests and change description.

Fixes: b07067627cd5 ("lib: Add ASN.1 encoder")
Signed-off-by: zzz <abc0eichs@qq.com>
---
Prompt summary: inspect current mainline for a reproducible bug and
prepare a kernel-source fix following the Linux submission rules.

Validation:
- A local UML KUnit suite called the actual encoder with six OID
  vectors and short buffers. The original failed the zero-component
  vectors; the fixed kernel passed both subtests.
- GCC with ASan/UBSan: 45 checks of the actual source passed, covering
  zero components, base-128 boundaries and output-buffer capacities.
- OpenSSL rejected the original 1.2.0 and 1.2.0.1 encodings and decoded
  the corrected encodings successfully.
- x86_64 allnoconfig object build with W=1: no warnings.
- checkpatch --strict: clean, including the Signed-off-by tag.

The sole current in-tree caller uses a fixed, nonzero TPM OID whose
encoding is unchanged. This fixes the exported encoder's handling of
valid zero components; it does not claim an observed TPM failure.

 lib/asn1_encoder.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/asn1_encoder.c b/lib/asn1_encoder.c
index 92f35aae1..0cff56769 100644
--- a/lib/asn1_encoder.c
+++ b/lib/asn1_encoder.c
@@ -97,7 +97,7 @@ static int asn1_encode_oid_digit(unsigned char **_data, int *data_len, u32 oid)
 
 	/* quick case */
 	if (oid == 0) {
-		*data++ = 0x80;
+		*data++ = 0;
 		(*data_len)--;
 		goto out;
 	}

base-commit: 65e4fd942f72a9dba0e4e257290c4fa1aa756854
-- 
2.42.0.windows.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] lib/asn1_encoder: fix encoding of zero OID components
  2026-11-20 13:00 [PATCH] lib/asn1_encoder: fix encoding of zero OID components zzz
@ 2026-11-24  6:03 ` Andrew Morton
  2026-11-24  9:21   ` Jarkko Sakkinen
                     ` (3 more replies)
  2026-11-24  6:03 ` + lib-asn1_encoder-fix-encoding-of-zero-oid-components.patch added to mm-nonmm-unstable branch Andrew Morton
  1 sibling, 4 replies; 7+ messages in thread
From: Andrew Morton @ 2026-11-24  6:03 UTC (permalink / raw)
  To: zzz
  Cc: linux-kernel, James.Bottomley, Jarkko Sakkinen, David Howells,
	keyrings, linux-integrity

On Fri, 20 Nov 2026 21:00:55 +0800 zzz <abc0eichs@qq.com> wrote:
>

Sorry, "zzz" isn't usable.  See
Documentation/process/submitting-patches.rst under "Sign your
work":"using a known identity (sorry, no anonymous contributions.)".

So for now I'll rewrite this as From:me and Reported-by:zzz.  Please
send a 

	Signed-off-by: Your Real Name <email@address>

as a reply-to-all to this message and I shall alter the attributions
appropriately.

> asn1_encode_oid_digit() encodes a zero component as 0x80, setting the
> continuation bit on its only octet. A trailing zero therefore produces
> an unterminated subidentifier, while an interior zero produces an
> overlong encoding of the following component.
> 
> For example, 1.2.0 is encoded as 06 02 2a 80 instead of 06 02 2a 00.
> Emit zero without the continuation bit, as required for the final
> octet of a base-128 subidentifier.

Super-slim bug and it appears that nothing in the current tree uses
this code path.  But a bug is a bug.

> Validation:
> - A local UML KUnit suite called the actual encoder with six OID
>   vectors and short buffers. The original failed the zero-component
>   vectors; the fixed kernel passed both subtests.

Feel free to send along a patch which adds a KUint test for the
asn1_decoder code.

> The sole current in-tree caller uses a fixed, nonzero TPM OID whose
> encoding is unchanged. This fixes the exported encoder's handling of
> valid zero components; it does not claim an observed TPM failure.

yup.

> --- a/lib/asn1_encoder.c
> +++ b/lib/asn1_encoder.c
> @@ -97,7 +97,7 @@ static int asn1_encode_oid_digit(unsigned char **_data, int *data_len, u32 oid)
>  
>  	/* quick case */
>  	if (oid == 0) {
> -		*data++ = 0x80;
> +		*data++ = 0;
>  		(*data_len)--;
>  		goto out;
>  	}

Thanks,  Added to the pile!

^ permalink raw reply	[flat|nested] 7+ messages in thread

* + lib-asn1_encoder-fix-encoding-of-zero-oid-components.patch added to mm-nonmm-unstable branch
  2026-11-20 13:00 [PATCH] lib/asn1_encoder: fix encoding of zero OID components zzz
  2026-11-24  6:03 ` Andrew Morton
@ 2026-11-24  6:03 ` Andrew Morton
  1 sibling, 0 replies; 7+ messages in thread
From: Andrew Morton @ 2026-11-24  6:03 UTC (permalink / raw)
  To: mm-commits, jarkko, james.bottomley, dhowells, abc0eichs, akpm


The patch titled
     Subject: lib/asn1_encoder: fix encoding of zero OID components
has been added to the -mm mm-nonmm-unstable branch.  Its filename is
     lib-asn1_encoder-fix-encoding-of-zero-oid-components.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/lib-asn1_encoder-fix-encoding-of-zero-oid-components.patch

This patch will later appear in the mm-nonmm-unstable branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Andrew Morton <akpm@linux-foundation.org>
Subject: lib/asn1_encoder: fix encoding of zero OID components
Date: Fri, 20 Nov 2026 21:00:55 +0800

asn1_encode_oid_digit() encodes a zero component as 0x80, which says
another octet belongs to the same subidentifier.  If zero is the last
component, the encoding ends with an unterminated subidentifier.  If
another component follows, a decoder may consume its octets as part of the
zero component instead.  For example, 1.2.0.1 becomes 06 03 2a 80 01: DER
rejects it, while a permissive decoder may read it as 1.2.1.

Encode zero as 0x00, the complete one-octet representation of a zero
subidentifier.

Link: https://patch.msgid.link/tencent_AFE15D698FFD4F18E16A6EF7CDA002D69806@qq.com
Fixes: b07067627cd5 ("lib: Add ASN.1 encoder")
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reported-by: zzz <abc0eichs@qq.com>
Cc: James Bottomley <james.bottomley@hansenpartnership.com>
Cc: Jarkko Sakkinen <jarkko@kernel.org>
Cc: David Howells <dhowells@redhat.com>

 lib/asn1_encoder.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/lib/asn1_encoder.c~lib-asn1_encoder-fix-encoding-of-zero-oid-components
+++ a/lib/asn1_encoder.c
@@ -97,7 +97,7 @@ static int asn1_encode_oid_digit(unsigne
 
 	/* quick case */
 	if (oid == 0) {
-		*data++ = 0x80;
+		*data++ = 0;
 		(*data_len)--;
 		goto out;
 	}
_

Patches currently in -mm which might be from akpm@linux-foundation.org are

kselftest-mm-rename-read_pmd_pagesize-to-pmd_psize-fix.patch
selftests-mm-check-madv_cold-and-madv_pageout-on-partly-advised-thps-fix.patch
alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix-fix.patch
alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix.patch
selftests-mm-add-uffdio_move-huge-zeropage-pmd-regression-test-fix.patch
selftests-mm-allocate-nr_pages-based-on-pmd-size-for-uffd-pmd_move-tests-fix.patch
selftests-mm-retain-move-pmd-coverage-and-verify-huge-pmds.patch
lib-test_xarray-check-unused-spare-node-after-range-creation.patch
mm-memory-failure-dont-unpoison-a-leaked-raw_hwp_unreliable-folio-fix.patch
mm-vmstat_kunit-add-synthetic-benchmark-for-mm-stats-fix.patch
lib-test_hmm-invalidate-all-mirrors-on-device-private-migration.patch
lib-test_hmm-invalidate-all-mirrors-on-device-private-migration-fix.patch
mm-prevent-pre-content-bypass-through-mremap-and-remap_file_pages.patch
mm-madvise-notify-userfaultfd-when-installing-guard-regions.patch
afs-do-not-put-error-pointer-after-address-lookup-failure.patch
selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache.patch
selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-2.patch
selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-3.patch
selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-4.patch
selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-5.patch
selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-6.patch
selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-7.patch
fuse-let-memory-failure-remove-poisoned-page-cache-folios.patch
nilfs2-report-mapping-errors-from-fsync.patch
ceph-preserve-data-writeback-errors-in-fsync.patch
ocfs2-unregister-the-jbd2-shrinker-before-journal-shutdown.patch
ipc-msg-clamp-negative-msg_info-usage-counts.patch
squashfs-fix-leaked-page-locks-when-page-actor-allocation-fails-fix.patch
squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index-fix.patch
ocfs2-skip-network-flush-when-no-domain-handlers-are-registered.patch
selftests-ptrace-check-that-ptrace_set_syscall_info-rejects-invalid-error-values-fix.patch
jfs-validate-xattr-entry-bounds-before-replacing-an-attribute.patch
proc-serialize-task-owner-updates.patch
lib-asn1_encoder-fix-encoding-of-zero-oid-components.patch
drivers-media-v4l2-core-v4l2-vp9c-reduce-inlining.patch


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] lib/asn1_encoder: fix encoding of zero OID components
  2026-11-24  6:03 ` Andrew Morton
@ 2026-11-24  9:21   ` Jarkko Sakkinen
  2026-11-24 13:56   ` James Bottomley
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Jarkko Sakkinen @ 2026-11-24  9:21 UTC (permalink / raw)
  To: Andrew Morton
  Cc: zzz, linux-kernel, James.Bottomley, David Howells, keyrings,
	linux-integrity

On Mon, Nov 23, 2026 at 10:03:11PM -0800, Andrew Morton wrote:
> > For example, 1.2.0 is encoded as 06 02 2a 80 instead of 06 02 2a 00.
> > Emit zero without the continuation bit, as required for the final
> > octet of a base-128 subidentifier.
> 
> Super-slim bug and it appears that nothing in the current tree uses
> this code path.  But a bug is a bug.

It's a good catch :-)

But yeah, we need a person, this is not anonymous/pseudonym project but
the change itself is apppreciated.

Br, Jarkko

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] lib/asn1_encoder: fix encoding of zero OID components
  2026-11-24  6:03 ` Andrew Morton
  2026-11-24  9:21   ` Jarkko Sakkinen
@ 2026-11-24 13:56   ` James Bottomley
  2026-11-27 15:32   ` Chenshuo Zhang
  2026-11-27 15:59   ` Chenshuo Zhang
  3 siblings, 0 replies; 7+ messages in thread
From: James Bottomley @ 2026-11-24 13:56 UTC (permalink / raw)
  To: Andrew Morton, zzz
  Cc: linux-kernel, Jarkko Sakkinen, David Howells, keyrings,
	linux-integrity

On Mon, 2026-11-23 at 22:03 -0800, Andrew Morton wrote:
> On Fri, 20 Nov 2026 21:00:55 +0800 zzz <abc0eichs@qq.com> wrote:
> > --- a/lib/asn1_encoder.c
> > +++ b/lib/asn1_encoder.c
> > @@ -97,7 +97,7 @@ static int asn1_encode_oid_digit(unsigned char
> > **_data, int *data_len, u32 oid)
> >  
> >  	/* quick case */
> >  	if (oid == 0) {
> > -		*data++ = 0x80;
> > +		*data++ = 0;
> >  		(*data_len)--;
> >  		goto out;
> >  	}

Yes, well, I was just thinking about the single case when I wrote it.
Actually, wouldn't getting rid of all the special casing make this code
more maintainable.  Something like

diff --git a/lib/asn1_encoder.c b/lib/asn1_encoder.c
index 92f35aae13b1..fc97cb4d27fa 100644
--- a/lib/asn1_encoder.c
+++ b/lib/asn1_encoder.c
@@ -92,15 +92,9 @@ static int asn1_encode_oid_digit(unsigned char
**_data, int *data_len, u32 oid)
 	int start = 7 + 7 + 7 + 7;
 	int ret = 0;
 
-	if (*data_len < 1)
-		return -EINVAL;
-
 	/* quick case */
-	if (oid == 0) {
-		*data++ = 0x80;
-		(*data_len)--;
-		goto out;
-	}
+	if (oid >> 7 == 0)
+		goto encode_last;
 
 	while (oid >> start == 0)
 		start -= 7;
@@ -116,6 +110,7 @@ static int asn1_encode_oid_digit(unsigned char
**_data, int *data_len, u32 oid)
 		(*data_len)--;
 	}
 
+ encode_last:
 	if (*data_len > 0) {
 		*data++ = oid;
 		(*data_len)--;
@@ -123,7 +118,6 @@ static int asn1_encode_oid_digit(unsigned char
**_data, int *data_len, u32 oid)
 		ret = -EINVAL;
 	}
 
- out:
 	*_data = data;
 	return ret;
 }

?

James

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] lib/asn1_encoder: fix encoding of zero OID components
  2026-11-24  6:03 ` Andrew Morton
  2026-11-24  9:21   ` Jarkko Sakkinen
  2026-11-24 13:56   ` James Bottomley
@ 2026-11-27 15:32   ` Chenshuo Zhang
  2026-11-27 15:59   ` Chenshuo Zhang
  3 siblings, 0 replies; 7+ messages in thread
From: Chenshuo Zhang @ 2026-11-27 15:32 UTC (permalink / raw)
  To: keyrings

> So for now I'll rewrite this as From:me and Reported-by:zzz.

Thanks, Andrew. Please use Chenshuo Zhang for the patch attribution.

I tested James's suggestion to share the final-octet path for
single-octet subidentifiers. The UML KUnit tests and the x86_64 W=1
build passed. Would you prefer a v2 to replace the queued patch,
or a follow-up patch?

Signed-off-by: Chenshuo Zhang <abc0eichs@qq.com>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] lib/asn1_encoder: fix encoding of zero OID components
  2026-11-24  6:03 ` Andrew Morton
                     ` (2 preceding siblings ...)
  2026-11-27 15:32   ` Chenshuo Zhang
@ 2026-11-27 15:59   ` Chenshuo Zhang
  3 siblings, 0 replies; 7+ messages in thread
From: Chenshuo Zhang @ 2026-11-27 15:59 UTC (permalink / raw)
  To: linux-kernel, linux-integrity

> So for now I'll rewrite this as From:me and Reported-by:zzz.

Thanks, Andrew. Please use Chenshuo Zhang for the patch attribution.

I tested James's suggestion to share the final-octet path for
single-octet subidentifiers. The UML KUnit tests and the x86_64 W=1
build passed. Would you prefer a v2 to replace the queued patch,
or a follow-up patch?

Signed-off-by: Chenshuo Zhang <abc0eichs@qq.com>


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-27 16:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 13:00 [PATCH] lib/asn1_encoder: fix encoding of zero OID components zzz
2026-11-24  6:03 ` Andrew Morton
2026-11-24  9:21   ` Jarkko Sakkinen
2026-11-24 13:56   ` James Bottomley
2026-11-27 15:32   ` Chenshuo Zhang
2026-11-27 15:59   ` Chenshuo Zhang
2026-11-24  6:03 ` + lib-asn1_encoder-fix-encoding-of-zero-oid-components.patch added to mm-nonmm-unstable branch Andrew Morton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.