Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions docs/cookbook/networking.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ The host's `inet hyper` table holds the rules that are the same for every VM:
their networks and metadata services in: private, shared and link-local IPv4,
unique local IPv6, and the NAT64, 6to4 and IPv4-mapped prefixes that lead
back to them.
- **No transit access.** New connections into the transit network are dropped
whatever `blocked` says, so VMs can't talk to each other over it.
- **No transit access.** New connections into the transit network, and packets
into it that conntrack can't track, are dropped whatever `blocked` says, so
neither VMs nor the internet reach it.
- **No untranslated traffic.** Packets conntrack can't track are dropped, as is
anything from a VM whose source is outside the transit network and the VM
range.
Expand Down
9 changes: 9 additions & 0 deletions native/.cargo/mutants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ exclude_re = [
"suidhelper/src/net/vm\\.rs:.*VmNet<'_>::",
"suidhelper/src/cli/vm/claim\\.rs:.*SocketPin<'_>::",
"suidhelper/src/cli/vm/teardown\\.rs:.*Teardown::unpin_sockets",
# dmsetup runs as genuine root from a root-owned binary; killed by the fake-dmsetup
# dm::thin _as_root tests in hyperrs-linux (snapshot_always_resumes_the_origin_as_root,
# deactivate_then_delete_removes_the_device_then_frees_its_id_as_root) and, for the old-pool
# check, suidhelper's pool_create_zeroes_the_metadata_only_once_no_old_pool_is_live_as_root,
# all in the privileged E2E job.
"hyperrs-linux/src/dm/mod\\.rs:.*Dmsetup::run ",
"hyperrs-linux/src/dm/device\\.rs:.*Device<'a>::(suspend|resume|remove) ",
"hyperrs-linux/src/dm/thin\\.rs:.*ThinPool<'a>::send ",
"suidhelper/src/cli/thin\\.rs:.*delete ! in <impl Op for ThinPoolCreate>::run$",
# Guest network setup runs inside a booted guest; killed by test/e2e/guest_network_test.exs
# in the KVM integration job.
"guest-agent/src/net\\.rs:.*GuestNet::(from_proc|configure)",
Expand Down
27 changes: 16 additions & 11 deletions native/hyperrs-linux/src/dm/device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{DmName, Dmsetup, RemoveMode, RunError};
use hyperrs_fs::dev::{DeviceNumber, ParseDeviceNumberError};
use hyperrs_fs::path::{IsAbsolute, SafePathBuf, StrictComponents};
use hyperrs_process::privilege::Root;
use std::ffi::OsStr;
use thiserror::Error as ThisError;

Expand All @@ -15,6 +16,7 @@ pub enum NumberError {
#[derive(Debug, Clone)]
pub struct Device<'a> {
pub(super) dmsetup: &'a Dmsetup,
pub(super) root: &'a Root<'a>,
pub(super) name: DmName,
}

Expand All @@ -28,26 +30,29 @@ impl<'a> Device<'a> {
}

pub fn number(&self) -> Result<DeviceNumber, NumberError> {
let out = self.dmsetup.run([
OsStr::new("info"),
OsStr::new("-c"),
OsStr::new("--noheadings"),
OsStr::new("-o"),
OsStr::new("major,minor"),
self.name.as_ref(),
])?;
let out = self.dmsetup.run(
self.root,
[
OsStr::new("info"),
OsStr::new("-c"),
OsStr::new("--noheadings"),
OsStr::new("-o"),
OsStr::new("major,minor"),
self.name.as_ref(),
],
)?;
Ok(out.trim().parse()?)
}

pub fn suspend(&self) -> Result<(), RunError> {
self.dmsetup
.run([OsStr::new("suspend"), self.name.as_ref()])
.run(self.root, [OsStr::new("suspend"), self.name.as_ref()])
.map(drop)
}

pub fn resume(&self) -> Result<(), RunError> {
self.dmsetup
.run([OsStr::new("resume"), self.name.as_ref()])
.run(self.root, [OsStr::new("resume"), self.name.as_ref()])
.map(drop)
}

Expand All @@ -57,6 +62,6 @@ impl<'a> Device<'a> {
args.push(OsStr::new("--retry"));
}
args.push(self.name.as_ref());
self.dmsetup.run(args).map(drop)
self.dmsetup.run(self.root, args).map(drop)
}
}
29 changes: 19 additions & 10 deletions native/hyperrs-linux/src/dm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ use derive_more::Constructor;
pub use dev_path::{DevPath, DevPathError};
pub use device::{Device, NumberError};
use hyperrs_process::binary::RootSafeBin;
use hyperrs_process::privilege::{Root, RootCommandExt};
pub use name::{DmName, InvalidDmName, OpenDeviceError, ParseDmDeviceError, SizeError};
use std::ffi::OsStr;
use std::io;
use std::os::unix::io::AsFd;
pub use table::{
DmTable, LinearTable, Origin, ParseTableError, Persistence, Role, SnapshotTable, ThinPoolTable,
ThinTable,
Expand Down Expand Up @@ -46,34 +48,40 @@ pub struct Dmsetup {
}

impl Dmsetup {
pub fn create(
&self,
pub fn create<'a>(
&'a self,
root: &'a Root<'a>,
name: &DmName,
table: &DmTable,
access: Access,
) -> Result<Device<'_>, RunError> {
) -> Result<Device<'a>, RunError> {
let table = table.to_string();
let mut args = vec![OsStr::new("create"), name.as_ref()];
if access == Access::ReadOnly {
args.push(OsStr::new("--readonly"));
}
args.extend([OsStr::new("--table"), OsStr::new(&table)]);
self.run(args)?;
Ok(self.device(name))
self.run(root, args)?;
Ok(self.device(root, name))
}

pub fn device(&self, name: &DmName) -> Device<'_> {
pub fn device<'a>(&'a self, root: &'a Root<'a>, name: &DmName) -> Device<'a> {
Device {
dmsetup: self,
root,
name: name.clone(),
}
}

pub fn thin_pool(&self, name: &DmName) -> Result<ThinPool<'_>, ThinPoolError> {
ThinPool::open(self, name)
pub fn thin_pool<'a>(
&'a self,
root: &'a Root<'a>,
name: &DmName,
) -> Result<ThinPool<'a>, ThinPoolError> {
ThinPool::open(self, root, name)
}

fn run<I, S>(&self, args: I) -> Result<String, RunError>
fn run<I, S>(&self, root: &Root<'_>, args: I) -> Result<String, RunError>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
Expand All @@ -82,7 +90,8 @@ impl Dmsetup {
.bin
.command()
.args(args)
.env_clear()
// The binary fd stays open so an interpreted tool can reopen itself via /proc/self/fd.
.spawn_as_root(root, &[self.bin.as_fd()])
.output()
.map_err(RunError::Spawn)?;
if !out.status.success() {
Expand Down
64 changes: 41 additions & 23 deletions native/hyperrs-linux/src/dm/thin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{
use crate::Access;
use derive_more::{Display, Into};
use hyperrs_fs::dev::DeviceNumber;
use hyperrs_process::privilege::Root;
use std::ffi::OsStr;
use std::num::ParseIntError;
use std::str::FromStr;
Expand Down Expand Up @@ -149,11 +150,15 @@ pub struct Thin<'a, S> {
}

impl<'a> ThinPool<'a> {
pub(super) fn open(dmsetup: &'a Dmsetup, name: &DmName) -> Result<Self, ThinPoolError> {
let table = dmsetup.run([OsStr::new("table"), name.as_ref()])?;
pub(super) fn open(
dmsetup: &'a Dmsetup,
root: &'a Root<'a>,
name: &DmName,
) -> Result<Self, ThinPoolError> {
let table = dmsetup.run(root, [OsStr::new("table"), name.as_ref()])?;
match Self::pool_metadata(&table) {
Some(metadata) => Ok(Self {
device: dmsetup.device(name),
device: dmsetup.device(root, name),
metadata,
}),
None => Err(ThinPoolError::NotAThinPool {
Expand Down Expand Up @@ -191,8 +196,8 @@ impl<'a> ThinPool<'a> {
}

pub fn open_thin(&self, name: &DmName) -> Result<Thin<'a, Active<'a>>, OpenThinError> {
let dmsetup = self.device.dmsetup;
let table = dmsetup.run([OsStr::new("table"), name.as_ref()])?;
let Device { dmsetup, root, .. } = self.device;
let table = dmsetup.run(root, [OsStr::new("table"), name.as_ref()])?;
let pool = self.device.number()?;
let not_a_thin = || OpenThinError::NotAThin {
name: name.clone(),
Expand All @@ -206,7 +211,7 @@ impl<'a> ThinPool<'a> {
Ok(Thin {
pool: self.clone(),
id: id.parse()?,
state: Active(dmsetup.device(name)),
state: Active(dmsetup.device(root, name)),
})
}
_ => Err(not_a_thin()),
Expand All @@ -233,12 +238,15 @@ impl<'a> ThinPool<'a> {
let message = message.to_string();
self.device
.dmsetup
.run([
OsStr::new("message"),
self.device.name().as_ref(),
OsStr::new("0"),
OsStr::new(&message),
])
.run(
self.device.root,
[
OsStr::new("message"),
self.device.name().as_ref(),
OsStr::new("0"),
OsStr::new(&message),
],
)
.map(drop)
}
}
Expand All @@ -262,11 +270,8 @@ impl<'a> Thin<'a, Inactive> {
id: self.id,
origin,
});
let device = self
.pool
.device
.dmsetup
.create(name, &table, Access::ReadWrite)?;
let Device { dmsetup, root, .. } = self.pool.device;
let device = dmsetup.create(root, name, &table, Access::ReadWrite)?;
Ok(Thin {
pool: self.pool,
id: self.id,
Expand Down Expand Up @@ -329,6 +334,7 @@ mod tests {
use assert_fs::prelude::*;
use assert_fs::TempDir;
use hyperrs_process::binary::RootSafeBin;
use hyperrs_process::privilege::Unprivileged;
use pretty_assertions::assert_eq;
use proptest::prelude::*;
use rstest::rstest;
Expand Down Expand Up @@ -544,8 +550,10 @@ mod tests {
eprintln!("SKIP thin_pool_refuses: needs root to own the fake dmsetup");
return;
}
let mut token = Unprivileged::take().unwrap();
let root = token.elevate().unwrap();
let fake = Fake::new(table, &[]);
let got = fake.dmsetup.thin_pool(&name("hyper-x"));
let got = fake.dmsetup.thin_pool(&root, &name("hyper-x"));
assert_matches!(got, Err(ThinPoolError::NotAThinPool { .. }));
assert_eq!(fake.calls(), ["table hyper-x"]);
}
Expand All @@ -569,8 +577,10 @@ mod tests {
eprintln!("SKIP snapshot_always_resumes: needs root to own the fake dmsetup");
return;
}
let mut token = Unprivileged::take().unwrap();
let root = token.elevate().unwrap();
let fake = Fake::new(POOL_TABLE, failing);
let pool = fake.dmsetup.thin_pool(&name("hyper-pool")).unwrap();
let pool = fake.dmsetup.thin_pool(&root, &name("hyper-pool")).unwrap();
let active = pool
.create_thin(ThinId::new(1).unwrap())
.unwrap()
Expand All @@ -589,8 +599,10 @@ mod tests {
eprintln!("SKIP delete_removes: needs root to own the fake dmsetup");
return;
}
let mut token = Unprivileged::take().unwrap();
let root = token.elevate().unwrap();
let fake = Fake::new(POOL_TABLE, &[]);
let pool = fake.dmsetup.thin_pool(&name("hyper-pool")).unwrap();
let pool = fake.dmsetup.thin_pool(&root, &name("hyper-pool")).unwrap();
pool.create_thin(ThinId::new(1).unwrap())
.unwrap()
.activate(&name("hyper-vol"), 100, None)
Expand All @@ -612,14 +624,16 @@ mod tests {
eprintln!("SKIP open_thin: needs root to own the fake dmsetup");
return;
}
let mut token = Unprivileged::take().unwrap();
let root = token.elevate().unwrap();
let fake = Fake::with_tables(
&[
("hyper-pool", POOL_TABLE),
("hyper-vol", "0 100 thin 253:1 7 253:4"),
],
&[],
);
let pool = fake.dmsetup.thin_pool(&name("hyper-pool")).unwrap();
let pool = fake.dmsetup.thin_pool(&root, &name("hyper-pool")).unwrap();

let snap = pool
.open_thin(&name("hyper-vol"))
Expand Down Expand Up @@ -652,8 +666,10 @@ mod tests {
eprintln!("SKIP open_thin refusal: needs root to own the fake dmsetup");
return;
}
let mut token = Unprivileged::take().unwrap();
let root = token.elevate().unwrap();
let fake = Fake::with_tables(&[("hyper-pool", POOL_TABLE), ("hyper-vol", table)], &[]);
let pool = fake.dmsetup.thin_pool(&name("hyper-pool")).unwrap();
let pool = fake.dmsetup.thin_pool(&root, &name("hyper-pool")).unwrap();

assert_matches!(
pool.open_thin(&name("hyper-vol")),
Expand All @@ -669,8 +685,10 @@ mod tests {
eprintln!("SKIP open_thin id: needs root to own the fake dmsetup");
return;
}
let mut token = Unprivileged::take().unwrap();
let root = token.elevate().unwrap();
let fake = Fake::with_tables(&[("hyper-pool", POOL_TABLE), ("hyper-vol", table)], &[]);
let pool = fake.dmsetup.thin_pool(&name("hyper-pool")).unwrap();
let pool = fake.dmsetup.thin_pool(&root, &name("hyper-pool")).unwrap();

assert_matches!(
pool.open_thin(&name("hyper-vol")),
Expand Down
12 changes: 6 additions & 6 deletions native/hyperrs-linux/src/dm/thin_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use super::ThinId;
use derive_more::{Constructor, Into};
use hyperrs_fs::file::{IsBlockDevice, SafeFile};
use hyperrs_process::binary::RootSafeBin;
use hyperrs_process::privilege::{self, RootCommandExt};
use serde::Deserialize;
use std::io;
use std::os::unix::io::AsRawFd;
use std::os::unix::io::{AsFd, AsRawFd};
use thiserror::Error as ThisError;

#[derive(Debug, ThisError)]
pub enum DumpError {
#[error("passing the metadata device: {0}")]
Inherit(#[source] nix::Error),
#[error("running thin_dump: {0}")]
Spawn(#[source] io::Error),
#[error("thin_dump failed: {0}")]
Expand Down Expand Up @@ -44,16 +43,17 @@ pub struct ThinDump {
impl ThinDump {
pub fn dump<R, O>(
&self,
root: &privilege::Root<'_>,
metadata: &SafeFile<IsBlockDevice, R, O>,
id: ThinId,
) -> Result<MetadataDump, DumpError> {
let inherited = metadata.inheritable().map_err(DumpError::Inherit)?;
let out = self
.bin
.command()
.args(["--metadata-snap", "--dev-id", &id.to_string()])
.arg(format!("/proc/self/fd/{}", inherited.as_raw_fd()))
.env_clear()
.arg(format!("/proc/self/fd/{}", metadata.as_raw_fd()))
// The binary fd stays open so an interpreted tool can reopen itself via /proc/self/fd.
.spawn_as_root(root, &[self.bin.as_fd(), metadata.as_fd()])
.output()
.map_err(DumpError::Spawn)?;

Expand Down
5 changes: 4 additions & 1 deletion native/hyperrs-linux/tests/dm_kernel_argv_as_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use hyperrs_linux::dm::{DevPath, DmName, DmTable, Dmsetup, LinearTable, Origin,
use hyperrs_linux::loopdev::{LoopControl, LoopDevice};
use hyperrs_linux::Access;
use hyperrs_process::binary::RootSafeBin;
use hyperrs_process::privilege::Unprivileged;
use pretty_assertions::assert_eq;
use rstest::rstest;
use std::ffi::OsStr;
Expand Down Expand Up @@ -66,6 +67,8 @@ fn a_linear_table_is_backed_by_the_path_it_names_as_root(
eprintln!("SKIP: needs root");
return;
}
let mut token = Unprivileged::take().unwrap();
let root = token.elevate().unwrap();
let dir = TempDir::new().unwrap();
let base = fs::canonicalize(dir.path()).unwrap();
let wanted = attach(&base.join("wanted.img"));
Expand All @@ -86,7 +89,7 @@ fn a_linear_table_is_backed_by_the_path_it_names_as_root(
sectors: 8,
origin: Origin::Path(origin),
});
let device = dm.create(&name, &table, Access::ReadOnly).unwrap();
let device = dm.create(&root, &name, &table, Access::ReadOnly).unwrap();

let live = Command::new(DMSETUP)
.args(["table", &name.to_string()])
Expand Down
Loading
Loading