Skip to content

bindings/c#: reject out-of-range nbits in mul_n_aggregate - #296

Closed
arib06 wants to merge 1 commit into
supranational:masterfrom
arib06:csharp-mul-n-aggregate-nbits
Closed

arib06 wants to merge 1 commit into
supranational:masterfrom
arib06:csharp-mul-n-aggregate-nbits

Conversation

@arib06

@arib06 arib06 commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Pairing.mul_n_aggregate takes the randomization bit-length as a signed int and forwards it to the native blst_pairing_mul_n_aggregate_pk_in_g{1,2} as (size_t)nbits with no range check. A negative nbits becomes SIZE_MAX, and any nbits greater than scalar.Length*8 makes POINTonE{1,2}_mult_w5 read the scalar as a wider value than the caller supplied, so the native side reads past the end of the scalar buffer (the C core only guards nbits != 0). The Go binding already rejects this (randBits < 0 || randBits > 256) and Rust validates scalar.len() against nbits; the C# binding was the one left unchecked.

Both overloads now reject an nbits that does not fit the scalar before the native call.

Reproduced against the native path with AddressSanitizer (nbits = 256, 1-byte scalar):

ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 1 ... 30 bytes after a 1-byte region
    #0 POINTonE2_mult_w5 e2.c:443
    #1 PAIRING_Aggregate_PK_in_G1 aggregate.c:281
    #2 blst_pairing_mul_n_aggregate_pk_in_g1 aggregate.c:359

A 32-byte scalar with nbits = 256 reads in bounds and is unaffected. poc.cs gets a regression case that fails without the guard and passes with it; dotnet run -c Release is green.

Pairing.mul_n_aggregate took the randomization bit-length as a signed int
and passed it to the native blst_pairing_mul_n_aggregate_pk_in_g{1,2} as
(size_t)nbits with no checks. A negative nbits converts to SIZE_MAX, and any
nbits larger than scalar.Length*8 makes POINTonE{1,2}_mult_w5 read the scalar
as a wider value than the caller supplied, reading past the scalar buffer. The
Go and Rust bindings already validate this; the C# one did not. Reject nbits
that does not fit the scalar before the native call.
@dot-asm dot-asm closed this in 500ff3a Oct 15, 2026
@dot-asm

dot-asm commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

As it says in the beginning of the modified .cs file "DO NOT EDIT THIS FILE." :-) Anyway, I've moved the tweaked modifications to the right place. Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants