FAPOLICYD.RULES:(5)

NAME
compiled.rules - compiled fapolicyd rules to determine access rights fapolicyd.rules - deprecated fapolicyd rules to determine access rights

DESCRIPTION
compiled.rules is a file that is compiled by fagenrules which contains the rules that fapolicyd uses to make decisions about access rights. The rules follow a simple format of:
decision perm subject : object
They are evaluated from top to bottom with the first rule to match being used for the access control decision. The colon is mandatory to separate subject and object since they share keywords.

  Decision
The decision is either allow, deny, allow_audit, deny_audit, allow_syslog, deny_syslog, allow_log, or deny_log. If the rule triggers, this is the access decision that fapolicyd will tell the kernel. If the decision is one of the audit variety, then the decision will trigger a FANOTIFY audit event with all relevant information. You must have at least one audit rule loaded to generate an audit event. If the decision is one of the syslog variety, then the decision will trigger writing an event into syslog. If the decision is of one the log variety, then it will create an audit event and a syslog event. Regardless of the notification, any rule with a deny in the keyword will deny access and any with an allow in the keyword will allow access.

  Perm
Perm describes what kind permission is being asked for. The permission is either open, execute, or any. If none are given, then open is assumed.

  Subject
The subject is the process that is performing actions on system resources. The fields in the rule that describe the subject are written in a name=value format. There can be one or more subject fields. Each field is and'ed with others to decide if a rule triggers. The name values can be any of the following:

    all This matches against any subject. When used, this must be the only subject in the rule.

    auid
        This is the login uid that the audit system assigns users when they log in to the system. When the audit login uid is unset, as is common for system daemons and kernel threads, the kernel value is -1 and fapolicyd reports it as 4294967295. The auid attribute is unsigned, so rules must use auid=4294967295; auid=-1 is invalid. The given value may otherwise be numeric or the account name.

    uid This is the user id that the program is running under. The given value may be numeric or the account name.

    gid This is the group id that the program is running under. The given value may be numeric or the group name.

    sessionid
        This is the numeric session id that the audit system assigns to users when they log in. Daemons have a value of -1.

    pid This is the numeric process id that a program has.

    ppid
        This is the numeric process id of the program's parent. Note that programs that are orphaned or started directly from systemd have a ppid value of 1. Kernel threads have a ppid value of 2.

    trust
        This is a boolean describing whether it is required for the subject to be in the trust database or not. A value of 1 means its required while 0 means its not. Trust checking is extended by the integrity setting in fapolicyd.conf. When trust is used on the subject, it could be a daemon. If that daemon gets updated on disk, the trustdb will be updated to the new SHA256 hash. If the integrity setting is not none, the running daemon is not likely to be trusted unless it gets restarted. The default rules are not written in a way that this would happen. But this needs to be highlighted as it may not be obvious when writing a new rule.

    comm
        This is the shortened command name. When an interpreter starts a program, it usually renames the program to the script rather than the interpreter.

    exe This is the full path to the executable. Values are exact paths unless they use the explicit glob: pattern prefix. See PATH GLOBBING below. You may also use the special keyword untrusted to match on the subject not being listed in the rpm database.

    dir If you wish to match a directory, then use this by giving the full path to the directory. Its recommended to end with the / to ensure it matches a directory. There are 3 keywords that dir supports: execdirs, systemdirs, untrusted.

        execdirs
            The execdirs option will match against the following list of directories:

            /usr/
                /bin/ /sbin/ /lib/ /lib64/ /usr/libexec/

        systemdirs
            The systemdirs option will match against the same list as execdirs but also includes /etc/.

        untrusted
            The untrusted option will look up the current executable's full path in the rpm database to see if the executable is known to the system. The rule will trigger if the file in question is not in the trust database. This option is deprecated in favor of using object trust with execute permission when writing rules. Rules that use dir=untrusted emit a deprecation warning when parsed, and this compatibility option will be removed in a future release. For compatibility with older policies, a subject dir=untrusted rule paired with a path object at the same rule field position will not match a trusted object. This subject/object cross-reference is obsolete; use explicit object trust rules for new policy.

    ftype
        This option takes the mime type of a file as an argument. Mime is determined based on magic patterns files. Fapolicyd uses two precompiled magic files /usr/share/fapolicyd/fapolicyd-magic.mgc and /usr/share/misc/magic.mgc. To compile a magic file run the command file -C -m <magic file>. If you wish to check the mime type of a file while writing rules, run the following command:
        fapolicyd-cli --ftype /path-to-file

    pattern
        There are various ways that an attacker may try to execute code that may reveal itself in the pattern of file accesses made during program startup. This rule can take one of several options depending on which access patterns is wished to be blocked. Fapolicyd is able to detect these different access patterns and provide the access decision as soon as it identifies the pattern. The pattern type can be any of:

        normal
            This matches against any ELF program that is dynamically linked.

        ld_so
            This matches against access patterns that indicate that the program is being started directly by the runtime linker.

        ld_preload
            This matches against access patterns that indicate that the program is being started with either LD_PRELOAD or LD_AUDIT present in the environment. Note that even without this rule, you have protection against LD_PRELOAD of unknown binaries when the rules are written such that trust is used to determine if a library should be opened. In that case, the preloaded library would be denied but the application will still execute. This rule makes it so that even trusted libraries can be denied and the application will not execute.

        static
            This matches against ELF files that are statically linked.

  Object
The object is the file that the subject is interacting with. The fields in the rule that describe the object are written in a name=value format. There can be one or more object fields. Each field is and'ed with others to decide if a rule triggers. The name values can be any of the following:

    all This matches against any object. When used, this must be the only object in the rule.

    path
        This is the full path to the file that will be accessed. Values are exact paths unless they use the explicit glob: pattern prefix. See PATH GLOBBING below. You may also use the special keyword untrusted to match on the object not being listed in the rpm database.

    dir If you wish to match on access to any file in a directory, then use this by giving the full path to the directory. Its recommended to end with the / to ensure it matches a directory. There are 3 keywords that dir supports: execdirs, systemdirs, untrusted. See the dir option under Subject for an explanation of these keywords.

    device
        This option will match against the device that the file being accessed resides on. To use it, start with /dev/ and add the target device name.

    ftype
        This option matches against the mime type of the file being accessed. See ftype under Subject for more information on determining the mime type.

    trust
        This is a boolean describing whether it is required for the object to be in the trust database or not. A value of 1 means its required while 0 means its not. Trust checking is extended by the integrity setting in fapolicyd.conf.

    FILE_HASH
        (the legacy keyword SHA256HASH is still accepted) This option matches against the hash of the file being accessed. The accepted hash length follows the trust source, so RPM entries built with SHA512 store SHA512 digests while other sources may continue to provide SHA256. The hash in the rules should be all lowercase letters and do NOT start with 0x. Lowercase is the default output of sha256sum and sha512sum. The SHA256HASH keyword remains for compatibility but is deprecated; prefer FILE_HASH for new rules.

QUOTED VALUES
Rule fields are separated by spaces. Enclose a complete string value in double quotes when it contains a space. The surrounding quotes are syntax and are not part of the value used for matching. This applies consistently to subject exe , subject and object dir , and object path:
allow perm=execute all : dir="/tmp/first last/"
allow perm=execute exe="/tmp/first last/tool" : all
allow perm=execute all : path="/tmp/first last/tool"
Double quotes can also be used in named string-set definitions and in each comma-separated string value. Within a quoted value, a backslash before a double quote or another backslash represents that character. Other backslashes are preserved, so glob patterns retain their normal backslash behavior. Percent escapes are not interpreted in rules. For example, dir="/tmp/first%20last/" matches a literal percent sign followed by 20, not a space. A backslash before a space and shell-style single quotes are not rule-file quoting forms; double quotes are the only supported way to include a literal space in a rule value.

PATH GLOBBING
Rule values use exact matching by default. Prefix a value with glob: to request pattern matching. This explicit marker distinguishes exact paths from patterns, so path names containing literal *, ?, or [ characters keep their historical exact-match meaning. For example, path=/tmp/name[1] matches only that literal path, while path=glob:/tmp/name[1] uses [1] as a bracket expression and matches /tmp/name1. The glob: prefix is supported only by the subject exe and object path attributes, and the pattern after the prefix must be an absolute path. The dir attribute remains a literal directory prefix; use it instead of a glob when a rule applies to a complete subtree rooted at a fixed directory. An unmarked dir value containing metacharacters remains literal. A glob: value assigned to dir, or to any attribute other than exe and path, is a rule error, including when the value comes from a named set. Use subject exe or object path when glob matching is required. The pattern following glob: uses fnmatch(3) syntax. A * matches zero or more characters, ? matches one character, and bracket expressions such as [0-9] match one character from the expression. Matching is case-sensitive and must cover the complete path. Wildcards do not match a slash, so each variable path component must be written explicitly. A wildcard does not match a leading period in a path component unless the period appears explicitly in the pattern. Backslash quotes a metacharacter. Recursive ** globstar matching is not supported. When a glob value contains a space, quote the complete value, including the glob: prefix. For example:
allow perm=execute all : path="glob:/tmp/first last/*"
allow perm=execute exe="glob:/tmp/first last/*" : all
The quotes are removed before glob validation and matching. The dir attribute remains a literal prefix and does not accept a quoted or unquoted glob: value. Bracket ranges such as [a-z] and named character classes such as [[:alpha:]] use the locale active in the daemon. Avoid ranges and named classes when policy must produce identical matches in every locale; use exact alternatives or enumerate the intended characters instead. For example, glob:/home/*/bin/tool matches /home/alice/bin/tool but not /home/alice/project/bin/tool or /home/.admin/bin/tool. Each pattern in a comma-separated attribute value or named string set needs its own glob: prefix. Exact paths and patterns may be mixed in the same set. Rule order is unchanged: rules are evaluated from top to bottom, and the first complete rule match determines the decision. The concrete object path is obtained from the event file descriptor through /proc/self/fd/, and the subject exe path is obtained through /proc/PID/exe. Symbolic links have therefore already been followed, so a rule normally matches the reported target path rather than the symbolic link name used by the application. Hard links and mount aliases can expose different concrete paths for the same inode; policy that must cover those aliases needs an exact value or pattern for each reported path. Globbing selects paths; it does not expand the pattern, alter the path reported in logs, or make matching files trusted. Trust database entries continue to use concrete paths. An allow rule for a user-writable location should normally pair an object path glob with object trust=1 or an explicit FILE_HASH constraint. A subject exe glob in a user-writable location should normally require subject trust=1. Otherwise, any file whose name fits the pattern can receive the allow decision. Prefer exact paths, named sets of exact paths, or dir when they express the policy because exact and prefix matching are less expensive than glob evaluation.
deny_audit perm=execute all : path=glob:/opt/vendor/product-*/bin/tool
allow perm=execute all : path=glob:/home/*/bin/tool trust=1

SETS
Set is a named group of values of the same type. Fapolicyd internally distinguishes between INT and STRING set types. You can define your own set and use it as a value for a specific rule attribute. The definition is in key=value syntax and starts with a set name. The set name has to start with '%' and the rest is alphanumeric or '_'. The value is a comma separated list. The set type is inherited from the first item in the list. If that can be turned into number then whole list is expected to carry numbers. One can use these sets as a value for subject and object attributes. It is also possible to use a plain list as an attribute value without previous definition. The assigned set has to match the attribute type. It is not possible set groups for TRUST and PATTERN attributes.

  SETS EXAMPLES
# definition
# string set
%python=/usr/bin/python2.7,/usr/bin/python3.6
allow exe=%python : all trust=1
#
# definition
# number set
%uuids=0,1000
allow uid=%uuids : all

  LANGUAGE SET EXTENSIONS
The installed %languages set identifies MIME types that policy treats as language or interpreter content. A package may add a MIME type without modifying the distribution-owned definition by placing a naturally ordered macro fragment after 10-languages.rules and before policy rules. For example, 11-vendor-languages.rules can contain:
%languages+=application/x-vendor-script
The += form is available only for %languages. It requires the base set to have been defined already, does not replace it, and treats an existing MIME type as a no-op. Run fagenrules after adding or removing a fragment. The added value must also be a MIME type emitted by fapolicyd's file type detector or the system magic database; adding it to the set alone does not add file type detection.

NOTES
When writing rules, you should keep them focused to one goal and store them in one file. These rule files are kept in the /etc/fapolicyd/rules.d directory. During daemon startup, fagenrules will run and compile all these component files into one master file, compiled.rules. See the fagenrules man page for more information. When you are writing a rule for the execute permission, remember that the file to be executed is an object. For example, you type ssh into the shell. The shell calls execve on /usr/bin/ssh. At that instant in time, ssh is the object that bash is working on. However, if you are blocking execution from a specific program, then you would normally state the program on the subject side and use all for the object side. If you are writing rules that use patterns, just select any as the permission to be clear that this applies to anything. In reality, pattern matching ignores the permission but the suggestion is for documentation purposes. Some interpreters do not immediately read all lines of input. Rather, they read content as needed until they get to end of file. This means that if they do stuff like networking or sleeping or anything that takes time, someone with the privileges to modify the file can add to it after the file's integrity has been checked. This is not unique to fapolicyd, it's simply how things work. Make sure that trusted file permissions are not excessive so that no unexpected file content modifications can occur.

EXAMPLES
The following rules illustrate the rule syntax.
deny_audit perm=open exe=/usr/bin/wget : dir=/tmp
allow perm=open exe=/usr/bin/python3.7 : ftype=text/x-python trust=1
deny_audit perm=any pattern ld_so : all
deny perm=any all : all

SEE ALSO
fapolicyd(8), fagenrules (8), fapolicyd-cli(8), fnmatch(3), and fapolicyd.conf(5)

AUTHOR
Steve Grubb