machutils

package
v0.0.0-...-972087a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Package cloudeng.io/macos/machutils

import cloudeng.io/macos/machutils

Package machutils provides low level utilities for interacting with the macOS kernel.

Constants

AppSandboxEntitlement
AppSandboxEntitlement = "com.apple.security.app-sandbox"

AppSandboxEntitlement is the entitlement that places a process in the macOS App Sandbox.

Variables

ErrFailedToRetrieveEntitlements
ErrFailedToRetrieveEntitlements = errors.New("failed to retrieve code signing entitlements from the kernel")

ErrFailedToRetrieveEntitlements is returned when the code signing entitlements of the running process cannot be retrieved from the kernel.

ErrFailedToRetrieveExecutablePath
ErrFailedToRetrieveExecutablePath = errors.
	New(
		"failed to retrieve the executable path from the kernel",
	)

ErrFailedToRetrieveExecutablePath is returned when the path of the running executable cannot be retrieved from the kernel.

ErrFailedToRetrieveParentUID
ErrFailedToRetrieveParentUID = errors.New("failed to retrieve parent process UID from the kernel")

ErrFailedToRetrieveParentUID is returned when the parent process UID cannot be retrieved from the kernel.

ErrFailedToRetrievePeerPID
ErrFailedToRetrievePeerPID = errors.New("failed to retrieve peer process ID from the kernel")

ErrFailedToRetrievePeerPID is returned when the process ID of the peer of a Unix domain socket connection cannot be retrieved from the kernel.

ErrFileCodeSignatureInvalid
ErrFileCodeSignatureInvalid = errors.New("code signature of file is not valid or does not satisfy the requirement")

ErrFileCodeSignatureInvalid is returned by VerifyFileCodeSignature or Requirement.VerifyFile when the code at the given path cannot be read, is unsigned, has been modified since it was signed, or does not satisfy the given requirement.

ErrInvalidRequirement
ErrInvalidRequirement = errors.New("invalid code signing requirement")

ErrInvalidRequirement is returned when a code signing requirement string cannot be compiled, e.g. because of a syntax error or invalid encoding.

ErrNoSelfRequirement
ErrNoSelfRequirement = errors.New("could not determine this process's own designated code signing requirement")

ErrNoSelfRequirement is returned by SelfRequirementString when the running binary's own designated code signing requirement cannot be determined, typically because it is unsigned.

ErrPeerCodeSignatureInvalid
ErrPeerCodeSignatureInvalid = errors.New("peer code signature is not valid or does not satisfy the requirement")

ErrPeerCodeSignatureInvalid is returned by VerifyPeerCodeSignature or Requirement.Verify when the peer either cannot be identified, is no longer running, or does not satisfy the given requirement.

Functions

Func EnsureParentProcessSafe
func EnsureParentProcessSafe() error

EnsureParentProcessSafe checks that:

  1. the parent process UID matches the executable's UID
  2. the current process UID matches that of the parent
  3. the executable is not group- or world-writable or executable
  4. the current process is not running with elevated privileges (SUID/SGID)
  5. the process has not been orphaned (reparented to launchd)

1 ensures that only the executable's owner can launch it, 2 ensures that the current process UID matches that of the executable owner, 3 ensures that the executable cannot be modified or run by other users, 4 ensures that the process has not escalated privileges, and 5 ensures that parent identity cannot be spoofed via orphaning.

Func Entitlements
func Entitlements() ([]byte, error)

Entitlements returns the code signing entitlements of the running executable, as the property list recorded in its signature, or nil if it was signed without any.

Only the property list form of the entitlements is consulted. A signature that carries them solely in their DER form, which the kernel keeps separately, is reported here as having none.

Func ExecutablePath
func ExecutablePath() (string, error)

ExecutablePath returns the path of the binary that this process is running, as the kernel records it for the executing file.

It exists because os.Executable does not answer that question on macOS. What os.Executable reports is the path the process was launched with, made absolute against the working directory when it is relative. That names the symbolic link when a process is started through one, and for a process in an App Sandbox it can name a path that does not exist at all, the recorded path having been joined to the container's redirected home directory rather than to the directory the binary occupies. Asking the kernel avoids both: the path returned is that of the file being executed, with symbolic links already resolved.

Func GetExecutableInfo
func GetExecutableInfo() (uint32, os.FileInfo, error)

GetExecutableInfo retrieves the owner UID and file info of the executable.

The executable is identified by ExecutablePath rather than by os.Executable, so that what is examined is the binary the kernel is running rather than the path it was launched through. The two differ for a process started through a symbolic link, and for one in an App Sandbox the latter can name a path that does not exist.

Func GetParentUID
func GetParentUID() (uint32, error)

GetParentUID retrieves the real user ID (RUID) of the parent process.

Func IsSandboxed
func IsSandboxed() (bool, error)

IsSandboxed reports whether the running executable is confined by the macOS App Sandbox, which it determines from the com.apple.security.app-sandbox entitlement in its code signature.

That entitlement is what places a process in the App Sandbox: it is applied by the kernel when the process is executed, and a process carrying it with no container to run in is killed rather than run unconfined. Its presence is therefore conclusive. App extensions, Safari web extensions among them, are always signed with it.

It says nothing about the other ways a process can be confined on macOS. A profile applied by sandbox_init, or by being launched under sandbox-exec, leaves no trace in the code signature and is not reported here.

Func PeerPID
func PeerPID(conn *net.UnixConn) (int32, error)

PeerPID returns the process ID of the process on the other end of conn, a Unix domain socket connection, via the LOCAL_PEEREPID socket option: a public, documented macOS extension to getsockopt (see unix(4)), unlike the csops interface the rest of this package otherwise resorts to, so no cgo is needed here.

The PID alone proves nothing about which binary is running as that process; pair it with VerifyPeerCodeSignature to check that.

Func SelfRequirementString
func SelfRequirementString() (string, error)

SelfRequirementString returns the running binary's own designated code signing requirement, in the same requirement-string language VerifyPeerCodeSignature takes. One use for it: a process can hand its own requirement string to a child process it spawns, so that child can verify the parent back over the same connection, making the check mutual rather than one-directional.

Func VerifyFileCodeSignature
func VerifyFileCodeSignature(path, requirement string) error

VerifyFileCodeSignature verifies that the code at path, an executable or a bundle, is validly signed, has not been modified since it was signed, and satisfies requirement, a code signing requirement string as for VerifyPeerCodeSignature. Code nested within a bundle is verified too.

It is the check to make before executing a binary that must be trusted, since checking the process once it is running, with VerifyPeerCodeSignature, is too late to keep untrusted code from running. The file can still be replaced between this check and its execution, so a caller that can should also verify the running process, e.g. with VerifyPeerCodeSignature over a socketpair shared with it.

Func VerifyPeerCodeSignature
func VerifyPeerCodeSignature(conn *net.UnixConn, requirement string) error

VerifyPeerCodeSignature verifies that the process connected via conn, a Unix domain socket connection, is a currently valid, unmodified binary satisfying requirement: a code signing requirement string in the language codesign and Xcode use (see man csreq), typically naming a Team ID and/or bundle identifier, e.g.

anchor apple generic and certificate leaf[subject.OU] = "TEAMID" and identifier "ai.onyourbehalf.router-helper"

Filesystem or socket permissions on their own only prove that the connecting process is allowed to reach the socket (e.g. membership of the same App Group container); they say nothing about which binary is on the other end of it. This is the additional check needed to prove that it is a specific, currently valid, signed binary, using the same mechanism SecCodeCheckValidity applies to the calling process, applied instead to the peer of a local socket connection.

On modern macOS systems, peer verification retrieves the peer's kernel audit token (via LOCAL_PEERTOKEN), which includes the task generation ID (pidversion). This binds verification directly to the connecting process instance, eliminating the PID recycling race condition. If audit tokens are unavailable, verification falls back to the peer PID.

A caller should perform this check immediately after accepting conn and before reading or acting on anything it sends, closing conn without further use if it returns an error.

Types

Type Requirement
type Requirement struct {
	// contains filtered or unexported fields
}

Requirement represents a compiled macOS code signing requirement. It can be safely reused across multiple connections for efficient verification.

Functions
func NewRequirement(requirement string) (*Requirement, error)

NewRequirement parses and compiles a code signing requirement string. It returns ErrInvalidRequirement if the requirement string is invalid.

Methods
func (r *Requirement) Close()

Close releases the underlying SecRequirementRef.

func (r *Requirement) String() string

String returns the requirement string from which r was compiled.

func (r *Requirement) Verify(conn *net.UnixConn) error

Verify verifies that the process connected via conn satisfies this requirement.

func (r *Requirement) VerifyFile(path string) error

VerifyFile verifies that the code at path, an executable or a bundle, is validly signed, unmodified on disk, and satisfies this requirement.

Documentation

Rendered for darwin/amd64

Overview

Package machutils provides low level utilities for interacting with the macOS kernel.

Index

Constants

View Source
const AppSandboxEntitlement = "com.apple.security.app-sandbox"

AppSandboxEntitlement is the entitlement that places a process in the macOS App Sandbox.

Variables

View Source
var ErrFailedToRetrieveEntitlements = errors.New("failed to retrieve code signing entitlements from the kernel")

ErrFailedToRetrieveEntitlements is returned when the code signing entitlements of the running process cannot be retrieved from the kernel.

View Source
var ErrFailedToRetrieveExecutablePath = errors.New("failed to retrieve the executable path from the kernel")

ErrFailedToRetrieveExecutablePath is returned when the path of the running executable cannot be retrieved from the kernel.

View Source
var ErrFailedToRetrieveParentUID = errors.New("failed to retrieve parent process UID from the kernel")

ErrFailedToRetrieveParentUID is returned when the parent process UID cannot be retrieved from the kernel.

View Source
var ErrFailedToRetrievePeerPID = errors.New("failed to retrieve peer process ID from the kernel")

ErrFailedToRetrievePeerPID is returned when the process ID of the peer of a Unix domain socket connection cannot be retrieved from the kernel.

View Source
var ErrFileCodeSignatureInvalid = errors.New("code signature of file is not valid or does not satisfy the requirement")

ErrFileCodeSignatureInvalid is returned by VerifyFileCodeSignature or Requirement.VerifyFile when the code at the given path cannot be read, is unsigned, has been modified since it was signed, or does not satisfy the given requirement.

View Source
var ErrInvalidRequirement = errors.New("invalid code signing requirement")

ErrInvalidRequirement is returned when a code signing requirement string cannot be compiled, e.g. because of a syntax error or invalid encoding.

View Source
var ErrNoSelfRequirement = errors.New("could not determine this process's own designated code signing requirement")

ErrNoSelfRequirement is returned by SelfRequirementString when the running binary's own designated code signing requirement cannot be determined, typically because it is unsigned.

View Source
var ErrPeerCodeSignatureInvalid = errors.New("peer code signature is not valid or does not satisfy the requirement")

ErrPeerCodeSignatureInvalid is returned by VerifyPeerCodeSignature or Requirement.Verify when the peer either cannot be identified, is no longer running, or does not satisfy the given requirement.

Functions

func EnsureParentProcessSafe

func EnsureParentProcessSafe() error

EnsureParentProcessSafe checks that:

  1. the parent process UID matches the executable's UID
  2. the current process UID matches that of the parent
  3. the executable is not group- or world-writable or executable
  4. the current process is not running with elevated privileges (SUID/SGID)
  5. the process has not been orphaned (reparented to launchd)

1 ensures that only the executable's owner can launch it, 2 ensures that the current process UID matches that of the executable owner, 3 ensures that the executable cannot be modified or run by other users, 4 ensures that the process has not escalated privileges, and 5 ensures that parent identity cannot be spoofed via orphaning.

func Entitlements

func Entitlements() ([]byte, error)

Entitlements returns the code signing entitlements of the running executable, as the property list recorded in its signature, or nil if it was signed without any.

Only the property list form of the entitlements is consulted. A signature that carries them solely in their DER form, which the kernel keeps separately, is reported here as having none.

func ExecutablePath

func ExecutablePath() (string, error)

ExecutablePath returns the path of the binary that this process is running, as the kernel records it for the executing file.

It exists because os.Executable does not answer that question on macOS. What os.Executable reports is the path the process was launched with, made absolute against the working directory when it is relative. That names the symbolic link when a process is started through one, and for a process in an App Sandbox it can name a path that does not exist at all, the recorded path having been joined to the container's redirected home directory rather than to the directory the binary occupies. Asking the kernel avoids both: the path returned is that of the file being executed, with symbolic links already resolved.

func GetExecutableInfo

func GetExecutableInfo() (uint32, os.FileInfo, error)

GetExecutableInfo retrieves the owner UID and file info of the executable.

The executable is identified by ExecutablePath rather than by os.Executable, so that what is examined is the binary the kernel is running rather than the path it was launched through. The two differ for a process started through a symbolic link, and for one in an App Sandbox the latter can name a path that does not exist.

func GetParentUID

func GetParentUID() (uint32, error)

GetParentUID retrieves the real user ID (RUID) of the parent process.

func IsSandboxed

func IsSandboxed() (bool, error)

IsSandboxed reports whether the running executable is confined by the macOS App Sandbox, which it determines from the com.apple.security.app-sandbox entitlement in its code signature.

That entitlement is what places a process in the App Sandbox: it is applied by the kernel when the process is executed, and a process carrying it with no container to run in is killed rather than run unconfined. Its presence is therefore conclusive. App extensions, Safari web extensions among them, are always signed with it.

It says nothing about the other ways a process can be confined on macOS. A profile applied by sandbox_init, or by being launched under sandbox-exec, leaves no trace in the code signature and is not reported here.

func PeerPID

func PeerPID(conn *net.UnixConn) (int32, error)

PeerPID returns the process ID of the process on the other end of conn, a Unix domain socket connection, via the LOCAL_PEEREPID socket option: a public, documented macOS extension to getsockopt (see unix(4)), unlike the csops interface the rest of this package otherwise resorts to, so no cgo is needed here.

The PID alone proves nothing about which binary is running as that process; pair it with VerifyPeerCodeSignature to check that.

func SelfRequirementString

func SelfRequirementString() (string, error)

SelfRequirementString returns the running binary's own designated code signing requirement, in the same requirement-string language VerifyPeerCodeSignature takes. One use for it: a process can hand its own requirement string to a child process it spawns, so that child can verify the parent back over the same connection, making the check mutual rather than one-directional.

func VerifyFileCodeSignature

func VerifyFileCodeSignature(path, requirement string) error

VerifyFileCodeSignature verifies that the code at path, an executable or a bundle, is validly signed, has not been modified since it was signed, and satisfies requirement, a code signing requirement string as for VerifyPeerCodeSignature. Code nested within a bundle is verified too.

It is the check to make before executing a binary that must be trusted, since checking the process once it is running, with VerifyPeerCodeSignature, is too late to keep untrusted code from running. The file can still be replaced between this check and its execution, so a caller that can should also verify the running process, e.g. with VerifyPeerCodeSignature over a socketpair shared with it.

func VerifyPeerCodeSignature

func VerifyPeerCodeSignature(conn *net.UnixConn, requirement string) error

VerifyPeerCodeSignature verifies that the process connected via conn, a Unix domain socket connection, is a currently valid, unmodified binary satisfying requirement: a code signing requirement string in the language codesign and Xcode use (see `man csreq`), typically naming a Team ID and/or bundle identifier, e.g.

anchor apple generic and certificate leaf[subject.OU] = "TEAMID" and identifier "ai.onyourbehalf.router-helper"

Filesystem or socket permissions on their own only prove that the connecting process is allowed to reach the socket (e.g. membership of the same App Group container); they say nothing about which binary is on the other end of it. This is the additional check needed to prove that it is a specific, currently valid, signed binary, using the same mechanism SecCodeCheckValidity applies to the calling process, applied instead to the peer of a local socket connection.

On modern macOS systems, peer verification retrieves the peer's kernel audit token (via LOCAL_PEERTOKEN), which includes the task generation ID (pidversion). This binds verification directly to the connecting process instance, eliminating the PID recycling race condition. If audit tokens are unavailable, verification falls back to the peer PID.

A caller should perform this check immediately after accepting conn and before reading or acting on anything it sends, closing conn without further use if it returns an error.

Types

type Requirement

type Requirement struct {
	// contains filtered or unexported fields
}

Requirement represents a compiled macOS code signing requirement. It can be safely reused across multiple connections for efficient verification.

func NewRequirement

func NewRequirement(requirement string) (*Requirement, error)

NewRequirement parses and compiles a code signing requirement string. It returns ErrInvalidRequirement if the requirement string is invalid.

func (*Requirement) Close

func (r *Requirement) Close()

Close releases the underlying SecRequirementRef.

func (*Requirement) String

func (r *Requirement) String() string

String returns the requirement string from which r was compiled.

func (*Requirement) Verify

func (r *Requirement) Verify(conn *net.UnixConn) error

Verify verifies that the process connected via conn satisfies this requirement.

func (*Requirement) VerifyFile

func (r *Requirement) VerifyFile(path string) error

VerifyFile verifies that the code at path, an executable or a bundle, is validly signed, unmodified on disk, and satisfies this requirement.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL