1 /* SPDX-License-Identifier: GPL-2.0 */
3 * A security identifier table (sidtab) is a lookup table
4 * of security context structures indexed by SID value.
9 * Copyright (C) 2018 Red Hat, Inc.
14 #include <linux/spinlock_types.h>
15 #include <linux/log2.h>
16 #include <linux/hashtable.h>
23 struct context context;
24 #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0
25 struct sidtab_str_cache __rcu *cache;
27 struct hlist_node list;
30 union sidtab_entry_inner {
31 struct sidtab_node_inner *ptr_inner;
32 struct sidtab_node_leaf *ptr_leaf;
35 /* align node size to page boundary */
36 #define SIDTAB_NODE_ALLOC_SHIFT PAGE_SHIFT
37 #define SIDTAB_NODE_ALLOC_SIZE PAGE_SIZE
39 #define size_to_shift(size) ((size) == 1 ? 1 : (const_ilog2((size) - 1) + 1))
41 #define SIDTAB_INNER_SHIFT \
42 (SIDTAB_NODE_ALLOC_SHIFT - size_to_shift(sizeof(union sidtab_entry_inner)))
43 #define SIDTAB_INNER_ENTRIES ((size_t)1 << SIDTAB_INNER_SHIFT)
44 #define SIDTAB_LEAF_ENTRIES \
45 (SIDTAB_NODE_ALLOC_SIZE / sizeof(struct sidtab_entry))
47 #define SIDTAB_MAX_BITS 32
48 #define SIDTAB_MAX U32_MAX
49 /* ensure enough tree levels for SIDTAB_MAX entries */
50 #define SIDTAB_MAX_LEVEL \
51 DIV_ROUND_UP(SIDTAB_MAX_BITS - size_to_shift(SIDTAB_LEAF_ENTRIES), \
54 struct sidtab_node_leaf {
55 struct sidtab_entry entries[SIDTAB_LEAF_ENTRIES];
58 struct sidtab_node_inner {
59 union sidtab_entry_inner entries[SIDTAB_INNER_ENTRIES];
62 struct sidtab_isid_entry {
64 struct sidtab_entry entry;
67 struct sidtab_convert_params {
68 int (*func)(struct context *oldc, struct context *newc, void *args);
70 struct sidtab *target;
73 #define SIDTAB_HASH_BITS CONFIG_SECURITY_SELINUX_SIDTAB_HASH_BITS
74 #define SIDTAB_HASH_BUCKETS (1 << SIDTAB_HASH_BITS)
78 * lock-free read access only for as many items as a prior read of
81 union sidtab_entry_inner roots[SIDTAB_MAX_LEVEL + 1];
83 * access atomically via {READ|WRITE}_ONCE(); only increment under
87 /* access only under spinlock */
88 struct sidtab_convert_params *convert;
91 #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0
92 /* SID -> context string cache */
94 struct list_head cache_lru_list;
95 spinlock_t cache_lock;
98 /* index == SID - 1 (no entry for SECSID_NULL) */
99 struct sidtab_isid_entry isids[SECINITSID_NUM];
101 /* Hash table for fast reverse context-to-sid lookups. */
102 DECLARE_HASHTABLE(context_to_sid, SIDTAB_HASH_BITS);
105 int sidtab_init(struct sidtab *s);
106 int sidtab_set_initial(struct sidtab *s, u32 sid, struct context *context);
107 struct sidtab_entry *sidtab_search_entry(struct sidtab *s, u32 sid);
108 struct sidtab_entry *sidtab_search_entry_force(struct sidtab *s, u32 sid);
110 static inline struct context *sidtab_search(struct sidtab *s, u32 sid)
112 struct sidtab_entry *entry = sidtab_search_entry(s, sid);
114 return entry ? &entry->context : NULL;
117 static inline struct context *sidtab_search_force(struct sidtab *s, u32 sid)
119 struct sidtab_entry *entry = sidtab_search_entry_force(s, sid);
121 return entry ? &entry->context : NULL;
124 int sidtab_convert(struct sidtab *s, struct sidtab_convert_params *params);
126 int sidtab_context_to_sid(struct sidtab *s, struct context *context, u32 *sid);
128 void sidtab_destroy(struct sidtab *s);
130 int sidtab_hash_stats(struct sidtab *sidtab, char *page);
132 #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0
133 void sidtab_sid2str_put(struct sidtab *s, struct sidtab_entry *entry,
134 const char *str, u32 str_len);
135 int sidtab_sid2str_get(struct sidtab *s, struct sidtab_entry *entry,
136 char **out, u32 *out_len);
138 static inline void sidtab_sid2str_put(struct sidtab *s,
139 struct sidtab_entry *entry,
140 const char *str, u32 str_len)
143 static inline int sidtab_sid2str_get(struct sidtab *s,
144 struct sidtab_entry *entry,
145 char **out, u32 *out_len)
149 #endif /* CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0 */
151 #endif /* _SS_SIDTAB_H_ */