sandlock-core 0.7.0

Lightweight process sandbox using Landlock, seccomp-bpf, and seccomp user notification
Documentation
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
use sandlock_core::{Policy, Sandbox};
use std::io::{BufRead, BufReader, Read as _, Write as _};
use std::net::{TcpListener, TcpStream};
use std::path::PathBuf;
use std::thread;

fn temp_file(name: &str) -> PathBuf {
    std::env::temp_dir().join(format!(
        "sandlock-test-http-{}-{}",
        name,
        std::process::id()
    ))
}

fn base_policy() -> sandlock_core::PolicyBuilder {
    Policy::builder()
        .fs_read("/usr")
        .fs_read("/lib")
        .fs_read_if_exists("/lib64")
        .fs_read("/bin")
        .fs_read("/etc")
        .fs_read("/proc")
        .fs_read("/dev")
        .fs_read("/tmp")
        .fs_write("/tmp")
}

/// Spawn a minimal HTTP server on 127.0.0.1:0 that accepts `n` requests.
/// Returns (port, join_handle). The server responds 200 with body "ok" to
/// every request regardless of method/path — ACL enforcement happens in
/// the proxy, not the origin server.
fn spawn_http_server(n: usize) -> (u16, thread::JoinHandle<()>) {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let port = listener.local_addr().unwrap().port();
    let handle = thread::spawn(move || {
        for _ in 0..n {
            if let Ok(mut stream) = listener.accept().map(|(s, _)| s) {
                handle_http_conn(&mut stream);
            }
        }
    });
    (port, handle)
}

/// Spawn a minimal HTTP server on [::1]:0 (IPv6 loopback).
fn spawn_http_server_v6(n: usize) -> (u16, thread::JoinHandle<()>) {
    let listener = TcpListener::bind("[::1]:0").unwrap();
    let port = listener.local_addr().unwrap().port();
    let handle = thread::spawn(move || {
        for _ in 0..n {
            if let Ok(mut stream) = listener.accept().map(|(s, _)| s) {
                handle_http_conn(&mut stream);
            }
        }
    });
    (port, handle)
}

/// Read one HTTP request and write a 200 OK response.
fn handle_http_conn(stream: &mut TcpStream) {
    let mut reader = BufReader::new(stream.try_clone().unwrap());
    // Read request line + headers until blank line.
    let mut content_length = 0usize;
    loop {
        let mut line = String::new();
        if reader.read_line(&mut line).unwrap_or(0) == 0 {
            break;
        }
        if line.to_lowercase().starts_with("content-length:") {
            content_length = line.split(':').nth(1)
                .and_then(|v| v.trim().parse().ok())
                .unwrap_or(0);
        }
        if line == "\r\n" || line == "\n" {
            break;
        }
    }
    // Drain request body if any.
    if content_length > 0 {
        let mut body = vec![0u8; content_length];
        let _ = reader.read_exact(&mut body);
    }
    let response = "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok";
    let _ = stream.write_all(response.as_bytes());
    let _ = stream.flush();
}

fn http_script(url: &str, out: &std::path::Path) -> String {
    format!(
        concat!(
            "import urllib.request, urllib.error\n",
            "try:\n",
            "    resp = urllib.request.urlopen('{url}')\n",
            "    open('{out}', 'w').write('OK:' + str(resp.status))\n",
            "except urllib.error.HTTPError as e:\n",
            "    open('{out}', 'w').write('HTTP:' + str(e.code))\n",
            "except Exception as e:\n",
            "    open('{out}', 'w').write('ERR:' + str(e))\n",
        ),
        url = url,
        out = out.display(),
    )
}

fn post_script(url: &str, out: &std::path::Path) -> String {
    format!(
        concat!(
            "import urllib.request, urllib.error\n",
            "try:\n",
            "    req = urllib.request.Request('{url}', method='POST', data=b'test')\n",
            "    resp = urllib.request.urlopen(req)\n",
            "    open('{out}', 'w').write('OK:' + str(resp.status))\n",
            "except urllib.error.HTTPError as e:\n",
            "    open('{out}', 'w').write('HTTP:' + str(e.code))\n",
            "except Exception as e:\n",
            "    open('{out}', 'w').write('ERR:' + str(e))\n",
        ),
        url = url,
        out = out.display(),
    )
}

// ============================================================
// Tests using local HTTP server — no external network required
// ============================================================

/// Allowed GET request passes through the ACL proxy to local server.
#[tokio::test]
async fn test_http_allow_get() {
    let out = temp_file("allow-get");
    let (port, srv) = spawn_http_server(1);

    let policy = base_policy()
        .http_allow(&format!("GET 127.0.0.1/*"))
        .http_port(port)
        .build()
        .unwrap();

    let script = http_script(&format!("http://127.0.0.1:{}/get", port), &out);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success(), "exit={:?}", result.code());
    let content = std::fs::read_to_string(&out).unwrap_or_default();
    assert!(content.starts_with("OK:200"), "expected OK:200, got: {}", content);

    srv.join().unwrap();
    let _ = std::fs::remove_file(&out);
}

/// GET to a non-matching path should be blocked (403) by the proxy.
#[tokio::test]
async fn test_http_deny_non_matching() {
    let out = temp_file("deny-nonmatch");
    // Server won't receive a connection (blocked by proxy), so don't wait.
    let (port, _srv) = spawn_http_server(1);

    let policy = base_policy()
        .http_allow(&format!("GET 127.0.0.1/allowed"))
        .http_port(port)
        .build()
        .unwrap();

    let script = http_script(&format!("http://127.0.0.1:{}/denied", port), &out);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success(), "exit={:?}", result.code());
    let content = std::fs::read_to_string(&out).unwrap_or_default();
    assert!(content.starts_with("HTTP:403"), "expected HTTP:403, got: {}", content);

    let _ = std::fs::remove_file(&out);
}

/// Deny rules take precedence over allow rules.
#[tokio::test]
async fn test_http_deny_precedence() {
    let out_allowed = temp_file("deny-prec-allowed");
    let out_denied = temp_file("deny-prec-denied");
    let (port, srv) = spawn_http_server(1); // only 1 request gets through

    let policy = base_policy()
        .http_allow(&format!("* 127.0.0.1/*"))
        .http_deny(&format!("* 127.0.0.1/secret"))
        .http_port(port)
        .build()
        .unwrap();

    // GET /public — should succeed
    let script = http_script(&format!("http://127.0.0.1:{}/public", port), &out_allowed);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success());
    let content = std::fs::read_to_string(&out_allowed).unwrap_or_default();
    assert!(content.starts_with("OK:200"), "expected OK:200 for /public, got: {}", content);

    // GET /secret — should be denied
    let script = http_script(&format!("http://127.0.0.1:{}/secret", port), &out_denied);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success());
    let content = std::fs::read_to_string(&out_denied).unwrap_or_default();
    assert!(content.starts_with("HTTP:403"), "expected HTTP:403 for /secret, got: {}", content);

    srv.join().unwrap();
    let _ = std::fs::remove_file(&out_allowed);
    let _ = std::fs::remove_file(&out_denied);
}

/// Without any HTTP ACL rules, traffic passes through normally
/// (provided the port is in net_connect).
#[tokio::test]
async fn test_http_no_acl_unrestricted() {
    let out = temp_file("no-acl");
    let (port, srv) = spawn_http_server(1);

    let policy = base_policy().net_connect_port(port).build().unwrap();

    let script = http_script(&format!("http://127.0.0.1:{}/get", port), &out);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success(), "exit={:?}", result.code());
    let content = std::fs::read_to_string(&out).unwrap_or_default();
    assert!(content.starts_with("OK:200"), "expected OK:200 (unrestricted), got: {}", content);

    srv.join().unwrap();
    let _ = std::fs::remove_file(&out);
}

/// Allow GET but not POST to the same endpoint — verifies method-level ACL.
#[tokio::test]
async fn test_http_method_filtering() {
    let out_get = temp_file("method-get");
    let out_post = temp_file("method-post");
    let (port, srv) = spawn_http_server(1); // only GET goes through

    let policy = base_policy()
        .http_allow(&format!("GET 127.0.0.1/anything"))
        .http_port(port)
        .build()
        .unwrap();

    // GET should succeed
    let script = http_script(&format!("http://127.0.0.1:{}/anything", port), &out_get);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success());
    let content = std::fs::read_to_string(&out_get).unwrap_or_default();
    assert!(content.starts_with("OK:200"), "expected OK:200 for GET, got: {}", content);

    // POST should be denied
    let script = post_script(&format!("http://127.0.0.1:{}/anything", port), &out_post);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success());
    let content = std::fs::read_to_string(&out_post).unwrap_or_default();
    assert!(content.starts_with("HTTP:403"), "expected HTTP:403 for POST, got: {}", content);

    srv.join().unwrap();
    let _ = std::fs::remove_file(&out_get);
    let _ = std::fs::remove_file(&out_post);
}

/// Multiple allow rules — only matching ones pass.
#[tokio::test]
async fn test_http_multiple_allow_rules() {
    let out_get = temp_file("multi-get");
    let out_other = temp_file("multi-other");
    let (port, srv) = spawn_http_server(1);

    let policy = base_policy()
        .http_allow(&format!("GET 127.0.0.1/get"))
        .http_allow(&format!("POST 127.0.0.1/post"))
        .http_port(port)
        .build()
        .unwrap();

    // GET /get — should succeed (matches first rule)
    let script = http_script(&format!("http://127.0.0.1:{}/get", port), &out_get);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success());
    let content = std::fs::read_to_string(&out_get).unwrap_or_default();
    assert!(content.starts_with("OK:200"), "expected OK:200 for /get, got: {}", content);

    // GET /anything — should be denied (not in allow list)
    let script = http_script(&format!("http://127.0.0.1:{}/anything", port), &out_other);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success());
    let content = std::fs::read_to_string(&out_other).unwrap_or_default();
    assert!(content.starts_with("HTTP:403"), "expected HTTP:403 for /anything, got: {}", content);

    srv.join().unwrap();
    let _ = std::fs::remove_file(&out_get);
    let _ = std::fs::remove_file(&out_other);
}

/// Wildcard host allow with a specific deny — deny takes precedence.
#[tokio::test]
async fn test_http_wildcard_host() {
    let out_get = temp_file("wildcard-get");
    let out_denied = temp_file("wildcard-denied");
    let (port, srv) = spawn_http_server(1);

    let policy = base_policy()
        .http_allow(&format!("* 127.0.0.1/*"))
        .http_deny("* */admin/*")
        .http_port(port)
        .build()
        .unwrap();

    // GET /get — should succeed
    let script = http_script(&format!("http://127.0.0.1:{}/get", port), &out_get);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success());
    let content = std::fs::read_to_string(&out_get).unwrap_or_default();
    assert!(content.starts_with("OK:200"), "expected OK:200 for /get, got: {}", content);

    // GET /admin/settings — should be denied
    let script = http_script(&format!("http://127.0.0.1:{}/admin/settings", port), &out_denied);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success());
    let content = std::fs::read_to_string(&out_denied).unwrap_or_default();
    assert!(content.starts_with("HTTP:403"), "expected HTTP:403 for /admin/settings, got: {}", content);

    srv.join().unwrap();
    let _ = std::fs::remove_file(&out_get);
    let _ = std::fs::remove_file(&out_denied);
}

/// Non-intercepted port traffic should NOT go through the proxy.
#[tokio::test]
async fn test_http_non_intercepted_port() {
    let out = temp_file("non-intercept");

    // ACL intercepts port 80 by default, not random ports
    let policy = base_policy()
        .http_allow("GET example.com/get")
        .build()
        .unwrap();

    let script = format!(
        concat!(
            "import socket, threading\n",
            "try:\n",
            "    srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
            "    srv.bind(('127.0.0.1', 0))\n",
            "    port = srv.getsockname()[1]\n",
            "    srv.listen(1)\n",
            "    def accept_one():\n",
            "        conn, _ = srv.accept()\n",
            "        conn.send(b'HELLO')\n",
            "        conn.close()\n",
            "    t = threading.Thread(target=accept_one, daemon=True)\n",
            "    t.start()\n",
            "    c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
            "    c.settimeout(2)\n",
            "    c.connect(('127.0.0.1', port))\n",
            "    data = c.recv(10)\n",
            "    c.close()\n",
            "    srv.close()\n",
            "    open('{out}', 'w').write('OK:' + data.decode())\n",
            "except Exception as e:\n",
            "    open('{out}', 'w').write('ERR:' + str(e))\n",
        ),
        out = out.display(),
    );

    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success(), "exit={:?}", result.code());
    let content = std::fs::read_to_string(&out).unwrap_or_default();
    assert!(content.starts_with("OK:HELLO"), "expected OK:HELLO, got: {}", content);

    let _ = std::fs::remove_file(&out);
}

// ============================================================
// IPv6 tests
// ============================================================

/// IPv6 loopback: allowed GET via [::1] passes through the ACL proxy.
#[tokio::test]
async fn test_http_acl_ipv6_allow() {
    let out = temp_file("ipv6-allow");
    let (port, srv) = spawn_http_server_v6(1);

    let policy = base_policy()
        .http_allow("GET */*")
        .http_port(port)
        .build()
        .unwrap();

    let script = http_script(&format!("http://[::1]:{}/get", port), &out);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success(), "exit={:?}", result.code());
    let content = std::fs::read_to_string(&out).unwrap_or_default();
    assert!(content.starts_with("OK:200"), "expected OK:200 for IPv6 allow, got: {}", content);

    srv.join().unwrap();
    let _ = std::fs::remove_file(&out);
}

/// IPv6 loopback: non-matching path denied by ACL proxy.
#[tokio::test]
async fn test_http_acl_ipv6_deny() {
    let out = temp_file("ipv6-deny");
    let (port, _srv) = spawn_http_server_v6(1);

    let policy = base_policy()
        .http_allow("GET */allowed")
        .http_port(port)
        .build()
        .unwrap();

    let script = http_script(&format!("http://[::1]:{}/denied", port), &out);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success(), "exit={:?}", result.code());
    let content = std::fs::read_to_string(&out).unwrap_or_default();
    assert!(content.starts_with("HTTP:403"), "expected HTTP:403 for IPv6 deny, got: {}", content);

    let _ = std::fs::remove_file(&out);
}

/// IPv6 non-intercepted port should pass through without proxy interference.
#[tokio::test]
async fn test_http_ipv6_non_intercepted_port() {
    let out = temp_file("ipv6-non-intercept");

    let policy = base_policy()
        .http_allow("GET example.com/get")
        .build()
        .unwrap();

    let script = format!(
        concat!(
            "import socket, threading\n",
            "try:\n",
            "    srv = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)\n",
            "    srv.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)\n",
            "    srv.bind(('::1', 0))\n",
            "    port = srv.getsockname()[1]\n",
            "    srv.listen(1)\n",
            "    def accept_one():\n",
            "        conn, _ = srv.accept()\n",
            "        conn.send(b'HELLO6')\n",
            "        conn.close()\n",
            "    t = threading.Thread(target=accept_one, daemon=True)\n",
            "    t.start()\n",
            "    c = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)\n",
            "    c.settimeout(2)\n",
            "    c.connect(('::1', port))\n",
            "    data = c.recv(10)\n",
            "    c.close()\n",
            "    srv.close()\n",
            "    open('{out}', 'w').write('OK:' + data.decode())\n",
            "except Exception as e:\n",
            "    open('{out}', 'w').write('ERR:' + str(e))\n",
        ),
        out = out.display(),
    );

    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success(), "exit={:?}", result.code());
    let content = std::fs::read_to_string(&out).unwrap_or_default();
    assert!(content.starts_with("OK:HELLO6"), "expected OK:HELLO6, got: {}", content);

    let _ = std::fs::remove_file(&out);
}

/// IPv6 method filtering: allow GET but deny POST via [::1].
#[tokio::test]
async fn test_http_acl_ipv6_method_filtering() {
    let out_get = temp_file("ipv6-method-get");
    let out_post = temp_file("ipv6-method-post");
    let (port, srv) = spawn_http_server_v6(1); // only GET goes through

    let policy = base_policy()
        .http_allow("GET */*")
        .http_port(port)
        .build()
        .unwrap();

    // GET should succeed
    let script = http_script(&format!("http://[::1]:{}/anything", port), &out_get);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success());
    let content = std::fs::read_to_string(&out_get).unwrap_or_default();
    assert!(content.starts_with("OK:200"), "expected OK:200 for IPv6 GET, got: {}", content);

    // POST should be denied
    let script = post_script(&format!("http://[::1]:{}/anything", port), &out_post);
    let result = Sandbox::run_interactive(&policy, &["python3", "-c", &script])
        .await
        .unwrap();
    assert!(result.success());
    let content = std::fs::read_to_string(&out_post).unwrap_or_default();
    assert!(content.starts_with("HTTP:403"), "expected HTTP:403 for IPv6 POST, got: {}", content);

    srv.join().unwrap();
    let _ = std::fs::remove_file(&out_get);
    let _ = std::fs::remove_file(&out_post);
}