View Issue Details

IDProjectCategoryView StatusLast Update
0012207Talermerchant backendpublic2026-11-18 16:48
Reporterbot-cg [agent] Locked byChristian Grothoff  
Estimated effort (hours)0.5 
PrioritynormalSeveritycrash 
Status planning 
Product Versiongit (master) 
Target Version1.7 
Summary0012207: POST /orders/$ID/pay replay with fewer donation key pairs reads past choice->outputs
DescriptionOn an idempotent replay of a paid order, phase_contract_paid() (taler-merchant-httpd_post-orders-ORDER_ID-pay.c) runs TALER_MERCHANTDB_iterate_order_token_blinded_sigs() with append_output_token_sig() as callback. That function walks the stored signatures and attributes each to choice->outputs[pc->output_index_gen], advancing output_index_gen with count_output_tokens(), which counts donation receipts with the replay request's num_bkps. The stored signatures were counted with the original request's num_bkps. When the replay has fewer (for instance no "donau" object at all, after an original payment that received two or more receipts), the surplus signatures advance output_index_gen to outputs_len, choice->outputs[outputs_len] is read past the end of the array, and count_output_tokens() asserts on the garbage output type. Nothing bounds output_index_gen, and choice_index is not re-checked on this path either.

Confirmed by reading the code at 495a791d; not reproduced at runtime.

The attached patch stops attributing signatures once output_index_gen reaches outputs_len, and checks choice_index before indexing the choices.
Additional InformationRecorded as R2-M4 in the merchant review notes; the patch is that review's 2105 unchanged, rebased onto 495a791d.
TagsNo tags attached.
Attached Files
12207-merchant-bound-the-output-index-when-replaying-pay-t.patch (2,854 bytes)   
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 17:41:26 +0100
Subject: [PATCH] merchant: bound the output index when replaying /pay token
 signatures

A replay may account for fewer outputs than the signatures stored for
the order, e.g. when it asks for fewer donation receipts, and the index
then ran past the choice's outputs.

Issue: https://bugs.taler.net/n/12207

Assisted by LLM
---
 ...-merchant-httpd_post-orders-ORDER_ID-pay.c | 23 +++++++++++++++----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/backend/taler-merchant-httpd_post-orders-ORDER_ID-pay.c b/src/backend/taler-merchant-httpd_post-orders-ORDER_ID-pay.c
index 38c2854a..21fd9afc 100644
--- a/src/backend/taler-merchant-httpd_post-orders-ORDER_ID-pay.c
+++ b/src/backend/taler-merchant-httpd_post-orders-ORDER_ID-pay.c
@@ -4571,6 +4571,8 @@ append_output_token_sig (void *cls,
                          struct GNUNET_CRYPTO_BlindedSignature *sig)
 {
   struct PayContext *pc = cls;
+  const struct TALER_MERCHANT_Contract *contract
+    = pc->check_contract.contract_terms;
   struct TALER_MERCHANT_ContractChoice *choice;
   const struct TALER_MERCHANT_ContractOutput *output;
   struct SignedOutputToken out;
@@ -4581,11 +4583,6 @@ append_output_token_sig (void *cls,
           sizeof (out));
   GNUNET_assert (TALER_MERCHANT_CONTRACT_VERSION_1 ==
                  pc->check_contract.contract_terms->pc->base->version);
-  choice = &pc->check_contract.contract_terms->pc->details.v1
-           .choices[pc->parse_wallet_data.choice_index];
-  output = &choice->outputs[pc->output_index_gen];
-  cnt = count_output_tokens (pc,
-                             output);
   out.output_index = pc->output_index_gen;
   out.h_issue.hash = *h_issue;
   out.sig.signature = sig;
@@ -4593,6 +4590,22 @@ append_output_token_sig (void *cls,
   GNUNET_array_append (pc->output_tokens,
                        pc->output_tokens_len,
                        out);
+  if ( (pc->parse_wallet_data.choice_index < 0) ||
+       (pc->parse_wallet_data.choice_index >=
+        contract->pc->details.v1.choices_len) )
+    return; /* no choice to attribute outputs to */
+  choice = &contract->pc->details.v1.choices[
+    pc->parse_wallet_data.choice_index];
+  if (pc->output_index_gen >= choice->outputs_len)
+  {
+    /* More signatures stored than this request's outputs account for,
+       e.g. because it asked for fewer donation receipts than paid. */
+    GNUNET_break_op (0);
+    return;
+  }
+  output = &choice->outputs[pc->output_index_gen];
+  cnt = count_output_tokens (pc,
+                             output);
   /* Go to next output once we've output all tokens for the current one. */
   pc->output_token_cnt++;
   if (pc->output_token_cnt >= cnt)
-- 
2.47.3

Required notifications

Address or linkRemarkAdded byNotifiedActions

The plan

IDSummaryStatusLocked byEstimated effort (hours)
This issue has no child issue. Work that is big enough to be divided is divided here, and each part is planned on its own.
This issue (hours)0.5
Its children (hours)0
Together (hours)0.5
Nothing is missing: the plan can be finished.

Relationships

child of 0012186 new Unchecked array lengths and counts in exchange, merchant and anastasis: review after 2a57cbcaf [meta] 

Activities

Issue History

Date ModifiedUsernameFieldChange
2026-11-18 16:41bot-cgNew Issue
2026-11-18 16:41bot-cgRelationship addedchild of 0012186
2026-11-18 16:41bot-cgImplementer => Bohdan
2026-11-18 16:41bot-cgReviewer => sebasjm
2026-11-18 16:41bot-cgRelease signed by => Florian Dold
2026-11-18 16:41bot-cgDeployed by => Bohdan
2026-11-18 16:41bot-cgVerifier => sebasjm
2026-11-18 16:41bot-cgCommunicator => CM_7
2026-11-18 16:41bot-cgFile Added: 12207-merchant-bound-the-output-index-when-replaying-pay-t.patch
2026-11-18 16:47Christian GrothoffLocked by => Christian Grothoff
2026-11-18 16:47Christian GrothoffStatusnew => planning
2026-11-18 16:48Christian GrothoffImplementerBohdan => Christian Grothoff
2026-11-18 16:48Christian GrothoffReviewersebasjm => Florian Dold
2026-11-18 16:48Christian GrothoffRelease signed byFlorian Dold => Christian Grothoff
2026-11-18 16:48Christian GrothoffDeployed byBohdan => vecirex
2026-11-18 16:48Christian GrothoffVerifiersebasjm => vecirex
2026-11-18 16:48Christian GrothoffTarget Version => 1.7
2026-11-18 16:48Christian GrothoffEstimated effort (hours) => 0.5