blob: 2d5e5a3a8aa923e4af3c47f0b856b54206edf50f [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 Molnarbb003072006-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 Molnarbb003072006-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);
74#define POLICY_RDLOCK read_lock(&policy_rwlock)
75#define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
76#define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
77#define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
78
Ingo Molnarbb003072006-03-22 00:09:14 -080079static DEFINE_MUTEX(load_mutex);
80#define LOAD_LOCK mutex_lock(&load_mutex)
81#define LOAD_UNLOCK mutex_unlock(&load_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83static struct sidtab sidtab;
84struct policydb policydb;
Eric Paris5d55a342008-04-18 17:38:33 -040085int ss_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * The largest sequence number that has been used when
89 * providing an access decision to the access vector cache.
90 * The sequence number only changes when a policy change
91 * occurs.
92 */
Eric Paris5d55a342008-04-18 17:38:33 -040093static u32 latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* Forward declaration. */
96static int context_struct_to_string(struct context *context, char **scontext,
97 u32 *scontext_len);
98
99/*
100 * Return the boolean value of a constraint expression
101 * when it is applied to the specified source and target
102 * security contexts.
103 *
104 * xcontext is a special beast... It is used by the validatetrans rules
105 * only. For these rules, scontext is the context before the transition,
106 * tcontext is the context after the transition, and xcontext is the context
107 * of the process performing the transition. All other callers of
108 * constraint_expr_eval should pass in NULL for xcontext.
109 */
110static int constraint_expr_eval(struct context *scontext,
111 struct context *tcontext,
112 struct context *xcontext,
113 struct constraint_expr *cexpr)
114{
115 u32 val1, val2;
116 struct context *c;
117 struct role_datum *r1, *r2;
118 struct mls_level *l1, *l2;
119 struct constraint_expr *e;
120 int s[CEXPR_MAXDEPTH];
121 int sp = -1;
122
123 for (e = cexpr; e; e = e->next) {
124 switch (e->expr_type) {
125 case CEXPR_NOT:
126 BUG_ON(sp < 0);
127 s[sp] = !s[sp];
128 break;
129 case CEXPR_AND:
130 BUG_ON(sp < 1);
131 sp--;
132 s[sp] &= s[sp+1];
133 break;
134 case CEXPR_OR:
135 BUG_ON(sp < 1);
136 sp--;
137 s[sp] |= s[sp+1];
138 break;
139 case CEXPR_ATTR:
140 if (sp == (CEXPR_MAXDEPTH-1))
141 return 0;
142 switch (e->attr) {
143 case CEXPR_USER:
144 val1 = scontext->user;
145 val2 = tcontext->user;
146 break;
147 case CEXPR_TYPE:
148 val1 = scontext->type;
149 val2 = tcontext->type;
150 break;
151 case CEXPR_ROLE:
152 val1 = scontext->role;
153 val2 = tcontext->role;
154 r1 = policydb.role_val_to_struct[val1 - 1];
155 r2 = policydb.role_val_to_struct[val2 - 1];
156 switch (e->op) {
157 case CEXPR_DOM:
158 s[++sp] = ebitmap_get_bit(&r1->dominates,
159 val2 - 1);
160 continue;
161 case CEXPR_DOMBY:
162 s[++sp] = ebitmap_get_bit(&r2->dominates,
163 val1 - 1);
164 continue;
165 case CEXPR_INCOMP:
Eric Paris5d55a342008-04-18 17:38:33 -0400166 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
167 val2 - 1) &&
168 !ebitmap_get_bit(&r2->dominates,
169 val1 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 continue;
171 default:
172 break;
173 }
174 break;
175 case CEXPR_L1L2:
176 l1 = &(scontext->range.level[0]);
177 l2 = &(tcontext->range.level[0]);
178 goto mls_ops;
179 case CEXPR_L1H2:
180 l1 = &(scontext->range.level[0]);
181 l2 = &(tcontext->range.level[1]);
182 goto mls_ops;
183 case CEXPR_H1L2:
184 l1 = &(scontext->range.level[1]);
185 l2 = &(tcontext->range.level[0]);
186 goto mls_ops;
187 case CEXPR_H1H2:
188 l1 = &(scontext->range.level[1]);
189 l2 = &(tcontext->range.level[1]);
190 goto mls_ops;
191 case CEXPR_L1H1:
192 l1 = &(scontext->range.level[0]);
193 l2 = &(scontext->range.level[1]);
194 goto mls_ops;
195 case CEXPR_L2H2:
196 l1 = &(tcontext->range.level[0]);
197 l2 = &(tcontext->range.level[1]);
198 goto mls_ops;
199mls_ops:
200 switch (e->op) {
201 case CEXPR_EQ:
202 s[++sp] = mls_level_eq(l1, l2);
203 continue;
204 case CEXPR_NEQ:
205 s[++sp] = !mls_level_eq(l1, l2);
206 continue;
207 case CEXPR_DOM:
208 s[++sp] = mls_level_dom(l1, l2);
209 continue;
210 case CEXPR_DOMBY:
211 s[++sp] = mls_level_dom(l2, l1);
212 continue;
213 case CEXPR_INCOMP:
214 s[++sp] = mls_level_incomp(l2, l1);
215 continue;
216 default:
217 BUG();
218 return 0;
219 }
220 break;
221 default:
222 BUG();
223 return 0;
224 }
225
226 switch (e->op) {
227 case CEXPR_EQ:
228 s[++sp] = (val1 == val2);
229 break;
230 case CEXPR_NEQ:
231 s[++sp] = (val1 != val2);
232 break;
233 default:
234 BUG();
235 return 0;
236 }
237 break;
238 case CEXPR_NAMES:
239 if (sp == (CEXPR_MAXDEPTH-1))
240 return 0;
241 c = scontext;
242 if (e->attr & CEXPR_TARGET)
243 c = tcontext;
244 else if (e->attr & CEXPR_XTARGET) {
245 c = xcontext;
246 if (!c) {
247 BUG();
248 return 0;
249 }
250 }
251 if (e->attr & CEXPR_USER)
252 val1 = c->user;
253 else if (e->attr & CEXPR_ROLE)
254 val1 = c->role;
255 else if (e->attr & CEXPR_TYPE)
256 val1 = c->type;
257 else {
258 BUG();
259 return 0;
260 }
261
262 switch (e->op) {
263 case CEXPR_EQ:
264 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
265 break;
266 case CEXPR_NEQ:
267 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
268 break;
269 default:
270 BUG();
271 return 0;
272 }
273 break;
274 default:
275 BUG();
276 return 0;
277 }
278 }
279
280 BUG_ON(sp != 0);
281 return s[0];
282}
283
284/*
285 * Compute access vectors based on a context structure pair for
286 * the permissions in a particular class.
287 */
288static int context_struct_compute_av(struct context *scontext,
289 struct context *tcontext,
290 u16 tclass,
291 u32 requested,
292 struct av_decision *avd)
293{
294 struct constraint_node *constraint;
295 struct role_allow *ra;
296 struct avtab_key avkey;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700297 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct class_datum *tclass_datum;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700299 struct ebitmap *sattr, *tattr;
300 struct ebitmap_node *snode, *tnode;
Eric Paris3f120702007-09-21 14:37:10 -0400301 const struct selinux_class_perm *kdefs = &selinux_class_perm;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700302 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /*
305 * Remap extended Netlink classes for old policy versions.
306 * Do this here rather than socket_type_to_security_class()
307 * in case a newer policy version is loaded, allowing sockets
308 * to remain in the correct class.
309 */
310 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
311 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
312 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
313 tclass = SECCLASS_NETLINK_SOCKET;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
316 * Initialize the access vectors to the default values.
317 */
318 avd->allowed = 0;
319 avd->decided = 0xffffffff;
320 avd->auditallow = 0;
321 avd->auditdeny = 0xffffffff;
322 avd->seqno = latest_granting;
323
324 /*
Eric Paris3f120702007-09-21 14:37:10 -0400325 * Check for all the invalid cases.
326 * - tclass 0
327 * - tclass > policy and > kernel
328 * - tclass > policy but is a userspace class
329 * - tclass > policy but we do not allow unknowns
330 */
331 if (unlikely(!tclass))
332 goto inval_class;
333 if (unlikely(tclass > policydb.p_classes.nprim))
334 if (tclass > kdefs->cts_len ||
335 !kdefs->class_to_string[tclass - 1] ||
336 !policydb.allow_unknown)
337 goto inval_class;
338
339 /*
340 * Kernel class and we allow unknown so pad the allow decision
341 * the pad will be all 1 for unknown classes.
342 */
343 if (tclass <= kdefs->cts_len && policydb.allow_unknown)
344 avd->allowed = policydb.undefined_perms[tclass - 1];
345
346 /*
347 * Not in policy. Since decision is completed (all 1 or all 0) return.
348 */
349 if (unlikely(tclass > policydb.p_classes.nprim))
350 return 0;
351
352 tclass_datum = policydb.class_val_to_struct[tclass - 1];
353
354 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * If a specific type enforcement rule was defined for
356 * this permission check, then use it.
357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700359 avkey.specified = AVTAB_AV;
360 sattr = &policydb.type_attr_map[scontext->type - 1];
361 tattr = &policydb.type_attr_map[tcontext->type - 1];
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900362 ebitmap_for_each_positive_bit(sattr, snode, i) {
363 ebitmap_for_each_positive_bit(tattr, tnode, j) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700364 avkey.source_type = i + 1;
365 avkey.target_type = j + 1;
366 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
367 node != NULL;
368 node = avtab_search_node_next(node, avkey.specified)) {
369 if (node->key.specified == AVTAB_ALLOWED)
370 avd->allowed |= node->datum.data;
371 else if (node->key.specified == AVTAB_AUDITALLOW)
372 avd->auditallow |= node->datum.data;
373 else if (node->key.specified == AVTAB_AUDITDENY)
374 avd->auditdeny &= node->datum.data;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Stephen Smalley782ebb92005-09-03 15:55:16 -0700377 /* Check conditional av table for additional permissions */
378 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
379
380 }
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /*
384 * Remove any permissions prohibited by a constraint (this includes
385 * the MLS policy).
386 */
387 constraint = tclass_datum->constraints;
388 while (constraint) {
389 if ((constraint->permissions & (avd->allowed)) &&
390 !constraint_expr_eval(scontext, tcontext, NULL,
391 constraint->expr)) {
392 avd->allowed = (avd->allowed) & ~(constraint->permissions);
393 }
394 constraint = constraint->next;
395 }
396
397 /*
398 * If checking process transition permission and the
399 * role is changing, then check the (current_role, new_role)
400 * pair.
401 */
402 if (tclass == SECCLASS_PROCESS &&
403 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
404 scontext->role != tcontext->role) {
405 for (ra = policydb.role_allow; ra; ra = ra->next) {
406 if (scontext->role == ra->role &&
407 tcontext->role == ra->new_role)
408 break;
409 }
410 if (!ra)
411 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
Eric Paris5d55a342008-04-18 17:38:33 -0400412 PROCESS__DYNTRANSITION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 return 0;
Eric Paris3f120702007-09-21 14:37:10 -0400416
417inval_class:
Eric Paris744ba352008-04-17 11:52:44 -0400418 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n", __func__,
419 tclass);
Eric Paris3f120702007-09-21 14:37:10 -0400420 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Eric Paris64dbf072008-03-31 12:17:33 +1100423/*
424 * Given a sid find if the type has the permissive flag set
425 */
426int security_permissive_sid(u32 sid)
427{
428 struct context *context;
429 u32 type;
430 int rc;
431
432 POLICY_RDLOCK;
433
434 context = sidtab_search(&sidtab, sid);
435 BUG_ON(!context);
436
437 type = context->type;
438 /*
439 * we are intentionally using type here, not type-1, the 0th bit may
440 * someday indicate that we are globally setting permissive in policy.
441 */
442 rc = ebitmap_get_bit(&policydb.permissive_map, type);
443
444 POLICY_RDUNLOCK;
445 return rc;
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400449 struct context *ncontext,
450 struct context *tcontext,
451 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 char *o = NULL, *n = NULL, *t = NULL;
454 u32 olen, nlen, tlen;
455
456 if (context_struct_to_string(ocontext, &o, &olen) < 0)
457 goto out;
458 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
459 goto out;
460 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
461 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100462 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -0400463 "security_validate_transition: denied for"
464 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
465 o, n, t, policydb.p_class_val_to_name[tclass-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466out:
467 kfree(o);
468 kfree(n);
469 kfree(t);
470
471 if (!selinux_enforcing)
472 return 0;
473 return -EPERM;
474}
475
476int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
Eric Paris5d55a342008-04-18 17:38:33 -0400477 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct context *ocontext;
480 struct context *ncontext;
481 struct context *tcontext;
482 struct class_datum *tclass_datum;
483 struct constraint_node *constraint;
484 int rc = 0;
485
486 if (!ss_initialized)
487 return 0;
488
489 POLICY_RDLOCK;
490
491 /*
492 * Remap extended Netlink classes for old policy versions.
493 * Do this here rather than socket_type_to_security_class()
494 * in case a newer policy version is loaded, allowing sockets
495 * to remain in the correct class.
496 */
497 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
498 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
499 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
500 tclass = SECCLASS_NETLINK_SOCKET;
501
502 if (!tclass || tclass > policydb.p_classes.nprim) {
Eric Paris744ba352008-04-17 11:52:44 -0400503 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
504 __func__, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 rc = -EINVAL;
506 goto out;
507 }
508 tclass_datum = policydb.class_val_to_struct[tclass - 1];
509
510 ocontext = sidtab_search(&sidtab, oldsid);
511 if (!ocontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400512 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
513 __func__, oldsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 rc = -EINVAL;
515 goto out;
516 }
517
518 ncontext = sidtab_search(&sidtab, newsid);
519 if (!ncontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400520 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
521 __func__, newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 rc = -EINVAL;
523 goto out;
524 }
525
526 tcontext = sidtab_search(&sidtab, tasksid);
527 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400528 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
529 __func__, tasksid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 rc = -EINVAL;
531 goto out;
532 }
533
534 constraint = tclass_datum->validatetrans;
535 while (constraint) {
536 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400537 constraint->expr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 rc = security_validtrans_handle_fail(ocontext, ncontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400539 tcontext, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto out;
541 }
542 constraint = constraint->next;
543 }
544
545out:
546 POLICY_RDUNLOCK;
547 return rc;
548}
549
550/**
551 * security_compute_av - Compute access vector decisions.
552 * @ssid: source security identifier
553 * @tsid: target security identifier
554 * @tclass: target security class
555 * @requested: requested permissions
556 * @avd: access vector decisions
557 *
558 * Compute a set of access vector decisions based on the
559 * SID pair (@ssid, @tsid) for the permissions in @tclass.
560 * Return -%EINVAL if any of the parameters are invalid or %0
561 * if the access vector decisions were computed successfully.
562 */
563int security_compute_av(u32 ssid,
564 u32 tsid,
565 u16 tclass,
566 u32 requested,
567 struct av_decision *avd)
568{
569 struct context *scontext = NULL, *tcontext = NULL;
570 int rc = 0;
571
572 if (!ss_initialized) {
Stephen Smalley4c443d12005-05-16 21:53:52 -0700573 avd->allowed = 0xffffffff;
574 avd->decided = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 avd->auditallow = 0;
576 avd->auditdeny = 0xffffffff;
577 avd->seqno = latest_granting;
578 return 0;
579 }
580
581 POLICY_RDLOCK;
582
583 scontext = sidtab_search(&sidtab, ssid);
584 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400585 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
586 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 rc = -EINVAL;
588 goto out;
589 }
590 tcontext = sidtab_search(&sidtab, tsid);
591 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400592 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
593 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 rc = -EINVAL;
595 goto out;
596 }
597
598 rc = context_struct_compute_av(scontext, tcontext, tclass,
599 requested, avd);
600out:
601 POLICY_RDUNLOCK;
602 return rc;
603}
604
605/*
606 * Write the security context string representation of
607 * the context structure `context' into a dynamically
608 * allocated string of the correct size. Set `*scontext'
609 * to point to this string and set `*scontext_len' to
610 * the length of the string.
611 */
612static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
613{
614 char *scontextp;
615
616 *scontext = NULL;
617 *scontext_len = 0;
618
Stephen Smalley12b29f32008-05-07 13:03:20 -0400619 if (context->len) {
620 *scontext_len = context->len;
621 *scontext = kstrdup(context->str, GFP_ATOMIC);
622 if (!(*scontext))
623 return -ENOMEM;
624 return 0;
625 }
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* Compute the size of the context. */
628 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
629 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
630 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
631 *scontext_len += mls_compute_context_len(context);
632
633 /* Allocate space for the context; caller must free this space. */
634 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -0400635 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 *scontext = scontextp;
638
639 /*
640 * Copy the user name, role name and type name into the context.
641 */
642 sprintf(scontextp, "%s:%s:%s",
643 policydb.p_user_val_to_name[context->user - 1],
644 policydb.p_role_val_to_name[context->role - 1],
645 policydb.p_type_val_to_name[context->type - 1]);
646 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
Eric Paris5d55a342008-04-18 17:38:33 -0400647 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
648 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 mls_sid_to_context(context, &scontextp);
651
652 *scontextp = 0;
653
654 return 0;
655}
656
657#include "initial_sid_to_string.h"
658
James Carterf0ee2e42007-04-04 10:11:29 -0400659const char *security_get_initial_sid_context(u32 sid)
660{
661 if (unlikely(sid > SECINITSID_NUM))
662 return NULL;
663 return initial_sid_to_string[sid];
664}
665
Stephen Smalley12b29f32008-05-07 13:03:20 -0400666static int security_sid_to_context_core(u32 sid, char **scontext,
667 u32 *scontext_len, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 struct context *context;
670 int rc = 0;
671
Stephen Smalley4f4acf32007-02-26 12:02:34 -0500672 *scontext = NULL;
673 *scontext_len = 0;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (!ss_initialized) {
676 if (sid <= SECINITSID_NUM) {
677 char *scontextp;
678
679 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -0400680 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca02006-05-15 09:43:48 -0700681 if (!scontextp) {
682 rc = -ENOMEM;
683 goto out;
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 strcpy(scontextp, initial_sid_to_string[sid]);
686 *scontext = scontextp;
687 goto out;
688 }
Eric Paris744ba352008-04-17 11:52:44 -0400689 printk(KERN_ERR "SELinux: %s: called before initial "
690 "load_policy on unknown SID %d\n", __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 rc = -EINVAL;
692 goto out;
693 }
694 POLICY_RDLOCK;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400695 if (force)
696 context = sidtab_search_force(&sidtab, sid);
697 else
698 context = sidtab_search(&sidtab, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (!context) {
Eric Paris744ba352008-04-17 11:52:44 -0400700 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
701 __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 rc = -EINVAL;
703 goto out_unlock;
704 }
705 rc = context_struct_to_string(context, scontext, scontext_len);
706out_unlock:
707 POLICY_RDUNLOCK;
708out:
709 return rc;
710
711}
712
Stephen Smalley12b29f32008-05-07 13:03:20 -0400713/**
714 * security_sid_to_context - Obtain a context for a given SID.
715 * @sid: security identifier, SID
716 * @scontext: security context
717 * @scontext_len: length in bytes
718 *
719 * Write the string representation of the context associated with @sid
720 * into a dynamically allocated string of the correct size. Set @scontext
721 * to point to this string and set @scontext_len to the length of the string.
722 */
723int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Stephen Smalley12b29f32008-05-07 13:03:20 -0400725 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
726}
727
728int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
729{
730 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
731}
732
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400733/*
734 * Caveat: Mutates scontext.
735 */
Stephen Smalley12b29f32008-05-07 13:03:20 -0400736static int string_to_context_struct(struct policydb *pol,
737 struct sidtab *sidtabp,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400738 char *scontext,
Stephen Smalley12b29f32008-05-07 13:03:20 -0400739 u32 scontext_len,
740 struct context *ctx,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400741 u32 def_sid)
Stephen Smalley12b29f32008-05-07 13:03:20 -0400742{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct role_datum *role;
744 struct type_datum *typdatum;
745 struct user_datum *usrdatum;
746 char *scontextp, *p, oldc;
747 int rc = 0;
748
Stephen Smalley12b29f32008-05-07 13:03:20 -0400749 context_init(ctx);
750
Stephen Smalley12b29f32008-05-07 13:03:20 -0400751 /* Parse the security context. */
752
753 rc = -EINVAL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400754 scontextp = (char *) scontext;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400755
756 /* Extract the user. */
757 p = scontextp;
758 while (*p && *p != ':')
759 p++;
760
761 if (*p == 0)
762 goto out;
763
764 *p++ = 0;
765
766 usrdatum = hashtab_search(pol->p_users.table, scontextp);
767 if (!usrdatum)
768 goto out;
769
770 ctx->user = usrdatum->value;
771
772 /* Extract role. */
773 scontextp = p;
774 while (*p && *p != ':')
775 p++;
776
777 if (*p == 0)
778 goto out;
779
780 *p++ = 0;
781
782 role = hashtab_search(pol->p_roles.table, scontextp);
783 if (!role)
784 goto out;
785 ctx->role = role->value;
786
787 /* Extract type. */
788 scontextp = p;
789 while (*p && *p != ':')
790 p++;
791 oldc = *p;
792 *p++ = 0;
793
794 typdatum = hashtab_search(pol->p_types.table, scontextp);
795 if (!typdatum)
796 goto out;
797
798 ctx->type = typdatum->value;
799
800 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
801 if (rc)
802 goto out;
803
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400804 if ((p - scontext) < scontext_len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -0400805 rc = -EINVAL;
806 goto out;
807 }
808
809 /* Check the validity of the new context. */
810 if (!policydb_context_isvalid(pol, ctx)) {
811 rc = -EINVAL;
812 context_destroy(ctx);
813 goto out;
814 }
815 rc = 0;
816out:
Stephen Smalley12b29f32008-05-07 13:03:20 -0400817 return rc;
818}
819
820static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
821 u32 *sid, u32 def_sid, gfp_t gfp_flags,
822 int force)
823{
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400824 char *scontext2, *str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400825 struct context context;
826 int rc = 0;
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (!ss_initialized) {
829 int i;
830
831 for (i = 1; i < SECINITSID_NUM; i++) {
832 if (!strcmp(initial_sid_to_string[i], scontext)) {
833 *sid = i;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 }
837 *sid = SECINITSID_KERNEL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 *sid = SECSID_NULL;
841
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400842 /* Copy the string so that we can modify the copy as we parse it. */
843 scontext2 = kmalloc(scontext_len+1, gfp_flags);
844 if (!scontext2)
845 return -ENOMEM;
846 memcpy(scontext2, scontext, scontext_len);
847 scontext2[scontext_len] = 0;
848
849 if (force) {
850 /* Save another copy for storing in uninterpreted form */
851 str = kstrdup(scontext2, gfp_flags);
852 if (!str) {
853 kfree(scontext2);
854 return -ENOMEM;
855 }
856 }
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 POLICY_RDLOCK;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400859 rc = string_to_context_struct(&policydb, &sidtab,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400860 scontext2, scontext_len,
861 &context, def_sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400862 if (rc == -EINVAL && force) {
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400863 context.str = str;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400864 context.len = scontext_len;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400865 str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400866 } else if (rc)
867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 rc = sidtab_context_to_sid(&sidtab, &context, sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400869 if (rc)
870 context_destroy(&context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871out:
Stephen Smalley12b29f32008-05-07 13:03:20 -0400872 POLICY_RDUNLOCK;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400873 kfree(scontext2);
874 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return rc;
876}
877
James Morrisf5c1d5b2005-07-28 01:07:37 -0700878/**
879 * security_context_to_sid - Obtain a SID for a given security context.
880 * @scontext: security context
881 * @scontext_len: length in bytes
882 * @sid: security identifier, SID
883 *
884 * Obtains a SID associated with the security context that
885 * has the string representation specified by @scontext.
886 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
887 * memory is available, or 0 on success.
888 */
David Howells8f0cfa52008-04-29 00:59:41 -0700889int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
James Morrisf5c1d5b2005-07-28 01:07:37 -0700890{
891 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -0400892 sid, SECSID_NULL, GFP_KERNEL, 0);
James Morrisf5c1d5b2005-07-28 01:07:37 -0700893}
894
895/**
896 * security_context_to_sid_default - Obtain a SID for a given security context,
897 * falling back to specified default if needed.
898 *
899 * @scontext: security context
900 * @scontext_len: length in bytes
901 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -0700902 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -0700903 *
904 * Obtains a SID associated with the security context that
905 * has the string representation specified by @scontext.
906 * The default SID is passed to the MLS layer to be used to allow
907 * kernel labeling of the MLS field if the MLS field is not present
908 * (for upgrading to MLS without full relabel).
Stephen Smalley12b29f32008-05-07 13:03:20 -0400909 * Implicitly forces adding of the context even if it cannot be mapped yet.
James Morrisf5c1d5b2005-07-28 01:07:37 -0700910 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
911 * memory is available, or 0 on success.
912 */
David Howells7bf570d2008-04-29 20:52:51 +0100913int security_context_to_sid_default(const char *scontext, u32 scontext_len,
914 u32 *sid, u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -0700915{
916 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -0400917 sid, def_sid, gfp_flags, 1);
918}
919
920int security_context_to_sid_force(const char *scontext, u32 scontext_len,
921 u32 *sid)
922{
923 return security_context_to_sid_core(scontext, scontext_len,
924 sid, SECSID_NULL, GFP_KERNEL, 1);
James Morrisf5c1d5b2005-07-28 01:07:37 -0700925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927static int compute_sid_handle_invalid_context(
928 struct context *scontext,
929 struct context *tcontext,
930 u16 tclass,
931 struct context *newcontext)
932{
933 char *s = NULL, *t = NULL, *n = NULL;
934 u32 slen, tlen, nlen;
935
936 if (context_struct_to_string(scontext, &s, &slen) < 0)
937 goto out;
938 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
939 goto out;
940 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
941 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100942 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 "security_compute_sid: invalid context %s"
944 " for scontext=%s"
945 " tcontext=%s"
946 " tclass=%s",
947 n, s, t, policydb.p_class_val_to_name[tclass-1]);
948out:
949 kfree(s);
950 kfree(t);
951 kfree(n);
952 if (!selinux_enforcing)
953 return 0;
954 return -EACCES;
955}
956
957static int security_compute_sid(u32 ssid,
958 u32 tsid,
959 u16 tclass,
960 u32 specified,
961 u32 *out_sid)
962{
963 struct context *scontext = NULL, *tcontext = NULL, newcontext;
964 struct role_trans *roletr = NULL;
965 struct avtab_key avkey;
966 struct avtab_datum *avdatum;
967 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 int rc = 0;
969
970 if (!ss_initialized) {
971 switch (tclass) {
972 case SECCLASS_PROCESS:
973 *out_sid = ssid;
974 break;
975 default:
976 *out_sid = tsid;
977 break;
978 }
979 goto out;
980 }
981
Venkat Yekkirala851f8a62006-07-30 03:03:18 -0700982 context_init(&newcontext);
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 POLICY_RDLOCK;
985
986 scontext = sidtab_search(&sidtab, ssid);
987 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400988 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
989 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 rc = -EINVAL;
991 goto out_unlock;
992 }
993 tcontext = sidtab_search(&sidtab, tsid);
994 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400995 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
996 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 rc = -EINVAL;
998 goto out_unlock;
999 }
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /* Set the user identity. */
1002 switch (specified) {
1003 case AVTAB_TRANSITION:
1004 case AVTAB_CHANGE:
1005 /* Use the process user identity. */
1006 newcontext.user = scontext->user;
1007 break;
1008 case AVTAB_MEMBER:
1009 /* Use the related object owner. */
1010 newcontext.user = tcontext->user;
1011 break;
1012 }
1013
1014 /* Set the role and type to default values. */
1015 switch (tclass) {
1016 case SECCLASS_PROCESS:
1017 /* Use the current role and type of process. */
1018 newcontext.role = scontext->role;
1019 newcontext.type = scontext->type;
1020 break;
1021 default:
1022 /* Use the well-defined object role. */
1023 newcontext.role = OBJECT_R_VAL;
1024 /* Use the type of the related object. */
1025 newcontext.type = tcontext->type;
1026 }
1027
1028 /* Look for a type transition/member/change rule. */
1029 avkey.source_type = scontext->type;
1030 avkey.target_type = tcontext->type;
1031 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001032 avkey.specified = specified;
1033 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -04001036 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001037 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001039 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 avdatum = &node->datum;
1041 break;
1042 }
1043 }
1044 }
1045
Stephen Smalley782ebb92005-09-03 15:55:16 -07001046 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -07001048 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050
1051 /* Check for class-specific changes. */
1052 switch (tclass) {
1053 case SECCLASS_PROCESS:
1054 if (specified & AVTAB_TRANSITION) {
1055 /* Look for a role transition rule. */
1056 for (roletr = policydb.role_tr; roletr;
1057 roletr = roletr->next) {
1058 if (roletr->role == scontext->role &&
1059 roletr->type == tcontext->type) {
1060 /* Use the role transition rule. */
1061 newcontext.role = roletr->new_role;
1062 break;
1063 }
1064 }
1065 }
1066 break;
1067 default:
1068 break;
1069 }
1070
1071 /* Set the MLS attributes.
1072 This is done last because it may allocate memory. */
1073 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1074 if (rc)
1075 goto out_unlock;
1076
1077 /* Check the validity of the context. */
1078 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1079 rc = compute_sid_handle_invalid_context(scontext,
1080 tcontext,
1081 tclass,
1082 &newcontext);
1083 if (rc)
1084 goto out_unlock;
1085 }
1086 /* Obtain the sid for the context. */
1087 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1088out_unlock:
1089 POLICY_RDUNLOCK;
1090 context_destroy(&newcontext);
1091out:
1092 return rc;
1093}
1094
1095/**
1096 * security_transition_sid - Compute the SID for a new subject/object.
1097 * @ssid: source security identifier
1098 * @tsid: target security identifier
1099 * @tclass: target security class
1100 * @out_sid: security identifier for new subject/object
1101 *
1102 * Compute a SID to use for labeling a new subject or object in the
1103 * class @tclass based on a SID pair (@ssid, @tsid).
1104 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1105 * if insufficient memory is available, or %0 if the new SID was
1106 * computed successfully.
1107 */
1108int security_transition_sid(u32 ssid,
1109 u32 tsid,
1110 u16 tclass,
1111 u32 *out_sid)
1112{
1113 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1114}
1115
1116/**
1117 * security_member_sid - Compute the SID for member selection.
1118 * @ssid: source security identifier
1119 * @tsid: target security identifier
1120 * @tclass: target security class
1121 * @out_sid: security identifier for selected member
1122 *
1123 * Compute a SID to use when selecting a member of a polyinstantiated
1124 * object of class @tclass based on a SID pair (@ssid, @tsid).
1125 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1126 * if insufficient memory is available, or %0 if the SID was
1127 * computed successfully.
1128 */
1129int security_member_sid(u32 ssid,
1130 u32 tsid,
1131 u16 tclass,
1132 u32 *out_sid)
1133{
1134 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1135}
1136
1137/**
1138 * security_change_sid - Compute the SID for object relabeling.
1139 * @ssid: source security identifier
1140 * @tsid: target security identifier
1141 * @tclass: target security class
1142 * @out_sid: security identifier for selected member
1143 *
1144 * Compute a SID to use for relabeling an object of class @tclass
1145 * based on a SID pair (@ssid, @tsid).
1146 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1147 * if insufficient memory is available, or %0 if the SID was
1148 * computed successfully.
1149 */
1150int security_change_sid(u32 ssid,
1151 u32 tsid,
1152 u16 tclass,
1153 u32 *out_sid)
1154{
1155 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1156}
1157
Chad Sellersb94c7e62006-11-06 12:38:18 -05001158/*
1159 * Verify that each kernel class that is defined in the
1160 * policy is correct
1161 */
1162static int validate_classes(struct policydb *p)
1163{
1164 int i, j;
1165 struct class_datum *cladatum;
1166 struct perm_datum *perdatum;
1167 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1168 u16 class_val;
1169 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1170 const char *def_class, *def_perm, *pol_class;
1171 struct symtab *perms;
1172
Eric Paris3f120702007-09-21 14:37:10 -04001173 if (p->allow_unknown) {
1174 u32 num_classes = kdefs->cts_len;
1175 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1176 if (!p->undefined_perms)
1177 return -ENOMEM;
1178 }
1179
Chad Sellersb94c7e62006-11-06 12:38:18 -05001180 for (i = 1; i < kdefs->cts_len; i++) {
1181 def_class = kdefs->class_to_string[i];
Stephen Smalleya764ae42007-03-26 13:36:26 -04001182 if (!def_class)
1183 continue;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001184 if (i > p->p_classes.nprim) {
1185 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001186 "SELinux: class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001187 def_class);
Eric Paris3f120702007-09-21 14:37:10 -04001188 if (p->reject_unknown)
1189 return -EINVAL;
1190 if (p->allow_unknown)
1191 p->undefined_perms[i-1] = ~0U;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001192 continue;
1193 }
1194 pol_class = p->p_class_val_to_name[i-1];
1195 if (strcmp(pol_class, def_class)) {
1196 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001197 "SELinux: class %d is incorrect, found %s but should be %s\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001198 i, pol_class, def_class);
1199 return -EINVAL;
1200 }
1201 }
1202 for (i = 0; i < kdefs->av_pts_len; i++) {
1203 class_val = kdefs->av_perm_to_string[i].tclass;
1204 perm_val = kdefs->av_perm_to_string[i].value;
1205 def_perm = kdefs->av_perm_to_string[i].name;
1206 if (class_val > p->p_classes.nprim)
1207 continue;
1208 pol_class = p->p_class_val_to_name[class_val-1];
1209 cladatum = hashtab_search(p->p_classes.table, pol_class);
1210 BUG_ON(!cladatum);
1211 perms = &cladatum->permissions;
1212 nprim = 1 << (perms->nprim - 1);
1213 if (perm_val > nprim) {
1214 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001215 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001216 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001217 if (p->reject_unknown)
1218 return -EINVAL;
1219 if (p->allow_unknown)
1220 p->undefined_perms[class_val-1] |= perm_val;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001221 continue;
1222 }
1223 perdatum = hashtab_search(perms->table, def_perm);
1224 if (perdatum == NULL) {
1225 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001226 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001227 def_perm, pol_class);
1228 return -EINVAL;
1229 }
1230 pol_val = 1 << (perdatum->value - 1);
1231 if (pol_val != perm_val) {
1232 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001233 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001234 def_perm, pol_class);
1235 return -EINVAL;
1236 }
1237 }
1238 for (i = 0; i < kdefs->av_inherit_len; i++) {
1239 class_val = kdefs->av_inherit[i].tclass;
1240 if (class_val > p->p_classes.nprim)
1241 continue;
1242 pol_class = p->p_class_val_to_name[class_val-1];
1243 cladatum = hashtab_search(p->p_classes.table, pol_class);
1244 BUG_ON(!cladatum);
1245 if (!cladatum->comdatum) {
1246 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001247 "SELinux: class %s should have an inherits clause but does not\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001248 pol_class);
1249 return -EINVAL;
1250 }
1251 tmp = kdefs->av_inherit[i].common_base;
1252 common_pts_len = 0;
1253 while (!(tmp & 0x01)) {
1254 common_pts_len++;
1255 tmp >>= 1;
1256 }
1257 perms = &cladatum->comdatum->permissions;
1258 for (j = 0; j < common_pts_len; j++) {
1259 def_perm = kdefs->av_inherit[i].common_pts[j];
1260 if (j >= perms->nprim) {
1261 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001262 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001263 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001264 if (p->reject_unknown)
1265 return -EINVAL;
1266 if (p->allow_unknown)
1267 p->undefined_perms[class_val-1] |= (1 << j);
Chad Sellersb94c7e62006-11-06 12:38:18 -05001268 continue;
1269 }
1270 perdatum = hashtab_search(perms->table, def_perm);
1271 if (perdatum == NULL) {
1272 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001273 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001274 def_perm, pol_class);
1275 return -EINVAL;
1276 }
1277 if (perdatum->value != j + 1) {
1278 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001279 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001280 def_perm, pol_class);
1281 return -EINVAL;
1282 }
1283 }
1284 }
1285 return 0;
1286}
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288/* Clone the SID into the new SID table. */
1289static int clone_sid(u32 sid,
1290 struct context *context,
1291 void *arg)
1292{
1293 struct sidtab *s = arg;
1294
1295 return sidtab_insert(s, sid, context);
1296}
1297
1298static inline int convert_context_handle_invalid_context(struct context *context)
1299{
1300 int rc = 0;
1301
1302 if (selinux_enforcing) {
1303 rc = -EINVAL;
1304 } else {
1305 char *s;
1306 u32 len;
1307
Stephen Smalley12b29f32008-05-07 13:03:20 -04001308 if (!context_struct_to_string(context, &s, &len)) {
1309 printk(KERN_WARNING
1310 "SELinux: Context %s would be invalid if enforcing\n",
1311 s);
1312 kfree(s);
1313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315 return rc;
1316}
1317
1318struct convert_context_args {
1319 struct policydb *oldp;
1320 struct policydb *newp;
1321};
1322
1323/*
1324 * Convert the values in the security context
1325 * structure `c' from the values specified
1326 * in the policy `p->oldp' to the values specified
1327 * in the policy `p->newp'. Verify that the
1328 * context is valid under the new policy.
1329 */
1330static int convert_context(u32 key,
1331 struct context *c,
1332 void *p)
1333{
1334 struct convert_context_args *args;
1335 struct context oldc;
1336 struct role_datum *role;
1337 struct type_datum *typdatum;
1338 struct user_datum *usrdatum;
1339 char *s;
1340 u32 len;
1341 int rc;
1342
1343 args = p;
1344
Stephen Smalley12b29f32008-05-07 13:03:20 -04001345 if (c->str) {
1346 struct context ctx;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001347 s = kstrdup(c->str, GFP_KERNEL);
1348 if (!s) {
1349 rc = -ENOMEM;
1350 goto out;
1351 }
1352 rc = string_to_context_struct(args->newp, NULL, s,
1353 c->len, &ctx, SECSID_NULL);
1354 kfree(s);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001355 if (!rc) {
1356 printk(KERN_INFO
1357 "SELinux: Context %s became valid (mapped).\n",
1358 c->str);
1359 /* Replace string with mapped representation. */
1360 kfree(c->str);
1361 memcpy(c, &ctx, sizeof(*c));
1362 goto out;
1363 } else if (rc == -EINVAL) {
1364 /* Retain string representation for later mapping. */
1365 rc = 0;
1366 goto out;
1367 } else {
1368 /* Other error condition, e.g. ENOMEM. */
1369 printk(KERN_ERR
1370 "SELinux: Unable to map context %s, rc = %d.\n",
1371 c->str, -rc);
1372 goto out;
1373 }
1374 }
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 rc = context_cpy(&oldc, c);
1377 if (rc)
1378 goto out;
1379
1380 rc = -EINVAL;
1381
1382 /* Convert the user. */
1383 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001384 args->oldp->p_user_val_to_name[c->user - 1]);
1385 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 c->user = usrdatum->value;
1388
1389 /* Convert the role. */
1390 role = hashtab_search(args->newp->p_roles.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001391 args->oldp->p_role_val_to_name[c->role - 1]);
1392 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 c->role = role->value;
1395
1396 /* Convert the type. */
1397 typdatum = hashtab_search(args->newp->p_types.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001398 args->oldp->p_type_val_to_name[c->type - 1]);
1399 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 c->type = typdatum->value;
1402
1403 rc = mls_convert_context(args->oldp, args->newp, c);
1404 if (rc)
1405 goto bad;
1406
1407 /* Check the validity of the new context. */
1408 if (!policydb_context_isvalid(args->newp, c)) {
1409 rc = convert_context_handle_invalid_context(&oldc);
1410 if (rc)
1411 goto bad;
1412 }
1413
1414 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001415 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416out:
1417 return rc;
1418bad:
Stephen Smalley12b29f32008-05-07 13:03:20 -04001419 /* Map old representation to string and save it. */
1420 if (context_struct_to_string(&oldc, &s, &len))
1421 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001423 context_destroy(c);
1424 c->str = s;
1425 c->len = len;
1426 printk(KERN_INFO
1427 "SELinux: Context %s became invalid (unmapped).\n",
1428 c->str);
1429 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 goto out;
1431}
1432
Paul Moore3bb56b22008-01-29 08:38:19 -05001433static void security_load_policycaps(void)
1434{
1435 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1436 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05001437 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1438 POLICYDB_CAPABILITY_OPENPERM);
Paul Moore3bb56b22008-01-29 08:38:19 -05001439}
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441extern void selinux_complete_init(void);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001442static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444/**
1445 * security_load_policy - Load a security policy configuration.
1446 * @data: binary policy data
1447 * @len: length of data in bytes
1448 *
1449 * Load a new set of security policy configuration data,
1450 * validate it and convert the SID table as necessary.
1451 * This function will flush the access vector cache after
1452 * loading the new policy.
1453 */
1454int security_load_policy(void *data, size_t len)
1455{
1456 struct policydb oldpolicydb, newpolicydb;
1457 struct sidtab oldsidtab, newsidtab;
1458 struct convert_context_args args;
1459 u32 seqno;
1460 int rc = 0;
1461 struct policy_file file = { data, len }, *fp = &file;
1462
1463 LOAD_LOCK;
1464
1465 if (!ss_initialized) {
1466 avtab_cache_init();
1467 if (policydb_read(&policydb, fp)) {
1468 LOAD_UNLOCK;
1469 avtab_cache_destroy();
1470 return -EINVAL;
1471 }
1472 if (policydb_load_isids(&policydb, &sidtab)) {
1473 LOAD_UNLOCK;
1474 policydb_destroy(&policydb);
1475 avtab_cache_destroy();
1476 return -EINVAL;
1477 }
Chad Sellersb94c7e62006-11-06 12:38:18 -05001478 /* Verify that the kernel defined classes are correct. */
1479 if (validate_classes(&policydb)) {
1480 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001481 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001482 LOAD_UNLOCK;
1483 sidtab_destroy(&sidtab);
1484 policydb_destroy(&policydb);
1485 avtab_cache_destroy();
1486 return -EINVAL;
1487 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001488 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 policydb_loaded_version = policydb.policyvers;
1490 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001491 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 LOAD_UNLOCK;
1493 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001494 avc_ss_reset(seqno);
1495 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001496 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001497 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return 0;
1499 }
1500
1501#if 0
1502 sidtab_hash_eval(&sidtab, "sids");
1503#endif
1504
1505 if (policydb_read(&newpolicydb, fp)) {
1506 LOAD_UNLOCK;
1507 return -EINVAL;
1508 }
1509
Stephen Smalley12b29f32008-05-07 13:03:20 -04001510 if (sidtab_init(&newsidtab)) {
1511 LOAD_UNLOCK;
1512 policydb_destroy(&newpolicydb);
1513 return -ENOMEM;
1514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Chad Sellersb94c7e62006-11-06 12:38:18 -05001516 /* Verify that the kernel defined classes are correct. */
1517 if (validate_classes(&newpolicydb)) {
1518 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001519 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001520 rc = -EINVAL;
1521 goto err;
1522 }
1523
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001524 rc = security_preserve_bools(&newpolicydb);
1525 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001526 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001527 goto err;
1528 }
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /* Clone the SID table. */
1531 sidtab_shutdown(&sidtab);
1532 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1533 rc = -ENOMEM;
1534 goto err;
1535 }
1536
Stephen Smalley12b29f32008-05-07 13:03:20 -04001537 /*
1538 * Convert the internal representations of contexts
1539 * in the new SID table.
1540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 args.oldp = &policydb;
1542 args.newp = &newpolicydb;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001543 rc = sidtab_map(&newsidtab, convert_context, &args);
1544 if (rc)
1545 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 /* Save the old policydb and SID table to free later. */
1548 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1549 sidtab_set(&oldsidtab, &sidtab);
1550
1551 /* Install the new policydb and SID table. */
1552 POLICY_WRLOCK;
1553 memcpy(&policydb, &newpolicydb, sizeof policydb);
1554 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05001555 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 seqno = ++latest_granting;
1557 policydb_loaded_version = policydb.policyvers;
1558 POLICY_WRUNLOCK;
1559 LOAD_UNLOCK;
1560
1561 /* Free the old policydb and SID table. */
1562 policydb_destroy(&oldpolicydb);
1563 sidtab_destroy(&oldsidtab);
1564
1565 avc_ss_reset(seqno);
1566 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001567 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001568 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 return 0;
1571
1572err:
1573 LOAD_UNLOCK;
1574 sidtab_destroy(&newsidtab);
1575 policydb_destroy(&newpolicydb);
1576 return rc;
1577
1578}
1579
1580/**
1581 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 * @protocol: protocol number
1583 * @port: port number
1584 * @out_sid: security identifier
1585 */
Paul Moore3e112172008-04-10 10:48:14 -04001586int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 struct ocontext *c;
1589 int rc = 0;
1590
1591 POLICY_RDLOCK;
1592
1593 c = policydb.ocontexts[OCON_PORT];
1594 while (c) {
1595 if (c->u.port.protocol == protocol &&
1596 c->u.port.low_port <= port &&
1597 c->u.port.high_port >= port)
1598 break;
1599 c = c->next;
1600 }
1601
1602 if (c) {
1603 if (!c->sid[0]) {
1604 rc = sidtab_context_to_sid(&sidtab,
1605 &c->context[0],
1606 &c->sid[0]);
1607 if (rc)
1608 goto out;
1609 }
1610 *out_sid = c->sid[0];
1611 } else {
1612 *out_sid = SECINITSID_PORT;
1613 }
1614
1615out:
1616 POLICY_RDUNLOCK;
1617 return rc;
1618}
1619
1620/**
1621 * security_netif_sid - Obtain the SID for a network interface.
1622 * @name: interface name
1623 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05001625int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 int rc = 0;
1628 struct ocontext *c;
1629
1630 POLICY_RDLOCK;
1631
1632 c = policydb.ocontexts[OCON_NETIF];
1633 while (c) {
1634 if (strcmp(name, c->u.name) == 0)
1635 break;
1636 c = c->next;
1637 }
1638
1639 if (c) {
1640 if (!c->sid[0] || !c->sid[1]) {
1641 rc = sidtab_context_to_sid(&sidtab,
1642 &c->context[0],
1643 &c->sid[0]);
1644 if (rc)
1645 goto out;
1646 rc = sidtab_context_to_sid(&sidtab,
1647 &c->context[1],
1648 &c->sid[1]);
1649 if (rc)
1650 goto out;
1651 }
1652 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05001653 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656out:
1657 POLICY_RDUNLOCK;
1658 return rc;
1659}
1660
1661static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1662{
1663 int i, fail = 0;
1664
Eric Paris5d55a342008-04-18 17:38:33 -04001665 for (i = 0; i < 4; i++)
1666 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 fail = 1;
1668 break;
1669 }
1670
1671 return !fail;
1672}
1673
1674/**
1675 * security_node_sid - Obtain the SID for a node (host).
1676 * @domain: communication domain aka address family
1677 * @addrp: address
1678 * @addrlen: address length in bytes
1679 * @out_sid: security identifier
1680 */
1681int security_node_sid(u16 domain,
1682 void *addrp,
1683 u32 addrlen,
1684 u32 *out_sid)
1685{
1686 int rc = 0;
1687 struct ocontext *c;
1688
1689 POLICY_RDLOCK;
1690
1691 switch (domain) {
1692 case AF_INET: {
1693 u32 addr;
1694
1695 if (addrlen != sizeof(u32)) {
1696 rc = -EINVAL;
1697 goto out;
1698 }
1699
1700 addr = *((u32 *)addrp);
1701
1702 c = policydb.ocontexts[OCON_NODE];
1703 while (c) {
1704 if (c->u.node.addr == (addr & c->u.node.mask))
1705 break;
1706 c = c->next;
1707 }
1708 break;
1709 }
1710
1711 case AF_INET6:
1712 if (addrlen != sizeof(u64) * 2) {
1713 rc = -EINVAL;
1714 goto out;
1715 }
1716 c = policydb.ocontexts[OCON_NODE6];
1717 while (c) {
1718 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1719 c->u.node6.mask))
1720 break;
1721 c = c->next;
1722 }
1723 break;
1724
1725 default:
1726 *out_sid = SECINITSID_NODE;
1727 goto out;
1728 }
1729
1730 if (c) {
1731 if (!c->sid[0]) {
1732 rc = sidtab_context_to_sid(&sidtab,
1733 &c->context[0],
1734 &c->sid[0]);
1735 if (rc)
1736 goto out;
1737 }
1738 *out_sid = c->sid[0];
1739 } else {
1740 *out_sid = SECINITSID_NODE;
1741 }
1742
1743out:
1744 POLICY_RDUNLOCK;
1745 return rc;
1746}
1747
1748#define SIDS_NEL 25
1749
1750/**
1751 * security_get_user_sids - Obtain reachable SIDs for a user.
1752 * @fromsid: starting SID
1753 * @username: username
1754 * @sids: array of reachable SIDs for user
1755 * @nel: number of elements in @sids
1756 *
1757 * Generate the set of SIDs for legal security contexts
1758 * for a given user that can be reached by @fromsid.
1759 * Set *@sids to point to a dynamically allocated
1760 * array containing the set of SIDs. Set *@nel to the
1761 * number of elements in the array.
1762 */
1763
1764int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04001765 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 u32 **sids,
1767 u32 *nel)
1768{
1769 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001770 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 u32 mynel = 0, maxnel = SIDS_NEL;
1772 struct user_datum *user;
1773 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001774 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 int rc = 0, i, j;
1776
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001777 *sids = NULL;
1778 *nel = 0;
1779
1780 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 POLICY_RDLOCK;
1784
Stephen Smalley12b29f32008-05-07 13:03:20 -04001785 context_init(&usercon);
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 fromcon = sidtab_search(&sidtab, fromsid);
1788 if (!fromcon) {
1789 rc = -EINVAL;
1790 goto out_unlock;
1791 }
1792
1793 user = hashtab_search(policydb.p_users.table, username);
1794 if (!user) {
1795 rc = -EINVAL;
1796 goto out_unlock;
1797 }
1798 usercon.user = user->value;
1799
James Morris89d155e2005-10-30 14:59:21 -08001800 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (!mysids) {
1802 rc = -ENOMEM;
1803 goto out_unlock;
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001806 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 role = policydb.role_val_to_struct[i];
1808 usercon.role = i+1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001809 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 usercon.type = j+1;
1811
1812 if (mls_setup_user_range(fromcon, user, &usercon))
1813 continue;
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001816 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (mynel < maxnel) {
1819 mysids[mynel++] = sid;
1820 } else {
1821 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08001822 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (!mysids2) {
1824 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 goto out_unlock;
1826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1828 kfree(mysids);
1829 mysids = mysids2;
1830 mysids[mynel++] = sid;
1831 }
1832 }
1833 }
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835out_unlock:
1836 POLICY_RDUNLOCK;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001837 if (rc || !mynel) {
1838 kfree(mysids);
1839 goto out;
1840 }
1841
1842 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1843 if (!mysids2) {
1844 rc = -ENOMEM;
1845 kfree(mysids);
1846 goto out;
1847 }
1848 for (i = 0, j = 0; i < mynel; i++) {
1849 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1850 SECCLASS_PROCESS,
1851 PROCESS__TRANSITION, AVC_STRICT,
1852 NULL);
1853 if (!rc)
1854 mysids2[j++] = mysids[i];
1855 cond_resched();
1856 }
1857 rc = 0;
1858 kfree(mysids);
1859 *sids = mysids2;
1860 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861out:
1862 return rc;
1863}
1864
1865/**
1866 * security_genfs_sid - Obtain a SID for a file in a filesystem
1867 * @fstype: filesystem type
1868 * @path: path from root of mount
1869 * @sclass: file security class
1870 * @sid: SID for path
1871 *
1872 * Obtain a SID to use for a file in a filesystem that
1873 * cannot support xattr or use a fixed labeling behavior like
1874 * transition SIDs or task SIDs.
1875 */
1876int security_genfs_sid(const char *fstype,
Eric Paris5d55a342008-04-18 17:38:33 -04001877 char *path,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 u16 sclass,
1879 u32 *sid)
1880{
1881 int len;
1882 struct genfs *genfs;
1883 struct ocontext *c;
1884 int rc = 0, cmp = 0;
1885
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05001886 while (path[0] == '/' && path[1] == '/')
1887 path++;
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 POLICY_RDLOCK;
1890
1891 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1892 cmp = strcmp(fstype, genfs->fstype);
1893 if (cmp <= 0)
1894 break;
1895 }
1896
1897 if (!genfs || cmp) {
1898 *sid = SECINITSID_UNLABELED;
1899 rc = -ENOENT;
1900 goto out;
1901 }
1902
1903 for (c = genfs->head; c; c = c->next) {
1904 len = strlen(c->u.name);
1905 if ((!c->v.sclass || sclass == c->v.sclass) &&
1906 (strncmp(c->u.name, path, len) == 0))
1907 break;
1908 }
1909
1910 if (!c) {
1911 *sid = SECINITSID_UNLABELED;
1912 rc = -ENOENT;
1913 goto out;
1914 }
1915
1916 if (!c->sid[0]) {
1917 rc = sidtab_context_to_sid(&sidtab,
1918 &c->context[0],
1919 &c->sid[0]);
1920 if (rc)
1921 goto out;
1922 }
1923
1924 *sid = c->sid[0];
1925out:
1926 POLICY_RDUNLOCK;
1927 return rc;
1928}
1929
1930/**
1931 * security_fs_use - Determine how to handle labeling for a filesystem.
1932 * @fstype: filesystem type
1933 * @behavior: labeling behavior
1934 * @sid: SID for filesystem (superblock)
1935 */
1936int security_fs_use(
1937 const char *fstype,
1938 unsigned int *behavior,
1939 u32 *sid)
1940{
1941 int rc = 0;
1942 struct ocontext *c;
1943
1944 POLICY_RDLOCK;
1945
1946 c = policydb.ocontexts[OCON_FSUSE];
1947 while (c) {
1948 if (strcmp(fstype, c->u.name) == 0)
1949 break;
1950 c = c->next;
1951 }
1952
1953 if (c) {
1954 *behavior = c->v.behavior;
1955 if (!c->sid[0]) {
1956 rc = sidtab_context_to_sid(&sidtab,
1957 &c->context[0],
1958 &c->sid[0]);
1959 if (rc)
1960 goto out;
1961 }
1962 *sid = c->sid[0];
1963 } else {
1964 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1965 if (rc) {
1966 *behavior = SECURITY_FS_USE_NONE;
1967 rc = 0;
1968 } else {
1969 *behavior = SECURITY_FS_USE_GENFS;
1970 }
1971 }
1972
1973out:
1974 POLICY_RDUNLOCK;
1975 return rc;
1976}
1977
1978int security_get_bools(int *len, char ***names, int **values)
1979{
1980 int i, rc = -ENOMEM;
1981
1982 POLICY_RDLOCK;
1983 *names = NULL;
1984 *values = NULL;
1985
1986 *len = policydb.p_bools.nprim;
1987 if (!*len) {
1988 rc = 0;
1989 goto out;
1990 }
1991
Eric Paris5d55a342008-04-18 17:38:33 -04001992 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if (!*names)
1994 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Jesper Juhle0795cf2006-01-09 20:54:46 -08001996 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (!*values)
1998 goto err;
1999
2000 for (i = 0; i < *len; i++) {
2001 size_t name_len;
2002 (*values)[i] = policydb.bool_val_to_struct[i]->state;
2003 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002004 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (!(*names)[i])
2006 goto err;
2007 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
2008 (*names)[i][name_len - 1] = 0;
2009 }
2010 rc = 0;
2011out:
2012 POLICY_RDUNLOCK;
2013 return rc;
2014err:
2015 if (*names) {
2016 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002017 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002019 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 goto out;
2021}
2022
2023
2024int security_set_bools(int len, int *values)
2025{
2026 int i, rc = 0;
2027 int lenp, seqno = 0;
2028 struct cond_node *cur;
2029
2030 POLICY_WRLOCK;
2031
2032 lenp = policydb.p_bools.nprim;
2033 if (len != lenp) {
2034 rc = -EFAULT;
2035 goto out;
2036 }
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00002039 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2040 audit_log(current->audit_context, GFP_ATOMIC,
2041 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05002042 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Steve Grubbaf601e42006-01-04 14:08:39 +00002043 policydb.p_bool_val_to_name[i],
2044 !!values[i],
2045 policydb.bool_val_to_struct[i]->state,
Eric Paris4746ec52008-01-08 10:06:53 -05002046 audit_get_loginuid(current),
2047 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00002048 }
Eric Paris5d55a342008-04-18 17:38:33 -04002049 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002051 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
2056 rc = evaluate_cond_node(&policydb, cur);
2057 if (rc)
2058 goto out;
2059 }
2060
2061 seqno = ++latest_granting;
2062
2063out:
2064 POLICY_WRUNLOCK;
2065 if (!rc) {
2066 avc_ss_reset(seqno);
2067 selnl_notify_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002068 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070 return rc;
2071}
2072
2073int security_get_bool_value(int bool)
2074{
2075 int rc = 0;
2076 int len;
2077
2078 POLICY_RDLOCK;
2079
2080 len = policydb.p_bools.nprim;
2081 if (bool >= len) {
2082 rc = -EFAULT;
2083 goto out;
2084 }
2085
2086 rc = policydb.bool_val_to_struct[bool]->state;
2087out:
2088 POLICY_RDUNLOCK;
2089 return rc;
2090}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002091
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002092static int security_preserve_bools(struct policydb *p)
2093{
2094 int rc, nbools = 0, *bvalues = NULL, i;
2095 char **bnames = NULL;
2096 struct cond_bool_datum *booldatum;
2097 struct cond_node *cur;
2098
2099 rc = security_get_bools(&nbools, &bnames, &bvalues);
2100 if (rc)
2101 goto out;
2102 for (i = 0; i < nbools; i++) {
2103 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2104 if (booldatum)
2105 booldatum->state = bvalues[i];
2106 }
2107 for (cur = p->cond_list; cur != NULL; cur = cur->next) {
2108 rc = evaluate_cond_node(p, cur);
2109 if (rc)
2110 goto out;
2111 }
2112
2113out:
2114 if (bnames) {
2115 for (i = 0; i < nbools; i++)
2116 kfree(bnames[i]);
2117 }
2118 kfree(bnames);
2119 kfree(bvalues);
2120 return rc;
2121}
2122
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002123/*
2124 * security_sid_mls_copy() - computes a new sid based on the given
2125 * sid and the mls portion of mls_sid.
2126 */
2127int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2128{
2129 struct context *context1;
2130 struct context *context2;
2131 struct context newcon;
2132 char *s;
2133 u32 len;
2134 int rc = 0;
2135
Venkat Yekkirala4eb327b2006-09-19 10:24:19 -07002136 if (!ss_initialized || !selinux_mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002137 *new_sid = sid;
2138 goto out;
2139 }
2140
2141 context_init(&newcon);
2142
2143 POLICY_RDLOCK;
2144 context1 = sidtab_search(&sidtab, sid);
2145 if (!context1) {
Eric Paris744ba352008-04-17 11:52:44 -04002146 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2147 __func__, sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002148 rc = -EINVAL;
2149 goto out_unlock;
2150 }
2151
2152 context2 = sidtab_search(&sidtab, mls_sid);
2153 if (!context2) {
Eric Paris744ba352008-04-17 11:52:44 -04002154 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2155 __func__, mls_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002156 rc = -EINVAL;
2157 goto out_unlock;
2158 }
2159
2160 newcon.user = context1->user;
2161 newcon.role = context1->role;
2162 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002163 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002164 if (rc)
2165 goto out_unlock;
2166
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002167 /* Check the validity of the new context. */
2168 if (!policydb_context_isvalid(&policydb, &newcon)) {
2169 rc = convert_context_handle_invalid_context(&newcon);
2170 if (rc)
2171 goto bad;
2172 }
2173
2174 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2175 goto out_unlock;
2176
2177bad:
2178 if (!context_struct_to_string(&newcon, &s, &len)) {
2179 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2180 "security_sid_mls_copy: invalid context %s", s);
2181 kfree(s);
2182 }
2183
2184out_unlock:
2185 POLICY_RDUNLOCK;
2186 context_destroy(&newcon);
2187out:
2188 return rc;
2189}
2190
Paul Moore220deb92008-01-29 08:38:23 -05002191/**
2192 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2193 * @nlbl_sid: NetLabel SID
2194 * @nlbl_type: NetLabel labeling protocol type
2195 * @xfrm_sid: XFRM SID
2196 *
2197 * Description:
2198 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2199 * resolved into a single SID it is returned via @peer_sid and the function
2200 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2201 * returns a negative value. A table summarizing the behavior is below:
2202 *
2203 * | function return | @sid
2204 * ------------------------------+-----------------+-----------------
2205 * no peer labels | 0 | SECSID_NULL
2206 * single peer label | 0 | <peer_label>
2207 * multiple, consistent labels | 0 | <peer_label>
2208 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2209 *
2210 */
2211int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2212 u32 xfrm_sid,
2213 u32 *peer_sid)
2214{
2215 int rc;
2216 struct context *nlbl_ctx;
2217 struct context *xfrm_ctx;
2218
2219 /* handle the common (which also happens to be the set of easy) cases
2220 * right away, these two if statements catch everything involving a
2221 * single or absent peer SID/label */
2222 if (xfrm_sid == SECSID_NULL) {
2223 *peer_sid = nlbl_sid;
2224 return 0;
2225 }
2226 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2227 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2228 * is present */
2229 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2230 *peer_sid = xfrm_sid;
2231 return 0;
2232 }
2233
2234 /* we don't need to check ss_initialized here since the only way both
2235 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2236 * security server was initialized and ss_initialized was true */
2237 if (!selinux_mls_enabled) {
2238 *peer_sid = SECSID_NULL;
2239 return 0;
2240 }
2241
2242 POLICY_RDLOCK;
2243
2244 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2245 if (!nlbl_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002246 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2247 __func__, nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002248 rc = -EINVAL;
2249 goto out_slowpath;
2250 }
2251 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2252 if (!xfrm_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002253 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2254 __func__, xfrm_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002255 rc = -EINVAL;
2256 goto out_slowpath;
2257 }
2258 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2259
2260out_slowpath:
2261 POLICY_RDUNLOCK;
2262 if (rc == 0)
2263 /* at present NetLabel SIDs/labels really only carry MLS
2264 * information so if the MLS portion of the NetLabel SID
2265 * matches the MLS portion of the labeled XFRM SID/label
2266 * then pass along the XFRM SID as it is the most
2267 * expressive */
2268 *peer_sid = xfrm_sid;
2269 else
2270 *peer_sid = SECSID_NULL;
2271 return rc;
2272}
2273
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002274static int get_classes_callback(void *k, void *d, void *args)
2275{
2276 struct class_datum *datum = d;
2277 char *name = k, **classes = args;
2278 int value = datum->value - 1;
2279
2280 classes[value] = kstrdup(name, GFP_ATOMIC);
2281 if (!classes[value])
2282 return -ENOMEM;
2283
2284 return 0;
2285}
2286
2287int security_get_classes(char ***classes, int *nclasses)
2288{
2289 int rc = -ENOMEM;
2290
2291 POLICY_RDLOCK;
2292
2293 *nclasses = policydb.p_classes.nprim;
2294 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2295 if (!*classes)
2296 goto out;
2297
2298 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2299 *classes);
2300 if (rc < 0) {
2301 int i;
2302 for (i = 0; i < *nclasses; i++)
2303 kfree((*classes)[i]);
2304 kfree(*classes);
2305 }
2306
2307out:
2308 POLICY_RDUNLOCK;
2309 return rc;
2310}
2311
2312static int get_permissions_callback(void *k, void *d, void *args)
2313{
2314 struct perm_datum *datum = d;
2315 char *name = k, **perms = args;
2316 int value = datum->value - 1;
2317
2318 perms[value] = kstrdup(name, GFP_ATOMIC);
2319 if (!perms[value])
2320 return -ENOMEM;
2321
2322 return 0;
2323}
2324
2325int security_get_permissions(char *class, char ***perms, int *nperms)
2326{
2327 int rc = -ENOMEM, i;
2328 struct class_datum *match;
2329
2330 POLICY_RDLOCK;
2331
2332 match = hashtab_search(policydb.p_classes.table, class);
2333 if (!match) {
Eric Paris744ba352008-04-17 11:52:44 -04002334 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002335 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002336 rc = -EINVAL;
2337 goto out;
2338 }
2339
2340 *nperms = match->permissions.nprim;
2341 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2342 if (!*perms)
2343 goto out;
2344
2345 if (match->comdatum) {
2346 rc = hashtab_map(match->comdatum->permissions.table,
2347 get_permissions_callback, *perms);
2348 if (rc < 0)
2349 goto err;
2350 }
2351
2352 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2353 *perms);
2354 if (rc < 0)
2355 goto err;
2356
2357out:
2358 POLICY_RDUNLOCK;
2359 return rc;
2360
2361err:
2362 POLICY_RDUNLOCK;
2363 for (i = 0; i < *nperms; i++)
2364 kfree((*perms)[i]);
2365 kfree(*perms);
2366 return rc;
2367}
2368
Eric Paris3f120702007-09-21 14:37:10 -04002369int security_get_reject_unknown(void)
2370{
2371 return policydb.reject_unknown;
2372}
2373
2374int security_get_allow_unknown(void)
2375{
2376 return policydb.allow_unknown;
2377}
2378
Paul Moore3bb56b22008-01-29 08:38:19 -05002379/**
Paul Moore3bb56b22008-01-29 08:38:19 -05002380 * security_policycap_supported - Check for a specific policy capability
2381 * @req_cap: capability
2382 *
2383 * Description:
2384 * This function queries the currently loaded policy to see if it supports the
2385 * capability specified by @req_cap. Returns true (1) if the capability is
2386 * supported, false (0) if it isn't supported.
2387 *
2388 */
2389int security_policycap_supported(unsigned int req_cap)
2390{
2391 int rc;
2392
2393 POLICY_RDLOCK;
2394 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
2395 POLICY_RDUNLOCK;
2396
2397 return rc;
2398}
2399
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002400struct selinux_audit_rule {
2401 u32 au_seqno;
2402 struct context au_ctxt;
2403};
2404
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002405void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002406{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002407 struct selinux_audit_rule *rule = vrule;
2408
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002409 if (rule) {
2410 context_destroy(&rule->au_ctxt);
2411 kfree(rule);
2412 }
2413}
2414
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002415int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002416{
2417 struct selinux_audit_rule *tmprule;
2418 struct role_datum *roledatum;
2419 struct type_datum *typedatum;
2420 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002421 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002422 int rc = 0;
2423
2424 *rule = NULL;
2425
2426 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07002427 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002428
2429 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002430 case AUDIT_SUBJ_USER:
2431 case AUDIT_SUBJ_ROLE:
2432 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002433 case AUDIT_OBJ_USER:
2434 case AUDIT_OBJ_ROLE:
2435 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002436 /* only 'equals' and 'not equals' fit user, role, and type */
2437 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2438 return -EINVAL;
2439 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002440 case AUDIT_SUBJ_SEN:
2441 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002442 case AUDIT_OBJ_LEV_LOW:
2443 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002444 /* we do not allow a range, indicated by the presense of '-' */
2445 if (strchr(rulestr, '-'))
2446 return -EINVAL;
2447 break;
2448 default:
2449 /* only the above fields are valid */
2450 return -EINVAL;
2451 }
2452
2453 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2454 if (!tmprule)
2455 return -ENOMEM;
2456
2457 context_init(&tmprule->au_ctxt);
2458
2459 POLICY_RDLOCK;
2460
2461 tmprule->au_seqno = latest_granting;
2462
2463 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002464 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002465 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002466 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2467 if (!userdatum)
2468 rc = -EINVAL;
2469 else
2470 tmprule->au_ctxt.user = userdatum->value;
2471 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002472 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002473 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002474 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2475 if (!roledatum)
2476 rc = -EINVAL;
2477 else
2478 tmprule->au_ctxt.role = roledatum->value;
2479 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002480 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002481 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002482 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2483 if (!typedatum)
2484 rc = -EINVAL;
2485 else
2486 tmprule->au_ctxt.type = typedatum->value;
2487 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002488 case AUDIT_SUBJ_SEN:
2489 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002490 case AUDIT_OBJ_LEV_LOW:
2491 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002492 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2493 break;
2494 }
2495
2496 POLICY_RDUNLOCK;
2497
2498 if (rc) {
2499 selinux_audit_rule_free(tmprule);
2500 tmprule = NULL;
2501 }
2502
2503 *rule = tmprule;
2504
2505 return rc;
2506}
2507
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002508/* Check to see if the rule contains any selinux fields */
2509int selinux_audit_rule_known(struct audit_krule *rule)
2510{
2511 int i;
2512
2513 for (i = 0; i < rule->field_count; i++) {
2514 struct audit_field *f = &rule->fields[i];
2515 switch (f->type) {
2516 case AUDIT_SUBJ_USER:
2517 case AUDIT_SUBJ_ROLE:
2518 case AUDIT_SUBJ_TYPE:
2519 case AUDIT_SUBJ_SEN:
2520 case AUDIT_SUBJ_CLR:
2521 case AUDIT_OBJ_USER:
2522 case AUDIT_OBJ_ROLE:
2523 case AUDIT_OBJ_TYPE:
2524 case AUDIT_OBJ_LEV_LOW:
2525 case AUDIT_OBJ_LEV_HIGH:
2526 return 1;
2527 }
2528 }
2529
2530 return 0;
2531}
2532
2533int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002534 struct audit_context *actx)
2535{
2536 struct context *ctxt;
2537 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002538 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002539 int match = 0;
2540
2541 if (!rule) {
2542 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002543 "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002544 return -ENOENT;
2545 }
2546
2547 POLICY_RDLOCK;
2548
2549 if (rule->au_seqno < latest_granting) {
2550 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002551 "selinux_audit_rule_match: stale rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002552 match = -ESTALE;
2553 goto out;
2554 }
2555
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002556 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002557 if (!ctxt) {
2558 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002559 "selinux_audit_rule_match: unrecognized SID %d\n",
2560 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002561 match = -ENOENT;
2562 goto out;
2563 }
2564
2565 /* a field/op pair that is not caught here will simply fall through
2566 without a match */
2567 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002568 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002569 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002570 switch (op) {
2571 case AUDIT_EQUAL:
2572 match = (ctxt->user == rule->au_ctxt.user);
2573 break;
2574 case AUDIT_NOT_EQUAL:
2575 match = (ctxt->user != rule->au_ctxt.user);
2576 break;
2577 }
2578 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002579 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002580 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002581 switch (op) {
2582 case AUDIT_EQUAL:
2583 match = (ctxt->role == rule->au_ctxt.role);
2584 break;
2585 case AUDIT_NOT_EQUAL:
2586 match = (ctxt->role != rule->au_ctxt.role);
2587 break;
2588 }
2589 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002590 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002591 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002592 switch (op) {
2593 case AUDIT_EQUAL:
2594 match = (ctxt->type == rule->au_ctxt.type);
2595 break;
2596 case AUDIT_NOT_EQUAL:
2597 match = (ctxt->type != rule->au_ctxt.type);
2598 break;
2599 }
2600 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002601 case AUDIT_SUBJ_SEN:
2602 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002603 case AUDIT_OBJ_LEV_LOW:
2604 case AUDIT_OBJ_LEV_HIGH:
2605 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04002606 field == AUDIT_OBJ_LEV_LOW) ?
2607 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002608 switch (op) {
2609 case AUDIT_EQUAL:
2610 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002611 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002612 break;
2613 case AUDIT_NOT_EQUAL:
2614 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002615 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002616 break;
2617 case AUDIT_LESS_THAN:
2618 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002619 level) &&
2620 !mls_level_eq(&rule->au_ctxt.range.level[0],
2621 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002622 break;
2623 case AUDIT_LESS_THAN_OR_EQUAL:
2624 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002625 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002626 break;
2627 case AUDIT_GREATER_THAN:
2628 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002629 &rule->au_ctxt.range.level[0]) &&
2630 !mls_level_eq(level,
2631 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002632 break;
2633 case AUDIT_GREATER_THAN_OR_EQUAL:
2634 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002635 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002636 break;
2637 }
2638 }
2639
2640out:
2641 POLICY_RDUNLOCK;
2642 return match;
2643}
2644
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002645static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002646
2647static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2648 u16 class, u32 perms, u32 *retained)
2649{
2650 int err = 0;
2651
2652 if (event == AVC_CALLBACK_RESET && aurule_callback)
2653 err = aurule_callback();
2654 return err;
2655}
2656
2657static int __init aurule_init(void)
2658{
2659 int err;
2660
2661 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
Eric Paris5d55a342008-04-18 17:38:33 -04002662 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002663 if (err)
2664 panic("avc_add_callback() failed, error %d\n", err);
2665
2666 return err;
2667}
2668__initcall(aurule_init);
2669
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002670#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002671/**
Paul Moore5778eab2007-02-28 15:14:22 -05002672 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2673 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002674 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002675 *
2676 * Description:
2677 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05002678 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2679 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002680 *
2681 */
Paul Moore5778eab2007-02-28 15:14:22 -05002682static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002683 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002684{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002685 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002686
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002687 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2688 if (sid_cache == NULL)
2689 return;
Paul Moore5778eab2007-02-28 15:14:22 -05002690 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002691 if (secattr->cache == NULL) {
2692 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05002693 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02002694 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002695
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002696 *sid_cache = sid;
2697 secattr->cache->free = kfree;
2698 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05002699 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002700}
2701
2702/**
Paul Moore5778eab2007-02-28 15:14:22 -05002703 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002704 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002705 * @sid: the SELinux SID
2706 *
2707 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002708 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002709 * SELinux SID. If the @secattr field does not contain a full SELinux
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002710 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2711 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2712 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2713 * conversion for future lookups. Returns zero on success, negative values on
2714 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002715 *
2716 */
Paul Moore5778eab2007-02-28 15:14:22 -05002717int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05002718 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002719{
2720 int rc = -EIDRM;
2721 struct context *ctx;
2722 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05002723
2724 if (!ss_initialized) {
2725 *sid = SECSID_NULL;
2726 return 0;
2727 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002728
2729 POLICY_RDLOCK;
2730
Paul Moore701a90b2006-11-17 17:38:46 -05002731 if (secattr->flags & NETLBL_SECATTR_CACHE) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002732 *sid = *(u32 *)secattr->cache->data;
2733 rc = 0;
Paul Moore16efd452008-01-29 08:37:59 -05002734 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2735 *sid = secattr->attr.secid;
2736 rc = 0;
Paul Moore701a90b2006-11-17 17:38:46 -05002737 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002738 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002739 if (ctx == NULL)
2740 goto netlbl_secattr_to_sid_return;
2741
2742 ctx_new.user = ctx->user;
2743 ctx_new.role = ctx->role;
2744 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05002745 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05002746 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05002747 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
Paul Moore16efd452008-01-29 08:37:59 -05002748 secattr->attr.mls.cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002749 goto netlbl_secattr_to_sid_return;
2750 ctx_new.range.level[1].cat.highbit =
2751 ctx_new.range.level[0].cat.highbit;
2752 ctx_new.range.level[1].cat.node =
2753 ctx_new.range.level[0].cat.node;
2754 } else {
2755 ebitmap_init(&ctx_new.range.level[0].cat);
2756 ebitmap_init(&ctx_new.range.level[1].cat);
2757 }
2758 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2759 goto netlbl_secattr_to_sid_return_cleanup;
2760
2761 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2762 if (rc != 0)
2763 goto netlbl_secattr_to_sid_return_cleanup;
2764
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002765 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05002766
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002767 ebitmap_destroy(&ctx_new.range.level[0].cat);
2768 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002769 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002770 rc = 0;
2771 }
2772
2773netlbl_secattr_to_sid_return:
2774 POLICY_RDUNLOCK;
2775 return rc;
2776netlbl_secattr_to_sid_return_cleanup:
2777 ebitmap_destroy(&ctx_new.range.level[0].cat);
2778 goto netlbl_secattr_to_sid_return;
2779}
2780
2781/**
Paul Moore5778eab2007-02-28 15:14:22 -05002782 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2783 * @sid: the SELinux SID
2784 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002785 *
2786 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002787 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2788 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002789 *
2790 */
Paul Moore5778eab2007-02-28 15:14:22 -05002791int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002792{
2793 int rc = -ENOENT;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002794 struct context *ctx;
2795
2796 if (!ss_initialized)
2797 return 0;
2798
2799 POLICY_RDLOCK;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002800 ctx = sidtab_search(&sidtab, sid);
2801 if (ctx == NULL)
Paul Moore5778eab2007-02-28 15:14:22 -05002802 goto netlbl_sid_to_secattr_failure;
2803 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2804 GFP_ATOMIC);
Paul Moore00447872008-04-12 19:06:42 -07002805 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY;
Paul Moore5778eab2007-02-28 15:14:22 -05002806 mls_export_netlbl_lvl(ctx, secattr);
2807 rc = mls_export_netlbl_cat(ctx, secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04002808 if (rc != 0)
Paul Moore5778eab2007-02-28 15:14:22 -05002809 goto netlbl_sid_to_secattr_failure;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002810 POLICY_RDUNLOCK;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002811
Paul Moore5778eab2007-02-28 15:14:22 -05002812 return 0;
Paul Moore9f2ad662006-11-17 17:38:53 -05002813
Paul Moore5778eab2007-02-28 15:14:22 -05002814netlbl_sid_to_secattr_failure:
2815 POLICY_RDUNLOCK;
Paul Mooref8687af2006-10-30 15:22:15 -08002816 return rc;
2817}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002818#endif /* CONFIG_NETLABEL */