View Issue Details

IDProjectCategoryView StatusLast Update
0012193Talermerchant backendpublic2026-11-18 15:02
Reporterbot-cg [agent]  
PrioritynormalSeveritycrashReproducibilityalways
Status new 
Summary0012193: POST /orders/$ID/pay: an over-long coins, tokens or tokens_evs array, or a null entry in tokens_evs, crashes taler-merchant-httpd during cleanup
DescriptionIn taler-merchant-httpd_post-orders-ORDER_ID-pay.c, phase_parse_pay() sets parse_pay.coins_cnt and parse_pay.tokens_cnt, and phase_parse_wallet_data() sets parse_wallet_data.token_envelopes_cnt, from json_array_size() before comparing them with MAX_COIN_ALLOWED_COINS, MAX_TOKEN_ALLOWED_INPUTS and MAX_TOKEN_ALLOWED_OUTPUTS. When an array is too long the request is refused before the matching array is allocated, but the count stays set, and pay_context_cleanup() then walks a NULL array of that many entries.

Separately, phase_parse_wallet_data() deliberately skips null entries in tokens_evs (and a parse error leaves the later slots empty), but pay_context_cleanup() calls GNUNET_CRYPTO_blinded_message_decref() on every slot, and that function dereferences its argument. The other users of the array (the duplicate check, sign_token_envelopes) skip NULL slots.

All of this happens while parsing, before the order is looked up, so the order does not have to exist.

The attached patch sets each count only after the length check, and skips empty envelope slots in the cleanup.
Steps To ReproduceWith the merchant from src/testing/test_merchant_api.conf and its admin instance, a POST to /orders/no-such-order/pay with a coins array of 1025 entries, with a tokens array of 65 entries, or with "wallet_data": {"choice_index": 0, "tokens_evs": [null]} is answered (400, 400, 404), and taler-merchant-httpd then exits with SIGSEGV. Observed with v1.6.24-52-g63e70c0a; the code is the same at 495a791d. With the attached patch applied to that version, the same three requests get the same answers and the httpd keeps running.
TagsNo tags attached.
Attached Files
12193-merchant-do-not-set-pay-array-counts-before-their-ar.patch (3,426 bytes)   
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 16:01:54 +0100
Subject: [PATCH] merchant: do not set /pay array counts before their arrays
 exist

pay_context_cleanup() walks coins, tokens and token envelopes by their
counts, so a count must not be set when the length check refuses the
array, and null token envelopes have no blinded message to release.

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

Assisted by LLM
---
 ...-merchant-httpd_post-orders-ORDER_ID-pay.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 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..c1d2bf79 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
@@ -5101,9 +5101,7 @@ phase_parse_wallet_data (struct PayContext *pc)
     }
   }
 
-  pc->parse_wallet_data.token_envelopes_cnt
-    = json_array_size (tokens_evs);
-  if (pc->parse_wallet_data.token_envelopes_cnt >
+  if (json_array_size (tokens_evs) >
       MAX_TOKEN_ALLOWED_OUTPUTS)
   {
     GNUNET_break_op (0);
@@ -5115,6 +5113,8 @@ phase_parse_wallet_data (struct PayContext *pc)
                "'tokens_evs' array too long"));
     return;
   }
+  pc->parse_wallet_data.token_envelopes_cnt
+    = json_array_size (tokens_evs);
   pc->parse_wallet_data.token_envelopes
     = GNUNET_new_array (pc->parse_wallet_data.token_envelopes_cnt,
                         struct TokenEnvelope);
@@ -5388,8 +5388,7 @@ phase_parse_pay (struct PayContext *pc)
     pc->parse_pay.session_id = GNUNET_strdup ("");
   }
 
-  pc->parse_pay.coins_cnt = json_array_size (coins);
-  if (pc->parse_pay.coins_cnt > MAX_COIN_ALLOWED_COINS)
+  if (json_array_size (coins) > MAX_COIN_ALLOWED_COINS)
   {
     GNUNET_break_op (0);
     pay_end (pc,
@@ -5401,6 +5400,7 @@ phase_parse_pay (struct PayContext *pc)
     return;
   }
   /* note: 1 coin = 1 deposit confirmation expected */
+  pc->parse_pay.coins_cnt = json_array_size (coins);
   pc->parse_pay.dc = GNUNET_new_array (pc->parse_pay.coins_cnt,
                                        struct DepositConfirmation);
 
@@ -5535,8 +5535,7 @@ phase_parse_pay (struct PayContext *pc)
     }
   }
 
-  pc->parse_pay.tokens_cnt = json_array_size (tokens);
-  if (pc->parse_pay.tokens_cnt > MAX_TOKEN_ALLOWED_INPUTS)
+  if (json_array_size (tokens) > MAX_TOKEN_ALLOWED_INPUTS)
   {
     GNUNET_break_op (0);
     pay_end (pc,
@@ -5548,6 +5547,7 @@ phase_parse_pay (struct PayContext *pc)
     return;
   }
 
+  pc->parse_pay.tokens_cnt = json_array_size (tokens);
   pc->parse_pay.tokens = GNUNET_new_array (pc->parse_pay.tokens_cnt,
                                            struct TokenUseConfirmation);
 
@@ -5693,7 +5693,10 @@ pay_context_cleanup (void *cls)
     struct TokenEnvelope *ev
       = &pc->parse_wallet_data.token_envelopes[i];
 
-    GNUNET_CRYPTO_blinded_message_decref (ev->blinded_token.blinded_pub);
+    /* null entries in tokens_evs, and those after a parse error,
+       have no blinded message */
+    if (NULL != ev->blinded_token.blinded_pub)
+      GNUNET_CRYPTO_blinded_message_decref (ev->blinded_token.blinded_pub);
   }
   GNUNET_free (pc->parse_wallet_data.token_envelopes);
   if (NULL != pc->output_tokens)
-- 
2.47.3

Required notifications

Address or linkRemarkAdded byNotifiedActions

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 15:01bot-cgNew Issue
2026-11-18 15:01bot-cgRelationship addedchild of 0012186
2026-11-18 15:01bot-cgImplementer => Bohdan
2026-11-18 15:01bot-cgReviewer => sebasjm
2026-11-18 15:01bot-cgRelease signed by => Florian Dold
2026-11-18 15:01bot-cgDeployed by => Bohdan
2026-11-18 15:01bot-cgVerifier => sebasjm
2026-11-18 15:01bot-cgCommunicator => CM_7
2026-11-18 15:02bot-cgFile Added: 12193-merchant-do-not-set-pay-array-counts-before-their-ar.patch