Skip to content

Commit c0ea3e0

Browse files
mr-milesjohntmyers
authored andcommitted
fix(network): refresh a policy DNS mapping that expires during authorization
A transparent connection to a synthetic address looks its mapping up twice: once to name the destination, refreshing it if it has expired, and again to pin the addresses after the connection is authorized. The second lookup read the store directly and never refreshed, so a mapping that was live at the first lookup but expired while the connection was authorized was refused with transparent_tcp_mapping_denied, on a host the policy allows. With a trusted resolver that answers TTL 0, every mapping lives one second, and authorization includes resolving the connecting binary's identity. The direct, staged and dispatch paths now pin through PolicyDnsService::lookup_for_pin, which refreshes an expired mapping the way the first lookup does and refuses observations. The bare store accessor and its non-refreshing lookup are test-only, so no connection path can bypass the refresh. Refs #4674 Signed-off-by: mr-miles <miles.waller@gmail.com>
1 parent d2ff33f commit c0ea3e0

3 files changed

Lines changed: 198 additions & 104 deletions

File tree

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ pub(crate) enum ConnectLookupError {
106106
Backoff,
107107
}
108108

109+
impl ConnectLookupError {
110+
/// The mapping failure a denial reports. A failed or backed-off refresh
111+
/// leaves the mapping expired.
112+
pub(crate) fn mapping_error(&self) -> MappingLookupError {
113+
match self {
114+
Self::Mapping(error) => *error,
115+
Self::Refresh(_) | Self::Backoff => MappingLookupError::Expired,
116+
}
117+
}
118+
}
119+
109120
impl<R: TrustedResolver> PolicyDnsService<R> {
110121
pub(crate) fn new(
111122
policy: Arc<OpaEngine>,
@@ -169,6 +180,25 @@ impl<R: TrustedResolver> PolicyDnsService<R> {
169180
lookup().map_err(Into::into)
170181
}
171182

183+
/// The live mapping to pin an authorized connection to. Authorization runs
184+
/// between [`Self::lookup_for_connect`] and this lookup, and a short-lived
185+
/// mapping can expire meanwhile, so an expired mapping is refreshed here
186+
/// too. Observations pin no address and are refused.
187+
pub(crate) async fn lookup_for_pin(
188+
&self,
189+
address: IpAddr,
190+
port: u16,
191+
) -> Result<MappingLookup, MappingLookupError> {
192+
let lookup = self
193+
.lookup_for_connect(address, port)
194+
.await
195+
.map_err(|error| error.mapping_error())?;
196+
if lookup.record.is_observation() {
197+
return Err(MappingLookupError::EndpointMismatch);
198+
}
199+
Ok(lookup)
200+
}
201+
172202
pub(crate) async fn answer_query(
173203
&self,
174204
raw_name: &str,
@@ -403,6 +433,10 @@ impl<R: TrustedResolver> PolicyDnsService<R> {
403433
}
404434
}
405435

436+
/// The bare store, for tests. Connections look mappings up through
437+
/// [`Self::lookup_for_connect`] and [`Self::lookup_for_pin`], which refresh
438+
/// expired mappings.
439+
#[cfg(test)]
406440
pub(crate) fn store(&self) -> &Arc<ResolvedEndpointStore> {
407441
&self.store
408442
}
@@ -1084,6 +1118,48 @@ process: { run_as_user: sandbox, run_as_group: sandbox }
10841118
.expect("one DNS response must satisfy all waiting connects");
10851119
}
10861120

1121+
/// Authorization runs between the connect lookup and the pin lookup. A
1122+
/// TTL-0 mapping that expires in between is refreshed, not refused.
1123+
#[tokio::test]
1124+
async fn pin_lookup_refreshes_a_mapping_that_expired_during_authorization() {
1125+
let service = scripted_service(
1126+
BASE_POLICY,
1127+
vec![Ok(dns_answer(&["8.8.8.8"])), Ok(dns_answer(&["8.8.4.4"]))],
1128+
);
1129+
let answer = service
1130+
.answer_query("db.example", AddressFamily::Ipv4, Instant::now())
1131+
.await
1132+
.unwrap();
1133+
let connect = service
1134+
.lookup_for_connect(answer.address, 5432)
1135+
.await
1136+
.unwrap();
1137+
assert_eq!(connect.record.mapping_id, answer.mapping_id);
1138+
1139+
tokio::time::sleep(MIN_MAPPING_TTL + Duration::from_millis(100)).await;
1140+
let pinned = service.lookup_for_pin(answer.address, 5432).await.unwrap();
1141+
assert_eq!(pinned.record.synthetic_address, answer.address);
1142+
assert_ne!(pinned.record.mapping_id, answer.mapping_id);
1143+
assert_eq!(
1144+
pinned.record.pinned_addresses,
1145+
["8.8.4.4".parse::<IpAddr>().unwrap()]
1146+
);
1147+
assert_eq!(service.resolver.calls.load(Ordering::SeqCst), 2);
1148+
}
1149+
1150+
#[tokio::test]
1151+
async fn pin_lookup_refuses_an_observation() {
1152+
let service = service(BASE_POLICY, vec![]);
1153+
let observed = service
1154+
.answer_query("unknown.example", AddressFamily::Ipv4, Instant::now())
1155+
.await
1156+
.unwrap();
1157+
assert_eq!(
1158+
service.lookup_for_pin(observed.address, 443).await.err(),
1159+
Some(MappingLookupError::EndpointMismatch)
1160+
);
1161+
}
1162+
10871163
#[tokio::test]
10881164
async fn policy_revocation_during_expired_mapping_refresh_fails_closed() {
10891165
let base = service(BASE_POLICY, vec!["8.8.8.8".parse().unwrap()]);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,10 @@ impl ResolvedEndpointStore {
482482
Ok(record)
483483
}
484484

485-
/// The live mapping to pin an authorized connection to. Observations pin
486-
/// no address and are refused.
485+
/// The live mapping at `synthetic_address`, without refreshing it; for
486+
/// tests. Connections pin through `PolicyDnsService::lookup_for_pin`.
487+
/// Observations pin no address and are refused.
488+
#[cfg(test)]
487489
pub(crate) fn lookup(
488490
&self,
489491
synthetic_address: IpAddr,

0 commit comments

Comments
 (0)