Skip to content

Commit aadf2a1

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@luma-energy.co.uk>
1 parent bd9a68c commit aadf2a1

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
@@ -1688,4 +1688,36 @@ process: { run_as_user: sandbox, run_as_group: sandbox }
16881688
service.policy.reload_from_proto(&widened).unwrap();
16891689
assert_eq!(lookup().await.unwrap().address, before.address, "new port");
16901690
}
1691+
1692+
/// The record pins every answer some eligible endpoint permits, in
1693+
/// resolver order, and nothing else: here the exact host permits the
1694+
/// private address, the wildcard the public one, and neither loopback.
1695+
#[tokio::test]
1696+
async fn mapping_pins_the_union_of_what_eligible_endpoints_permit() {
1697+
let policy = BASE_POLICY.replace("db.example", "db.example.com").replace(
1698+
"filesystem_policy:",
1699+
" web:\n name: web\n endpoints:\n - { host: '*.example.com', port: 443 }\n binaries: [{ path: /usr/bin/curl }]\nfilesystem_policy:",
1700+
);
1701+
let service = service(
1702+
&policy,
1703+
vec![
1704+
"8.8.8.8".parse().unwrap(),
1705+
"127.0.0.1".parse().unwrap(),
1706+
"10.2.3.4".parse().unwrap(),
1707+
],
1708+
);
1709+
let now = Instant::now();
1710+
let answer = service
1711+
.answer_query("db.example.com", AddressFamily::Ipv4, now)
1712+
.await
1713+
.unwrap();
1714+
let mapping = service.store.lookup(answer.address, 443, now).unwrap();
1715+
assert_eq!(
1716+
mapping.record.pinned_addresses,
1717+
[
1718+
"8.8.8.8".parse::<IpAddr>().unwrap(),
1719+
"10.2.3.4".parse().unwrap()
1720+
]
1721+
);
1722+
}
16911723
}

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9124,6 +9124,107 @@ process: { run_as_user: sandbox, run_as_group: sandbox }
91249124
);
91259125
}
91269126

9127+
/// A kept answer gives no access the current policy does not: once its
9128+
/// host leaves the policy, or the policy is quarantined, it is refused by
9129+
/// the policy decision.
9130+
#[tokio::test]
9131+
async fn policy_dns_answer_is_refused_once_the_policy_no_longer_allows_it() {
9132+
let rego = include_str!("../data/sandbox-policy.rego");
9133+
let engine = Arc::new(native_identity_engine(rego, POLICY_DNS_OPEN_POLICY).unwrap());
9134+
let store = policy_dns_test_store();
9135+
let without_pypi = POLICY_DNS_OPEN_POLICY.replace(
9136+
" - { host: pypi.org, port: 80 }",
9137+
" - { host: files.pythonhosted.org, port: 80 }",
9138+
);
9139+
let kept = keep_pypi_answer_across(&engine, &store, &without_pypi).await;
9140+
assert_eq!(
9141+
admit_staged(&store, &engine, SocketAddr::new(kept, 80)).await,
9142+
(false, TcpOpenDecision::Denied(TcpOpenDenial::PolicyDenied))
9143+
);
9144+
9145+
let engine = Arc::new(native_identity_engine(rego, POLICY_DNS_OPEN_POLICY).unwrap());
9146+
let store = policy_dns_test_store();
9147+
let approved = POLICY_DNS_OPEN_POLICY.replace(
9148+
"filesystem_policy:",
9149+
" github:\n name: github\n endpoints:\n - { host: github.com, port: 443 }\n binaries: [{ path: /usr/bin/curl }]\nfilesystem_policy:",
9150+
);
9151+
let kept = keep_pypi_answer_across(&engine, &store, &approved).await;
9152+
engine.enter_fail_closed("test quarantine").unwrap();
9153+
let (admitted, _) = admit_staged(&store, &engine, SocketAddr::new(kept, 80)).await;
9154+
assert!(!admitted);
9155+
}
9156+
9157+
/// One name allowed for two binaries with different destination rules:
9158+
/// psql may reach a private address of its exact host, curl only public
9159+
/// addresses through a wildcard. The answer pins both addresses, and each
9160+
/// connection reaches only what its own endpoint permits.
9161+
#[tokio::test]
9162+
async fn policy_dns_answer_reaches_only_what_the_connecting_binary_may() {
9163+
let policy = r"
9164+
network_policies:
9165+
database:
9166+
name: database
9167+
endpoints:
9168+
- { host: db.service.example, port: 5432, protocol: tcp }
9169+
binaries: [{ path: /usr/bin/psql }]
9170+
service:
9171+
name: service
9172+
endpoints:
9173+
- { host: '*.service.example', port: 5432, protocol: tcp }
9174+
binaries: [{ path: /usr/bin/curl }]
9175+
filesystem_policy: { include_workdir: true, read_only: [], read_write: [] }
9176+
landlock: { compatibility: best_effort }
9177+
process: { run_as_user: sandbox, run_as_group: sandbox }
9178+
";
9179+
let rego = include_str!("../data/sandbox-policy.rego");
9180+
let engine = Arc::new(
9181+
native_identity_engine(rego, &policy_with_native_executable_paths(policy)).unwrap(),
9182+
);
9183+
let store = policy_dns_test_store();
9184+
let dns = crate::policy_dns::test_service(
9185+
engine.clone(),
9186+
store.clone(),
9187+
vec!["10.2.3.4".parse().unwrap(), "8.8.8.8".parse().unwrap()],
9188+
);
9189+
let answer = dns
9190+
.answer_query(
9191+
"db.service.example",
9192+
crate::policy_dns::AddressFamily::Ipv4,
9193+
std::time::Instant::now(),
9194+
)
9195+
.await
9196+
.unwrap();
9197+
let mapping = store
9198+
.lookup(answer.address, 5432, std::time::Instant::now())
9199+
.unwrap();
9200+
assert_eq!(
9201+
mapping.record.pinned_addresses,
9202+
[
9203+
"10.2.3.4".parse::<IpAddr>().unwrap(),
9204+
"8.8.8.8".parse().unwrap()
9205+
]
9206+
);
9207+
9208+
let destination = SocketAddr::new(answer.address, 5432);
9209+
let (open, completion) = staged_curl_open(destination, engine.current_generation());
9210+
let (_, _, _, transparent) = preauthorize_transparent_open(
9211+
open,
9212+
Some(&store),
9213+
&engine,
9214+
&BinaryIdentityCache::new(),
9215+
None,
9216+
None,
9217+
false,
9218+
None,
9219+
None,
9220+
)
9221+
.await
9222+
.expect("curl may reach the public address");
9223+
assert_eq!(completion.await.unwrap(), TcpOpenDecision::RelayReady);
9224+
let (_, connector) = transparent.and_then(|open| open.authorization).unwrap();
9225+
assert_eq!(connector.addrs(), &["8.8.8.8:5432".parse().unwrap()]);
9226+
}
9227+
91279228
#[test]
91289229
fn staged_denial_names_the_mapped_host_and_keeps_the_synthetic_address() {
91299230
let (open, _) = staged_curl_open("198.18.0.2:80".parse().unwrap(), 1);

0 commit comments

Comments
 (0)