Skip to content

Commit

Permalink
Fix stat when the last path component is a symlink to ...
Browse files Browse the repository at this point in the history
Replace `stat_via_parent` with `stat_manually` which similarly to
`open_manually`. Factor the `open_manually` implemenation to allow
parts to be reused.
  • Loading branch information
sunfishcode committed Aug 21, 2020
1 parent 87e4836 commit d1fa735
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 246 deletions.
4 changes: 2 additions & 2 deletions cap-primitives/src/fs/canonicalize_manually.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Manual path canonicalization, one component at a time, with manual symlink
//! resolution, in order to enforce sandboxing.

use crate::fs::{canonicalize_options, open_manually, FollowSymlinks, MaybeOwnedFile};
use crate::fs::{canonicalize_options, open_manually_impl, FollowSymlinks, MaybeOwnedFile};
use std::{
fs, io,
path::{Path, PathBuf},
Expand All @@ -28,7 +28,7 @@ pub(crate) fn canonicalize_manually(
let mut canonical_path = PathBuf::new();
let start = MaybeOwnedFile::borrowed(start);

if let Err(e) = open_manually(
if let Err(e) = open_manually_impl(
start,
path,
canonicalize_options().follow(follow),
Expand Down
22 changes: 22 additions & 0 deletions cap-primitives/src/fs/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,35 @@ impl Metadata {
/// Constructs a new instance of `Self` from the given `std::fs::Metadata`.
#[inline]
pub fn from_std(std: fs::Metadata) -> Self {
// TODO: Initialize `created` on Linux with `std.created().ok()` once yanix
// has `statx` and we make use of it.
Self {
file_type: FileType::from_std(std.file_type()),
len: std.len(),
permissions: Permissions::from_std(std.permissions()),
modified: std.modified().ok(),
accessed: std.accessed().ok(),

#[cfg(any(
target_os = "freebsd",
target_os = "openbsd",
target_os = "macos",
target_os = "ios",
target_os = "netbsd",
windows,
))]
created: std.created().ok(),

#[cfg(not(any(
target_os = "freebsd",
target_os = "openbsd",
target_os = "macos",
target_os = "ios",
target_os = "netbsd",
windows,
)))]
created: None,

ext: MetadataExt::from_std(std),
}
}
Expand Down
2 changes: 0 additions & 2 deletions cap-primitives/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ mod set_permissions;
#[cfg(not(target_os = "linux"))] // doesn't work reliably on linux
mod set_permissions_via_parent;
mod stat;
mod stat_via_parent;
mod symlink;
mod symlink_via_parent;
mod unlink;
Expand Down Expand Up @@ -70,7 +69,6 @@ pub(crate) use rename_via_parent::*;
pub(crate) use rmdir_via_parent::*;
#[cfg(not(target_os = "linux"))] // doesn't work reliably on linux
pub(crate) use set_permissions_via_parent::*;
pub(crate) use stat_via_parent::*;
pub(crate) use symlink_via_parent::*;
pub(crate) use unlink_via_parent::*;

Expand Down
4 changes: 2 additions & 2 deletions cap-primitives/src/fs/open_entry_manually.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::fs::{
open_manually, open_unchecked, readlink_one, FollowSymlinks, MaybeOwnedFile, OpenOptions,
open_manually_impl, open_unchecked, readlink_one, FollowSymlinks, MaybeOwnedFile, OpenOptions,
OpenUncheckedError,
};
use std::{ffi::OsStr, fs, io};
Expand All @@ -19,7 +19,7 @@ pub(crate) fn open_entry_manually(
let mut symlink_count = 0;
let destination = readlink_one(start, path, &mut symlink_count)?;
let maybe = MaybeOwnedFile::borrowed(start);
open_manually(maybe, &destination, options, &mut symlink_count, None)
open_manually_impl(maybe, &destination, options, &mut symlink_count, None)
.map(MaybeOwnedFile::unwrap_owned)
}
Err(OpenUncheckedError::NotFound(err)) | Err(OpenUncheckedError::Other(err)) => Err(err),
Expand Down
Loading

0 comments on commit d1fa735

Please sign in to comment.