Skip to content

Commit

Permalink
Fix fd_readdir to properly truncate directory entry names.
Browse files Browse the repository at this point in the history
Previously, `fd_readdir` was truncating directory entry names based on the
calculation of `min(name_len, buf_len - bufused)`, but `bufused` was not being
updated after writing in the `dirent` structure to the buffer.

This allowed `bufused` to be incremented beyond `buf_len` and returned as the
number of bytes written to the buffer, which is invalid.

This fix adjusts `bufused` when the buffer is written to for the `dirent` so
that name truncation happens as expected.

Fixes #2618.
  • Loading branch information
peterhuene committed Jan 28, 2021
1 parent d1c1cb6 commit 5b35c73
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions crates/wasi-common/src/snapshots/wasi_snapshot_preview1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let dirent_len: types::Size = dirent_raw.len().try_into()?;
let name_raw = name.as_bytes();
let name_len = name_raw.len().try_into()?;
let offset = dirent_len.checked_add(name_len).ok_or(Error::Overflow)?;

// Copy as many bytes of the dirent as we can, up to the end of the buffer.
let dirent_copy_len = min(dirent_len, buf_len - bufused);
Expand All @@ -318,6 +317,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
}

buf = buf.add(dirent_copy_len)?;
bufused = bufused.checked_add(dirent_copy_len).ok_or(Error::Overflow)?;

// Copy as many bytes of the name as we can, up to the end of the buffer.
let name_copy_len = min(name_len, buf_len - bufused);
Expand All @@ -331,8 +331,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
}

buf = buf.add(name_copy_len)?;

bufused += offset;
bufused = bufused.checked_add(name_copy_len).ok_or(Error::Overflow)?;
}

Ok(bufused)
Expand Down

0 comments on commit 5b35c73

Please sign in to comment.