Skip to content

Conversation

@winocreative
Copy link

@winocreative winocreative commented May 23, 2025

Adds launch-protection support to the crate.
A new ServiceLaunchProtected enum (wrapping the four SERVICE_LAUNCH_PROTECTED_* constants) is introduced together with the public helpers Service::set_launch_protected and Service::get_launch_protected, which use SERVICE_CONFIG_LAUNCH_PROTECTED internally. The change is purely additive and does not modify existing APIs.


This change is Reviewable

Copy link
Contributor

@kkent030315 kkent030315 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this great patch. I am also waiting this PR to be merged!

/// for more information.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum ServiceLaunchProtected {
Copy link
Contributor

@kkent030315 kkent030315 Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like they are inserting newline after each field so it's better to follow in this enum. Also, you could add doc comments for each field.

Suggested change
pub enum ServiceLaunchProtected {
pub enum ServiceLaunchProtected {
/// No launch protection. The service can be modified or replaced without restriction.
None = Services::SERVICE_LAUNCH_PROTECTED_NONE,
/// Launch protection for Windows components.
Windows = Services::SERVICE_LAUNCH_PROTECTED_WINDOWS,
/// A lighter version of Windows launch protection.
WindowsLight = Services::SERVICE_LAUNCH_PROTECTED_WINDOWS_LIGHT,
/// Launch protection used by antimalware (ELAM) services.
AntimalwareLight = Services::SERVICE_LAUNCH_PROTECTED_ANTIMALWARE_LIGHT,
}

src/service.rs Outdated
*self as u32
}

pub fn from_raw(raw: u32) -> crate::Result<ServiceLaunchProtected> {
Copy link
Contributor

@kkent030315 kkent030315 Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can turn this whole function into TryFrom trait impl.

impl TryFrom<u32> for ServiceLaunchProtected {
    type Error = Error;

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        match value {
            Services::SERVICE_LAUNCH_PROTECTED_NONE => Ok(ServiceLaunchProtected::None),
            Services::SERVICE_LAUNCH_PROTECTED_WINDOWS => Ok(ServiceLaunchProtected::Windows),
            Services::SERVICE_LAUNCH_PROTECTED_WINDOWS_LIGHT => {
                Ok(ServiceLaunchProtected::WindowsLight)
            }
            Services::SERVICE_LAUNCH_PROTECTED_ANTIMALWARE_LIGHT => {
                Ok(ServiceLaunchProtected::AntimalwareLight)
            }
            _ => Err(Error::ParseValue(
                "Invalid launch protection value",
                ParseRawError::InvalidInteger(value),
            )),
        }
    }
}

src/service.rs Outdated
/// Get service launch protection.
/// This is a security feature that allows the service to run in a more secure environment.
pub fn get_launch_protected(&self) -> crate::Result<ServiceLaunchProtected> {
let mut data = vec![0u8; std::mem::size_of::<Services::SERVICE_LAUNCH_PROTECTED_INFO>()];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No allocation is actually required here since the size is known at compile-time. You can remove vec! so that it forces data to be on a stack.

Suggested change
let mut data = vec![0u8; std::mem::size_of::<Services::SERVICE_LAUNCH_PROTECTED_INFO>()];
let mut data = [0u8; std::mem::size_of::<Services::SERVICE_LAUNCH_PROTECTED_INFO>()];

)
.map_err(Error::Winapi)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put an newline between function definitions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants