Skip to content

Commit aa64a7d

Browse files
committed
test(network): kept policy DNS answers give no access the current policy denies
A kept answer is refused by the policy decision once its host leaves the policy or the policy is quarantined. One name allowed for two binaries with different destination rules pins both resolved addresses, and each connection reaches only the addresses its own endpoint permits, driven through a real policy DNS answer. The record pins the union of what the eligible endpoints permit, in resolver order. Signed-off-by: mr-miles <miles.waller@gmail.com>
1 parent d752596 commit aa64a7d

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

‎crates/openshell-supervisor-network/src/policy_dns/mod.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,4 +2022,36 @@ process: { run_as_user: sandbox, run_as_group: sandbox }
20222022
service.policy.reload_from_proto(&widened).unwrap();
20232023
assert_eq!(lookup().await.unwrap().address, before.address, "new port");
20242024
}
2025+
2026+
/// The record pins every answer some eligible endpoint permits, in
2027+
/// resolver order, and nothing else: here the exact host permits the
2028+
/// private address, the wildcard the public one, and neither loopback.
2029+
#[tokio::test]
2030+
async fn mapping_pins_the_union_of_what_eligible_endpoints_permit() {
2031+
let policy = BASE_POLICY.replace("db.example", "db.example.com").replace(
2032+
"filesystem_policy:",
2033+
" web:\n name: web\n endpoints:\n - { host: '*.example.com', port: 443 }\n binaries: [{ path: /usr/bin/curl }]\nfilesystem_policy:",
2034+
);
2035+
let service = service(
2036+
&policy,
2037+
vec![
2038+
"8.8.8.8".parse().unwrap(),
2039+
"127.0.0.1".parse().unwrap(),
2040+
"10.2.3.4".parse().unwrap(),
2041+
],
2042+
);
2043+
let now = Instant::now();
2044+
let answer = service
2045+
.answer_query("db.example.com", AddressFamily::Ipv4, now)
2046+
.await
2047+
.unwrap();
2048+
let mapping = service.store.lookup(answer.address, 443, now).unwrap();
2049+
assert_eq!(
2050+
mapping.record.pinned_addresses,
2051+
[
2052+
"8.8.8.8".parse::<IpAddr>().unwrap(),
2053+
"10.2.3.4".parse().unwrap()
2054+
]
2055+
);
2056+
}
20252057
}

‎crates/openshell-supervisor-network/src/proxy.rs‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9725,6 +9725,107 @@ process: { run_as_user: sandbox, run_as_group: sandbox }
97259725
);
97269726
}
97279727

9728+
/// A kept answer gives no access the current policy does not: once its
9729+
/// host leaves the policy, or the policy is quarantined, it is refused by
9730+
/// the policy decision.
9731+
#[tokio::test]
9732+
async fn policy_dns_answer_is_refused_once_the_policy_no_longer_allows_it() {
9733+
let rego = include_str!("../data/sandbox-policy.rego");
9734+
let engine = Arc::new(native_identity_engine(rego, POLICY_DNS_OPEN_POLICY).unwrap());
9735+
let store = policy_dns_test_store();
9736+
let without_pypi = POLICY_DNS_OPEN_POLICY.replace(
9737+
" - { host: pypi.org, port: 80 }",
9738+
" - { host: files.pythonhosted.org, port: 80 }",
9739+
);
9740+
let kept = keep_pypi_answer_across(&engine, &store, &without_pypi).await;
9741+
assert_eq!(
9742+
admit_staged(&store, &engine, SocketAddr::new(kept, 80)).await,
9743+
(false, TcpOpenDecision::Denied(TcpOpenDenial::PolicyDenied))
9744+
);
9745+
9746+
let engine = Arc::new(native_identity_engine(rego, POLICY_DNS_OPEN_POLICY).unwrap());
9747+
let store = policy_dns_test_store();
9748+
let approved = POLICY_DNS_OPEN_POLICY.replace(
9749+
"filesystem_policy:",
9750+
" github:\n name: github\n endpoints:\n - { host: github.com, port: 443 }\n binaries: [{ path: /usr/bin/curl }]\nfilesystem_policy:",
9751+
);
9752+
let kept = keep_pypi_answer_across(&engine, &store, &approved).await;
9753+
engine.enter_fail_closed("test quarantine").unwrap();
9754+
let (admitted, _) = admit_staged(&store, &engine, SocketAddr::new(kept, 80)).await;
9755+
assert!(!admitted);
9756+
}
9757+
9758+
/// One name allowed for two binaries with different destination rules:
9759+
/// psql may reach a private address of its exact host, curl only public
9760+
/// addresses through a wildcard. The answer pins both addresses, and each
9761+
/// connection reaches only what its own endpoint permits.
9762+
#[tokio::test]
9763+
async fn policy_dns_answer_reaches_only_what_the_connecting_binary_may() {
9764+
let policy = r"
9765+
network_policies:
9766+
database:
9767+
name: database
9768+
endpoints:
9769+
- { host: db.service.example, port: 5432, protocol: tcp }
9770+
binaries: [{ path: /usr/bin/psql }]
9771+
service:
9772+
name: service
9773+
endpoints:
9774+
- { host: '*.service.example', port: 5432, protocol: tcp }
9775+
binaries: [{ path: /usr/bin/curl }]
9776+
filesystem_policy: { include_workdir: true, read_only: [], read_write: [] }
9777+
landlock: { compatibility: best_effort }
9778+
process: { run_as_user: sandbox, run_as_group: sandbox }
9779+
";
9780+
let rego = include_str!("../data/sandbox-policy.rego");
9781+
let engine = Arc::new(
9782+
native_identity_engine(rego, &policy_with_native_executable_paths(policy)).unwrap(),
9783+
);
9784+
let store = policy_dns_test_store();
9785+
let dns = crate::policy_dns::test_service(
9786+
engine.clone(),
9787+
store.clone(),
9788+
vec!["10.2.3.4".parse().unwrap(), "8.8.8.8".parse().unwrap()],
9789+
);
9790+
let answer = dns
9791+
.answer_query(
9792+
"db.service.example",
9793+
crate::policy_dns::AddressFamily::Ipv4,
9794+
std::time::Instant::now(),
9795+
)
9796+
.await
9797+
.unwrap();
9798+
let mapping = store
9799+
.lookup(answer.address, 5432, std::time::Instant::now())
9800+
.unwrap();
9801+
assert_eq!(
9802+
mapping.record.pinned_addresses,
9803+
[
9804+
"10.2.3.4".parse::<IpAddr>().unwrap(),
9805+
"8.8.8.8".parse().unwrap()
9806+
]
9807+
);
9808+
9809+
let destination = SocketAddr::new(answer.address, 5432);
9810+
let (open, completion) = staged_curl_open(destination, engine.current_generation());
9811+
let (_, _, _, transparent) = preauthorize_transparent_open(
9812+
open,
9813+
Some(&policy_dns_test_service(&store, &engine)),
9814+
&engine,
9815+
&BinaryIdentityCache::new(),
9816+
None,
9817+
None,
9818+
false,
9819+
None,
9820+
None,
9821+
)
9822+
.await
9823+
.expect("curl may reach the public address");
9824+
assert_eq!(completion.await.unwrap(), TcpOpenDecision::RelayReady);
9825+
let (_, connector) = transparent.and_then(|open| open.authorization).unwrap();
9826+
assert_eq!(connector.addrs(), &["8.8.8.8:5432".parse().unwrap()]);
9827+
}
9828+
97289829
#[test]
97299830
fn staged_denial_names_the_mapped_host_and_keeps_the_synthetic_address() {
97309831
let (open, _) = staged_curl_open("198.18.0.2:80".parse().unwrap(), 1);

0 commit comments

Comments
 (0)