Skip to content

fix: arena only for sentinel ctx_offset; stable heap frames (issue #15) - #26

Merged
sleevetosleeve merged 1 commit into
vlm_masterfrom
vibe/ext-ctx-arena-sentinel-only-d2e85d
Nov 28, 2026
Merged

sleevetosleeve merged 1 commit into
vlm_masterfrom
vibe/ext-ctx-arena-sentinel-only-d2e85d

Conversation

@sleevetosleeve

Copy link
Copy Markdown
Owner

Summary

Fixes the build-and-check failure from the manual c-cpp.yml run 46124008604 on vlm_master (PKIX1 example, check-ber: sample-Certificate-1.der: Decode failed ... Input processing error). Root cause was in the external decode context from #21/#22, in two parts:

  1. Arena consulted whenever present. asn_get_struct_ctx() used the arena for any ctx_offset when the codec context carried one. But the ANY text/JER/XER adapters (ANY.c ANY_text_prepare) decode through stack-temporary view structs whose address and specifics are reused across sequential ANY values — so arena frames keyed on them collided and the certificate's second ANY decoded as already-finished (0 bytes). Fix: the arena is used only for the ASN_CTX_OFFSET_EXTERNAL sentinel (-fno-ctx-members code); a real offset always resolves to the embedded member. The (st, type_key) frame keying from feat: onboard converter-example, document and harden the external ctx (-fno-ctx-members, issue #15) #21 stays — ext-mode code is all-sentinel, where offset-0 inline members still alias.
  2. Realloc-moved frame array. Heap-overflow frames lived in a realloc-grown array of frames; decoders hold asn_struct_ctx_t * across nested decode calls, so growing the array (PKIX1 certificate in ext mode needs 42 frames) left those pointers dangling — heap-use-after-free in SET_OF_decode_ber under ASan. Fix: the overflow is now an array of pointers to individually CALLOC'd frames; frame addresses are stable for the arena's lifetime. check-ext-ctx now also asserts frame pointers survive array growth.

Bisection: example passed at d7d0f7e (post-#18), failed at 8d23fe6 (post-#21); 8d23fe6 skeletons + pre-#21 converter-example passed, isolating the converter/arena path; API repro showed plain asn_decode OK vs asn_decode_ctx failing, then the ANY stack-view collision, then the UAF.

Related issues

Type of change

  • Bug fix

Testing

  • PKIX1 example make check passes in both codegen modes (default and -fno-ctx-members): all formats, buffer sizes 1/17/33/980/8192, junk tests — this is the exact CI step that failed.
  • Certificate decodes under ASan in ext mode (42 arena frames, heap overflow exercised) with clean ASN_STRUCT_FREE_CTX.
  • tests-skeletons 71/71 (incl. updated check-ext-ctx), tests-c-compiler 94/94, smoke 4/4 (check-ext-ctx.sh, check-BER-retag.sh, check-CER.sh, check-issue-617.sh), libasn1fix 5/5, check-610 pair 2/2.

Checklist

  • Up to date with vlm_master
  • CI passes (manual Actions per repo policy — this is the run to validate with)
  • Docs/ChangeLog updated (external-ctx-design.md decision log, ChangeLog)

Two fixes for the external decode context, found via the manual c-cpp.yml
run on vlm_master (PKIX1 example, build-and-check job):

1. asn_get_struct_ctx() consulted the arena whenever the codec context
   carried one. The ANY text/JER/XER adapters (ANY.c ANY_text_prepare) decode
   through stack-temporary view structs whose address and specifics are
   reused across sequential ANY values, so arena frames keyed on them
   collided and the PKIX1 certificate failed to decode (check-ber). Now the
   arena is used only for ASN_CTX_OFFSET_EXTERNAL (-fno-ctx-members code);
   a real ctx_offset always resolves to the embedded member. The
   (st, type_key) frame keying stays: ext-mode code is all-sentinel, where
   offset-0 inline members still alias.

2. Heap-overflow frames were stored in a realloc-grown array of frames;
   decoders hold asn_struct_ctx_t* across nested decode calls, so growth
   left them dangling (heap-use-after-free in SET_OF_decode_ber on the PKIX1
   certificate in ext mode, >8 live frames, ASan). The overflow is now an
   array of pointers to individually CALLOC'd frames; frame addresses are
   stable for the arena lifetime. check-ext-ctx additionally asserts frame
   pointers survive array growth.

Verified: PKIX1 example make check passes in BOTH codegen modes (all
formats, buffer sizes 1..8192, junk tests); certificate decodes under ASan
in ext mode (42 frames) with clean free; tests-skeletons 71/71,
tests-c-compiler 94/94, smoke 4/4 (incl. check-ext-ctx.sh, check-BER-retag,
check-CER, check-issue-617), libasn1fix 5/5, check-610 pair 2/2.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>

Co-authored-by: sleevetosleeve <sleevetosleeve@users.noreply.github.com>
@sleevetosleeve
sleevetosleeve marked this pull request as ready for review November 28, 2026 10:26
@sleevetosleeve
sleevetosleeve merged commit 5e3cd97 into vlm_master Nov 28, 2026
@sleevetosleeve
sleevetosleeve deleted the vibe/ext-ctx-arena-sentinel-only-d2e85d branch November 28, 2026 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants