fileroots

package
v0.4.91 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 6 Imported by: ? modules, 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.

Documentation

Overview

Package fileroots confines the file API to configured directories ([file] ManagementRoots). The core runs as root: without a boundary, a signed-in session reads, writes and deletes anything on the machine.

Ported from EdmundFu-233/ReCasaOS pkg/filesecurity (managed roots, Apache-2.0): strict path spelling; on every check the root is opened (no symlink in its path) and the client path resolved with openat2(RESOLVE_BENEATH| RESOLVE_NO_MAGICLINKS) below it, so neither `..` nor a symlink leads out. It is a check before the operation: the operation itself still works on the path (see Check).

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrOutside: the path is not below a root, or resolves out of it.
	ErrOutside = errors.New("the path is outside the managed file roots")
	// ErrInvalidRoot: a root in the configuration is not usable.
	ErrInvalidRoot = errors.New("invalid managed file root")
	// ErrUnsupported: confinement needs Linux openat2.
	ErrUnsupported = errors.New("managed file roots need Linux openat2")
	// ErrRootUnavailable: the root cannot be opened now (not mounted yet, gone,
	// replaced by a symlink).
	ErrRootUnavailable = errors.New("the managed file root is not available")
	// ErrRootNotReal: the root is a symlink, or spelled through one; roots are
	// spelled as their real path
	ErrRootNotReal = errors.New("the managed file root is spelled through a symlink: use its real path")
	// ErrRoot: a root itself, or a folder above one, is not deleted or renamed.
	ErrRoot = errors.New("a managed file root is not deleted or renamed")
)

Functions

func Parse

func Parse(value string) ([]string, error)

Parse reads the comma-separated setting. Empty: no roots, the file API is not confined (unchanged behaviour). The filesystem root is refused: it would make every check a no-op.

Types

type Roots

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

Roots is the configuration. The zero value and nil are unconfined.

var Default *Roots

Default: the roots of this process ([file] ManagementRoots), set at start. nil: unconfined.

func Open

func Open(paths []string) (*Roots, error)

Open takes the roots (Parse) and makes sure the kernel has openat2. The roots are not opened here but on every check: one mounted later (a disk, a USB stick) is the one checked, and one missing now is refused until it is there (Unavailable). No roots: unconfined.

func Refusing

func Refusing(err error) *Roots

Refusing: Roots that refuse every path with err (a configuration that cannot be used).

func (*Roots) Check

func (r *Roots) Check(path string) (string, error)

Check returns the canonical spelling of a client path that resolves below its root, symlinks followed and kept beneath. The root is opened for the check: not there (not mounted yet), or a symlink now → ErrRootUnavailable. A path that does not exist yet (an upload, a new folder, a rename's target) is checked through its nearest existing ancestor; a dangling link on the way is refused (writing through it could land anywhere). Relative links that stay below the root are followed; absolute links are refused, even to inside (RESOLVE_BENEATH). Unconfined: the path as given.

It is a check before the operation: a component swapped for a symlink between the check and the operation is not caught here.

func (*Roots) CheckAll

func (r *Roots) CheckAll(paths []string) ([]string, error)

CheckAll: Check for every path; the canonical paths, or the first error.

func (*Roots) CheckEach

func (r *Roots) CheckEach(paths ...*string) error

CheckEach replaces each *path with its canonical spelling, or returns the first error.

func (*Roots) CheckEntries

func (r *Roots) CheckEntries(paths ...*string) error

CheckEntries: CheckEntry for each *path, replaced with its canonical spelling; the first error.

func (*Roots) CheckEntry

func (r *Roots) CheckEntry(path string) (string, error)

CheckEntry is Check for what deletes or renames the entry itself, not what it leads to: its folder must be below a root, the entry need not resolve there (a link that leads out is removed or renamed as a link). A root, or a folder above one, is refused (ErrRoot). Unconfined: the path as given.

func (*Roots) Confined

func (r *Roots) Confined() bool

Confined: there are roots to enforce, or every path is refused.

func (*Roots) IsAncestor

func (r *Roots) IsAncestor(path string) bool

IsAncestor: path is a directory above a root ("/" above "/DATA") and not itself below one (with /DATA and /DATA/sub, /DATA is managed). Listing it shows only the way down to the roots (Leads).

func (*Roots) Leads

func (r *Roots) Leads(dir, name string) bool

Leads: the entry name of directory dir is a root or on the way to one.

func (*Roots) Paths

func (r *Roots) Paths() []string

Paths: the configured roots, most specific first.

func (*Roots) Unavailable

func (r *Roots) Unavailable() map[string]error

Unavailable: the roots that cannot be opened now, with why (for a warning at start): ErrRootUnavailable, and ErrRootNotReal too for one spelled through a symlink.

Jump to

Keyboard shortcuts

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