Commit graph

1119 commits

Author SHA1 Message Date
Thiébaud Weksteen
6d45cb4d91 Remove VNDK flavor of libselinux
The library is already available as part of LL-NDK. It was partially
removed in commit 1ea855e, complete the clean up.

Test: m
Bug: 151303316
Change-Id: If824aeeb4f79b5759e103a7d0473946fd51f9171
2022-05-18 11:17:40 +10:00
Thiébaud Weksteen
1eb4e09449 Deprecate selinux_android_load_policy
Remove the exported header entry and replace the function body with a
log message. Keep the linker entry for compatibility.

Bug: 230037499
Test: m libselinux
Change-Id: I9abd7be80c228362ec2fe44d105301ed6e4d0095
2022-05-18 11:17:33 +10:00
Thiébaud Weksteen
5a0344eaec Merge changes I71954b62,I02aafefa
* changes:
  Refactor context files definitions
  Add documentation to Android functions
2022-04-26 01:00:05 +00:00
Thiébaud Weksteen
a24523e8ea Refactor context files definitions
The loading of context files is similar between the different backends.
Refactor their definitions and loading. The trade-off is that each
backend context configuration will be a 6x2 pointer array vs exact size
previously. This is negligible as the majority of backends are defined
for almost all partitions (with vndservice being the exception). Having
these constant-size arrays greatly simplifies the processing.

The only difference in behaviour is that each file_context being loaded
is also being reported to the logs (similarly to service and
keystore2_key backend).

Test: build and boot, verify logcat messages from SELinux
Change-Id: I71954b624ce5ca7cca68a18d7e81c462359a4330
2022-04-22 13:29:45 +10:00
Thiébaud Weksteen
3b85bf0628 Add documentation to Android functions
Rename once to seapp_once, to avoid confusion with the other once marker
used within libselinux.

Test: build
Change-Id: I02aafefa7280ddd557650483b050219e1763ae47
2022-04-22 13:27:01 +10:00
Bill Yi
3c41e9fae0 Merge SP2A.220405.004 to aosp-master - DO NOT MERGE
Merged-In: I01ee936126ca754ea4092feaca04dadc4cb68d6a
Merged-In: I394278532b510e7de035945f6dbf3409e944ddf5
Change-Id: I8c28575a7b1589b231c5bd253dd6e14015ec7907
2022-04-07 18:40:55 -07:00
Thiébaud Weksteen
e718c7c64d Remove android_host.c
Test: m libselinux
Change-Id: Id479a4e99a2398c1fb52e67296b47431b2f0a49c
2022-03-28 11:37:29 +11:00
Colin Cross
eee17d58fb Merge "Tweak linux_glibc properties for musl builds in external/selinux" 2022-03-21 17:27:27 +00:00
Mohammad Samiul Islam
1e9872d61c Enable selinux detect sdk data on other volumes
App data can be found on `/mnt/expand/<volume-uuid>`. Similarly, we want
sdk data to be together with app data on other volumes. Such directories
should get their labeling from seapp_context just like app data.

Bug: 222034645
Test: atest SdkSandboxStorageHostTest (see ag/17120883)
Ignore-AOSP-First: End to end test added which exists in internal branch
    only. Will cherry-pick this CL to aosp standalone once it is safely
    merged to internal branch.
Change-Id: I10b53d4827495466521983067e2830486fbd080e
Merged-In: I10b53d4827495466521983067e2830486fbd080e
(cherry picked from commit a58a9091cd)
2022-03-18 11:10:30 +00:00
Thiébaud Weksteen
5ab1edf09d Merge remote-tracking branch 'aosp/upstream-master' into update_head
Change-Id: I294af47a0cc73b753d23d8449575015d21219ddf
2022-03-15 12:24:38 +11:00
Colin Cross
19da933186 Tweak linux_glibc properties for musl builds in external/selinux
For convenience, builds against musl libc currently use the
linux_glibc properties because they are almost always linux-specific
and not glibc-specific.  In preparation for removing this hack,
tweak the linux_glibc properties by either moving them to host_linux,
which will apply to linux_glibc, linux_musl and linux_bionic, or
by setting appropriate musl or linux_musl properties.  Properties
that must not be repeated while musl uses linux_musl and also still
uses the linux_glibc properties are moved to glibc properties, which
don't apply to musl.  Whether these stay as glibc properties or get
moved back to linux_glibc later once the musl hack is removed is TBD.

Bug: 223257095
Test: m checkbuild
Test: m USE_HOST_MUSL=true host-native
Change-Id: I416ea94e6a6d8dac014cc1c70226090f1baeddfc
2022-03-08 15:12:49 -08:00
Nikita Ioffe
8a2236b007 Rename SupplementalProcess to SdkSandbox
Bug: 220320098
Test: presubmit
Change-Id: I3b6da9ab867fefdf54e3b69cff07abe40c24083d
Merged-In: I3b6da9ab867fefdf54e3b69cff07abe40c24083d
(cherry picked from commit fb613c7c55)
2022-03-07 15:06:53 +00:00
Samiul Islam
978ab82001 Teach pkgdir_selabel_lookup about supplemental process and data
This cl does two things:

  1. Supplemental process will be running in a new UID range, between
  20k to 30k. As such, we need a new username which is different than app
  and isolated process.
  2. Supplmental data, which is an extension of app data, is located in
  /data/misc_{ce,de}/<user-id>/supplemental directory. Their labeling
  need to come from seapp_context file just like other app data
  labeling.

See the selinux changes made in ag/16803377 for context on how this is
being used.

Bug: 217543371
Bug: 217559719
Test: atest SupplementalProcessStorageHostTest
      - #testSelinuxLabel
      - #testSupplementalDataAppDirectory_SharedStorageIsUsable
Change-Id: Ib22c9963761603633d66a232b421dbd5ee752bed
Merged-In: Ib22c9963761603633d66a232b421dbd5ee752bed
(cherry picked from commit 9528843267)
2022-03-07 15:06:19 +00:00
Vit Mojzis
4bab3ecc37 libselinux: Strip spaces before values in config
Spaces before values in /etc/selinux/config should be ignored just as
spaces after them are.

E.g. "SELINUXTYPE= targeted" should be a valid value.

Fixes:
   # sed -i 's/^SELINUXTYPE=/SELINUXTYPE= /g' /etc/selinux/config
   # dnf install <any_package>
   ...
   RPM: error: selabel_open: (/etc/selinux/ targeted/contexts/files/file_contexts) No such file or directory
   RPM: error: Plugin selinux: hook tsm_pre failed
   ...
   Error: Could not run transaction.

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
2022-03-03 12:08:44 -05:00
Thiébaud Weksteen
b6f173c558 Skip closef for property backend
In 50400d3 the selabel_open function was updated to call selabel_close
should the initialization of a backend fails. selabel_close includes a
call to the backend-specific close function: closef. This same function
is previously call, in case of an error, within the init function.

The file backend was adapted by introducing a safe guard to prevent a
double free. Update the property backend in a similar fashion.

Bug: 215387420
Test: build & boot
Change-Id: Id6b7c4d10538ccbec43b2af1de462da077f10c44
(cherry picked from commit 98fa1b865d)
Merged-In:Id6b7c4d10538ccbec43b2af1de462da077f10c44
2022-02-14 23:12:28 +00:00
Jeff Vander Stoep
3395071236 Use apex *_contexts when available
Bug: 199914227
Test: aosp/1910032
Change-Id: I620218d00f24f99d5936bf26f557bf70af0f5558
2022-01-10 10:46:58 +01:00
Thiébaud Weksteen
671624ef61 Merge upstream '3.3' into update_3_3
Followed the following steps:
    # aosp mirror does not have the upstream tags. Manually mark the
    # 3.3 release. See
    # https://github.com/SELinuxProject/selinux/releases/tag/3.3
    git tag 3.3 7f600c40bc
    repo start update_3_3 .
    git merge 3.3 --no-ff # No merge conflicts were found.
    lunch && m
    repo upload .

Bug: 37755687
Test: Build aosp_bramble-userdebug and manually compare the generated
    /{system,vendor,product}/etc/selinux* files with their previous
    versions. All are identical.
Change-Id: I6a514d7db9a752c44cc61d343f7c1a60f750f317
2021-12-22 09:15:13 +11:00
Christian Göttsche
e0da140d82 libselinux: use PCRE2 by default
Quoting pcre.org:

    There are two major versions of the PCRE library. The current
    version, PCRE2, released in 2015, is now at version 10.39.

    The older, but still widely deployed PCRE library, originally
    released in 1997, is at version 8.45. This version of PCRE is now at
    end of life, and is no longer being actively maintained. Version
    8.45 is expected to be the final release of the older PCRE library,
    and new projects should use PCRE2 instead.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
2021-12-09 11:07:00 -05:00
Petr Lautrbach
4bafb8eb7a libselinux: Fix selinux_restorecon_parallel symbol version
selinux_restorecon_parallel was originally proposed before 3.3, but it
was merged after release so it will be introduced in version 3.4.

Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
Acked-by: Ondrej Mosnacek <omosnace@redhat.com>
2021-11-29 18:50:52 +01:00
Jiyong Park
c3375d1e70 Make libselinux_bindgen available to the virt APEX
Bug: 204852957
Test: m
Change-Id: I652c42c38a568883bb05d7756e9012adc12b4194
2021-11-24 22:10:20 +09:00
Ondrej Mosnacek
02f302fc5b selinux_restorecon: introduce selinux_restorecon_parallel(3)
Refactor selinux_restorecon(3) to allow for distributing the relabeling
to multiple threads and add a new function
selinux_restorecon_parallel(3), which allows specifying the number of
threads to use. The existing selinux_restorecon(3) function maintains
the same interface and maintains the same behavior (i.e. relabeling is
done on a single thread).

The parallel implementation takes a simple approach of performing all
the directory tree traversal in a critical section and only letting the
relabeling of individual objects run in parallel. Thankfully, this
approach turns out to be efficient enough in practice, as shown by
restorecon benchmarks (detailed in a subsequent patch that switches
setfiles & restorecon to use selinux_restorecon_parallel(3)).

Note that to be able to use the parallelism, the calling application/
library must be explicitly linked to the libpthread library (statically
or dynamically). This is necessary to mantain the requirement that
libselinux shouldn't explicitly link with libpthread. (I don't know what
exactly was the reason behind this requirement as the commit logs are
fuzzy, but special care has been taken in the past to maintain it, so I
didn't want to break it...)

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
2021-11-23 10:03:18 +01:00
Ondrej Mosnacek
a578d1cecd selinux_restorecon: add a global mutex to synchronize progress output
Another small incremental change to pave the way for a parallel
selinux_restorecon() function.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
2021-11-23 10:03:17 +01:00
Ondrej Mosnacek
78bdce9c2a libselinux: make is_context_customizable() thread-safe
Use the __selinux_once() macro to ensure that threads don't race to
initialize the list of customizable types.

Reported-by: Christian Göttsche <cgzones@googlemail.com>
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
Tested-by: Christian Göttsche <cgzones@googlemail.com>
2021-11-23 10:03:16 +01:00
Ondrej Mosnacek
a3516ec647 libselinux: make selinux_log() thread-safe
Ensure that selinux_log() is thread-safe by guarding the call to the
underlying callback with a mutex.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
2021-11-23 10:03:16 +01:00
Ondrej Mosnacek
46427054f8 selinux_restorecon: protect file_spec list with a mutex
Not very useful on its own, but will allow to implement a parallel
version of selinux_restorecon() in subsequent patches.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
2021-11-23 10:03:15 +01:00
Ondrej Mosnacek
43dc50fc5c selinux_restorecon: simplify fl_head allocation by using calloc()
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
2021-11-23 10:03:15 +01:00
Ondrej Mosnacek
abe410aa86 label_file: fix a data race
The 'matches' member of 'struct spec' may be written to by different
threads, so it needs to be accessed using the proper atomic constructs.
Since the actual count of matches doesn't matter and is not used,
convert this field to a bool and just atomically set/read it using GCC
__atomic builtins (which are already being used in another place).

If the compiler lacks support for __atomic builtins (which seem to have
been introduced in GCC 4.1), just fail the compilation. I don't think
it's worth tryin to invent a workaround to support a 15 years old
compiler.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
2021-11-23 10:03:14 +01:00
Christian Göttsche
85982d83d1
libselinux: use valid address to silence glibc 2.34 warnings
Glibc 2.34 added an access function attribute to pthread_setspecific(3).
This leads to the following GCC warnings:

    In file included from matchpathcon.c:5:
    matchpathcon.c: In function ‘matchpathcon_init_prefix’:
    selinux_internal.h:38:25: error: ‘pthread_setspecific’ expecting 1 byte in a region of size 0 [-Werror=stringop-overread]
       38 |                         pthread_setspecific(KEY, VALUE);        \
          |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    matchpathcon.c:359:9: note: in expansion of macro ‘__selinux_setspecific’
      359 |         __selinux_setspecific(destructor_key, (void *)1);
          |         ^~~~~~~~~~~~~~~~~~~~~
    In file included from selinux_internal.h:2,
                     from matchpathcon.c:5:
    /usr/include/pthread.h:1167:12: note: in a call to function ‘pthread_setspecific’ declared with attribute ‘access (none, 2)’
     1167 | extern int pthread_setspecific (pthread_key_t __key,
          |            ^~~~~~~~~~~~~~~~~~~

The actual value and the validity of the passed pointer is irrelevant,
since it does not gets accessed internally by glibc and
pthread_getspecific(3) is not used.
Use a pointer to a global object to please GCC.

Closes: https://github.com/SELinuxProject/selinux/issues/311
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
2021-11-11 22:40:40 +01:00
Jeffrey Vander Stoep
4f48fac399 Merge "Remove references to nonplat sepolicy" 2021-11-08 07:02:40 +00:00
Jeff Vander Stoep
d835556651 Remove references to nonplat sepolicy
"nonplat" was renamed to "vendor" in Android Pie, but was retained
here for Treble compatibility.

We're now outside of the compatbility window for these devices so
it can safely be removed.

Test: build boot cuttlefish device. adb remount, modify
/system/etc/selinux/plat_sepolicy_and_mapping.sha256 to force
on-device policy compilation. reboot. Verify that device boots
without new selinux denials.

Change-Id: Ifec2d60810e4b2742f34b09d646db5e4ff6bb83b
2021-11-05 09:45:59 +01:00
Treehugger Robot
7713594f48 Merge "[BugFix] avoid SEGV because info maybe nullptr on restorecon_sb" 2021-11-03 16:23:34 +00:00
Thiébaud Weksteen
e1de766bff Revert^2 "Fix build and use new cil_write_build_ast"
f4408b8e8e

Change-Id: I68eca0e6b3bea874561a414733d5320e6ffa62c9
2021-10-27 04:50:56 +00:00
Thiébaud Weksteen
454466e2e4 Revert "Revert "Merge remote-tracking branch 'aosp/upstream-mast..."
Revert^2 "Use cil_write_build_ast"

bde09de39feec91cf8220f0f798a6e52154d69e9

Change-Id: I3ab19bda9c1968409ad5a4f4d0866649036c683c
2021-10-27 04:50:56 +00:00
Petr Lautrbach
7f600c40bc
Update VERSIONs to 3.3 for release.
Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
2021-10-21 16:31:23 +02:00
James Carter
c3ad59cc97 libselinux: Fix potential undefined shifts
An expression of the form "1 << x" is undefined if x == 31 because
the "1" is an int and cannot be left shifted by 31.

Instead, use "UINT32_C(1) << x" which will be an unsigned int of
at least 32 bits.

Signed-off-by: James Carter <jwcart2@gmail.com>
2021-10-12 13:44:55 -04:00
Petr Lautrbach
5319c49d8a
Update VERSIONs to 3.3-rc3 for release.
Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
2021-10-06 13:28:15 +02:00
chenguanyou
78041af5bb [BugFix] avoid SEGV because info maybe nullptr on restorecon_sb
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x18
backtrace:
00 pc 0000000000015c6c  /system/lib64/libselinux.so (restorecon_sb+688)
01 pc 0000000000014f6c  /system/lib64/libselinux.so (selinux_android_restorecon_common+480)

Test: manual

Signed-off-by: chenguanyou <chenguanyou@xiaomi.com>
Change-Id: I771394b3d96df94fd1a57df2381c5dac3e83e923
2021-10-02 14:22:24 +08:00
Petr Lautrbach
0b833973bf
Update VERSIONs to 3.3-rc2 for release.
Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
2021-09-22 17:14:25 +02:00
Christian Göttsche
ffa73128ea libselinux/utils: drop requirement to combine compiling and linking
The extra dependency of sefcontext_compile on its object file causes the
compile and link step to be separated.
During the link step the CFLAGS are not passed, which might contain
optimization or sanitizer flags.

Reorder the LDLIBS requirements to avoid the symbol 'pcre_fullinfo'
being unresolvable at link time.

Current behavior:

    gcc-11 **custom CFLAGS** -I../include -D_GNU_SOURCE     -c -o sefcontext_compile.o sefcontext_compile.c
    gcc-11 -L../src  sefcontext_compile.o ../src/regex.o  -lselinux  -lpcre ../src/libselinux.a -lsepol -o sefcontext_compile

Changed:

    gcc-11 **custom CFLAGS** -I../include -D_GNU_SOURCE    -L../src  sefcontext_compile.c  -lselinux  ../src/libselinux.a -lpcre -lsepol -o sefcontext_compile

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
2021-09-22 12:31:53 +02:00
Thiébaud Weksteen
c65aca49bb Revert "Merge remote-tracking branch 'aosp/upstream-master' into..."
Revert "Use cil_write_build_ast"

Revert submission 1827311-update_libselinux

Reason for revert: b/200771997 
Reverted Changes:
I088d1e94c:Fix build and use new cil_write_build_ast
I14dc4dc58:Merge remote-tracking branch 'aosp/upstream-master...
I7b77f4469:Use cil_write_build_ast

Change-Id: Iec17732997ab203787f021f437f31e51ef886425
2021-09-22 09:15:53 +00:00
Thiébaud Weksteen
f4408b8e8e Revert "Fix build and use new cil_write_build_ast"
Revert "Use cil_write_build_ast"

Revert submission 1827311-update_libselinux

Reason for revert: b/200771997 
Reverted Changes:
I088d1e94c:Fix build and use new cil_write_build_ast
I14dc4dc58:Merge remote-tracking branch 'aosp/upstream-master...
I7b77f4469:Use cil_write_build_ast

Change-Id: I7b34185a9205c550cdfee2ac29acad1bea7879a4
2021-09-22 09:15:53 +00:00
Thiébaud Weksteen
3342f74ef8 Fix build and use new cil_write_build_ast
Previously, Android used its own cil_write_ast function to output the
resulting AST. libsepol now defines a similar function named
cil_write_build_ast. The new function differs slightly in behaviour:

* It will output "source information" nodes in the resulting CIL. When
  loading, it is expected that each source information line (e.g.,
  `;;* lms 100 file.cil`) will be matched with a terminating entry (e.g.,
  `;;* lme`). If not, the loading will fail. Because we split and merge
  policy files in AOSP, explicitly ignore these lines when writing the
  AST.

* genfscon paths are now quoted following 644c5bb.

* An extra superfluous set of parentheses was previously added for some
  operators (e.g., "range" "and" or "not").

For typeattributes, cil_write_build_ast uses the `fqn` field and not
`name`. Ensure the nodes are correctly populated.

Bug: 190808996
Test: Build aosp_bramble-userdebug and manually compare the generated
    /{system,vendor,product}/etc/selinux* files with their previous
    versions. The differences are due to the new behaviours described
    above.
Test: Force a recompilation of the policy on device, the new policy is
    correctly loaded.
Change-Id: I088d1e94ca07cfbd0b6c604f1f82464b3537c392
2021-09-16 16:52:44 +02:00
Colin Cross
7c516b8428 libselinux: rename gettid() to something which never conflicts with the libc
Musl recently added a wrapper for gettid() syscall. There is no way to
detect this new version in a reliable way, so rename our gettid()
wrapper to a non-conflicting name.

Introduce a new function which, when using a libc known to provide a
wrapper for gettid(), calls it, and which, otherwise, performs the
syscall directly.

Anyway this function is only used on systems where /proc/thread-self
does not exist, which are therefore running Linux<3.17.

Fixes: https://github.com/SELinuxProject/selinux/issues/282
Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
Acked-by: Petr Lautrbach <plautrba@redhat.com>
(cherry picked from commit 398d2ceef9)
Bug: 190084016
Test: m USE_HOST_MUSL=true fastboot
Change-Id: I7e42155fd341bb2dfdcb73ab93041a1eee3ca64e
2021-09-14 12:34:07 -07:00
Thiébaud Weksteen
9e8922418a Merge remote-tracking branch 'aosp/upstream-master' into update_3_3
Change-Id: I14dc4dc589a5f72af6ba6d2cc1800d145f0234aa
2021-09-09 15:05:30 +02:00
Petr Lautrbach
38cb18e931 Update VERSIONs and Python bindings version to 3.3-rc1 for release
Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
2021-09-08 09:49:46 +02:00
Kelvin Zhang
408093ee0a Improve error message for label file validation
This is cherry-picked from upstream
8c21eeeace

Signed-off-by: Kelvin Zhang <zhangxp1998@gmail.com>
Change-Id: I3745d2b45eb42e62f29823edc1b20629bf8ab9d7
2021-08-23 07:59:42 -07:00
Kelvin Zhang
8c21eeeace Improve error message for label file validation
Signed-off-by: Kelvin Zhang <zhangxp1998@gmail.com>
2021-08-23 10:30:10 -04:00
Ivan Lozano
ec15ebd030 Remove bindgen test boilerplate code
The boilerplate is no longer necessary for defining rust_test modules
testing generated source.

Bug: 196076408
Test: m libselinux_bindgen_test
Change-Id: Iae623f4146e7580bc58090cebd78a21413ac844d
2021-08-11 13:42:52 -04:00
Joel Galenson
2b5ecc21d3 Add libselinux bindgen bindings test
Test: atest
Change-Id: I91d82f714c0ed0d671dc9b9678e241b679863df7
2021-08-10 12:01:04 -07:00
Christian Göttsche
2d4904284f libselinux: replace strerror by %m
The standard function `strerror(3)` is not thread safe.  This does not
only affect the concurrent usage of libselinux itself but also with
other `strerror(3)` linked libraries.
Use the thread safe GNU extension format specifier `%m`[1].

libselinux already uses the GNU extension format specifier `%ms`.

[1]: https://www.gnu.org/software/libc/manual/html_node/Other-Output-Conversions.html

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
2021-08-10 14:21:28 -04:00