# Quickstart

The platform-extension-proxy package contains only the library. It does
nothing until a configuration is installed. There are two ways to get one.

A. Install the default configuration package
   system-plugin-config-platform-extension-proxy (built from the system-plugin
   repository, conf/platform-extension-proxy/). It pulls in
   platform-extension-proxy and installs /etc/ld.so.preload, the policy, the
   enforce list and the permissive switch described below, so reboot the
   system and you are done.

B. Install the example files from this package by hand, see "Example
   configuration" below.

In both cases the policy is checked but not enforced, as permissive mode is
on by default (see "Permissive mode" at the end).

Next steps should be:
- check /run/pe-proxy-deny.log and update /etc/platform-extension/proxy/policy accordingly
  (proxy-deny2policy from the platform-extension-proxy-tools package can do this
  from log files, see its README)
- disable the permissive mode:

    echo > /etc/platform-extension/proxy/permissive

# Example configuration

The package installs these files in its documentation directory
(/usr/share/doc/platform-extension-proxy/):

- README            this file
- policy.example    minimal policy with three rules: code located under /hal
                    (the HAL modules) may access /hal and /sys, everything else
                    is denied to it. It is a starting point for writing a real
                    policy, not a complete one (real HAL modules need more,
                    for example /dev and /proc).
- enforce.example   the list of all functions this version of the library can
                    check. It is generated from the wrapper list in proxy.c
                    at build time, so it always matches the installed library.
                    Remove the lines of the functions you do not want to check.
- permissive.example  contains "1", which enables permissive mode (violations
                    are only logged). An empty file disables it.

To get a working configuration from them (as root):

    D=/etc/platform-extension/proxy
    mkdir -p $D
    cp /usr/share/doc/platform-extension-proxy/policy.example     $D/policy
    cp /usr/share/doc/platform-extension-proxy/enforce.example    $D/enforce
    cp /usr/share/doc/platform-extension-proxy/permissive.example $D/permissive
    echo /usr/lib64/libplatform-extension-proxy.so.0 > /etc/ld.so.preload

then fix the permissions (see below) and reboot, or start new processes: the
library is loaded into every process that starts after /etc/ld.so.preload is
in place. Use /usr/lib instead of /usr/lib64 on 32-bit images. Always name the
soname (.so.0), not the fully versioned file.

IMPORTANT: permissions. /etc/ld.so.preload is read by the dynamic loader and the
policy, enforce and permissive files by the library inside *every* process,
whatever its user and Smack label, including daemons running with reduced
privileges. If a
process cannot read a file, the library treats it as missing (an empty policy
allows everything, an empty enforce list checks nothing), so the failure is
silent and your configuration just does not apply to that process. Therefore:

- the files must be mode 0644 and the directory 0755, owned by root,
- the files and the directory must have the Smack label "_" (floor), which
  every subject can read:

    chmod 0755 $D
    chmod 0644 $D/policy $D/enforce $D/permissive /etc/ld.so.preload
    chsmack -a '_' $D $D/policy $D/enforce $D/permissive /etc/ld.so.preload

  Files created by cp get the label of the creating process, which is not "_"
  in general, so do not skip the chsmack step. Check the result with
  "ls -Z" or "chsmack $D/*". Only the deny log /run/pe-proxy-deny.log has a
  different label ("*") and mode (0666), because every process writes to it.

The example files are only copied from the documentation directory, they are
never read by the library from there.

# Doc
A preloaded library that checks file system access against a policy.

The platform-extension-proxy package installs
/usr/lib64/libplatform-extension-proxy.so.0.0.0. The library does nothing
until a configuration is installed. The default configuration is the
system-plugin-config-platform-extension-proxy package (built from the
system-plugin repository, see Quickstart). It installs /etc/ld.so.preload,
which loads the library into every process, and the policy, enforce and
permissive files described below. The library provides proxy functions for various libc functions
accessing files and directories and checks if a process, based on policy,
can access requested files. The main purpose of this library is to check
if hal services don't access files beyond permitted scope.

This library is a development tool and should not be used on a production image.

The access policy is loaded from /etc/platform-extension/proxy/policy at
startup. Each rule has a subject path, a target path, and an allow, deny
or log verdict. First matching rule wins, default is allow. The pattern
matching works like this:

- /dir matches /dir, /dir/file but not /directory,
- /dir/ does not match /dir itself, it only matches /dir/file,
- a trailing * matches any path starting with the part before it, e.g.
  /usr/lib/libfoo.so* matches /usr/lib/libfoo.so.1.2.3 (and /usr/lib/libfoo.so.bak),
  * anywhere else in the path is not special,
- as a special case / matches everything.
# comments are allowed but only on the line start. If the policy is malformed
or fails to load, nothing is blocked.

The subject of a rule is the canonical path of the object (executable or
shared library) that called the libc function. If a HAL module accesses a
file through another library (for example glib or libstdc++), the subject is
that library, not the HAL module. The target is the canonical path of the
accessed file. For calls that do not follow a symbolic link in the last path
component (for example lstat, unlink, rename or O_NOFOLLOW), only the parent
directory is resolved. Only a policy verdict denies an access. If the proxy cannot resolve the caller
or the path (an internal error), the call is let through and the error is
logged. Denied accesses fail with EACCES and are logged to
/run/pe-proxy-deny.log (/dev/kmsg for PID 1). The log is mode 0666 and
labelled "*" so that every process can append to it; tmpfiles.d sets this up
at boot and the library does the same if it creates the file earlier.

The library cannot see accesses that do not go through the dynamically
linked libc functions it replaces. These include statically linked binaries,
direct syscall(2) calls, io_uring, and file access inside libc itself (for
example NSS or the dynamic loader).

Additionally, /etc/platform-extension/proxy/enforce is loaded at startup. Each
line contains a name of a function. If a function is mentioned in this file,
all checks for this function are enabled. Again, # comments are allowed but
only on the line start. A name this version of the library does not know is
logged and ignored. A malformed file or one that fails to load is treated as
if empty. A missing policy or enforce file is silently treated as empty.

Performance notes:
The calling object of each proxied call is determined with _dl_find_object()
and cached per loaded object, so calls from objects that no deny rule applies
to cost nothing extra. For the others, the directory part of the target path
is resolved once and cached (32 entries, about 8 KB, in processes that
perform checks only), then the last component is checked with one lstat(2).
The cache is flushed whenever the process itself renames, unlinks, symlinks,
mounts etc., and entries expire after HAL_DIRCACHE_TTL_MS (default 1000), so
a change made to the file system by another process, e.g. retargeting a
symlink in a directory path, can go unnoticed for up to that long. Build with
-DHAL_DIRCACHE_TTL_MS=0 to disable the directory cache.
bench/run.sh, bench/difftest.sh and bench/cachetest.sh measure and test all
of this.

Permissive mode:
This mode enables logging access violations without actually denying access
to files. The mode can be enabled by creating non-zero sized file named
/etc/platform-extension/proxy/permissive or setting the action field
of a particular rule to "log".

Providing another configuration:
The configuration is the only interface to the library, there is no other
dependency between the two packages. A package replacing
system-plugin-config-platform-extension-proxy should
- provide and conflict with platform-extension-proxy-config (only one package
  can own /etc/ld.so.preload),
- have "Requires: platform-extension-proxy" (the library does not require config),
- install /etc/ld.so.preload naming the soname
  /usr/lib64/libplatform-extension-proxy.so.0 (not the full version),
- install policy, enforce and permissive in /etc/platform-extension/proxy/,
  readable by every process (mode 0644, Smack label "_", see "Example
  configuration"). The *.example files of this package can be used as a base.
