Skip to content

Commit 4373838

Browse files
fix: polynomials module audit response (#22361)
## Summary Addresses findings from the polynomials/ module security audit (AztecProtocol/barretenberg-claude#2383). - **F1** (Low): Harden file-backed polynomial memory: restrict temp file permissions to `0600`, add `O_EXCL` to prevent race conditions, use `MAP_PRIVATE` instead of `MAP_SHARED` - **F4** (Info): Guard `compute_linear_polynomial_product` against `n=0` to prevent underflow - **F5** (Info): Clarify `UnivariateCoefficientBasis` docstring to distinguish Karatsuba precomputation from polynomial coefficients **Already fixed upstream:** - **F2** (Low): `factor_roots` empty polynomial guard (in #22282) - **F3** (Info): `get_scratch_space` thread safety via mutex (in #22306)
1 parent 783d14f commit 4373838

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

barretenberg/cpp/src/barretenberg/polynomials/backing_memory.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ template <typename Fr> struct BackingMemory {
159159

160160
std::string filename = temp_dir / ("poly-mmap-" + std::to_string(getpid()) + "-" + std::to_string(id));
161161

162-
int fd = open(filename.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0644);
162+
int fd = open(filename.c_str(), O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0600);
163163
if (fd < 0) {
164164
current_storage_usage.fetch_sub(required_bytes);
165165
return false;
@@ -172,7 +172,7 @@ template <typename Fr> struct BackingMemory {
172172
return false;
173173
}
174174

175-
void* addr = mmap(nullptr, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
175+
void* addr = mmap(nullptr, file_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
176176
if (addr == MAP_FAILED) {
177177
close(fd);
178178
std::filesystem::remove(filename);

barretenberg/cpp/src/barretenberg/polynomials/polynomial_arithmetic.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ template <typename Fr> Fr compute_sum(const Fr* src, const size_t n)
212212
// This function computes the polynomial (x - a)(x - b)(x - c)... given n distinct roots (a, b, c, ...).
213213
template <typename Fr> void compute_linear_polynomial_product(const Fr* roots, Fr* dest, const size_t n)
214214
{
215+
if (n == 0) {
216+
return;
217+
}
215218

216219
auto scratch_space_ptr = get_scratch_space<Fr>(n);
217220
auto scratch_space = scratch_space_ptr.get();

barretenberg/cpp/src/barretenberg/polynomials/univariate_coefficient_basis.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ template <class Fr, size_t domain_end, bool has_a0_plus_a1> class UnivariateCoef
4343
using value_type = Fr; // used to get the type of the elements consistently with std::array
4444

4545
/**
46-
* @brief coefficients is a length-3 array with the following representation:
46+
* @brief Storage for polynomial coefficients (always 3 elements for uniform layout).
4747
* @details This class represents a polynomial P(X) = a0 + a1.X + a2.X^2
48-
* We define `coefficients[0] = a0` and `coefficients[1] = a1`
49-
* If LENGTH == 2 AND `has_a0_plus_a1 = true` then `coefficients[2] = a0 + a1`
50-
* If LENGTH == 3 then `coefficients[2] = a2`
48+
* `coefficients[0] = a0`, `coefficients[1] = a1`.
49+
* The meaning of `coefficients[2]` depends on the template parameters:
50+
* - LENGTH == 2 AND has_a0_plus_a1 == true: coefficients[2] = a0 + a1
51+
* (precomputed for Karatsuba multiplication; NOT a polynomial coefficient)
52+
* - LENGTH == 2 AND has_a0_plus_a1 == false: coefficients[2] is unused
53+
* - LENGTH == 3: coefficients[2] = a2
5154
*/
5255
std::array<Fr, 3> coefficients;
5356

0 commit comments

Comments
 (0)