blob: 543fd0f31b33d0e54449a82a1fd2af73dd619bbe [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static struct sidtab sidtab;
76struct policydb policydb;
Eric Paris5d55a342008-04-18 17:38:33 -040077int ss_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * The largest sequence number that has been used when
81 * providing an access decision to the access vector cache.
82 * The sequence number only changes when a policy change
83 * occurs.
84 */
Eric Paris5d55a342008-04-18 17:38:33 -040085static u32 latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/* Forward declaration. */
88static int context_struct_to_string(struct context *context, char **scontext,
89 u32 *scontext_len);
90
91/*
92 * Return the boolean value of a constraint expression
93 * when it is applied to the specified source and target
94 * security contexts.
95 *
96 * xcontext is a special beast... It is used by the validatetrans rules
97 * only. For these rules, scontext is the context before the transition,
98 * tcontext is the context after the transition, and xcontext is the context
99 * of the process performing the transition. All other callers of
100 * constraint_expr_eval should pass in NULL for xcontext.
101 */
102static int constraint_expr_eval(struct context *scontext,
103 struct context *tcontext,
104 struct context *xcontext,
105 struct constraint_expr *cexpr)
106{
107 u32 val1, val2;
108 struct context *c;
109 struct role_datum *r1, *r2;
110 struct mls_level *l1, *l2;
111 struct constraint_expr *e;
112 int s[CEXPR_MAXDEPTH];
113 int sp = -1;
114
115 for (e = cexpr; e; e = e->next) {
116 switch (e->expr_type) {
117 case CEXPR_NOT:
118 BUG_ON(sp < 0);
119 s[sp] = !s[sp];
120 break;
121 case CEXPR_AND:
122 BUG_ON(sp < 1);
123 sp--;
124 s[sp] &= s[sp+1];
125 break;
126 case CEXPR_OR:
127 BUG_ON(sp < 1);
128 sp--;
129 s[sp] |= s[sp+1];
130 break;
131 case CEXPR_ATTR:
132 if (sp == (CEXPR_MAXDEPTH-1))
133 return 0;
134 switch (e->attr) {
135 case CEXPR_USER:
136 val1 = scontext->user;
137 val2 = tcontext->user;
138 break;
139 case CEXPR_TYPE:
140 val1 = scontext->type;
141 val2 = tcontext->type;
142 break;
143 case CEXPR_ROLE:
144 val1 = scontext->role;
145 val2 = tcontext->role;
146 r1 = policydb.role_val_to_struct[val1 - 1];
147 r2 = policydb.role_val_to_struct[val2 - 1];
148 switch (e->op) {
149 case CEXPR_DOM:
150 s[++sp] = ebitmap_get_bit(&r1->dominates,
151 val2 - 1);
152 continue;
153 case CEXPR_DOMBY:
154 s[++sp] = ebitmap_get_bit(&r2->dominates,
155 val1 - 1);
156 continue;
157 case CEXPR_INCOMP:
Eric Paris5d55a342008-04-18 17:38:33 -0400158 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
159 val2 - 1) &&
160 !ebitmap_get_bit(&r2->dominates,
161 val1 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 continue;
163 default:
164 break;
165 }
166 break;
167 case CEXPR_L1L2:
168 l1 = &(scontext->range.level[0]);
169 l2 = &(tcontext->range.level[0]);
170 goto mls_ops;
171 case CEXPR_L1H2:
172 l1 = &(scontext->range.level[0]);
173 l2 = &(tcontext->range.level[1]);
174 goto mls_ops;
175 case CEXPR_H1L2:
176 l1 = &(scontext->range.level[1]);
177 l2 = &(tcontext->range.level[0]);
178 goto mls_ops;
179 case CEXPR_H1H2:
180 l1 = &(scontext->range.level[1]);
181 l2 = &(tcontext->range.level[1]);
182 goto mls_ops;
183 case CEXPR_L1H1:
184 l1 = &(scontext->range.level[0]);
185 l2 = &(scontext->range.level[1]);
186 goto mls_ops;
187 case CEXPR_L2H2:
188 l1 = &(tcontext->range.level[0]);
189 l2 = &(tcontext->range.level[1]);
190 goto mls_ops;
191mls_ops:
192 switch (e->op) {
193 case CEXPR_EQ:
194 s[++sp] = mls_level_eq(l1, l2);
195 continue;
196 case CEXPR_NEQ:
197 s[++sp] = !mls_level_eq(l1, l2);
198 continue;
199 case CEXPR_DOM:
200 s[++sp] = mls_level_dom(l1, l2);
201 continue;
202 case CEXPR_DOMBY:
203 s[++sp] = mls_level_dom(l2, l1);
204 continue;
205 case CEXPR_INCOMP:
206 s[++sp] = mls_level_incomp(l2, l1);
207 continue;
208 default:
209 BUG();
210 return 0;
211 }
212 break;
213 default:
214 BUG();
215 return 0;
216 }
217
218 switch (e->op) {
219 case CEXPR_EQ:
220 s[++sp] = (val1 == val2);
221 break;
222 case CEXPR_NEQ:
223 s[++sp] = (val1 != val2);
224 break;
225 default:
226 BUG();
227 return 0;
228 }
229 break;
230 case CEXPR_NAMES:
231 if (sp == (CEXPR_MAXDEPTH-1))
232 return 0;
233 c = scontext;
234 if (e->attr & CEXPR_TARGET)
235 c = tcontext;
236 else if (e->attr & CEXPR_XTARGET) {
237 c = xcontext;
238 if (!c) {
239 BUG();
240 return 0;
241 }
242 }
243 if (e->attr & CEXPR_USER)
244 val1 = c->user;
245 else if (e->attr & CEXPR_ROLE)
246 val1 = c->role;
247 else if (e->attr & CEXPR_TYPE)
248 val1 = c->type;
249 else {
250 BUG();
251 return 0;
252 }
253
254 switch (e->op) {
255 case CEXPR_EQ:
256 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
257 break;
258 case CEXPR_NEQ:
259 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
260 break;
261 default:
262 BUG();
263 return 0;
264 }
265 break;
266 default:
267 BUG();
268 return 0;
269 }
270 }
271
272 BUG_ON(sp != 0);
273 return s[0];
274}
275
276/*
277 * Compute access vectors based on a context structure pair for
278 * the permissions in a particular class.
279 */
280static int context_struct_compute_av(struct context *scontext,
281 struct context *tcontext,
282 u16 tclass,
283 u32 requested,
284 struct av_decision *avd)
285{
286 struct constraint_node *constraint;
287 struct role_allow *ra;
288 struct avtab_key avkey;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700289 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct class_datum *tclass_datum;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700291 struct ebitmap *sattr, *tattr;
292 struct ebitmap_node *snode, *tnode;
Eric Paris3f120702007-09-21 14:37:10 -0400293 const struct selinux_class_perm *kdefs = &selinux_class_perm;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700294 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /*
297 * Remap extended Netlink classes for old policy versions.
298 * Do this here rather than socket_type_to_security_class()
299 * in case a newer policy version is loaded, allowing sockets
300 * to remain in the correct class.
301 */
302 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
303 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
304 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
305 tclass = SECCLASS_NETLINK_SOCKET;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /*
308 * Initialize the access vectors to the default values.
309 */
310 avd->allowed = 0;
311 avd->decided = 0xffffffff;
312 avd->auditallow = 0;
313 avd->auditdeny = 0xffffffff;
314 avd->seqno = latest_granting;
315
316 /*
Eric Paris3f120702007-09-21 14:37:10 -0400317 * Check for all the invalid cases.
318 * - tclass 0
319 * - tclass > policy and > kernel
320 * - tclass > policy but is a userspace class
321 * - tclass > policy but we do not allow unknowns
322 */
323 if (unlikely(!tclass))
324 goto inval_class;
325 if (unlikely(tclass > policydb.p_classes.nprim))
326 if (tclass > kdefs->cts_len ||
Eric Pariscea78dc2008-06-09 15:43:12 -0400327 !kdefs->class_to_string[tclass] ||
Eric Paris3f120702007-09-21 14:37:10 -0400328 !policydb.allow_unknown)
329 goto inval_class;
330
331 /*
332 * Kernel class and we allow unknown so pad the allow decision
333 * the pad will be all 1 for unknown classes.
334 */
335 if (tclass <= kdefs->cts_len && policydb.allow_unknown)
336 avd->allowed = policydb.undefined_perms[tclass - 1];
337
338 /*
339 * Not in policy. Since decision is completed (all 1 or all 0) return.
340 */
341 if (unlikely(tclass > policydb.p_classes.nprim))
342 return 0;
343
344 tclass_datum = policydb.class_val_to_struct[tclass - 1];
345
346 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * If a specific type enforcement rule was defined for
348 * this permission check, then use it.
349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700351 avkey.specified = AVTAB_AV;
352 sattr = &policydb.type_attr_map[scontext->type - 1];
353 tattr = &policydb.type_attr_map[tcontext->type - 1];
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900354 ebitmap_for_each_positive_bit(sattr, snode, i) {
355 ebitmap_for_each_positive_bit(tattr, tnode, j) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700356 avkey.source_type = i + 1;
357 avkey.target_type = j + 1;
358 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
359 node != NULL;
360 node = avtab_search_node_next(node, avkey.specified)) {
361 if (node->key.specified == AVTAB_ALLOWED)
362 avd->allowed |= node->datum.data;
363 else if (node->key.specified == AVTAB_AUDITALLOW)
364 avd->auditallow |= node->datum.data;
365 else if (node->key.specified == AVTAB_AUDITDENY)
366 avd->auditdeny &= node->datum.data;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Stephen Smalley782ebb92005-09-03 15:55:16 -0700369 /* Check conditional av table for additional permissions */
370 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
371
372 }
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /*
376 * Remove any permissions prohibited by a constraint (this includes
377 * the MLS policy).
378 */
379 constraint = tclass_datum->constraints;
380 while (constraint) {
381 if ((constraint->permissions & (avd->allowed)) &&
382 !constraint_expr_eval(scontext, tcontext, NULL,
383 constraint->expr)) {
384 avd->allowed = (avd->allowed) & ~(constraint->permissions);
385 }
386 constraint = constraint->next;
387 }
388
389 /*
390 * If checking process transition permission and the
391 * role is changing, then check the (current_role, new_role)
392 * pair.
393 */
394 if (tclass == SECCLASS_PROCESS &&
395 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
396 scontext->role != tcontext->role) {
397 for (ra = policydb.role_allow; ra; ra = ra->next) {
398 if (scontext->role == ra->role &&
399 tcontext->role == ra->new_role)
400 break;
401 }
402 if (!ra)
403 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
Eric Paris5d55a342008-04-18 17:38:33 -0400404 PROCESS__DYNTRANSITION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
407 return 0;
Eric Paris3f120702007-09-21 14:37:10 -0400408
409inval_class:
Eric Paris744ba352008-04-17 11:52:44 -0400410 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n", __func__,
411 tclass);
Eric Paris3f120702007-09-21 14:37:10 -0400412 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Eric Paris64dbf072008-03-31 12:17:33 +1100415/*
416 * Given a sid find if the type has the permissive flag set
417 */
418int security_permissive_sid(u32 sid)
419{
420 struct context *context;
421 u32 type;
422 int rc;
423
James Morris0804d112008-06-06 18:40:29 +1000424 read_lock(&policy_rwlock);
Eric Paris64dbf072008-03-31 12:17:33 +1100425
426 context = sidtab_search(&sidtab, sid);
427 BUG_ON(!context);
428
429 type = context->type;
430 /*
431 * we are intentionally using type here, not type-1, the 0th bit may
432 * someday indicate that we are globally setting permissive in policy.
433 */
434 rc = ebitmap_get_bit(&policydb.permissive_map, type);
435
James Morris0804d112008-06-06 18:40:29 +1000436 read_unlock(&policy_rwlock);
Eric Paris64dbf072008-03-31 12:17:33 +1100437 return rc;
438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400441 struct context *ncontext,
442 struct context *tcontext,
443 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 char *o = NULL, *n = NULL, *t = NULL;
446 u32 olen, nlen, tlen;
447
448 if (context_struct_to_string(ocontext, &o, &olen) < 0)
449 goto out;
450 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
451 goto out;
452 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
453 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100454 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -0400455 "security_validate_transition: denied for"
456 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
457 o, n, t, policydb.p_class_val_to_name[tclass-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458out:
459 kfree(o);
460 kfree(n);
461 kfree(t);
462
463 if (!selinux_enforcing)
464 return 0;
465 return -EPERM;
466}
467
468int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
Eric Paris5d55a342008-04-18 17:38:33 -0400469 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct context *ocontext;
472 struct context *ncontext;
473 struct context *tcontext;
474 struct class_datum *tclass_datum;
475 struct constraint_node *constraint;
476 int rc = 0;
477
478 if (!ss_initialized)
479 return 0;
480
James Morris0804d112008-06-06 18:40:29 +1000481 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /*
484 * Remap extended Netlink classes for old policy versions.
485 * Do this here rather than socket_type_to_security_class()
486 * in case a newer policy version is loaded, allowing sockets
487 * to remain in the correct class.
488 */
489 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
490 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
491 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
492 tclass = SECCLASS_NETLINK_SOCKET;
493
494 if (!tclass || tclass > policydb.p_classes.nprim) {
Eric Paris744ba352008-04-17 11:52:44 -0400495 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
496 __func__, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 rc = -EINVAL;
498 goto out;
499 }
500 tclass_datum = policydb.class_val_to_struct[tclass - 1];
501
502 ocontext = sidtab_search(&sidtab, oldsid);
503 if (!ocontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400504 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
505 __func__, oldsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 rc = -EINVAL;
507 goto out;
508 }
509
510 ncontext = sidtab_search(&sidtab, newsid);
511 if (!ncontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400512 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
513 __func__, newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 rc = -EINVAL;
515 goto out;
516 }
517
518 tcontext = sidtab_search(&sidtab, tasksid);
519 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400520 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
521 __func__, tasksid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 rc = -EINVAL;
523 goto out;
524 }
525
526 constraint = tclass_datum->validatetrans;
527 while (constraint) {
528 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400529 constraint->expr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 rc = security_validtrans_handle_fail(ocontext, ncontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400531 tcontext, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto out;
533 }
534 constraint = constraint->next;
535 }
536
537out:
James Morris0804d112008-06-06 18:40:29 +1000538 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return rc;
540}
541
542/**
543 * security_compute_av - Compute access vector decisions.
544 * @ssid: source security identifier
545 * @tsid: target security identifier
546 * @tclass: target security class
547 * @requested: requested permissions
548 * @avd: access vector decisions
549 *
550 * Compute a set of access vector decisions based on the
551 * SID pair (@ssid, @tsid) for the permissions in @tclass.
552 * Return -%EINVAL if any of the parameters are invalid or %0
553 * if the access vector decisions were computed successfully.
554 */
555int security_compute_av(u32 ssid,
556 u32 tsid,
557 u16 tclass,
558 u32 requested,
559 struct av_decision *avd)
560{
561 struct context *scontext = NULL, *tcontext = NULL;
562 int rc = 0;
563
564 if (!ss_initialized) {
Stephen Smalley4c443d12005-05-16 21:53:52 -0700565 avd->allowed = 0xffffffff;
566 avd->decided = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 avd->auditallow = 0;
568 avd->auditdeny = 0xffffffff;
569 avd->seqno = latest_granting;
570 return 0;
571 }
572
James Morris0804d112008-06-06 18:40:29 +1000573 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 scontext = sidtab_search(&sidtab, ssid);
576 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400577 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
578 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 rc = -EINVAL;
580 goto out;
581 }
582 tcontext = sidtab_search(&sidtab, tsid);
583 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400584 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
585 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 rc = -EINVAL;
587 goto out;
588 }
589
590 rc = context_struct_compute_av(scontext, tcontext, tclass,
591 requested, avd);
592out:
James Morris0804d112008-06-06 18:40:29 +1000593 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return rc;
595}
596
597/*
598 * Write the security context string representation of
599 * the context structure `context' into a dynamically
600 * allocated string of the correct size. Set `*scontext'
601 * to point to this string and set `*scontext_len' to
602 * the length of the string.
603 */
604static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
605{
606 char *scontextp;
607
608 *scontext = NULL;
609 *scontext_len = 0;
610
Stephen Smalley12b29f32008-05-07 13:03:20 -0400611 if (context->len) {
612 *scontext_len = context->len;
613 *scontext = kstrdup(context->str, GFP_ATOMIC);
614 if (!(*scontext))
615 return -ENOMEM;
616 return 0;
617 }
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* Compute the size of the context. */
620 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
621 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
622 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
623 *scontext_len += mls_compute_context_len(context);
624
625 /* Allocate space for the context; caller must free this space. */
626 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -0400627 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 *scontext = scontextp;
630
631 /*
632 * Copy the user name, role name and type name into the context.
633 */
634 sprintf(scontextp, "%s:%s:%s",
635 policydb.p_user_val_to_name[context->user - 1],
636 policydb.p_role_val_to_name[context->role - 1],
637 policydb.p_type_val_to_name[context->type - 1]);
638 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
Eric Paris5d55a342008-04-18 17:38:33 -0400639 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
640 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 mls_sid_to_context(context, &scontextp);
643
644 *scontextp = 0;
645
646 return 0;
647}
648
649#include "initial_sid_to_string.h"
650
James Carterf0ee2e42007-04-04 10:11:29 -0400651const char *security_get_initial_sid_context(u32 sid)
652{
653 if (unlikely(sid > SECINITSID_NUM))
654 return NULL;
655 return initial_sid_to_string[sid];
656}
657
Stephen Smalley12b29f32008-05-07 13:03:20 -0400658static int security_sid_to_context_core(u32 sid, char **scontext,
659 u32 *scontext_len, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct context *context;
662 int rc = 0;
663
Stephen Smalley4f4acf32007-02-26 12:02:34 -0500664 *scontext = NULL;
665 *scontext_len = 0;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!ss_initialized) {
668 if (sid <= SECINITSID_NUM) {
669 char *scontextp;
670
671 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -0400672 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca02006-05-15 09:43:48 -0700673 if (!scontextp) {
674 rc = -ENOMEM;
675 goto out;
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 strcpy(scontextp, initial_sid_to_string[sid]);
678 *scontext = scontextp;
679 goto out;
680 }
Eric Paris744ba352008-04-17 11:52:44 -0400681 printk(KERN_ERR "SELinux: %s: called before initial "
682 "load_policy on unknown SID %d\n", __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 rc = -EINVAL;
684 goto out;
685 }
James Morris0804d112008-06-06 18:40:29 +1000686 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400687 if (force)
688 context = sidtab_search_force(&sidtab, sid);
689 else
690 context = sidtab_search(&sidtab, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (!context) {
Eric Paris744ba352008-04-17 11:52:44 -0400692 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
693 __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 rc = -EINVAL;
695 goto out_unlock;
696 }
697 rc = context_struct_to_string(context, scontext, scontext_len);
698out_unlock:
James Morris0804d112008-06-06 18:40:29 +1000699 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700out:
701 return rc;
702
703}
704
Stephen Smalley12b29f32008-05-07 13:03:20 -0400705/**
706 * security_sid_to_context - Obtain a context for a given SID.
707 * @sid: security identifier, SID
708 * @scontext: security context
709 * @scontext_len: length in bytes
710 *
711 * Write the string representation of the context associated with @sid
712 * into a dynamically allocated string of the correct size. Set @scontext
713 * to point to this string and set @scontext_len to the length of the string.
714 */
715int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Stephen Smalley12b29f32008-05-07 13:03:20 -0400717 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
718}
719
720int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
721{
722 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
723}
724
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400725/*
726 * Caveat: Mutates scontext.
727 */
Stephen Smalley12b29f32008-05-07 13:03:20 -0400728static int string_to_context_struct(struct policydb *pol,
729 struct sidtab *sidtabp,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400730 char *scontext,
Stephen Smalley12b29f32008-05-07 13:03:20 -0400731 u32 scontext_len,
732 struct context *ctx,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400733 u32 def_sid)
Stephen Smalley12b29f32008-05-07 13:03:20 -0400734{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct role_datum *role;
736 struct type_datum *typdatum;
737 struct user_datum *usrdatum;
738 char *scontextp, *p, oldc;
739 int rc = 0;
740
Stephen Smalley12b29f32008-05-07 13:03:20 -0400741 context_init(ctx);
742
Stephen Smalley12b29f32008-05-07 13:03:20 -0400743 /* Parse the security context. */
744
745 rc = -EINVAL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400746 scontextp = (char *) scontext;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400747
748 /* Extract the user. */
749 p = scontextp;
750 while (*p && *p != ':')
751 p++;
752
753 if (*p == 0)
754 goto out;
755
756 *p++ = 0;
757
758 usrdatum = hashtab_search(pol->p_users.table, scontextp);
759 if (!usrdatum)
760 goto out;
761
762 ctx->user = usrdatum->value;
763
764 /* Extract role. */
765 scontextp = p;
766 while (*p && *p != ':')
767 p++;
768
769 if (*p == 0)
770 goto out;
771
772 *p++ = 0;
773
774 role = hashtab_search(pol->p_roles.table, scontextp);
775 if (!role)
776 goto out;
777 ctx->role = role->value;
778
779 /* Extract type. */
780 scontextp = p;
781 while (*p && *p != ':')
782 p++;
783 oldc = *p;
784 *p++ = 0;
785
786 typdatum = hashtab_search(pol->p_types.table, scontextp);
787 if (!typdatum)
788 goto out;
789
790 ctx->type = typdatum->value;
791
792 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
793 if (rc)
794 goto out;
795
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400796 if ((p - scontext) < scontext_len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -0400797 rc = -EINVAL;
798 goto out;
799 }
800
801 /* Check the validity of the new context. */
802 if (!policydb_context_isvalid(pol, ctx)) {
803 rc = -EINVAL;
804 context_destroy(ctx);
805 goto out;
806 }
807 rc = 0;
808out:
Stephen Smalley12b29f32008-05-07 13:03:20 -0400809 return rc;
810}
811
812static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
813 u32 *sid, u32 def_sid, gfp_t gfp_flags,
814 int force)
815{
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400816 char *scontext2, *str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400817 struct context context;
818 int rc = 0;
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!ss_initialized) {
821 int i;
822
823 for (i = 1; i < SECINITSID_NUM; i++) {
824 if (!strcmp(initial_sid_to_string[i], scontext)) {
825 *sid = i;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828 }
829 *sid = SECINITSID_KERNEL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 *sid = SECSID_NULL;
833
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400834 /* Copy the string so that we can modify the copy as we parse it. */
835 scontext2 = kmalloc(scontext_len+1, gfp_flags);
836 if (!scontext2)
837 return -ENOMEM;
838 memcpy(scontext2, scontext, scontext_len);
839 scontext2[scontext_len] = 0;
840
841 if (force) {
842 /* Save another copy for storing in uninterpreted form */
843 str = kstrdup(scontext2, gfp_flags);
844 if (!str) {
845 kfree(scontext2);
846 return -ENOMEM;
847 }
848 }
849
James Morris0804d112008-06-06 18:40:29 +1000850 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400851 rc = string_to_context_struct(&policydb, &sidtab,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400852 scontext2, scontext_len,
853 &context, def_sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400854 if (rc == -EINVAL && force) {
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400855 context.str = str;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400856 context.len = scontext_len;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400857 str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400858 } else if (rc)
859 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 rc = sidtab_context_to_sid(&sidtab, &context, sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400861 if (rc)
862 context_destroy(&context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863out:
James Morris0804d112008-06-06 18:40:29 +1000864 read_unlock(&policy_rwlock);
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400865 kfree(scontext2);
866 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return rc;
868}
869
James Morrisf5c1d5b2005-07-28 01:07:37 -0700870/**
871 * security_context_to_sid - Obtain a SID for a given security context.
872 * @scontext: security context
873 * @scontext_len: length in bytes
874 * @sid: security identifier, SID
875 *
876 * Obtains a SID associated with the security context that
877 * has the string representation specified by @scontext.
878 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
879 * memory is available, or 0 on success.
880 */
David Howells8f0cfa52008-04-29 00:59:41 -0700881int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
James Morrisf5c1d5b2005-07-28 01:07:37 -0700882{
883 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -0400884 sid, SECSID_NULL, GFP_KERNEL, 0);
James Morrisf5c1d5b2005-07-28 01:07:37 -0700885}
886
887/**
888 * security_context_to_sid_default - Obtain a SID for a given security context,
889 * falling back to specified default if needed.
890 *
891 * @scontext: security context
892 * @scontext_len: length in bytes
893 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -0700894 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -0700895 *
896 * Obtains a SID associated with the security context that
897 * has the string representation specified by @scontext.
898 * The default SID is passed to the MLS layer to be used to allow
899 * kernel labeling of the MLS field if the MLS field is not present
900 * (for upgrading to MLS without full relabel).
Stephen Smalley12b29f32008-05-07 13:03:20 -0400901 * Implicitly forces adding of the context even if it cannot be mapped yet.
James Morrisf5c1d5b2005-07-28 01:07:37 -0700902 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
903 * memory is available, or 0 on success.
904 */
David Howells7bf570d2008-04-29 20:52:51 +0100905int security_context_to_sid_default(const char *scontext, u32 scontext_len,
906 u32 *sid, u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -0700907{
908 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -0400909 sid, def_sid, gfp_flags, 1);
910}
911
912int security_context_to_sid_force(const char *scontext, u32 scontext_len,
913 u32 *sid)
914{
915 return security_context_to_sid_core(scontext, scontext_len,
916 sid, SECSID_NULL, GFP_KERNEL, 1);
James Morrisf5c1d5b2005-07-28 01:07:37 -0700917}
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919static int compute_sid_handle_invalid_context(
920 struct context *scontext,
921 struct context *tcontext,
922 u16 tclass,
923 struct context *newcontext)
924{
925 char *s = NULL, *t = NULL, *n = NULL;
926 u32 slen, tlen, nlen;
927
928 if (context_struct_to_string(scontext, &s, &slen) < 0)
929 goto out;
930 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
931 goto out;
932 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
933 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100934 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 "security_compute_sid: invalid context %s"
936 " for scontext=%s"
937 " tcontext=%s"
938 " tclass=%s",
939 n, s, t, policydb.p_class_val_to_name[tclass-1]);
940out:
941 kfree(s);
942 kfree(t);
943 kfree(n);
944 if (!selinux_enforcing)
945 return 0;
946 return -EACCES;
947}
948
949static int security_compute_sid(u32 ssid,
950 u32 tsid,
951 u16 tclass,
952 u32 specified,
953 u32 *out_sid)
954{
955 struct context *scontext = NULL, *tcontext = NULL, newcontext;
956 struct role_trans *roletr = NULL;
957 struct avtab_key avkey;
958 struct avtab_datum *avdatum;
959 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int rc = 0;
961
962 if (!ss_initialized) {
963 switch (tclass) {
964 case SECCLASS_PROCESS:
965 *out_sid = ssid;
966 break;
967 default:
968 *out_sid = tsid;
969 break;
970 }
971 goto out;
972 }
973
Venkat Yekkirala851f8a62006-07-30 03:03:18 -0700974 context_init(&newcontext);
975
James Morris0804d112008-06-06 18:40:29 +1000976 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 scontext = sidtab_search(&sidtab, ssid);
979 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400980 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
981 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 rc = -EINVAL;
983 goto out_unlock;
984 }
985 tcontext = sidtab_search(&sidtab, tsid);
986 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400987 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
988 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 rc = -EINVAL;
990 goto out_unlock;
991 }
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /* Set the user identity. */
994 switch (specified) {
995 case AVTAB_TRANSITION:
996 case AVTAB_CHANGE:
997 /* Use the process user identity. */
998 newcontext.user = scontext->user;
999 break;
1000 case AVTAB_MEMBER:
1001 /* Use the related object owner. */
1002 newcontext.user = tcontext->user;
1003 break;
1004 }
1005
1006 /* Set the role and type to default values. */
1007 switch (tclass) {
1008 case SECCLASS_PROCESS:
1009 /* Use the current role and type of process. */
1010 newcontext.role = scontext->role;
1011 newcontext.type = scontext->type;
1012 break;
1013 default:
1014 /* Use the well-defined object role. */
1015 newcontext.role = OBJECT_R_VAL;
1016 /* Use the type of the related object. */
1017 newcontext.type = tcontext->type;
1018 }
1019
1020 /* Look for a type transition/member/change rule. */
1021 avkey.source_type = scontext->type;
1022 avkey.target_type = tcontext->type;
1023 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001024 avkey.specified = specified;
1025 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -04001028 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001029 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001031 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 avdatum = &node->datum;
1033 break;
1034 }
1035 }
1036 }
1037
Stephen Smalley782ebb92005-09-03 15:55:16 -07001038 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -07001040 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
1043 /* Check for class-specific changes. */
1044 switch (tclass) {
1045 case SECCLASS_PROCESS:
1046 if (specified & AVTAB_TRANSITION) {
1047 /* Look for a role transition rule. */
1048 for (roletr = policydb.role_tr; roletr;
1049 roletr = roletr->next) {
1050 if (roletr->role == scontext->role &&
1051 roletr->type == tcontext->type) {
1052 /* Use the role transition rule. */
1053 newcontext.role = roletr->new_role;
1054 break;
1055 }
1056 }
1057 }
1058 break;
1059 default:
1060 break;
1061 }
1062
1063 /* Set the MLS attributes.
1064 This is done last because it may allocate memory. */
1065 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1066 if (rc)
1067 goto out_unlock;
1068
1069 /* Check the validity of the context. */
1070 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1071 rc = compute_sid_handle_invalid_context(scontext,
1072 tcontext,
1073 tclass,
1074 &newcontext);
1075 if (rc)
1076 goto out_unlock;
1077 }
1078 /* Obtain the sid for the context. */
1079 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1080out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001081 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 context_destroy(&newcontext);
1083out:
1084 return rc;
1085}
1086
1087/**
1088 * security_transition_sid - Compute the SID for a new subject/object.
1089 * @ssid: source security identifier
1090 * @tsid: target security identifier
1091 * @tclass: target security class
1092 * @out_sid: security identifier for new subject/object
1093 *
1094 * Compute a SID to use for labeling a new subject or object in the
1095 * class @tclass based on a SID pair (@ssid, @tsid).
1096 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1097 * if insufficient memory is available, or %0 if the new SID was
1098 * computed successfully.
1099 */
1100int security_transition_sid(u32 ssid,
1101 u32 tsid,
1102 u16 tclass,
1103 u32 *out_sid)
1104{
1105 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1106}
1107
1108/**
1109 * security_member_sid - Compute the SID for member selection.
1110 * @ssid: source security identifier
1111 * @tsid: target security identifier
1112 * @tclass: target security class
1113 * @out_sid: security identifier for selected member
1114 *
1115 * Compute a SID to use when selecting a member of a polyinstantiated
1116 * object of class @tclass based on a SID pair (@ssid, @tsid).
1117 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1118 * if insufficient memory is available, or %0 if the SID was
1119 * computed successfully.
1120 */
1121int security_member_sid(u32 ssid,
1122 u32 tsid,
1123 u16 tclass,
1124 u32 *out_sid)
1125{
1126 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1127}
1128
1129/**
1130 * security_change_sid - Compute the SID for object relabeling.
1131 * @ssid: source security identifier
1132 * @tsid: target security identifier
1133 * @tclass: target security class
1134 * @out_sid: security identifier for selected member
1135 *
1136 * Compute a SID to use for relabeling an object of class @tclass
1137 * based on a SID pair (@ssid, @tsid).
1138 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1139 * if insufficient memory is available, or %0 if the SID was
1140 * computed successfully.
1141 */
1142int security_change_sid(u32 ssid,
1143 u32 tsid,
1144 u16 tclass,
1145 u32 *out_sid)
1146{
1147 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1148}
1149
Chad Sellersb94c7e62006-11-06 12:38:18 -05001150/*
1151 * Verify that each kernel class that is defined in the
1152 * policy is correct
1153 */
1154static int validate_classes(struct policydb *p)
1155{
1156 int i, j;
1157 struct class_datum *cladatum;
1158 struct perm_datum *perdatum;
1159 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1160 u16 class_val;
1161 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1162 const char *def_class, *def_perm, *pol_class;
1163 struct symtab *perms;
1164
Eric Paris3f120702007-09-21 14:37:10 -04001165 if (p->allow_unknown) {
1166 u32 num_classes = kdefs->cts_len;
1167 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1168 if (!p->undefined_perms)
1169 return -ENOMEM;
1170 }
1171
Chad Sellersb94c7e62006-11-06 12:38:18 -05001172 for (i = 1; i < kdefs->cts_len; i++) {
1173 def_class = kdefs->class_to_string[i];
Stephen Smalleya764ae42007-03-26 13:36:26 -04001174 if (!def_class)
1175 continue;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001176 if (i > p->p_classes.nprim) {
1177 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001178 "SELinux: class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001179 def_class);
Eric Paris3f120702007-09-21 14:37:10 -04001180 if (p->reject_unknown)
1181 return -EINVAL;
1182 if (p->allow_unknown)
1183 p->undefined_perms[i-1] = ~0U;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001184 continue;
1185 }
1186 pol_class = p->p_class_val_to_name[i-1];
1187 if (strcmp(pol_class, def_class)) {
1188 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001189 "SELinux: class %d is incorrect, found %s but should be %s\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001190 i, pol_class, def_class);
1191 return -EINVAL;
1192 }
1193 }
1194 for (i = 0; i < kdefs->av_pts_len; i++) {
1195 class_val = kdefs->av_perm_to_string[i].tclass;
1196 perm_val = kdefs->av_perm_to_string[i].value;
1197 def_perm = kdefs->av_perm_to_string[i].name;
1198 if (class_val > p->p_classes.nprim)
1199 continue;
1200 pol_class = p->p_class_val_to_name[class_val-1];
1201 cladatum = hashtab_search(p->p_classes.table, pol_class);
1202 BUG_ON(!cladatum);
1203 perms = &cladatum->permissions;
1204 nprim = 1 << (perms->nprim - 1);
1205 if (perm_val > nprim) {
1206 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001207 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001208 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001209 if (p->reject_unknown)
1210 return -EINVAL;
1211 if (p->allow_unknown)
1212 p->undefined_perms[class_val-1] |= perm_val;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001213 continue;
1214 }
1215 perdatum = hashtab_search(perms->table, def_perm);
1216 if (perdatum == NULL) {
1217 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001218 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001219 def_perm, pol_class);
1220 return -EINVAL;
1221 }
1222 pol_val = 1 << (perdatum->value - 1);
1223 if (pol_val != perm_val) {
1224 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001225 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001226 def_perm, pol_class);
1227 return -EINVAL;
1228 }
1229 }
1230 for (i = 0; i < kdefs->av_inherit_len; i++) {
1231 class_val = kdefs->av_inherit[i].tclass;
1232 if (class_val > p->p_classes.nprim)
1233 continue;
1234 pol_class = p->p_class_val_to_name[class_val-1];
1235 cladatum = hashtab_search(p->p_classes.table, pol_class);
1236 BUG_ON(!cladatum);
1237 if (!cladatum->comdatum) {
1238 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001239 "SELinux: class %s should have an inherits clause but does not\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001240 pol_class);
1241 return -EINVAL;
1242 }
1243 tmp = kdefs->av_inherit[i].common_base;
1244 common_pts_len = 0;
1245 while (!(tmp & 0x01)) {
1246 common_pts_len++;
1247 tmp >>= 1;
1248 }
1249 perms = &cladatum->comdatum->permissions;
1250 for (j = 0; j < common_pts_len; j++) {
1251 def_perm = kdefs->av_inherit[i].common_pts[j];
1252 if (j >= perms->nprim) {
1253 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001254 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001255 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001256 if (p->reject_unknown)
1257 return -EINVAL;
1258 if (p->allow_unknown)
1259 p->undefined_perms[class_val-1] |= (1 << j);
Chad Sellersb94c7e62006-11-06 12:38:18 -05001260 continue;
1261 }
1262 perdatum = hashtab_search(perms->table, def_perm);
1263 if (perdatum == NULL) {
1264 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001265 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001266 def_perm, pol_class);
1267 return -EINVAL;
1268 }
1269 if (perdatum->value != j + 1) {
1270 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001271 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001272 def_perm, pol_class);
1273 return -EINVAL;
1274 }
1275 }
1276 }
1277 return 0;
1278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280/* Clone the SID into the new SID table. */
1281static int clone_sid(u32 sid,
1282 struct context *context,
1283 void *arg)
1284{
1285 struct sidtab *s = arg;
1286
1287 return sidtab_insert(s, sid, context);
1288}
1289
1290static inline int convert_context_handle_invalid_context(struct context *context)
1291{
1292 int rc = 0;
1293
1294 if (selinux_enforcing) {
1295 rc = -EINVAL;
1296 } else {
1297 char *s;
1298 u32 len;
1299
Stephen Smalley12b29f32008-05-07 13:03:20 -04001300 if (!context_struct_to_string(context, &s, &len)) {
1301 printk(KERN_WARNING
1302 "SELinux: Context %s would be invalid if enforcing\n",
1303 s);
1304 kfree(s);
1305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307 return rc;
1308}
1309
1310struct convert_context_args {
1311 struct policydb *oldp;
1312 struct policydb *newp;
1313};
1314
1315/*
1316 * Convert the values in the security context
1317 * structure `c' from the values specified
1318 * in the policy `p->oldp' to the values specified
1319 * in the policy `p->newp'. Verify that the
1320 * context is valid under the new policy.
1321 */
1322static int convert_context(u32 key,
1323 struct context *c,
1324 void *p)
1325{
1326 struct convert_context_args *args;
1327 struct context oldc;
1328 struct role_datum *role;
1329 struct type_datum *typdatum;
1330 struct user_datum *usrdatum;
1331 char *s;
1332 u32 len;
1333 int rc;
1334
1335 args = p;
1336
Stephen Smalley12b29f32008-05-07 13:03:20 -04001337 if (c->str) {
1338 struct context ctx;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001339 s = kstrdup(c->str, GFP_KERNEL);
1340 if (!s) {
1341 rc = -ENOMEM;
1342 goto out;
1343 }
1344 rc = string_to_context_struct(args->newp, NULL, s,
1345 c->len, &ctx, SECSID_NULL);
1346 kfree(s);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001347 if (!rc) {
1348 printk(KERN_INFO
1349 "SELinux: Context %s became valid (mapped).\n",
1350 c->str);
1351 /* Replace string with mapped representation. */
1352 kfree(c->str);
1353 memcpy(c, &ctx, sizeof(*c));
1354 goto out;
1355 } else if (rc == -EINVAL) {
1356 /* Retain string representation for later mapping. */
1357 rc = 0;
1358 goto out;
1359 } else {
1360 /* Other error condition, e.g. ENOMEM. */
1361 printk(KERN_ERR
1362 "SELinux: Unable to map context %s, rc = %d.\n",
1363 c->str, -rc);
1364 goto out;
1365 }
1366 }
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 rc = context_cpy(&oldc, c);
1369 if (rc)
1370 goto out;
1371
1372 rc = -EINVAL;
1373
1374 /* Convert the user. */
1375 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001376 args->oldp->p_user_val_to_name[c->user - 1]);
1377 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 c->user = usrdatum->value;
1380
1381 /* Convert the role. */
1382 role = hashtab_search(args->newp->p_roles.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001383 args->oldp->p_role_val_to_name[c->role - 1]);
1384 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 c->role = role->value;
1387
1388 /* Convert the type. */
1389 typdatum = hashtab_search(args->newp->p_types.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001390 args->oldp->p_type_val_to_name[c->type - 1]);
1391 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 c->type = typdatum->value;
1394
1395 rc = mls_convert_context(args->oldp, args->newp, c);
1396 if (rc)
1397 goto bad;
1398
1399 /* Check the validity of the new context. */
1400 if (!policydb_context_isvalid(args->newp, c)) {
1401 rc = convert_context_handle_invalid_context(&oldc);
1402 if (rc)
1403 goto bad;
1404 }
1405
1406 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001407 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408out:
1409 return rc;
1410bad:
Stephen Smalley12b29f32008-05-07 13:03:20 -04001411 /* Map old representation to string and save it. */
1412 if (context_struct_to_string(&oldc, &s, &len))
1413 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001415 context_destroy(c);
1416 c->str = s;
1417 c->len = len;
1418 printk(KERN_INFO
1419 "SELinux: Context %s became invalid (unmapped).\n",
1420 c->str);
1421 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 goto out;
1423}
1424
Paul Moore3bb56b22008-01-29 08:38:19 -05001425static void security_load_policycaps(void)
1426{
1427 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1428 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05001429 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1430 POLICYDB_CAPABILITY_OPENPERM);
Paul Moore3bb56b22008-01-29 08:38:19 -05001431}
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433extern void selinux_complete_init(void);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001434static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436/**
1437 * security_load_policy - Load a security policy configuration.
1438 * @data: binary policy data
1439 * @len: length of data in bytes
1440 *
1441 * Load a new set of security policy configuration data,
1442 * validate it and convert the SID table as necessary.
1443 * This function will flush the access vector cache after
1444 * loading the new policy.
1445 */
1446int security_load_policy(void *data, size_t len)
1447{
1448 struct policydb oldpolicydb, newpolicydb;
1449 struct sidtab oldsidtab, newsidtab;
1450 struct convert_context_args args;
1451 u32 seqno;
1452 int rc = 0;
1453 struct policy_file file = { data, len }, *fp = &file;
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (!ss_initialized) {
1456 avtab_cache_init();
1457 if (policydb_read(&policydb, fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 avtab_cache_destroy();
1459 return -EINVAL;
1460 }
1461 if (policydb_load_isids(&policydb, &sidtab)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 policydb_destroy(&policydb);
1463 avtab_cache_destroy();
1464 return -EINVAL;
1465 }
Chad Sellersb94c7e62006-11-06 12:38:18 -05001466 /* Verify that the kernel defined classes are correct. */
1467 if (validate_classes(&policydb)) {
1468 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001469 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001470 sidtab_destroy(&sidtab);
1471 policydb_destroy(&policydb);
1472 avtab_cache_destroy();
1473 return -EINVAL;
1474 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001475 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 policydb_loaded_version = policydb.policyvers;
1477 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001478 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001480 avc_ss_reset(seqno);
1481 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001482 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001483 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return 0;
1485 }
1486
1487#if 0
1488 sidtab_hash_eval(&sidtab, "sids");
1489#endif
1490
Eric Paris89abd0a2008-06-09 15:58:04 -04001491 if (policydb_read(&newpolicydb, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Stephen Smalley12b29f32008-05-07 13:03:20 -04001494 if (sidtab_init(&newsidtab)) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04001495 policydb_destroy(&newpolicydb);
1496 return -ENOMEM;
1497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Chad Sellersb94c7e62006-11-06 12:38:18 -05001499 /* Verify that the kernel defined classes are correct. */
1500 if (validate_classes(&newpolicydb)) {
1501 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001502 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001503 rc = -EINVAL;
1504 goto err;
1505 }
1506
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001507 rc = security_preserve_bools(&newpolicydb);
1508 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001509 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001510 goto err;
1511 }
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /* Clone the SID table. */
1514 sidtab_shutdown(&sidtab);
1515 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1516 rc = -ENOMEM;
1517 goto err;
1518 }
1519
Stephen Smalley12b29f32008-05-07 13:03:20 -04001520 /*
1521 * Convert the internal representations of contexts
1522 * in the new SID table.
1523 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 args.oldp = &policydb;
1525 args.newp = &newpolicydb;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001526 rc = sidtab_map(&newsidtab, convert_context, &args);
1527 if (rc)
1528 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 /* Save the old policydb and SID table to free later. */
1531 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1532 sidtab_set(&oldsidtab, &sidtab);
1533
1534 /* Install the new policydb and SID table. */
James Morris0804d112008-06-06 18:40:29 +10001535 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 memcpy(&policydb, &newpolicydb, sizeof policydb);
1537 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05001538 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 seqno = ++latest_granting;
1540 policydb_loaded_version = policydb.policyvers;
James Morris0804d112008-06-06 18:40:29 +10001541 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 /* Free the old policydb and SID table. */
1544 policydb_destroy(&oldpolicydb);
1545 sidtab_destroy(&oldsidtab);
1546
1547 avc_ss_reset(seqno);
1548 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001549 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001550 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 return 0;
1553
1554err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 sidtab_destroy(&newsidtab);
1556 policydb_destroy(&newpolicydb);
1557 return rc;
1558
1559}
1560
1561/**
1562 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 * @protocol: protocol number
1564 * @port: port number
1565 * @out_sid: security identifier
1566 */
Paul Moore3e112172008-04-10 10:48:14 -04001567int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 struct ocontext *c;
1570 int rc = 0;
1571
James Morris0804d112008-06-06 18:40:29 +10001572 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 c = policydb.ocontexts[OCON_PORT];
1575 while (c) {
1576 if (c->u.port.protocol == protocol &&
1577 c->u.port.low_port <= port &&
1578 c->u.port.high_port >= port)
1579 break;
1580 c = c->next;
1581 }
1582
1583 if (c) {
1584 if (!c->sid[0]) {
1585 rc = sidtab_context_to_sid(&sidtab,
1586 &c->context[0],
1587 &c->sid[0]);
1588 if (rc)
1589 goto out;
1590 }
1591 *out_sid = c->sid[0];
1592 } else {
1593 *out_sid = SECINITSID_PORT;
1594 }
1595
1596out:
James Morris0804d112008-06-06 18:40:29 +10001597 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return rc;
1599}
1600
1601/**
1602 * security_netif_sid - Obtain the SID for a network interface.
1603 * @name: interface name
1604 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05001606int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 int rc = 0;
1609 struct ocontext *c;
1610
James Morris0804d112008-06-06 18:40:29 +10001611 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 c = policydb.ocontexts[OCON_NETIF];
1614 while (c) {
1615 if (strcmp(name, c->u.name) == 0)
1616 break;
1617 c = c->next;
1618 }
1619
1620 if (c) {
1621 if (!c->sid[0] || !c->sid[1]) {
1622 rc = sidtab_context_to_sid(&sidtab,
1623 &c->context[0],
1624 &c->sid[0]);
1625 if (rc)
1626 goto out;
1627 rc = sidtab_context_to_sid(&sidtab,
1628 &c->context[1],
1629 &c->sid[1]);
1630 if (rc)
1631 goto out;
1632 }
1633 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05001634 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637out:
James Morris0804d112008-06-06 18:40:29 +10001638 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return rc;
1640}
1641
1642static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1643{
1644 int i, fail = 0;
1645
Eric Paris5d55a342008-04-18 17:38:33 -04001646 for (i = 0; i < 4; i++)
1647 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 fail = 1;
1649 break;
1650 }
1651
1652 return !fail;
1653}
1654
1655/**
1656 * security_node_sid - Obtain the SID for a node (host).
1657 * @domain: communication domain aka address family
1658 * @addrp: address
1659 * @addrlen: address length in bytes
1660 * @out_sid: security identifier
1661 */
1662int security_node_sid(u16 domain,
1663 void *addrp,
1664 u32 addrlen,
1665 u32 *out_sid)
1666{
1667 int rc = 0;
1668 struct ocontext *c;
1669
James Morris0804d112008-06-06 18:40:29 +10001670 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 switch (domain) {
1673 case AF_INET: {
1674 u32 addr;
1675
1676 if (addrlen != sizeof(u32)) {
1677 rc = -EINVAL;
1678 goto out;
1679 }
1680
1681 addr = *((u32 *)addrp);
1682
1683 c = policydb.ocontexts[OCON_NODE];
1684 while (c) {
1685 if (c->u.node.addr == (addr & c->u.node.mask))
1686 break;
1687 c = c->next;
1688 }
1689 break;
1690 }
1691
1692 case AF_INET6:
1693 if (addrlen != sizeof(u64) * 2) {
1694 rc = -EINVAL;
1695 goto out;
1696 }
1697 c = policydb.ocontexts[OCON_NODE6];
1698 while (c) {
1699 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1700 c->u.node6.mask))
1701 break;
1702 c = c->next;
1703 }
1704 break;
1705
1706 default:
1707 *out_sid = SECINITSID_NODE;
1708 goto out;
1709 }
1710
1711 if (c) {
1712 if (!c->sid[0]) {
1713 rc = sidtab_context_to_sid(&sidtab,
1714 &c->context[0],
1715 &c->sid[0]);
1716 if (rc)
1717 goto out;
1718 }
1719 *out_sid = c->sid[0];
1720 } else {
1721 *out_sid = SECINITSID_NODE;
1722 }
1723
1724out:
James Morris0804d112008-06-06 18:40:29 +10001725 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return rc;
1727}
1728
1729#define SIDS_NEL 25
1730
1731/**
1732 * security_get_user_sids - Obtain reachable SIDs for a user.
1733 * @fromsid: starting SID
1734 * @username: username
1735 * @sids: array of reachable SIDs for user
1736 * @nel: number of elements in @sids
1737 *
1738 * Generate the set of SIDs for legal security contexts
1739 * for a given user that can be reached by @fromsid.
1740 * Set *@sids to point to a dynamically allocated
1741 * array containing the set of SIDs. Set *@nel to the
1742 * number of elements in the array.
1743 */
1744
1745int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04001746 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 u32 **sids,
1748 u32 *nel)
1749{
1750 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001751 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 u32 mynel = 0, maxnel = SIDS_NEL;
1753 struct user_datum *user;
1754 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001755 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 int rc = 0, i, j;
1757
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001758 *sids = NULL;
1759 *nel = 0;
1760
1761 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
James Morris0804d112008-06-06 18:40:29 +10001764 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Stephen Smalley12b29f32008-05-07 13:03:20 -04001766 context_init(&usercon);
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 fromcon = sidtab_search(&sidtab, fromsid);
1769 if (!fromcon) {
1770 rc = -EINVAL;
1771 goto out_unlock;
1772 }
1773
1774 user = hashtab_search(policydb.p_users.table, username);
1775 if (!user) {
1776 rc = -EINVAL;
1777 goto out_unlock;
1778 }
1779 usercon.user = user->value;
1780
James Morris89d155e2005-10-30 14:59:21 -08001781 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (!mysids) {
1783 rc = -ENOMEM;
1784 goto out_unlock;
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001787 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 role = policydb.role_val_to_struct[i];
1789 usercon.role = i+1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001790 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 usercon.type = j+1;
1792
1793 if (mls_setup_user_range(fromcon, user, &usercon))
1794 continue;
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001797 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (mynel < maxnel) {
1800 mysids[mynel++] = sid;
1801 } else {
1802 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08001803 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (!mysids2) {
1805 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto out_unlock;
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1809 kfree(mysids);
1810 mysids = mysids2;
1811 mysids[mynel++] = sid;
1812 }
1813 }
1814 }
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001817 read_unlock(&policy_rwlock);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001818 if (rc || !mynel) {
1819 kfree(mysids);
1820 goto out;
1821 }
1822
1823 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1824 if (!mysids2) {
1825 rc = -ENOMEM;
1826 kfree(mysids);
1827 goto out;
1828 }
1829 for (i = 0, j = 0; i < mynel; i++) {
1830 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1831 SECCLASS_PROCESS,
1832 PROCESS__TRANSITION, AVC_STRICT,
1833 NULL);
1834 if (!rc)
1835 mysids2[j++] = mysids[i];
1836 cond_resched();
1837 }
1838 rc = 0;
1839 kfree(mysids);
1840 *sids = mysids2;
1841 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842out:
1843 return rc;
1844}
1845
1846/**
1847 * security_genfs_sid - Obtain a SID for a file in a filesystem
1848 * @fstype: filesystem type
1849 * @path: path from root of mount
1850 * @sclass: file security class
1851 * @sid: SID for path
1852 *
1853 * Obtain a SID to use for a file in a filesystem that
1854 * cannot support xattr or use a fixed labeling behavior like
1855 * transition SIDs or task SIDs.
1856 */
1857int security_genfs_sid(const char *fstype,
Eric Paris5d55a342008-04-18 17:38:33 -04001858 char *path,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 u16 sclass,
1860 u32 *sid)
1861{
1862 int len;
1863 struct genfs *genfs;
1864 struct ocontext *c;
1865 int rc = 0, cmp = 0;
1866
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05001867 while (path[0] == '/' && path[1] == '/')
1868 path++;
1869
James Morris0804d112008-06-06 18:40:29 +10001870 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1873 cmp = strcmp(fstype, genfs->fstype);
1874 if (cmp <= 0)
1875 break;
1876 }
1877
1878 if (!genfs || cmp) {
1879 *sid = SECINITSID_UNLABELED;
1880 rc = -ENOENT;
1881 goto out;
1882 }
1883
1884 for (c = genfs->head; c; c = c->next) {
1885 len = strlen(c->u.name);
1886 if ((!c->v.sclass || sclass == c->v.sclass) &&
1887 (strncmp(c->u.name, path, len) == 0))
1888 break;
1889 }
1890
1891 if (!c) {
1892 *sid = SECINITSID_UNLABELED;
1893 rc = -ENOENT;
1894 goto out;
1895 }
1896
1897 if (!c->sid[0]) {
1898 rc = sidtab_context_to_sid(&sidtab,
1899 &c->context[0],
1900 &c->sid[0]);
1901 if (rc)
1902 goto out;
1903 }
1904
1905 *sid = c->sid[0];
1906out:
James Morris0804d112008-06-06 18:40:29 +10001907 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return rc;
1909}
1910
1911/**
1912 * security_fs_use - Determine how to handle labeling for a filesystem.
1913 * @fstype: filesystem type
1914 * @behavior: labeling behavior
1915 * @sid: SID for filesystem (superblock)
1916 */
1917int security_fs_use(
1918 const char *fstype,
1919 unsigned int *behavior,
1920 u32 *sid)
1921{
1922 int rc = 0;
1923 struct ocontext *c;
1924
James Morris0804d112008-06-06 18:40:29 +10001925 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 c = policydb.ocontexts[OCON_FSUSE];
1928 while (c) {
1929 if (strcmp(fstype, c->u.name) == 0)
1930 break;
1931 c = c->next;
1932 }
1933
1934 if (c) {
1935 *behavior = c->v.behavior;
1936 if (!c->sid[0]) {
1937 rc = sidtab_context_to_sid(&sidtab,
1938 &c->context[0],
1939 &c->sid[0]);
1940 if (rc)
1941 goto out;
1942 }
1943 *sid = c->sid[0];
1944 } else {
1945 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1946 if (rc) {
1947 *behavior = SECURITY_FS_USE_NONE;
1948 rc = 0;
1949 } else {
1950 *behavior = SECURITY_FS_USE_GENFS;
1951 }
1952 }
1953
1954out:
James Morris0804d112008-06-06 18:40:29 +10001955 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return rc;
1957}
1958
1959int security_get_bools(int *len, char ***names, int **values)
1960{
1961 int i, rc = -ENOMEM;
1962
James Morris0804d112008-06-06 18:40:29 +10001963 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 *names = NULL;
1965 *values = NULL;
1966
1967 *len = policydb.p_bools.nprim;
1968 if (!*len) {
1969 rc = 0;
1970 goto out;
1971 }
1972
Eric Paris5d55a342008-04-18 17:38:33 -04001973 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (!*names)
1975 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Jesper Juhle0795cf2006-01-09 20:54:46 -08001977 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (!*values)
1979 goto err;
1980
1981 for (i = 0; i < *len; i++) {
1982 size_t name_len;
1983 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1984 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04001985 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (!(*names)[i])
1987 goto err;
1988 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1989 (*names)[i][name_len - 1] = 0;
1990 }
1991 rc = 0;
1992out:
James Morris0804d112008-06-06 18:40:29 +10001993 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return rc;
1995err:
1996 if (*names) {
1997 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001998 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002000 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 goto out;
2002}
2003
2004
2005int security_set_bools(int len, int *values)
2006{
2007 int i, rc = 0;
2008 int lenp, seqno = 0;
2009 struct cond_node *cur;
2010
James Morris0804d112008-06-06 18:40:29 +10002011 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 lenp = policydb.p_bools.nprim;
2014 if (len != lenp) {
2015 rc = -EFAULT;
2016 goto out;
2017 }
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00002020 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2021 audit_log(current->audit_context, GFP_ATOMIC,
2022 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05002023 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Steve Grubbaf601e42006-01-04 14:08:39 +00002024 policydb.p_bool_val_to_name[i],
2025 !!values[i],
2026 policydb.bool_val_to_struct[i]->state,
Eric Paris4746ec52008-01-08 10:06:53 -05002027 audit_get_loginuid(current),
2028 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00002029 }
Eric Paris5d55a342008-04-18 17:38:33 -04002030 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002032 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
2037 rc = evaluate_cond_node(&policydb, cur);
2038 if (rc)
2039 goto out;
2040 }
2041
2042 seqno = ++latest_granting;
2043
2044out:
James Morris0804d112008-06-06 18:40:29 +10002045 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (!rc) {
2047 avc_ss_reset(seqno);
2048 selnl_notify_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002049 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051 return rc;
2052}
2053
2054int security_get_bool_value(int bool)
2055{
2056 int rc = 0;
2057 int len;
2058
James Morris0804d112008-06-06 18:40:29 +10002059 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 len = policydb.p_bools.nprim;
2062 if (bool >= len) {
2063 rc = -EFAULT;
2064 goto out;
2065 }
2066
2067 rc = policydb.bool_val_to_struct[bool]->state;
2068out:
James Morris0804d112008-06-06 18:40:29 +10002069 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return rc;
2071}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002072
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002073static int security_preserve_bools(struct policydb *p)
2074{
2075 int rc, nbools = 0, *bvalues = NULL, i;
2076 char **bnames = NULL;
2077 struct cond_bool_datum *booldatum;
2078 struct cond_node *cur;
2079
2080 rc = security_get_bools(&nbools, &bnames, &bvalues);
2081 if (rc)
2082 goto out;
2083 for (i = 0; i < nbools; i++) {
2084 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2085 if (booldatum)
2086 booldatum->state = bvalues[i];
2087 }
2088 for (cur = p->cond_list; cur != NULL; cur = cur->next) {
2089 rc = evaluate_cond_node(p, cur);
2090 if (rc)
2091 goto out;
2092 }
2093
2094out:
2095 if (bnames) {
2096 for (i = 0; i < nbools; i++)
2097 kfree(bnames[i]);
2098 }
2099 kfree(bnames);
2100 kfree(bvalues);
2101 return rc;
2102}
2103
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002104/*
2105 * security_sid_mls_copy() - computes a new sid based on the given
2106 * sid and the mls portion of mls_sid.
2107 */
2108int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2109{
2110 struct context *context1;
2111 struct context *context2;
2112 struct context newcon;
2113 char *s;
2114 u32 len;
2115 int rc = 0;
2116
Venkat Yekkirala4eb327b2006-09-19 10:24:19 -07002117 if (!ss_initialized || !selinux_mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002118 *new_sid = sid;
2119 goto out;
2120 }
2121
2122 context_init(&newcon);
2123
James Morris0804d112008-06-06 18:40:29 +10002124 read_lock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002125 context1 = sidtab_search(&sidtab, sid);
2126 if (!context1) {
Eric Paris744ba352008-04-17 11:52:44 -04002127 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2128 __func__, sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002129 rc = -EINVAL;
2130 goto out_unlock;
2131 }
2132
2133 context2 = sidtab_search(&sidtab, mls_sid);
2134 if (!context2) {
Eric Paris744ba352008-04-17 11:52:44 -04002135 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2136 __func__, mls_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002137 rc = -EINVAL;
2138 goto out_unlock;
2139 }
2140
2141 newcon.user = context1->user;
2142 newcon.role = context1->role;
2143 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002144 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002145 if (rc)
2146 goto out_unlock;
2147
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002148 /* Check the validity of the new context. */
2149 if (!policydb_context_isvalid(&policydb, &newcon)) {
2150 rc = convert_context_handle_invalid_context(&newcon);
2151 if (rc)
2152 goto bad;
2153 }
2154
2155 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2156 goto out_unlock;
2157
2158bad:
2159 if (!context_struct_to_string(&newcon, &s, &len)) {
2160 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2161 "security_sid_mls_copy: invalid context %s", s);
2162 kfree(s);
2163 }
2164
2165out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002166 read_unlock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002167 context_destroy(&newcon);
2168out:
2169 return rc;
2170}
2171
Paul Moore220deb92008-01-29 08:38:23 -05002172/**
2173 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2174 * @nlbl_sid: NetLabel SID
2175 * @nlbl_type: NetLabel labeling protocol type
2176 * @xfrm_sid: XFRM SID
2177 *
2178 * Description:
2179 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2180 * resolved into a single SID it is returned via @peer_sid and the function
2181 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2182 * returns a negative value. A table summarizing the behavior is below:
2183 *
2184 * | function return | @sid
2185 * ------------------------------+-----------------+-----------------
2186 * no peer labels | 0 | SECSID_NULL
2187 * single peer label | 0 | <peer_label>
2188 * multiple, consistent labels | 0 | <peer_label>
2189 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2190 *
2191 */
2192int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2193 u32 xfrm_sid,
2194 u32 *peer_sid)
2195{
2196 int rc;
2197 struct context *nlbl_ctx;
2198 struct context *xfrm_ctx;
2199
2200 /* handle the common (which also happens to be the set of easy) cases
2201 * right away, these two if statements catch everything involving a
2202 * single or absent peer SID/label */
2203 if (xfrm_sid == SECSID_NULL) {
2204 *peer_sid = nlbl_sid;
2205 return 0;
2206 }
2207 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2208 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2209 * is present */
2210 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2211 *peer_sid = xfrm_sid;
2212 return 0;
2213 }
2214
2215 /* we don't need to check ss_initialized here since the only way both
2216 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2217 * security server was initialized and ss_initialized was true */
2218 if (!selinux_mls_enabled) {
2219 *peer_sid = SECSID_NULL;
2220 return 0;
2221 }
2222
James Morris0804d112008-06-06 18:40:29 +10002223 read_lock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002224
2225 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2226 if (!nlbl_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002227 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2228 __func__, nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002229 rc = -EINVAL;
2230 goto out_slowpath;
2231 }
2232 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2233 if (!xfrm_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002234 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2235 __func__, xfrm_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002236 rc = -EINVAL;
2237 goto out_slowpath;
2238 }
2239 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2240
2241out_slowpath:
James Morris0804d112008-06-06 18:40:29 +10002242 read_unlock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002243 if (rc == 0)
2244 /* at present NetLabel SIDs/labels really only carry MLS
2245 * information so if the MLS portion of the NetLabel SID
2246 * matches the MLS portion of the labeled XFRM SID/label
2247 * then pass along the XFRM SID as it is the most
2248 * expressive */
2249 *peer_sid = xfrm_sid;
2250 else
2251 *peer_sid = SECSID_NULL;
2252 return rc;
2253}
2254
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002255static int get_classes_callback(void *k, void *d, void *args)
2256{
2257 struct class_datum *datum = d;
2258 char *name = k, **classes = args;
2259 int value = datum->value - 1;
2260
2261 classes[value] = kstrdup(name, GFP_ATOMIC);
2262 if (!classes[value])
2263 return -ENOMEM;
2264
2265 return 0;
2266}
2267
2268int security_get_classes(char ***classes, int *nclasses)
2269{
2270 int rc = -ENOMEM;
2271
James Morris0804d112008-06-06 18:40:29 +10002272 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002273
2274 *nclasses = policydb.p_classes.nprim;
2275 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2276 if (!*classes)
2277 goto out;
2278
2279 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2280 *classes);
2281 if (rc < 0) {
2282 int i;
2283 for (i = 0; i < *nclasses; i++)
2284 kfree((*classes)[i]);
2285 kfree(*classes);
2286 }
2287
2288out:
James Morris0804d112008-06-06 18:40:29 +10002289 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002290 return rc;
2291}
2292
2293static int get_permissions_callback(void *k, void *d, void *args)
2294{
2295 struct perm_datum *datum = d;
2296 char *name = k, **perms = args;
2297 int value = datum->value - 1;
2298
2299 perms[value] = kstrdup(name, GFP_ATOMIC);
2300 if (!perms[value])
2301 return -ENOMEM;
2302
2303 return 0;
2304}
2305
2306int security_get_permissions(char *class, char ***perms, int *nperms)
2307{
2308 int rc = -ENOMEM, i;
2309 struct class_datum *match;
2310
James Morris0804d112008-06-06 18:40:29 +10002311 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002312
2313 match = hashtab_search(policydb.p_classes.table, class);
2314 if (!match) {
Eric Paris744ba352008-04-17 11:52:44 -04002315 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002316 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002317 rc = -EINVAL;
2318 goto out;
2319 }
2320
2321 *nperms = match->permissions.nprim;
2322 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2323 if (!*perms)
2324 goto out;
2325
2326 if (match->comdatum) {
2327 rc = hashtab_map(match->comdatum->permissions.table,
2328 get_permissions_callback, *perms);
2329 if (rc < 0)
2330 goto err;
2331 }
2332
2333 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2334 *perms);
2335 if (rc < 0)
2336 goto err;
2337
2338out:
James Morris0804d112008-06-06 18:40:29 +10002339 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002340 return rc;
2341
2342err:
James Morris0804d112008-06-06 18:40:29 +10002343 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002344 for (i = 0; i < *nperms; i++)
2345 kfree((*perms)[i]);
2346 kfree(*perms);
2347 return rc;
2348}
2349
Eric Paris3f120702007-09-21 14:37:10 -04002350int security_get_reject_unknown(void)
2351{
2352 return policydb.reject_unknown;
2353}
2354
2355int security_get_allow_unknown(void)
2356{
2357 return policydb.allow_unknown;
2358}
2359
Paul Moore3bb56b22008-01-29 08:38:19 -05002360/**
Paul Moore3bb56b22008-01-29 08:38:19 -05002361 * security_policycap_supported - Check for a specific policy capability
2362 * @req_cap: capability
2363 *
2364 * Description:
2365 * This function queries the currently loaded policy to see if it supports the
2366 * capability specified by @req_cap. Returns true (1) if the capability is
2367 * supported, false (0) if it isn't supported.
2368 *
2369 */
2370int security_policycap_supported(unsigned int req_cap)
2371{
2372 int rc;
2373
James Morris0804d112008-06-06 18:40:29 +10002374 read_lock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002375 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
James Morris0804d112008-06-06 18:40:29 +10002376 read_unlock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002377
2378 return rc;
2379}
2380
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002381struct selinux_audit_rule {
2382 u32 au_seqno;
2383 struct context au_ctxt;
2384};
2385
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002386void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002387{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002388 struct selinux_audit_rule *rule = vrule;
2389
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002390 if (rule) {
2391 context_destroy(&rule->au_ctxt);
2392 kfree(rule);
2393 }
2394}
2395
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002396int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002397{
2398 struct selinux_audit_rule *tmprule;
2399 struct role_datum *roledatum;
2400 struct type_datum *typedatum;
2401 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002402 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002403 int rc = 0;
2404
2405 *rule = NULL;
2406
2407 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07002408 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002409
2410 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002411 case AUDIT_SUBJ_USER:
2412 case AUDIT_SUBJ_ROLE:
2413 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002414 case AUDIT_OBJ_USER:
2415 case AUDIT_OBJ_ROLE:
2416 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002417 /* only 'equals' and 'not equals' fit user, role, and type */
2418 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2419 return -EINVAL;
2420 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002421 case AUDIT_SUBJ_SEN:
2422 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002423 case AUDIT_OBJ_LEV_LOW:
2424 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002425 /* we do not allow a range, indicated by the presense of '-' */
2426 if (strchr(rulestr, '-'))
2427 return -EINVAL;
2428 break;
2429 default:
2430 /* only the above fields are valid */
2431 return -EINVAL;
2432 }
2433
2434 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2435 if (!tmprule)
2436 return -ENOMEM;
2437
2438 context_init(&tmprule->au_ctxt);
2439
James Morris0804d112008-06-06 18:40:29 +10002440 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002441
2442 tmprule->au_seqno = latest_granting;
2443
2444 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002445 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002446 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002447 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2448 if (!userdatum)
2449 rc = -EINVAL;
2450 else
2451 tmprule->au_ctxt.user = userdatum->value;
2452 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002453 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002454 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002455 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2456 if (!roledatum)
2457 rc = -EINVAL;
2458 else
2459 tmprule->au_ctxt.role = roledatum->value;
2460 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002461 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002462 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002463 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2464 if (!typedatum)
2465 rc = -EINVAL;
2466 else
2467 tmprule->au_ctxt.type = typedatum->value;
2468 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002469 case AUDIT_SUBJ_SEN:
2470 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002471 case AUDIT_OBJ_LEV_LOW:
2472 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002473 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2474 break;
2475 }
2476
James Morris0804d112008-06-06 18:40:29 +10002477 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002478
2479 if (rc) {
2480 selinux_audit_rule_free(tmprule);
2481 tmprule = NULL;
2482 }
2483
2484 *rule = tmprule;
2485
2486 return rc;
2487}
2488
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002489/* Check to see if the rule contains any selinux fields */
2490int selinux_audit_rule_known(struct audit_krule *rule)
2491{
2492 int i;
2493
2494 for (i = 0; i < rule->field_count; i++) {
2495 struct audit_field *f = &rule->fields[i];
2496 switch (f->type) {
2497 case AUDIT_SUBJ_USER:
2498 case AUDIT_SUBJ_ROLE:
2499 case AUDIT_SUBJ_TYPE:
2500 case AUDIT_SUBJ_SEN:
2501 case AUDIT_SUBJ_CLR:
2502 case AUDIT_OBJ_USER:
2503 case AUDIT_OBJ_ROLE:
2504 case AUDIT_OBJ_TYPE:
2505 case AUDIT_OBJ_LEV_LOW:
2506 case AUDIT_OBJ_LEV_HIGH:
2507 return 1;
2508 }
2509 }
2510
2511 return 0;
2512}
2513
2514int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Eric Parisf5269712008-05-14 11:27:45 -04002515 struct audit_context *actx)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002516{
2517 struct context *ctxt;
2518 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002519 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002520 int match = 0;
2521
2522 if (!rule) {
2523 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002524 "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002525 return -ENOENT;
2526 }
2527
James Morris0804d112008-06-06 18:40:29 +10002528 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002529
2530 if (rule->au_seqno < latest_granting) {
2531 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002532 "selinux_audit_rule_match: stale rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002533 match = -ESTALE;
2534 goto out;
2535 }
2536
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002537 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002538 if (!ctxt) {
2539 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002540 "selinux_audit_rule_match: unrecognized SID %d\n",
2541 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002542 match = -ENOENT;
2543 goto out;
2544 }
2545
2546 /* a field/op pair that is not caught here will simply fall through
2547 without a match */
2548 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002549 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002550 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002551 switch (op) {
2552 case AUDIT_EQUAL:
2553 match = (ctxt->user == rule->au_ctxt.user);
2554 break;
2555 case AUDIT_NOT_EQUAL:
2556 match = (ctxt->user != rule->au_ctxt.user);
2557 break;
2558 }
2559 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002560 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002561 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002562 switch (op) {
2563 case AUDIT_EQUAL:
2564 match = (ctxt->role == rule->au_ctxt.role);
2565 break;
2566 case AUDIT_NOT_EQUAL:
2567 match = (ctxt->role != rule->au_ctxt.role);
2568 break;
2569 }
2570 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002571 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002572 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002573 switch (op) {
2574 case AUDIT_EQUAL:
2575 match = (ctxt->type == rule->au_ctxt.type);
2576 break;
2577 case AUDIT_NOT_EQUAL:
2578 match = (ctxt->type != rule->au_ctxt.type);
2579 break;
2580 }
2581 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002582 case AUDIT_SUBJ_SEN:
2583 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002584 case AUDIT_OBJ_LEV_LOW:
2585 case AUDIT_OBJ_LEV_HIGH:
2586 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04002587 field == AUDIT_OBJ_LEV_LOW) ?
2588 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002589 switch (op) {
2590 case AUDIT_EQUAL:
2591 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002592 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002593 break;
2594 case AUDIT_NOT_EQUAL:
2595 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002596 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002597 break;
2598 case AUDIT_LESS_THAN:
2599 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002600 level) &&
2601 !mls_level_eq(&rule->au_ctxt.range.level[0],
2602 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002603 break;
2604 case AUDIT_LESS_THAN_OR_EQUAL:
2605 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002606 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002607 break;
2608 case AUDIT_GREATER_THAN:
2609 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002610 &rule->au_ctxt.range.level[0]) &&
2611 !mls_level_eq(level,
2612 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002613 break;
2614 case AUDIT_GREATER_THAN_OR_EQUAL:
2615 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002616 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002617 break;
2618 }
2619 }
2620
2621out:
James Morris0804d112008-06-06 18:40:29 +10002622 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002623 return match;
2624}
2625
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002626static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002627
2628static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
Eric Parisf5269712008-05-14 11:27:45 -04002629 u16 class, u32 perms, u32 *retained)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002630{
2631 int err = 0;
2632
2633 if (event == AVC_CALLBACK_RESET && aurule_callback)
2634 err = aurule_callback();
2635 return err;
2636}
2637
2638static int __init aurule_init(void)
2639{
2640 int err;
2641
2642 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
Eric Paris5d55a342008-04-18 17:38:33 -04002643 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002644 if (err)
2645 panic("avc_add_callback() failed, error %d\n", err);
2646
2647 return err;
2648}
2649__initcall(aurule_init);
2650
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002651#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002652/**
Paul Moore5778eab2007-02-28 15:14:22 -05002653 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2654 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002655 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002656 *
2657 * Description:
2658 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05002659 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2660 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002661 *
2662 */
Paul Moore5778eab2007-02-28 15:14:22 -05002663static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002664 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002665{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002666 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002667
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002668 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2669 if (sid_cache == NULL)
2670 return;
Paul Moore5778eab2007-02-28 15:14:22 -05002671 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002672 if (secattr->cache == NULL) {
2673 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05002674 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02002675 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002676
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002677 *sid_cache = sid;
2678 secattr->cache->free = kfree;
2679 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05002680 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002681}
2682
2683/**
Paul Moore5778eab2007-02-28 15:14:22 -05002684 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002685 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002686 * @sid: the SELinux SID
2687 *
2688 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002689 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002690 * SELinux SID. If the @secattr field does not contain a full SELinux
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002691 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2692 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2693 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2694 * conversion for future lookups. Returns zero on success, negative values on
2695 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002696 *
2697 */
Paul Moore5778eab2007-02-28 15:14:22 -05002698int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05002699 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002700{
2701 int rc = -EIDRM;
2702 struct context *ctx;
2703 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05002704
2705 if (!ss_initialized) {
2706 *sid = SECSID_NULL;
2707 return 0;
2708 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002709
James Morris0804d112008-06-06 18:40:29 +10002710 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002711
Paul Moore701a90b2006-11-17 17:38:46 -05002712 if (secattr->flags & NETLBL_SECATTR_CACHE) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002713 *sid = *(u32 *)secattr->cache->data;
2714 rc = 0;
Paul Moore16efd452008-01-29 08:37:59 -05002715 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2716 *sid = secattr->attr.secid;
2717 rc = 0;
Paul Moore701a90b2006-11-17 17:38:46 -05002718 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002719 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002720 if (ctx == NULL)
2721 goto netlbl_secattr_to_sid_return;
2722
2723 ctx_new.user = ctx->user;
2724 ctx_new.role = ctx->role;
2725 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05002726 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05002727 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05002728 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
Paul Moore16efd452008-01-29 08:37:59 -05002729 secattr->attr.mls.cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002730 goto netlbl_secattr_to_sid_return;
2731 ctx_new.range.level[1].cat.highbit =
2732 ctx_new.range.level[0].cat.highbit;
2733 ctx_new.range.level[1].cat.node =
2734 ctx_new.range.level[0].cat.node;
2735 } else {
2736 ebitmap_init(&ctx_new.range.level[0].cat);
2737 ebitmap_init(&ctx_new.range.level[1].cat);
2738 }
2739 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2740 goto netlbl_secattr_to_sid_return_cleanup;
2741
2742 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2743 if (rc != 0)
2744 goto netlbl_secattr_to_sid_return_cleanup;
2745
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002746 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05002747
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002748 ebitmap_destroy(&ctx_new.range.level[0].cat);
2749 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002750 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002751 rc = 0;
2752 }
2753
2754netlbl_secattr_to_sid_return:
James Morris0804d112008-06-06 18:40:29 +10002755 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002756 return rc;
2757netlbl_secattr_to_sid_return_cleanup:
2758 ebitmap_destroy(&ctx_new.range.level[0].cat);
2759 goto netlbl_secattr_to_sid_return;
2760}
2761
2762/**
Paul Moore5778eab2007-02-28 15:14:22 -05002763 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2764 * @sid: the SELinux SID
2765 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002766 *
2767 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002768 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2769 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002770 *
2771 */
Paul Moore5778eab2007-02-28 15:14:22 -05002772int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002773{
2774 int rc = -ENOENT;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002775 struct context *ctx;
2776
2777 if (!ss_initialized)
2778 return 0;
2779
James Morris0804d112008-06-06 18:40:29 +10002780 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002781 ctx = sidtab_search(&sidtab, sid);
2782 if (ctx == NULL)
Paul Moore5778eab2007-02-28 15:14:22 -05002783 goto netlbl_sid_to_secattr_failure;
2784 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2785 GFP_ATOMIC);
Paul Moore00447872008-04-12 19:06:42 -07002786 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY;
Paul Moore5778eab2007-02-28 15:14:22 -05002787 mls_export_netlbl_lvl(ctx, secattr);
2788 rc = mls_export_netlbl_cat(ctx, secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04002789 if (rc != 0)
Paul Moore5778eab2007-02-28 15:14:22 -05002790 goto netlbl_sid_to_secattr_failure;
James Morris0804d112008-06-06 18:40:29 +10002791 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002792
Paul Moore5778eab2007-02-28 15:14:22 -05002793 return 0;
Paul Moore9f2ad662006-11-17 17:38:53 -05002794
Paul Moore5778eab2007-02-28 15:14:22 -05002795netlbl_sid_to_secattr_failure:
James Morris0804d112008-06-06 18:40:29 +10002796 read_unlock(&policy_rwlock);
Paul Mooref8687af2006-10-30 15:22:15 -08002797 return rc;
2798}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002799#endif /* CONFIG_NETLABEL */