Blog · Copenhagen · October 2026

The limit overrun: one balance, many requests

On Sunday, 2 March 2014, someone opened an account at Flexcoin, a company that called itself “the bitcoin bank”, deposited a few coins, and sent the bank thousands of requests at once. By Monday the bank had closed.

Léon Gondelman, 13 October 2026

About fifteen minutes of reading, with one program in four versions.

Nothing was broken into. Every request was one the bank's own web page could send: move some coins from one of the sender's accounts to another. The code that served it was written the way most programmers would write it: look up the balance, check that it covers the amount, then subtract. Each of those steps is correct. Together, under many requests at once, they are not. This page follows that one bug, the limit overrun as the trade calls it, over ten years, from a bank that died of it to a technique that makes it reproducible, and ends with the three ways of controlling interleavings that session 7 of my Programming Paradigms course teaches, and what each does to it.

The same bug, ten years apart. The scale is time: from March 2014 at the left to the end of 2024 at the right.

Flexcoin and Poloniex, 2014

Flexcoin held its customers' bitcoins and let them pay each other inside the bank, a transfer between two Flexcoin accounts. On 3 March 2014 it announced that its hot wallet, the coins kept online, had been emptied: 896 bitcoins, about six hundred thousand dollars at the time. The next day it explained how. The sentences are the bank's own, as quoted by Computerworld.

“By sending thousands of simultaneous requests, the attacker was able to ‘move’ coins from one user account to another until the sending account was overdrawn, before balances were updated.”

“This was then repeated through multiple accounts, snowballing the amount, until the attacker withdrew the coins.”

Flexcoin's statement of 4 March 2014, in Lucian Constantin's report for Computerworld.

The account was overdrawn before balances were updated: the requests were checked against a balance that none of them had yet reduced. Each request, on its own, was refused or accepted correctly. Thousands of them, arriving together, were all accepted. The bank's announcement said that it did not have the resources to come back from the loss and that it was closing its doors immediately. The same week an exchange called Poloniex lost 12.3% of its customers’ bitcoins in the same way. Its owner described the mechanism more precisely than Flexcoin had: place several withdrawals “all in practically the same instant” and “they will get processed at more or less the same time”, which gives “a negative balance, but valid insertions into the database, which then get picked up by the withdrawal daemon”. He named two faults: withdrawals should have been queued and processed one after the other, and, the sentence this page is about, the auditing “were not explicitly looking for negative balances”. At Poloniex nobody had written down that a balance stays at or above zero, so nothing checked it.

The program

Here is what a program with this bug looks like in Java, the language of session 7 of my course. One gift card, worth 10. A request to redeem it for 10 reads the balance from the database, checks it, confirms the order, which takes a round trip or two to other services, about a millisecond, and then writes the new balance. In the shop, the read and the write are two database statements; each is atomic on its own, it takes effect as one indivisible step with nothing of another request's inside it, and the database sees to that. In Java I model each statement by a short block that only one thread at a time may run, a block under the card's lock (synchronized): another statement sees a statement's effect on the row whole or not at all, and another thread sees the block's the same way. The millisecond is a call that sleeps, a stand-in for the confirmation that, in this model, never fails. The stretch between a request's read and its write I call its window.

static int balance = 10;                  // the card, in the shop's database
static final Object card = new Object();

static boolean redeem(int x) {
  int b;
  synchronized (card) { b = balance; }    // SELECT balance             (one statement)
  if (b < x) return false;                // the check
  confirmOrder();                         // the order is built: about a millisecond
  synchronized (card) { balance -= x; }   // UPDATE balance = balance - x (one statement)
  return true;
}

Two requests arrive together. The table gives the balance after each step, in the order the shop expected and in the order the shop got.

StepOne after the otherbalanceTogetherbalance
1request 1 reads 10, passes the check10request 1 reads 10, passes the check10
2request 1 confirms the order10request 2 reads 10, passes the check10
3request 1 writes 10 − 100both confirm their orders10
4request 2 reads 0, is refused0request 1 writes 10 − 100
50request 2 writes 0 − 10−10
request 1 request 2 balance read 10 check ✓ confirm the order, ~1 ms write 0 read 10 check ✓ confirm the order, ~1 ms write −10 the window: between request 1's read and its write 10 0 −10 time →
Two requests on one balance. Request 2 reads inside request 1's window, so both pass the check; both write; the card worth 10 pays for 20 and ends at −10.

The program overrun/Overrun.java releases thirty threads at once on the card, as the attacker's thousands of requests were released on Flexcoin. One run:

race: card worth 10, 30 requests of 10: accepted 30, balance left -290

Twenty runs on a laptop, twenty times the same line. The millisecond is wide enough for all thirty reads to fall inside the first request's window, and no write is lost: every decrement is there, which is how the balance reaches −290. What is lost is the check. The bug has a name in the security literature, time of check to time of use: the fact that was checked had changed by the time it was used. In the vocabulary of the course, the invariant of the card, balance ≥ 0, is maintained by each step and broken by the interleaving of two, the order in which the steps of two requests happen to run.

Starbucks, 2015

A year later the same bug paid for coffee. In the spring of 2015 the security researcher Egor Homakov looked at the page where Starbucks lets a customer move money between two of their gift cards. The transfer took two requests: the first saved the amount and the two cards in the customer's session, the second performed the transfer and cleared the session. Two browser sessions, both sending the second request at the same moment, and a card worth five dollars might pay out twice. The window was small and the network's delays varied: Homakov writes that after five failed attempts he was about to give up, and that the sixth created two five-dollar transfers from a card that held five. Three cards bought at five dollars each, fifteen in all, now held twenty. He put fifteen dollars of his own money in, spent sixteen dollars seventy at a Starbucks to prove the money was real, and reported the bug on 23 March. The answer came on 29 April; the representative who called spoke of fraud and malicious actions.

The bug was the one of Flexcoin, in a company of another size and another kind. And it took six tries. The two requests had to arrive within the window, and the path from a laptop to a server adds a different delay to each request: jitter, which the next section is about. The window is a property of the server's code; the jitter is a property of the network; the attacker wins on the tries where the two arrivals fall closer together than the window.

The single-packet attack, 2023

In August 2023, at the Black Hat conference, James Kettle of PortSwigger presented a way to remove the jitter. He put the window of a typical web application at about a millisecond, far below the variation in delivery times across the internet. The idea is to make the network deliver many requests in one go.

HTTP/2 lets a client send several requests over one TCP connection at the same time. The attack sends twenty or thirty requests on one connection, each with a small fragment held back, so that the server cannot begin to process any of them. Then it sends the final fragments all together; the operating system puts them into one TCP packet, which holds about 1,500 bytes. One packet arrives at one instant, and the twenty or thirty requests complete, and begin to be processed, within a millisecond of each other, whatever the jitter on the way did to the earlier fragments. Kettle's benchmark sends twenty requests from Melbourne to a server in Dublin: the spread between the first and the last to begin executing is about one millisecond, median. The technique is in Burp Suite's Repeater and in its Turbo Intruder extension, so what took Homakov six tries now usually takes one attempt.

1. thirty requests, each missing its last fragment 2. the thirty last fragments, in one packet one TCP packet, ≈1,500 bytes the server thirty requests complete within about 1 ms The earlier fragments travel with the network's jitter; the final fragments arrive as one.
The single-packet attack (Kettle, 2023). The fragments withheld are the lever: a request is not processed until it is complete, and thirty requests are completed by one packet.

Kettle calls the family by its name in the trade, the limit overrun: requests that arrive together and overcome some limit the application enforces one request at a time. His list of examples reads like a catalogue of this page: “redeeming a gift card multiple times”, applying a single discount code repeatedly, withdrawing more than a balance. The list is the surface. Underneath, a web application is a state machine kept in a database, and every transition that takes two statements, with no lock held between them, has a window; the attack is a way of looking into windows the application's authors never knew they had opened.

nopCommerce, 2024

In 2024, testers at Outpost24 took nopCommerce, an open-source shop used by many online stores, and did what this page has described. Two sessions, two carts, a gift card worth enough for one of them; the request that confirms an order, sent from both sessions in one packet. Their account of what happens is the sentence of Flexcoin ten years on: both requests check the gift card's balance before either updates it, and the card is overdrawn. The bug is recorded as CVE-2024-58248; the shop patched it at the end of the year, and the testers report that a slight change to the sequence of requests found the window again.

The three ways

In session 7 of the course the executions of a multi-threaded program are its interleavings, and there are three things one can do about the ones one does not want. Two of them fix the gift card; the third cannot, and says why. Overrun.java runs each version with the same thirty requests. The versions model the interleaving and nothing else: a confirmation that fails, a request that dies halfway, a restart, are left out. In the shop the debit and the confirmation must take effect together or not at all. A database transaction promises that for the rows it touches; a call to another service lies outside it, so a real checkout reserves, confirms and settles in steps, each of which can be redone or undone. The session reaches transactions at its end, under the question of what composes.

Forbid the interleaving. One lock, held from the read to the write. The check and the write become one operation, atomic by exclusion: no other request's step can fall between them. What one must establish is that every access to the balance happens under this lock; the one forgotten is the one the attacker finds. What one risks is waiting: thirty requests on one card are served one at a time, the accepted one holding the lock through its millisecond, and a request that blocks or hangs while holding it stops them all. In a database this is SELECT … FOR UPDATE, which locks the row until the transaction ends; a transaction alone, at the isolation level databases default to, does not close the window.

static boolean redeem(int x) {
  synchronized (card) {                   // read to write: nothing of ours between
    if (balance < x) return false;
    confirmOrder();
    balance -= x;
    return true;
  }
}

Detect the interleaving, and redo. No lock. Read the balance, decide, and then write only if the balance is still what was read, in one atomic operation of the processor, compare-and-set. If another request wrote first, the instruction fails, and the loop reads again; on the second read the balance is 0, and the request is refused. The order is confirmed after the debit has taken effect, as a shop reserves and then confirms: whatever the loop did between the read and the compare-and-set would have to be done again or undone on a failed attempt, and here it does nothing. What one must establish is that the loop re-reads, that the decision depends on nothing but the value compared, and that nothing between the read and the write needs undoing. What one risks, in general, is a request that keeps losing the race and retries for ever while others finish; the system as a whole always progresses, the single request may not. Here the loop ends in one or two reads, since after the one debit every read finds 0. In a database the same loop is UPDATE cards SET balance = balance - 10 WHERE id = ? AND balance = ?, with the value read in the second place: if no row was updated, read again. A shop that writes the check into the statement instead, AND balance >= 10, needs no loop; the database's row lock makes the one statement atomic, which is the first way inside one statement.

static final AtomicInteger balance = new AtomicInteger(10);

static boolean redeem(int x) {
  while (true) {
    int b = balance.get();                        // read
    if (b < x) return false;                      // check
    if (balance.compareAndSet(b, b - x)) break;   // still b? then b - x, in one step
    // another request wrote first: read again
  }
  confirmOrder();                                 // after the debit: nothing to undo
  return true;
}

Make the order irrelevant. The third way asks for operations whose result is the same in any order: increments, the union of sets, a sum computed per thread and combined at the end. The gift card refuses it. Two redemptions of 10 on a card worth 10 must end differently in the two orders, since at most one can succeed; a debit with a floor does not commute with itself. What can be done is to give the balance one owner, so that there is no interleaving to speak of: one thread holds it as a local variable, which no other thread can even name, and serves requests from a queue in their order of arrival, the first accepted and the rest refused. That is exclusion again, arranged by the language instead of a lock. What one must establish is that nothing but the owner touches the balance, which a local variable enforces. What one risks is the owner: one thread does all the work on that card, and its queue can grow. This owner lives in one process and keeps the balance in memory; it shows the shape, not the durability. The shop's version is a queue in front of the database, as Poloniex's was, with one writer per card. The same fact is known in replicated systems, where the third way has a name: a conflict-free replicated data type, a CRDT, is a type whose updates commute, so that copies of it on different machines converge whatever order the updates reach them in; a counter is one, and a non-negative balance is the textbook case that is not, the bounded-counter problem, whose remedy is again ownership, each copy given its own share of the balance to spend without asking.

So the three ways are general, and this card takes two of them. Forbid or detect and redo, lock or compare-and-set, close its window; the owner is the first way in another dress; and the third way, the order made irrelevant, belongs to state whose updates commute, the counter of the course, not to a balance with a floor.

static Thread owner = new Thread(() -> {
  int b = 10;                             // the balance: a local variable of one thread
  try {
    while (true) {
      Request r = inbox.take();           // the next request, in the order of arrival
      boolean ok = b >= r.amount();
      if (ok) { confirmOrder(); b -= r.amount(); }
      r.answer(ok);
    }
  } catch (InterruptedException e) { left = b; }
});
lock: card worth 10, 30 requests of 10: accepted 1, balance left 0
cas: card worth 10, 30 requests of 10: accepted 1, balance left 0
queue: card worth 10, 30 requests of 10: accepted 1, balance left 0
The wayWhat it promisesWhat one must establishWhat it risks
Forbid, a lockthe check and the write are one operationevery access to the balance under the lockwaiting; a holder that stops
Detect and redo, compare-and-setthe write takes effect only on the value checkedthe loop re-reads; the decision depends only on that valuea request that retries for ever
One owner, a queue, in place of the third wayno interleaving: one thread, one ordernothing but the owner touches the balancethe owner's queue grows

None of Flexcoin, Starbucks and nopCommerce used any of the three; each ran a check and a write with other requests free to run between them. Poloniex, after its loss, chose the owner and the queue: its owner wrote that withdrawals and orders now went into one global execution queue, processed in sequence, and that the database operations were being turned into transactions. His second fault, the audit that was not looking for negative balances, is the fourth thing, which is not a way but a habit: the invariant written down, no balance below zero, and something that watches it. A fix of any of the three kinds would have made that audit never fire; the audit is what reveals a missing fix.

Lessons

Sources

Written with an AI assistant; every source was opened and every quotation checked. This post is the story page of session 7 of my course Programming Paradigms at Aalborg University Copenhagen, autumn 2026, in the first person.