Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement secure_join for path #354

Merged
merged 3 commits into from
Oct 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use nix::unistd;
use std::collections::HashMap;
use std::ffi::CString;
use std::fs::{self, DirBuilder, File};
use std::io::ErrorKind;
use std::ops::Deref;
use std::os::linux::fs::MetadataExt;
use std::os::unix::fs::DirBuilderExt;
Expand Down Expand Up @@ -152,6 +153,71 @@ pub fn ensure_procfs(path: &Path) -> Result<()> {
Ok(())
}

pub fn secure_join(rootfs: &Path, unsafe_path: &Path) -> Result<PathBuf> {
let mut rootfs = PathBuf::from(rootfs);
let mut path = PathBuf::from(unsafe_path);
let mut clean_path = PathBuf::new();

let mut part = path.iter();
let mut i = 0;

loop {
if i > 255 {
bail!("dereference too many symlinks, may be infinite loop");
}

let part_path;
match part.next() {
Some(part) => {
part_path = PathBuf::from(part);
}
None => {
break;
}
}

if !part_path.is_absolute() {
if part_path.starts_with("..") {
clean_path.pop();
} else {
// check if symlink then dereference
let curr_path = PathBuf::from(&rootfs).join(&clean_path).join(&part_path);
let metadata = match curr_path.symlink_metadata() {
Ok(metadata) => Some(metadata),
Err(error) => match error.kind() {
// if file does not exists, treat it as normal path
ErrorKind::NotFound => None,
other_error => {
bail!(
"unable to obtain symlink metadata for file {:?}: {:?}",
curr_path,
other_error
);
}
},
};

if let Some(metadata) = metadata {
if metadata.file_type().is_symlink() {
let link_path = fs::read_link(curr_path)?;
path = link_path.join(part.as_path());
part = path.iter();

// increase after dereference symlink
i += 1;
continue;
}
}

clean_path.push(&part_path);
}
}
}

rootfs.push(clean_path);
Ok(rootfs)
}

pub struct TempDir {
path: Option<PathBuf>,
}
Expand Down Expand Up @@ -298,4 +364,75 @@ mod tests {

Ok(())
}
#[test]
fn test_secure_join() {
assert_eq!(
secure_join(Path::new("/tmp/rootfs"), Path::new("path")).unwrap(),
PathBuf::from("/tmp/rootfs/path")
);
assert_eq!(
secure_join(Path::new("/tmp/rootfs"), Path::new("more/path")).unwrap(),
PathBuf::from("/tmp/rootfs/more/path")
);
assert_eq!(
secure_join(Path::new("/tmp/rootfs"), Path::new("/absolute/path")).unwrap(),
PathBuf::from("/tmp/rootfs/absolute/path")
);
assert_eq!(
secure_join(
Path::new("/tmp/rootfs"),
Path::new("/path/with/../parent/./sample")
)
.unwrap(),
PathBuf::from("/tmp/rootfs/path/parent/sample")
);
assert_eq!(
secure_join(Path::new("/tmp/rootfs"), Path::new("/../../../../tmp")).unwrap(),
PathBuf::from("/tmp/rootfs/tmp")
);
assert_eq!(
secure_join(Path::new("/tmp/rootfs"), Path::new("./../../../../var/log")).unwrap(),
PathBuf::from("/tmp/rootfs/var/log")
);
assert_eq!(
secure_join(
Path::new("/tmp/rootfs"),
Path::new("../../../../etc/passwd")
)
.unwrap(),
PathBuf::from("/tmp/rootfs/etc/passwd")
);
}
#[test]
fn test_secure_join_symlink() {
use std::os::unix::fs::symlink;

let tmp = create_temp_dir("root").unwrap();
let test_root_dir = tmp.path();

symlink("somepath", PathBuf::from(&test_root_dir).join("etc")).unwrap();
symlink(
"../../../../../../../../../../../../../etc",
PathBuf::from(&test_root_dir).join("longbacklink"),
)
.unwrap();
symlink(
"/../../../../../../../../../../../../../etc/passwd",
PathBuf::from(&test_root_dir).join("absolutelink"),
)
.unwrap();

assert_eq!(
secure_join(test_root_dir, PathBuf::from("etc").as_path()).unwrap(),
PathBuf::from(&test_root_dir).join("somepath")
);
assert_eq!(
secure_join(test_root_dir, PathBuf::from("longbacklink").as_path()).unwrap(),
PathBuf::from(&test_root_dir).join("somepath")
);
assert_eq!(
secure_join(test_root_dir, PathBuf::from("absolutelink").as_path()).unwrap(),
PathBuf::from(&test_root_dir).join("somepath/passwd")
);
}
}