Skip to content

Commit 4c77b21

Browse files
Merge #349
349: Closing `TcpListeners` in runner r=Pagten a=raoulstrackx When a Nitro enclave closes a `TcpListener`, sockets in the runner need to be cleaned up as well. This PR introduces the required changes to the ABI and runner. Co-authored-by: Raoul Strackx <[email protected]>
2 parents 4dc0b4b + 04b9793 commit 4c77b21

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

fortanix-vme/ci-common.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function cargo_test {
110110
fi
111111
RUST_BACKTRACE=full ${elf} -- --nocapture > ${out} 2> ${err}
112112

113-
out=$(cat ${out} | grep -v "#" || true)
113+
out=$(cat ${out} | grep -v "^#" || true)
114114
expected=$(cat ./out.expected)
115115

116116
if [ "${out}" == "${expected}" ]; then

fortanix-vme/fortanix-vme-abi/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ pub enum Request {
2626
Accept {
2727
/// The Vsock port the enclave is listening on
2828
enclave_port: u32,
29-
}
29+
},
30+
Close {
31+
enclave_port: u32,
32+
},
3033
}
3134

3235
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -87,7 +90,8 @@ pub enum Response {
8790
/// The vsock port number the runner will connect to the enclave in order to forward the
8891
/// incoming connection
8992
proxy_port: u32,
90-
}
93+
},
94+
Closed,
9195
}
9296

9397
#[cfg(test)]

fortanix-vme/fortanix-vme-runner/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ impl Server {
313313
self.listeners.read().unwrap().get(&addr).cloned()
314314
}
315315

316+
fn remove_listener(&self, addr: &VsockAddr) -> Option<Arc<Mutex<Listener>>> {
317+
self.listeners.write().unwrap().remove(&addr)
318+
}
319+
316320
// Preliminary work for PLAT-367
317321
#[allow(dead_code)]
318322
fn connection(&self, enclave: VsockAddr, runner: VsockAddr) -> Option<ConnectionInfo> {
@@ -413,11 +417,34 @@ impl Server {
413417
}
414418
}
415419

420+
fn handle_request_close(self: Arc<Self>, enclave_port: u32, enclave: &mut VsockStream) -> Result<(), IoError> {
421+
let cid: u32 = enclave.peer().unwrap().parse().unwrap_or(vsock::VMADDR_CID_HYPERVISOR);
422+
let addr = VsockAddr::new(cid, enclave_port);
423+
if let Some(listener) = self.remove_listener(&addr) {
424+
// Close `TcpListener`
425+
drop(listener);
426+
} else {
427+
println!("[warning] Can't close the connection as it can't be located.");
428+
}
429+
let response = Response::Closed;
430+
Self::log_communication(
431+
"runner",
432+
enclave.local_port().unwrap_or_default(),
433+
"enclave",
434+
enclave.peer_port().unwrap_or_default(),
435+
&format!("{:?}", &response),
436+
Direction::Right,
437+
"vsock");
438+
enclave.write(&serde_cbor::ser::to_vec(&response).unwrap())?;
439+
Ok(())
440+
}
441+
416442
fn handle_client(self: Arc<Self>, stream: &mut VsockStream) -> Result<(), IoError> {
417443
match Self::read_request(stream) {
418444
Ok(Request::Connect{ addr }) => self.handle_request_connect(&addr, stream)?,
419445
Ok(Request::Bind{ addr, enclave_port }) => self.handle_request_bind(&addr, enclave_port, stream)?,
420446
Ok(Request::Accept{ enclave_port }) => self.handle_request_accept(enclave_port, stream)?,
447+
Ok(Request::Close{ enclave_port }) => self.handle_request_close(enclave_port, stream)?,
421448
Err(_e) => return Err(IoError::new(IoErrorKind::InvalidData, "Failed to read request")),
422449
};
423450
Ok(())
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
Bind to socket to 3400
1+
Server run #1
2+
Bind TCP socket to port 3400
23
Listening for incoming connections...
34
Waiting for connection 1
45
Connection 1: Connected
56
Waiting for connection 2
67
Connection 2: Connected
8+
Server run #2
9+
Bind TCP socket to port 3400
10+
Listening for incoming connections...
11+
Waiting for connection 1
12+
Connection 1: Connected
13+
Waiting for connection 2
14+
Connection 2: Connected
15+
Bye bye

fortanix-vme/tests/incoming_connection/src/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::net::{IpAddr, Ipv4Addr, Shutdown, SocketAddr, TcpListener};
22
use std::io::{Read, Write};
33

4-
fn main() {
5-
println!("Bind to socket to 3400");
4+
fn server_run() {
5+
println!("Bind TCP socket to port 3400");
66
let listener = TcpListener::bind("127.0.0.1:3400").expect("Bind failed");
77
assert_eq!(listener.local_addr().unwrap(), SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 3400));
88

@@ -28,3 +28,11 @@ fn main() {
2828
}
2929
}
3030
}
31+
32+
fn main() {
33+
for run in 1..=2 {
34+
println!("Server run #{}", run);
35+
server_run()
36+
}
37+
println!("Bye bye");
38+
}

fortanix-vme/tests/incoming_connection/test_interaction.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ while [ true ]
44
do
55
echo "Interacting with test"
66
timeout 1s curl -k localhost:3400 || true
7-
sleep 20s
7+
sleep 5s
88
done

0 commit comments

Comments
 (0)