erofs-rs xattr POC + xattr_size() fix
Hi,
I’m Priyansh, a student at IIITM Gwalior. I am applying for the erofs-rs GSoC project. I have been reading the code for a few weeks.
I found a small issue in types.rs in the function xattr_size().
(count - 1) * size_of::<XattrEntry>() + size_of::<XattrHeader>()
when count = 1, it returns only 12, which is just the header size but there is also one inline entry after the header, and it is not counted.
In line 115 on filesystem.rs, this value is used to find where the inline file data starts. Because of this any FlatInline inode with exactly one xattr gives wrong file data. There is no error or panic, but the output is wrong.
While working on this, I also built a working inline xattr parser on a real test image:
https://github.com/priyansh-saxena1/erofs-rs/tree/xattr-poc
I will send a patch soon to fix xattr_size().
I have also submitted my GSoC proposal.
Thanks,
Priyansh Saxena
https://github.com/priyansh-saxena1
PATCH 1/1 erofs-rs: fix inline xattr size for tail offsets
From: Transcendental-Programmer <[email protected]>
--- erofs/src/async/filesystem.rs | 64 ++++++++++++++++++++++++++++++++++- erofs/src/filesystem.rs | 53 +++++++++++++++++++++++++++-- erofs/src/sync/filesystem.rs | 34 ++++++++++++++++++- erofs/src/types.rs | 7 ++++ 4 files changed, 153 insertions(+), 5 deletions(-)
erofs/src/async/filesystem.rs +63 -1
@@ -1,5 +1,7 @@use alloc::format;use alloc::vec::Vec ; +use binrw::{BinRead , io::Cursor }; +use core::mem::size_of;use typed_path::Component ;use bytes::Buf ;
@@ -83,6 +85,44 @@ impl<I: AsyncImage> EroFS<I> { self.core.block_size } +async fn xattr_ibody_size (&self, inode: &Inode ) ->Result <usize > { +let total_count = inode.xattr_count (); +if total_count ==0 { +return Ok (0 ); + } + +let inode_offset = self.core.get_inode_offset (inode.id ())as usize ; +let xattr_start = inode_offset + inode.size (); + +let mut header_buf =vec! [0u8 ;size_of ::<XattrHeader >()]; + self.image.read_exact_at (&mut header_buf, xattr_start).await ?; +let header =XattrHeader ::read (&mut Cursor ::new (&header_buf))?; + +let shared_count = header.shared_countas usize ; +let total = total_countas usize ; +if shared_count > total { +return Err (Error ::CorruptedData ( +"xattr shared count exceeds total count" .to_string (), + )); + } + +let mut offset = xattr_start +size_of ::<XattrHeader >() + shared_count *size_of ::<u32 >(); +let inline_count = total - shared_count; +for _in 0 ..inline_count { +let mut entry_buf =vec! [0u8 ;size_of ::<XattrEntry >()]; + self.image.read_exact_at (&mut entry_buf, offset).await ?; +let entry =XattrEntry ::read (&mut Cursor ::new (&entry_buf))?; + offset +=size_of ::<XattrEntry >(); + +let payload = entry.name_lenas usize + entry.value_lenas usize ; + offset = offset + .checked_add (payload) + .ok_or_else (||Error ::CorruptedData ("xattr size overflow" .to_string ()))?; + } + +Ok (offset - xattr_start) + } +pub async fn get_inode (&self, nid:u64 ) ->Result <Inode > {let offset = self.core.get_inode_offset (nid)as usize ;let mut buf =vec! [0u8 ;InodeExtended ::size ()];
@@ -91,7 +131,29 @@ impl<I: AsyncImage> EroFS<I> { } pub(crate) async fn read_inode_block(&self, inode: &Inode, offset: usize) -> Result<Vec<u8>> { - match self.core.plan_inode_block_read(inode, offset)? { + let layout = inode.layout()?; + let xattr_size = if inode.xattr_count() == 0 { + 0 + } else { + match layout { + Layout::FlatInline => { + let block_count = inode.data_size().div_ceil(self.core.block_size); + let block_index = offset / self.core.block_size; + if block_count != 0 && block_index == block_count - 1 { + self.xattr_ibody_size(inode).await? + } else { + 0 + } + } + Layout::ChunkBased => self.xattr_ibody_size(inode).await?, + _ => 0, + } + }; + + match self + .core + .plan_inode_block_read(inode, offset, xattr_size)? + { BlockPlan::Direct { offset, size } => { if size > self.core.block_size { return Err(Error::CorruptedData(format!(
erofs/src/filesystem.rs +50 -3
79 lines not on this page · raw message
erofs/src/sync/filesystem.rs +33 -1
@@ -143,6 +143,16 @@ impl<I: Image> EroFS<I> { self.core.block_size } +fn xattr_ibody_size (&self, inode: &Inode ) ->Result <usize > { +let inode_offset = self.core.get_inode_offset (inode.id ())as usize ; +let xattr_start = inode_offset + inode.size (); +let data = self + .image + .get (xattr_start..) + .ok_or_else (||Error ::OutOfBounds ("failed to read xattr data" .to_string ()))?; +EroFSCore ::xattr_ibody_size_from_slice (data, inode.xattr_count ()) + } +pub fn get_inode (&self, nid:u64 ) ->Result <Inode > {let offset = self.core.get_inode_offset (nid)as usize ;let data = self
@@ -153,7 +163,29 @@ impl<I: Image> EroFS<I> { }pub (crate )fn get_inode_block (&self, inode: &Inode , offset:usize ) ->Result <&[u8 ]> { -match self.core.plan_inode_block_read (inode, offset)? { +let layout = inode.layout ()?; +let xattr_size =if inode.xattr_count () ==0 { +0 + }else { +match layout { +Layout ::FlatInline => { +let block_count = inode.data_size ().div_ceil (self.core.block_size); +let block_index = offset / self.core.block_size; +if block_count !=0 && block_index == block_count -1 { + self.xattr_ibody_size (inode)? + }else { +0 + } + } +Layout ::ChunkBased => self.xattr_ibody_size (inode)?, + _ =>0 , + } + }; + +match self + .core + .plan_inode_block_read (inode, offset, xattr_size)? + {BlockPlan ::Direct { offset, size } => self .image .get (offset..offset + size)
erofs/src/types.rs +7 -0
@@ -185,6 +185,13 @@ impl Inode { } } +pub fn xattr_count (&self) ->u16 { +match self { +Self ::Compact ((_, n)) => n.xattr_count, +Self ::Extended ((_, n)) => n.xattr_count, + } + } +pub fn file_type (&self) ->FileType {match self {Self ::Compact ((_, n)) =>FileType ::from_raw_mode (n.modeas _ ),
Signature · 1 line
2.43.0
PATCH v2 1/1 erofs-rs: fix inline xattr size for tail offsets
From: Transcendental-Programmer <[email protected]>
--- erofs/src/async/filesystem.rs | 64 ++++++++++++++++++++++++++++++++++- erofs/src/filesystem.rs | 53 +++++++++++++++++++++++++++-- erofs/src/sync/filesystem.rs | 34 ++++++++++++++++++- erofs/src/types.rs | 7 ++++ 4 files changed, 153 insertions(+), 5 deletions(-)
erofs/src/async/filesystem.rs +63 -1
@@ -1,5 +1,7 @@use alloc::format;use alloc::vec::Vec ; +use binrw::{BinRead , io::Cursor }; +use core::mem::size_of;use typed_path::Component ;use bytes::Buf ;
@@ -83,6 +85,44 @@ impl<I: AsyncImage> EroFS<I> { self.core.block_size } +async fn xattr_ibody_size (&self, inode: &Inode ) ->Result <usize > { +let total_count = inode.xattr_count (); +if total_count ==0 { +return Ok (0 ); + } + +let inode_offset = self.core.get_inode_offset (inode.id ())as usize ; +let xattr_start = inode_offset + inode.size (); + +let mut header_buf =vec! [0u8 ;size_of ::<XattrHeader >()]; + self.image.read_exact_at (&mut header_buf, xattr_start).await ?; +let header =XattrHeader ::read (&mut Cursor ::new (&header_buf))?; + +let shared_count = header.shared_countas usize ; +let total = total_countas usize ; +if shared_count > total { +return Err (Error ::CorruptedData ( +"xattr shared count exceeds total count" .to_string (), + )); + } + +let mut offset = xattr_start +size_of ::<XattrHeader >() + shared_count *size_of ::<u32 >(); +let inline_count = total - shared_count; +for _in 0 ..inline_count { +let mut entry_buf =vec! [0u8 ;size_of ::<XattrEntry >()]; + self.image.read_exact_at (&mut entry_buf, offset).await ?; +let entry =XattrEntry ::read (&mut Cursor ::new (&entry_buf))?; + offset +=size_of ::<XattrEntry >(); + +let payload = entry.name_lenas usize + entry.value_lenas usize ; + offset = offset + .checked_add (payload) + .ok_or_else (||Error ::CorruptedData ("xattr size overflow" .to_string ()))?; + } + +Ok (offset - xattr_start) + } +pub async fn get_inode (&self, nid:u64 ) ->Result <Inode > {let offset = self.core.get_inode_offset (nid)as usize ;let mut buf =vec! [0u8 ;InodeExtended ::size ()];
@@ -91,7 +131,29 @@ impl<I: AsyncImage> EroFS<I> { } pub(crate) async fn read_inode_block(&self, inode: &Inode, offset: usize) -> Result<Vec<u8>> { - match self.core.plan_inode_block_read(inode, offset)? { + let layout = inode.layout()?; + let xattr_size = if inode.xattr_count() == 0 { + 0 + } else { + match layout { + Layout::FlatInline => { + let block_count = inode.data_size().div_ceil(self.core.block_size); + let block_index = offset / self.core.block_size; + if block_count != 0 && block_index == block_count - 1 { + self.xattr_ibody_size(inode).await? + } else { + 0 + } + } + Layout::ChunkBased => self.xattr_ibody_size(inode).await?, + _ => 0, + } + }; + + match self + .core + .plan_inode_block_read(inode, offset, xattr_size)? + { BlockPlan::Direct { offset, size } => { if size > self.core.block_size { return Err(Error::CorruptedData(format!(
erofs/src/filesystem.rs +50 -3
79 lines not on this page · raw message
erofs/src/sync/filesystem.rs +33 -1
@@ -143,6 +143,16 @@ impl<I: Image> EroFS<I> { self.core.block_size } +fn xattr_ibody_size (&self, inode: &Inode ) ->Result <usize > { +let inode_offset = self.core.get_inode_offset (inode.id ())as usize ; +let xattr_start = inode_offset + inode.size (); +let data = self + .image + .get (xattr_start..) + .ok_or_else (||Error ::OutOfBounds ("failed to read xattr data" .to_string ()))?; +EroFSCore ::xattr_ibody_size_from_slice (data, inode.xattr_count ()) + } +pub fn get_inode (&self, nid:u64 ) ->Result <Inode > {let offset = self.core.get_inode_offset (nid)as usize ;let data = self
@@ -153,7 +163,29 @@ impl<I: Image> EroFS<I> { }pub (crate )fn get_inode_block (&self, inode: &Inode , offset:usize ) ->Result <&[u8 ]> { -match self.core.plan_inode_block_read (inode, offset)? { +let layout = inode.layout ()?; +let xattr_size =if inode.xattr_count () ==0 { +0 + }else { +match layout { +Layout ::FlatInline => { +let block_count = inode.data_size ().div_ceil (self.core.block_size); +let block_index = offset / self.core.block_size; +if block_count !=0 && block_index == block_count -1 { + self.xattr_ibody_size (inode)? + }else { +0 + } + } +Layout ::ChunkBased => self.xattr_ibody_size (inode)?, + _ =>0 , + } + }; + +match self + .core + .plan_inode_block_read (inode, offset, xattr_size)? + {BlockPlan ::Direct { offset, size } => self .image .get (offset..offset + size)
erofs/src/types.rs +7 -0
@@ -185,6 +185,13 @@ impl Inode { } } +pub fn xattr_count (&self) ->u16 { +match self { +Self ::Compact ((_, n)) => n.xattr_count, +Self ::Extended ((_, n)) => n.xattr_count, + } + } +pub fn file_type (&self) ->FileType {match self {Self ::Compact ((_, n)) =>FileType ::from_raw_mode (n.modeas _ ),
Signature · 1 line
2.43.0
Re: [PATCH v2] erofs-rs: fix inline xattr size for tail offsets
Please submit PR directly to Dreamacro/erofs-rs.