Skip to content

Commit 8525b5a

Browse files
committed
fixup: memory leak
1 parent e685318 commit 8525b5a

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

talpid-windows/src/fs.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
use core::ffi::c_void;
12
use std::io;
23
use std::os::windows::io::AsRawHandle;
34
use std::ptr;
45

56
use windows_sys::Win32::{
6-
Foundation::ERROR_SUCCESS,
7+
Foundation::{LocalFree, ERROR_SUCCESS},
78
Security::{
89
Authorization::{GetSecurityInfo, SE_FILE_OBJECT},
910
IsWellKnownSid, WinBuiltinAdministratorsSid, WinLocalSystemSid, OWNER_SECURITY_INFORMATION,
10-
SID,
11+
SECURITY_DESCRIPTOR, SID,
1112
},
1213
};
1314

1415
/// Return whether a file handle is owned by either SYSTEM or the built-in administrators account
1516
pub fn is_admin_owned<T: AsRawHandle>(handle: T) -> io::Result<bool> {
17+
let mut security_descriptor: *mut SECURITY_DESCRIPTOR = ptr::null_mut();
1618
let mut owner: *mut SID = ptr::null_mut();
1719

1820
// SAFETY: `handle` is a valid handle. We return a pointer to the owner associated with the handle(?)
@@ -21,22 +23,25 @@ pub fn is_admin_owned<T: AsRawHandle>(handle: T) -> io::Result<bool> {
2123
handle.as_raw_handle() as isize,
2224
SE_FILE_OBJECT,
2325
OWNER_SECURITY_INFORMATION,
24-
(&mut owner) as *mut *mut SID as _,
25-
ptr::null_mut(),
26+
(&mut owner) as *mut *mut SID as *mut *mut c_void,
2627
ptr::null_mut(),
2728
ptr::null_mut(),
2829
ptr::null_mut(),
30+
(&mut security_descriptor) as *mut *mut SECURITY_DESCRIPTOR as *mut *mut c_void,
2931
)
3032
};
3133

3234
if result != ERROR_SUCCESS {
3335
return Err(io::Error::from_raw_os_error(result as i32));
3436
}
3537

36-
Ok(
37-
// SAFETY: `owner` is valid, and the well-known type is a valid argument
38-
unsafe { IsWellKnownSid(owner as _, WinBuiltinAdministratorsSid) != 0 } ||
39-
// SAFETY: `owner` is valid, and the well-known type is a valid argument
40-
unsafe { IsWellKnownSid(owner as _, WinLocalSystemSid) != 0 },
41-
)
38+
// SAFETY: `owner` is valid since `security_descriptor` still is, and the well-known type is a valid argument
39+
let is_system_owned = unsafe { IsWellKnownSid(owner as _, WinLocalSystemSid) != 0 };
40+
// SAFETY: `owner` is valid since `security_descriptor` still is, and the well-known type is a valid argument
41+
let is_admin_owned = unsafe { IsWellKnownSid(owner as _, WinBuiltinAdministratorsSid) != 0 };
42+
43+
// SAFETY: Since we no longer need the descriptor (or owner), it may be freed
44+
unsafe { LocalFree(security_descriptor.cast()) };
45+
46+
Ok(is_system_owned || is_admin_owned)
4247
}

0 commit comments

Comments
 (0)