From 06c3d928fee602d838852a7c4de65aa83bca1d57 Mon Sep 17 00:00:00 2001 From: iefa m Date: Fri, 6 Nov 2026 23:22:39 +0530 Subject: [PATCH] Serialize access to Blake2State in the generichash bindings libsodium is called with the GIL released, so concurrent update() calls on a shared blake2b state race on its buffer length inside blake2b_update and can write past the end of the state buffer. Hold a per-state lock around update, final and copy, as hashlib does. --- src/nacl/bindings/crypto_generichash.py | 26 +++++++++++++++---------- tests/test_generichash.py | 25 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/nacl/bindings/crypto_generichash.py b/src/nacl/bindings/crypto_generichash.py index 521873cc..506f7937 100644 --- a/src/nacl/bindings/crypto_generichash.py +++ b/src/nacl/bindings/crypto_generichash.py @@ -13,6 +13,7 @@ # limitations under the License. from __future__ import annotations +import threading from typing import TYPE_CHECKING, NoReturn from nacl import exceptions as exc @@ -156,13 +157,15 @@ class Blake2State: Python-level wrapper for the crypto_generichash_blake2b state buffer """ - __slots__ = ["_statebuf", "digest_size"] + __slots__ = ["_lock", "_statebuf", "digest_size"] def __init__(self, digest_size: int): self._statebuf = ffi.new( "unsigned char[]", crypto_generichash_STATEBYTES ) self.digest_size = digest_size + # libsodium runs without the GIL, so serialize access to the state + self._lock = threading.Lock() def __reduce__(self) -> NoReturn: """ @@ -173,9 +176,10 @@ def __reduce__(self) -> NoReturn: def copy(self) -> Self: _st = self.__class__(self.digest_size) - ffi.memmove( - _st._statebuf, self._statebuf, crypto_generichash_STATEBYTES - ) + with self._lock: + ffi.memmove( + _st._statebuf, self._statebuf, crypto_generichash_STATEBYTES + ) return _st @@ -249,9 +253,10 @@ def generichash_blake2b_update(state: Blake2State, data: bytes) -> None: raising=exc.TypeError, ) - rc = lib.crypto_generichash_blake2b_update( - state._statebuf, data, len(data) - ) + with state._lock: + rc = lib.crypto_generichash_blake2b_update( + state._statebuf, data, len(data) + ) ensure(rc == 0, "Unexpected failure", raising=exc.RuntimeError) @@ -272,9 +277,10 @@ def generichash_blake2b_final(state: Blake2State) -> bytes: ) _digest = ffi.new("unsigned char[]", crypto_generichash_BYTES_MAX) - rc = lib.crypto_generichash_blake2b_final( - state._statebuf, _digest, state.digest_size - ) + with state._lock: + rc = lib.crypto_generichash_blake2b_final( + state._statebuf, _digest, state.digest_size + ) ensure(rc == 0, "Unexpected failure", raising=exc.RuntimeError) return ffi.buffer(_digest, state.digest_size)[:] diff --git a/tests/test_generichash.py b/tests/test_generichash.py index 7aee1e4e..78ced2b2 100644 --- a/tests/test_generichash.py +++ b/tests/test_generichash.py @@ -16,8 +16,10 @@ import binascii import copy +import hashlib import json import os +import threading from typing import AnyStr import pytest @@ -245,3 +247,26 @@ def test_blake2_digest_size_descriptor_coherence(): assert h.name == "blake2b" assert h.block_size == 128 assert h.digest_size == 64 + + +def test_hashlib_blake2_concurrent_update(): + # libsodium is called with the GIL released, so concurrent updates on a + # shared state must be serialized by the binding. + chunk = b"\xaa" * 100003 + nthreads, nupdates = 8, 20 + h = nacl.hashlib.blake2b() + barrier = threading.Barrier(nthreads) + + def worker() -> None: + barrier.wait() + for _ in range(nupdates): + h.update(chunk) + + threads = [threading.Thread(target=worker) for _ in range(nthreads)] + for t in threads: + t.start() + for t in threads: + t.join() + + expected = hashlib.blake2b(chunk * nthreads * nupdates, digest_size=32) + assert h.digest() == expected.digest()