blob: 04c0b70c80121676e9391e1c711350b193232680 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the security services.
3 *
4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
Eric Paris5d55a342008-04-18 17:38:33 -04005 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
8 *
9 * Support for enhanced MLS infrastructure.
Darrel Goeddel376bd9c2006-02-24 15:44:05 -060010 * Support for context based audit filters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
13 *
Eric Paris5d55a342008-04-18 17:38:33 -040014 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070016 * Updated: Hewlett-Packard <paul.moore@hp.com>
17 *
18 * Added support for NetLabel
Paul Moore3bb56b22008-01-29 08:38:19 -050019 * Added support for the policy capability bitmap
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070020 *
Chad Sellersb94c7e62006-11-06 12:38:18 -050021 * Updated: Chad Sellers <csellers@tresys.com>
22 *
23 * Added validation of kernel classes and permissions
24 *
Paul Moore3bb56b22008-01-29 08:38:19 -050025 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
Darrel Goeddel376bd9c2006-02-24 15:44:05 -060026 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
Chad Sellersb94c7e62006-11-06 12:38:18 -050027 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
29 * This program is free software; you can redistribute it and/or modify
Eric Paris5d55a342008-04-18 17:38:33 -040030 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * the Free Software Foundation, version 2.
32 */
33#include <linux/kernel.h>
34#include <linux/slab.h>
35#include <linux/string.h>
36#include <linux/spinlock.h>
Paul Moore9f2ad662006-11-17 17:38:53 -050037#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/errno.h>
39#include <linux/in.h>
40#include <linux/sched.h>
41#include <linux/audit.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080042#include <linux/mutex.h>
Adrian Bunk0e55a002008-03-31 01:54:02 +030043#include <linux/selinux.h>
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070044#include <net/netlabel.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "flask.h"
47#include "avc.h"
48#include "avc_ss.h"
49#include "security.h"
50#include "context.h"
51#include "policydb.h"
52#include "sidtab.h"
53#include "services.h"
54#include "conditional.h"
55#include "mls.h"
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070056#include "objsec.h"
Paul Moorec60475b2007-02-28 15:14:23 -050057#include "netlabel.h"
Paul Moore3de4bab2006-11-17 17:38:54 -050058#include "xfrm.h"
Paul Moore02752762006-11-29 13:18:18 -050059#include "ebitmap.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020060#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62extern void selnl_notify_policyload(u32 seqno);
63unsigned int policydb_loaded_version;
64
Paul Moore3bb56b22008-01-29 08:38:19 -050065int selinux_policycap_netpeer;
Eric Parisb0c636b2008-02-28 12:58:40 -050066int selinux_policycap_openperm;
Paul Moore3bb56b22008-01-29 08:38:19 -050067
Chad Sellersb94c7e62006-11-06 12:38:18 -050068/*
69 * This is declared in avc.c
70 */
71extern const struct selinux_class_perm selinux_class_perm;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static DEFINE_RWLOCK(policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static struct sidtab sidtab;
76struct policydb policydb;
Eric Paris5d55a342008-04-18 17:38:33 -040077int ss_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * The largest sequence number that has been used when
81 * providing an access decision to the access vector cache.
82 * The sequence number only changes when a policy change
83 * occurs.
84 */
Eric Paris5d55a342008-04-18 17:38:33 -040085static u32 latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/* Forward declaration. */
88static int context_struct_to_string(struct context *context, char **scontext,
89 u32 *scontext_len);
90
91/*
92 * Return the boolean value of a constraint expression
93 * when it is applied to the specified source and target
94 * security contexts.
95 *
96 * xcontext is a special beast... It is used by the validatetrans rules
97 * only. For these rules, scontext is the context before the transition,
98 * tcontext is the context after the transition, and xcontext is the context
99 * of the process performing the transition. All other callers of
100 * constraint_expr_eval should pass in NULL for xcontext.
101 */
102static int constraint_expr_eval(struct context *scontext,
103 struct context *tcontext,
104 struct context *xcontext,
105 struct constraint_expr *cexpr)
106{
107 u32 val1, val2;
108 struct context *c;
109 struct role_datum *r1, *r2;
110 struct mls_level *l1, *l2;
111 struct constraint_expr *e;
112 int s[CEXPR_MAXDEPTH];
113 int sp = -1;
114
115 for (e = cexpr; e; e = e->next) {
116 switch (e->expr_type) {
117 case CEXPR_NOT:
118 BUG_ON(sp < 0);
119 s[sp] = !s[sp];
120 break;
121 case CEXPR_AND:
122 BUG_ON(sp < 1);
123 sp--;
124 s[sp] &= s[sp+1];
125 break;
126 case CEXPR_OR:
127 BUG_ON(sp < 1);
128 sp--;
129 s[sp] |= s[sp+1];
130 break;
131 case CEXPR_ATTR:
132 if (sp == (CEXPR_MAXDEPTH-1))
133 return 0;
134 switch (e->attr) {
135 case CEXPR_USER:
136 val1 = scontext->user;
137 val2 = tcontext->user;
138 break;
139 case CEXPR_TYPE:
140 val1 = scontext->type;
141 val2 = tcontext->type;
142 break;
143 case CEXPR_ROLE:
144 val1 = scontext->role;
145 val2 = tcontext->role;
146 r1 = policydb.role_val_to_struct[val1 - 1];
147 r2 = policydb.role_val_to_struct[val2 - 1];
148 switch (e->op) {
149 case CEXPR_DOM:
150 s[++sp] = ebitmap_get_bit(&r1->dominates,
151 val2 - 1);
152 continue;
153 case CEXPR_DOMBY:
154 s[++sp] = ebitmap_get_bit(&r2->dominates,
155 val1 - 1);
156 continue;
157 case CEXPR_INCOMP:
Eric Paris5d55a342008-04-18 17:38:33 -0400158 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
159 val2 - 1) &&
160 !ebitmap_get_bit(&r2->dominates,
161 val1 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 continue;
163 default:
164 break;
165 }
166 break;
167 case CEXPR_L1L2:
168 l1 = &(scontext->range.level[0]);
169 l2 = &(tcontext->range.level[0]);
170 goto mls_ops;
171 case CEXPR_L1H2:
172 l1 = &(scontext->range.level[0]);
173 l2 = &(tcontext->range.level[1]);
174 goto mls_ops;
175 case CEXPR_H1L2:
176 l1 = &(scontext->range.level[1]);
177 l2 = &(tcontext->range.level[0]);
178 goto mls_ops;
179 case CEXPR_H1H2:
180 l1 = &(scontext->range.level[1]);
181 l2 = &(tcontext->range.level[1]);
182 goto mls_ops;
183 case CEXPR_L1H1:
184 l1 = &(scontext->range.level[0]);
185 l2 = &(scontext->range.level[1]);
186 goto mls_ops;
187 case CEXPR_L2H2:
188 l1 = &(tcontext->range.level[0]);
189 l2 = &(tcontext->range.level[1]);
190 goto mls_ops;
191mls_ops:
192 switch (e->op) {
193 case CEXPR_EQ:
194 s[++sp] = mls_level_eq(l1, l2);
195 continue;
196 case CEXPR_NEQ:
197 s[++sp] = !mls_level_eq(l1, l2);
198 continue;
199 case CEXPR_DOM:
200 s[++sp] = mls_level_dom(l1, l2);
201 continue;
202 case CEXPR_DOMBY:
203 s[++sp] = mls_level_dom(l2, l1);
204 continue;
205 case CEXPR_INCOMP:
206 s[++sp] = mls_level_incomp(l2, l1);
207 continue;
208 default:
209 BUG();
210 return 0;
211 }
212 break;
213 default:
214 BUG();
215 return 0;
216 }
217
218 switch (e->op) {
219 case CEXPR_EQ:
220 s[++sp] = (val1 == val2);
221 break;
222 case CEXPR_NEQ:
223 s[++sp] = (val1 != val2);
224 break;
225 default:
226 BUG();
227 return 0;
228 }
229 break;
230 case CEXPR_NAMES:
231 if (sp == (CEXPR_MAXDEPTH-1))
232 return 0;
233 c = scontext;
234 if (e->attr & CEXPR_TARGET)
235 c = tcontext;
236 else if (e->attr & CEXPR_XTARGET) {
237 c = xcontext;
238 if (!c) {
239 BUG();
240 return 0;
241 }
242 }
243 if (e->attr & CEXPR_USER)
244 val1 = c->user;
245 else if (e->attr & CEXPR_ROLE)
246 val1 = c->role;
247 else if (e->attr & CEXPR_TYPE)
248 val1 = c->type;
249 else {
250 BUG();
251 return 0;
252 }
253
254 switch (e->op) {
255 case CEXPR_EQ:
256 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
257 break;
258 case CEXPR_NEQ:
259 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
260 break;
261 default:
262 BUG();
263 return 0;
264 }
265 break;
266 default:
267 BUG();
268 return 0;
269 }
270 }
271
272 BUG_ON(sp != 0);
273 return s[0];
274}
275
276/*
277 * Compute access vectors based on a context structure pair for
278 * the permissions in a particular class.
279 */
280static int context_struct_compute_av(struct context *scontext,
281 struct context *tcontext,
282 u16 tclass,
283 u32 requested,
284 struct av_decision *avd)
285{
286 struct constraint_node *constraint;
287 struct role_allow *ra;
288 struct avtab_key avkey;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700289 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct class_datum *tclass_datum;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700291 struct ebitmap *sattr, *tattr;
292 struct ebitmap_node *snode, *tnode;
Eric Paris3f120702007-09-21 14:37:10 -0400293 const struct selinux_class_perm *kdefs = &selinux_class_perm;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700294 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /*
297 * Remap extended Netlink classes for old policy versions.
298 * Do this here rather than socket_type_to_security_class()
299 * in case a newer policy version is loaded, allowing sockets
300 * to remain in the correct class.
301 */
302 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
303 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
304 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
305 tclass = SECCLASS_NETLINK_SOCKET;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /*
308 * Initialize the access vectors to the default values.
309 */
310 avd->allowed = 0;
311 avd->decided = 0xffffffff;
312 avd->auditallow = 0;
313 avd->auditdeny = 0xffffffff;
314 avd->seqno = latest_granting;
315
316 /*
Eric Paris3f120702007-09-21 14:37:10 -0400317 * Check for all the invalid cases.
318 * - tclass 0
319 * - tclass > policy and > kernel
320 * - tclass > policy but is a userspace class
321 * - tclass > policy but we do not allow unknowns
322 */
323 if (unlikely(!tclass))
324 goto inval_class;
325 if (unlikely(tclass > policydb.p_classes.nprim))
326 if (tclass > kdefs->cts_len ||
Eric Pariscea78dc2008-06-09 15:43:12 -0400327 !kdefs->class_to_string[tclass] ||
Eric Paris3f120702007-09-21 14:37:10 -0400328 !policydb.allow_unknown)
329 goto inval_class;
330
331 /*
332 * Kernel class and we allow unknown so pad the allow decision
333 * the pad will be all 1 for unknown classes.
334 */
335 if (tclass <= kdefs->cts_len && policydb.allow_unknown)
336 avd->allowed = policydb.undefined_perms[tclass - 1];
337
338 /*
339 * Not in policy. Since decision is completed (all 1 or all 0) return.
340 */
341 if (unlikely(tclass > policydb.p_classes.nprim))
342 return 0;
343
344 tclass_datum = policydb.class_val_to_struct[tclass - 1];
345
346 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * If a specific type enforcement rule was defined for
348 * this permission check, then use it.
349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700351 avkey.specified = AVTAB_AV;
352 sattr = &policydb.type_attr_map[scontext->type - 1];
353 tattr = &policydb.type_attr_map[tcontext->type - 1];
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900354 ebitmap_for_each_positive_bit(sattr, snode, i) {
355 ebitmap_for_each_positive_bit(tattr, tnode, j) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700356 avkey.source_type = i + 1;
357 avkey.target_type = j + 1;
358 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
359 node != NULL;
360 node = avtab_search_node_next(node, avkey.specified)) {
361 if (node->key.specified == AVTAB_ALLOWED)
362 avd->allowed |= node->datum.data;
363 else if (node->key.specified == AVTAB_AUDITALLOW)
364 avd->auditallow |= node->datum.data;
365 else if (node->key.specified == AVTAB_AUDITDENY)
366 avd->auditdeny &= node->datum.data;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Stephen Smalley782ebb92005-09-03 15:55:16 -0700369 /* Check conditional av table for additional permissions */
370 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
371
372 }
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /*
376 * Remove any permissions prohibited by a constraint (this includes
377 * the MLS policy).
378 */
379 constraint = tclass_datum->constraints;
380 while (constraint) {
381 if ((constraint->permissions & (avd->allowed)) &&
382 !constraint_expr_eval(scontext, tcontext, NULL,
383 constraint->expr)) {
384 avd->allowed = (avd->allowed) & ~(constraint->permissions);
385 }
386 constraint = constraint->next;
387 }
388
389 /*
390 * If checking process transition permission and the
391 * role is changing, then check the (current_role, new_role)
392 * pair.
393 */
394 if (tclass == SECCLASS_PROCESS &&
395 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
396 scontext->role != tcontext->role) {
397 for (ra = policydb.role_allow; ra; ra = ra->next) {
398 if (scontext->role == ra->role &&
399 tcontext->role == ra->new_role)
400 break;
401 }
402 if (!ra)
403 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
Eric Paris5d55a342008-04-18 17:38:33 -0400404 PROCESS__DYNTRANSITION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
407 return 0;
Eric Paris3f120702007-09-21 14:37:10 -0400408
409inval_class:
Stephen Smalley22df4ad2008-06-09 16:03:56 -0400410 if (!tclass || tclass > kdefs->cts_len ||
411 !kdefs->class_to_string[tclass]) {
412 if (printk_ratelimit())
413 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
414 __func__, tclass);
415 return -EINVAL;
416 }
417
418 /*
419 * Known to the kernel, but not to the policy.
420 * Handle as a denial (allowed is 0).
421 */
422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Eric Paris64dbf072008-03-31 12:17:33 +1100425/*
426 * Given a sid find if the type has the permissive flag set
427 */
428int security_permissive_sid(u32 sid)
429{
430 struct context *context;
431 u32 type;
432 int rc;
433
James Morris0804d112008-06-06 18:40:29 +1000434 read_lock(&policy_rwlock);
Eric Paris64dbf072008-03-31 12:17:33 +1100435
436 context = sidtab_search(&sidtab, sid);
437 BUG_ON(!context);
438
439 type = context->type;
440 /*
441 * we are intentionally using type here, not type-1, the 0th bit may
442 * someday indicate that we are globally setting permissive in policy.
443 */
444 rc = ebitmap_get_bit(&policydb.permissive_map, type);
445
James Morris0804d112008-06-06 18:40:29 +1000446 read_unlock(&policy_rwlock);
Eric Paris64dbf072008-03-31 12:17:33 +1100447 return rc;
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400451 struct context *ncontext,
452 struct context *tcontext,
453 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 char *o = NULL, *n = NULL, *t = NULL;
456 u32 olen, nlen, tlen;
457
458 if (context_struct_to_string(ocontext, &o, &olen) < 0)
459 goto out;
460 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
461 goto out;
462 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
463 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100464 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -0400465 "security_validate_transition: denied for"
466 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
467 o, n, t, policydb.p_class_val_to_name[tclass-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468out:
469 kfree(o);
470 kfree(n);
471 kfree(t);
472
473 if (!selinux_enforcing)
474 return 0;
475 return -EPERM;
476}
477
478int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
Eric Paris5d55a342008-04-18 17:38:33 -0400479 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct context *ocontext;
482 struct context *ncontext;
483 struct context *tcontext;
484 struct class_datum *tclass_datum;
485 struct constraint_node *constraint;
486 int rc = 0;
487
488 if (!ss_initialized)
489 return 0;
490
James Morris0804d112008-06-06 18:40:29 +1000491 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /*
494 * Remap extended Netlink classes for old policy versions.
495 * Do this here rather than socket_type_to_security_class()
496 * in case a newer policy version is loaded, allowing sockets
497 * to remain in the correct class.
498 */
499 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
500 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
501 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
502 tclass = SECCLASS_NETLINK_SOCKET;
503
504 if (!tclass || tclass > policydb.p_classes.nprim) {
Eric Paris744ba352008-04-17 11:52:44 -0400505 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
506 __func__, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 rc = -EINVAL;
508 goto out;
509 }
510 tclass_datum = policydb.class_val_to_struct[tclass - 1];
511
512 ocontext = sidtab_search(&sidtab, oldsid);
513 if (!ocontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400514 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
515 __func__, oldsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 rc = -EINVAL;
517 goto out;
518 }
519
520 ncontext = sidtab_search(&sidtab, newsid);
521 if (!ncontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400522 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
523 __func__, newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 rc = -EINVAL;
525 goto out;
526 }
527
528 tcontext = sidtab_search(&sidtab, tasksid);
529 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400530 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
531 __func__, tasksid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 rc = -EINVAL;
533 goto out;
534 }
535
536 constraint = tclass_datum->validatetrans;
537 while (constraint) {
538 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400539 constraint->expr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 rc = security_validtrans_handle_fail(ocontext, ncontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400541 tcontext, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto out;
543 }
544 constraint = constraint->next;
545 }
546
547out:
James Morris0804d112008-06-06 18:40:29 +1000548 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return rc;
550}
551
552/**
553 * security_compute_av - Compute access vector decisions.
554 * @ssid: source security identifier
555 * @tsid: target security identifier
556 * @tclass: target security class
557 * @requested: requested permissions
558 * @avd: access vector decisions
559 *
560 * Compute a set of access vector decisions based on the
561 * SID pair (@ssid, @tsid) for the permissions in @tclass.
562 * Return -%EINVAL if any of the parameters are invalid or %0
563 * if the access vector decisions were computed successfully.
564 */
565int security_compute_av(u32 ssid,
566 u32 tsid,
567 u16 tclass,
568 u32 requested,
569 struct av_decision *avd)
570{
571 struct context *scontext = NULL, *tcontext = NULL;
572 int rc = 0;
573
574 if (!ss_initialized) {
Stephen Smalley4c443d12005-05-16 21:53:52 -0700575 avd->allowed = 0xffffffff;
576 avd->decided = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 avd->auditallow = 0;
578 avd->auditdeny = 0xffffffff;
579 avd->seqno = latest_granting;
580 return 0;
581 }
582
James Morris0804d112008-06-06 18:40:29 +1000583 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 scontext = sidtab_search(&sidtab, ssid);
586 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400587 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
588 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 rc = -EINVAL;
590 goto out;
591 }
592 tcontext = sidtab_search(&sidtab, tsid);
593 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400594 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
595 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 rc = -EINVAL;
597 goto out;
598 }
599
600 rc = context_struct_compute_av(scontext, tcontext, tclass,
601 requested, avd);
602out:
James Morris0804d112008-06-06 18:40:29 +1000603 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return rc;
605}
606
607/*
608 * Write the security context string representation of
609 * the context structure `context' into a dynamically
610 * allocated string of the correct size. Set `*scontext'
611 * to point to this string and set `*scontext_len' to
612 * the length of the string.
613 */
614static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
615{
616 char *scontextp;
617
618 *scontext = NULL;
619 *scontext_len = 0;
620
Stephen Smalley12b29f32008-05-07 13:03:20 -0400621 if (context->len) {
622 *scontext_len = context->len;
623 *scontext = kstrdup(context->str, GFP_ATOMIC);
624 if (!(*scontext))
625 return -ENOMEM;
626 return 0;
627 }
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* Compute the size of the context. */
630 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
631 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
632 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
633 *scontext_len += mls_compute_context_len(context);
634
635 /* Allocate space for the context; caller must free this space. */
636 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -0400637 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 *scontext = scontextp;
640
641 /*
642 * Copy the user name, role name and type name into the context.
643 */
644 sprintf(scontextp, "%s:%s:%s",
645 policydb.p_user_val_to_name[context->user - 1],
646 policydb.p_role_val_to_name[context->role - 1],
647 policydb.p_type_val_to_name[context->type - 1]);
648 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
Eric Paris5d55a342008-04-18 17:38:33 -0400649 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
650 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 mls_sid_to_context(context, &scontextp);
653
654 *scontextp = 0;
655
656 return 0;
657}
658
659#include "initial_sid_to_string.h"
660
James Carterf0ee2e42007-04-04 10:11:29 -0400661const char *security_get_initial_sid_context(u32 sid)
662{
663 if (unlikely(sid > SECINITSID_NUM))
664 return NULL;
665 return initial_sid_to_string[sid];
666}
667
Stephen Smalley12b29f32008-05-07 13:03:20 -0400668static int security_sid_to_context_core(u32 sid, char **scontext,
669 u32 *scontext_len, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 struct context *context;
672 int rc = 0;
673
Stephen Smalley4f4acf32007-02-26 12:02:34 -0500674 *scontext = NULL;
675 *scontext_len = 0;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (!ss_initialized) {
678 if (sid <= SECINITSID_NUM) {
679 char *scontextp;
680
681 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -0400682 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca062006-05-15 09:43:48 -0700683 if (!scontextp) {
684 rc = -ENOMEM;
685 goto out;
686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 strcpy(scontextp, initial_sid_to_string[sid]);
688 *scontext = scontextp;
689 goto out;
690 }
Eric Paris744ba352008-04-17 11:52:44 -0400691 printk(KERN_ERR "SELinux: %s: called before initial "
692 "load_policy on unknown SID %d\n", __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 rc = -EINVAL;
694 goto out;
695 }
James Morris0804d112008-06-06 18:40:29 +1000696 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400697 if (force)
698 context = sidtab_search_force(&sidtab, sid);
699 else
700 context = sidtab_search(&sidtab, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (!context) {
Eric Paris744ba352008-04-17 11:52:44 -0400702 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
703 __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 rc = -EINVAL;
705 goto out_unlock;
706 }
707 rc = context_struct_to_string(context, scontext, scontext_len);
708out_unlock:
James Morris0804d112008-06-06 18:40:29 +1000709 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710out:
711 return rc;
712
713}
714
Stephen Smalley12b29f32008-05-07 13:03:20 -0400715/**
716 * security_sid_to_context - Obtain a context for a given SID.
717 * @sid: security identifier, SID
718 * @scontext: security context
719 * @scontext_len: length in bytes
720 *
721 * Write the string representation of the context associated with @sid
722 * into a dynamically allocated string of the correct size. Set @scontext
723 * to point to this string and set @scontext_len to the length of the string.
724 */
725int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Stephen Smalley12b29f32008-05-07 13:03:20 -0400727 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
728}
729
730int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
731{
732 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
733}
734
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400735/*
736 * Caveat: Mutates scontext.
737 */
Stephen Smalley12b29f32008-05-07 13:03:20 -0400738static int string_to_context_struct(struct policydb *pol,
739 struct sidtab *sidtabp,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400740 char *scontext,
Stephen Smalley12b29f32008-05-07 13:03:20 -0400741 u32 scontext_len,
742 struct context *ctx,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400743 u32 def_sid)
Stephen Smalley12b29f32008-05-07 13:03:20 -0400744{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct role_datum *role;
746 struct type_datum *typdatum;
747 struct user_datum *usrdatum;
748 char *scontextp, *p, oldc;
749 int rc = 0;
750
Stephen Smalley12b29f32008-05-07 13:03:20 -0400751 context_init(ctx);
752
Stephen Smalley12b29f32008-05-07 13:03:20 -0400753 /* Parse the security context. */
754
755 rc = -EINVAL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400756 scontextp = (char *) scontext;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400757
758 /* Extract the user. */
759 p = scontextp;
760 while (*p && *p != ':')
761 p++;
762
763 if (*p == 0)
764 goto out;
765
766 *p++ = 0;
767
768 usrdatum = hashtab_search(pol->p_users.table, scontextp);
769 if (!usrdatum)
770 goto out;
771
772 ctx->user = usrdatum->value;
773
774 /* Extract role. */
775 scontextp = p;
776 while (*p && *p != ':')
777 p++;
778
779 if (*p == 0)
780 goto out;
781
782 *p++ = 0;
783
784 role = hashtab_search(pol->p_roles.table, scontextp);
785 if (!role)
786 goto out;
787 ctx->role = role->value;
788
789 /* Extract type. */
790 scontextp = p;
791 while (*p && *p != ':')
792 p++;
793 oldc = *p;
794 *p++ = 0;
795
796 typdatum = hashtab_search(pol->p_types.table, scontextp);
797 if (!typdatum)
798 goto out;
799
800 ctx->type = typdatum->value;
801
802 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
803 if (rc)
804 goto out;
805
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400806 if ((p - scontext) < scontext_len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -0400807 rc = -EINVAL;
808 goto out;
809 }
810
811 /* Check the validity of the new context. */
812 if (!policydb_context_isvalid(pol, ctx)) {
813 rc = -EINVAL;
814 context_destroy(ctx);
815 goto out;
816 }
817 rc = 0;
818out:
Stephen Smalley12b29f32008-05-07 13:03:20 -0400819 return rc;
820}
821
822static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
823 u32 *sid, u32 def_sid, gfp_t gfp_flags,
824 int force)
825{
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400826 char *scontext2, *str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400827 struct context context;
828 int rc = 0;
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (!ss_initialized) {
831 int i;
832
833 for (i = 1; i < SECINITSID_NUM; i++) {
834 if (!strcmp(initial_sid_to_string[i], scontext)) {
835 *sid = i;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838 }
839 *sid = SECINITSID_KERNEL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842 *sid = SECSID_NULL;
843
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400844 /* Copy the string so that we can modify the copy as we parse it. */
845 scontext2 = kmalloc(scontext_len+1, gfp_flags);
846 if (!scontext2)
847 return -ENOMEM;
848 memcpy(scontext2, scontext, scontext_len);
849 scontext2[scontext_len] = 0;
850
851 if (force) {
852 /* Save another copy for storing in uninterpreted form */
853 str = kstrdup(scontext2, gfp_flags);
854 if (!str) {
855 kfree(scontext2);
856 return -ENOMEM;
857 }
858 }
859
James Morris0804d112008-06-06 18:40:29 +1000860 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400861 rc = string_to_context_struct(&policydb, &sidtab,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400862 scontext2, scontext_len,
863 &context, def_sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400864 if (rc == -EINVAL && force) {
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400865 context.str = str;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400866 context.len = scontext_len;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400867 str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400868 } else if (rc)
869 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 rc = sidtab_context_to_sid(&sidtab, &context, sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400871 if (rc)
872 context_destroy(&context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873out:
James Morris0804d112008-06-06 18:40:29 +1000874 read_unlock(&policy_rwlock);
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400875 kfree(scontext2);
876 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return rc;
878}
879
James Morrisf5c1d5b2005-07-28 01:07:37 -0700880/**
881 * security_context_to_sid - Obtain a SID for a given security context.
882 * @scontext: security context
883 * @scontext_len: length in bytes
884 * @sid: security identifier, SID
885 *
886 * Obtains a SID associated with the security context that
887 * has the string representation specified by @scontext.
888 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
889 * memory is available, or 0 on success.
890 */
David Howells8f0cfa52008-04-29 00:59:41 -0700891int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
James Morrisf5c1d5b2005-07-28 01:07:37 -0700892{
893 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -0400894 sid, SECSID_NULL, GFP_KERNEL, 0);
James Morrisf5c1d5b2005-07-28 01:07:37 -0700895}
896
897/**
898 * security_context_to_sid_default - Obtain a SID for a given security context,
899 * falling back to specified default if needed.
900 *
901 * @scontext: security context
902 * @scontext_len: length in bytes
903 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -0700904 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -0700905 *
906 * Obtains a SID associated with the security context that
907 * has the string representation specified by @scontext.
908 * The default SID is passed to the MLS layer to be used to allow
909 * kernel labeling of the MLS field if the MLS field is not present
910 * (for upgrading to MLS without full relabel).
Stephen Smalley12b29f32008-05-07 13:03:20 -0400911 * Implicitly forces adding of the context even if it cannot be mapped yet.
James Morrisf5c1d5b2005-07-28 01:07:37 -0700912 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
913 * memory is available, or 0 on success.
914 */
David Howells7bf570d2008-04-29 20:52:51 +0100915int security_context_to_sid_default(const char *scontext, u32 scontext_len,
916 u32 *sid, u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -0700917{
918 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -0400919 sid, def_sid, gfp_flags, 1);
920}
921
922int security_context_to_sid_force(const char *scontext, u32 scontext_len,
923 u32 *sid)
924{
925 return security_context_to_sid_core(scontext, scontext_len,
926 sid, SECSID_NULL, GFP_KERNEL, 1);
James Morrisf5c1d5b2005-07-28 01:07:37 -0700927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929static int compute_sid_handle_invalid_context(
930 struct context *scontext,
931 struct context *tcontext,
932 u16 tclass,
933 struct context *newcontext)
934{
935 char *s = NULL, *t = NULL, *n = NULL;
936 u32 slen, tlen, nlen;
937
938 if (context_struct_to_string(scontext, &s, &slen) < 0)
939 goto out;
940 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
941 goto out;
942 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
943 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100944 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 "security_compute_sid: invalid context %s"
946 " for scontext=%s"
947 " tcontext=%s"
948 " tclass=%s",
949 n, s, t, policydb.p_class_val_to_name[tclass-1]);
950out:
951 kfree(s);
952 kfree(t);
953 kfree(n);
954 if (!selinux_enforcing)
955 return 0;
956 return -EACCES;
957}
958
959static int security_compute_sid(u32 ssid,
960 u32 tsid,
961 u16 tclass,
962 u32 specified,
963 u32 *out_sid)
964{
965 struct context *scontext = NULL, *tcontext = NULL, newcontext;
966 struct role_trans *roletr = NULL;
967 struct avtab_key avkey;
968 struct avtab_datum *avdatum;
969 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int rc = 0;
971
972 if (!ss_initialized) {
973 switch (tclass) {
974 case SECCLASS_PROCESS:
975 *out_sid = ssid;
976 break;
977 default:
978 *out_sid = tsid;
979 break;
980 }
981 goto out;
982 }
983
Venkat Yekkirala851f8a62006-07-30 03:03:18 -0700984 context_init(&newcontext);
985
James Morris0804d112008-06-06 18:40:29 +1000986 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 scontext = sidtab_search(&sidtab, ssid);
989 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400990 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
991 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 rc = -EINVAL;
993 goto out_unlock;
994 }
995 tcontext = sidtab_search(&sidtab, tsid);
996 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400997 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
998 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 rc = -EINVAL;
1000 goto out_unlock;
1001 }
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /* Set the user identity. */
1004 switch (specified) {
1005 case AVTAB_TRANSITION:
1006 case AVTAB_CHANGE:
1007 /* Use the process user identity. */
1008 newcontext.user = scontext->user;
1009 break;
1010 case AVTAB_MEMBER:
1011 /* Use the related object owner. */
1012 newcontext.user = tcontext->user;
1013 break;
1014 }
1015
1016 /* Set the role and type to default values. */
1017 switch (tclass) {
1018 case SECCLASS_PROCESS:
1019 /* Use the current role and type of process. */
1020 newcontext.role = scontext->role;
1021 newcontext.type = scontext->type;
1022 break;
1023 default:
1024 /* Use the well-defined object role. */
1025 newcontext.role = OBJECT_R_VAL;
1026 /* Use the type of the related object. */
1027 newcontext.type = tcontext->type;
1028 }
1029
1030 /* Look for a type transition/member/change rule. */
1031 avkey.source_type = scontext->type;
1032 avkey.target_type = tcontext->type;
1033 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001034 avkey.specified = specified;
1035 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -04001038 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001039 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001041 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 avdatum = &node->datum;
1043 break;
1044 }
1045 }
1046 }
1047
Stephen Smalley782ebb92005-09-03 15:55:16 -07001048 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -07001050 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
1053 /* Check for class-specific changes. */
1054 switch (tclass) {
1055 case SECCLASS_PROCESS:
1056 if (specified & AVTAB_TRANSITION) {
1057 /* Look for a role transition rule. */
1058 for (roletr = policydb.role_tr; roletr;
1059 roletr = roletr->next) {
1060 if (roletr->role == scontext->role &&
1061 roletr->type == tcontext->type) {
1062 /* Use the role transition rule. */
1063 newcontext.role = roletr->new_role;
1064 break;
1065 }
1066 }
1067 }
1068 break;
1069 default:
1070 break;
1071 }
1072
1073 /* Set the MLS attributes.
1074 This is done last because it may allocate memory. */
1075 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1076 if (rc)
1077 goto out_unlock;
1078
1079 /* Check the validity of the context. */
1080 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1081 rc = compute_sid_handle_invalid_context(scontext,
1082 tcontext,
1083 tclass,
1084 &newcontext);
1085 if (rc)
1086 goto out_unlock;
1087 }
1088 /* Obtain the sid for the context. */
1089 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1090out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001091 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 context_destroy(&newcontext);
1093out:
1094 return rc;
1095}
1096
1097/**
1098 * security_transition_sid - Compute the SID for a new subject/object.
1099 * @ssid: source security identifier
1100 * @tsid: target security identifier
1101 * @tclass: target security class
1102 * @out_sid: security identifier for new subject/object
1103 *
1104 * Compute a SID to use for labeling a new subject or object in the
1105 * class @tclass based on a SID pair (@ssid, @tsid).
1106 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1107 * if insufficient memory is available, or %0 if the new SID was
1108 * computed successfully.
1109 */
1110int security_transition_sid(u32 ssid,
1111 u32 tsid,
1112 u16 tclass,
1113 u32 *out_sid)
1114{
1115 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1116}
1117
1118/**
1119 * security_member_sid - Compute the SID for member selection.
1120 * @ssid: source security identifier
1121 * @tsid: target security identifier
1122 * @tclass: target security class
1123 * @out_sid: security identifier for selected member
1124 *
1125 * Compute a SID to use when selecting a member of a polyinstantiated
1126 * object of class @tclass based on a SID pair (@ssid, @tsid).
1127 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1128 * if insufficient memory is available, or %0 if the SID was
1129 * computed successfully.
1130 */
1131int security_member_sid(u32 ssid,
1132 u32 tsid,
1133 u16 tclass,
1134 u32 *out_sid)
1135{
1136 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1137}
1138
1139/**
1140 * security_change_sid - Compute the SID for object relabeling.
1141 * @ssid: source security identifier
1142 * @tsid: target security identifier
1143 * @tclass: target security class
1144 * @out_sid: security identifier for selected member
1145 *
1146 * Compute a SID to use for relabeling an object of class @tclass
1147 * based on a SID pair (@ssid, @tsid).
1148 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1149 * if insufficient memory is available, or %0 if the SID was
1150 * computed successfully.
1151 */
1152int security_change_sid(u32 ssid,
1153 u32 tsid,
1154 u16 tclass,
1155 u32 *out_sid)
1156{
1157 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1158}
1159
Chad Sellersb94c7e62006-11-06 12:38:18 -05001160/*
1161 * Verify that each kernel class that is defined in the
1162 * policy is correct
1163 */
1164static int validate_classes(struct policydb *p)
1165{
1166 int i, j;
1167 struct class_datum *cladatum;
1168 struct perm_datum *perdatum;
1169 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1170 u16 class_val;
1171 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1172 const char *def_class, *def_perm, *pol_class;
1173 struct symtab *perms;
1174
Eric Paris3f120702007-09-21 14:37:10 -04001175 if (p->allow_unknown) {
1176 u32 num_classes = kdefs->cts_len;
1177 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1178 if (!p->undefined_perms)
1179 return -ENOMEM;
1180 }
1181
Chad Sellersb94c7e62006-11-06 12:38:18 -05001182 for (i = 1; i < kdefs->cts_len; i++) {
1183 def_class = kdefs->class_to_string[i];
Stephen Smalleya764ae42007-03-26 13:36:26 -04001184 if (!def_class)
1185 continue;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001186 if (i > p->p_classes.nprim) {
1187 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001188 "SELinux: class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001189 def_class);
Eric Paris3f120702007-09-21 14:37:10 -04001190 if (p->reject_unknown)
1191 return -EINVAL;
1192 if (p->allow_unknown)
1193 p->undefined_perms[i-1] = ~0U;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001194 continue;
1195 }
1196 pol_class = p->p_class_val_to_name[i-1];
1197 if (strcmp(pol_class, def_class)) {
1198 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001199 "SELinux: class %d is incorrect, found %s but should be %s\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001200 i, pol_class, def_class);
1201 return -EINVAL;
1202 }
1203 }
1204 for (i = 0; i < kdefs->av_pts_len; i++) {
1205 class_val = kdefs->av_perm_to_string[i].tclass;
1206 perm_val = kdefs->av_perm_to_string[i].value;
1207 def_perm = kdefs->av_perm_to_string[i].name;
1208 if (class_val > p->p_classes.nprim)
1209 continue;
1210 pol_class = p->p_class_val_to_name[class_val-1];
1211 cladatum = hashtab_search(p->p_classes.table, pol_class);
1212 BUG_ON(!cladatum);
1213 perms = &cladatum->permissions;
1214 nprim = 1 << (perms->nprim - 1);
1215 if (perm_val > nprim) {
1216 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001217 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001218 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001219 if (p->reject_unknown)
1220 return -EINVAL;
1221 if (p->allow_unknown)
1222 p->undefined_perms[class_val-1] |= perm_val;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001223 continue;
1224 }
1225 perdatum = hashtab_search(perms->table, def_perm);
1226 if (perdatum == NULL) {
1227 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001228 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001229 def_perm, pol_class);
1230 return -EINVAL;
1231 }
1232 pol_val = 1 << (perdatum->value - 1);
1233 if (pol_val != perm_val) {
1234 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001235 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001236 def_perm, pol_class);
1237 return -EINVAL;
1238 }
1239 }
1240 for (i = 0; i < kdefs->av_inherit_len; i++) {
1241 class_val = kdefs->av_inherit[i].tclass;
1242 if (class_val > p->p_classes.nprim)
1243 continue;
1244 pol_class = p->p_class_val_to_name[class_val-1];
1245 cladatum = hashtab_search(p->p_classes.table, pol_class);
1246 BUG_ON(!cladatum);
1247 if (!cladatum->comdatum) {
1248 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001249 "SELinux: class %s should have an inherits clause but does not\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001250 pol_class);
1251 return -EINVAL;
1252 }
1253 tmp = kdefs->av_inherit[i].common_base;
1254 common_pts_len = 0;
1255 while (!(tmp & 0x01)) {
1256 common_pts_len++;
1257 tmp >>= 1;
1258 }
1259 perms = &cladatum->comdatum->permissions;
1260 for (j = 0; j < common_pts_len; j++) {
1261 def_perm = kdefs->av_inherit[i].common_pts[j];
1262 if (j >= perms->nprim) {
1263 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001264 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001265 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001266 if (p->reject_unknown)
1267 return -EINVAL;
1268 if (p->allow_unknown)
1269 p->undefined_perms[class_val-1] |= (1 << j);
Chad Sellersb94c7e62006-11-06 12:38:18 -05001270 continue;
1271 }
1272 perdatum = hashtab_search(perms->table, def_perm);
1273 if (perdatum == NULL) {
1274 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001275 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001276 def_perm, pol_class);
1277 return -EINVAL;
1278 }
1279 if (perdatum->value != j + 1) {
1280 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001281 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001282 def_perm, pol_class);
1283 return -EINVAL;
1284 }
1285 }
1286 }
1287 return 0;
1288}
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290/* Clone the SID into the new SID table. */
1291static int clone_sid(u32 sid,
1292 struct context *context,
1293 void *arg)
1294{
1295 struct sidtab *s = arg;
1296
1297 return sidtab_insert(s, sid, context);
1298}
1299
1300static inline int convert_context_handle_invalid_context(struct context *context)
1301{
1302 int rc = 0;
1303
1304 if (selinux_enforcing) {
1305 rc = -EINVAL;
1306 } else {
1307 char *s;
1308 u32 len;
1309
Stephen Smalley12b29f32008-05-07 13:03:20 -04001310 if (!context_struct_to_string(context, &s, &len)) {
1311 printk(KERN_WARNING
1312 "SELinux: Context %s would be invalid if enforcing\n",
1313 s);
1314 kfree(s);
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317 return rc;
1318}
1319
1320struct convert_context_args {
1321 struct policydb *oldp;
1322 struct policydb *newp;
1323};
1324
1325/*
1326 * Convert the values in the security context
1327 * structure `c' from the values specified
1328 * in the policy `p->oldp' to the values specified
1329 * in the policy `p->newp'. Verify that the
1330 * context is valid under the new policy.
1331 */
1332static int convert_context(u32 key,
1333 struct context *c,
1334 void *p)
1335{
1336 struct convert_context_args *args;
1337 struct context oldc;
1338 struct role_datum *role;
1339 struct type_datum *typdatum;
1340 struct user_datum *usrdatum;
1341 char *s;
1342 u32 len;
1343 int rc;
1344
1345 args = p;
1346
Stephen Smalley12b29f32008-05-07 13:03:20 -04001347 if (c->str) {
1348 struct context ctx;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001349 s = kstrdup(c->str, GFP_KERNEL);
1350 if (!s) {
1351 rc = -ENOMEM;
1352 goto out;
1353 }
1354 rc = string_to_context_struct(args->newp, NULL, s,
1355 c->len, &ctx, SECSID_NULL);
1356 kfree(s);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001357 if (!rc) {
1358 printk(KERN_INFO
1359 "SELinux: Context %s became valid (mapped).\n",
1360 c->str);
1361 /* Replace string with mapped representation. */
1362 kfree(c->str);
1363 memcpy(c, &ctx, sizeof(*c));
1364 goto out;
1365 } else if (rc == -EINVAL) {
1366 /* Retain string representation for later mapping. */
1367 rc = 0;
1368 goto out;
1369 } else {
1370 /* Other error condition, e.g. ENOMEM. */
1371 printk(KERN_ERR
1372 "SELinux: Unable to map context %s, rc = %d.\n",
1373 c->str, -rc);
1374 goto out;
1375 }
1376 }
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 rc = context_cpy(&oldc, c);
1379 if (rc)
1380 goto out;
1381
1382 rc = -EINVAL;
1383
1384 /* Convert the user. */
1385 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001386 args->oldp->p_user_val_to_name[c->user - 1]);
1387 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 c->user = usrdatum->value;
1390
1391 /* Convert the role. */
1392 role = hashtab_search(args->newp->p_roles.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001393 args->oldp->p_role_val_to_name[c->role - 1]);
1394 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 c->role = role->value;
1397
1398 /* Convert the type. */
1399 typdatum = hashtab_search(args->newp->p_types.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001400 args->oldp->p_type_val_to_name[c->type - 1]);
1401 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 c->type = typdatum->value;
1404
1405 rc = mls_convert_context(args->oldp, args->newp, c);
1406 if (rc)
1407 goto bad;
1408
1409 /* Check the validity of the new context. */
1410 if (!policydb_context_isvalid(args->newp, c)) {
1411 rc = convert_context_handle_invalid_context(&oldc);
1412 if (rc)
1413 goto bad;
1414 }
1415
1416 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001417 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418out:
1419 return rc;
1420bad:
Stephen Smalley12b29f32008-05-07 13:03:20 -04001421 /* Map old representation to string and save it. */
1422 if (context_struct_to_string(&oldc, &s, &len))
1423 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001425 context_destroy(c);
1426 c->str = s;
1427 c->len = len;
1428 printk(KERN_INFO
1429 "SELinux: Context %s became invalid (unmapped).\n",
1430 c->str);
1431 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 goto out;
1433}
1434
Paul Moore3bb56b22008-01-29 08:38:19 -05001435static void security_load_policycaps(void)
1436{
1437 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1438 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05001439 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1440 POLICYDB_CAPABILITY_OPENPERM);
Paul Moore3bb56b22008-01-29 08:38:19 -05001441}
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443extern void selinux_complete_init(void);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001444static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446/**
1447 * security_load_policy - Load a security policy configuration.
1448 * @data: binary policy data
1449 * @len: length of data in bytes
1450 *
1451 * Load a new set of security policy configuration data,
1452 * validate it and convert the SID table as necessary.
1453 * This function will flush the access vector cache after
1454 * loading the new policy.
1455 */
1456int security_load_policy(void *data, size_t len)
1457{
1458 struct policydb oldpolicydb, newpolicydb;
1459 struct sidtab oldsidtab, newsidtab;
1460 struct convert_context_args args;
1461 u32 seqno;
1462 int rc = 0;
1463 struct policy_file file = { data, len }, *fp = &file;
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (!ss_initialized) {
1466 avtab_cache_init();
1467 if (policydb_read(&policydb, fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 avtab_cache_destroy();
1469 return -EINVAL;
1470 }
1471 if (policydb_load_isids(&policydb, &sidtab)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 policydb_destroy(&policydb);
1473 avtab_cache_destroy();
1474 return -EINVAL;
1475 }
Chad Sellersb94c7e62006-11-06 12:38:18 -05001476 /* Verify that the kernel defined classes are correct. */
1477 if (validate_classes(&policydb)) {
1478 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001479 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001480 sidtab_destroy(&sidtab);
1481 policydb_destroy(&policydb);
1482 avtab_cache_destroy();
1483 return -EINVAL;
1484 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001485 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 policydb_loaded_version = policydb.policyvers;
1487 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001488 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001490 avc_ss_reset(seqno);
1491 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001492 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001493 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return 0;
1495 }
1496
1497#if 0
1498 sidtab_hash_eval(&sidtab, "sids");
1499#endif
1500
Eric Paris89abd0a2008-06-09 15:58:04 -04001501 if (policydb_read(&newpolicydb, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Stephen Smalley12b29f32008-05-07 13:03:20 -04001504 if (sidtab_init(&newsidtab)) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04001505 policydb_destroy(&newpolicydb);
1506 return -ENOMEM;
1507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Chad Sellersb94c7e62006-11-06 12:38:18 -05001509 /* Verify that the kernel defined classes are correct. */
1510 if (validate_classes(&newpolicydb)) {
1511 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001512 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001513 rc = -EINVAL;
1514 goto err;
1515 }
1516
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001517 rc = security_preserve_bools(&newpolicydb);
1518 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001519 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001520 goto err;
1521 }
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 /* Clone the SID table. */
1524 sidtab_shutdown(&sidtab);
1525 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1526 rc = -ENOMEM;
1527 goto err;
1528 }
1529
Stephen Smalley12b29f32008-05-07 13:03:20 -04001530 /*
1531 * Convert the internal representations of contexts
1532 * in the new SID table.
1533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 args.oldp = &policydb;
1535 args.newp = &newpolicydb;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001536 rc = sidtab_map(&newsidtab, convert_context, &args);
1537 if (rc)
1538 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 /* Save the old policydb and SID table to free later. */
1541 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1542 sidtab_set(&oldsidtab, &sidtab);
1543
1544 /* Install the new policydb and SID table. */
James Morris0804d112008-06-06 18:40:29 +10001545 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 memcpy(&policydb, &newpolicydb, sizeof policydb);
1547 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05001548 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 seqno = ++latest_granting;
1550 policydb_loaded_version = policydb.policyvers;
James Morris0804d112008-06-06 18:40:29 +10001551 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 /* Free the old policydb and SID table. */
1554 policydb_destroy(&oldpolicydb);
1555 sidtab_destroy(&oldsidtab);
1556
1557 avc_ss_reset(seqno);
1558 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001559 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001560 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 return 0;
1563
1564err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 sidtab_destroy(&newsidtab);
1566 policydb_destroy(&newpolicydb);
1567 return rc;
1568
1569}
1570
1571/**
1572 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 * @protocol: protocol number
1574 * @port: port number
1575 * @out_sid: security identifier
1576 */
Paul Moore3e112172008-04-10 10:48:14 -04001577int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 struct ocontext *c;
1580 int rc = 0;
1581
James Morris0804d112008-06-06 18:40:29 +10001582 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 c = policydb.ocontexts[OCON_PORT];
1585 while (c) {
1586 if (c->u.port.protocol == protocol &&
1587 c->u.port.low_port <= port &&
1588 c->u.port.high_port >= port)
1589 break;
1590 c = c->next;
1591 }
1592
1593 if (c) {
1594 if (!c->sid[0]) {
1595 rc = sidtab_context_to_sid(&sidtab,
1596 &c->context[0],
1597 &c->sid[0]);
1598 if (rc)
1599 goto out;
1600 }
1601 *out_sid = c->sid[0];
1602 } else {
1603 *out_sid = SECINITSID_PORT;
1604 }
1605
1606out:
James Morris0804d112008-06-06 18:40:29 +10001607 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return rc;
1609}
1610
1611/**
1612 * security_netif_sid - Obtain the SID for a network interface.
1613 * @name: interface name
1614 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05001616int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 int rc = 0;
1619 struct ocontext *c;
1620
James Morris0804d112008-06-06 18:40:29 +10001621 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 c = policydb.ocontexts[OCON_NETIF];
1624 while (c) {
1625 if (strcmp(name, c->u.name) == 0)
1626 break;
1627 c = c->next;
1628 }
1629
1630 if (c) {
1631 if (!c->sid[0] || !c->sid[1]) {
1632 rc = sidtab_context_to_sid(&sidtab,
1633 &c->context[0],
1634 &c->sid[0]);
1635 if (rc)
1636 goto out;
1637 rc = sidtab_context_to_sid(&sidtab,
1638 &c->context[1],
1639 &c->sid[1]);
1640 if (rc)
1641 goto out;
1642 }
1643 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05001644 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647out:
James Morris0804d112008-06-06 18:40:29 +10001648 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return rc;
1650}
1651
1652static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1653{
1654 int i, fail = 0;
1655
Eric Paris5d55a342008-04-18 17:38:33 -04001656 for (i = 0; i < 4; i++)
1657 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 fail = 1;
1659 break;
1660 }
1661
1662 return !fail;
1663}
1664
1665/**
1666 * security_node_sid - Obtain the SID for a node (host).
1667 * @domain: communication domain aka address family
1668 * @addrp: address
1669 * @addrlen: address length in bytes
1670 * @out_sid: security identifier
1671 */
1672int security_node_sid(u16 domain,
1673 void *addrp,
1674 u32 addrlen,
1675 u32 *out_sid)
1676{
1677 int rc = 0;
1678 struct ocontext *c;
1679
James Morris0804d112008-06-06 18:40:29 +10001680 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 switch (domain) {
1683 case AF_INET: {
1684 u32 addr;
1685
1686 if (addrlen != sizeof(u32)) {
1687 rc = -EINVAL;
1688 goto out;
1689 }
1690
1691 addr = *((u32 *)addrp);
1692
1693 c = policydb.ocontexts[OCON_NODE];
1694 while (c) {
1695 if (c->u.node.addr == (addr & c->u.node.mask))
1696 break;
1697 c = c->next;
1698 }
1699 break;
1700 }
1701
1702 case AF_INET6:
1703 if (addrlen != sizeof(u64) * 2) {
1704 rc = -EINVAL;
1705 goto out;
1706 }
1707 c = policydb.ocontexts[OCON_NODE6];
1708 while (c) {
1709 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1710 c->u.node6.mask))
1711 break;
1712 c = c->next;
1713 }
1714 break;
1715
1716 default:
1717 *out_sid = SECINITSID_NODE;
1718 goto out;
1719 }
1720
1721 if (c) {
1722 if (!c->sid[0]) {
1723 rc = sidtab_context_to_sid(&sidtab,
1724 &c->context[0],
1725 &c->sid[0]);
1726 if (rc)
1727 goto out;
1728 }
1729 *out_sid = c->sid[0];
1730 } else {
1731 *out_sid = SECINITSID_NODE;
1732 }
1733
1734out:
James Morris0804d112008-06-06 18:40:29 +10001735 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return rc;
1737}
1738
1739#define SIDS_NEL 25
1740
1741/**
1742 * security_get_user_sids - Obtain reachable SIDs for a user.
1743 * @fromsid: starting SID
1744 * @username: username
1745 * @sids: array of reachable SIDs for user
1746 * @nel: number of elements in @sids
1747 *
1748 * Generate the set of SIDs for legal security contexts
1749 * for a given user that can be reached by @fromsid.
1750 * Set *@sids to point to a dynamically allocated
1751 * array containing the set of SIDs. Set *@nel to the
1752 * number of elements in the array.
1753 */
1754
1755int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04001756 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 u32 **sids,
1758 u32 *nel)
1759{
1760 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001761 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 u32 mynel = 0, maxnel = SIDS_NEL;
1763 struct user_datum *user;
1764 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001765 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 int rc = 0, i, j;
1767
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001768 *sids = NULL;
1769 *nel = 0;
1770
1771 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
James Morris0804d112008-06-06 18:40:29 +10001774 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Stephen Smalley12b29f32008-05-07 13:03:20 -04001776 context_init(&usercon);
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 fromcon = sidtab_search(&sidtab, fromsid);
1779 if (!fromcon) {
1780 rc = -EINVAL;
1781 goto out_unlock;
1782 }
1783
1784 user = hashtab_search(policydb.p_users.table, username);
1785 if (!user) {
1786 rc = -EINVAL;
1787 goto out_unlock;
1788 }
1789 usercon.user = user->value;
1790
James Morris89d155e2005-10-30 14:59:21 -08001791 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (!mysids) {
1793 rc = -ENOMEM;
1794 goto out_unlock;
1795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001797 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 role = policydb.role_val_to_struct[i];
1799 usercon.role = i+1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001800 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 usercon.type = j+1;
1802
1803 if (mls_setup_user_range(fromcon, user, &usercon))
1804 continue;
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001807 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (mynel < maxnel) {
1810 mysids[mynel++] = sid;
1811 } else {
1812 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08001813 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (!mysids2) {
1815 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 goto out_unlock;
1817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1819 kfree(mysids);
1820 mysids = mysids2;
1821 mysids[mynel++] = sid;
1822 }
1823 }
1824 }
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001827 read_unlock(&policy_rwlock);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001828 if (rc || !mynel) {
1829 kfree(mysids);
1830 goto out;
1831 }
1832
1833 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1834 if (!mysids2) {
1835 rc = -ENOMEM;
1836 kfree(mysids);
1837 goto out;
1838 }
1839 for (i = 0, j = 0; i < mynel; i++) {
1840 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1841 SECCLASS_PROCESS,
1842 PROCESS__TRANSITION, AVC_STRICT,
1843 NULL);
1844 if (!rc)
1845 mysids2[j++] = mysids[i];
1846 cond_resched();
1847 }
1848 rc = 0;
1849 kfree(mysids);
1850 *sids = mysids2;
1851 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852out:
1853 return rc;
1854}
1855
1856/**
1857 * security_genfs_sid - Obtain a SID for a file in a filesystem
1858 * @fstype: filesystem type
1859 * @path: path from root of mount
1860 * @sclass: file security class
1861 * @sid: SID for path
1862 *
1863 * Obtain a SID to use for a file in a filesystem that
1864 * cannot support xattr or use a fixed labeling behavior like
1865 * transition SIDs or task SIDs.
1866 */
1867int security_genfs_sid(const char *fstype,
Eric Paris5d55a342008-04-18 17:38:33 -04001868 char *path,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 u16 sclass,
1870 u32 *sid)
1871{
1872 int len;
1873 struct genfs *genfs;
1874 struct ocontext *c;
1875 int rc = 0, cmp = 0;
1876
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05001877 while (path[0] == '/' && path[1] == '/')
1878 path++;
1879
James Morris0804d112008-06-06 18:40:29 +10001880 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1883 cmp = strcmp(fstype, genfs->fstype);
1884 if (cmp <= 0)
1885 break;
1886 }
1887
1888 if (!genfs || cmp) {
1889 *sid = SECINITSID_UNLABELED;
1890 rc = -ENOENT;
1891 goto out;
1892 }
1893
1894 for (c = genfs->head; c; c = c->next) {
1895 len = strlen(c->u.name);
1896 if ((!c->v.sclass || sclass == c->v.sclass) &&
1897 (strncmp(c->u.name, path, len) == 0))
1898 break;
1899 }
1900
1901 if (!c) {
1902 *sid = SECINITSID_UNLABELED;
1903 rc = -ENOENT;
1904 goto out;
1905 }
1906
1907 if (!c->sid[0]) {
1908 rc = sidtab_context_to_sid(&sidtab,
1909 &c->context[0],
1910 &c->sid[0]);
1911 if (rc)
1912 goto out;
1913 }
1914
1915 *sid = c->sid[0];
1916out:
James Morris0804d112008-06-06 18:40:29 +10001917 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return rc;
1919}
1920
1921/**
1922 * security_fs_use - Determine how to handle labeling for a filesystem.
1923 * @fstype: filesystem type
1924 * @behavior: labeling behavior
1925 * @sid: SID for filesystem (superblock)
1926 */
1927int security_fs_use(
1928 const char *fstype,
1929 unsigned int *behavior,
1930 u32 *sid)
1931{
1932 int rc = 0;
1933 struct ocontext *c;
1934
James Morris0804d112008-06-06 18:40:29 +10001935 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 c = policydb.ocontexts[OCON_FSUSE];
1938 while (c) {
1939 if (strcmp(fstype, c->u.name) == 0)
1940 break;
1941 c = c->next;
1942 }
1943
1944 if (c) {
1945 *behavior = c->v.behavior;
1946 if (!c->sid[0]) {
1947 rc = sidtab_context_to_sid(&sidtab,
1948 &c->context[0],
1949 &c->sid[0]);
1950 if (rc)
1951 goto out;
1952 }
1953 *sid = c->sid[0];
1954 } else {
1955 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1956 if (rc) {
1957 *behavior = SECURITY_FS_USE_NONE;
1958 rc = 0;
1959 } else {
1960 *behavior = SECURITY_FS_USE_GENFS;
1961 }
1962 }
1963
1964out:
James Morris0804d112008-06-06 18:40:29 +10001965 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return rc;
1967}
1968
1969int security_get_bools(int *len, char ***names, int **values)
1970{
1971 int i, rc = -ENOMEM;
1972
James Morris0804d112008-06-06 18:40:29 +10001973 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 *names = NULL;
1975 *values = NULL;
1976
1977 *len = policydb.p_bools.nprim;
1978 if (!*len) {
1979 rc = 0;
1980 goto out;
1981 }
1982
Eric Paris5d55a342008-04-18 17:38:33 -04001983 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (!*names)
1985 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Jesper Juhle0795cf2006-01-09 20:54:46 -08001987 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (!*values)
1989 goto err;
1990
1991 for (i = 0; i < *len; i++) {
1992 size_t name_len;
1993 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1994 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04001995 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (!(*names)[i])
1997 goto err;
1998 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1999 (*names)[i][name_len - 1] = 0;
2000 }
2001 rc = 0;
2002out:
James Morris0804d112008-06-06 18:40:29 +10002003 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return rc;
2005err:
2006 if (*names) {
2007 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002008 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002010 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 goto out;
2012}
2013
2014
2015int security_set_bools(int len, int *values)
2016{
2017 int i, rc = 0;
2018 int lenp, seqno = 0;
2019 struct cond_node *cur;
2020
James Morris0804d112008-06-06 18:40:29 +10002021 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 lenp = policydb.p_bools.nprim;
2024 if (len != lenp) {
2025 rc = -EFAULT;
2026 goto out;
2027 }
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00002030 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2031 audit_log(current->audit_context, GFP_ATOMIC,
2032 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05002033 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Steve Grubbaf601e42006-01-04 14:08:39 +00002034 policydb.p_bool_val_to_name[i],
2035 !!values[i],
2036 policydb.bool_val_to_struct[i]->state,
Eric Paris4746ec52008-01-08 10:06:53 -05002037 audit_get_loginuid(current),
2038 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00002039 }
Eric Paris5d55a342008-04-18 17:38:33 -04002040 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002042 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
2047 rc = evaluate_cond_node(&policydb, cur);
2048 if (rc)
2049 goto out;
2050 }
2051
2052 seqno = ++latest_granting;
2053
2054out:
James Morris0804d112008-06-06 18:40:29 +10002055 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (!rc) {
2057 avc_ss_reset(seqno);
2058 selnl_notify_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002059 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061 return rc;
2062}
2063
2064int security_get_bool_value(int bool)
2065{
2066 int rc = 0;
2067 int len;
2068
James Morris0804d112008-06-06 18:40:29 +10002069 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 len = policydb.p_bools.nprim;
2072 if (bool >= len) {
2073 rc = -EFAULT;
2074 goto out;
2075 }
2076
2077 rc = policydb.bool_val_to_struct[bool]->state;
2078out:
James Morris0804d112008-06-06 18:40:29 +10002079 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return rc;
2081}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002082
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002083static int security_preserve_bools(struct policydb *p)
2084{
2085 int rc, nbools = 0, *bvalues = NULL, i;
2086 char **bnames = NULL;
2087 struct cond_bool_datum *booldatum;
2088 struct cond_node *cur;
2089
2090 rc = security_get_bools(&nbools, &bnames, &bvalues);
2091 if (rc)
2092 goto out;
2093 for (i = 0; i < nbools; i++) {
2094 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2095 if (booldatum)
2096 booldatum->state = bvalues[i];
2097 }
2098 for (cur = p->cond_list; cur != NULL; cur = cur->next) {
2099 rc = evaluate_cond_node(p, cur);
2100 if (rc)
2101 goto out;
2102 }
2103
2104out:
2105 if (bnames) {
2106 for (i = 0; i < nbools; i++)
2107 kfree(bnames[i]);
2108 }
2109 kfree(bnames);
2110 kfree(bvalues);
2111 return rc;
2112}
2113
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002114/*
2115 * security_sid_mls_copy() - computes a new sid based on the given
2116 * sid and the mls portion of mls_sid.
2117 */
2118int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2119{
2120 struct context *context1;
2121 struct context *context2;
2122 struct context newcon;
2123 char *s;
2124 u32 len;
2125 int rc = 0;
2126
Venkat Yekkirala4eb327b2006-09-19 10:24:19 -07002127 if (!ss_initialized || !selinux_mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002128 *new_sid = sid;
2129 goto out;
2130 }
2131
2132 context_init(&newcon);
2133
James Morris0804d112008-06-06 18:40:29 +10002134 read_lock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002135 context1 = sidtab_search(&sidtab, sid);
2136 if (!context1) {
Eric Paris744ba352008-04-17 11:52:44 -04002137 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2138 __func__, sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002139 rc = -EINVAL;
2140 goto out_unlock;
2141 }
2142
2143 context2 = sidtab_search(&sidtab, mls_sid);
2144 if (!context2) {
Eric Paris744ba352008-04-17 11:52:44 -04002145 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2146 __func__, mls_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002147 rc = -EINVAL;
2148 goto out_unlock;
2149 }
2150
2151 newcon.user = context1->user;
2152 newcon.role = context1->role;
2153 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002154 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002155 if (rc)
2156 goto out_unlock;
2157
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002158 /* Check the validity of the new context. */
2159 if (!policydb_context_isvalid(&policydb, &newcon)) {
2160 rc = convert_context_handle_invalid_context(&newcon);
2161 if (rc)
2162 goto bad;
2163 }
2164
2165 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2166 goto out_unlock;
2167
2168bad:
2169 if (!context_struct_to_string(&newcon, &s, &len)) {
2170 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2171 "security_sid_mls_copy: invalid context %s", s);
2172 kfree(s);
2173 }
2174
2175out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002176 read_unlock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002177 context_destroy(&newcon);
2178out:
2179 return rc;
2180}
2181
Paul Moore220deb92008-01-29 08:38:23 -05002182/**
2183 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2184 * @nlbl_sid: NetLabel SID
2185 * @nlbl_type: NetLabel labeling protocol type
2186 * @xfrm_sid: XFRM SID
2187 *
2188 * Description:
2189 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2190 * resolved into a single SID it is returned via @peer_sid and the function
2191 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2192 * returns a negative value. A table summarizing the behavior is below:
2193 *
2194 * | function return | @sid
2195 * ------------------------------+-----------------+-----------------
2196 * no peer labels | 0 | SECSID_NULL
2197 * single peer label | 0 | <peer_label>
2198 * multiple, consistent labels | 0 | <peer_label>
2199 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2200 *
2201 */
2202int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2203 u32 xfrm_sid,
2204 u32 *peer_sid)
2205{
2206 int rc;
2207 struct context *nlbl_ctx;
2208 struct context *xfrm_ctx;
2209
2210 /* handle the common (which also happens to be the set of easy) cases
2211 * right away, these two if statements catch everything involving a
2212 * single or absent peer SID/label */
2213 if (xfrm_sid == SECSID_NULL) {
2214 *peer_sid = nlbl_sid;
2215 return 0;
2216 }
2217 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2218 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2219 * is present */
2220 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2221 *peer_sid = xfrm_sid;
2222 return 0;
2223 }
2224
2225 /* we don't need to check ss_initialized here since the only way both
2226 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2227 * security server was initialized and ss_initialized was true */
2228 if (!selinux_mls_enabled) {
2229 *peer_sid = SECSID_NULL;
2230 return 0;
2231 }
2232
James Morris0804d112008-06-06 18:40:29 +10002233 read_lock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002234
2235 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2236 if (!nlbl_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002237 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2238 __func__, nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002239 rc = -EINVAL;
2240 goto out_slowpath;
2241 }
2242 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2243 if (!xfrm_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002244 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2245 __func__, xfrm_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002246 rc = -EINVAL;
2247 goto out_slowpath;
2248 }
2249 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2250
2251out_slowpath:
James Morris0804d112008-06-06 18:40:29 +10002252 read_unlock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002253 if (rc == 0)
2254 /* at present NetLabel SIDs/labels really only carry MLS
2255 * information so if the MLS portion of the NetLabel SID
2256 * matches the MLS portion of the labeled XFRM SID/label
2257 * then pass along the XFRM SID as it is the most
2258 * expressive */
2259 *peer_sid = xfrm_sid;
2260 else
2261 *peer_sid = SECSID_NULL;
2262 return rc;
2263}
2264
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002265static int get_classes_callback(void *k, void *d, void *args)
2266{
2267 struct class_datum *datum = d;
2268 char *name = k, **classes = args;
2269 int value = datum->value - 1;
2270
2271 classes[value] = kstrdup(name, GFP_ATOMIC);
2272 if (!classes[value])
2273 return -ENOMEM;
2274
2275 return 0;
2276}
2277
2278int security_get_classes(char ***classes, int *nclasses)
2279{
2280 int rc = -ENOMEM;
2281
James Morris0804d112008-06-06 18:40:29 +10002282 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002283
2284 *nclasses = policydb.p_classes.nprim;
2285 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2286 if (!*classes)
2287 goto out;
2288
2289 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2290 *classes);
2291 if (rc < 0) {
2292 int i;
2293 for (i = 0; i < *nclasses; i++)
2294 kfree((*classes)[i]);
2295 kfree(*classes);
2296 }
2297
2298out:
James Morris0804d112008-06-06 18:40:29 +10002299 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002300 return rc;
2301}
2302
2303static int get_permissions_callback(void *k, void *d, void *args)
2304{
2305 struct perm_datum *datum = d;
2306 char *name = k, **perms = args;
2307 int value = datum->value - 1;
2308
2309 perms[value] = kstrdup(name, GFP_ATOMIC);
2310 if (!perms[value])
2311 return -ENOMEM;
2312
2313 return 0;
2314}
2315
2316int security_get_permissions(char *class, char ***perms, int *nperms)
2317{
2318 int rc = -ENOMEM, i;
2319 struct class_datum *match;
2320
James Morris0804d112008-06-06 18:40:29 +10002321 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002322
2323 match = hashtab_search(policydb.p_classes.table, class);
2324 if (!match) {
Eric Paris744ba352008-04-17 11:52:44 -04002325 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002326 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002327 rc = -EINVAL;
2328 goto out;
2329 }
2330
2331 *nperms = match->permissions.nprim;
2332 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2333 if (!*perms)
2334 goto out;
2335
2336 if (match->comdatum) {
2337 rc = hashtab_map(match->comdatum->permissions.table,
2338 get_permissions_callback, *perms);
2339 if (rc < 0)
2340 goto err;
2341 }
2342
2343 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2344 *perms);
2345 if (rc < 0)
2346 goto err;
2347
2348out:
James Morris0804d112008-06-06 18:40:29 +10002349 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002350 return rc;
2351
2352err:
James Morris0804d112008-06-06 18:40:29 +10002353 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002354 for (i = 0; i < *nperms; i++)
2355 kfree((*perms)[i]);
2356 kfree(*perms);
2357 return rc;
2358}
2359
Eric Paris3f120702007-09-21 14:37:10 -04002360int security_get_reject_unknown(void)
2361{
2362 return policydb.reject_unknown;
2363}
2364
2365int security_get_allow_unknown(void)
2366{
2367 return policydb.allow_unknown;
2368}
2369
Paul Moore3bb56b22008-01-29 08:38:19 -05002370/**
Paul Moore3bb56b22008-01-29 08:38:19 -05002371 * security_policycap_supported - Check for a specific policy capability
2372 * @req_cap: capability
2373 *
2374 * Description:
2375 * This function queries the currently loaded policy to see if it supports the
2376 * capability specified by @req_cap. Returns true (1) if the capability is
2377 * supported, false (0) if it isn't supported.
2378 *
2379 */
2380int security_policycap_supported(unsigned int req_cap)
2381{
2382 int rc;
2383
James Morris0804d112008-06-06 18:40:29 +10002384 read_lock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002385 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
James Morris0804d112008-06-06 18:40:29 +10002386 read_unlock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002387
2388 return rc;
2389}
2390
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002391struct selinux_audit_rule {
2392 u32 au_seqno;
2393 struct context au_ctxt;
2394};
2395
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002396void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002397{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002398 struct selinux_audit_rule *rule = vrule;
2399
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002400 if (rule) {
2401 context_destroy(&rule->au_ctxt);
2402 kfree(rule);
2403 }
2404}
2405
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002406int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002407{
2408 struct selinux_audit_rule *tmprule;
2409 struct role_datum *roledatum;
2410 struct type_datum *typedatum;
2411 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002412 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002413 int rc = 0;
2414
2415 *rule = NULL;
2416
2417 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07002418 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002419
2420 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002421 case AUDIT_SUBJ_USER:
2422 case AUDIT_SUBJ_ROLE:
2423 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002424 case AUDIT_OBJ_USER:
2425 case AUDIT_OBJ_ROLE:
2426 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002427 /* only 'equals' and 'not equals' fit user, role, and type */
2428 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2429 return -EINVAL;
2430 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002431 case AUDIT_SUBJ_SEN:
2432 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002433 case AUDIT_OBJ_LEV_LOW:
2434 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002435 /* we do not allow a range, indicated by the presense of '-' */
2436 if (strchr(rulestr, '-'))
2437 return -EINVAL;
2438 break;
2439 default:
2440 /* only the above fields are valid */
2441 return -EINVAL;
2442 }
2443
2444 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2445 if (!tmprule)
2446 return -ENOMEM;
2447
2448 context_init(&tmprule->au_ctxt);
2449
James Morris0804d112008-06-06 18:40:29 +10002450 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002451
2452 tmprule->au_seqno = latest_granting;
2453
2454 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002455 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002456 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002457 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2458 if (!userdatum)
2459 rc = -EINVAL;
2460 else
2461 tmprule->au_ctxt.user = userdatum->value;
2462 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002463 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002464 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002465 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2466 if (!roledatum)
2467 rc = -EINVAL;
2468 else
2469 tmprule->au_ctxt.role = roledatum->value;
2470 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002471 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002472 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002473 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2474 if (!typedatum)
2475 rc = -EINVAL;
2476 else
2477 tmprule->au_ctxt.type = typedatum->value;
2478 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002479 case AUDIT_SUBJ_SEN:
2480 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002481 case AUDIT_OBJ_LEV_LOW:
2482 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002483 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2484 break;
2485 }
2486
James Morris0804d112008-06-06 18:40:29 +10002487 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002488
2489 if (rc) {
2490 selinux_audit_rule_free(tmprule);
2491 tmprule = NULL;
2492 }
2493
2494 *rule = tmprule;
2495
2496 return rc;
2497}
2498
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002499/* Check to see if the rule contains any selinux fields */
2500int selinux_audit_rule_known(struct audit_krule *rule)
2501{
2502 int i;
2503
2504 for (i = 0; i < rule->field_count; i++) {
2505 struct audit_field *f = &rule->fields[i];
2506 switch (f->type) {
2507 case AUDIT_SUBJ_USER:
2508 case AUDIT_SUBJ_ROLE:
2509 case AUDIT_SUBJ_TYPE:
2510 case AUDIT_SUBJ_SEN:
2511 case AUDIT_SUBJ_CLR:
2512 case AUDIT_OBJ_USER:
2513 case AUDIT_OBJ_ROLE:
2514 case AUDIT_OBJ_TYPE:
2515 case AUDIT_OBJ_LEV_LOW:
2516 case AUDIT_OBJ_LEV_HIGH:
2517 return 1;
2518 }
2519 }
2520
2521 return 0;
2522}
2523
2524int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Eric Parisf5269712008-05-14 11:27:45 -04002525 struct audit_context *actx)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002526{
2527 struct context *ctxt;
2528 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002529 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002530 int match = 0;
2531
2532 if (!rule) {
2533 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002534 "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002535 return -ENOENT;
2536 }
2537
James Morris0804d112008-06-06 18:40:29 +10002538 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002539
2540 if (rule->au_seqno < latest_granting) {
2541 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002542 "selinux_audit_rule_match: stale rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002543 match = -ESTALE;
2544 goto out;
2545 }
2546
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002547 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002548 if (!ctxt) {
2549 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002550 "selinux_audit_rule_match: unrecognized SID %d\n",
2551 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002552 match = -ENOENT;
2553 goto out;
2554 }
2555
2556 /* a field/op pair that is not caught here will simply fall through
2557 without a match */
2558 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002559 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002560 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002561 switch (op) {
2562 case AUDIT_EQUAL:
2563 match = (ctxt->user == rule->au_ctxt.user);
2564 break;
2565 case AUDIT_NOT_EQUAL:
2566 match = (ctxt->user != rule->au_ctxt.user);
2567 break;
2568 }
2569 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002570 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002571 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002572 switch (op) {
2573 case AUDIT_EQUAL:
2574 match = (ctxt->role == rule->au_ctxt.role);
2575 break;
2576 case AUDIT_NOT_EQUAL:
2577 match = (ctxt->role != rule->au_ctxt.role);
2578 break;
2579 }
2580 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002581 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002582 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002583 switch (op) {
2584 case AUDIT_EQUAL:
2585 match = (ctxt->type == rule->au_ctxt.type);
2586 break;
2587 case AUDIT_NOT_EQUAL:
2588 match = (ctxt->type != rule->au_ctxt.type);
2589 break;
2590 }
2591 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002592 case AUDIT_SUBJ_SEN:
2593 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002594 case AUDIT_OBJ_LEV_LOW:
2595 case AUDIT_OBJ_LEV_HIGH:
2596 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04002597 field == AUDIT_OBJ_LEV_LOW) ?
2598 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002599 switch (op) {
2600 case AUDIT_EQUAL:
2601 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002602 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002603 break;
2604 case AUDIT_NOT_EQUAL:
2605 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002606 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002607 break;
2608 case AUDIT_LESS_THAN:
2609 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002610 level) &&
2611 !mls_level_eq(&rule->au_ctxt.range.level[0],
2612 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002613 break;
2614 case AUDIT_LESS_THAN_OR_EQUAL:
2615 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002616 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002617 break;
2618 case AUDIT_GREATER_THAN:
2619 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002620 &rule->au_ctxt.range.level[0]) &&
2621 !mls_level_eq(level,
2622 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002623 break;
2624 case AUDIT_GREATER_THAN_OR_EQUAL:
2625 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002626 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002627 break;
2628 }
2629 }
2630
2631out:
James Morris0804d112008-06-06 18:40:29 +10002632 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002633 return match;
2634}
2635
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002636static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002637
2638static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
Eric Parisf5269712008-05-14 11:27:45 -04002639 u16 class, u32 perms, u32 *retained)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002640{
2641 int err = 0;
2642
2643 if (event == AVC_CALLBACK_RESET && aurule_callback)
2644 err = aurule_callback();
2645 return err;
2646}
2647
2648static int __init aurule_init(void)
2649{
2650 int err;
2651
2652 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
Eric Paris5d55a342008-04-18 17:38:33 -04002653 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002654 if (err)
2655 panic("avc_add_callback() failed, error %d\n", err);
2656
2657 return err;
2658}
2659__initcall(aurule_init);
2660
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002661#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002662/**
Paul Moore5778eab2007-02-28 15:14:22 -05002663 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2664 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002665 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002666 *
2667 * Description:
2668 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05002669 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2670 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002671 *
2672 */
Paul Moore5778eab2007-02-28 15:14:22 -05002673static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002674 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002675{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002676 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002677
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002678 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2679 if (sid_cache == NULL)
2680 return;
Paul Moore5778eab2007-02-28 15:14:22 -05002681 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002682 if (secattr->cache == NULL) {
2683 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05002684 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02002685 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002686
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002687 *sid_cache = sid;
2688 secattr->cache->free = kfree;
2689 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05002690 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002691}
2692
2693/**
Paul Moore5778eab2007-02-28 15:14:22 -05002694 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002695 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002696 * @sid: the SELinux SID
2697 *
2698 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002699 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002700 * SELinux SID. If the @secattr field does not contain a full SELinux
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002701 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2702 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2703 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2704 * conversion for future lookups. Returns zero on success, negative values on
2705 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002706 *
2707 */
Paul Moore5778eab2007-02-28 15:14:22 -05002708int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05002709 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002710{
2711 int rc = -EIDRM;
2712 struct context *ctx;
2713 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05002714
2715 if (!ss_initialized) {
2716 *sid = SECSID_NULL;
2717 return 0;
2718 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002719
James Morris0804d112008-06-06 18:40:29 +10002720 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002721
Paul Moore701a90b2006-11-17 17:38:46 -05002722 if (secattr->flags & NETLBL_SECATTR_CACHE) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002723 *sid = *(u32 *)secattr->cache->data;
2724 rc = 0;
Paul Moore16efd452008-01-29 08:37:59 -05002725 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2726 *sid = secattr->attr.secid;
2727 rc = 0;
Paul Moore701a90b2006-11-17 17:38:46 -05002728 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002729 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002730 if (ctx == NULL)
2731 goto netlbl_secattr_to_sid_return;
2732
2733 ctx_new.user = ctx->user;
2734 ctx_new.role = ctx->role;
2735 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05002736 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05002737 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05002738 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
Paul Moore16efd452008-01-29 08:37:59 -05002739 secattr->attr.mls.cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002740 goto netlbl_secattr_to_sid_return;
2741 ctx_new.range.level[1].cat.highbit =
2742 ctx_new.range.level[0].cat.highbit;
2743 ctx_new.range.level[1].cat.node =
2744 ctx_new.range.level[0].cat.node;
2745 } else {
2746 ebitmap_init(&ctx_new.range.level[0].cat);
2747 ebitmap_init(&ctx_new.range.level[1].cat);
2748 }
2749 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2750 goto netlbl_secattr_to_sid_return_cleanup;
2751
2752 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2753 if (rc != 0)
2754 goto netlbl_secattr_to_sid_return_cleanup;
2755
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002756 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05002757
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002758 ebitmap_destroy(&ctx_new.range.level[0].cat);
2759 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002760 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002761 rc = 0;
2762 }
2763
2764netlbl_secattr_to_sid_return:
James Morris0804d112008-06-06 18:40:29 +10002765 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002766 return rc;
2767netlbl_secattr_to_sid_return_cleanup:
2768 ebitmap_destroy(&ctx_new.range.level[0].cat);
2769 goto netlbl_secattr_to_sid_return;
2770}
2771
2772/**
Paul Moore5778eab2007-02-28 15:14:22 -05002773 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2774 * @sid: the SELinux SID
2775 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002776 *
2777 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002778 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2779 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002780 *
2781 */
Paul Moore5778eab2007-02-28 15:14:22 -05002782int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002783{
2784 int rc = -ENOENT;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002785 struct context *ctx;
2786
2787 if (!ss_initialized)
2788 return 0;
2789
James Morris0804d112008-06-06 18:40:29 +10002790 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002791 ctx = sidtab_search(&sidtab, sid);
2792 if (ctx == NULL)
Paul Moore5778eab2007-02-28 15:14:22 -05002793 goto netlbl_sid_to_secattr_failure;
2794 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2795 GFP_ATOMIC);
Paul Moore00447872008-04-12 19:06:42 -07002796 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY;
Paul Moore5778eab2007-02-28 15:14:22 -05002797 mls_export_netlbl_lvl(ctx, secattr);
2798 rc = mls_export_netlbl_cat(ctx, secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04002799 if (rc != 0)
Paul Moore5778eab2007-02-28 15:14:22 -05002800 goto netlbl_sid_to_secattr_failure;
James Morris0804d112008-06-06 18:40:29 +10002801 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002802
Paul Moore5778eab2007-02-28 15:14:22 -05002803 return 0;
Paul Moore9f2ad662006-11-17 17:38:53 -05002804
Paul Moore5778eab2007-02-28 15:14:22 -05002805netlbl_sid_to_secattr_failure:
James Morris0804d112008-06-06 18:40:29 +10002806 read_unlock(&policy_rwlock);
Paul Mooref8687af2006-10-30 15:22:15 -08002807 return rc;
2808}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002809#endif /* CONFIG_NETLABEL */