Development documentation

This is the in-progress lota-next branch. For the released version, see the stable documentation.

BPF LSM program

The daemon enforces through one BPF object, src/bpf/lota_lsm.bpf.c, attached to LSM hooks, tracepoints and function entries. It refuses what the startup policy and Configuration refuse, guards the agent and the protected processes (Protected processes), and records what the agent measures. The definitions it shares with user space are in include/lota.h; the loader is src/agent/bpf_loader.c.

Loading

The loader reads the object and its detached signature, <object>.sig, checks the signature against the enforcement public key, and opens the verified bytes from memory, so a file changed after the check is not what loads. A refused signature or layout ends the start before PCR 14 is touched (Agent lifecycle).

After the load, in this order:

  1. lota_task_auth gets LOTA_TASK_AUTH_ADMIN and LOTA_TASK_AUTH_AGENT on the daemon's thread group leader, and the map is frozen. The flags are read on a task's group leader, so every thread of the daemon is the agent and no other process can become it.

  2. lota_config gets LOTA_CFG_LOCK_BPF (The bpf syscall).

  3. integrity_cfg records whether module signatures are enforced and lockdown is at integrity or above. When either is off the daemon logs Kernel integrity baseline not met (...); module and firmware loads are refused in every mode.

  4. guarded_files gets the cgroup.kill and cgroup.freeze files of the agent's cgroup and its ancestors, and is frozen. With no unified cgroup hierarchy, or no such file, the map is frozen empty.

  5. The startup policy writes lota_config (Mode and switches).

  6. Each program is attached. The start fails when one does not attach, or when any of lota_ptrace_access_check, lota_agent_exit, lota_no_core, lota_agent_moved, lota_stop_enter and lota_stop_leave is missing from the object.

Mode and switches

lota_config holds the mode, five switches of lota.conf and LOTA_CFG_LOCK_BPF. The startup policy writes them, and a reload writes the changed ones:

Key

lota.conf

Effect

LOTA_CFG_MODE

mode

LOTA_MODE_MONITOR (0) or LOTA_MODE_ENFORCE (1).

LOTA_CFG_STRICT_MMAP

strict_mmap

Executable mappings of a protected process; trusted libraries.

LOTA_CFG_BLOCK_PTRACE

block_ptrace

ptrace attach to any process.

LOTA_CFG_BLOCK_ANON_EXEC

block_anon_exec

Anonymous executable memory of a protected process.

LOTA_CFG_STRICT_EXEC

strict_exec

execve of files outside allow_verity.

LOTA_CFG_STRICT_MODULES

strict_modules

Modules and firmware outside allow_verity.

LOTA_CFG_LOCK_BPF

none

LOTA maps and links closed to other processes.

The five lota.conf switches take effect only in enforce mode; in monitor mode they change nothing. LOTA_CFG_LOCK_BPF and the rules that guard the agent, the protected processes and the kernel hold in both modes.

Hooks

A hook refuses with -EPERM, but lsm/task_alloc when it cannot store a mark. A kernel path that asks ptrace_access_check returns its own error: an open of /proc/<pid>/mem fails with EACCES.

Execution and kernel input

lsm/bprm_check_security

Reads the fs-verity digest of the file. In enforce mode with strict_exec it refuses a script starting with #!, an interpreter a binfmt handler hands the exec to, an exec through a descriptor, and a file whose digest is missing or not in allow_verity. Emits EXEC or EXEC_BLOCKED.

lsm/kernel_read_file, lsm/kernel_load_data

Rule on modules, firmware, kexec images, IMA policy and certificates with lota_kread_denied() (Verdict functions). Emit MODULE_LOAD or MODULE_BLOCKED.

lsm/bprm_committed_creds

Notes in born_protected that a protected process executed its current image under protection.

lsm/task_fix_setuid

Emits SETUID when the real uid changes. Refuses nothing.

Memory mappings

lsm.s/mmap_file, lsm/file_mprotect

Record an executable mapping of a file: in mapped_seen for every process, in exec_history for a protected one. Memory with no file behind it, a /dev/zero mapping, SysV shared memory, and an unlinked tmpfs or hugetlbfs file count as anonymous; with block_anon_exec a protected process is refused them (ANON_EXEC_BLOCKED). With strict_mmap a protected process is refused an executable mapping that is writable, or whose file is neither in allow_verity nor a trusted library (MMAP_EXEC, MMAP_BLOCKED). The last refused object is kept in lota_refusal.

Files and mounts

lsm.s/file_open

Refuses, in every mode:

  • /dev/mem, /dev/kmem and /dev/port;

  • an entry of /proc/<pid> that reads the memory of the agent, or of a protected process from outside its instance (PTRACE_BLOCKED); the agent's reads and a process's reads of itself pass;

  • a write open of a guarded cgroup file to all but the agent (KILL_GUARD_BLOCKED);

  • a write open of a file in mapped_exec (MAPPED_WRITE_BLOCKED).

In enforce mode with strict_mmap it refuses a write open of a trusted library. O_WRONLY, O_RDWR and O_TRUNC count as write.

lsm/path_truncate

Refuses a truncate of a file in mapped_exec, and of a trusted library under strict_mmap.

lsm/sb_mount, lsm/move_mount

In enforce mode with strict_mmap, refuse a mount(2) with MS_BIND or MS_MOVE and a move_mount(2) onto a trusted library (MOUNT_BLOCKED).

lsm.s/file_post_open

Answers the agent's questions in exe_hash_slots: the file's IMA hash, or whether a pin holds (Runtime image measurement).

Processes and signals

lsm/ptrace_access_check

Rules with lota_ptrace_denied() and emits PTRACE or PTRACE_BLOCKED. An access inside one instance is recorded in instance_actors.

lsm/task_kill

Rules with lota_signal_denied() on a signal at the agent or a protected process, and emits KILL or KILL_BLOCKED. A signal inside one thread group, from the agent, or a parent-death signal is not judged.

lsm/file_send_sigiotask

Rules with lota_sigio_denied() on a signal F_SETOWN aims at the agent or a protected process, with the sender counted as outside the instance and none of the exceptions of lsm/task_kill, and emits KILL_BLOCKED when it refuses.

lsm/task_setrlimit

Refuses a change to the limits of the agent, and of a protected process from outside its instance (KILL_BLOCKED).

lsm/task_alloc

Marks a child of a protected process before it first runs, and refuses the clone when the mark cannot be stored or lota_clone_refused() says so.

tp_btf/signal_deliver

Ends a protected process with SIGKILL when a core-dumping signal it does not handle is delivered (KILL_BLOCKED).

tp_btf/sched_process_exit

When the agent's thread group exits, sends SIGKILL to every protected process.

lsm/task_free

Removes a protected process from protected_pids when its group leader is freed.

Sockets

lsm/socket_post_create, lsm/socket_bind, lsm/socket_listen

Mark a unix socket a process of an instance created, bound or listens on.

lsm/unix_stream_connect, lsm/unix_may_send

Record in instance_actors a process outside the instance that connects or sends to a marked socket. Refuse nothing.

Cgroups

fentry/cgroup_attach_task

A move of the agent to another cgroup ends every protected process and the agent before the move takes effect (AGENT_MOVED).

fentry/cgroup_enter_frozen, fexit/cgroup_leave_frozen

Add the time a thread of an instance stands stopped to instance_stops of the instance root (Protected processes).

The bpf syscall

With LOTA_CFG_LOCK_BPF set, in every mode, lsm/bpf_map refuses a descriptor of a map the object declares in .maps to all but the agent, and lsm/bpf refuses BPF_LINK_DETACH. The hook compares the map's address with those maps, so a map of another program with the same name stays open, and so does the frozen map of the object's string constants.

Maps

Map

Type

Contents

events

ring buffer, 256 KiB

Events; the agent reads them.

stats

array

Statistics.

lota_config

array

Mode and switches; written by the agent.

integrity_cfg

array

Module signature enforcement and lockdown, written at load.

event_budget

array

One emission window per event type (Event budget).

lota_task_auth

task storage

The agent's flags, written at load and frozen.

lota_instance

task storage

The instance root a process inherited.

born_protected

task storage

Set by bprm_committed_creds.

instance_stops

task storage

Stopped threads and stopped time of an instance root.

lota_refusal

task storage

The last refused mapping and its time.

verity_scratch

per-CPU array

Scratch space for a digest.

protected_pids

hash, 1024

Protected processes: TGID to start time in clock ticks; written by the agent.

trusted_libs

hash, 512

Trusted libraries by device and inode (trust_lib).

guarded_files

hash, 64

Guarded cgroup files, written at load and frozen.

mapped_exec

hash, 16384

Files the agent pinned while they are mapped.

mapped_seen

LRU hash, 65536

Size and change time of each file seen mapped executable.

exec_history

hash, 65536

Executable mappings of protected processes.

lota_sock_owner

socket storage

The instance a unix socket belongs to.

instance_actors

hash, 4096

Actors of each instance.

allow_verity

hash, 1024

Allowed fs-verity digests, SHA-256 or SHA-512 (allow_verity).

exe_hash_slots

hash, 64

One question per agent thread for file_post_open.

Device numbers in the keys of trusted_libs, guarded_files and mapped_exec use the kernel's layout (include/lota_devt.h).

Events

Each event is a struct lota_exec_event. Every type sets timestamp_ns, pid, tgid, uid and comm; uid is 0 on the module types. Beyond those:

Type

Fields

EXEC

filename, hash (the first 32 bytes of the fs-verity digest)

MODULE_LOAD

filename

MMAP_EXEC

filename, flags

ANON_EXEC

filename: a fixed label, or a refused memory file's name

PTRACE

target_pid, filename (the target's), flags

KILL

target_pid, signal, filename (a fixed label)

SETUID

target_uid (the new uid)

AGENT_MOVED

target_pid, filename (the cgroup)

PTRACE_BLOCKED on a /proc open

target_pid, filename (the entry)

MOUNT_BLOCKED, KILL_GUARD_BLOCKED, MAPPED_WRITE_BLOCKED

filename

Other *_BLOCKED

As the base type

LOTA_EVENT_FLAG_PROTECTED marks a mapping by a protected process, which the agent measures again; LOTA_EVENT_FLAG_INSIDE_INSTANCE a ptrace access within one instance.

The agent writes each event to the journal at info, AGENT_MOVED at warn (src/agent/event.c); most types as:

[TIMESTAMP] TYPE COMM: FILENAME (pid=P, uid=U)

Event budget

In enforce mode each type has a one-second window: 256 events of a permitted type, 1024 of a refused one. MOUNT_BLOCKED, KILL_GUARD_BLOCKED, MAPPED_WRITE_BLOCKED and the PTRACE_BLOCKED of an open take the refused cap in both modes; SETUID and AGENT_MOVED have none. In monitor mode the other types are emitted without a cap. Past 75% of the ring, a type under a cap that emitted 8 in the window yields to quieter ones (include/lota_event_budget.h).

Statistics

stats counts executions, mappings, refusals, ptrace access checks, events sent, ring-buffer drops, events suppressed or yielded by the budget, signal-0 probes, and the entries exec_history, instance_actors and instance_stops could not store. When the daemon leaves its event loop it logs them on one line that starts Shutdown statistics: exec=, without the suppressed events, STAT_EXEC_BLOCKED, STAT_ANON_EXEC and STAT_ANON_EXEC_BLOCKED.

Verdict functions

The decisions of the hooks are plain functions in include/lota.h, compiled into the object and into the unit tests alike.

int lota_kread_is_module(unsigned int id)

Whether a purpose names a kernel module.

Parameters

unsigned int id

enum kernel_read_file_id, as LOTA_KREAD_*.

finit_module(MODULE_INIT_COMPRESSED_FILE) reads under the compressed purpose, any other module read under the plain one.

Return

1 for a module purpose, 0 otherwise.

int lota_kread_is_known(unsigned int id)

Whether a purpose is on the list above.

Parameters

unsigned int id

as for lota_kread_is_module().

One above the list comes from a newer kernel and may be a module load, so lota_kread_denied() refuses it under strict module loading.

Return

1 up to LOTA_KREAD_MAX_KNOWN, 0 above it.

int lota_kread_is_watched(unsigned int id, int from_file)

Whether the gate rules on a purpose.

Parameters

unsigned int id

as for lota_kread_is_module().

int from_file

kernel_read_file, else kernel_load_data. A certificate is watched only in the second.

Return

1 when the gate rules on id, 0 when it passes unjudged.

int lota_kread_denied(unsigned int id, int from_file, int policy_on, int strict_modules, int baseline_ok, int verity_allowed, int foreign_userns)

Verdict on what the kernel takes in on its own behalf.

Parameters

unsigned int id

as for lota_kread_is_module().

int from_file

kernel_read_file, else kernel_load_data.

int policy_on

The mode is enforce.

int strict_modules

The strict_modules setting.

int baseline_ok

Module signature enforcement and lockdown are both on.

int verity_allowed

The file's fs-verity digest is on the allow_verity list.

int foreign_userns

The caller is outside the initial user namespace.

Integrity holds in every mode:

  • kexec replaces the measured kernel, and is refused;

  • a user namespace other than the initial one takes in nothing watched;

  • a module or firmware load needs module signature enforcement and lockdown.

strict_modules is policy and applies in enforce only:

  • a module or firmware read from a file needs an allow_verity digest;

  • one handed over from memory has no file to check, and is refused;

  • a purpose not on the list is refused.

An IMA policy read from a file is always allowed.

Return

1 to deny, 0 to allow.

int lota_ptrace_is_attach(unsigned int ptrace_mode)

Whether an access asks to trace the target.

Parameters

unsigned int ptrace_mode

PTRACE_MODE_* bits the hook was given.

Return

1 when ptrace_mode carries PTRACE_MODE_ATTACH, 0 otherwise.

int lota_ptrace_agent_read_exempt(unsigned int ptrace_mode, int actor_is_agent)

Whether an access is the agent's own read.

Parameters

unsigned int ptrace_mode

as for lota_ptrace_is_attach().

int actor_is_agent

The caller is the agent.

procfs checks the agent's reads of /proc/<pid>/exe and maps with PTRACE_MODE_READ. Refused, they leave a protected process unmeasured.

Return

1 for a read by the agent, 0 otherwise.

int lota_ptrace_denied(unsigned int ptrace_mode, unsigned int lota_mode, int block_ptrace, int target_is_agent, int target_is_protected)

Verdict on a ptrace access.

Parameters

unsigned int ptrace_mode

as for lota_ptrace_is_attach().

unsigned int lota_mode

LOTA_MODE_*.

int block_ptrace

The block_ptrace setting.

int target_is_agent

The target is the agent.

int target_is_protected

Protected, and the actor is outside its instance.

The caller has settled the agent's read exemption.

The agent and a protected process refuse both access modes in every enforcement mode: a token speaks for an isolated process measured by an intact agent.

block_ptrace is policy over any other access: enforce only, and attach only. A read stays with the kernel's permission model, as tools such as lsof read other processes' /proc.

Return

1 to deny, 0 to allow.

int lota_proc_entry_reads_mm(const char *name, unsigned int len)

Whether a /proc/<pid> entry reads memory.

Parameters

const char *name

entry name, need not be NUL-terminated.

unsigned int len

bytes in name.

mm_access() admits CAP_PERFMON past a refused READ ptrace check, so these opens are refused on their own, by the rules of the ptrace verdict. Entries that only name the process stay open.

Return

1 for an entry that reads memory, 0 otherwise.

int lota_clone_inherits_protection(unsigned long clone_flags, int creator_protected)

Whether a new task becomes protected.

Parameters

unsigned long clone_flags

flags of the clone.

int creator_protected

The creating task is protected.

The child is marked before it first runs and stays marked across execve. A thread belongs to its leader and takes no mark of its own.

Return

1 when the child takes the mark, 0 otherwise.

int lota_clone_refused(int creator_protected, int creator_traced, int tracer_protected)

Whether a traced protected process may not clone.

Parameters

int creator_protected

The creating task is protected.

int creator_traced

The creating task has a tracer.

int tracer_protected

The tracer is inside the creator's instance.

A tracer attached before the process was protected stays attached, and ptrace_init_task() links it to every child it asked for with no LSM hook.

Return

1 to refuse the clone, 0 to allow it.

int lota_inside_instance(int actor_protected, unsigned int actor_root, unsigned long long actor_ticks, int target_protected, unsigned int target_root, unsigned long long target_ticks)

Whether an access stays inside one instance.

Parameters

int actor_protected

The actor is protected.

unsigned int actor_root

tgid of the root of the actor's instance.

unsigned long long actor_ticks

start time of that root.

int target_protected

The target is protected.

unsigned int target_root

tgid of the root of the target's instance.

unsigned long long target_ticks

start time of that root.

An instance is the tree one protected process started, named by that root's tgid and start time, so a recycled tgid names another instance. The wineserver ptraces processes it hosts, so being protected refuses no ptrace inside an instance: it is recorded, and only block_ptrace can refuse an attach.

Return

1 when both are protected under the same root, 0 otherwise.

int lota_signal_denied(int sig, int target_is_agent, int sender_inside)

Verdict on a signal.

Parameters

int sig

signal number, 0 for a probe.

int target_is_agent

The target is the agent.

int sender_inside

The sender is in the target's instance.

Self-signals, the agent, the admin identity, a parent-death signal and a signal the kernel does not count as sent from user space never reach this.

Only a probe (sig 0) and SIGHUP, its reload, reach the agent. It stops with lota-agent --shutdown.

A protected process can be ended. From outside its instance it takes no stopping signal, nor one whose default writes its memory to a core file. Inside it everything passes: the wineserver drives the processes it hosts with signals.

The mode does not change the verdict: a killed agent stops measuring without a trace.

Return

1 to refuse, 0 to deliver.

int lota_sigio_denied(int sig, int target_is_agent)

Verdict on a signal aimed through F_SETOWN.

Parameters

int sig

signal number, 0 for the default SIGIO.

int target_is_agent

The target is the agent.

The sender is unknown and counts as outside.

Return

as for lota_signal_denied().

int lota_signal_event(int sig, int denied)

Event for a signal aimed at a protected target.

Parameters

int sig

signal number, 0 for a probe.

int denied

The answer of lota_signal_denied().

A permitted probe (sig 0) is not recorded: launchers poll several times a second.

Return

An enum lota_event_type, or 0 for none.

int lota_kill_guard_denied(int write_open, int guarded, int opener_exempt)

Verdict on opening a guarded cgroup file.

Parameters

int write_open

The open is for writing.

int guarded

The file is a guarded one.

int opener_exempt

The opener is the agent or the admin identity.

A write to it ends or stops tasks past the signal hook, so in every mode only the agent and the admin identity may open it for writing.

Return

1 to refuse, 0 to allow.

int lota_verity_key_from_digest_buf(const void *buf, struct lota_verity_digest_key *out)

Build an allowlist key from that buffer.

Parameters

const void *buf

At least LOTA_FSVERITY_DIGEST_BUF_SIZE wide and aligned for the header.

struct lota_verity_digest_key *out

filled on success, untouched on failure.

Return

0 on success, -1 when the reported size is not one policy enforces.