2 * Implementation of the SID table type.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <linux/spinlock.h>
9 #include <linux/errno.h>
14 #define SIDTAB_HASH(sid) \
15 (sid & SIDTAB_HASH_MASK)
17 #define INIT_SIDTAB_LOCK(s) spin_lock_init(&s->lock)
18 #define SIDTAB_LOCK(s, x) spin_lock_irqsave(&s->lock, x)
19 #define SIDTAB_UNLOCK(s, x) spin_unlock_irqrestore(&s->lock, x)
21 int sidtab_init(struct sidtab *s)
25 s->htable = kmalloc(sizeof(*(s->htable)) * SIDTAB_SIZE, GFP_ATOMIC);
28 for (i = 0; i < SIDTAB_SIZE; i++)
37 int sidtab_insert(struct sidtab *s, u32 sid, struct context *context)
40 struct sidtab_node *prev, *cur, *newnode;
47 hvalue = SIDTAB_HASH(sid);
49 cur = s->htable[hvalue];
50 while (cur != NULL && sid > cur->sid) {
55 if (cur && sid == cur->sid) {
60 newnode = kmalloc(sizeof(*newnode), GFP_ATOMIC);
61 if (newnode == NULL) {
66 if (context_cpy(&newnode->context, context)) {
73 newnode->next = prev->next;
77 newnode->next = s->htable[hvalue];
79 s->htable[hvalue] = newnode;
83 if (sid >= s->next_sid)
84 s->next_sid = sid + 1;
89 static struct context *sidtab_search_core(struct sidtab *s, u32 sid, int force)
92 struct sidtab_node *cur;
97 hvalue = SIDTAB_HASH(sid);
98 cur = s->htable[hvalue];
99 while (cur != NULL && sid > cur->sid)
102 if (force && cur && sid == cur->sid && cur->context.len)
103 return &cur->context;
105 if (cur == NULL || sid != cur->sid || cur->context.len) {
106 /* Remap invalid SIDs to the unlabeled SID. */
107 sid = SECINITSID_UNLABELED;
108 hvalue = SIDTAB_HASH(sid);
109 cur = s->htable[hvalue];
110 while (cur != NULL && sid > cur->sid)
112 if (!cur || sid != cur->sid)
116 return &cur->context;
119 struct context *sidtab_search(struct sidtab *s, u32 sid)
121 return sidtab_search_core(s, sid, 0);
124 struct context *sidtab_search_force(struct sidtab *s, u32 sid)
126 return sidtab_search_core(s, sid, 1);
129 int sidtab_map(struct sidtab *s,
130 int (*apply) (u32 sid,
131 struct context *context,
136 struct sidtab_node *cur;
141 for (i = 0; i < SIDTAB_SIZE; i++) {
143 while (cur != NULL) {
144 rc = apply(cur->sid, &cur->context, args);
154 static inline u32 sidtab_search_context(struct sidtab *s,
155 struct context *context)
158 struct sidtab_node *cur;
160 for (i = 0; i < SIDTAB_SIZE; i++) {
162 while (cur != NULL) {
163 if (context_cmp(&cur->context, context))
171 int sidtab_context_to_sid(struct sidtab *s,
172 struct context *context,
179 *out_sid = SECSID_NULL;
181 sid = sidtab_search_context(s, context);
183 SIDTAB_LOCK(s, flags);
184 /* Rescan now that we hold the lock. */
185 sid = sidtab_search_context(s, context);
188 /* No SID exists for the context. Allocate a new one. */
189 if (s->next_sid == UINT_MAX || s->shutdown) {
196 "SELinux: Context %s is not valid (left unmapped).\n",
198 ret = sidtab_insert(s, sid, context);
202 SIDTAB_UNLOCK(s, flags);
212 void sidtab_hash_eval(struct sidtab *h, char *tag)
214 int i, chain_len, slots_used, max_chain_len;
215 struct sidtab_node *cur;
219 for (i = 0; i < SIDTAB_SIZE; i++) {
229 if (chain_len > max_chain_len)
230 max_chain_len = chain_len;
234 printk(KERN_DEBUG "%s: %d entries and %d/%d buckets used, longest "
235 "chain length %d\n", tag, h->nel, slots_used, SIDTAB_SIZE,
239 void sidtab_destroy(struct sidtab *s)
242 struct sidtab_node *cur, *temp;
247 for (i = 0; i < SIDTAB_SIZE; i++) {
249 while (cur != NULL) {
252 context_destroy(&temp->context);
263 void sidtab_set(struct sidtab *dst, struct sidtab *src)
267 SIDTAB_LOCK(src, flags);
268 dst->htable = src->htable;
270 dst->next_sid = src->next_sid;
272 SIDTAB_UNLOCK(src, flags);
275 void sidtab_shutdown(struct sidtab *s)
279 SIDTAB_LOCK(s, flags);
281 SIDTAB_UNLOCK(s, flags);