safeio

package
v0.2.44 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package safeio writes files and directories into a location that a less privileged account can influence, without letting that account redirect the write. Every helper refuses a symlink anywhere along the path before it creates or changes anything: a worker-owned `~/.railgrid -> /etc` would otherwise turn a root-side install into an arbitrary-file overwrite.

These started life in pkg/cli/cmd/launchd.go for the macOS LaunchDaemon installer. The edge add-on manager needs exactly the same guarantees when it prepares an add-on state directory under another account's home, so they live here rather than being duplicated.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureDir

func EnsureDir(path string, mode os.FileMode, uid, gid int) error

EnsureDir creates path with mode, then enforces mode and ownership on it.

Ancestors that do not exist yet are created with the same mode and owner. os.MkdirAll alone would leave them owned by the caller (root, for the edge agent) with a mode that account cannot traverse, so a directory nominally the account's own — <its home>/.railgrid/runner/<harness> — sat behind a root-only ~/.railgrid, and the runner child failed to enter its working directory. Go reports that from the child as "fork/exec <binary>: permission denied", which names the wrong thing entirely. Ancestors that already exist are left exactly as they are: the home directory itself, /var/lib and the like belong to somebody else.

uid/gid of 0 means "leave ownership alone" so a non-root caller preparing its own directories does not have to special-case the chown.

func RejectSymlink(path string) error

RejectSymlink fails when path itself is a symlink. A missing path is fine — the caller is about to create it.

func RejectSymlinkPath

func RejectSymlinkPath(path string) error

RejectSymlinkPath checks every existing component of a directory path before anything is created or changed below it. Components that do not exist yet are fine; an existing component that is a symlink, or is not a directory, is not.

func WriteFile

func WriteFile(path string, data []byte, mode os.FileMode, uid, gid int) error

WriteFile replaces path atomically: it writes a temporary file in the same directory with the requested mode and ownership, then renames it over the destination. The permissions and owner are set BEFORE the content is written, so the data is never briefly readable by the wrong account. uid/gid of 0 leaves ownership to the calling process.

Types

This section is empty.

Jump to

Keyboard shortcuts

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