Commit graph

1242 commits

Author SHA1 Message Date
Thiébaud Weksteen
c23cbe8ca5 Merge "Add build flags for libselinux" into main 2024-03-04 23:28:31 +00:00
Thiébaud Weksteen
2273a74d00 Merge "Add selabel_get_digests_all_partial_matches binary" into main 2024-02-29 01:16:38 +00:00
Thiébaud Weksteen
f3007e9fd4 Add selabel_get_digests_all_partial_matches binary
Add build rule for selabel_get_digests_all_partial_matches. It is not
included by default in the system image, but can be useful to debug
the computation of security.sehash.

Bug: 317296680
Test: adb remount; adb push selabel_get_digests_all_partial_matches
  /system/bin; selabel_get_digests_all_partial_matches -r /data/data
Change-Id: I2b7e8d994f15539849d69ded5695293c4f2cf8b2
2024-02-28 13:26:57 +11:00
Ellen Arteca
cd26ca2162 The order the fields were printed in did not match the order in which their values are listed; likely a typo
Change-Id: I2e00216ef7e3cedd274fb16aa361637d9a98ba2c
2024-02-26 09:14:18 +00:00
Thiébaud Weksteen
8f719500fd Add build flags for libselinux
Consider /data/data as an app data directory (and skip any restorcon) if
the flag release_selinux_data_data_ignore is enabled.

Test: boot;
      setfattr -x security.sehash /data;
      setfattr -x security.sehash /data/data;
      reboot, restorecon ignores /data/data
Bug: 317296680
Change-Id: If341864555398cd042dbe5b89085821cc2f8a0c0
2024-02-23 15:46:51 +11:00
Daniel Chapin
27d4f93b76 Revert "Use generic isSelector"
This reverts commit 3d85f1e116.

Reason for revert: Droidfood blocking bug: b/314704483

Change-Id: I4cec1f1c4de25c28536c4f56cfd297ab1a9f3812
2023-12-05 00:52:17 +00:00
Thiébaud Weksteen
3d85f1e116 Use generic isSelector
seapp_contexts supports multiple boolean attributes: isPrivApp,
isEphemeralApp, isIsolatedComputeApp, isSdkSandboxAudit,
isSdkSandboxNext, fromRunAs. Each of these exists to support a specific
labelling scenario from the framework. When a new predicate is required,
an update to libselinux is also required. This change generically
handles any attribute starting with "is" and maps it directly
(case-insensitive) to the same seinfo field.

It is assumed that only one of these is required at a time. An error is
raised if seapp_contexts contains multiple is-selector within one rule.
An error is raised if seinfo contains multiple is-selector.

The order for comparison between seapp_contexts is altered: an entry
with an is-selector will be prioritized over one with an unspecifed
is-selector. This is not quite the previous order (e.g., isPrivApp <
targetSdkVersion < fromRunAs), but it is understood that the previous
order was not intentional and emerged from the incremental contributions
to this library.

The boolean info.isPreinstalledApp is replaced by checking the first
byte of info.partition.

Test: atest --host libselinux_test
Bug: 307635909
Change-Id: Ice3b84870e3255f6d9357d9750acbe9691b45aad
2023-12-01 10:42:50 +11:00
Thiébaud Weksteen
7fd89c00f7 Refactor the parsing of seinfo
The seinfo string contains many attributes provided by the caller to
match an seapp_contexts rule. Its usage has evolved organically and now
contains multiple fields for various purposes.

Refactor the parsing of seinfo, relying on strtok as the string
informally follows the convention of using colons between attributes and
an equal sign to separate an attribute and its value. For instance,

  default:privapp:targetSdkVersion=10000:partition=system:complete

A new internal structure is introduced to capture the attributes. The
new parse_seinfo function replaces seinfo_parse (which only parsed the
first attribute, historically the original seinfo), get_partition and
get_app_targetSdkVersion.

The new function is expected to behave similarly to the previous code.
Unknown attributes are now logged, but still ignored. The "complete"
attribute is now interpreted (as the last attribute), but not required.

Unit tests are added to cover standard and edge cases.

Test: boot and verify denial logs
Test: atest --host libselinux_test
Bug: 307635909
Change-Id: Ia0e3522c42c80e6e631ff1af644e03f53d88da93
2023-11-21 13:59:42 +11:00
Sandro Montanari
90c0d6546d Introduce sdk_sandbox_audit SELinux domain
Bug: 295861450
Test: atest CtsSdkSandboxInprocessTests and adb shell ps -Z
Change-Id: Ic2dc4c854b3bbe5719b83fcd5504766a1e92e6a4
2023-10-26 10:05:49 +00:00
Thiébaud Weksteen
e9448817b3 Remove APEX sepolicy support
Test: boot aosp_cf_x86_64_phone
Bug: 297794885
Change-Id: Ia447f1ce783eb83db41454aaee5e93f7f09c36b1
2023-09-04 14:14:05 +10:00
Inseob Kim
5cfac38d10 Add a comment to keep in sync with CTS
Test: N/A
Change-Id: I8d8c5033bcd9553a7b33e2d3875cc387fc4ddb86
2023-08-16 11:15:48 +09:00
Inseob Kim
cde31a9d4d Change seapp partition log to warning
It makes more sense to print it as a warning, because it's not a hard
error for now (until we resolve all violations and create a compliance
test)

Bug: N/A
Test: boot
Change-Id: Iac5deb1f965394ecd4c2acb3711bd07317956236
2023-08-01 01:56:20 +00:00
Inseob Kim
51fde66c16 Give priority to platform side seapp_contexts
This is to remove duplicate errors while fixing seapp_contexts
violations (because old vendors still have the entries).

Bug: 280547417
Test: TH
Change-Id: I8c381dad6e8bf5e91148494b55278e124b845c13
2023-07-26 13:57:15 +09:00
Inseob Kim
e7d2d82bbb Fix preinstalled app partition check
There is a bug on the code checking the partition, so it's printing
wrong logcat messages. This fixes it by renaming the function name for
better readability.

Also it fixes a bug that the check only happens when levelFrom != NONE.

Bug: 291005833
Test: boot and see logcat
Merged-In: I2dd51a995d76b2c50dae2b2c4af8e3a3a4599408
Change-Id: I2dd51a995d76b2c50dae2b2c4af8e3a3a4599408
(cherry picked from commit 321c025259)
2023-07-25 10:33:06 +09:00
Inseob Kim
be36d71068 Check preinstalled app's partition
Bug: 280547417
Test: boot pixel and cuttlefish
Change-Id: I6ed125eff392020ace6686514e0a102dab1fb10f
Merged-In: I6ed125eff392020ace6686514e0a102dab1fb10f
(cherry picked from commit dc9f3516d7)
2023-07-25 10:32:35 +09:00
Inseob Kim
c3d1e5a24a Fix code detecting duplicated seapp_contexts entry
There are two problems addressed by this change.

1) qsort doesn't compare all pairs of elements having the same
   precedence. We can't rely only on qsort's comparator to detect
   duplicates.

2) comparing logic is broken. For example,

        s1->isPrivAppSet && s1->isPrivApp == s2->isPrivApp

   really should be

        !s1->isPrivAppSet || s1->isPrivApp == s2->isPrivApp

Bug: 291528964
Test: manually create two duplicated entries and boot
Change-Id: Ieae4a7f5419e18636bb2fd5f70700faa4fa8acf1
2023-07-17 10:04:00 +00:00
Nikita Ioffe
f8cf22eba8 selinux_android_restorecon: log if selinux is disabled
Right now selinux_android_restorecon will silently succeed if selinux is
disabled which is confusing.

This change adds a log statement that should help with debugging issues
related to disabled selinux (see attached bug).

Bug: 284277137
Test: presubmit
Change-Id: I4ebc6400ac7188660658ef3cccfb7cbdc76c0f22
2023-06-16 12:59:16 +01:00
Mugdha Lakhani
e1c842285b Add applySdkSandboxNextRestrictions flag
seapp_context_lookup_internal applies a flag that is referenced in
seapp_contexts based on the seInfo string passed to it.

This enables testers to test out the set of restriction planned the
next SDK version and give feedback before we decide on the actual
restrictions for the next release.

Bug: b/270148964
Test: manual test app and adb shell ps -Z
Change-Id: I175229d135d99516dd6f38b8963d0ccc93a61a4f
2023-05-11 17:48:51 +00:00
Thiébaud Weksteen
366f01fd64 Skip newlines for SELinux logs
libselinux log messages usually end with a new line character. Android
log system does not require the new line character and will include the
character as-is in the log buffer.

selinux_log_callback and selinux_vendor_log_callback implementations are
merged as they provide similar functionalities.

Match the indentation (i.e., tabs) with the rest of the file.

Test: boot & inspect logcat
Change-Id: I0a5e53b8f048c65f29c5df3bd7e0b38f523e42cd
2023-04-04 10:26:19 +10:00
Thiébaud Weksteen
a9f20263fd Merge tag '3.5' into master
We were previously on 3.5-rc2, there has been only little changes since
then.

Followed the steps:
  repo start update_3.5 .
  git merge 3.5 --no-ff # No merge conflicts were found.
  lunch && m
  repo upload .
  # Update METADATA in a separate change.

Test: TH
Change-Id: If88fe90d2cbdb1ba6a279cba8b397cd2c808c6ab
2023-03-21 15:27:40 +11:00
Jason Zaman
d6e96c5929
Update VERSIONs to 3.5 for release.
Signed-off-by: Jason Zaman <jason@perfinion.com>
2023-02-23 05:16:11 -08:00
Jason Zaman
83e56c8a8b
Update VERSIONs to 3.5-rc3 for release.
Signed-off-by: Jason Zaman <jason@perfinion.com>
2023-02-10 22:32:13 -08:00
Christian Göttsche
49e65b85d6 libselinux: getcon.3: add note about PID races
Add a note that querying a foreign process via its PID is inherently
racy.

Suggested-by: Stephen Smalley <stephen.smalley.work@gmail.com>
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: Jason Zaman <jason@perfinion.com>
2023-02-10 22:23:17 -08:00
Christian Göttsche
494eb683f3 libselinux: add getpidprevcon
Add the public interfaces getpidprevcon(3) and getpidprevcon_raw(3), and
the utility getpidprevcon to gather the previous context before the last
exec of a given process.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: Jason Zaman <jason@perfinion.com>
2023-02-10 22:23:11 -08:00
Christian Göttsche
1609b9fdfd libselinux: restore: use fixed sized integer for hash index
The hash mask is set to 2^16 - 1, which does not fit into a signed 16
bit integer.  Use uint32_t to be on the safe side.  Also use size_t for
counting in debug function.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: Jason Zaman <jason@perfinion.com>
2023-02-10 22:19:00 -08:00
Christian Göttsche
06512c4373 libselinux: restore: misc tweaks
Add const qualifier to read-only state struct.

Minimize scope of function local variables, to reduce complexity.

Pass only the file type related file flags to selabel_lookup(3).

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: Jason Zaman <jason@perfinion.com>
2023-02-10 22:18:53 -08:00
Christian Göttsche
f9df9487ad libselinux: drop obsolete optimization flag
The optimization flag -funit-at-a-time is enabled by default in GCC[1]
and not supported by Clang:

    clang: error: optimization flag '-funit-at-a-time' is not supported [-Werror,-Wignored-optimization-argument]

[1]: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: Jason Zaman <jason@perfinion.com>
2023-02-10 22:18:46 -08:00
Carlo Marcelo Arenas Belón
72806f3933 libselinux: improve performance with pcre matches
Since 30b3e9d2 (libselinux: Workaround for heap overhead of pcre,
2023-01-12), performance of PCRE2 matches has been affected due to
excesive recreation of the match_data in an attempt to reduce memory
utilization; instead of a workaround, it would be better to address
the problem and maybe even improve performance in the process.

The issue is that currently the structure that holds PCRE state has
both a pcre2_code (which is per pattern) and a pcre2_match_data (which
is per match), forcing us to add a mutex to prevent multiple matches to
step on each other.

Lets remove the match_data and the mutex and instead allocate one once
in a thread independent way that could be used and reused, by extending
our pthread interface to not only store TLS variables but also retrieve
them, and then use one of those.

Since we are not interested on the capture groups (if any) lets only
allocate 1 pair which is all that will be needed and change the logic
so that a return of 0 (which means the pattern matched but there were
not enough capture spots) is also considered a match.

This will ensure that the memory use would be bound to the number of
concurrent matches instead of the number of patterns and therefore
reduce the impact that recent changes on the way that the frames used
for matching are allocated might had brough since 10.41 was released.

For cases where threads are not available, just keep it working in slow
mode as done before the workaround was reverted.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>

cherry picked from:
https://patchwork.kernel.org/project/selinux/patch/20230123014047.84911-3-carenas@gmail.com/

Bug: 262469329
Test: atest MicrodroidBenchmarkApp
Change-Id: I3207c6dd2a215f81699aa62e8fcdf65c745bae72
2023-02-02 13:07:40 +09:00
Charles Chen
fc29fffbde Merge "Integrate isIsolatedComputeApp selection logics for secontexts" 2023-02-01 17:34:34 +00:00
Charles Chen
48c968d184 Integrate isIsolatedComputeApp selection logics for secontexts
Enables processes with :IsolatedComputeApp set to be selected by seapp contexts with isIsolatedComputeApp selector.

Bug: 265540209
Bug: 265746493
Test: m && atest --host libselinux_test with change on android_unittest.cpp
Change-Id: I44f33bdd17454586708cbff2631ecd6725e53087
2023-01-26 15:09:19 +00:00
Treehugger Robot
a40fe12235 Merge "Revert "Set AGGRESSIVE_FREE_AFTER_REGEX_MATCH"" 2023-01-20 05:13:32 +00:00
Inseob Kim
50fe98a771 Revert "Set AGGRESSIVE_FREE_AFTER_REGEX_MATCH"
This reverts commit 6ffce0a0e3.

Reason for revert: b/266107191

Change-Id: I89e072c384f951637c130d7d2f5515ecc07a74f0
2023-01-20 00:26:44 +00:00
Treehugger Robot
2529ea6e51 Merge "Set AGGRESSIVE_FREE_AFTER_REGEX_MATCH" 2023-01-19 12:07:55 +00:00
David Brazdil
2eb09c5d74 Merge "Make libselinux_bindgen visible to all Virtualization targets" 2023-01-19 11:50:50 +00:00
Inseob Kim
6ffce0a0e3 Set AGGRESSIVE_FREE_AFTER_REGEX_MATCH
This is to workaround a regression on peak memory usage, due to a
behavior change of pcre2. With this patch, peak memory usage decreases
for about 4 MB.

Also verified with microdroid benchmarks that the runtime impact is
negligible.

Before this patch:

testMicrodroidBootTime[protectedVm=false]:
    avf_perf/microdroid/boot_time_average_ms: 1072.2437260666668
    avf_perf/microdroid/boot_time_max_ms: 1153.957195
    avf_perf/microdroid/boot_time_min_ms: 987.760254
    avf_perf/microdroid/boot_time_stdev_ms: 43.715968392943445
testMicrodroidBootTime[protectedVm=true]:
    avf_perf/microdroid/boot_time_average_ms: 1318.7790113333335
    avf_perf/microdroid/boot_time_max_ms: 1367.490967
    avf_perf/microdroid/boot_time_min_ms: 1239.080486
    avf_perf/microdroid/boot_time_stdev_ms: 33.82832311810135

After this patch:

testMicrodroidBootTime[protectedVm=false]:
    avf_perf/microdroid/boot_time_average_ms: 1074.9152321333336
    avf_perf/microdroid/boot_time_max_ms: 1172.233481
    avf_perf/microdroid/boot_time_min_ms: 971.020793
    avf_perf/microdroid/boot_time_stdev_ms: 45.3782260524823
testMicrodroidBootTime[protectedVm=true]:
    avf_perf/microdroid/boot_time_average_ms: 1286.4607849333333
    avf_perf/microdroid/boot_time_max_ms: 1380.643678
    avf_perf/microdroid/boot_time_min_ms: 1209.573649
    avf_perf/microdroid/boot_time_stdev_ms: 44.44544241596637

Bug: 262469329
Test: atest MicrodroidBenchmarks
Test: run device boot time test
Change-Id: Ifc1be381255c263638ea262b995bc06fa3c7bdcc
2023-01-19 10:21:29 +09:00
Inseob Kim
ad990cb28b Merge remote-tracking branch 'aosp/upstream-master' into HEAD
Bug: 262469329
Test: build and boot cuttlefish
Change-Id: Ic97540265491a501cd89f40f46bf1f97d6a753e2
2023-01-16 13:45:35 +09:00
Jason Zaman
3ccea01c69
Update VERSIONs to 3.5-rc2 for release.
Signed-off-by: Jason Zaman <jason@perfinion.com>
2023-01-15 15:40:55 -08:00
Inseob Kim
30b3e9d25f libselinux: Workaround for heap overhead of pcre
pcre's behavior is changed so that pcre2_match always allocates heap for
match_data, rather than stack, regardless of size. The heap isn't freed
until explicitly calling pcre2_match_data_free. This new behavior may
result in heap overhead, which may increase the peak memory usage about
a few megabytes. It's because regex_match is first called for regex_data
objects, and then regex_data objects are freed at once.

To workaround it, free match_data as soon as we call regex_match. It's
fine because libselinux currently doesn't use match_data, but use only
the return value.

Signed-off-by: Inseob Kim <inseob@google.com>
Acked-by: Jason Zaman <jason@perfinion.com>
2023-01-15 14:17:15 -08:00
David Brazdil
c8c407b1a9 Make libselinux_bindgen visible to all Virtualization targets
Bug: 245727626
Test: builds
Change-Id: I0057c4a222a5247a4e83c5398e39473574b46507
2023-01-12 21:12:30 +00:00
Christian Göttsche
b32e85cf67 Correct misc typos
Found by codespell(1) and typos[1].

[1]: https://github.com/crate-ci/typos

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2023-01-11 08:45:08 -05:00
Jason Zaman
013ecfd7fa Update VERSIONs to 3.5-rc1 for release.
Signed-off-by: Jason Zaman <jason@perfinion.com>
2022-12-22 13:10:26 -08:00
Jason Zaman
daf687247a libselinux: Ignore installed when installing python bindings to DESTDIR
When the python bindings are installed to a destdir with pip install
--prefix= --root=, pip tries to uninstall the existing root-owned
package and fails

Fixes:
running build_ext
python3 -m pip install --prefix=/usr `test -n "/tmp/selinux-release//build-master" && echo --root /tmp/selinux-release//build-master`  .
Processing /tmp/selinux-release/selinux-master/libselinux/src
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: selinux
  Building wheel for selinux (setup.py) ... done
  Created wheel for selinux: filename=selinux-3.4-cp310-cp310-linux_x86_64.whl size=725511 sha256=b35e9cdb2a6efce389eeece45446826b4ac6b41f81fdc128893f947036f27e8e
  Stored in directory: /tmp/pip-ephem-wheel-cache-kemjh99e/wheels/ca/2d/1e/d1ab52426d9add92931471cfa0d2558bcbeed89084af2388c9
Successfully built selinux
Installing collected packages: selinux
  Attempting uninstall: selinux
    Found existing installation: selinux 3.4
    Uninstalling selinux-3.4:
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: '__init__.cpython-310.pyc'
Consider using the `--user` option or check the permissions.

Signed-off-by: Jason Zaman <jason@perfinion.com>
Acked-by: Petr Lautrbach <lautrbach@redhat.com>
2022-12-22 08:58:30 -08:00
Petr Lautrbach
4f9e836f98 Use pip install instead of setup.py install
Fixes:
    /usr/lib/python3.11/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.

Signed-off-by: Petr Lautrbach <lautrbach@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-12-16 17:09:27 -05:00
James Carter
2a91411d7f Revert "Use pip install instead of setup.py install"
This reverts commit 2c3b818f5d.

An earlier version of the patch was commited by mistake.

Signed-off-by: James Carter <jwcart2@gmail.com>
2022-12-16 17:08:58 -05:00
Jie Lu
4c47f92758 libselinux:add check for malloc
Add return check for regex_data_create() to avoid NULL reference of regex_data

(gdb) bt
 #0  0x00007fbde5caec14 in pthread_mutex_init () from /usr/lib64/libc.so.6
 #1  0x00007fbde5e3a489 in regex_data_create () at regex.c:260
 #2  0x00007fbde5e3a4af in regex_prepare_data (regex=regex@entry=0x7fbde4613770, pattern_string=pattern_string@entry=0x563c6799a820 "^/home$", errordata=errordata@entry=0x7ffeb83fa950) at regex.c:76
 #3  0x00007fbde5e32fe6 in compile_regex (errbuf=0x0, spec=0x7fbde4613748) at label_file.h:407
 #4  lookup_all (key=0x563c679974e5 "/var/log/kadmind.log", type=<optimized out>, partial=partial@entry=false, match_count=match_count@entry=0x0, rec=<optimized out>, rec=<optimized out>)
     at label_file.c:949
 #5  0x00007fbde5e33350 in lookup (rec=<optimized out>, key=<optimized out>, type=<optimized out>) at label_file.c:1092
 #6  0x00007fbde5e31878 in selabel_lookup_common (rec=0x563c67998cc0, translating=1, key=<optimized out>, type=<optimized out>) at label.c:167

Signed-off-by: Jie Lu <lujie54@huawei.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-12-16 16:32:04 -05:00
Petr Lautrbach
2c3b818f5d Use pip install instead of setup.py install
Fixes:
    /usr/lib/python3.11/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.

Signed-off-by: Petr Lautrbach <lautrbach@redhat.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-11-28 09:23:11 -05:00
Jie Lu
001af27a6d libselinux: fix some memory issues in db_init
1. check the return of strdup to avoid a potential NULL reference.
2. make sure line_buf is freed.

Signed-off-by: Jie Lu <lujie54@huawei.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-11-28 09:22:31 -05:00
Thiébaud Weksteen
f8c13e3158 Use Bionic strlcpy when available
Bionic provides its own version of strlcpy. Ignore the re-definition
from SELinux for devices.

Bug: 260539369
Test: lunch sdk && m sdk
Change-Id: Icb9d8678c29562ab7b73d7a0f14a233fd71dfefd
2022-11-28 17:57:14 +11:00
Thiébaud Weksteen
4d132a10b0 Fix build after upstream merge
- Reference renamed file: COPYING -> LICENSE in Android.bp
- Fix constext_str calls now returning const char *
- Comment out cil_write_src_info_node which is not used on Android
- Include new selinux_internal.c source file

Bug: 253327909
Test: build and boot on bramble
Test: sediff between current and new policy; no change
Change-Id: I506479befb3c0b99136cd842b2a77a6a8bea18ed
2022-11-25 14:10:15 +11:00
Christian Göttsche
d31280c26e libselinux: filter arguments with path separators
Boolean names, taken by security_get_boolean_pending(3),
security_get_boolean_active(3) and security_set_boolean(3), as well as
user names, taken by security_get_initial_context(3), are used in path
constructions.  Ensure they do not contain path separators to avoid
unwanted path traversal.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
2022-11-23 08:23:31 -05:00