Consensys / gnark

gnark is a fast zk-SNARK library that offers a high-level API to design circuits. The library is open source and developed under the Apache 2.0 license
https://hackmd.io/@gnark
Apache License 2.0
1.45k stars 381 forks source link

fix: sanitize groth16 verification key reading #1307

Closed ivokub closed 4 weeks ago

ivokub commented 1 month ago

Description

See the related security advisory https://github.com/Consensys/gnark/security/advisories/GHSA-cph5-3pgr-c82g (will publish once PR has been reviewed and tested) for full description and POC.

The main issue is that serialized Groth16 verification key includes in a header number of expected Pedersen verification keys which is used for initializing a slice. When modifying the serialized file, we may allocate huge slice, leading to OOM.

In this PR instead of allocating the slice beforehand, we instead try to read all the existing commitment keys in the file and then compare that the number of read commitment keys corresponds to the expected. This prevents using heuristics on the number of commitment keys and bounds the size of the in-memory full verification key to the serialized file size.

Type of change

How has this been tested?

Tested against the POC in the advisory. Does not OOM anymore.

Checklist:

ivokub commented 4 weeks ago

@gbotrel - it is done now :) I didn't anticipate making that much bugs :/