Commit graph

5339 commits

Author SHA1 Message Date
Christian Göttsche
956bda08f6
libselinux: restorecon: avoid printing NULL pointer
The variable `curcon` is NULL in case the file has no current security
context.  Most C standard libraries handle it fine, avoid it nonetheless
for standard conformance.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: Nicolas Iooss <nicolas.iooss@m4x.org>
2022-06-30 21:11:01 +02:00
Ondrej Mosnacek
8cc529af51
semodule: rename --rebuild-if-modules-changed to --refresh
After the last commit this option's name and description no longer
matches the semantic, so give it a new one and update the descriptions.
The old name is still recognized and aliased to the new one for
backwards compatibility.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
Acked-by: Nicolas Iooss <nicolas.iooss@m4x.org>
2022-06-30 21:09:28 +02:00
Ondrej Mosnacek
bdbe52be1b
libsemanage: always write kernel policy when check_ext_changes is specified
For the use case of rebuilding the policy after package updates, we need
the check_ext_changes operation to always do at least the do_write_kernel
step, because the various semanage dbs may have also changed content
relative to the current binary policy. As this step is itself relatively
fast, we can do it unconditionally.

Fixes: 286a679fad ("libsemanage: optionally rebuild policy when modules are changed externally")
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
Acked-by: Nicolas Iooss <nicolas.iooss@m4x.org>
2022-06-30 21:09:00 +02:00
Colin Cross
ca8aba0b86 Manual merge of mirror-aosp-master to stage-aosp-master
Manual merge of mirror-aosp-master to stage-aosp-master to work around
a change lost by the automerger.

Bug: 236830947
Test: treehugger
Change-Id: I3c42682677c6f602ab7ad49ce26aa24d83e53d74
2022-06-30 08:27:17 -07:00
Vit Mojzis
344463076b gettext: handle unsupported languages properly
With "fallback=True" gettext.translation behaves the same as
gettext.install and uses NullTranslations in case the
translation file for given language was not found (as opposed to
throwing an exception).

Fixes:
  # LANG is set to any "unsupported" language, e.g. en_US.UTF-8
  $ chcat --help
  Traceback (most recent call last):
  File "/usr/bin/chcat", line 39, in <module>
    t = gettext.translation(PROGNAME,
  File "/usr/lib64/python3.9/gettext.py", line 592, in translation
    raise FileNotFoundError(ENOENT,
  FileNotFoundError: [Errno 2] No translation file found for domain: 'selinux-python'

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
Reviewed-by: Daniel Burgener <dburgener@linux.microsoft.com>
Acked-by: Petr Lautrbach <plautrba@redhat.com>
2022-06-29 15:51:07 +02:00
Christian Göttsche
25e9c91a8b checkpolicy: error out if required permission would exceed limit
A require statement for a class permission adds that permission to the
class representation for the current module.  In case the resulting
class would have more than the supported amount of 32 permissions
assigned the resulting binary module will fail to load at link-time
without an informative error message (since [1]).

Bail out if adding a permission would result in a class having more than
the supported amount of 32 permissions assigned.

[1]: 97af65f696

Closes: https://github.com/SELinuxProject/selinux/issues/356
Reported-by: Julie Pichon
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-06-21 15:20:58 +02:00
Thiébaud Weksteen
e52b1a6bb5 Merge changes from topic "revert-2110147-TRFMPNYVUL"
* changes:
  Revert "Refactor sources in Android.bp"
  Revert "Add unit tests for service contexts backend"
2022-06-17 01:17:38 +00:00
Thiébaud Weksteen
d773e7ab33 Merge "Revert "Disable darwin target for libselinux"" 2022-06-17 00:48:06 +00:00
Thiébaud Weksteen
46c4d4521e Revert "Refactor sources in Android.bp"
Revert submission 2110147

Reason for revert: Broken sdk_mac target.
Reverted Changes:
Ifcd00954b:Refactor sources in Android.bp
Ie313b6d63:Add unit tests for service contexts backend

Change-Id: Id3d9c69c508b4774e667a600346b03a7c09e3a96
2022-06-16 23:46:53 +00:00
Thiébaud Weksteen
94ad1bb10e Revert "Add unit tests for service contexts backend"
Revert submission 2110147

Reason for revert: Broken sdk_mac target.
Reverted Changes:
Ifcd00954b:Refactor sources in Android.bp
Ie313b6d63:Add unit tests for service contexts backend

Change-Id: I616f43ed57626b5255dcf193f5f2c750b47f2fae
2022-06-16 23:46:53 +00:00
Thiébaud Weksteen
0bc22ad967 Revert "Disable darwin target for libselinux"
This reverts commit b4634567e5.

Reason for revert: This was not sufficient to fix the sdk_mac target. Reverting for now, as well as the other changes.

Change-Id: I5a1e039d031c7fc65972a39f1ae47701838e2f98
2022-06-16 23:46:18 +00:00
PETER LIANG
92e86a11f8 Merge "Disable darwin target for libselinux" am: ec86efad78 am: 87b9c80876 am: d1ac049e0e am: cc9e0859d2 am: 1320c928c0
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2125272

Change-Id: I334b50e0150cf2c4d69957a38d370d19938f40e8
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-16 12:09:29 +00:00
PETER LIANG
1320c928c0 Merge "Disable darwin target for libselinux" am: ec86efad78 am: 87b9c80876 am: d1ac049e0e am: cc9e0859d2
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2125272

Change-Id: I0461f683cbc2e069d9875c9adf0c82ca41f1be99
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-16 11:52:29 +00:00
PETER LIANG
cc9e0859d2 Merge "Disable darwin target for libselinux" am: ec86efad78 am: 87b9c80876 am: d1ac049e0e
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2125272

Change-Id: I84f1d37ad8a561dc0af04ddb4ebe0fb717ed8e60
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-16 11:16:40 +00:00
PETER LIANG
d1ac049e0e Merge "Disable darwin target for libselinux" am: ec86efad78 am: 87b9c80876
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2125272

Change-Id: I49d1633074a725b34a0450cc08e2107b50cdc840
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-16 10:57:44 +00:00
PETER LIANG
87b9c80876 Merge "Disable darwin target for libselinux" am: ec86efad78
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2125272

Change-Id: I9f0e26b62aec8a84ee37e69297f9ece367475f76
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-16 10:38:50 +00:00
PETER LIANG
ec86efad78 Merge "Disable darwin target for libselinux" 2022-06-16 10:12:24 +00:00
Thiébaud Weksteen
b4634567e5 Disable darwin target for libselinux
Bug: 235910628
Test: Forest build
Change-Id: I43341a896d41d63b65bf2d00f3c7893014011b96
2022-06-16 10:16:25 +10:00
Christian Göttsche
88a703399f libsepol: fix validation of user declarations in modules
Users are allowed to be declared in modules. Modules do not get expanded
leaving the `struct user_datum` members `exp_range` and `exp_dfltlevel`
empty.
Do no validate the expanded range and level for modular polices.

Reported-by: bauen1 <j2468h@gmail.com>
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-06-15 08:59:07 -04:00
Petr Lautrbach
3f23f07f01 libsepol: Drop unused assignment
Fixes:
cil/src/cil_build_ast.c:4622:4: warning[deadcode.DeadStores]: Value stored to 'rc' is never read

Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-06-15 08:59:01 -04:00
Christian Göttsche
c14a86af9a python/audit2allow: close file stream on error
sepolgen-ifgen-attr-helper.c: In function ‘load_policy’:
    sepolgen-ifgen-attr-helper.c:196:17: warning: leak of FILE ‘fp’ [CWE-775] [-Wanalyzer-file-leak]
      196 |                 fprintf(stderr, "Out of memory!\n");
          |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-06-15 08:58:54 -04:00
Christian Göttsche
c760a2bff6 libselinux: declare parameter of security_load_policy(3) const
security_load_policy(3) takes a read-only memory address for a binary
policy to be loaded.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-06-15 08:58:48 -04:00
Christian Göttsche
a6c572b1af libselinux: name parameters in context.h
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-06-15 08:58:41 -04:00
Christian Göttsche
29e5233d85 libselinux: enclose macro definition in parenthesis
Avoid potential precedence issues.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-06-15 08:58:35 -04:00
Christian Göttsche
90bc03b732 libselinux: add man page redirections
The following interfaces are documented but do not have a redirection:

  - context_str(3)
  - security_get_checkreqprot(3)
  - security_set_boolean_list(3)
  - selinux_sepgsql_context_path(3)
  - setexecfilecon(3)

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-06-15 08:58:00 -04:00
Thiébaud Weksteen
61c0ef0c3e Merge changes Ie313b6d6,Ifcd00954 am: 93164aa9cb am: a0e7b15afc am: 3b269bb2c0 am: be9a33d294 am: d8cf393b4d
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2110147

Change-Id: I1d6688d8afdf8185ff86f5b2623878d8b5818234
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-14 03:19:37 +00:00
Thiébaud Weksteen
d8cf393b4d Merge changes Ie313b6d6,Ifcd00954 am: 93164aa9cb am: a0e7b15afc am: 3b269bb2c0 am: be9a33d294
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2110147

Change-Id: I1b0fcaea5ba9b20c8a12c485e11a7a9d197276d4
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-14 02:51:19 +00:00
Thiébaud Weksteen
be9a33d294 Merge changes Ie313b6d6,Ifcd00954 am: 93164aa9cb am: a0e7b15afc am: 3b269bb2c0
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2110147

Change-Id: I2ca0579b32f3e6a20fdd6da7ff5625d8612dc988
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-14 02:34:14 +00:00
Thiébaud Weksteen
3b269bb2c0 Merge changes Ie313b6d6,Ifcd00954 am: 93164aa9cb am: a0e7b15afc
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2110147

Change-Id: Ia77e19aaa97d1bd6c520a76f6e699adc01123f55
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-14 02:15:32 +00:00
Thiébaud Weksteen
a0e7b15afc Merge changes Ie313b6d6,Ifcd00954 am: 93164aa9cb
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2110147

Change-Id: Ia482713fb5b9a6ac983fd9cf9468211b8df206f7
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-14 01:56:57 +00:00
Thiébaud Weksteen
93164aa9cb Merge changes Ie313b6d6,Ifcd00954
* changes:
  Add unit tests for service contexts backend
  Refactor sources in Android.bp
2022-06-14 01:28:45 +00:00
Christian Göttsche
1eb6229a48 libselinux: check for truncations
Check for truncations when building or copying strings involving user
input.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
2022-06-08 08:48:53 -04:00
Christian Göttsche
7d5a89314b libselinux: introduce strlcpy
To copy string safely, by always NULL-terminating them, and provide an
easy way to check for truncation introduce the nonstandard function
strlcpy(3).  Use the system implementation if available.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
2022-06-08 08:48:47 -04:00
Christian Göttsche
2b7da16595 libselinux: add header guard for internal header
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
2022-06-08 08:48:43 -04:00
Christian Göttsche
31e3537624 libselinux: simplify policy path logic to avoid uninitialized read
In case the function __policy_init() gets called with a NULL pointer,
the stack variable path remains uninitialized (except at its last
index).  If parsing the binary policy fails in sepol_policydb_read() the
error branch would access those uninitialized memory.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
2022-06-08 08:48:40 -04:00
Thiébaud Weksteen
91bf1d362d Move to AID_APP_START and AID_USER_OFFSET am: 2a325a44e5 am: 5413298d4c am: 857461f528 am: 016fd1c917 am: a99dd4c037
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2117153

Change-Id: I19da2c6e6ccfa212256c83bb82c0d1823712dd6b
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-07 09:05:24 +00:00
Thiébaud Weksteen
a99dd4c037 Move to AID_APP_START and AID_USER_OFFSET am: 2a325a44e5 am: 5413298d4c am: 857461f528 am: 016fd1c917
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2117153

Change-Id: I313d2b17cb454556d0e83c719d2a07e2aac66ac2
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-07 08:40:30 +00:00
Thiébaud Weksteen
016fd1c917 Move to AID_APP_START and AID_USER_OFFSET am: 2a325a44e5 am: 5413298d4c am: 857461f528
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2117153

Change-Id: Ie561a356c97b8ac5029390142e6babaefc068a27
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-07 08:16:10 +00:00
Thiébaud Weksteen
857461f528 Move to AID_APP_START and AID_USER_OFFSET am: 2a325a44e5 am: 5413298d4c
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2117153

Change-Id: I175c519535355138b94455c91cba10a396d6e07c
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-07 07:48:30 +00:00
Thiébaud Weksteen
5413298d4c Move to AID_APP_START and AID_USER_OFFSET am: 2a325a44e5
Original change: https://android-review.googlesource.com/c/platform/external/selinux/+/2117153

Change-Id: I59eb030a78e8927a675700cbf7bdd08c63f19d42
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-06-07 07:22:25 +00:00
Vit Mojzis
abaf812c38 python: Split "semanage import" into two transactions
First transaction applies all deletion operations, so that there are no
collisions when applying the rest of the changes.

Fixes:
  # semanage port -a -t http_cache_port_t -r s0 -p tcp 3024
  # semanage export | semanage import
  ValueError: Port tcp/3024 already defined

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
2022-06-06 16:16:27 -04:00
Nicolas Iooss
c8ba7968b3 libselinux: do not return the cached prev_current value when using getpidcon()
libselinux implements a cache mechanism for get*con() functions, such
that when a thread calls setcon(...) then getcon(...), the context is
directly returned. Unfortunately, getpidcon(pid, &context) uses the same
cached variable, so when a program uses setcon("something"), all later
calls to getpidcon(pid, ...) returns "something". This is a bug.

Here is a program which illustrates this bug:

    #include <stdio.h>
    #include <selinux/selinux.h>

    int main() {
        char *context = "";
        if (getpidcon(1, &context) < 0) {
            perror("getpidcon(1)");
        }
        printf("getpidcon(1) = %s\n", context);

        if (getcon(&context) < 0) {
            perror("getcon()");
        }
        printf("getcon() = %s\n", context);
        if (setcon(context) < 0) {
            perror("setcon()");
        }
        if (getpidcon(1, &context) < 0) {
            perror("getpidcon(1)");
        }
        printf("getpidcon(1) = %s\n", context);

        return 0;
    }

On an Arch Linux system using unconfined user, this program displays:

    getpidcon(1) = system_u:system_r:init_t
    getcon() = unconfined_u:unconfined_r:unconfined_t
    getpidcon(1) = unconfined_u:unconfined_r:unconfined_t

With this commit, this program displays:

    getpidcon(1) = system_u:system_r:init_t
    getcon() = unconfined_u:unconfined_r:unconfined_t
    getpidcon(1) = system_u:system_r:init_t

This bug was present in the first commit of
https://github.com/SELinuxProject/selinux git history. It was reported
in https://lore.kernel.org/selinux/20220121084012.GS7643@suse.com/ and a
patch to fix it was sent in
https://patchwork.kernel.org/project/selinux/patch/20220127130741.31940-1-jsegitz@suse.de/
without a clear explanation. This patch added pid checks, which made
sense but were difficult to read. Instead, it is possible to change the
way the functions are called so that they directly know which cache
variable to use.

Moreover, as the code is not clear at all (I spent too much time trying
to understand what the switch did and what the thread-local variable
contained), this commit also reworks libselinux/src/procattr.c to:
- not use hard-to-understand switch/case constructions on strings (they
  are replaced by a new argument filled by macros)
- remove getpidattr_def macro (it was only used once, for pidcon, and
  the code is clearer with one less macro)
- remove the pid parameter of setprocattrcon() and setprocattrcon_raw()
  (it is always zero)

Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
Cc: Johannes Segitz <jsegitz@suse.de>
2022-06-06 16:16:17 -04:00
Thiébaud Weksteen
2a325a44e5 Move to AID_APP_START and AID_USER_OFFSET
Replace AID_APP with AID_APP_START and AID_USER with AID_USER_OFFSET.
These have the same constant values. See android_filesystem_config.h.

Test: m
Change-Id: I3f696ee30de09f1a41f1c7eb4be6508c0d34c6ce
2022-06-06 16:13:01 +10:00
Petr Lautrbach
de285252a1 Revert "libselinux: restorecon: pin file to avoid TOCTOU issues"
This reverts commit 7e979b56fd.

The reverted commit broke `setfiles` when it's run from a chroot
without /proc mounted, e.g.

    # chroot /mnt/sysimage

    chroot# setfiles -e /proc -e /sys /sys /etc/selinux/targeted/contexts/files/file_contexts /
    [strace]
    openat(AT_FDCWD, "/", O_RDONLY|O_EXCL|O_NOFOLLOW|O_PATH) = 3
    newfstatat(3, "", {st_mode=S_IFDIR|0555, st_size=4096, ...}, AT_EMPTY_PATH) = 0
    mmap(NULL, 2101248, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1697c91000
    fgetxattr(3, "security.selinux", 0x55be8881d3f0, 255) = -1 EBADF (Bad file descriptor)
    fcntl(3, F_GETFL)                       = 0x220000 (flags O_RDONLY|O_NOFOLLOW|O_PATH)
    getxattr("/proc/self/fd/3", "security.selinux", 0x55be8881d3f0, 255) = -1 ENOENT (No such file or directory)
    [/strace]
    setfiles: Could not set context for /:  No such file or directory

Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-06-02 08:32:42 -04:00
Christian Göttsche
6d02b2fa29 semodule: avoid toctou on output module
Do not check for file existence and open afterwards, open with the
exclusive flag (supported in Glibc and musl 0.9.6 and also standardized
in C11).

Found by GitHub CodeQL.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: Nicolas Iooss <nicolas.iooss@m4x.org>
2022-06-02 08:31:07 -04:00
Christian Göttsche
dd98fa3227 libselinux: declare return value of context_str(3) const
context_str(3) returns a string representation of the given context.
This string is owned by the context and free'd on context_free(3).
Declare it const, as already done in the man page, since it must not be
free'd by the caller.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: Nicolas Iooss <nicolas.iooss@m4x.org>
2022-06-02 08:29:01 -04:00
Thiébaud Weksteen
74e8681330 Add unit tests for service contexts backend
A compilation option ANDROID_UNIT_TESTING is introduced to skip the
implicit search for /sys/fs/selinux when libselinux is first
initialized. The label functions of libselinux are used as-is (instead
of a mock or fake).

Expose the context_handle function internally so it may be called within
the unit tests.

Bug: 234313751
Test: atest --host libselinux_test
Change-Id: Ie313b6d63d25d42d9ec1b16a8bb42160950b5be8
2022-05-30 16:44:39 +10:00
Thiébaud Weksteen
a34f5325c8 Refactor sources in Android.bp
Move sources files to the default libselinux rule so that other binaries
may reuse the same defaults (i.e., unittests). The majority of sources
can be compiled for the host.

Bug: 234313751
Test: build
Change-Id: Ifcd00954b09f1d05c91f3deaf15cf16091455308
2022-05-30 16:43:58 +10:00
Petr Lautrbach
0a8c177dac
Update VERSIONs to 3.4 for release.
Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
2022-05-18 16:51:03 +02:00
Christian Göttsche
ed4cd8772a ci: declare git repository a safe directory
Since version 2.35.2, due to CVE-2022-24765, git refuses to operate by
default on a repository owned by a different user.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: Petr Lautrbach <plautrba@redhat.com>
2022-05-18 12:31:31 +02:00