-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These tests are based on the filepath-securejoin tests, and notably include tests of the key cases when operating on procfs. Signed-off-by: Aleksa Sarai <[email protected]>
- Loading branch information
Showing
7 changed files
with
419 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* libpathrs: safe path resolution on Linux | ||
* Copyright (C) 2019-2024 Aleksa Sarai <[email protected]> | ||
* Copyright (C) 2019-2024 SUSE LLC | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your option) any | ||
* later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along | ||
* with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
use std::{ | ||
ffi::CString, | ||
fs::File, | ||
io::Error as IOError, | ||
os::fd::{AsRawFd, RawFd}, | ||
path::{Path, PathBuf}, | ||
ptr, | ||
}; | ||
|
||
use crate::syscalls; | ||
|
||
use anyhow::Error; | ||
use libc::c_int; | ||
|
||
unsafe fn unshare(flags: c_int) -> Result<(), IOError> { | ||
// SAFETY: Caller guarantees that this unshare operation is safe. | ||
let ret = unsafe { libc::unshare(flags) }; | ||
let err = IOError::last_os_error(); | ||
if ret >= 0 { | ||
Ok(()) | ||
} else { | ||
Err(err) | ||
} | ||
} | ||
|
||
unsafe fn setns(fd: RawFd, flags: c_int) -> Result<(), IOError> { | ||
// SAFETY: Caller guarantees that this setns operation is safe. | ||
let ret = unsafe { libc::setns(fd, flags) }; | ||
let err = IOError::last_os_error(); | ||
if ret >= 0 { | ||
Ok(()) | ||
} else { | ||
Err(err) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub(crate) enum MountType { | ||
Tmpfs, | ||
Bind { src: PathBuf }, | ||
} | ||
|
||
pub(crate) fn mount<P: AsRef<Path>>(dst: P, ty: MountType) -> Result<(), Error> { | ||
let dst = dst.as_ref(); | ||
let dst_file = syscalls::openat(libc::AT_FDCWD, dst, libc::O_NOFOLLOW | libc::O_PATH, 0)?; | ||
let dst_path = CString::new(format!("/proc/self/fd/{}", dst_file.as_raw_fd()))?; | ||
|
||
let ret = match ty { | ||
MountType::Tmpfs => unsafe { | ||
libc::mount( | ||
CString::new("")?.as_ptr(), | ||
dst_path.as_ptr(), | ||
CString::new("tmpfs")?.as_ptr(), | ||
0, | ||
ptr::null(), | ||
) | ||
}, | ||
MountType::Bind { src } => { | ||
let src_file = | ||
syscalls::openat(libc::AT_FDCWD, src, libc::O_NOFOLLOW | libc::O_PATH, 0)?; | ||
let src_path = CString::new(format!("/proc/self/fd/{}", src_file.as_raw_fd()))?; | ||
unsafe { | ||
libc::mount( | ||
src_path.as_ptr(), | ||
dst_path.as_ptr(), | ||
ptr::null(), | ||
libc::MS_BIND, | ||
ptr::null(), | ||
) | ||
} | ||
} | ||
}; | ||
let err = IOError::last_os_error(); | ||
|
||
if ret >= 0 { | ||
Ok(()) | ||
} else { | ||
Err(err.into()) | ||
} | ||
} | ||
|
||
pub(crate) fn in_mnt_ns<F, T>(func: F) -> Result<T, Error> | ||
where | ||
F: FnOnce() -> Result<T, Error>, | ||
{ | ||
let old_ns = File::open("/proc/self/ns/mnt")?; | ||
|
||
// TODO: Run this in a subprocess. | ||
|
||
unsafe { unshare(libc::CLONE_FS | libc::CLONE_NEWNS) } | ||
.expect("unable to create a mount namespace"); | ||
|
||
let ret = func(); | ||
|
||
unsafe { setns(old_ns.as_raw_fd(), libc::CLONE_NEWNS) } | ||
.expect("unable to rejoin old namespace"); | ||
|
||
ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,6 @@ | |
|
||
mod root; | ||
pub(crate) use root::*; | ||
|
||
mod mntns; | ||
pub(in crate::tests) use mntns::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ | |
|
||
pub(crate) mod common; | ||
|
||
mod test_procfs; | ||
mod test_resolve; |
Oops, something went wrong.