mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-27 11:02:33 +01:00
3dcdee6d2b
Note: this PR does not change any behaviour of zed itself. The old sandbox implementation is still in use. The code in this PR is not considered secure. I'll need more time to torture it before I'm confident :) The old sandbox used `bwrap` to set up mounts to restrict FS access, and an in-sandbox network proxy to forward HTTP requests. It has significant limitations however: - it cannot restrict FS access to paths that don't exist at sandbox creation time. This means that files that become available while a command is running are unprotected - it cannot restrict FS access based on a glob - each individual mount must be specified explicitly - it cannot restrict network access that is not HTTP (i.e. SSH traffic). - it cannot change permissions while a sandboxed command is running - the grants given at the start of the execution are fixed. Agents can request to re-run the app with broader permissions, which is fine, but not great UX - it cannot detect whether a command failed because it hit the sandbox boundary or not. Again, agents can guess, but we'd like something more robust - it cannot dynamically prompt the user to escalate privileges The new sandbox has a different different design which solves these problems: - all restrictions go through seccomp user notifications - this allows us to register a supervisor process with the kernel to validate the syscalls of another program. This supervisor can run arbitrary userspace code to inspect syscalls before allowing/denying/modifying them - we create and upload a policy object to this supervisor which checks syscalls against this policy - if the policy says that the operation requires "ask"-ing, the supervisor will ask zed to show a prompt to the user and report a decision This design is a lot more unified, and since it is much more flexible, is also potentially suitable for sandboxing more parts of zed (user terminals, LSP servers, etc.). Release Notes: - N/A
259 lines
8.4 KiB
Rust
259 lines
8.4 KiB
Rust
//! Thin wrappers over the raw syscalls the sandbox needs that `libc` / `nix`
|
|
//! don't wrap (or that we want with exact semantics).
|
|
|
|
use std::ffi::{CStr, CString};
|
|
use std::io;
|
|
use std::os::fd::{AsRawFd as _, BorrowedFd, FromRawFd as _, OwnedFd, RawFd};
|
|
use std::os::unix::ffi::OsStrExt as _;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
pub(crate) const MOUNT_ATTR_RDONLY: u64 = 0x1;
|
|
|
|
// Syscalls added after the per-arch tables were unified share one number.
|
|
const SYS_PIDFD_OPEN: libc::c_long = 434;
|
|
const SYS_PIDFD_GETFD: libc::c_long = 438;
|
|
const SYS_MOUNT_SETATTR: libc::c_long = 442;
|
|
const AT_RECURSIVE: libc::c_uint = 0x8000;
|
|
|
|
pub(crate) fn errno(code: i32) -> io::Error {
|
|
io::Error::from_raw_os_error(code)
|
|
}
|
|
|
|
pub(crate) fn cvt(result: libc::c_int) -> io::Result<libc::c_int> {
|
|
if result < 0 {
|
|
Err(io::Error::last_os_error())
|
|
} else {
|
|
Ok(result)
|
|
}
|
|
}
|
|
|
|
pub(crate) fn cstring(bytes: &[u8]) -> io::Result<CString> {
|
|
CString::new(bytes).map_err(|_| errno(libc::EINVAL))
|
|
}
|
|
|
|
pub(crate) fn path_cstring(path: &Path) -> io::Result<CString> {
|
|
cstring(path.as_os_str().as_bytes())
|
|
}
|
|
|
|
/// `openat(2)`. `O_CLOEXEC` is always added.
|
|
pub(crate) fn openat(dirfd: RawFd, path: &CStr, flags: i32, mode: u32) -> io::Result<OwnedFd> {
|
|
// SAFETY: valid NUL-terminated path.
|
|
let fd = cvt(unsafe { libc::openat(dirfd, path.as_ptr(), flags | libc::O_CLOEXEC, mode) })?;
|
|
// SAFETY: a freshly returned descriptor that nothing else owns.
|
|
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
|
|
}
|
|
|
|
pub(crate) fn open(path: &Path, flags: i32) -> io::Result<OwnedFd> {
|
|
openat(libc::AT_FDCWD, &path_cstring(path)?, flags, 0)
|
|
}
|
|
|
|
/// `/proc/self/fd/N`: opening or operating on this path reaches exactly the
|
|
/// object `fd` refers to, through the mount `fd` was opened on.
|
|
pub(crate) fn proc_self_fd(fd: BorrowedFd<'_>) -> PathBuf {
|
|
PathBuf::from(format!("/proc/self/fd/{}", fd.as_raw_fd()))
|
|
}
|
|
|
|
/// The path of the object `fd` refers to, as rendered by the kernel.
|
|
///
|
|
/// For objects in another mount namespace (e.g. a sandbox's), the kernel
|
|
/// renders the path as seen from that namespace's root.
|
|
pub(crate) fn fd_path(fd: BorrowedFd<'_>) -> io::Result<PathBuf> {
|
|
std::fs::read_link(proc_self_fd(fd))
|
|
}
|
|
|
|
pub(crate) fn fstat(fd: BorrowedFd<'_>) -> io::Result<libc::stat> {
|
|
// SAFETY: `stat` is plain old data, filled in by the kernel.
|
|
let mut stat: libc::stat = unsafe { std::mem::zeroed() };
|
|
// SAFETY: valid fd and pointer to a `stat`.
|
|
cvt(unsafe { libc::fstat(fd.as_raw_fd(), &mut stat) })?;
|
|
Ok(stat)
|
|
}
|
|
|
|
pub(crate) fn mount(
|
|
source: Option<&Path>,
|
|
target: &Path,
|
|
filesystem: Option<&str>,
|
|
flags: libc::c_ulong,
|
|
data: Option<&str>,
|
|
) -> io::Result<()> {
|
|
let source = source.map(path_cstring).transpose()?;
|
|
let target = path_cstring(target)?;
|
|
let filesystem = filesystem
|
|
.map(|name| cstring(name.as_bytes()))
|
|
.transpose()?;
|
|
let data = data.map(|data| cstring(data.as_bytes())).transpose()?;
|
|
let pointer = |value: &Option<CString>| {
|
|
value
|
|
.as_ref()
|
|
.map_or(std::ptr::null(), |value| value.as_ptr())
|
|
};
|
|
// SAFETY: every pointer is null or a valid NUL-terminated string.
|
|
cvt(unsafe {
|
|
libc::mount(
|
|
pointer(&source),
|
|
target.as_ptr(),
|
|
pointer(&filesystem),
|
|
flags,
|
|
pointer(&data).cast(),
|
|
)
|
|
})?;
|
|
Ok(())
|
|
}
|
|
|
|
#[repr(C)]
|
|
struct MountAttr {
|
|
attr_set: u64,
|
|
attr_clr: u64,
|
|
propagation: u64,
|
|
userns_fd: u64,
|
|
}
|
|
|
|
/// `mount_setattr(2)`: add `attributes` to the mount at `target` (and, with
|
|
/// `recursive`, every mount beneath it).
|
|
pub(crate) fn mount_setattr(target: &Path, attributes: u64, recursive: bool) -> io::Result<()> {
|
|
let target = path_cstring(target)?;
|
|
let attr = MountAttr {
|
|
attr_set: attributes,
|
|
attr_clr: 0,
|
|
propagation: 0,
|
|
userns_fd: 0,
|
|
};
|
|
let flags = if recursive { AT_RECURSIVE } else { 0 };
|
|
// SAFETY: valid path and a correctly sized `mount_attr`.
|
|
let result = unsafe {
|
|
libc::syscall(
|
|
SYS_MOUNT_SETATTR,
|
|
libc::AT_FDCWD,
|
|
target.as_ptr(),
|
|
flags,
|
|
&attr as *const MountAttr,
|
|
size_of::<MountAttr>(),
|
|
)
|
|
};
|
|
if result < 0 {
|
|
return Err(io::Error::last_os_error());
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Duplicate `fd` to the lowest free descriptor `>= minimum`, close-on-exec.
|
|
pub(crate) fn duplicate_above(fd: BorrowedFd<'_>, minimum: RawFd) -> io::Result<OwnedFd> {
|
|
// SAFETY: fcntl on a valid descriptor.
|
|
let duplicate = cvt(unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_DUPFD_CLOEXEC, minimum) })?;
|
|
// SAFETY: a freshly returned descriptor that nothing else owns.
|
|
Ok(unsafe { OwnedFd::from_raw_fd(duplicate) })
|
|
}
|
|
|
|
fn own_syscall_fd(result: libc::c_long) -> io::Result<OwnedFd> {
|
|
if result < 0 {
|
|
return Err(io::Error::last_os_error());
|
|
}
|
|
// SAFETY: a freshly returned descriptor that nothing else owns.
|
|
Ok(unsafe { OwnedFd::from_raw_fd(result as RawFd) })
|
|
}
|
|
|
|
/// A duplicate of descriptor `fd` of process `pid` (a thread group leader).
|
|
pub(crate) fn pidfd_getfd(pid: i32, fd: RawFd) -> io::Result<OwnedFd> {
|
|
// SAFETY: plain syscalls; each returns a new descriptor or fails.
|
|
let pidfd = own_syscall_fd(unsafe { libc::syscall(SYS_PIDFD_OPEN, pid, 0) })?;
|
|
// SAFETY: as above.
|
|
own_syscall_fd(unsafe { libc::syscall(SYS_PIDFD_GETFD, pidfd.as_raw_fd(), fd, 0) })
|
|
}
|
|
|
|
/// `socket(2)`. `SOCK_CLOEXEC` is always added.
|
|
pub(crate) fn socket(domain: i32, kind: i32, protocol: i32) -> io::Result<OwnedFd> {
|
|
// SAFETY: plain socket call.
|
|
let fd = cvt(unsafe { libc::socket(domain, kind | libc::SOCK_CLOEXEC, protocol) })?;
|
|
// SAFETY: a freshly returned descriptor that nothing else owns.
|
|
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
|
|
}
|
|
|
|
pub(crate) fn socket_option(fd: BorrowedFd<'_>, level: i32, name: i32) -> io::Result<i32> {
|
|
let mut value: libc::c_int = 0;
|
|
let mut length = size_of::<libc::c_int>() as libc::socklen_t;
|
|
// SAFETY: an int-sized buffer with its length.
|
|
cvt(unsafe {
|
|
libc::getsockopt(
|
|
fd.as_raw_fd(),
|
|
level,
|
|
name,
|
|
(&mut value as *mut libc::c_int).cast(),
|
|
&mut length,
|
|
)
|
|
})?;
|
|
Ok(value)
|
|
}
|
|
|
|
pub(crate) fn set_socket_option(
|
|
fd: BorrowedFd<'_>,
|
|
level: i32,
|
|
name: i32,
|
|
value: i32,
|
|
) -> io::Result<()> {
|
|
// SAFETY: an int-sized value with its length.
|
|
cvt(unsafe {
|
|
libc::setsockopt(
|
|
fd.as_raw_fd(),
|
|
level,
|
|
name,
|
|
(&value as *const libc::c_int).cast(),
|
|
size_of::<libc::c_int>() as libc::socklen_t,
|
|
)
|
|
})?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Wait until `fd` has one of `events`, giving up after `timeout` or as
|
|
/// soon as `keep_waiting` returns false.
|
|
pub(crate) fn wait_for(
|
|
fd: BorrowedFd<'_>,
|
|
events: libc::c_short,
|
|
timeout: std::time::Duration,
|
|
keep_waiting: &dyn Fn() -> bool,
|
|
) -> io::Result<()> {
|
|
let deadline = std::time::Instant::now() + timeout;
|
|
loop {
|
|
let mut poll_fd = libc::pollfd {
|
|
fd: fd.as_raw_fd(),
|
|
events,
|
|
revents: 0,
|
|
};
|
|
// SAFETY: one valid pollfd.
|
|
let ready = unsafe { libc::poll(&mut poll_fd, 1, 250) };
|
|
if ready > 0 {
|
|
return Ok(());
|
|
}
|
|
if ready < 0 {
|
|
let error = io::Error::last_os_error();
|
|
if error.raw_os_error() != Some(libc::EINTR) {
|
|
return Err(error);
|
|
}
|
|
}
|
|
if !keep_waiting() {
|
|
return Err(errno(libc::ECONNABORTED));
|
|
}
|
|
if std::time::Instant::now() >= deadline {
|
|
return Err(errno(libc::ETIMEDOUT));
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn set_parent_death_signal() -> io::Result<()> {
|
|
// SAFETY: plain prctl.
|
|
cvt(unsafe { libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL, 0, 0, 0) })?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Take ownership of a descriptor this process inherited at a fixed number.
|
|
///
|
|
/// # Safety
|
|
/// `fd` must be open and not owned by anything else in this process.
|
|
pub(crate) unsafe fn inherited_fd(fd: RawFd) -> io::Result<OwnedFd> {
|
|
// SAFETY: fcntl on a possibly-closed descriptor only reports EBADF.
|
|
let flags = cvt(unsafe { libc::fcntl(fd, libc::F_GETFD) })?;
|
|
// SAFETY: as above.
|
|
cvt(unsafe { libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC) })?;
|
|
// SAFETY: guaranteed by the caller.
|
|
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
|
|
}
|