Skip to content

Commit 97ec53f

Browse files
fix(procmgr): read TOKEN_USER with read_unaligned on Windows
GetTokenInformation fills a byte buffer that may not be aligned for TOKEN_USER. Use ptr::read_unaligned instead of dereferencing a cast pointer to avoid undefined behavior when resolving runtime_user.
1 parent 5a10a77 commit 97ec53f

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

pkg/procmgr/rust/src/platform/windows/runtime_user.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,20 @@ fn token_user_sid(token: &TokenHandle) -> Result<Vec<u8>> {
7474
bail!("GetTokenInformation: {}", std::io::Error::last_os_error());
7575
}
7676

77-
let token_user = &*(buffer.as_ptr() as *const TOKEN_USER);
78-
if token_user.User.Sid.is_null() {
77+
// GetTokenInformation writes into an arbitrary byte buffer; read_unaligned
78+
// avoids UB from casting Vec<u8> to &TOKEN_USER.
79+
let token_user = ptr::read_unaligned(buffer.as_ptr().cast::<TOKEN_USER>());
80+
let sid_ptr = token_user.User.Sid;
81+
if sid_ptr.is_null() {
7982
bail!("TokenUser SID is null");
8083
}
8184

82-
let sid_len = GetLengthSid(token_user.User.Sid);
85+
let sid_len = GetLengthSid(sid_ptr);
8386
if sid_len == 0 {
8487
bail!("GetLengthSid returned 0");
8588
}
8689
let mut sid = vec![0u8; sid_len as usize];
87-
std::ptr::copy_nonoverlapping(
88-
token_user.User.Sid as *const u8,
89-
sid.as_mut_ptr(),
90-
sid_len as usize,
91-
);
90+
std::ptr::copy_nonoverlapping(sid_ptr as *const u8, sid.as_mut_ptr(), sid_len as usize);
9291
Ok(sid)
9392
}
9493
}

0 commit comments

Comments
 (0)