all lists on lists.proxmox.com
 help / color / mirror / Atom feed
* [PATCH proxmox{,-backup} v2 00/10] Fix Undefined Behavior in Tape Block Header Deallocation
@ 2026-08-21 14:02 Max R. Carrara
  2026-08-21 14:02 ` [PATCH proxmox v2 01/10] proxmox-alloc: introduce proxmox-alloc with `LayoutAwareBox<T>` type Max R. Carrara
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Max R. Carrara @ 2026-08-21 14:02 UTC (permalink / raw)
  To: pbs-devel

Fix Undefined Behavior in Tape Block Header Deallocation - v2
=============================================================

We allocate `BlockHeader` in `pbs-tape` with an alignment equal
to the page size, but because it uses packed / 1-byte alignment
(`#[repr(C, packed)]` to be precise), the compiler will treat it as if
it was allocated with 1-byte alignment.

Miri [0] will flag this as undefined behavior:

    error: Undefined Behavior: incorrect layout on deallocation:
    alloc46398 has size 262144 and alignment 4096, but gave size 262144
    and alignment 1

This is because Rust cares about a memory region's alignment on
deallocation, even if the underlying allocator does not -- hence why
this hasn't actually been a problem for us. Still, there is no guarantee
that this will not change at some point in the future.

The same kind of UB is also caused by the `alloc_page_aligned_buffer()`
function of the `sgutils2` module.

This series addresses both of these issues by introducing a new type
called `LayoutAwareBox`, which keeps track of the layout a given heap
allocation used.

Special thanks to @Robert, who brought this to my attention and totally
managed to nerd-snipe me with this. I have added a respective git
trailer in each of the patches that address the occurrences of UB.

Notable Changes
---------------

This v2 here is a greater rework of v1, so a lot has changed.

- Introduce the `proxmox-alloc` crate together with its `LayoutAwareBox`
  type that keeps track of the layout used during allocation and re-uses
  it during deallocation.

  This is the main type that allows us to prevent UB in this series.

  I was first thinking of keeping this type inside of `pbs-tape`, but
  after implementing two thirds of it, I figured it's probably better to
  introduce it as part of a separate crate together with ample tests and
  documentation instead. This documents the problems the type addresses
  and also shows how it ought to be used, which might be helpful for
  developers who rarely touch lower-level Rust code.

  FYI, the docs of all our crates in proxmox.git can be built and opened
  using:

    cargo doc --workspace --no-deps --open

- Refactor the tape code in smaller pieces to make reviewing all changes
  easier.

- Move `BlockHeader` into a separate module and rename it to
  `TapeBlock`, since that's what it actually represents.

- Get rid of any `pub` fields in `TapeBlock` and add any necessary
  getters and setters.

- Remove haphazard `unsafe` blocks by implementing them as methods
  instead.

- Add "SAFETY" comments to all `unsafe` blocks that are touched or
  introduced.

- Add tests in `pbs-tape` that allow Miri to easily catch any UB
  regarding the two instances this patch series addresses.

Testing
-------

Would be great if somebody with a working tape storage could give this a
spin -- miri does not report any UB anymore and the tests we have pass,
but some smoke-testing would nevertheless be appreciated. My virtual
tape library has unfortunately borked itself, and I haven't come around
to un-borking it yet.

[0]: https://github.com/rust-lang/miri

Previous Versions
-----------------

v1: https://lore.proxmox.com/pbs-devel/20260805141620.4190773-1-m.carrara@proxmox.com/

Summary of Changes
------------------


proxmox:

Max R. Carrara (2):
  proxmox-alloc: introduce proxmox-alloc with `LayoutAwareBox<T>` type
  proxmox-alloc: document undefined behavior regarding custom allocs

 Cargo.toml                       |    1 +
 proxmox-alloc/Cargo.toml         |   24 +
 proxmox-alloc/examples/ub.rs     |   99 ++
 proxmox-alloc/src/aware_boxed.rs | 2199 ++++++++++++++++++++++++++++++
 proxmox-alloc/src/lib.rs         |   19 +
 5 files changed, 2342 insertions(+)
 create mode 100644 proxmox-alloc/Cargo.toml
 create mode 100644 proxmox-alloc/examples/ub.rs
 create mode 100644 proxmox-alloc/src/aware_boxed.rs
 create mode 100644 proxmox-alloc/src/lib.rs


proxmox-backup:

Max R. Carrara (8):
  tape: move tape block structs into separate file module
  tape: rename `BlockHeader` and `BlockHeaderFlags`
  tape: blocked_{reader,writer}: rename `buffer` to `tape_block`
  tape: tape block: represent tape block header with its own struct
  tape: tape block: make `payload` field private
  tape: blocked_{reader,writer}: remove haphazard `unsafe` blocks
  tape: tape block: fix undefined behavior on tape block deallocation
  tape: sgutils2: fix undefined behavior in dealloc of buffer

 Cargo.toml                     |   3 +
 pbs-tape/Cargo.toml            |   1 +
 pbs-tape/src/blocked_reader.rs |  72 ++++++-------
 pbs-tape/src/blocked_writer.rs |  51 +++++----
 pbs-tape/src/lib.rs            |  86 +++------------
 pbs-tape/src/sgutils2.rs       |  49 ++++++---
 pbs-tape/src/tape_block.rs     | 191 +++++++++++++++++++++++++++++++++
 7 files changed, 300 insertions(+), 153 deletions(-)
 create mode 100644 pbs-tape/src/tape_block.rs


Summary over all repositories:
  12 files changed, 2642 insertions(+), 153 deletions(-)

-- 
Generated by murpp 0.12.0




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-08-21 14:03 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-21 14:02 [PATCH proxmox{,-backup} v2 00/10] Fix Undefined Behavior in Tape Block Header Deallocation Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox v2 01/10] proxmox-alloc: introduce proxmox-alloc with `LayoutAwareBox<T>` type Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox v2 02/10] proxmox-alloc: document undefined behavior regarding custom allocs Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 03/10] tape: move tape block structs into separate file module Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 04/10] tape: rename `BlockHeader` and `BlockHeaderFlags` Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 05/10] tape: blocked_{reader,writer}: rename `buffer` to `tape_block` Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 06/10] tape: tape block: represent tape block header with its own struct Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 07/10] tape: tape block: make `payload` field private Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 08/10] tape: blocked_{reader,writer}: remove haphazard `unsafe` blocks Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 09/10] tape: tape block: fix undefined behavior on tape block deallocation Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 10/10] tape: sgutils2: fix undefined behavior in dealloc of buffer Max R. Carrara

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal