sidtab.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * A security identifier table (sidtab) is a lookup table
  4. * of security context structures indexed by SID value.
  5. *
  6. * Original author: Stephen Smalley, <sds@tycho.nsa.gov>
  7. * Author: Ondrej Mosnacek, <omosnacek@gmail.com>
  8. *
  9. * Copyright (C) 2018 Red Hat, Inc.
  10. */
  11. #ifndef _SS_SIDTAB_H_
  12. #define _SS_SIDTAB_H_
  13. #include <linux/spinlock_types.h>
  14. #include <linux/log2.h>
  15. #include <linux/hashtable.h>
  16. #include "context.h"
  17. #include "flask.h"
  18. struct sidtab_entry_leaf {
  19. u32 sid;
  20. struct context context;
  21. struct hlist_node list;
  22. };
  23. struct sidtab_node_inner;
  24. struct sidtab_node_leaf;
  25. union sidtab_entry_inner {
  26. struct sidtab_node_inner *ptr_inner;
  27. struct sidtab_node_leaf *ptr_leaf;
  28. };
  29. /* align node size to page boundary */
  30. #define SIDTAB_NODE_ALLOC_SHIFT PAGE_SHIFT
  31. #define SIDTAB_NODE_ALLOC_SIZE PAGE_SIZE
  32. #define size_to_shift(size) ((size) == 1 ? 1 : (const_ilog2((size) - 1) + 1))
  33. #define SIDTAB_INNER_SHIFT \
  34. (SIDTAB_NODE_ALLOC_SHIFT - size_to_shift(sizeof(union sidtab_entry_inner)))
  35. #define SIDTAB_INNER_ENTRIES ((size_t)1 << SIDTAB_INNER_SHIFT)
  36. #define SIDTAB_LEAF_ENTRIES \
  37. (SIDTAB_NODE_ALLOC_SIZE / sizeof(struct sidtab_entry_leaf))
  38. #define SIDTAB_MAX_BITS 32
  39. #define SIDTAB_MAX U32_MAX
  40. /* ensure enough tree levels for SIDTAB_MAX entries */
  41. #define SIDTAB_MAX_LEVEL \
  42. DIV_ROUND_UP(SIDTAB_MAX_BITS - size_to_shift(SIDTAB_LEAF_ENTRIES), \
  43. SIDTAB_INNER_SHIFT)
  44. struct sidtab_node_leaf {
  45. struct sidtab_entry_leaf entries[SIDTAB_LEAF_ENTRIES];
  46. };
  47. struct sidtab_node_inner {
  48. union sidtab_entry_inner entries[SIDTAB_INNER_ENTRIES];
  49. };
  50. struct sidtab_isid_entry {
  51. int set;
  52. struct sidtab_entry_leaf leaf;
  53. };
  54. struct sidtab_convert_params {
  55. int (*func)(struct context *oldc, struct context *newc, void *args);
  56. void *args;
  57. struct sidtab *target;
  58. };
  59. #define SIDTAB_HASH_BITS CONFIG_SECURITY_SELINUX_SIDTAB_HASH_BITS
  60. #define SIDTAB_HASH_BUCKETS (1 << SIDTAB_HASH_BITS)
  61. struct sidtab {
  62. /*
  63. * lock-free read access only for as many items as a prior read of
  64. * 'count'
  65. */
  66. union sidtab_entry_inner roots[SIDTAB_MAX_LEVEL + 1];
  67. /*
  68. * access atomically via {READ|WRITE}_ONCE(); only increment under
  69. * spinlock
  70. */
  71. u32 count;
  72. /* access only under spinlock */
  73. struct sidtab_convert_params *convert;
  74. spinlock_t lock;
  75. /* index == SID - 1 (no entry for SECSID_NULL) */
  76. struct sidtab_isid_entry isids[SECINITSID_NUM];
  77. /* Hash table for fast reverse context-to-sid lookups. */
  78. DECLARE_HASHTABLE(context_to_sid, SIDTAB_HASH_BITS);
  79. };
  80. int sidtab_init(struct sidtab *s);
  81. int sidtab_set_initial(struct sidtab *s, u32 sid, struct context *context);
  82. struct context *sidtab_search(struct sidtab *s, u32 sid);
  83. struct context *sidtab_search_force(struct sidtab *s, u32 sid);
  84. int sidtab_convert(struct sidtab *s, struct sidtab_convert_params *params);
  85. int sidtab_context_to_sid(struct sidtab *s, struct context *context, u32 *sid);
  86. void sidtab_destroy(struct sidtab *s);
  87. int sidtab_hash_stats(struct sidtab *sidtab, char *page);
  88. #endif /* _SS_SIDTAB_H_ */