1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Do nothing on unix as tube_transporter is windows only.
#![cfg(windows)]

//! This IPC crate is used by the broker process to send boot data across the
//! different crosvm child processes on Windows.

use std::fmt;
use std::fmt::Display;

use base::deserialize_and_recv;
use base::named_pipes::BlockingMode;
use base::named_pipes::FramingMode;
use base::named_pipes::PipeConnection;
use base::serialize_and_send;
use base::set_alias_pid;
use base::set_duplicate_handle_tube;
use base::DuplicateHandleTube;
use base::FromRawDescriptor;
use base::RawDescriptor;
use base::Tube;
use base::TubeError;
use serde::Deserialize;
use serde::Serialize;
use thiserror::Error as ThisError;

pub mod packed_tube;

pub type TransportTubeResult<T> = std::result::Result<T, TubeTransportError>;

/// Contains information for a child process to set up the Tube for use.
#[derive(Serialize, Deserialize, Debug)]
pub struct TubeTransferData {
    // Tube to be sent to the child process.
    pub tube: Tube,
    // Used to determine what the Tube's purpose is.
    pub tube_token: TubeToken,
}

#[derive(Debug, ThisError)]
pub enum TubeTransportError {
    #[error("Serializing and sending failed: {0}")]
    SerializeSendError(TubeError),
    #[error("Serializing and recving failed: {0}")]
    DeserializeRecvError(TubeError),
    #[error("Tube with token {0} not found")]
    TubeNotFound(TubeToken),
}

/// The target child process will use this decide what a Tube's purpose is.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub enum TubeToken {
    Bootstrap,
    Control,
    Ipc,
    VhostUser,
}

impl Display for TubeToken {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TransportData {
    dh_tube: Option<Tube>,
    alias_pid: Option<u32>,
    // A list Tubes and related metadata to transfer. This list should not be emptied, since
    // that would cause a Tube to drop and thus closing the Tube.
    tube_transfer_data_list: Vec<TubeTransferData>,
}

/// Used by the Broker process to transport Tubes to a target child process.
pub struct TubeTransporter {
    pipe_connection: PipeConnection,
    transport_data: TransportData,
}

impl TubeTransporter {
    /// WARNING: PipeConnection must be a message mode, blocking pipe.
    pub fn new(
        pipe_connection: PipeConnection,
        tube_transfer_data_list: Vec<TubeTransferData>,
        alias_pid: Option<u32>,
        dh_tube: Option<Tube>,
    ) -> TubeTransporter {
        TubeTransporter {
            pipe_connection,
            transport_data: TransportData {
                dh_tube,
                alias_pid,
                tube_transfer_data_list,
            },
        }
    }

    /// Sends tubes to the other end of the pipe. Note that you must provide the destination
    /// PID so that descriptors can be sent.
    pub fn serialize_and_transport(&self, child_pid: u32) -> TransportTubeResult<()> {
        serialize_and_send(
            |buf| self.pipe_connection.write(buf),
            &self.transport_data,
            /* target_pid= */ Some(child_pid),
        )
        .map_err(TubeTransportError::SerializeSendError)?;
        Ok(())
    }

    pub fn push_tube(&mut self, tube: Tube, tube_token: TubeToken) {
        self.transport_data
            .tube_transfer_data_list
            .push(TubeTransferData { tube, tube_token });
    }
}

/// Used by the child process to read Tubes sent from the Broker.
pub struct TubeTransporterReader {
    reader_pipe_connection: PipeConnection,
}

impl TubeTransporterReader {
    /// WARNING: PipeConnection must be a message mode, blocking pipe.
    pub fn create_tube_transporter_reader(pipe_connection: PipeConnection) -> Self {
        TubeTransporterReader {
            reader_pipe_connection: pipe_connection,
        }
    }

    pub fn read_tubes(&self) -> TransportTubeResult<TubeTransferDataList> {
        let res: TransportData =
            // SAFETY: We provide a valid buffer to `read()`
            deserialize_and_recv(|buf| unsafe { self.reader_pipe_connection.read(buf) })
                .map_err(TubeTransportError::DeserializeRecvError)?;

        if let Some(tube) = res.dh_tube {
            let dh_tube = DuplicateHandleTube::new(tube);
            set_duplicate_handle_tube(dh_tube);
        }
        if let Some(alias_pid) = res.alias_pid {
            set_alias_pid(alias_pid);
        }
        Ok(TubeTransferDataList(res.tube_transfer_data_list))
    }
}

impl FromRawDescriptor for TubeTransporterReader {
    /// Creates a TubeTransporterReader from a raw descriptor.
    /// # Safety
    /// 1. descriptor is valid & ownership is released to the TubeTransporterReader
    ///
    /// # Avoiding U.B.
    /// 1. The underlying pipe is a message pipe in wait mode.
    unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Self {
        TubeTransporterReader::create_tube_transporter_reader(PipeConnection::from_raw_descriptor(
            descriptor,
            FramingMode::Message,
            BlockingMode::Wait,
        ))
    }
}

#[derive(Debug)]
pub struct TubeTransferDataList(Vec<TubeTransferData>);

impl TubeTransferDataList {
    pub fn get_tube(&mut self, token: TubeToken) -> TransportTubeResult<Tube> {
        Ok(self
            .0
            .remove(
                match self
                    .0
                    .iter()
                    .position(|tube_data| tube_data.tube_token == token)
                {
                    Some(pos) => pos,
                    None => return Err(TubeTransportError::TubeNotFound(token)),
                },
            )
            .tube)
    }
}

#[cfg(test)]
mod tests {
    use std::thread;

    use base::named_pipes::pair;
    use base::named_pipes::BlockingMode;
    use base::named_pipes::FramingMode;
    use base::Event;
    use base::EventWaitResult;
    use winapi::um::processthreadsapi::GetCurrentProcessId;

    use super::*;

    #[test]
    fn test_send_tubes_through_tube_transporter() {
        let (broker_pipe_connection_server, child_process_pipe) = pair(
            &FramingMode::Message,
            &BlockingMode::Wait,
            /* timeout= */ 0,
        )
        .unwrap();

        // Start a thread to simulate a new process. This thread will attempt to read the Tubes
        // from the pipe.
        let child_join_handle = thread::Builder::new()
            .name("Sandboxed child process listening thread".to_string())
            .spawn(move || {
                let child_process_pipe = child_process_pipe;
                let transporter_reader =
                    TubeTransporterReader::create_tube_transporter_reader(child_process_pipe);
                transporter_reader.read_tubes().unwrap()
            })
            .unwrap();

        // SAFETY: This kernel function just returns the current PID.
        //
        // We want to get the current PID as a sanity check for the `OpenProcess` call
        // when duplicating a handle through a Tube.
        let current_pid = unsafe { GetCurrentProcessId() };

        // We want the test to drop device_tube_1 and 2 after transportation is complete. Since
        // Tubes have many SafeDescriptors, we still want Tubes to work even if their original
        // handles are closed.
        let (main_tube_1, main_tube_2) = {
            let (main_tube_1, device_tube_1) = Tube::pair().unwrap();
            let (main_tube_2, device_tube_2) = Tube::pair().unwrap();

            let tube_transporter = TubeTransporter::new(
                broker_pipe_connection_server,
                vec![
                    TubeTransferData {
                        tube: device_tube_1,
                        tube_token: TubeToken::Control,
                    },
                    TubeTransferData {
                        tube: device_tube_2,
                        tube_token: TubeToken::Ipc,
                    },
                ],
                /* alias_pid= */
                None,
                /* dh_tube= */
                None,
            );

            // TODO: we just test within the same process here, so we send to ourselves.
            tube_transporter
                .serialize_and_transport(current_pid)
                .expect("serialize and transporting failed");

            (main_tube_1, main_tube_2)
        };

        let tube_data_list = child_join_handle.join().unwrap().0;
        assert_eq!(tube_data_list.len(), 2);
        assert_eq!(tube_data_list[0].tube_token, TubeToken::Control);
        assert_eq!(tube_data_list[1].tube_token, TubeToken::Ipc);

        // Test sending a string through the Tubes
        tube_data_list[0]
            .tube
            .send(&"hello main 1".to_string())
            .expect("tube 1 failed to send");
        tube_data_list[1]
            .tube
            .send(&"hello main 2".to_string())
            .expect("tube 2 failed to send.");

        assert_eq!(main_tube_1.recv::<String>().unwrap(), "hello main 1");
        assert_eq!(main_tube_2.recv::<String>().unwrap(), "hello main 2");

        // Test sending a handle through a Tube. Note that the Tube in `tube_data_list[1]` can't
        // send a handle across because `CHILD_PID` isn't mapped to a real process.
        let event_handle = Event::new().unwrap();

        tube_data_list[0]
            .tube
            .send(&event_handle)
            .expect("tube 1 failed to send");

        let duped_handle = main_tube_1.recv::<Event>().unwrap();

        event_handle.signal().unwrap();

        assert!(matches!(
            duped_handle
                .wait_timeout(std::time::Duration::from_millis(2000))
                .unwrap(),
            EventWaitResult::Signaled
        ));
    }
}