Skip to content

provenance: multi-block armored keyrings silently drop keys depending on block alignment #32567

Description

@TerryHowe

Follow-up to #32281, which added support for ASCII-armored keyrings made of several concatenated blocks (cat a.asc b.asc > keyring.asc). This applies once that PR is merged.

What happens

loadArmoredKeyRing (pkg/provenance/sign.go:395) loops over one *bytes.Reader:

r := bytes.NewReader(data)
for {
	block, err := armor.Decode(r)
	...
}

Each armor.Decode call wraps the reader in a fresh bufio.NewReaderSize(in, 100) and reads ahead. Whatever is sitting in that 100-byte buffer when the block ends is discarded along with the buffer, so the next Decode can start past the following -----BEGIN line. It then finds no armor header, returns io.EOF, and the loop breaks — with no error and a keyring that is silently missing keys.

go-crypto documents this explicitly:

Decode reads a PGP armored block from the given Reader. [...] The given Reader is not usable after calling this function: an arbitrary amount of data may have been read past the end of the block.

When it triggers

How far the decoder over-reads is a function of the length of the previous block's last base64 line, which armor wraps at 64 chars — so it is effectively a per-key constant (total_base64_len % 64).

Generating Ed25519 keys with varying UID lengths and concatenating each one with the committed testdata/helm-test-key.asc:

last base64 line = 32, 36, 40, 44, 48, 52, 56  ->  1 entity loaded, want 2, err == nil
last base64 line = 60 (and most other lengths) ->  2 entities, OK

Roughly one key in nine drops the block that follows it.

testdata/helm-mixed-keyring.asc ends its first block on a 60-char line, where the over-read stops 2 bytes short of the next -----BEGIN. So TestLoadKeyRingArmoredMultiBlock passes on fixture alignment rather than on correctness — it would pass just as happily against the buggy implementation.

User-visible symptom

A concatenated armored keyring loads without error but is short one or more keys, and verification then fails with "signed by an unknown key" for a key that is present in the file. No integrity impact — dropping keys makes verification stricter, never more permissive.

Suggested fix

Hand each armored block its own reader:

begin := bytes.Index(rest, []byte("-----BEGIN "))
if begin < 0 {
	break
}
end := bytes.Index(rest[begin:], []byte("-----END "))
if end < 0 {
	return nil, errors.New("armored block is missing its end line")
}
end += begin
if nl := bytes.IndexByte(rest[end:], '\n'); nl >= 0 {
	end += nl + 1
} else {
	end = len(rest)
}
block, err := armor.Decode(bytes.NewReader(rest[begin:end]))
...
rest = rest[end:]

This passes all 70 generated alignments as well as both committed fixtures.

Test

Worth adding a fixture whose first block ends on a short last base64 line (32-56 chars), so the multi-block test fails against the current code and pins the behavior against a future refactor back to a shared reader.

Activity

  1. added
    help wanted
    on Aug 23, 2026
  2. sandaruwijewardhana commented on Aug 23, 2026

    @sandaruwijewardhana

    Hi @TerryHowe I'd like to pick this up if it's still available.
    I went through your analysis and I followed up hte root cause of that armor.Decode wraps the reader in a bufio.NewReaderSize(in, 100) and can read ahead past the end of a block, and because we reuse the same *bytes.Reader across iterations, those buffered bytes are discarded. So the next Decode would start after the following BEGIN, hit no armor header, and the loop breaks with err == nil and a key silently missing.

    My plan is to stop sharing one reader across blocks and to split data into individual armored blocks on the BEGIN … END boundaries and decode each with its own reader, so an over-read can't consume into the next block. I'll also add a test that concatenates keys with varying UID lengths so it actually exercises the base64 line alignment (right now TestLoadKeyRingArmoredMultiBlock passes on fixture alignment rather than correctness, as you pointed out) and assert the full key count.

    Does it sounds good or any suggestions?

  3. shashankxrm commented on Aug 23, 2026

    @shashankxrm

    Hi @TerryHowe

    I've dug into the root cause and here's what's happening: armor.Decode() reads ahead into a 100-byte buffer for performance, but we're reusing the same *bytes.Reader across multiple blocks. When that buffer gets thrown away between iterations, any lookahead data is lost. The next decode starts blind and can't find the -----BEGIN marker.

    The alignment issue is the nasty part. Since armor wraps base64 at 64 chars, the over-read distance depends on where the previous block's base64 ends (total_len % 64). Most alignments still catch the next -----BEGIN, but 32-56 char endings over-read enough to skip it entirely. That's why the test passes—the fixture just happens to land on a safe alignment.

    What I'd change

    I'd break the dependency on a shared reader: manually extract each block's BEGIN/END boundaries and give each its own fresh bytes.NewReader. This completely eliminates the buffer interaction.

    For tests, I want to generate a key that forces a "bad" alignment and concatenate it with the existing test. The old test would fail on the current code, proving the bug exists, then pass with the fix. I'd also add a parametrized test covering all major alignment points to ensure we don't regress.

    One thing I want to clarify

    Should the parser fail fast on the first malformed block, or should it try to skip bad blocks and load what's valid? The current code bails out, but I want to make sure that matches the intended behavior for keyrings.

    I can have a PR ready quickly—the code change is small but the test coverage is important to get right.

  4. added a commit that references this issue on Aug 25, 2026
    310c7ce
  5. Asthenia0412 commented on Oct 13, 2026

    @Asthenia0412

    Claiming this issue — will fork, fix the multi-block armored keyring reader bug in pkg/provenance/sign.go, add a regression test fixture with a short first-block alignment, and open a PR.

  6. Asthenia0412 commented on Oct 13, 2026

    @Asthenia0412

    PR created: #32747 — per-block reader fix + regression test covering short last-base64-line alignment across multiple concatenation layouts. Let me know if you need adjustments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions