Skip to content

Commit cdcf7b9

Browse files
committed
see if this makes a difference on windows (#386)
1 parent d6c6ec6 commit cdcf7b9

File tree

2 files changed

+38
-43
lines changed

2 files changed

+38
-43
lines changed

git-sec/src/lib.rs

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ pub mod identity {
5252
use std::borrow::Cow;
5353
use std::path::Path;
5454

55-
fn err(msg: &str) -> std::io::Error {
56-
std::io::Error::new(std::io::ErrorKind::Other, msg)
55+
fn err(msg: impl Into<String>) -> std::io::Error {
56+
std::io::Error::new(std::io::ErrorKind::Other, msg.into())
5757
}
5858

5959
pub fn is_path_owned_by_current_user(path: Cow<'_, Path>) -> std::io::Result<bool> {
@@ -75,54 +75,46 @@ pub mod identity {
7575
.map_err(|_| err("Failed to open process token"))?;
7676

7777
let mut len = 0_u32;
78-
if Security::GetTokenInformation(&handle, Security::TokenUser, std::ptr::null_mut(), 0, &mut len)
79-
.as_bool()
78+
let mut info = Security::TOKEN_USER::default();
79+
if Security::GetTokenInformation(
80+
handle,
81+
Security::TokenUser,
82+
&mut info as *mut _ as *mut std::ffi::c_void,
83+
std::mem::size_of_val(&info) as u32,
84+
&mut len,
85+
)
86+
.as_bool()
8087
{
81-
let mut token_user = Security::TOKEN_USER::default();
82-
if Security::GetTokenInformation(
83-
&handle,
84-
Security::TokenUser,
85-
&mut token_user as *mut _ as *mut std::ffi::c_void,
86-
len,
87-
&mut len,
88-
)
89-
.as_bool()
90-
{
91-
// NOTE: we avoid to copy the sid or cache it in any way for now, even though it should be possible
92-
// with a custom allocation/vec/box and it's just very raw. Can the `windows` crate do better?
93-
// When/If yes, then let's improve this.
94-
if Security::IsValidSid(token_user.User.Sid).as_bool() {
95-
use std::os::windows::ffi::OsStrExt;
96-
let mut wide_path: Vec<_> = path.as_ref().as_os_str().encode_wide().collect();
97-
// err = GetNamedSecurityInfoW(wpath, SE_FILE_OBJECT,
98-
// OWNER_SECURITY_INFORMATION |
99-
// DACL_SECURITY_INFORMATION,
100-
// &sid, NULL, NULL, NULL, &descriptor);
101-
let mut path_sid = PSID::default();
102-
let res = Security::Authorization::GetNamedSecurityInfoW(
103-
windows::core::PCWSTR(wide_path.as_mut_ptr()),
104-
SE_FILE_OBJECT,
105-
Security::OWNER_SECURITY_INFORMATION | Security::DACL_SECURITY_INFORMATION,
106-
&mut path_sid,
107-
std::ptr::null_mut(),
108-
std::ptr::null_mut(),
109-
std::ptr::null_mut(),
110-
&mut descriptor,
111-
);
88+
// NOTE: we avoid to copy the sid or cache it in any way for now, even though it should be possible
89+
// with a custom allocation/vec/box and it's just very raw. Can the `windows` crate do better?
90+
// When/If yes, then let's improve this.
91+
if Security::IsValidSid(info.User.Sid).as_bool() {
92+
use std::os::windows::ffi::OsStrExt;
93+
let mut wide_path: Vec<_> = path.as_ref().as_os_str().encode_wide().collect();
94+
wide_path.push(0);
95+
let mut path_sid = PSID::default();
96+
let res = Security::Authorization::GetNamedSecurityInfoW(
97+
windows::core::PCWSTR(wide_path.as_ptr()),
98+
SE_FILE_OBJECT,
99+
Security::OWNER_SECURITY_INFORMATION | Security::DACL_SECURITY_INFORMATION,
100+
&mut path_sid,
101+
std::ptr::null_mut(),
102+
std::ptr::null_mut(),
103+
std::ptr::null_mut(),
104+
&mut descriptor,
105+
);
112106

113-
if res == ERROR_SUCCESS.0 && Security::IsValidSid(path_sid).as_bool() {
114-
is_owned = Security::EqualSid(path_sid, token_user.User.Sid).as_bool();
115-
} else {
116-
err_msg = "couldn't get owner for path or it wasn't valid".into();
117-
}
107+
if res == ERROR_SUCCESS.0 && Security::IsValidSid(path_sid).as_bool() {
108+
is_owned = Security::EqualSid(path_sid, info.User.Sid).as_bool();
109+
dbg!(is_owned, path.as_ref());
118110
} else {
119-
err_msg = "owner id of current process wasn't set or valid".into();
111+
err_msg = format!("couldn't get owner for path or it wasn't valid: {}", res).into();
120112
}
121113
} else {
122-
err_msg = "Could not get information about the token user".into();
114+
err_msg = String::from("owner id of current process wasn't set or valid").into();
123115
}
124116
} else {
125-
err_msg = "Could not get token information for length of token user".into();
117+
err_msg = String::from("Could not get information about the token user").into();
126118
}
127119
CloseHandle(handle);
128120
if !descriptor.is_invalid() {

git-sec/tests/identity/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ mod uid {
22
#[test]
33
fn from_path() -> crate::Result {
44
let dir = tempfile::tempdir()?;
5+
let file = dir.path().join("file");
6+
std::fs::write(&file, &[])?;
7+
assert!(git_sec::identity::is_path_owned_by_current_user(file.into())?);
58
assert!(git_sec::identity::is_path_owned_by_current_user(dir.path().into())?);
69
Ok(())
710
}

0 commit comments

Comments
 (0)