From c3586fe0a556e22bfabd24a3cff1c110069dc556 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 4 Sep 2020 19:20:08 -0700 Subject: [PATCH] Upstream external/selinux Fix #2645 --- native/jni/external/selinux | 2 +- native/jni/magiskpolicy/sepolicy.cpp | 30 ++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/native/jni/external/selinux b/native/jni/external/selinux index a54be0532..969114cff 160000 --- a/native/jni/external/selinux +++ b/native/jni/external/selinux @@ -1 +1 @@ -Subproject commit a54be053299c04658d64229468a004ca40b6b4d5 +Subproject commit 969114cff6ee973f47a3c84cacdef77e504d7bd9 diff --git a/native/jni/magiskpolicy/sepolicy.cpp b/native/jni/magiskpolicy/sepolicy.cpp index d9dea4dc5..a7d6aa31c 100644 --- a/native/jni/magiskpolicy/sepolicy.cpp +++ b/native/jni/magiskpolicy/sepolicy.cpp @@ -398,7 +398,6 @@ bool sepol_impl::add_type_rule(const char *s, const char *t, const char *c, cons bool sepol_impl::add_filename_trans(const char *s, const char *t, const char *c, const char *d, const char *o) { type_datum_t *src, *tgt, *def; class_datum_t *cls; - filename_trans_datum_t *trans; src = hashtab_find(db->p_types.table, s); if (src == nullptr) { @@ -421,22 +420,37 @@ bool sepol_impl::add_filename_trans(const char *s, const char *t, const char *c, return false; } - filename_trans_t key; - key.stype = src->s.value; + filename_trans_key_t key; key.ttype = tgt->s.value; key.tclass = cls->s.value; key.name = (char *) o; - trans = hashtab_find(db->filename_trans, (hashtab_key_t) &key); + filename_trans_datum_t *last = nullptr; + filename_trans_datum_t *trans = hashtab_find(db->filename_trans, (hashtab_key_t) &key); + while (trans) { + if (ebitmap_get_bit(&trans->stypes, src->s.value - 1)) { + // Duplicate, overwrite existing data and return + trans->otype = def->s.value; + return true; + } + if (trans->otype == def->s.value) + break; + last = trans; + trans = trans->next; + } if (trans == nullptr) { trans = auto_cast(xcalloc(sizeof(*trans), 1)); - hashtab_insert(db->filename_trans, (hashtab_key_t) &key, trans); + filename_trans_key_t *new_key = auto_cast(malloc(sizeof(*new_key))); + *new_key = key; + new_key->name = strdup(key.name); + trans->next = last; + trans->otype = def->s.value; + hashtab_insert(db->filename_trans, (hashtab_key_t) new_key, trans); } - // Overwrite existing - trans->otype = def->s.value; - return true; + db->filename_trans_count++; + return ebitmap_set_bit(&trans->stypes, src->s.value - 1, 1) == 0; } bool sepol_impl::add_genfscon(const char *fs_name, const char *path, const char *context) {