|
2 | 2 | // |
3 | 3 | // SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | | -use std::ffi::{CString, OsStr}; |
| 5 | +use std::ffi::OsStr; |
6 | 6 | use std::path::Path; |
7 | 7 | use std::sync::Arc; |
8 | 8 | use std::time::{Duration, SystemTime}; |
@@ -207,8 +207,8 @@ impl TdxSimulatorFs { |
207 | 207 | fn new(generator: Arc<TdxGenerator>) -> Result<Self> { |
208 | 208 | Ok(Self { |
209 | 209 | state: SimulatorState::new(generator, CCEL_FIXTURE, None)?, |
210 | | - uid: unsafe { libc::geteuid() }, |
211 | | - gid: unsafe { libc::getegid() }, |
| 210 | + uid: nix::unistd::geteuid().as_raw(), |
| 211 | + gid: nix::unistd::getegid().as_raw(), |
212 | 212 | }) |
213 | 213 | } |
214 | 214 |
|
@@ -448,25 +448,44 @@ pub(crate) fn ensure_configfs_mount(mountpoint: &Path) -> Result<()> { |
448 | 448 | } |
449 | 449 |
|
450 | 450 | if !mountpoint.is_dir() { |
451 | | - let source = CString::new("configfs")?; |
452 | | - let target = CString::new("/sys/kernel/config")?; |
453 | | - let fstype = CString::new("configfs")?; |
454 | | - let rc = unsafe { |
455 | | - libc::mount( |
456 | | - source.as_ptr(), |
457 | | - target.as_ptr(), |
458 | | - fstype.as_ptr(), |
459 | | - libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC, |
460 | | - std::ptr::null(), |
461 | | - ) |
462 | | - }; |
463 | | - if rc != 0 { |
464 | | - let error = std::io::Error::last_os_error(); |
465 | | - if error.raw_os_error() != Some(libc::EBUSY) { |
| 451 | + let flags = nix::mount::MsFlags::MS_NOSUID |
| 452 | + | nix::mount::MsFlags::MS_NODEV |
| 453 | + | nix::mount::MsFlags::MS_NOEXEC; |
| 454 | + if let Err(error) = nix::mount::mount( |
| 455 | + Some("configfs"), |
| 456 | + "/sys/kernel/config", |
| 457 | + Some("configfs"), |
| 458 | + flags, |
| 459 | + None::<&str>, |
| 460 | + ) { |
| 461 | + if error != nix::errno::Errno::EBUSY { |
466 | 462 | return Err(error).context("failed to mount configfs"); |
467 | 463 | } |
468 | 464 | } |
469 | 465 | } |
| 466 | + // configfs rejects arbitrary directories when no kernel TSM provider has |
| 467 | + // registered the `tsm` subsystem. In a no-TEE development guest the |
| 468 | + // simulator is that provider, so shadow the otherwise-empty configfs with |
| 469 | + // a private tmpfs and create the userspace ABI hierarchy there. |
| 470 | + if let Err(error) = std::fs::create_dir_all(mountpoint) { |
| 471 | + if !matches!(error.raw_os_error(), Some(libc::EPERM) | Some(libc::EACCES)) { |
| 472 | + return Err(error) |
| 473 | + .with_context(|| format!("failed to create {}", mountpoint.display())); |
| 474 | + } |
| 475 | + let flags = nix::mount::MsFlags::MS_NOSUID |
| 476 | + | nix::mount::MsFlags::MS_NODEV |
| 477 | + | nix::mount::MsFlags::MS_NOEXEC; |
| 478 | + nix::mount::mount( |
| 479 | + Some("dstack-tee-simulator"), |
| 480 | + "/sys/kernel/config", |
| 481 | + Some("tmpfs"), |
| 482 | + flags, |
| 483 | + Some("mode=0755"), |
| 484 | + ) |
| 485 | + .context("failed to mount simulator configfs shadow")?; |
| 486 | + std::fs::create_dir_all(mountpoint) |
| 487 | + .with_context(|| format!("failed to create {}", mountpoint.display()))?; |
| 488 | + } |
470 | 489 | if !mountpoint.is_dir() { |
471 | 490 | bail!( |
472 | 491 | "tsm report mountpoint is unavailable: {}", |
|
0 commit comments