Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PVF: fix detection of unshare-and-change-root security capability #2304

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion polkadot/node/core/candidate-validation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async fn run<Context>(
),
pvf_metrics,
)
.await;
.await?;
ctx.spawn_blocking("pvf-validation-host", task.boxed())?;

loop {
Expand Down
1 change: 1 addition & 0 deletions polkadot/node/core/pvf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ polkadot-core-primitives = { path = "../../../core-primitives" }
polkadot-node-core-pvf-common = { path = "common" }
polkadot-node-metrics = { path = "../../metrics" }
polkadot-node-primitives = { path = "../../primitives" }
polkadot-node-subsystem = { path = "../../subsystem" }
polkadot-primitives = { path = "../../../primitives" }

sp-core = { path = "../../../../substrate/primitives/core" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl TestHost {
execute_worker_path,
);
f(&mut config);
let (host, task) = start(config, Metrics::default()).await;
let (host, task) = start(config, Metrics::default()).await.unwrap();
let _ = handle.spawn(task);
Self { host: Mutex::new(host) }
}
Expand Down
6 changes: 3 additions & 3 deletions polkadot/node/core/pvf/common/src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ macro_rules! decl_worker_main {
std::process::exit(status)
},
"--check-can-unshare-user-namespace-and-change-root" => {
#[cfg(target_os = "linux")]
let cache_path_tempdir = std::path::Path::new(&args[2]);
#[cfg(target_os = "linux")]
let status = if let Err(err) = security::unshare_user_namespace_and_change_root(
$crate::worker::WorkerKind::CheckPivotRoot,
worker_pid,
// We're not accessing any files, so we can try to pivot_root in the temp
// dir without conflicts with other processes.
&std::env::temp_dir(),
&cache_path_tempdir,
) {
// Write the error to stderr, log it on the host-side.
eprintln!("{}", err);
Expand Down
10 changes: 7 additions & 3 deletions polkadot/node/core/pvf/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use polkadot_node_core_pvf_common::{
error::{PrepareError, PrepareResult},
pvf::PvfPrepData,
};
use polkadot_node_subsystem::SubsystemResult;
use polkadot_parachain_primitives::primitives::ValidationResult;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -203,11 +204,14 @@ impl Config {
/// The future should not return normally but if it does then that indicates an unrecoverable error.
/// In that case all pending requests will be canceled, dropping the result senders and new ones
/// will be rejected.
pub async fn start(config: Config, metrics: Metrics) -> (ValidationHost, impl Future<Output = ()>) {
pub async fn start(
config: Config,
metrics: Metrics,
) -> SubsystemResult<(ValidationHost, impl Future<Output = ()>)> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already was going to add -> SubsystemResult here in another PR soon.

gum::debug!(target: LOG_TARGET, ?config, "starting PVF validation host");

// Run checks for supported security features once per host startup. Warn here if not enabled.
let security_status = security::check_security_status(&config).await;
let security_status = security::check_security_status(&config).await?;
Copy link
Member

@eskimor eskimor Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait a second. This looks like it does not actually resolve the issue. If we can't check, we can now no longer do any validation, despite being not yet strict in enforcing security? Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now the only possible error should never happen (https://github.com/paritytech/polkadot-sdk/blob/2e2a75ff81/polkadot/node/core/pvf/src/worker_intf.rs#L189-L196). But I could have done this better. Thanks!


let (to_host_tx, to_host_rx) = mpsc::channel(10);

Expand Down Expand Up @@ -273,7 +277,7 @@ pub async fn start(config: Config, metrics: Metrics) -> (ValidationHost, impl Fu
};
};

(validation_host, task)
Ok((validation_host, task))
}

/// A mapping from an artifact ID which is in preparation state to the list of pending execution
Expand Down
18 changes: 12 additions & 6 deletions polkadot/node/core/pvf/src/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use crate::{Config, SecurityStatus, LOG_TARGET};
use crate::{worker_intf::tmppath_in, Config, SecurityStatus, LOG_TARGET};
use futures::join;
use std::{fmt, path::Path};
use std::{fmt, io, path::Path};
use tokio::{
fs::{File, OpenOptions},
io::{AsyncReadExt, AsyncSeekExt, SeekFrom},
Expand All @@ -27,14 +27,18 @@ const SECURE_MODE_ANNOUNCEMENT: &'static str =
\nMore information: https://wiki.polkadot.network/docs/maintain-guides-secure-validator#secure-validator-mode";

/// Run checks for supported security features.
pub async fn check_security_status(config: &Config) -> SecurityStatus {
let Config { prepare_worker_program_path, .. } = config;
pub async fn check_security_status(config: &Config) -> io::Result<SecurityStatus> {
let Config { prepare_worker_program_path, cache_path, .. } = config;

// TODO: add check that syslog is available and that seccomp violations are logged?
let cache_dir_tempdir = tmppath_in("check-can-unshare", &cache_path).await?;
let (landlock, seccomp, change_root) = join!(
check_landlock(prepare_worker_program_path),
check_seccomp(prepare_worker_program_path),
check_can_unshare_user_namespace_and_change_root(prepare_worker_program_path)
check_can_unshare_user_namespace_and_change_root(
prepare_worker_program_path,
&cache_dir_tempdir
)
);

let security_status = SecurityStatus {
Expand All @@ -56,7 +60,7 @@ pub async fn check_security_status(config: &Config) -> SecurityStatus {
);
}

security_status
Ok(security_status)
}

type SecureModeResult = std::result::Result<(), SecureModeError>;
Expand Down Expand Up @@ -149,11 +153,13 @@ fn print_secure_mode_message(errs: Vec<SecureModeError>) -> bool {
async fn check_can_unshare_user_namespace_and_change_root(
#[cfg_attr(not(target_os = "linux"), allow(unused_variables))]
prepare_worker_program_path: &Path,
#[cfg_attr(not(target_os = "linux"), allow(unused_variables))] cache_dir_tempdir: &Path,
) -> SecureModeResult {
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
match tokio::process::Command::new(prepare_worker_program_path)
.arg("--check-can-unshare-user-namespace-and-change-root")
.arg(cache_dir_tempdir)
.output()
.await
{
Expand Down
2 changes: 1 addition & 1 deletion polkadot/node/core/pvf/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl TestHost {
execute_worker_path,
);
f(&mut config);
let (host, task) = start(config, Metrics::default()).await;
let (host, task) = start(config, Metrics::default()).await.unwrap();
let _ = tokio::task::spawn(task);
Self { cache_dir, host: Mutex::new(host) }
}
Expand Down