Skip to content

gloo/tcp: fix size_t overflow in recv bounds check (relative write-what-where)#509

Merged
meta-codesync[bot] merged 1 commit into
pytorch:mainfrom
d4l3k:export-D108082771
Jun 10, 2026
Merged

gloo/tcp: fix size_t overflow in recv bounds check (relative write-what-where)#509
meta-codesync[bot] merged 1 commit into
pytorch:mainfrom
d4l3k:export-D108082771

Conversation

@d4l3k

@d4l3k d4l3k commented Jun 9, 2026

Copy link
Copy Markdown
Member

Summary:
tcp::Pair::prepareRead validated incoming SEND_BUFFER payloads with GLOO_ENFORCE_LE(op.preamble.roffset + op.preamble.length, op.buf->size_), where roffset and length are size_t fields read directly off the socket (Op::preamble in pair.h). The addition wraps modulo 2^64, so a malicious peer can pick e.g. roffset = (size_t)-8 and length = 8: the sum wraps to 0, passes the check, and recv() then writes attacker-controlled bytes at ((char*)op.buf->ptr_) + roffset (= ptr_ - 8). Varying (roffset, length) pairs whose sum wraps to <= size_ yields a relative write-what-where primitive at an arbitrary 64-bit offset from a registered buffer, with fully attacker-controlled content. Gloo SECURITY.md puts network-exploitable RCE in scope.

This rewrites the check to validate each term independently so the sum cannot wrap: roffset <= size_ and length <= size_ - roffset. This mirrors the existing overflow-safe pattern already used in Pair::send/recv/tryRecv. The check is also moved ahead of the iov_base computation so an out-of-bounds pointer is never formed — UBSan flagged the old ptr_ + roffset arithmetic itself as pointer-overflow, independent of the enforce.

The same prepareRead is inherited by the TLS transport (tcp::tls::Pair), so both TCP and TCP+TLS are fixed by this change.

Differential Revision: D108082771

…at-where)

Summary:
`tcp::Pair::prepareRead` validated incoming `SEND_BUFFER` payloads with `GLOO_ENFORCE_LE(op.preamble.roffset + op.preamble.length, op.buf->size_)`, where `roffset` and `length` are `size_t` fields read directly off the socket (`Op::preamble` in `pair.h`). The addition wraps modulo 2^64, so a malicious peer can pick e.g. `roffset = (size_t)-8` and `length = 8`: the sum wraps to `0`, passes the check, and `recv()` then writes attacker-controlled bytes at `((char*)op.buf->ptr_) + roffset` (= `ptr_ - 8`). Varying `(roffset, length)` pairs whose sum wraps to `<= size_` yields a relative write-what-where primitive at an arbitrary 64-bit offset from a registered buffer, with fully attacker-controlled content. Gloo `SECURITY.md` puts network-exploitable RCE in scope.

This rewrites the check to validate each term independently so the sum cannot wrap: `roffset <= size_` and `length <= size_ - roffset`. This mirrors the existing overflow-safe pattern already used in `Pair::send`/`recv`/`tryRecv`. The check is also moved ahead of the `iov_base` computation so an out-of-bounds pointer is never formed — UBSan flagged the old `ptr_ + roffset` arithmetic itself as `pointer-overflow`, independent of the enforce.

The same `prepareRead` is inherited by the TLS transport (`tcp::tls::Pair`), so both TCP and TCP+TLS are fixed by this change.

Differential Revision: D108082771
@meta-cla meta-cla Bot added the CLA Signed label Jun 9, 2026
@meta-codesync

meta-codesync Bot commented Jun 9, 2026

Copy link
Copy Markdown

@d4l3k has exported this pull request. If you are a Meta employee, you can view the originating Diff in D108082771.

@meta-codesync meta-codesync Bot merged commit 74cc005 into pytorch:main Jun 10, 2026
15 of 16 checks passed
@d4l3k d4l3k deleted the export-D108082771 branch June 10, 2026 18:03
pytorchmergebot pushed a commit to pytorch/pytorch that referenced this pull request Jun 11, 2026
…87077)

## Summary

Bumps the `gloo` submodule from `3135b0b` to `74cc005` (15 commits, 2026-01-09 -> 2026-06-10; +615/-144 across 29 files).

The headline change is a **security fix**: a `size_t` overflow in the TCP transport's recv bounds check that allowed a relative write-what-where. `roffset` and `length` are read directly off the wire, and the old check `roffset + length <= size_` could wrap around 2^64, letting an out-of-bounds pair pass and yield an arbitrary write at `ptr_ + roffset`. The fix validates each term independently before forming the pointer.

The rest is a mix of portability/bug fixes (ibverbs destructor crash on no-RDMA hosts, TCP address-family mismatch, ROCm `[[nodiscard]]` and `-Wnontrivial-memcall` warnings), toolchain/CI work (move to C++20, native CMake HIP support for ROCm, new CUDA/ROCm/arm64 CI runners, clang-format 21), and named gloo threads for observability.

Note: the SHM allreduce optimization ([#458](pytorch/gloo#458)) was added then reverted ([#490](pytorch/gloo#490)) within this range, so it is **not** present in the final tree.

## Included commits (newest -> oldest)

| Commit | Author | Date | Description |
|--------|--------|------|-------------|
| `74cc005` | Tristan Rice | 2026-06-10 | **gloo/tcp: fix size_t overflow in recv bounds check (relative write-what-where)** ([#509](pytorch/gloo#509)) |
| `70dc360` | Tristan Rice | 2026-04-20 | Fix address family mismatch by reusing bound socket ([#503](pytorch/gloo#503)) |
| `6f4c667` | Tristan Rice | 2026-03-19 | Fix `std::terminate` in ibverbs destructors on systems without RDMA hardware ([#500](pytorch/gloo#500)) |
| `2ba34a6` | Karl Gyllstrom | 2026-03-11 | Fix `[[nodiscard]]` `cudaDeviceEnablePeerAccess` warning |
| `845824e` | Richard Barnes | 2026-03-11 | Move gloo onto C++20 |
| `bcd1672` | Gavin Zhao | 2026-02-12 | ROCm: Migrate to native CMake HIP support ([#478](pytorch/gloo#478)) |
| `9322e67` | Tristan Rice | 2026-02-09 | Revert "Intra-node shared memory (SHM) optimizations" ([#490](pytorch/gloo#490)) |
| `f834c75` | Tristan Rice | 2026-02-06 | gloo: improve error message on connection closed |
| `8789be7` | Tristan Rice | 2026-02-05 | ci: add CUDA and rocm builds |
| `d8d0f77` | Nathan Brown | 2026-02-05 | ci: add arm64 runner for github actions ([#487](pytorch/gloo#487)) |
| `8d0b9a4` | Lydia Kim | 2026-01-14 | Fix `-Wnontrivial-memcall` |
| `980c925` | Lucian Adrian Grijincu | 2026-01-13 | Fix `-Wnontrivial-memcall` error in AllreduceLocal |
| `b9cac96` | Tristan Rice | 2026-01-13 | Add `setThreadName` helper and name all gloo threads |
| `7ec708d` | Tristan Rice | 2026-01-12 | ci: bump linter to clang-format 21.1.2 |
| `5994546` | gaopengff | 2026-01-09 | Intra-node shared memory (SHM) optimizations for CPU primitives ([#458](pytorch/gloo#458)) -- *later reverted by `9322e67`* |

## Test Plan

CI. Submodule bump only; the gloo changes carry their own unit tests (TCP pair bounds-check coverage added in #509).

Pull Request resolved: #187077
Approved by: https://github.com/dolpm, https://github.com/kapilsh, https://github.com/Regina8023, https://github.com/malfet
jemitche1 pushed a commit to jemitche1/pytorch that referenced this pull request Jun 13, 2026
…torch#187077)

## Summary

Bumps the `gloo` submodule from `3135b0b` to `74cc005` (15 commits, 2026-01-09 -> 2026-06-10; +615/-144 across 29 files).

The headline change is a **security fix**: a `size_t` overflow in the TCP transport's recv bounds check that allowed a relative write-what-where. `roffset` and `length` are read directly off the wire, and the old check `roffset + length <= size_` could wrap around 2^64, letting an out-of-bounds pair pass and yield an arbitrary write at `ptr_ + roffset`. The fix validates each term independently before forming the pointer.

The rest is a mix of portability/bug fixes (ibverbs destructor crash on no-RDMA hosts, TCP address-family mismatch, ROCm `[[nodiscard]]` and `-Wnontrivial-memcall` warnings), toolchain/CI work (move to C++20, native CMake HIP support for ROCm, new CUDA/ROCm/arm64 CI runners, clang-format 21), and named gloo threads for observability.

Note: the SHM allreduce optimization ([pytorch#458](pytorch/gloo#458)) was added then reverted ([pytorch#490](pytorch/gloo#490)) within this range, so it is **not** present in the final tree.

## Included commits (newest -> oldest)

| Commit | Author | Date | Description |
|--------|--------|------|-------------|
| `74cc005` | Tristan Rice | 2026-06-10 | **gloo/tcp: fix size_t overflow in recv bounds check (relative write-what-where)** ([pytorch#509](pytorch/gloo#509)) |
| `70dc360` | Tristan Rice | 2026-04-20 | Fix address family mismatch by reusing bound socket ([pytorch#503](pytorch/gloo#503)) |
| `6f4c667` | Tristan Rice | 2026-03-19 | Fix `std::terminate` in ibverbs destructors on systems without RDMA hardware ([pytorch#500](pytorch/gloo#500)) |
| `2ba34a6` | Karl Gyllstrom | 2026-03-11 | Fix `[[nodiscard]]` `cudaDeviceEnablePeerAccess` warning |
| `845824e` | Richard Barnes | 2026-03-11 | Move gloo onto C++20 |
| `bcd1672` | Gavin Zhao | 2026-02-12 | ROCm: Migrate to native CMake HIP support ([pytorch#478](pytorch/gloo#478)) |
| `9322e67` | Tristan Rice | 2026-02-09 | Revert "Intra-node shared memory (SHM) optimizations" ([pytorch#490](pytorch/gloo#490)) |
| `f834c75` | Tristan Rice | 2026-02-06 | gloo: improve error message on connection closed |
| `8789be7` | Tristan Rice | 2026-02-05 | ci: add CUDA and rocm builds |
| `d8d0f77` | Nathan Brown | 2026-02-05 | ci: add arm64 runner for github actions ([pytorch#487](pytorch/gloo#487)) |
| `8d0b9a4` | Lydia Kim | 2026-01-14 | Fix `-Wnontrivial-memcall` |
| `980c925` | Lucian Adrian Grijincu | 2026-01-13 | Fix `-Wnontrivial-memcall` error in AllreduceLocal |
| `b9cac96` | Tristan Rice | 2026-01-13 | Add `setThreadName` helper and name all gloo threads |
| `7ec708d` | Tristan Rice | 2026-01-12 | ci: bump linter to clang-format 21.1.2 |
| `5994546` | gaopengff | 2026-01-09 | Intra-node shared memory (SHM) optimizations for CPU primitives ([pytorch#458](pytorch/gloo#458)) -- *later reverted by `9322e67`* |

## Test Plan

CI. Submodule bump only; the gloo changes carry their own unit tests (TCP pair bounds-check coverage added in pytorch#509).

Pull Request resolved: pytorch#187077
Approved by: https://github.com/dolpm, https://github.com/kapilsh, https://github.com/Regina8023, https://github.com/malfet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants