rustjail: Provide useful context on device node creation errors

create_devices() within the rustjail module is responsible for creating
device nodes within the (inner) containers.  Errors that occur here will
be propagated up, but are likely to be low level failures of mknod() - e.g.
ENOENT or EACCESS - which won't be very useful without context when
reported all the way up to the runtime without the context of what we were
trying to do.

Add some anyhow context information giving the details of the device we
were trying to create when it failed.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
David Gibson 2021-10-13 15:04:57 +11:00
parent 42b92b2b05
commit 2680c0bfee

View File

@ -832,14 +832,14 @@ fn create_devices(devices: &[LinuxDevice], bind: bool) -> Result<()> {
let op: fn(&LinuxDevice) -> Result<()> = if bind { bind_dev } else { mknod_dev }; let op: fn(&LinuxDevice) -> Result<()> = if bind { bind_dev } else { mknod_dev };
let old = stat::umask(Mode::from_bits_truncate(0o000)); let old = stat::umask(Mode::from_bits_truncate(0o000));
for dev in DEFAULT_DEVICES.iter() { for dev in DEFAULT_DEVICES.iter() {
op(dev)?; op(dev).context(format!("Creating container device {:?}", dev))?;
} }
for dev in devices { for dev in devices {
if !dev.path.starts_with("/dev") || dev.path.contains("..") { if !dev.path.starts_with("/dev") || dev.path.contains("..") {
let msg = format!("{} is not a valid device path", dev.path); let msg = format!("{} is not a valid device path", dev.path);
bail!(anyhow!(msg)); bail!(anyhow!(msg));
} }
op(dev)?; op(dev).context(format!("Creating container device {:?}", dev))?;
} }
stat::umask(old); stat::umask(old);
Ok(()) Ok(())