context.c 862 B

1234567891011121314151617181920212223242526272829303132
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Implementations of the security context functions.
  4. *
  5. * Author: Ondrej Mosnacek <[email protected]>
  6. * Copyright (C) 2020 Red Hat, Inc.
  7. */
  8. #include <linux/jhash.h>
  9. #include "context.h"
  10. #include "mls.h"
  11. u32 context_compute_hash(const struct context *c)
  12. {
  13. u32 hash = 0;
  14. /*
  15. * If a context is invalid, it will always be represented by a
  16. * context struct with only the len & str set (and vice versa)
  17. * under a given policy. Since context structs from different
  18. * policies should never meet, it is safe to hash valid and
  19. * invalid contexts differently. The context_cmp() function
  20. * already operates under the same assumption.
  21. */
  22. if (c->len)
  23. return full_name_hash(NULL, c->str, c->len);
  24. hash = jhash_3words(c->user, c->role, c->type, hash);
  25. hash = mls_range_hash(&c->range, hash);
  26. return hash;
  27. }