Skip to content

Commit 6c808ea

Browse files
committed
devices/vsock: selectively enable AF_UNIX in TSI
AF_UNIX transparency is only supported when running in a containerized environment. We detect such case by checking we only have a single virtio-fs device, and that device is pointing to "/", which is the scenario when running under podman+crun. In libkrun 2.x, the whole network configuration, including the TSI features, should be exposed to the users explicitly. Signed-off-by: Sergio Lopez <[email protected]>
1 parent 3d120b4 commit 6c808ea

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/devices/src/virtio/vsock/device.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl Vsock {
5353
queues: Vec<VirtQueue>,
5454
unix_ipc_port_map: Option<HashMap<u32, (PathBuf, bool)>>,
5555
enable_tsi: bool,
56+
enable_tsi_unix: bool,
5657
) -> super::Result<Vsock> {
5758
let mut queue_events = Vec::new();
5859
for _ in 0..queues.len() {
@@ -65,7 +66,13 @@ impl Vsock {
6566

6667
Ok(Vsock {
6768
cid,
68-
muxer: VsockMuxer::new(cid, host_port_map, unix_ipc_port_map, enable_tsi),
69+
muxer: VsockMuxer::new(
70+
cid,
71+
host_port_map,
72+
unix_ipc_port_map,
73+
enable_tsi,
74+
enable_tsi_unix,
75+
),
6976
queue_rx,
7077
queue_tx,
7178
queues,
@@ -84,12 +91,20 @@ impl Vsock {
8491
host_port_map: Option<HashMap<u16, u16>>,
8592
unix_ipc_port_map: Option<HashMap<u32, (PathBuf, bool)>>,
8693
enable_tsi: bool,
94+
enable_tsi_unix: bool,
8795
) -> super::Result<Vsock> {
8896
let queues: Vec<VirtQueue> = defs::QUEUE_SIZES
8997
.iter()
9098
.map(|&max_size| VirtQueue::new(max_size))
9199
.collect();
92-
Self::with_queues(cid, host_port_map, queues, unix_ipc_port_map, enable_tsi)
100+
Self::with_queues(
101+
cid,
102+
host_port_map,
103+
queues,
104+
unix_ipc_port_map,
105+
enable_tsi,
106+
enable_tsi_unix,
107+
)
93108
}
94109

95110
pub fn id(&self) -> &str {

src/devices/src/virtio/vsock/muxer.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub struct VsockMuxer {
107107
reaper_sender: Option<Sender<u64>>,
108108
unix_ipc_port_map: Option<HashMap<u32, (PathBuf, bool)>>,
109109
enable_tsi: bool,
110+
enable_tsi_unix: bool,
110111
}
111112

112113
impl VsockMuxer {
@@ -115,6 +116,7 @@ impl VsockMuxer {
115116
host_port_map: Option<HashMap<u16, u16>>,
116117
unix_ipc_port_map: Option<HashMap<u32, (PathBuf, bool)>>,
117118
enable_tsi: bool,
119+
enable_tsi_unix: bool,
118120
) -> Self {
119121
VsockMuxer {
120122
cid,
@@ -128,6 +130,7 @@ impl VsockMuxer {
128130
reaper_sender: None,
129131
unix_ipc_port_map,
130132
enable_tsi,
133+
enable_tsi_unix,
131134
}
132135
}
133136

@@ -282,6 +285,10 @@ impl VsockMuxer {
282285
defs::SOCK_STREAM => {
283286
debug!("vsock: proxy create stream");
284287
let id = ((req.peer_port as u64) << 32) | (defs::TSI_PROXY_PORT as u64);
288+
if req.family as i32 == libc::AF_UNIX && !self.enable_tsi_unix {
289+
warn!("vsock: rejecting tcp unix proxy because tsi_unix is disabled");
290+
return;
291+
}
285292
match TcpProxy::new(
286293
id,
287294
self.cid,
@@ -305,6 +312,10 @@ impl VsockMuxer {
305312
defs::SOCK_DGRAM => {
306313
debug!("vsock: proxy create dgram");
307314
let id = ((req.peer_port as u64) << 32) | (defs::TSI_PROXY_PORT as u64);
315+
if req.family as i32 == libc::AF_UNIX && !self.enable_tsi_unix {
316+
warn!("vsock: rejecting udp unix proxy because tsi_unix is disabled");
317+
return;
318+
}
308319
match UdpProxy::new(
309320
id,
310321
self.cid,

src/libkrun/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,7 @@ pub extern "C" fn krun_start_enter(ctx_id: u32) -> i32 {
21892189
host_port_map: None,
21902190
unix_ipc_port_map: None,
21912191
enable_tsi: false,
2192+
enable_tsi_unix: false,
21922193
};
21932194

21942195
#[cfg(feature = "net")]
@@ -2210,6 +2211,15 @@ pub extern "C" fn krun_start_enter(ctx_id: u32) -> i32 {
22102211
}
22112212

22122213
if vsock_set {
2214+
if vsock_config.enable_tsi {
2215+
// We only support using TSI for AF_UNIX in a containerized context,
2216+
// so only enable it when we have a single virtio-fs device pointing
2217+
// to root.
2218+
#[cfg(not(feature = "tee"))]
2219+
if ctx_cfg.vmr.fs.len() == 1 && ctx_cfg.vmr.fs[0].shared_dir == "/" {
2220+
vsock_config.enable_tsi_unix = true;
2221+
}
2222+
}
22132223
ctx_cfg.vmr.set_vsock_device(vsock_config).unwrap();
22142224
}
22152225

src/vmm/src/vmm_config/vsock.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub struct VsockDeviceConfig {
4242
pub unix_ipc_port_map: Option<HashMap<u32, (PathBuf, bool)>>,
4343
/// Whether to enable TSI
4444
pub enable_tsi: bool,
45+
/// Whether to enable TSI for AF_UNIX
46+
pub enable_tsi_unix: bool,
4547
}
4648

4749
struct VsockWrapper {
@@ -81,6 +83,7 @@ impl VsockBuilder {
8183
cfg.host_port_map,
8284
cfg.unix_ipc_port_map,
8385
cfg.enable_tsi,
86+
cfg.enable_tsi_unix,
8487
)
8588
.map_err(VsockConfigError::CreateVsockDevice)
8689
}

0 commit comments

Comments
 (0)