subc-os 0.1.55

Operating-system primitives for the subc daemon that need unsafe code, behind a safe API: per-process identity, signalling, memory and CPU usage, and the launch-nonce descriptor handoff.
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#![allow(unsafe_code)]

use super::{invalid, OpenRetainedError, Stage};
use std::{
    ffi::OsStr,
    fs::File,
    io,
    mem::{offset_of, size_of},
    os::windows::{
        ffi::OsStrExt,
        io::{AsRawHandle, FromRawHandle},
    },
    ptr::{null, null_mut},
    thread,
    time::Duration,
};
use windows_sys::{
    Wdk::{
        Foundation::OBJECT_ATTRIBUTES,
        Storage::FileSystem::{
            FileRenameInformation, FileRenameInformationEx, NtCreateFile, NtSetInformationFile,
            FILE_CREATE, FILE_DIRECTORY_FILE, FILE_OPEN, FILE_OPEN_REPARSE_POINT,
            FILE_SYNCHRONOUS_IO_NONALERT, FILE_WRITE_THROUGH,
        },
    },
    Win32::{
        Foundation::{
            RtlNtStatusToDosError, ERROR_INVALID_FUNCTION, ERROR_INVALID_PARAMETER,
            ERROR_NOT_SUPPORTED, ERROR_SHARING_VIOLATION, NTSTATUS, UNICODE_STRING,
        },
        Security::Cryptography::{BCryptGenRandom, BCRYPT_USE_SYSTEM_PREFERRED_RNG},
        Storage::FileSystem::{
            FileDispositionInfo, GetFileInformationByHandle, GetFileType,
            SetFileInformationByHandle, BY_HANDLE_FILE_INFORMATION, DELETE,
            FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_REPARSE_POINT,
            FILE_DISPOSITION_INFO, FILE_GENERIC_READ, FILE_GENERIC_WRITE, FILE_READ_ATTRIBUTES,
            FILE_RENAME_INFO, FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_TYPE_DISK,
            READ_CONTROL, SYNCHRONIZE, WRITE_DAC, WRITE_OWNER,
        },
        System::{
            WindowsProgramming::{
                FILE_RENAME_FLAG_POSIX_SEMANTICS, FILE_RENAME_FLAG_REPLACE_IF_EXISTS,
            },
            IO::IO_STATUS_BLOCK,
        },
    },
};

#[path = "windows/security.rs"]
mod security;
pub(super) fn preserve_metadata(source: &File, destination: &File) -> io::Result<()> {
    security::preserve_metadata(source, destination)
}

const SHARE_ALL: u32 = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;

fn nt_result(status: NTSTATUS) -> io::Result<()> {
    if status >= 0 {
        return Ok(());
    }
    // SAFETY: RtlNtStatusToDosError accepts any NTSTATUS value.
    Err(io::Error::from_raw_os_error(
        unsafe { RtlNtStatusToDosError(status) } as i32,
    ))
}

pub(super) fn random(bytes: &mut [u8; 16]) -> io::Result<()> {
    // SAFETY: The buffer is valid for the supplied size; the system RNG
    // requires no provider handle.
    nt_result(unsafe {
        BCryptGenRandom(
            null_mut(),
            bytes.as_mut_ptr(),
            bytes.len() as u32,
            BCRYPT_USE_SYSTEM_PREFERRED_RNG,
        )
    })
}

fn open_relative(
    parent: &File,
    name: &OsStr,
    access: u32,
    share: u32,
    disposition: u32,
    options: u32,
) -> io::Result<File> {
    open_relative_secured(
        parent,
        name,
        access,
        share,
        disposition,
        options,
        null_mut(),
    )
}

fn open_relative_secured(
    parent: &File,
    name: &OsStr,
    access: u32,
    share: u32,
    disposition: u32,
    options: u32,
    descriptor: *mut std::ffi::c_void,
) -> io::Result<File> {
    const OBJ_CASE_INSENSITIVE: u32 = 0x40;
    let mut name: Vec<_> = name.encode_wide().collect();
    let length = u16::try_from(name.len().saturating_mul(size_of::<u16>()))
        .map_err(|_| invalid("relative filename is too long"))?;
    let name = UNICODE_STRING {
        Length: length,
        MaximumLength: length,
        Buffer: name.as_mut_ptr(),
    };
    let attributes = OBJECT_ATTRIBUTES {
        Length: size_of::<OBJECT_ATTRIBUTES>() as u32,
        RootDirectory: parent.as_raw_handle(),
        ObjectName: &name,
        Attributes: OBJ_CASE_INSENSITIVE,
        // A null descriptor lets the filesystem inherit the parent's security.
        SecurityDescriptor: descriptor.cast_const().cast(),
        SecurityQualityOfService: null(),
    };
    let mut handle = null_mut();
    let mut status = IO_STATUS_BLOCK::default();
    // SAFETY: The parent and the validated single-component UTF-16 name stay
    // live. The output buffers are writable and the open is synchronous.
    nt_result(unsafe {
        NtCreateFile(
            &mut handle,
            access | SYNCHRONIZE,
            &attributes,
            &mut status,
            null(),
            FILE_ATTRIBUTE_NORMAL,
            share,
            disposition,
            options | FILE_OPEN_REPARSE_POINT | FILE_SYNCHRONOUS_IO_NONALERT,
            null(),
            0,
        )
    })?;
    // SAFETY: A successful synchronous NtCreateFile returned an owned handle.
    Ok(unsafe { File::from_raw_handle(handle) })
}

fn object_info(file: &File) -> io::Result<BY_HANDLE_FILE_INFORMATION> {
    match read_object_info(file) {
        Err(OpenRetainedError::Symlink) => Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "refusing a reparse point",
        )),
        Err(OpenRetainedError::NotRegular) => {
            Err(invalid("retained handle is not a disk file or directory"))
        }
        Err(OpenRetainedError::Io(error)) => Err(error),
        Err(OpenRetainedError::Hardlinked) => Err(invalid("entry is not a singly linked file")),
        Ok(info) => Ok(info),
    }
}

fn read_object_info(file: &File) -> Result<BY_HANDLE_FILE_INFORMATION, OpenRetainedError> {
    let mut info = BY_HANDLE_FILE_INFORMATION::default();
    // SAFETY: file owns the live handle and info is a writable output buffer.
    unsafe {
        if GetFileType(file.as_raw_handle()) != FILE_TYPE_DISK {
            return Err(OpenRetainedError::NotRegular);
        }
        if GetFileInformationByHandle(file.as_raw_handle(), &mut info) == 0 {
            return Err(io::Error::last_os_error().into());
        }
    }
    if info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT != 0 {
        return Err(OpenRetainedError::Symlink);
    }
    Ok(info)
}

pub(super) fn open_read_at(
    parent: &File,
    name: &OsStr,
    directory: bool,
) -> Result<File, OpenRetainedError> {
    if read_object_info(parent)?.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == 0 {
        return Err(OpenRetainedError::NotRegular);
    }
    let file = open_relative(parent, name, FILE_GENERIC_READ, SHARE_ALL, FILE_OPEN, 0)?;
    let info = read_object_info(&file)?;
    if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0) != directory {
        return Err(OpenRetainedError::NotRegular);
    }
    if !directory && info.nNumberOfLinks != 1 {
        return Err(OpenRetainedError::Hardlinked);
    }
    Ok(file)
}

pub(super) fn ensure_directory(parent: &File) -> io::Result<()> {
    if object_info(parent)?.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == 0 {
        return Err(invalid("retained parent is not a directory"));
    }
    Ok(())
}

pub(super) fn regular_file_at(parent: &File, leaf: &OsStr) -> io::Result<()> {
    let file = open_relative(parent, leaf, FILE_READ_ATTRIBUTES, SHARE_ALL, FILE_OPEN, 0)?;
    if object_info(&file)?.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0 {
        return Err(invalid("expected a regular file"));
    }
    Ok(())
}

pub(super) fn create(
    parent: &File,
    name: &str,
    directory: bool,
    publish: bool,
) -> io::Result<File> {
    let access = FILE_GENERIC_READ | FILE_GENERIC_WRITE | DELETE;
    let (access, share, options) = if directory {
        (
            access,
            FILE_SHARE_READ | FILE_SHARE_WRITE,
            FILE_DIRECTORY_FILE,
        )
    } else if publish {
        (access, 0, 0)
    } else {
        (access | WRITE_DAC | WRITE_OWNER, 0, 0)
    };
    if directory && !publish {
        return crate::windows_acl::with_security(true, |attributes| {
            open_relative_secured(
                parent,
                name.as_ref(),
                access,
                share,
                FILE_CREATE,
                options | FILE_WRITE_THROUGH,
                attributes.lpSecurityDescriptor,
            )
        });
    }
    open_relative(
        parent,
        name.as_ref(),
        access,
        share,
        FILE_CREATE,
        options | FILE_WRITE_THROUGH,
    )
}

pub(super) fn open_target(parent: &File, name: &OsStr) -> io::Result<File> {
    let file = open_relative(
        parent,
        name,
        READ_CONTROL | FILE_READ_ATTRIBUTES,
        SHARE_ALL,
        FILE_OPEN,
        0,
    )?;
    let info = object_info(&file)?;
    if info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0 || info.nNumberOfLinks != 1 {
        return Err(invalid("expected a singly linked regular file"));
    }
    Ok(file)
}

pub(super) fn verify_stage_entry(stage: &Stage<'_>) -> io::Result<()> {
    let info = object_info(&stage.file)?;
    if !stage.directory && info.nNumberOfLinks != 1 {
        return Err(invalid("expected a singly linked staging file"));
    }
    // The source of the rename is the staging handle itself. It does not
    // share deletion, so no other open can rename it out of the directory.
    Ok(())
}

pub(super) fn rename(stage: &Stage<'_>, leaf: &OsStr, replace: bool) -> io::Result<()> {
    rename_open_file(&stage.file, leaf, replace)
}

fn rename_open_file(file: &File, leaf: &OsStr, replace: bool) -> io::Result<()> {
    rename_to(file, None, leaf, replace)
}

fn rename_to(
    file: &File,
    destination: Option<&File>,
    leaf: &OsStr,
    replace: bool,
) -> io::Result<()> {
    let name: Vec<u16> = leaf.encode_wide().collect();
    let name_bytes = name.len() * size_of::<u16>();
    let buffer_bytes =
        (offset_of!(FILE_RENAME_INFO, FileName) + name_bytes).max(size_of::<FILE_RENAME_INFO>());
    let length =
        u32::try_from(buffer_bytes).map_err(|_| invalid("relative filename is too long"))?;
    let mut buffer = vec![0usize; buffer_bytes.div_ceil(size_of::<usize>())];
    let info = buffer.as_mut_ptr().cast::<FILE_RENAME_INFO>();
    let mut status = IO_STATUS_BLOCK::default();
    // SAFETY: The aligned buffer contains the whole variable-length NT rename
    // structure. Both handles and the buffer live throughout each synchronous
    // call. A null RootDirectory with a bare filename renames the source
    // within its current directory. The source denies delete sharing. For a
    // move to a different directory, RootDirectory is the retained destination
    // handle, and the filename remains a single component.
    unsafe {
        if let Some(directory) = destination {
            (*info).RootDirectory = directory.as_raw_handle();
        }
        (*info).FileNameLength = name_bytes as u32;
        std::ptr::copy_nonoverlapping(name.as_ptr(), (*info).FileName.as_mut_ptr(), name.len());
        if replace {
            (*info).Anonymous.Flags =
                FILE_RENAME_FLAG_REPLACE_IF_EXISTS | FILE_RENAME_FLAG_POSIX_SEMANTICS;
            match nt_result(NtSetInformationFile(
                file.as_raw_handle(),
                &mut status,
                info.cast(),
                length,
                FileRenameInformationEx,
            )) {
                Ok(()) => return Ok(()),
                Err(error)
                    if matches!(
                        error.raw_os_error().map(|code| code as u32),
                        Some(
                            ERROR_INVALID_PARAMETER | ERROR_NOT_SUPPORTED | ERROR_INVALID_FUNCTION
                        )
                    ) => {}
                Err(error) => return Err(error),
            }
        }
        // The legacy information class still renames atomically by handle,
        // but can refuse replacement when the target has open readers.
        (*info).Anonymous.Flags = u32::from(replace);
        nt_result(NtSetInformationFile(
            file.as_raw_handle(),
            &mut status,
            info.cast(),
            length,
            FileRenameInformation,
        ))
    }
}

pub(super) fn move_aside(parent: &File, leaf: &OsStr, aside: &OsStr) -> io::Result<File> {
    let file = open_relative(
        parent,
        leaf,
        FILE_GENERIC_READ | DELETE,
        FILE_SHARE_READ,
        FILE_OPEN,
        0,
    )?;
    let info = object_info(&file)?;
    if info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0 || info.nNumberOfLinks != 1 {
        return Err(invalid("expected a singly linked regular file"));
    }
    rename_open_file(&file, aside, false)?;
    Ok(file)
}

pub(super) fn put_back(moved: &super::RetainedMovedFile<'_>, leaf: &OsStr) -> io::Result<()> {
    ensure_directory(moved.parent)?;
    rename_open_file(&moved.file, leaf, false)
}

pub(super) fn relocate(
    moved: &super::RetainedMovedFile<'_>,
    dest: &File,
    leaf: &OsStr,
) -> io::Result<()> {
    let directory = (dest.as_raw_handle() != moved.parent.as_raw_handle()).then_some(dest);
    rename_to(&moved.file, directory, leaf, false)
}

pub(super) fn directory_path(parent: &File) -> io::Result<std::path::PathBuf> {
    use std::{ffi::OsString, os::windows::ffi::OsStringExt};
    use windows_sys::Win32::Storage::FileSystem::GetFinalPathNameByHandleW;
    let mut buffer = vec![0u16; 32768];
    // SAFETY: the directory handle is borrowed and the buffer is writable.
    let len = unsafe {
        GetFinalPathNameByHandleW(
            parent.as_raw_handle(),
            buffer.as_mut_ptr(),
            buffer.len() as u32,
            0,
        )
    } as usize;
    if len == 0 {
        return Err(io::Error::last_os_error());
    }
    if len >= buffer.len() {
        return Err(invalid("directory path is too long"));
    }
    Ok(std::path::PathBuf::from(OsString::from_wide(
        &buffer[..len],
    )))
}

pub(super) fn verify_private_directory(parent: &File) -> io::Result<()> {
    ensure_directory(parent)?;
    crate::windows_acl::verify_private_dir_handle(parent)
}

pub(super) fn discard_moved(
    moved: &super::RetainedMovedFile<'_>,
    inspected: crate::RetainedFileIdentity,
) -> io::Result<()> {
    if crate::retained_file_identity(&moved.file)? != inspected {
        return Err(invalid("recovery handle identity changed"));
    }
    remove_by_handle(&moved.file)
}

pub(super) fn retry<T>(mut operation: impl FnMut() -> io::Result<T>) -> io::Result<T> {
    for delay in [5, 10, 20, 40, 80] {
        match operation() {
            Err(error) if error.raw_os_error() == Some(ERROR_SHARING_VIOLATION as i32) => {
                thread::sleep(Duration::from_millis(delay));
            }
            result => return result,
        }
    }
    operation()
}

pub(super) fn flush_staging(file: &File, directory: bool) -> io::Result<()> {
    if directory {
        // The empty directory was created with FILE_WRITE_THROUGH.
        Ok(())
    } else {
        file.sync_all()
    }
}

pub(super) fn flush_publication(stage: &Stage<'_>) -> io::Result<()> {
    // Flush the exact renamed object opened with FILE_WRITE_THROUGH, not a
    // lookup of the final path. Windows provides no POSIX directory fsync.
    stage.file.sync_all()
}

pub(super) fn remove_stage(stage: &Stage<'_>) -> io::Result<()> {
    remove_by_handle(&stage.file)
}

fn remove_by_handle(file: &File) -> io::Result<()> {
    let disposition = FILE_DISPOSITION_INFO { DeleteFile: true };
    // SAFETY: The handle is the exact staging object opened with DELETE. The
    // input structure is valid and live through this synchronous call.
    if unsafe {
        SetFileInformationByHandle(
            file.as_raw_handle(),
            FileDispositionInfo,
            (&disposition as *const FILE_DISPOSITION_INFO).cast(),
            size_of::<FILE_DISPOSITION_INFO>() as u32,
        )
    } == 0
    {
        Err(io::Error::last_os_error())
    } else {
        Ok(())
    }
}