* [pbs-devel] [PATCH v1 proxmox-backup] datastore: fix safety issue when writing FixedIndexHeader
@ 2025-11-21 14:30 Robert Obkircher
0 siblings, 0 replies; only message in thread
From: Robert Obkircher @ 2025-11-21 14:30 UTC (permalink / raw)
To: pbs-devel
In the old version the u8 buffer was not guaraneed to be sufficiently
aligned to be cast to an 8-byte aligned FixedIndexHeader reference.
The pointer was also obtained via Vec::as_ptr, whose result must not
be used for mutations according to its documentation. In practice,
it is unlikely that these issues caused any problems, because big
allocations are usually aligned and because the actual implementation
of as_ptr is similar to as_mut_ptr.
This change adopts the approach used by the DynamicIndexWriter. It
continues to guarantee that the 4096 bytes are not stack allocated.
Signed-off-by: Robert Obkircher <r.obkircher@proxmox.com>
---
pbs-datastore/src/dynamic_index.rs | 2 ++
pbs-datastore/src/fixed_index.rs | 30 ++++++++++++++++++++++--------
2 files changed, 24 insertions(+), 8 deletions(-)
diff --git a/pbs-datastore/src/dynamic_index.rs b/pbs-datastore/src/dynamic_index.rs
index ad49cdf3..37954b56 100644
--- a/pbs-datastore/src/dynamic_index.rs
+++ b/pbs-datastore/src/dynamic_index.rs
@@ -48,6 +48,8 @@ impl DynamicIndexHeader {
pub fn as_bytes(&self) -> &[u8] {
unsafe {
+ // SAFETY: there is no (uninitialized) padding, because the total
+ // size of all members is equal to the statically asserted size.
std::slice::from_raw_parts(
self as *const Self as *const u8,
std::mem::size_of::<Self>(),
diff --git a/pbs-datastore/src/fixed_index.rs b/pbs-datastore/src/fixed_index.rs
index 6c3be2d4..90fd29fd 100644
--- a/pbs-datastore/src/fixed_index.rs
+++ b/pbs-datastore/src/fixed_index.rs
@@ -29,6 +29,26 @@ pub struct FixedIndexHeader {
}
proxmox_lang::static_assert_size!(FixedIndexHeader, 4096);
+impl FixedIndexHeader {
+ /// Convenience method to allocate a zero-initialized header struct.
+ pub fn zeroed() -> Box<Self> {
+ unsafe {
+ Box::from_raw(std::alloc::alloc_zeroed(std::alloc::Layout::new::<Self>()) as *mut Self)
+ }
+ }
+
+ pub fn as_bytes(&self) -> &[u8] {
+ unsafe {
+ // SAFETY: there is no (uninitialized) padding, because the total
+ // size of all members is equal to the statically asserted size.
+ std::slice::from_raw_parts(
+ self as *const Self as *const u8,
+ std::mem::size_of::<Self>(),
+ )
+ }
+ }
+}
+
// split image into fixed size chunks
pub struct FixedIndexReader {
@@ -237,7 +257,6 @@ impl Drop for FixedIndexWriter {
}
impl FixedIndexWriter {
- #[allow(clippy::cast_ptr_alignment)]
// Requires obtaining a shared chunk store lock beforehand
pub fn create(
store: Arc<ChunkStore>,
@@ -267,18 +286,13 @@ impl FixedIndexWriter {
let uuid = Uuid::generate();
- let buffer = vec![0u8; header_size];
- let header = unsafe { &mut *(buffer.as_ptr() as *mut FixedIndexHeader) };
-
+ let mut header = FixedIndexHeader::zeroed();
header.magic = file_formats::FIXED_SIZED_CHUNK_INDEX_1_0;
header.ctime = i64::to_le(ctime);
header.size = u64::to_le(size as u64);
header.chunk_size = u64::to_le(chunk_size as u64);
header.uuid = *uuid.as_bytes();
-
- header.index_csum = [0u8; 32];
-
- file.write_all(&buffer)?;
+ file.write_all(header.as_bytes())?;
let index_length = size.div_ceil(chunk_size);
let index_size = index_length * 32;
--
2.47.3
_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2025-11-21 14:33 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-21 14:30 [pbs-devel] [PATCH v1 proxmox-backup] datastore: fix safety issue when writing FixedIndexHeader Robert Obkircher
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox