Skip to content

Commit fab272e

Browse files
committed
Auto merge of #58216 - pitdicker:sqos_flags, r=alexcrichton
Set secure flags when opening a named pipe on Windows Fixes #42036, see also the previous attempt in #44556. Whether this is correct depends on if it is somehow possible to create a symlink to a named pipe, outside the named pipe filesystem (NPFS). But as far as I can tell that should be impossible. Also fixes that `security_qos_flags(SECURITY_ANONYMOUS)` does not set the `SECURITY_SQOS_PRESENT` flag, and the incorrect documentation about the default value of `security_qos_flags`.
2 parents 9cfed5d + 089524c commit fab272e

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/libstd/sys/windows/ext/fs.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,27 @@ pub trait OpenOptionsExt {
220220
/// the specified value (or combines it with `custom_flags` and `attributes`
221221
/// to set the `dwFlagsAndAttributes` for [`CreateFile`]).
222222
///
223-
/// By default, `security_qos_flags` is set to `SECURITY_ANONYMOUS`. For
224-
/// information about possible values, see [Impersonation Levels] on the
225-
/// Windows Dev Center site.
226-
///
223+
/// By default `security_qos_flags` is not set. It should be specified when
224+
/// opening a named pipe, to control to which degree a server process can
225+
/// act on behalf of a client process (security impersonation level).
226+
///
227+
/// When `security_qos_flags` is not set a malicious program can gain the
228+
/// elevated privileges of a privileged Rust process when it allows opening
229+
/// user-specified paths, by tricking it into opening a named pipe. So
230+
/// arguably `security_qos_flags` should also be set when opening arbitrary
231+
/// paths. However the bits can then conflict with other flags, specifically
232+
/// `FILE_FLAG_OPEN_NO_RECALL`.
233+
///
234+
/// For information about possible values, see [Impersonation Levels] on the
235+
/// Windows Dev Center site. The `SECURITY_SQOS_PRESENT` flag is set
236+
/// automatically when using this method.
237+
227238
/// # Examples
228239
///
229240
/// ```no_run
241+
/// # #[cfg(for_demonstration_only)]
242+
/// extern crate winapi;
243+
/// # mod winapi { pub const SECURITY_IDENTIFICATION: u32 = 0; }
230244
/// use std::fs::OpenOptions;
231245
/// use std::os::windows::prelude::*;
232246
///
@@ -235,9 +249,9 @@ pub trait OpenOptionsExt {
235249
/// .create(true)
236250
///
237251
/// // Sets the flag value to `SecurityIdentification`.
238-
/// .security_qos_flags(1)
252+
/// .security_qos_flags(winapi::SECURITY_IDENTIFICATION)
239253
///
240-
/// .open("foo.txt");
254+
/// .open(r"\\.\pipe\MyPipe");
241255
/// ```
242256
///
243257
/// [`CreateFile`]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858.aspx

src/libstd/sys/windows/fs.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ impl OpenOptions {
191191
pub fn access_mode(&mut self, access_mode: u32) { self.access_mode = Some(access_mode); }
192192
pub fn share_mode(&mut self, share_mode: u32) { self.share_mode = share_mode; }
193193
pub fn attributes(&mut self, attrs: u32) { self.attributes = attrs; }
194-
pub fn security_qos_flags(&mut self, flags: u32) { self.security_qos_flags = flags; }
194+
pub fn security_qos_flags(&mut self, flags: u32) {
195+
// We have to set `SECURITY_SQOS_PRESENT` here, because one of the valid flags we can
196+
// receive is `SECURITY_ANONYMOUS = 0x0`, which we can't check for later on.
197+
self.security_qos_flags = flags | c::SECURITY_SQOS_PRESENT;
198+
}
195199
pub fn security_attributes(&mut self, attrs: c::LPSECURITY_ATTRIBUTES) {
196200
self.security_attributes = attrs as usize;
197201
}
@@ -239,7 +243,6 @@ impl OpenOptions {
239243
self.custom_flags |
240244
self.attributes |
241245
self.security_qos_flags |
242-
if self.security_qos_flags != 0 { c::SECURITY_SQOS_PRESENT } else { 0 } |
243246
if self.create_new { c::FILE_FLAG_OPEN_REPARSE_POINT } else { 0 }
244247
}
245248
}

0 commit comments

Comments
 (0)