Re: [PATCH] selinux: sidtab: reverse lookup hash table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Nov 7, 2019 at 3:52 PM Jeff Vander Stoep <jeffv@xxxxxxxxxx> wrote:
> This replaces the reverse table lookup and reverse cache with a
> hashtable which improves cache-miss reverse-lookup times from
> O(n) to O(1)* and maintains the same performance as a reverse
> cache hit.
>
> This reduces the time needed to add a new sidtab entry from ~500us
> to 5us on a Pixel 3 when there are ~10,000 sidtab entries.
>
> The implementation uses the kernel's generic hashtable API,
> It uses the context's string represtation as the hash source,
> and the kernels generic string hashing algorithm full_name_hash()
> to reduce the string to a 32 bit value.
>
> This change also maintains the improvement introduced in
> commit ee1a84fdfeed ("selinux: overhaul sidtab to fix bug and improve
> performance") which removed the need to keep the current sidtab
> locked during policy reload. It does however introduce periodic
> locking of the target sidtab while converting the hashtable. Sidtab
> entries are never modified or removed, so the context struct stored
> in the sid_to_context tree can also be used for the context_to_sid
> hashtable to reduce memory usage.
>
> This bug was reported by:
> - On the selinux bug tracker.
>   BUG: kernel softlockup due to too many SIDs/contexts #37
>   https://github.com/SELinuxProject/selinux-kernel/issues/37
> - Jovana Knezevic on Android's bugtracker.
>   Bug: 140252993
>   "During multi-user performance testing, we create and remove users
>   many times. selinux_android_restorecon_pkgdir goes from 1ms to over
>   20ms after about 200 user creations and removals. Accumulated over
>   ~280 packages, that adds a significant time to user creation,
>   making perf benchmarks unreliable."
>
> * Hashtable lookup is only O(1) when n < the number of buckets.
>
> Changes in V2:
> -The hashtable uses sidtab_entry_leaf objects directly so these
> objects are shared between the sid_to_context lookup tree and the
> context_to_sid hashtable. This simplifies memory allocation and
> was suggested by Ondrej Mosnacek.
> -The new sidtab hash stats file in selinuxfs has been moved out of
> the avc dir and into a new "ss" dir.
>
> V3:
> -Add lock nesting notation.
>
> V4/V5:
> -Moved to *_rcu variants of the various hashtable functions
> as suggested by Will Deacon.
> -Naming/spelling fixups.
>
> V6
> -Remove use of rcu_head/kfree_rcu(), they're unnecessary because
> hashtable objects are never removed when read/add operations are
> occurring.
> -Remove nested locking. Use lock of active sidtab to gate
> access to the new sidtab.
>
> Signed-off-by: Jeff Vander Stoep <jeffv@xxxxxxxxxx>
> Reported-by: Stephen Smalley <sds@xxxxxxxxxxxxx>
> Reported-by: Jovana Knezevic <jovanak@xxxxxxxxxx>
> ---
>  security/selinux/Kconfig            |  12 ++
>  security/selinux/include/security.h |   1 +
>  security/selinux/selinuxfs.c        |  65 +++++++
>  security/selinux/ss/context.h       |  11 +-
>  security/selinux/ss/policydb.c      |   5 +
>  security/selinux/ss/services.c      |  83 +++++++--
>  security/selinux/ss/services.h      |   4 +-
>  security/selinux/ss/sidtab.c        | 264 ++++++++++++++--------------
>  security/selinux/ss/sidtab.h        |  16 +-
>  9 files changed, 300 insertions(+), 161 deletions(-)

...

> diff --git a/security/selinux/ss/context.h b/security/selinux/ss/context.h
> index 513e67f48878..3ba044fe02ed 100644
> --- a/security/selinux/ss/context.h
> +++ b/security/selinux/ss/context.h
> @@ -192,5 +196,10 @@ static inline int context_cmp(struct context *c1, struct context *c2)
>                 mls_context_cmp(c1, c2));
>  }
>
> +static inline unsigned int context_compute_hash(const char *s)
> +{
> +       return full_name_hash(NULL, s, strlen(s));

While we can obviously use any hash function we want here, is there
any value in sticking with an algorithm similar to symhash()?  Or
alternatively, and beyond the scope of this patch, it is worth
converting symhash() to use full_name_hash()?  Or should we just leave
everything well enough alone ;)

> diff --git a/security/selinux/ss/sidtab.c b/security/selinux/ss/sidtab.c
> index 7d49994e8d5f..e705a3bc4d0d 100644
> --- a/security/selinux/ss/sidtab.c
> +++ b/security/selinux/ss/sidtab.c
> @@ -47,14 +64,61 @@ int sidtab_set_initial(struct sidtab *s, u32 sid, struct context *context)
>
>         entry = &s->isids[sid - 1];
>
> -       rc = context_cpy(&entry->context, context);
> +       rc = context_cpy(&entry->leaf.context, context);
>         if (rc)
>                 return rc;
>
>         entry->set = 1;
> +
> +       /*
> +        * Multiple initial sids may map to the same context. Check that this
> +        * context is not already represented in the context_to_sid hashtable
> +        * to avoid duplicate entries and long linked lists upon hash
> +        * collision.
> +        */
> +       if (!context_to_sid(s, context)) {
> +               entry->leaf.sid = sid;
> +               hash_add(s->context_to_sid, &entry->leaf.list, context->hash);
> +       }
> +
>         return 0;
>  }
>
> +int sidtab_hash_stats(struct sidtab *sidtab, char *page)
> +{
> +       int i;
> +       int chain_len = 0;
> +       int slots_used = 0;
> +       int entries = 0;
> +       int max_chain_len = 0;
> +       int cur_bucket = 0;
> +       struct sidtab_entry_leaf *entry;
> +
> +       rcu_read_lock();
> +       hash_for_each_rcu(sidtab->context_to_sid, i, entry, list) {
> +               entries++;
> +               if (i == cur_bucket) {
> +                       chain_len++;
> +                       if (chain_len == 1)
> +                               slots_used++;
> +               } else {
> +                       cur_bucket = i;
> +                       if (chain_len > max_chain_len)
> +                               max_chain_len = chain_len;
> +                       chain_len = 0;
> +               }
> +       }
> +       rcu_read_unlock();
> +
> +       if (chain_len > max_chain_len)
> +               max_chain_len = chain_len;
> +
> +       return scnprintf(page, PAGE_SIZE, "context_to_sid:  %d entries and "
> +                        "%d/%d buckets used, longest chain length %d\n",
> +                        entries, slots_used, SIDTAB_HASH_BUCKETS,
> +                        max_chain_len);

I wonder if there is value in sticking to the format currently in use
with /sys/fs/selinux/avc/hash_stats?

-- 
paul moore
www.paul-moore.com



[Index of Archives]     [Selinux Refpolicy]     [Linux SGX]     [Fedora Users]     [Fedora Desktop]     [Yosemite Photos]     [Yosemite Camping]     [Yosemite Campsites]     [KDE Users]     [Gnome Users]

  Powered by Linux