Skip to contentSkip to conversation

Semanage fcontext with incorrect regexp

Bug report Vit Mojzis 3 participants 3 messages Aug 16, 2016 SELinux security module

lore
Share & tools
lore

Vit Mojzis

author
#0 lore raw

When given invalid regexp, semanage reports error, but adds the invalid context to "/etc/selinux/targeted/contexts/files/file_contexts.local", which breaks the system (won't boot in enforcing mode). The new context doesn't show up on "#semanage fcontext -l -C" and cannot be removed by "semanage fcontext --delete".

Investigation on Fedora version of libsemanage showed that "file_contexts.local" is not backed up before the new version is installed into the system and therefore cannot be restored after failure. For more details see: https://bugzilla.redhat.com/show_bug.cgi?id=1362041

# semanage fcontext -a -t httpd_exec_t "(/.*)?"
        specfiles SHA1 digest: 980289cabd78157523679695fd2e4fd0a5b5ff05
        calculated using the following specfile(s):
/etc/selinux/targeted/contexts/files/file_contexts.subs_dist
        /etc/selinux/targeted/contexts/files/file_contexts.subs
        /etc/selinux/targeted/contexts/files/file_contexts
/etc/selinux/targeted/contexts/files/file_contexts.homedirs
        /etc/selinux/targeted/contexts/files/file_contexts.local
        libsemanage.semanage_exec_prog: Child process 
/sbin/sefcontext_compile did not exit cleanly. (No such file or directory).
        libsemanage.sefcontext_compile: sefcontext_compile returned 
error code -1. Compiling /etc/selinux/targeted/contexts/files/file_contexts.local (No such file or directory).
        specfiles SHA1 digest: 5097e1780892f53aedb6d30d5d61206a159a20e7
        calculated using the following specfile(s):
/etc/selinux/targeted/contexts/files/file_contexts.subs_dist
        /etc/selinux/targeted/contexts/files/file_contexts.subs
        /etc/selinux/targeted/contexts/files/file_contexts.bin
/etc/selinux/targeted/contexts/files/file_contexts.homedirs
        /etc/selinux/targeted/contexts/files/file_contexts.local
        libsemanage.semanage_exec_prog: Child process 
/sbin/sefcontext_compile did not exit cleanly. (No such file or directory).
        libsemanage.sefcontext_compile: sefcontext_compile returned 
error code -1. Compiling /etc/selinux/targeted/contexts/files/file_contexts.local (No such file or directory).
        FileNotFoundError: [Errno 2] No such file or directory
# semanage fcontext -l -C
    -
# cat /etc/selinux/targeted/contexts/files/file_contexts.local
        # This file is auto-generated by libsemanage
        # Do not edit directly.
        (/.*)?    system_u:object_r:httpd_exec_t:s0
# semanage fcontext -d -t httpd_exec_t "(/.*)?"
        ValueError: File context for (/.*)? is not defined
#1 lore raw
Quoted 6 lines · Vit Mojzis

When given invalid regexp, semanage reports error, but adds the invalid context to "/etc/selinux/targeted/contexts/files/file_contexts.local", which breaks the system (won't boot in enforcing mode). The new context doesn't show up on "#semanage fcontext -l -C" and cannot be removed by "semanage fcontext --delete".

Slightly related to this. Now that we have this pretty nice priorities feature and CIL in general. Can we not take advantage of that and consider unifying the stuff in /var/lib/selinux where possible.

CIL should be perfect for things like this.

For example instead of maintaining these file_contexts.local files, just make (lib)semanage load cil modules on some specified priority for these type of customization's. That way the stuff is more uniform (its pretty much all in cil modules and written in cil instead of these {port_contexts,file_context}.local files etc. (there is then also automatically a module to remove any invalid specs)

That will not deal with any regular expression validation, but if we could make secilc smart enough to identify invalid regular expressions then we have a solution for this in a central place as well.

I am pretty sure that I am overlooking things here but it seems like a good idea to me.

Quoted 51 lines

Investigation on Fedora version of libsemanage showed that "file_contexts.local" is not backed up before the new version is installed into the system and therefore cannot be restored after failure. For more details see: https://bugzilla.redhat.com/show_bug.cgi?id=1362041

# semanage fcontext -a -t httpd_exec_t "(/.*)?"
        specfiles SHA1 digest: 980289cabd78157523679695fd2e4fd0a5b5ff05
        calculated using the following specfile(s):
/etc/selinux/targeted/contexts/files/file_contexts.subs_dist
        /etc/selinux/targeted/contexts/files/file_contexts.subs
        /etc/selinux/targeted/contexts/files/file_contexts
/etc/selinux/targeted/contexts/files/file_contexts.homedirs
        /etc/selinux/targeted/contexts/files/file_contexts.local
        libsemanage.semanage_exec_prog: Child process
/sbin/sefcontext_compile did not exit cleanly. (No such file or directory).
        libsemanage.sefcontext_compile: sefcontext_compile returned
error code -1. Compiling
/etc/selinux/targeted/contexts/files/file_contexts.local (No such file
or directory).
        specfiles SHA1 digest: 5097e1780892f53aedb6d30d5d61206a159a20e7
        calculated using the following specfile(s):
/etc/selinux/targeted/contexts/files/file_contexts.subs_dist
        /etc/selinux/targeted/contexts/files/file_contexts.subs
        /etc/selinux/targeted/contexts/files/file_contexts.bin
/etc/selinux/targeted/contexts/files/file_contexts.homedirs
        /etc/selinux/targeted/contexts/files/file_contexts.local
        libsemanage.semanage_exec_prog: Child process
/sbin/sefcontext_compile did not exit cleanly. (No such file or directory).
        libsemanage.sefcontext_compile: sefcontext_compile returned
error code -1. Compiling
/etc/selinux/targeted/contexts/files/file_contexts.local (No such file
or directory).
        FileNotFoundError: [Errno 2] No such file or directory
# semanage fcontext -l -C
    -
# cat /etc/selinux/targeted/contexts/files/file_contexts.local
        # This file is auto-generated by libsemanage
        # Do not edit directly.
        (/.*)?    system_u:object_r:httpd_exec_t:s0
# semanage fcontext -d -t httpd_exec_t "(/.*)?"
        ValueError: File context for (/.*)? is not defined

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].

Signature · 3 lines
Key fingerprint = 5F4D 3CDB D3F8 3652 FBD8  02D5 3B6C 5F1D 2C7B 6B02
https://sks-keyservers.net/pks/lookup?op=get&search=0x3B6C5F1D2C7B6B02
Dominick Grift
#2 lore raw
In reply to Vit Mojzis
Quoted 42 lines · Vit Mojzis

When given invalid regexp, semanage reports error, but adds the invalid context to "/etc/selinux/targeted/contexts/files/file_contexts.local", which breaks the system (won't boot in enforcing mode). The new context doesn't show up on "#semanage fcontext -l -C" and cannot be removed by "semanage fcontext --delete".

Investigation on Fedora version of libsemanage showed that "file_contexts.local" is not backed up before the new version is installed into the system and therefore cannot be restored after failure. For more details see: https://bugzilla.redhat.com/show_bug.cgi?id=1362041

# semanage fcontext -a -t httpd_exec_t "(/.*)?"
        specfiles SHA1 digest: 980289cabd78157523679695fd2e4fd0a5b5ff05
        calculated using the following specfile(s):
/etc/selinux/targeted/contexts/files/file_contexts.subs_dist
        /etc/selinux/targeted/contexts/files/file_contexts.subs
        /etc/selinux/targeted/contexts/files/file_contexts
/etc/selinux/targeted/contexts/files/file_contexts.homedirs
        /etc/selinux/targeted/contexts/files/file_contexts.local
        libsemanage.semanage_exec_prog: Child process
/sbin/sefcontext_compile did not exit cleanly. (No such file or directory).
        libsemanage.sefcontext_compile: sefcontext_compile returned
error code -1. Compiling
/etc/selinux/targeted/contexts/files/file_contexts.local (No such file
or directory).
        specfiles SHA1 digest: 5097e1780892f53aedb6d30d5d61206a159a20e7
        calculated using the following specfile(s):
/etc/selinux/targeted/contexts/files/file_contexts.subs_dist
        /etc/selinux/targeted/contexts/files/file_contexts.subs
        /etc/selinux/targeted/contexts/files/file_contexts.bin
/etc/selinux/targeted/contexts/files/file_contexts.homedirs
        /etc/selinux/targeted/contexts/files/file_contexts.local
        libsemanage.semanage_exec_prog: Child process
/sbin/sefcontext_compile did not exit cleanly. (No such file or directory).
        libsemanage.sefcontext_compile: sefcontext_compile returned
error code -1. Compiling
/etc/selinux/targeted/contexts/files/file_contexts.local (No such file
or directory).
        FileNotFoundError: [Errno 2] No such file or directory
# semanage fcontext -l -C
    -
Quoted 9 lines
# cat /etc/selinux/targeted/contexts/files/file_contexts.local
        # This file is auto-generated by libsemanage
        # Do not edit directly.
        (/.*)?    system_u:object_r:httpd_exec_t:s0
# semanage fcontext -d -t httpd_exec_t "(/.*)?"
        ValueError: File context for (/.*)? is not defined

It seems like there are several different bugs here:
1) pcre_study() returning NULL is not necessarily an error. We did fix the handling in libselinux compile_regex() in commit 14342674191ca11c4455ff7ac9b76b05173eb575 but it appears that sefcontext_compile and libselinux load_mmap() don't handle a NULL study result correctly.

2) The file_contexts.local file should be validated earlier, before copying any files to /etc/selinux.

3) If errors are possible after copying files to /etc/selinux, then we need to unlink any of the local files as part of rolling back the transaction.