# proxy-deny2policy

A developer tool that updates the proxy policy from the deny logs of a device.
It is installed by the platform-extension-proxy-tools package (as
/usr/bin/proxy-deny2policy). Run it on a PC against the policy in the
system-plugin repository (conf/platform-extension-proxy/policy). python3 only, no
other dependencies.

# Workflow

1. Run the workload on a device with the proxy in permissive mode (the
   default), so nothing is blocked and every violation is logged.
2. Copy /run/pe-proxy-deny.log from the device (several logs can be used).
3. Preview and apply:

    proxy-deny2policy LOG [LOG...] -p PATH/TO/system-plugin/conf/platform-extension-proxy/policy --dry-run
    proxy-deny2policy LOG [LOG...] -p PATH/TO/system-plugin/conf/platform-extension-proxy/policy

4. Review `git diff` of the policy in system-plugin. Every logged access that passes the checks
   becomes an allow, so remove entries that only come from test artifacts
   (for example test apps under /opt/usr/globalapps). The tool cannot tell
   them apart.
5. Commit, then disable the permissive mode (see ../README).

Running the tool again with the same logs changes nothing.

# Input

Lines of the form

    [rootstrap-check-deny] [PID:.. TID:.. EXE:..]:SUBJECT:FUNCTION:TARGET:rule=N

optionally followed by " (permissive)". Lines without the [rootstrap-check-deny]
tag are ignored. A line that has the tag but cannot be parsed is reported as

    UNPARSED: FILE:LINE: the line looks like a deny log entry but cannot be
    parsed, it is ignored: ...

Only the unique (SUBJECT, TARGET) pairs matter; PID, EXE and the rule number
are ignored (FUNCTION is only used to recognise directories). Each pair is
checked against the current policy file, not against the policy that produced
the log.

# Rules

Matching is the same as in the library (see ../README, proxy.c pat_init and
pat_match), for both the subject and the target of a rule:

- /dir matches /dir and everything under it, but not /directory,
- /dir/ matches only what is under /dir, not /dir itself,
- a trailing * matches any path starting with the part before it,
- / matches everything,
- the first rule matching both subject and target decides,
- when no rule matches, the access is allowed.

The subject of a new rule is the logged library path turned into a pattern:

    /hal/lib64/libfoo.so.1.2.3    ->  /hal/lib64/libfoo.so*
    /hal/lib64/libbar-24.3.4.so   ->  /hal/lib64/libbar-*
    anything else                 ->  the path itself

The target of a new rule is the logged path, exactly, written as a plain path.
A plain path also matches everything below it, so for a FILE the rule is exactly
what was logged, but for a DIRECTORY it would allow the whole subtree. The tool
cannot see file types; it only knows a target is a directory when other logged
paths are below it or when it is accessed with chdir, mkdir, mkdirat, rmdir,
opendir, scandir, mkdtemp, ftw, nftw or chroot. Such accesses are not added
(see SKIPPED below). For any other target the tool assumes a file, so check
the targets in `git diff` (stat() and realpath() on a directory look like a
file). Rules are written as "allow".

# What happens to each logged access

The tool is strict: a new allow is only written when every check below
passes. Anything that is not added is reported on the console (NOT ADDED,
SKIPPED or UNPARSED) and counted in the summary, and the tool never changes
an existing rule. The tool finds the first rule that matches the access. Then:

1. No rule matches, or the first matching rule is allow: nothing to do, the
   access is already permitted. It is only counted in the summary.

2. The allow cannot be written safely: the tool prints

    SKIPPED: cannot write a rule for the access to TARGET by SUBJECT: REASON.

   and the rule it did not add. The reasons are: a subject or target that is
   not an absolute path, that contains whitespace or bytes that are not valid
   UTF-8, or that is longer than PATH_MAX; a target ending in * (it would
   become a prefix pattern); a target ending in / (it would only match what is
   under it, and "/" alone would match everything); a target that is
   certainly a directory (see Rules). Add such a rule by hand if it is needed.

3. The policy has a deny or log rule for exactly this target (the same target
   path, written without a trailing / or *) and a subject that overlaps the
   subject of the new rule, whether the rule is above or below the blocking
   rule: the allow is NOT added, the existing rule is kept, and the tool
   prints

    NOT ADDED: policy line N is a deny rule for exactly this target and a
    subject that overlaps the new rule: ...

   (or "a log rule"). The new subject is a pattern (libfoo.so.1.2.3 becomes
   libfoo.so*), so it also overlaps rules written for other versions of the
   library or for the exact library file. Existing log rules are never changed
   to allow. Broader deny and log rules (for a directory, or a catch-all) do
   not count: the new allow is placed above them.

4. The policy already has an allow with that subject and target, but an
   earlier deny or log rule overrides it: the allow is NOT added, the existing
   rules are kept, and the tool prints

    NOT ADDED: policy line N already has an identical allow, but it is
    shadowed by an earlier deny rule at line M: ...

   Move or fix the rules by hand if that is not what you want.

5. The rule that decides is a log rule that covers more than the access (a
   wider subject or target): a new allow is inserted above it, and the tool
   prints

    INFO: the allow for SUBJECT TARGET will shadow the log rule at policy
    line N: ...

   The wider log rule is left alone; it just stops logging this access.

6. Otherwise a deny decides: a new allow is inserted above that deny, so it
   is reached first.

When several libraries (libfoo.so.1, libfoo.so.2) get the same rule, it is
written once, above the earliest of their blocking rules.

# Summary

At the end the tool prints how many unique accesses were found, how many were
already allowed, how many rules were added, how many were not added and why,
and how many log lines could not be parsed. If anything was not added or not
read, it says so. "nothing to insert" does not mean that everything is
allowed: check the NOT ADDED, SKIPPED and UNPARSED messages.

# Where new rules go

A new allow goes right before the rule that blocks it, because the first
match wins and an allow after the blocking rule would never be reached. If
comment lines (with blank lines in between or around them) directly precede
that rule, the new lines go above the comments, so they stay with the rule
they describe, for example above

    # Catch-all: everything else for HAL is denied
    /hal/    /    deny

Several new rules for the same place are written in sorted order. Existing
lines are never reordered or removed. Rules are written as
"SUBJECT TARGET allow", separated by single spaces.

# Safety

- A line that the library would reject (not three fields, a verdict other
  than allow, deny or log, a path not starting with /, or a comment after the
  rule) stops the tool with an error before anything is written. The library
  drops the whole policy on such a line, so the tool refuses to build on it.
- Before anything is written, the new policy is read back and checked against
  every logged access: each access that got a rule must be allowed, and every
  other logged access must keep its verdict. If not, the tool prints
  "ERROR: verification failed" and does not modify the policy. This is a safety
  net; the checks above should already prevent it.
- --dry-run prints and verifies what would change and does not touch the
  policy.
- The policy file is rewritten in place. Run it in a git checkout.
