Implement bounded Windows Event Log jobs and verify contracts on cloud Linux
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -7,7 +7,7 @@ use rmm_agent_core::{
|
||||
jobs, ledger,
|
||||
reconcile::{self, SyncClock},
|
||||
};
|
||||
use rmm_agent_windows::{CAPABILITIES, journal::Journal, native};
|
||||
use rmm_agent_windows::{CAPABILITIES, job_binding::readonly_binding, journal::Journal, native};
|
||||
use rmm_domain::jobs::{Action, ExecutionPermitPayload};
|
||||
use rmm_protocol::*;
|
||||
use rusqlite::{Connection, OptionalExtension};
|
||||
@@ -216,104 +216,6 @@ fn verified_window(
|
||||
binding.verify(request, now + chrono::Duration::seconds(2))?;
|
||||
Ok(result)
|
||||
}
|
||||
fn readonly_binding(path: &Path) -> Result<Binding> {
|
||||
use std::ptr;
|
||||
use windows_sys::Win32::{
|
||||
Foundation::LocalFree,
|
||||
Security::{
|
||||
self, ACCESS_ALLOWED_ACE, ACL,
|
||||
Authorization::{ConvertSidToStringSidW, GetNamedSecurityInfoW, SE_FILE_OBJECT},
|
||||
DACL_SECURITY_INFORMATION, OWNER_SECURITY_INFORMATION,
|
||||
},
|
||||
};
|
||||
unsafe fn sid(sid: Security::PSID) -> Result<String> {
|
||||
let mut raw = ptr::null_mut();
|
||||
ensure!(
|
||||
unsafe { ConvertSidToStringSidW(sid, &mut raw) } != 0,
|
||||
"BINDING_SID"
|
||||
);
|
||||
let mut len = 0;
|
||||
while unsafe { *raw.add(len) } != 0 {
|
||||
len += 1;
|
||||
ensure!(len < 256, "BINDING_SID_LIMIT");
|
||||
}
|
||||
let value = String::from_utf16(unsafe { std::slice::from_raw_parts(raw, len) })?;
|
||||
unsafe {
|
||||
LocalFree(raw.cast());
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
ensure!(path.is_absolute(), "ABSOLUTE_JOB_BINDING_REQUIRED");
|
||||
// The immediate configuration directory must also deny agent replacement.
|
||||
for (item, directory) in [
|
||||
(path, false),
|
||||
(path.parent().context("BINDING_PARENT")?, true),
|
||||
] {
|
||||
native::private_path(item, directory)?;
|
||||
let mut owner = ptr::null_mut();
|
||||
let mut dacl: *mut ACL = ptr::null_mut();
|
||||
let mut descriptor = ptr::null_mut();
|
||||
let status = unsafe {
|
||||
GetNamedSecurityInfoW(
|
||||
native::wide(item).as_ptr(),
|
||||
SE_FILE_OBJECT,
|
||||
OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
|
||||
&mut owner,
|
||||
ptr::null_mut(),
|
||||
&mut dacl,
|
||||
ptr::null_mut(),
|
||||
&mut descriptor,
|
||||
)
|
||||
};
|
||||
ensure!(status == 0, "BINDING_SECURITY");
|
||||
let check = (|| -> Result<()> {
|
||||
ensure!(!owner.is_null() && !dacl.is_null(), "BINDING_NULL_SECURITY");
|
||||
ensure!(
|
||||
matches!(unsafe { sid(owner) }?.as_str(), "S-1-5-18" | "S-1-5-32-544"),
|
||||
"BINDING_ADMIN_OWNER_REQUIRED"
|
||||
);
|
||||
for index in 0..unsafe { (*dacl).AceCount } {
|
||||
let mut raw = ptr::null_mut();
|
||||
ensure!(
|
||||
unsafe { Security::GetAce(dacl, u32::from(index), &mut raw) } != 0,
|
||||
"BINDING_ACE"
|
||||
);
|
||||
let ace = unsafe { &*raw.cast::<ACCESS_ALLOWED_ACE>() };
|
||||
ensure!(
|
||||
ace.Header.AceType == 0
|
||||
&& usize::from(ace.Header.AceSize)
|
||||
>= std::mem::size_of::<ACCESS_ALLOWED_ACE>(),
|
||||
"BINDING_ACE_TYPE"
|
||||
);
|
||||
let who = unsafe { sid(ptr::addr_of!(ace.SidStart).cast_mut().cast()) }?;
|
||||
if !matches!(who.as_str(), "S-1-5-18" | "S-1-5-32-544") {
|
||||
// Generic write/all, owner/DACL/delete and all file/directory mutation bits.
|
||||
ensure!(ace.Mask & 0x500d_0156 == 0, "BINDING_AGENT_WRITE_FORBIDDEN");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
unsafe {
|
||||
LocalFree(descriptor.cast());
|
||||
}
|
||||
check?;
|
||||
}
|
||||
let binding: Binding = serde_json::from_slice(&protected_read(path, 8192)?)?;
|
||||
ensure!(
|
||||
!binding.tenant_id.is_nil()
|
||||
&& !binding.agent_id.is_nil()
|
||||
&& !binding.asset_id.is_nil()
|
||||
&& binding.enrollment_generation > 0
|
||||
&& binding.assignment_version > 0
|
||||
&& binding.public_key_hex.len() == 64
|
||||
&& binding
|
||||
.public_key_hex
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_hexdigit()),
|
||||
"JOB_BINDING_PROFILE"
|
||||
);
|
||||
Ok(binding)
|
||||
}
|
||||
fn import_legacy_inventory(journal: &mut Journal) -> Result<()> {
|
||||
let original = {
|
||||
let mut q = journal
|
||||
@@ -1164,6 +1066,7 @@ impl Agent {
|
||||
let directory = self.config.state_directory.join("jobs");
|
||||
let work_offer = offer.clone();
|
||||
let work_grant = grant.clone();
|
||||
let original_boot = Uuid::parse_str(&self.boot)?;
|
||||
let worker = tokio::task::spawn_blocking(move || {
|
||||
let outcome = (|| -> Result<Outcome> {
|
||||
if !fence.allows(generation, &clock)
|
||||
@@ -1174,17 +1077,24 @@ impl Agent {
|
||||
"START_WINDOW_OR_CLOCK_INVALID_BEFORE_EFFECT",
|
||||
));
|
||||
}
|
||||
let script = if matches!(job.action, Action::RunScript { .. }) {
|
||||
match rmm_agent_windows::actions::Script::prepare(
|
||||
&job,
|
||||
&directory,
|
||||
work_grant.grant_id,
|
||||
) {
|
||||
Ok(script) => Some(script),
|
||||
Err(_) => return Ok(jobs::no_start("failed", "SCRIPT_PREPARATION_FAILED")),
|
||||
let prepared = match rmm_agent_windows::actions::PreparedAction::prepare(
|
||||
&job,
|
||||
&directory,
|
||||
work_grant.grant_id,
|
||||
&request,
|
||||
original_boot,
|
||||
) {
|
||||
Ok(prepared) => prepared,
|
||||
Err(_) => {
|
||||
return Ok(jobs::no_start(
|
||||
"failed",
|
||||
if matches!(job.action, Action::RunScript { .. }) {
|
||||
"SCRIPT_PREPARATION_FAILED"
|
||||
} else {
|
||||
"ACTION_PREPARATION_FAILED"
|
||||
},
|
||||
));
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// File creation, pool scheduling and fsync never extend a start window.
|
||||
if worker_cancel.load(Ordering::SeqCst) {
|
||||
@@ -1198,11 +1108,7 @@ impl Agent {
|
||||
"START_WINDOW_OR_CLOCK_INVALID_BEFORE_EFFECT",
|
||||
));
|
||||
}
|
||||
if let Some(script) = script {
|
||||
script.run(&job, &worker_cancel)
|
||||
} else {
|
||||
rmm_agent_windows::actions::inventory(&job)
|
||||
}
|
||||
prepared.run(&job, &worker_cancel)
|
||||
})()
|
||||
.unwrap_or_else(|_| Outcome::unknown("WINDOWS_EXECUTOR_EFFECT_UNKNOWN"));
|
||||
let _ = completed.blocking_send((work_offer, work_grant, outcome));
|
||||
@@ -1549,6 +1455,10 @@ fn service_run(stop: &'static AtomicBool) -> Result<()> {
|
||||
}
|
||||
pub fn main() -> Result<()> {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
if args.first().map(String::as_str) == Some(rmm_agent_windows::event_log_job::MODE) {
|
||||
ensure!(args.len() == 2, "EVENT_LOG_WORKER_ARGUMENTS");
|
||||
return rmm_agent_windows::event_log_job::worker(Path::new(&args[1]));
|
||||
}
|
||||
if args == ["--help"] {
|
||||
println!(
|
||||
"Development Windows service. --service --config ABSOLUTE_PATH. Jobs disabled by default; require qualified clock, provisioned signing binding, signed Sync and WebUI-authorized grant. No approval bypass."
|
||||
|
||||
Reference in New Issue
Block a user