Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d5bbf856 authored by Nick Kralevich's avatar Nick Kralevich
Browse files

android_os_SELinux.cpp: Refactor GetSELabelHandle

Slightly refactor GetSELabelHandle to abstract out the handle
fetching code and atomic cache used to store the SELinux handle.
This change will make it easier, in a future commit, to add additional
backends.

This should be a no-op.

Test: compiles and boots
Change-Id: I4a75daf8910d5c01bbd6732756ba9007e08c5bcb
parent af71583a
Loading
Loading
Loading
Loading
+11 −7
Original line number Diff line number Diff line
@@ -34,23 +34,27 @@

namespace android {
namespace {
std::atomic<selabel_handle*> sehandle{nullptr};
std::atomic<selabel_handle *> file_sehandle{nullptr};

selabel_handle* GetSELabelHandle() {
    selabel_handle* h = sehandle.load();
selabel_handle *GetSELabelHandle_impl(selabel_handle *(*handle_func)(),
                                      std::atomic<selabel_handle *> *handle_cache) {
    selabel_handle *h = handle_cache->load();
    if (h != nullptr) {
        return h;
    }

    h = selinux_android_file_context_handle();
    h = handle_func();
    selabel_handle* expected = nullptr;
    if (!sehandle.compare_exchange_strong(expected, h)) {
    if (!handle_cache->compare_exchange_strong(expected, h)) {
        selabel_close(h);
        return sehandle.load();
        return handle_cache->load();
    }
    return h;
}

selabel_handle *GetSELabelFileBackendHandle() {
    return GetSELabelHandle_impl(selinux_android_file_context_handle, &file_sehandle);
}
}

struct SecurityContext_Delete {
@@ -106,7 +110,7 @@ static jstring fileSelabelLookup(JNIEnv* env, jobject, jstring pathStr) {
        return NULL;
    }

    auto* selabel_handle = GetSELabelHandle();
    auto *selabel_handle = GetSELabelFileBackendHandle();
    if (selabel_handle == NULL) {
        ALOGE("fileSelabelLookup => Failed to get SEHandle");
        return NULL;