Skip to content

updateMaxPacketLength permanently bricks the capacitor: modulo instead of division, plus an off-by-one in the pending count #439

Open

Description

@Jr-kenny

Problem

updateMaxPacketLength in contracts/capacitors/HashChainCapacitor.sol:72-101 (HEAD 130b1f8) has two compounding errors. When the owner decreases maxPacketLength, it over-creates packets, messagePacked advances past the messages that actually exist, zero roots are stored for phantom packets, and every later addPackedMessage reverts on a uint64 underflow (messageCount - messagePacked, panic 0x11). The capacitor is permanently bricked — no new messages can ever be added, and there is no setter for messagePacked to recover.

  1. Modulo instead of division. The number of complete packets of the new size among the pending messages is pending / newMax. The code computes pending % newMax — the remainder, not the count. When the remainder is larger than the true packet count, it over-creates packets.
  2. Pending count is off by one. nextMessageCount starts at 1 (1-indexed), so the messages actually added are nextMessageCount - 1. The code uses nextMessageCount - messagePacked, one more than the true pending count.

Either error alone can over-create packets; together they make the overshoot common.

Reproduction

Foundry, against the current code. Deploy HashChainCapacitor with max packet length 5, add 1 message (nextMessageCount=2, messagePacked=0), then as owner call updateMaxPacketLength(3).

Observed: packets = 2 % 3 = 2. Two packets are created, messagePacked becomes 6, both roots are zero. The next addPackedMessage reverts with panic 0x11.

It triggers even with zero messages pending: nextMessageCount is 1 with no messages, so the computed pending is 1 - 0 = 1, 1 % 3 = 1 packet is created, messagePacked becomes 3, and the very first message ever added reverts.

Scope

This is an owner-triggered permanent DoS on one capacitor, not an attacker exploit and not a fund loss. HashChainCapacitor is experimental and not deployed via the factory today (CapacitorFactory.deploy only creates SingleCapacitor), so this bricks a standalone contract, not the live deployment path. It matters if/when batching is re-enabled. It is a distinct defect from #436 / PR #438, which cover the stale-state MAX_LEN validation in the same function, not the modulo/off-by-one brick.

Fix

Replace the packet count with the true pending count divided by the new max. Both changes are required — fixing only one still overshoots in some cases.

uint64 pending = nextMessageCount - messagePacked - 1;
uint64 packets = pending / uint64(maxPacketLength_);

This does not touch the separate stale-state validation bug (#436/#438) — that has its own open PR.

Tests

  • New reproduction tests: 1 pending (shrink 5→3, messagePacked becomes 6, next adds revert), 4 pending (shrink 5→3, messagePacked becomes 6, next add reverts), 0 pending (shrink 5→3, messagePacked becomes 3, first add reverts). All three pass against the buggy code.
  • Fix-verification tests: five tests against the fixed contract (0/1/4/6 pending, shrink-after-some-packed), all pass. The three reproduction tests fail against the fixed contract ("did not revert as expected"), confirming the brick is gone.
  • The repo's existing 10 HashChainCapacitor.t.sol tests all pass against the fixed contract, no regression. No test coverage for updateMaxPacketLength existed in the repo before this.

I have the fix ready and will open the PR.

Activity

  1. Jr-kenny commented on Nov 12, 2026

    @Jr-kenny
    Author

    Hey, quick context since this is my first time here — I'm a developer who has been going through the capacitor contracts after the Socket exploit, checking whether the post-exploit code is sound. I was testing updateMaxPacketLength by shrinking the max packet length and the capacitor bricked, every later addPackedMessage reverts. Traced it to the modulo plus the off-by-one in the pending count. Fix is in #440.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions