sandbox-ipc 0.4.0

An IPC implementation with an eye toward enabling privilege separation.
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use ::io::{SendableFile, SendableSocket};
use ::ser::{SerializeWrapper, SerializeWrapperGuard};

extern crate mio_named_pipes;

mod channel;
mod sharedmem;
mod sync;

pub use self::channel::*;
pub(crate) use self::sharedmem::*;
pub(crate) use self::sync::*;

use std::{io, fs, mem, ptr, fmt};
use std::cell::RefCell;
use std::borrow::Borrow;
use std::sync::{Once, ONCE_INIT};
use std::net::UdpSocket;
use std::os::raw::{c_int, c_ulong, c_ushort, c_uchar};
use std::os::windows::prelude::*;

use serde::{Serialize, Deserialize, Serializer, Deserializer};
use winapi::shared::ntdef::{HANDLE, WCHAR};
use winapi::shared::guiddef::{GUID};
use winapi::shared::minwindef::{DWORD, FALSE};
use winapi::um::winnt::{DUPLICATE_CLOSE_SOURCE, DUPLICATE_SAME_ACCESS};
use winapi::um::handleapi::{CloseHandle, DuplicateHandle};
use winapi::um::processthreadsapi::{GetCurrentProcess, GetCurrentProcessId};
use winapi::um::winsock2::{WSADuplicateSocketW, WSAGetLastError, WSASocketW};
use winapi::um::winsock2::{INVALID_SOCKET};
use winhandle::*;

thread_local! {
    static CURRENT_SERIALIZE_CHANNEL_REMOTE_PROCESS: RefCell<Option<HandleSender>> = RefCell::new(None);
    static CURRENT_DESERIALIZE_CHANNEL_REMOTE_PROCESS: RefCell<Option<HandleReceiver>> = RefCell::new(None);
}

#[derive(Debug)]
pub struct SendableWinHandle<B = WinHandle>(pub B);

impl From<WinHandle> for SendableWinHandle {
    fn from(h: WinHandle) -> Self { SendableWinHandle(h) }
}

impl<'a, B> Serialize for SendableWinHandle<&'a B> where
    B: AsRawHandle,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        use serde::ser::Error;

        CURRENT_SERIALIZE_CHANNEL_REMOTE_PROCESS.with(|sender_guard| {
            let mut sender_guard = sender_guard.borrow_mut();
            let sender = sender_guard.as_mut()
                .ok_or_else(|| S::Error::custom("attempted to serialize handle outside of IPC channel"))?;
            let remote_handle = sender.send(self.0.borrow().as_raw_handle())
                .map_err(|x| S::Error::custom(x))?;
            (remote_handle as usize).serialize(serializer)
        })
    }
}

impl Serialize for SendableWinHandle {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        SendableWinHandle(&self.0).serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for SendableWinHandle {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>
    {
        use serde::de::Error;

        CURRENT_DESERIALIZE_CHANNEL_REMOTE_PROCESS.with(|receiver_guard| {
            let mut receiver_guard = receiver_guard.borrow_mut();
            let _receiver = receiver_guard.as_mut()
                .ok_or_else(|| D::Error::custom("attempted to deserialize handle outside of IPC channel"))?;
            let handle = usize::deserialize(deserializer)? as HANDLE;
            let handle = WinHandle::from_raw(handle)
                .ok_or_else(|| D::Error::custom("invalid Windows handle received"))?;
            Ok(SendableWinHandle(handle))
        })
    }
}

impl<B> Serialize for SendableFile<B> where
    B: Borrow<fs::File>,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        SendableWinHandle(self.0.borrow()).serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for SendableFile {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>
    {
        let handle = SendableWinHandle::deserialize(deserializer)?;
        Ok(SendableFile(unsafe { fs::File::from_raw_handle(handle.0.into_raw()) }))
    }
}

impl<'a, T> Serialize for SendableSocket<T> where
    T: AsRawSocket,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer,
    {
        use serde::ser::Error;

        CURRENT_SERIALIZE_CHANNEL_REMOTE_PROCESS.with(|sender_guard| {
            let mut sender_guard = sender_guard.borrow_mut();
            let sender = sender_guard.as_mut()
                .ok_or_else(|| S::Error::custom("attempted to serialize handle outside of IPC channel"))?;
            let proto_info = unsafe {
                let mut proto_info: WsaProtoInfo = mem::zeroed();
                if WSADuplicateSocketW(
                    self.0.as_raw_socket() as usize,
                    sender.remote_process_id().map_err(|x| S::Error::custom(x))?,
                    &mut proto_info as *mut WsaProtoInfo as _,
                ) != 0 {
                    return Err(S::Error::custom(io::Error::from_raw_os_error(WSAGetLastError())));
                }
                proto_info
            };
            proto_info.serialize(serializer)
        })
    }
}

static CALLED_WSASTARTUP: Once = ONCE_INIT;

impl<'de, T> Deserialize<'de> for SendableSocket<T> where
    T: FromRawSocket,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>,
    {
        use serde::de::Error;

        // Make sure we've called WSAStartup
        CALLED_WSASTARTUP.call_once(|| {
            // Any libstd socket operation will call WSAStartup
            let _ = UdpSocket::bind("127.0.0.1:9999");
        });

        CURRENT_DESERIALIZE_CHANNEL_REMOTE_PROCESS.with(|receiver_guard| {
            let mut receiver_guard = receiver_guard.borrow_mut();
            let _receiver = receiver_guard.as_mut()
                .ok_or_else(|| D::Error::custom("attempted to deserialize handle outside of IPC channel"))?;
            let mut proto_info: WsaProtoInfo = WsaProtoInfo::deserialize(deserializer)?;
            let raw_socket = unsafe { WSASocketW(
                proto_info.iAddressFamily,
                proto_info.iSocketType,
                proto_info.iProtocol,
                &mut proto_info as *mut WsaProtoInfo as _,
                0,
                0, // Flags are ignored when receiving sockets from another process
            )};
            if raw_socket == INVALID_SOCKET {
                return Err(D::Error::custom(io::Error::from_raw_os_error(unsafe { WSAGetLastError() })));
            }
            Ok(SendableSocket(unsafe { T::from_raw_socket(raw_socket as RawSocket) }))
        })
    }
}

#[allow(bad_style)]
#[repr(C)]
#[derive(Serialize, Deserialize)]
struct WsaProtoInfo {
    dwServiceFlags1: DWORD,
    dwServiceFlags2: DWORD,
    dwServiceFlags3: DWORD,
    dwServiceFlags4: DWORD,
    dwProviderFlags: DWORD,
    #[serde(with = "serde_guid")]
    ProviderId: GUID,
    dwCatalogEntryId: DWORD,
    ProtocolChain: WsaProtocolChain,
    iVersion: c_int,
    iAddressFamily: c_int,
    iMaxSockAddr: c_int,
    iMinSockAddr: c_int,
    iSocketType: c_int,
    iProtocol: c_int,
    iProtocolMaxOffset: c_int,
    iNetworkByteOrder: c_int,
    iSecurityScheme: c_int,
    dwMessageSize: DWORD,
    dwProviderReserved: DWORD,
    #[serde(with = "serde_wchar256")]
    szProtocol: [WCHAR; 256],
}

#[allow(bad_style)]
#[repr(C)]
#[derive(Serialize, Deserialize, Debug)]
struct WsaProtocolChain {
    ChainLen: c_int,
    ChainEntries: [DWORD; 7],
}

mod serde_guid {
    use super::*;

    pub fn serialize<S>(f: &GUID, serializer: S) -> Result<S::Ok, S::Error> where
        S: Serializer,
    {
        (f.Data1, f.Data2, f.Data3, f.Data4).serialize(serializer)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<GUID, D::Error> where
        D: Deserializer<'de>,
    {
        let data: (c_ulong, c_ushort, c_ushort, [c_uchar; 8]) = Deserialize::deserialize(deserializer)?;
        Ok(GUID {
            Data1: data.0,
            Data2: data.1,
            Data3: data.2,
            Data4: data.3,
        })
    }
}

mod serde_wchar256 {
    use super::*;

    pub fn serialize<S>(f: &[WCHAR; 256], serializer: S) -> Result<S::Ok, S::Error> where
        S: Serializer,
    {
        use serde::ser::{SerializeTuple};

        let mut serializer = serializer.serialize_tuple(256)?;
        for wchar in f.iter() {
            serializer.serialize_element::<WCHAR>(wchar)?;
        }
        serializer.end()
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<[WCHAR; 256], D::Error> where
        D: Deserializer<'de>,
    {
        use serde::de::{Visitor, SeqAccess, Error};

        struct AVisitor;

        impl<'de> Visitor<'de> for AVisitor {
            type Value = [WCHAR; 256];

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("WCHAR string of length 256")
            }

            fn visit_seq<V>(self, mut seq: V) -> Result<[WCHAR; 256], V::Error>
                where V: SeqAccess<'de>
            {
                let mut buffer: [WCHAR; 256] = [0; 256];
                for (i, c) in buffer.iter_mut().enumerate() {
                    let read_c: WCHAR = seq.next_element()?
                              .ok_or_else(|| Error::invalid_length(i, &self))?;
                    *c = read_c;
                }
                Ok(buffer)
            }
        }

        deserializer.deserialize_tuple(256, AVisitor)
    }
}

pub(crate) struct ChannelSerializeWrapper;

impl<'a, C> SerializeWrapper<'a, C> for ChannelSerializeWrapper where
    C: Channel,
{
    type SerializeGuard = ChannelSerializeGuard;
    type DeserializeGuard = ChannelDeserializeGuard;

    fn before_serialize(channel: &'a mut C) -> ChannelSerializeGuard {
        let mut sender = Some(HandleSender {
            target: unsafe { mem::transmute::<HandleTarget<&ProcessHandle>, HandleTarget<&'static ProcessHandle>>(channel.handle_target()) },
            handles: Vec::new(),
        });

        CURRENT_SERIALIZE_CHANNEL_REMOTE_PROCESS.with(|x| 
            mem::swap(
                &mut *x.borrow_mut(),
                &mut sender,
            )
        );
        ChannelSerializeGuard(sender)
    }

    fn before_deserialize(_channel: &'a mut C) -> ChannelDeserializeGuard {
        let mut receiver = Some(HandleReceiver);
        CURRENT_DESERIALIZE_CHANNEL_REMOTE_PROCESS.with(|x|
            mem::swap(
                &mut *x.borrow_mut(),
                &mut receiver,
            )
        );
        ChannelDeserializeGuard(receiver)
    }
}

pub(crate) struct ChannelSerializeGuard(Option<HandleSender>);

impl Drop for ChannelSerializeGuard {
    fn drop(&mut self) {
        CURRENT_SERIALIZE_CHANNEL_REMOTE_PROCESS.with(|sender_guard| {
            let mut sender_guard = sender_guard.borrow_mut();
            *sender_guard = self.0.take()
        });
    }
}

impl<'a> SerializeWrapperGuard<'a> for ChannelSerializeGuard {
    fn commit(self) {
        CURRENT_SERIALIZE_CHANNEL_REMOTE_PROCESS.with(|sender_guard| {
            let mut sender_guard = sender_guard.borrow_mut();
            sender_guard.as_mut().unwrap().handles.clear();
        });
    }
}

pub(crate) struct ChannelDeserializeGuard(Option<HandleReceiver>);

impl Drop for ChannelDeserializeGuard {
    fn drop(&mut self) {
        CURRENT_DESERIALIZE_CHANNEL_REMOTE_PROCESS.with(|x| *x.borrow_mut() = self.0.take());
    }
}

impl<'a> SerializeWrapperGuard<'a> for ChannelDeserializeGuard {
    fn commit(self) {
    }
}

pub(crate) trait Channel {
    fn handle_target(&self) -> HandleTarget<&ProcessHandle>;
}

struct HandleSender {
    target: HandleTarget<&'static ProcessHandle>,
    handles: Vec<HANDLE>,
}

impl Drop for HandleSender {
    fn drop(&mut self) {
        // If this is called without self.handles being empty, there was a failure somewhere and
        // we should close all the handles we sent.
        match self.target {
            HandleTarget::CurrentProcess => {
                for &handle in self.handles.iter() {
                    unsafe { CloseHandle(handle); }
                }
            },
            HandleTarget::RemoteProcess(process_handle) => {
                for &handle in self.handles.iter() {
                    if unsafe { DuplicateHandle(
                        process_handle.handle.0.get(), handle,
                        ptr::null_mut(), ptr::null_mut(),
                        0, FALSE, DUPLICATE_CLOSE_SOURCE,
                    ) } == FALSE {
                        error!("Closing remote handle via DuplicateHandle failed: {}", io::Error::last_os_error());
                    }
                }
            },
            HandleTarget::None => {},
        }
    }
}

struct HandleReceiver;

impl HandleSender {
    fn send(&mut self, handle: HANDLE) -> io::Result<HANDLE> {
        let remote_process_handle = match self.target {
            HandleTarget::CurrentProcess => unsafe { GetCurrentProcess() },
            HandleTarget::RemoteProcess(remote) => remote.handle.0.get(),
            HandleTarget::None => return Err(io::Error::new(io::ErrorKind::BrokenPipe, "this pipe is not configured to send handles")),
        };

        unsafe {
            let mut remote_handle = ptr::null_mut();
            winapi_bool_call!(log: DuplicateHandle(
                GetCurrentProcess(), handle,
                remote_process_handle, &mut remote_handle,
                0, FALSE, DUPLICATE_SAME_ACCESS,
            ))?;

            self.handles.push(remote_handle);

            Ok(remote_handle)
        }
    }

    fn remote_process_id(&self) -> io::Result<DWORD> {
        Ok(match self.target {
            HandleTarget::CurrentProcess => unsafe { GetCurrentProcessId() },
            HandleTarget::RemoteProcess(remote) => remote.id,
            HandleTarget::None => return Err(io::Error::new(io::ErrorKind::BrokenPipe, "this pipe is not configured to send handles")),
        })
    }
}