Skip to content

Commit 21900bd

Browse files
authored
net: add security flags to named pipe ServerOptions (#4845)
1 parent 228d4fc commit 21900bd

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

tokio/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ net = [
5757
"winapi/winbase",
5858
"winapi/winnt",
5959
"winapi/minwindef",
60+
"winapi/accctrl",
61+
"winapi/aclapi"
6062
]
6163
process = [
6264
"bytes",

tokio/src/net/windows/named_pipe.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,106 @@ impl ServerOptions {
19551955
self
19561956
}
19571957

1958+
/// Requests permission to modify the pipe's discretionary access control list.
1959+
///
1960+
/// This corresponds to setting [`WRITE_DAC`] in dwOpenMode.
1961+
///
1962+
/// # Examples
1963+
///
1964+
/// ```
1965+
/// use std::{io, os::windows::prelude::AsRawHandle, ptr};
1966+
//
1967+
/// use tokio::net::windows::named_pipe::ServerOptions;
1968+
/// use winapi::{
1969+
/// shared::winerror::ERROR_SUCCESS,
1970+
/// um::{accctrl::SE_KERNEL_OBJECT, aclapi::SetSecurityInfo, winnt::DACL_SECURITY_INFORMATION},
1971+
/// };
1972+
///
1973+
/// const PIPE_NAME: &str = r"\\.\pipe\write_dac_pipe";
1974+
///
1975+
/// # #[tokio::main] async fn main() -> io::Result<()> {
1976+
/// let mut pipe_template = ServerOptions::new();
1977+
/// pipe_template.write_dac(true);
1978+
/// let pipe = pipe_template.create(PIPE_NAME)?;
1979+
///
1980+
/// unsafe {
1981+
/// assert_eq!(
1982+
/// ERROR_SUCCESS,
1983+
/// SetSecurityInfo(
1984+
/// pipe.as_raw_handle(),
1985+
/// SE_KERNEL_OBJECT,
1986+
/// DACL_SECURITY_INFORMATION,
1987+
/// ptr::null_mut(),
1988+
/// ptr::null_mut(),
1989+
/// ptr::null_mut(),
1990+
/// ptr::null_mut(),
1991+
/// )
1992+
/// );
1993+
/// }
1994+
///
1995+
/// # Ok(()) }
1996+
/// ```
1997+
///
1998+
/// ```
1999+
/// use std::{io, os::windows::prelude::AsRawHandle, ptr};
2000+
//
2001+
/// use tokio::net::windows::named_pipe::ServerOptions;
2002+
/// use winapi::{
2003+
/// shared::winerror::ERROR_ACCESS_DENIED,
2004+
/// um::{accctrl::SE_KERNEL_OBJECT, aclapi::SetSecurityInfo, winnt::DACL_SECURITY_INFORMATION},
2005+
/// };
2006+
///
2007+
/// const PIPE_NAME: &str = r"\\.\pipe\write_dac_pipe_fail";
2008+
///
2009+
/// # #[tokio::main] async fn main() -> io::Result<()> {
2010+
/// let mut pipe_template = ServerOptions::new();
2011+
/// pipe_template.write_dac(false);
2012+
/// let pipe = pipe_template.create(PIPE_NAME)?;
2013+
///
2014+
/// unsafe {
2015+
/// assert_eq!(
2016+
/// ERROR_ACCESS_DENIED,
2017+
/// SetSecurityInfo(
2018+
/// pipe.as_raw_handle(),
2019+
/// SE_KERNEL_OBJECT,
2020+
/// DACL_SECURITY_INFORMATION,
2021+
/// ptr::null_mut(),
2022+
/// ptr::null_mut(),
2023+
/// ptr::null_mut(),
2024+
/// ptr::null_mut(),
2025+
/// )
2026+
/// );
2027+
/// }
2028+
///
2029+
/// # Ok(()) }
2030+
/// ```
2031+
///
2032+
/// [`WRITE_DAC`]: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createnamedpipea
2033+
pub fn write_dac(&mut self, requested: bool) -> &mut Self {
2034+
bool_flag!(self.open_mode, requested, winnt::WRITE_DAC);
2035+
self
2036+
}
2037+
2038+
/// Requests permission to modify the pipe's owner.
2039+
///
2040+
/// This corresponds to setting [`WRITE_OWNER`] in dwOpenMode.
2041+
///
2042+
/// [`WRITE_OWNER`]: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createnamedpipea
2043+
pub fn write_owner(&mut self, requested: bool) -> &mut Self {
2044+
bool_flag!(self.open_mode, requested, winnt::WRITE_OWNER);
2045+
self
2046+
}
2047+
2048+
/// Requests permission to modify the pipe's system access control list.
2049+
///
2050+
/// This corresponds to setting [`ACCESS_SYSTEM_SECURITY`] in dwOpenMode.
2051+
///
2052+
/// [`ACCESS_SYSTEM_SECURITY`]: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createnamedpipea
2053+
pub fn access_system_security(&mut self, requested: bool) -> &mut Self {
2054+
bool_flag!(self.open_mode, requested, winnt::ACCESS_SYSTEM_SECURITY);
2055+
self
2056+
}
2057+
19582058
/// Indicates whether this server can accept remote clients or not. Remote
19592059
/// clients are disabled by default.
19602060
///

0 commit comments

Comments
 (0)