Commit Graph

3557 Commits

Author SHA1 Message Date
Christian Göttsche
0e2a78d5b2 libsepol: free initial sid names
Commit 55b75a2c ("libsepol: stop translating deprecated intial SIDs to
strings") dropped several names of obsolete initial sids ans replaced
them with NULL.  This leads to their printable string being dynamically
allocated but not free'd.
Instead of keeping track of which name was allocated dynamically and
which not, allocate all on the heap, which simplifies the later cleanup.

While on it also free the name in case of a strs_add_at_index() failure.

Reported-by: oss-fuzz (issue 60271)
Fixes: 55b75a2c ("libsepol: stop translating deprecated intial SIDs to strings")

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:59:27 -04:00
Juraj Marcin
0c50de03cd libsepol/cil: add support for prefix/suffix filename transtions to CIL
This patch implements the support for prefix/suffix filename transitions
in the CIL structures as well as in the CIL policy parser.

Syntax of the new prefix/suffix filename transition rule:

    (typetransition source_type_id target_type_id class_id object_name match_type default_type_id)

where match_type is either the keyword "prefix" or "suffix".

Examples:

    (typetransition ta tb CLASS01 "file01" prefix td)
    (typetransition td te CLASS01 "file02" suffix tf)

Reviewed-by: Ondrej Mosnacek <omosnace@redhat.com>
Signed-off-by: Juraj Marcin <juraj@jurajmarcin.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:58:28 -04:00
Juraj Marcin
c39ebd07ac checkpolicy,libsepol: add prefix/suffix support to module policy
This patch extends the structures for module and base policy (avrule_t)
to support prefix/suffix transitions. In addition to this, it implements
the necessary changes to functions for reading and writing the binary
policy, as well as parsing the policy conf.

Syntax of the new prefix/suffix filename transition rule:

    type_transition source_type target_type : class default_type object_name match_type;

where match_type is either keyword "prefix" or "suffix"

Examples:

    type_transition ta tb:CLASS01 tc "file01" prefix;
    type_transition td te:CLASS01 tf "file02" suffix;

Reviewed-by: Ondrej Mosnacek <omosnace@redhat.com>
Signed-off-by: Juraj Marcin <juraj@jurajmarcin.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:58:25 -04:00
Juraj Marcin
1174483d29 checkpolicy,libsepol: add prefix/suffix support to kernel policy
Currently, filename type transitions support only exact name matching.
However, in practice, the names contain variable parts. This leads to
many duplicated rules in the policy that differ only in the part of the
name, or it is even impossible to cover all possible combinations.

This patch extends the filename type transitions structures to include
new types of filename transitions - prefix and suffix filename
transitions. It also implements the reading and writing of those rules
in the kernel binary policy format together with increasing its version.

Reviewed-by: Ondrej Mosnacek <omosnace@redhat.com>
Signed-off-by: Juraj Marcin <juraj@jurajmarcin.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:58:24 -04:00
Juraj Marcin
11013986ac libsepol: implement new module binary format of avrule
Implement a new module policy format that closely matches the new
internal representation of avrule introduced in the previous patch.

This patch bumps the maximum module policy version and implements
reading/writing functions such that the module binary policy structure
matches its internal representation, namely, the object name attribute
used for the filename transition rules.

These changes have no significant effect on the size of the module
policy file (tested with Fedora policy).

Reviewed-by: Ondrej Mosnacek <omosnace@redhat.com>
Signed-off-by: Juraj Marcin <juraj@jurajmarcin.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:58:22 -04:00
Juraj Marcin
7b77edd919 libsepol: implement new kernel binary format for avtab
Implement a new binary policy format that closely matches the new
internal representation introduced in the previous patch.

This patch bumps the maximum kernel policy version and implements
reading/writing functions such that kernel binary policy structure
matches internal representation.

These changes can cause the binary policy to grow in size due to
effectively undoing the benefits of the commit 8206b8cb ("libsepol:
implement POLICYDB_VERSION_COMP_FTRANS "), but this will be mitigated by
adding the prefix/suffix support as described in the previous patch.

Reviewed-by: Ondrej Mosnacek <omosnace@redhat.com>
Signed-off-by: Juraj Marcin <juraj@jurajmarcin.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:58:21 -04:00
Juraj Marcin
565d87489b checkpolicy,libsepol: move filename transition rules to avrule
Similarly to the previous patch, filename transition rules are stored
and parsed separately from other type enforcement rules. Moving them to
avrule makes it consistent with the filename transitions in avtab and
makes future improvements easier to implement.

This patch adds an optional object name attribute to the avrule
structure and uses this new attribute to move filename transition rules
to avrule. It also updates functions for parsing type enforcement rules
to accept rules with a filename as their last argument (filename
transition rules), separate functions for parsing filename transitions
are therefore no longer needed.

Reviewed-by: Ondrej Mosnacek <omosnace@redhat.com>
Signed-off-by: Juraj Marcin <juraj@jurajmarcin.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:58:19 -04:00
Juraj Marcin
e169fe2653 checkpolicy,libsepol: move filename transitions to avtab
Currently, filename transitions are stored separately from other type
enforcement rules. This leads to possibly sub-optimal performance and
makes further improvements cumbersome.

This patch adds a symbol table with filename transitions to the
transition structure added to avtab in the previous patch. It also
implements functions required for reading and writing filename
transitions (either binary or source formats) and updates the code for
expanding attributes. Last but not least, it updates the conflict check
in the conditional avtab to account for empty transitions in the
non-conditional avtab.

These changes are expected to cause higher memory usage, as now there
needs to be a filename transition structure for every stype. This patch
effectively undoes most of the commit 42ae834a ("libsepol,checkpolicy:
optimize storage of filename transitions"), but this will be mitigated
by providing support for matching prefix/suffix of the filename for
filename transitions in future patches which will reduce to need to have
so many of them.

Reviewed-by: Ondrej Mosnacek <omosnace@redhat.com>
Signed-off-by: Juraj Marcin <juraj@jurajmarcin.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:58:17 -04:00
Juraj Marcin
de708edf52 checkpolicy,libsepol: move transition to separate structure in avtab
To move filename transitions to be part of avtab, we need to create
space for it in the avtab_datum structure which holds the rule for
a certain combination of stype, ttype and tclass.

As only type transitions have a special variant that uses a filename, it
would be suboptimal to add a (mostly empty) pointer to some structure to
all avtab rules.

Therefore, this patch adds a new structure to the avtab_datum and moves
the otype of the transition to this structure. In the next patch, this
structure will also hold filename transitions for the combination of
stype, ttype and tclass.

Reviewed-by: Ondrej Mosnacek <omosnace@redhat.com>
Signed-off-by: Juraj Marcin <juraj@jurajmarcin.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:58:15 -04:00
Petr Lautrbach
b8d3f6e41c python: update python.pot
Signed-off-by: Petr Lautrbach <lautrbach@redhat.com>
Reviewed-by: Vit Mojzis <vmojzis@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:57:42 -04:00
Petr Lautrbach
5ad545180e semanage: Drop unnecessary import from seobject
sepolgen.module is not used for permissive domains

Signed-off-by: Petr Lautrbach <lautrbach@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:57:40 -04:00
Petr Lautrbach
181ae59276 python: Drop hard formating from localized strings
It confuses translators and new lines are dropped by parser module anyway.

Signed-off-by: Petr Lautrbach <lautrbach@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:57:36 -04:00
Petr Lautrbach
b945bf050a python: improve format strings for proper localization
If a string contains more than one unnamed argument it's hard for
translators to proper localize as they don't know which value is
represented by a unnamed argument. It also blocks them to use a
different order of arguments which would make better sense in other
languages.

Fixes:

    $ xgettext --default-domain=python -L Python --keyword=_ --keyword=N_ ../audit2allow/audit2allow ../chcat/chcat ../semanage/semanage ../semanage/seobject.py ../sepolgen/src/sepolgen/interfaces.py ../sepolicy/sepolicy/generate.py ../sepolicy/sepolicy/gui.py ../sepolicy/sepolicy/__init__.py ../sepolicy/sepolicy/interface.py ../sepolicy/sepolicy.py
    ../chcat/chcat:220: warning: 'msgid' format string with unnamed arguments cannot be properly localized:
                                 The translator cannot reorder the arguments.
                                 Please consider using a format string with named arguments,
                                 and a mapping instead of a tuple for the arguments.
    ../semanage/seobject.py:1178: warning: 'msgid' format string with unnamed arguments cannot be properly localized:
                                           The translator cannot reorder the arguments.
                                           Please consider using a format string with named arguments,
                                           and a mapping instead of a tuple for the arguments.
    ...

Signed-off-by: Petr Lautrbach <lautrbach@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-07-12 12:57:31 -04:00
Jeffery To
1a29c28afb python/sepolicy: Fix get_os_version except clause
This adds more exceptions to be handled by the except clause in
`get_os_version()`:

* If the `distro` package is not installed, then `import distro` raises
  a `ModuleNotFoundError` exception.

* The distro documentation[1] lists `OSError` and `UnicodeError` as
  exceptions that can be raised.

* Older versions of distro (<= 1.6.0) may also raise
  `subprocessCalledProcessError`[2].

[1]: https://github.com/python-distro/distro/blob/v1.8.0/src/distro/distro.py#L749-L753
[2]: https://github.com/python-distro/distro/blob/v1.6.0/distro.py#L726-L728

Signed-off-by: Jeffery To <jeffery.to@gmail.com>
Acked-by: Petr Lautrbach <lautrbach@redhat.com>
2023-06-30 14:27:34 +02:00
Huaxin Lu
d8edd363be libselinux: add check for calloc in check_booleans
Check the return value of calloc() to avoid null pointer reference.

Signed-off-by: Huaxin Lu <luhuaxin1@huawei.com>
Acked-by: Petr Lautrbach <lautrbach@redhat.com>
2023-06-30 14:26:56 +02:00
Masatake YAMATO
b3788b9ce9 dismod, dispol: reduce the messages in batch mode
A change in v2:
* pass `verbose' to sepol_module_package_read().

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
2023-06-30 14:21:22 +02:00
Masatake YAMATO
6e077ba726 dismod: print the policy version only in interactive mode
Instead, a new action, 'v' for printing the policy (and/or
module) version in batch mode is added.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-30 14:21:22 +02:00
Christian Göttsche
4c069224ff checkpolicy/dismod: misc improvements
* fix minus self formatting in neverallow rules, avoiding `~ - self`

* show neverallow and neverallowxperm rules

* whitespace improvements in output
  - avoid duplicate whitespaces before permission list, since
    sepol_av_to_string() already adds a trailing one
  - avoid duplicate whitespace after wildcard type
  - unify indentation for xperm rules

* drop unused global variables

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-30 11:54:06 +02:00
Christian Göttsche
b87724cbdd checkpolicy: add option to skip checking neverallow rules
Add the command line argument `-N/--disable-neverallow`, similar to
secilc(8), to checkpolicy(8) and checkmodule(8) to skip the check of
neverallow rule violations.

This is mainly useful in development, e.g. to quickly add rules to a
policy without fulfilling all neverallow rules or build policies with
known violations.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-30 11:53:44 +02:00
Ondrej Mosnacek
02e471f17e libsepol: add support for the new "init" initial SID
Resurrect the naming of the "init" initial SID, as it has been
reintroduced in the kernel. Also add the new "userspace_initial_context"
policy capability that is used to enable the new semantics for this
initial SID.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
2023-06-30 10:29:06 +02:00
Ondrej Mosnacek
55b75a2c7d libsepol: stop translating deprecated intial SIDs to strings
Many of the initial SIDs are no longer used by the kernel, so
translating them to the legacy names doesn't bring much value. Clear the
legacy names from the table and let the code translate them to the
fallback "unknown" names instead.

Note that this only affects the generated text output when converting
policies from binary to text form. The text policy languages let the
policy define its own names for the initial SIDs based on the order in
which they are declared, so the table is never used to convert from name
to SID. Thus this is just a cosmetic change and has no functional
impact.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-30 10:29:06 +02:00
Vit Mojzis
48306c4ef3 python/sepolicy: Add/remove user even when SELinux is disabled
Use "semanage user -a/-d" in spec file generated by "sepolicy generate"
even when SELinux is disabled. The command works properly when SELinux
is disabled and with this change the user will be present once SELinux
is re-enabled.
Also, do not execute the command when the package is updated, only when
it is first installed.

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
Acked-by: Petr Lautrbach <lautrbach@redhat.com>
2023-06-27 16:01:26 +02:00
Vit Mojzis
391cf12600 python/sepolicy: Fix template for confined user policy modules
The following commit
330b0fc333
changed the userdom_base_user_template, which now requires a role
corresponding to the user being created to be defined outside of the
template.
Similar change was also done to fedora-selinux/selinux-policy
e1e216b25d

Although I believe the template should define the role (just as it
defines the new user), that will require extensive changes to refpolicy.
In the meantime the role needs to be defined separately.

Fixes:
    # sepolicy generate --term_user -n newuser
    Created the following files:
    /root/a/test/newuser.te # Type Enforcement file
    /root/a/test/newuser.if # Interface file
    /root/a/test/newuser.fc # File Contexts file
    /root/a/test/newuser_selinux.spec # Spec file
    /root/a/test/newuser.sh # Setup Script

    # ./newuser.sh
    Building and Loading Policy
    + make -f /usr/share/selinux/devel/Makefile newuser.pp
    Compiling targeted newuser module
    Creating targeted newuser.pp policy package
    rm tmp/newuser.mod tmp/newuser.mod.fc
    + /usr/sbin/semodule -i newuser.pp
    Failed to resolve roleattributeset statement at /var/lib/selinux/targeted/tmp/modules/400/newuser/cil:8
    Failed to resolve AST
    /usr/sbin/semodule:  Failed!

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
Acked-by: Petr Lautrbach <lautrbach@redhat.com>
2023-06-12 19:45:19 +02:00
Masatake YAMATO
666a7dfdc8 dispol: add --actions option for non-interactive use
Example session:

    $ ./dispol --help
    Usage:
     ./dismod [OPTIONS] binary_pol_file

    Options:
     -h, --help              print this help message
     -a, --actions ACTIONS   run non-interactively

    Actions:
      1    display unconditional AVTAB
      2    display conditional AVTAB (entirely)
      3    display conditional AVTAB (only ENABLED rules)
      4    display conditional AVTAB (only DISABLED rules)
      5    display conditional bools
      6    display conditional expressions
      8    display role transitions
      c    display policy capabilities
      b    display booleans
      C    display classes
      r    display roles
      t    display types
      a    display type attributes
      p    display the list of permissive types
      u    display unknown handling setting
      F    display filename_trans rules

    $ ./dispol --actions 1 /etc/selinux/targeted/policy/policy.33  | head
    Reading policy...
    libsepol.policydb_index_others: security:  8 users, 15 roles, 5408 types, 358 bools
    libsepol.policydb_index_others: security: 1 sens, 1024 cats
    libsepol.policydb_index_others: security:  134 classes, 86750 rules, 8818 cond rules
    binary policy file loaded

    allow abrt_dump_oops_t sssd_var_lib_t : sock_file { write getattr append open };
    type_transition authconfig_t entropyd_initrc_exec_t : process initrc_t;
    type_transition glusterd_t syslogd_initrc_exec_t : process initrc_t;xxxx

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-08 15:13:21 -04:00
Masatake YAMATO
f8a076f1ca dispol: handle EOF in user interaction
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-08 15:13:19 -04:00
Masatake YAMATO
eeb0a75119 dispol: delete an unnecessary empty line
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-08 15:13:17 -04:00
Masatake YAMATO
f78eea5a34 dispol: add --help option
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-08 15:13:15 -04:00
Topi Miettinen
6360af7a98 sepolicy: clarify manual page of sepolicy interface
Expand the description to make it more clear what "interfaces" mean
here. They're different from network interfaces used by SELinux
command `semanage interface`.

Add a note that the information comes from on-disk file which has been
installed and it doesn't necessarily match the policy loaded to the
kernel.

Signed-off-by: Topi Miettinen <toiwoton@gmail.com>
Acked-by: Petr Lautrbach <lautrbach@redhat.com>
2023-06-08 15:13:04 -04:00
Vit Mojzis
d596efb4ea libselinux: Add examples to man pages
Also fix some typos and remove trailing whitespaces.

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
Acked-by: Petr Lautrbach <lautrbach@redhat.com>
2023-06-08 15:10:11 -04:00
Vit Mojzis
966de0c89e checkpolicy: Add examples to man pages
Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
Acked-by: Petr Lautrbach <lautrbach@redhat.com>
2023-06-08 15:10:10 -04:00
Vit Mojzis
535dc2479b sandbox: Add examples to man pages
While at it, remove trailing whitespaces.

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
Acked-by: Petr Lautrbach <lautrbach@redhat.com>
2023-06-08 15:10:08 -04:00
Vit Mojzis
0b1cb09ad3 python/sepolicy: Improve man pages
- Add missing options
- Add examples
- Emphasize keywords
- Remove trailing whitespaces

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
Acked-by: Petr Lautrbach <lautrbach@redhat.com>
2023-06-08 15:10:06 -04:00
Vit Mojzis
e867c95ba4 policycoreutils: Add examples to man pages
While at it, remove trailing whitespaces.

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
Acked-by: Petr Lautrbach <lautrbach@redhat.com>
2023-06-08 15:10:05 -04:00
Masatake YAMATO
df0b1929a0 dismod: add --actions option for non-interactive use
Example session:

    $ ./dismod --help
    Usage:
     ./dismod [OPTIONS] binary_pol_file

    Options:
     -h, --help              print this help message
     -a, --actions ACTIONS   run non-interactively

    Actions:
      1    display unconditional AVTAB
      2    display conditional AVTAB
      3    display users
      4    display bools
      5    display roles
      6    display types, attributes, and aliases
      7    display role transitions
      8    display role allows
      9    Display policycon
      0    Display initial SIDs
      a    Display avrule requirements
      b    Display avrule declarations
      c    Display policy capabilities
      u    Display the unknown handling setting
      F    Display filename_trans rules

    $ ./dismod --actions 16 input.mod
    Reading policy...
    libsepol.policydb_index_others: security:  0 users, 1 roles, 2 types, 0 bools
    libsepol.policydb_index_others: security: 0 sens, 0 cats
    libsepol.policydb_index_others: security:  1 classes, 0 rules, 0 cond rules
    libsepol.policydb_index_others: security:  0 users, 1 roles, 2 types, 0 bools
    libsepol.policydb_index_others: security: 0 sens, 0 cats
    libsepol.policydb_index_others: security:  1 classes, 0 rules, 0 cond rules
    Binary policy module file loaded.
    Module name: input
    Module version: 1.0.0
    Policy version: 21

    unconditional avtab:
    --- begin avrule block ---
    decl 1:
      allow [httpd_t] [http_port_t] : [tcp_socket] { name_bind };

     [http_port_t] [2]: type flags:0
     [httpd_t] [1]: type flags:0

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-08 15:09:34 -04:00
Masatake YAMATO
d1a9cddfd2 dismod: handle EOF in user interaction
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-08 15:09:33 -04:00
Masatake YAMATO
5b1a2f1d10 dismod: delete an unnecessary empty line
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-08 15:09:31 -04:00
Masatake YAMATO
5045368da3 dismod: add --help option
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-08 15:09:29 -04:00
Christian Göttsche
30fe0f1962 libsepol: replace log_err() by ERR()
Use the libsepol internal wrapper ERR() with a NULL handler to emit
error messages.  ERR() will besides adding a prefix of "libsepol" also
write to stderr.  One benefit is the option to suppress the messages via
sepol_debug(), although marked deprecated, e.g. in fuzzers.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:20:31 -04:00
Christian Göttsche
5c35a7bed0 libsepol: replace sepol_log_err() by ERR()
Use the libsepol internal wrapper ERR() with a NULL handler to emit
error messages.  ERR() will besides adding a prefix of "libsepol" also
write to stderr.  One benefit is the option to suppress the messages via
sepol_debug(), although marked deprecated, e.g. in fuzzers.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:20:30 -04:00
Christian Göttsche
b041ecc6a5 libsepol: drop duplicate newline in sepol_log_err() calls
sepol_log_err() will already append a newline unconditionally.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:20:28 -04:00
Christian Göttsche
808a43ab88 libsepol: drop message for uncommon error cases
Match surrounding code and the message were quite generic too.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:20:26 -04:00
Christian Göttsche
cae65d9a10 libsepol: expand: skip invalid cat
Bail out on expanding levels with invalid low category.

UBSAN report:

    expand.c:952:21: runtime error: unsigned integer overflow: 0 - 1 cannot be represented in type 'uint32_t' (aka 'unsigned int')

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:19:02 -04:00
Christian Göttsche
4ba8f7c38f libsepol: validate: reject XEN policy with xperm rules
XEN policies with extended permissions are not supported, e.g. writing
them will fail (see write.c:avrule_write()).

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:19:00 -04:00
Christian Göttsche
ac015a3996 libsepol: validate: check low category is not bigger than high
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:18:58 -04:00
Christian Göttsche
4cf37608b5 libsepol: validate old style range trans classes
For old style range transition rules the class defaults to process.
However the policy might not declare the process class leading to
setting a wrong bit later on via:

    if (ebitmap_set_bit(&rtr->tclasses, rt->target_class - 1, 1))

UBSAN report:

    policydb.c:3684:56: runtime error: unsigned integer overflow: 0 - 1 cannot be represented in type 'uint32_t' (aka 'unsigned int')

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:18:56 -04:00
Christian Göttsche
45a4fc77e1 libsepol: validate some object contexts
Ensure various object context entries have a name, since they are
duplicated via strdup(3), and the order for ports and memory regions is
valid.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:18:53 -04:00
Christian Göttsche
f5d664ebeb libsepol: dump non-mls validatetrans rules as such
The functions constraint_expr_to_str() prepare a string representation
for validatetrans and mlsvalidatetrans rules.  To decide what keyword to
use the type of expression is consulted.  Currently the extra target
type (CEXPR_XTARGET) is considered to be an MLS statement while its not,
e.g.:

    validatetrans CLASS1 t3 == ATTR1;

Actually check for MLS expression types only.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:17:03 -04:00
Christian Göttsche
00728e12d4 checkpolicy: only set declared permission bits for wildcards
When setting permission bits from a wildcard or complement only set the
bits for permissions actually declared for the associated class.  This
helps optimizing the policy later, since only rules are dropped with a
complete empty permission bitset.  Example policy:

    class CLASS1
    sid kernel
    class CLASS1 { PERM1 }
    type TYPE1;
    bool BOOL1 true;
    allow TYPE1 self : CLASS1 { PERM1 };
    role ROLE1;
    role ROLE1 types { TYPE1 };
    if ! BOOL1 { allow TYPE1 self: CLASS1 *; }
    user USER1 roles ROLE1;
    sid kernel USER1:ROLE1:TYPE1

Also emit a warning if a rule will have an empty permission bitset due
to an exhausting complement.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:17:00 -04:00
Christian Göttsche
c646f3904d checkpolicy: reject condition with bool and tunable in expression
If tunables are not preserved (the mode unconditionally used by
checkpolicy) an expression must not consist of booleans and tunables,
since such expressions are not supported during expansion (see expand.c:
discard_tunables()).

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:16:59 -04:00
Christian Göttsche
2d5f97b870 checkpolicy: drop unused token CLONE
The token CLONE is never used in the grammar; drop it.

As side effect `clone` and `CLONE` become available as identifier names.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-06-05 15:16:57 -04:00