1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! Process backend — OS process with seccomp, Landlock, namespaces, cgroups.
pub mod cgroups;
pub mod landlock_enforce;
pub mod namespaces;
pub mod seccomp;
use crate::backend::{Backend, SandboxBackend};
use crate::lifecycle::{ExecResult, SandboxConfig};
use crate::policy::SandboxPolicy;
/// Process-based sandbox backend.
#[derive(Debug)]
pub struct ProcessBackend {
_config: SandboxConfig,
}
impl ProcessBackend {
/// Create a new process backend from configuration.
pub fn new(config: &SandboxConfig) -> crate::Result<Self> {
Ok(Self {
_config: config.clone(),
})
}
}
#[async_trait::async_trait]
impl SandboxBackend for ProcessBackend {
fn backend_type(&self) -> Backend {
Backend::Process
}
async fn exec(&self, command: &str, policy: &SandboxPolicy) -> crate::Result<ExecResult> {
// Parse command into program + args
let parts = shell_words(command);
if parts.is_empty() {
return Err(crate::KavachError::ExecFailed("empty command".into()));
}
let program = &parts[0];
let args = &parts[1..];
let mut cmd = tokio::process::Command::new(program);
cmd.args(args)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped());
// Apply environment from config
for (k, v) in &self._config.env {
cmd.env(k, v);
}
// Apply working directory
if let Some(ref workdir) = self._config.workdir {
cmd.current_dir(workdir);
}
// ── Pre-exec isolation (Linux only) ─────────────────────────────
#[cfg(target_os = "linux")]
{
use crate::backend::capabilities;
let caps = capabilities::detect_capabilities();
// Pre-build seccomp BPF program (before fork, can allocate freely)
let seccomp_program = if policy.seccomp_enabled && caps.seccomp_available {
let profile = policy.seccomp_profile.as_deref().unwrap_or("basic");
match seccomp::build_filter(profile) {
Ok(p) => Some(p),
Err(e) => {
tracing::warn!("seccomp filter build failed, skipping: {e}");
None
}
}
} else {
None
};
// Derive namespace config — only apply if namespaces are available
let ns_config = if caps.namespaces_available {
Some(namespaces::NamespaceConfig::from_policy(policy))
} else {
None
};
// Only apply landlock if kernel supports it
let apply_ll = caps.landlock_available && landlock_enforce::should_apply(policy);
// Clone policy for pre_exec closure
let policy_clone = policy.clone();
// SAFETY: `CommandExt::pre_exec` requires unsafe because the closure
// runs in the child process between fork() and exec(), where only
// async-signal-safe operations are permitted (no heap allocation,
// no mutex acquisition, no stdio beyond write()).
//
// This closure satisfies those requirements:
// 1. The BPF program is pre-compiled above (before fork) — no
// allocation happens inside the closure.
// 2. All operations are direct kernel syscalls via FFI:
// - unshare(2) for namespace isolation
// - landlock_create_ruleset(2) / landlock_restrict_self(2)
// - prctl(2) for capability dropping
// - setrlimit(2) for resource limits
// - seccomp(2) / prctl(PR_SET_SECCOMP) for BPF filter
// 3. Error paths use eprintln! (write to fd 2) which is
// async-signal-safe, or return Err (no cleanup needed).
// 4. No heap-allocated data is created inside the closure —
// all captured values (ns_config, policy_clone, seccomp_program,
// apply_ll) are moved in and only read.
// 5. Ordering is critical and documented inline: namespaces first
// (needs unshare), then landlock (needs landlock_* syscalls),
// then caps (needs capset), then seccomp last (would block
// all preceding syscalls).
unsafe {
cmd.pre_exec(move || {
// Order matters: each step needs syscalls the next would block.
// 1. Namespaces (needs unshare syscall) — best-effort
if let Some(ref ns) = ns_config
&& ns.any_enabled()
&& let Err(e) = namespaces::apply_namespaces(ns)
{
eprintln!("kavach: namespace isolation skipped: {e}");
}
// 2. Landlock (needs landlock_* syscalls) — best-effort
if apply_ll && let Err(e) = landlock_enforce::apply_landlock(&policy_clone) {
eprintln!("kavach: landlock skipped: {e}");
}
// 3. Drop capabilities (needs capset syscall) — best-effort
let _ = namespaces::drop_capabilities();
// 4. Apply resource limits via rlimits — best-effort
let _ = cgroups::apply_rlimits(&policy_clone);
// 5. Seccomp filter (MUST BE LAST — blocks future syscalls)
if let Some(ref program) = seccomp_program {
seccomp::apply_filter(program)
.map_err(|e| std::io::Error::other(e.to_string()))?;
}
Ok(())
});
}
}
crate::backend::exec_util::execute_with_timeout(
&mut cmd,
self._config.timeout_ms,
"process",
)
.await
}
async fn health_check(&self) -> crate::Result<bool> {
Ok(true)
}
async fn destroy(&self) -> crate::Result<()> {
Ok(())
}
}
/// Simple whitespace-based command splitting (no shell expansion).
fn shell_words(input: &str) -> Vec<String> {
let mut words = Vec::with_capacity(8);
let mut current = String::new();
let mut in_single = false;
let mut in_double = false;
let mut escape = false;
for ch in input.chars() {
if escape {
current.push(ch);
escape = false;
continue;
}
match ch {
'\\' if !in_single => escape = true,
'\'' if !in_double => in_single = !in_single,
'"' if !in_single => in_double = !in_double,
' ' | '\t' if !in_single && !in_double => {
if !current.is_empty() {
words.push(std::mem::take(&mut current));
}
}
_ => current.push(ch),
}
}
if !current.is_empty() {
words.push(current);
}
words
}
#[cfg(test)]
mod tests {
use super::*;
use crate::backend::Backend;
#[test]
fn shell_words_basic() {
assert_eq!(shell_words("echo hello"), vec!["echo", "hello"]);
assert_eq!(shell_words("ls -la /tmp"), vec!["ls", "-la", "/tmp"]);
}
#[test]
fn shell_words_quoted() {
assert_eq!(
shell_words(r#"echo "hello world""#),
vec!["echo", "hello world"]
);
assert_eq!(
shell_words("echo 'hello world'"),
vec!["echo", "hello world"]
);
}
#[test]
fn shell_words_empty() {
assert!(shell_words("").is_empty());
assert!(shell_words(" ").is_empty());
}
#[tokio::test]
async fn exec_echo() {
let config = SandboxConfig::builder().backend(Backend::Process).build();
let backend = ProcessBackend::new(&config).unwrap();
let policy = SandboxPolicy::minimal();
let result = backend.exec("echo hello", &policy).await.unwrap();
assert_eq!(result.exit_code, 0);
assert_eq!(result.stdout.trim(), "hello");
assert!(!result.timed_out);
}
#[tokio::test]
async fn exec_false_returns_nonzero() {
let config = SandboxConfig::builder().backend(Backend::Process).build();
let backend = ProcessBackend::new(&config).unwrap();
let policy = SandboxPolicy::minimal();
let result = backend.exec("false", &policy).await.unwrap();
assert_ne!(result.exit_code, 0);
}
#[tokio::test]
async fn exec_timeout() {
let config = SandboxConfig::builder()
.backend(Backend::Process)
.timeout_ms(100)
.build();
let backend = ProcessBackend::new(&config).unwrap();
let policy = SandboxPolicy::minimal();
let result = backend.exec("sleep 10", &policy).await.unwrap();
assert!(result.timed_out);
assert_eq!(result.exit_code, -1);
}
#[tokio::test]
async fn exec_empty_command() {
let config = SandboxConfig::builder().backend(Backend::Process).build();
let backend = ProcessBackend::new(&config).unwrap();
let policy = SandboxPolicy::minimal();
let result = backend.exec("", &policy).await;
assert!(result.is_err());
}
}