blob: 1e52356664d6866ed2b24ad753088b8349a167ba [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>
5 * James Morris <jmorris@redhat.com>
6 *
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 *
14 * Added conditional policy language extensions
15 *
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070016 * Updated: Hewlett-Packard <paul.moore@hp.com>
17 *
18 * Added support for NetLabel
19 *
Chad Sellersb94c7e62006-11-06 12:38:18 -050020 * Updated: Chad Sellers <csellers@tresys.com>
21 *
22 * Added validation of kernel classes and permissions
23 *
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070024 * Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
Darrel Goeddel376bd9c2006-02-24 15:44:05 -060025 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
Chad Sellersb94c7e62006-11-06 12:38:18 -050026 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, version 2.
31 */
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/string.h>
35#include <linux/spinlock.h>
Paul Moore9f2ad662006-11-17 17:38:53 -050036#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/errno.h>
38#include <linux/in.h>
39#include <linux/sched.h>
40#include <linux/audit.h>
Ingo Molnarbb003072006-03-22 00:09:14 -080041#include <linux/mutex.h>
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070042#include <net/sock.h>
43#include <net/netlabel.h>
Ingo Molnarbb003072006-03-22 00:09:14 -080044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "flask.h"
46#include "avc.h"
47#include "avc_ss.h"
48#include "security.h"
49#include "context.h"
50#include "policydb.h"
51#include "sidtab.h"
52#include "services.h"
53#include "conditional.h"
54#include "mls.h"
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070055#include "objsec.h"
56#include "selinux_netlabel.h"
Paul Moore3de4bab2006-11-17 17:38:54 -050057#include "xfrm.h"
Paul Moore02752762006-11-29 13:18:18 -050058#include "ebitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60extern void selnl_notify_policyload(u32 seqno);
61unsigned int policydb_loaded_version;
62
Chad Sellersb94c7e62006-11-06 12:38:18 -050063/*
64 * This is declared in avc.c
65 */
66extern const struct selinux_class_perm selinux_class_perm;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static DEFINE_RWLOCK(policy_rwlock);
69#define POLICY_RDLOCK read_lock(&policy_rwlock)
70#define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
71#define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
72#define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
73
Ingo Molnarbb003072006-03-22 00:09:14 -080074static DEFINE_MUTEX(load_mutex);
75#define LOAD_LOCK mutex_lock(&load_mutex)
76#define LOAD_UNLOCK mutex_unlock(&load_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static struct sidtab sidtab;
79struct policydb policydb;
80int ss_initialized = 0;
81
82/*
83 * The largest sequence number that has been used when
84 * providing an access decision to the access vector cache.
85 * The sequence number only changes when a policy change
86 * occurs.
87 */
88static u32 latest_granting = 0;
89
90/* Forward declaration. */
91static int context_struct_to_string(struct context *context, char **scontext,
92 u32 *scontext_len);
93
94/*
95 * Return the boolean value of a constraint expression
96 * when it is applied to the specified source and target
97 * security contexts.
98 *
99 * xcontext is a special beast... It is used by the validatetrans rules
100 * only. For these rules, scontext is the context before the transition,
101 * tcontext is the context after the transition, and xcontext is the context
102 * of the process performing the transition. All other callers of
103 * constraint_expr_eval should pass in NULL for xcontext.
104 */
105static int constraint_expr_eval(struct context *scontext,
106 struct context *tcontext,
107 struct context *xcontext,
108 struct constraint_expr *cexpr)
109{
110 u32 val1, val2;
111 struct context *c;
112 struct role_datum *r1, *r2;
113 struct mls_level *l1, *l2;
114 struct constraint_expr *e;
115 int s[CEXPR_MAXDEPTH];
116 int sp = -1;
117
118 for (e = cexpr; e; e = e->next) {
119 switch (e->expr_type) {
120 case CEXPR_NOT:
121 BUG_ON(sp < 0);
122 s[sp] = !s[sp];
123 break;
124 case CEXPR_AND:
125 BUG_ON(sp < 1);
126 sp--;
127 s[sp] &= s[sp+1];
128 break;
129 case CEXPR_OR:
130 BUG_ON(sp < 1);
131 sp--;
132 s[sp] |= s[sp+1];
133 break;
134 case CEXPR_ATTR:
135 if (sp == (CEXPR_MAXDEPTH-1))
136 return 0;
137 switch (e->attr) {
138 case CEXPR_USER:
139 val1 = scontext->user;
140 val2 = tcontext->user;
141 break;
142 case CEXPR_TYPE:
143 val1 = scontext->type;
144 val2 = tcontext->type;
145 break;
146 case CEXPR_ROLE:
147 val1 = scontext->role;
148 val2 = tcontext->role;
149 r1 = policydb.role_val_to_struct[val1 - 1];
150 r2 = policydb.role_val_to_struct[val2 - 1];
151 switch (e->op) {
152 case CEXPR_DOM:
153 s[++sp] = ebitmap_get_bit(&r1->dominates,
154 val2 - 1);
155 continue;
156 case CEXPR_DOMBY:
157 s[++sp] = ebitmap_get_bit(&r2->dominates,
158 val1 - 1);
159 continue;
160 case CEXPR_INCOMP:
161 s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
162 val2 - 1) &&
163 !ebitmap_get_bit(&r2->dominates,
164 val1 - 1) );
165 continue;
166 default:
167 break;
168 }
169 break;
170 case CEXPR_L1L2:
171 l1 = &(scontext->range.level[0]);
172 l2 = &(tcontext->range.level[0]);
173 goto mls_ops;
174 case CEXPR_L1H2:
175 l1 = &(scontext->range.level[0]);
176 l2 = &(tcontext->range.level[1]);
177 goto mls_ops;
178 case CEXPR_H1L2:
179 l1 = &(scontext->range.level[1]);
180 l2 = &(tcontext->range.level[0]);
181 goto mls_ops;
182 case CEXPR_H1H2:
183 l1 = &(scontext->range.level[1]);
184 l2 = &(tcontext->range.level[1]);
185 goto mls_ops;
186 case CEXPR_L1H1:
187 l1 = &(scontext->range.level[0]);
188 l2 = &(scontext->range.level[1]);
189 goto mls_ops;
190 case CEXPR_L2H2:
191 l1 = &(tcontext->range.level[0]);
192 l2 = &(tcontext->range.level[1]);
193 goto mls_ops;
194mls_ops:
195 switch (e->op) {
196 case CEXPR_EQ:
197 s[++sp] = mls_level_eq(l1, l2);
198 continue;
199 case CEXPR_NEQ:
200 s[++sp] = !mls_level_eq(l1, l2);
201 continue;
202 case CEXPR_DOM:
203 s[++sp] = mls_level_dom(l1, l2);
204 continue;
205 case CEXPR_DOMBY:
206 s[++sp] = mls_level_dom(l2, l1);
207 continue;
208 case CEXPR_INCOMP:
209 s[++sp] = mls_level_incomp(l2, l1);
210 continue;
211 default:
212 BUG();
213 return 0;
214 }
215 break;
216 default:
217 BUG();
218 return 0;
219 }
220
221 switch (e->op) {
222 case CEXPR_EQ:
223 s[++sp] = (val1 == val2);
224 break;
225 case CEXPR_NEQ:
226 s[++sp] = (val1 != val2);
227 break;
228 default:
229 BUG();
230 return 0;
231 }
232 break;
233 case CEXPR_NAMES:
234 if (sp == (CEXPR_MAXDEPTH-1))
235 return 0;
236 c = scontext;
237 if (e->attr & CEXPR_TARGET)
238 c = tcontext;
239 else if (e->attr & CEXPR_XTARGET) {
240 c = xcontext;
241 if (!c) {
242 BUG();
243 return 0;
244 }
245 }
246 if (e->attr & CEXPR_USER)
247 val1 = c->user;
248 else if (e->attr & CEXPR_ROLE)
249 val1 = c->role;
250 else if (e->attr & CEXPR_TYPE)
251 val1 = c->type;
252 else {
253 BUG();
254 return 0;
255 }
256
257 switch (e->op) {
258 case CEXPR_EQ:
259 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
260 break;
261 case CEXPR_NEQ:
262 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
263 break;
264 default:
265 BUG();
266 return 0;
267 }
268 break;
269 default:
270 BUG();
271 return 0;
272 }
273 }
274
275 BUG_ON(sp != 0);
276 return s[0];
277}
278
279/*
280 * Compute access vectors based on a context structure pair for
281 * the permissions in a particular class.
282 */
283static int context_struct_compute_av(struct context *scontext,
284 struct context *tcontext,
285 u16 tclass,
286 u32 requested,
287 struct av_decision *avd)
288{
289 struct constraint_node *constraint;
290 struct role_allow *ra;
291 struct avtab_key avkey;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700292 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct class_datum *tclass_datum;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700294 struct ebitmap *sattr, *tattr;
295 struct ebitmap_node *snode, *tnode;
296 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /*
299 * Remap extended Netlink classes for old policy versions.
300 * Do this here rather than socket_type_to_security_class()
301 * in case a newer policy version is loaded, allowing sockets
302 * to remain in the correct class.
303 */
304 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
305 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
306 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
307 tclass = SECCLASS_NETLINK_SOCKET;
308
309 if (!tclass || tclass > policydb.p_classes.nprim) {
310 printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
311 tclass);
312 return -EINVAL;
313 }
314 tclass_datum = policydb.class_val_to_struct[tclass - 1];
315
316 /*
317 * Initialize the access vectors to the default values.
318 */
319 avd->allowed = 0;
320 avd->decided = 0xffffffff;
321 avd->auditallow = 0;
322 avd->auditdeny = 0xffffffff;
323 avd->seqno = latest_granting;
324
325 /*
326 * If a specific type enforcement rule was defined for
327 * this permission check, then use it.
328 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700330 avkey.specified = AVTAB_AV;
331 sattr = &policydb.type_attr_map[scontext->type - 1];
332 tattr = &policydb.type_attr_map[tcontext->type - 1];
333 ebitmap_for_each_bit(sattr, snode, i) {
334 if (!ebitmap_node_get_bit(snode, i))
335 continue;
336 ebitmap_for_each_bit(tattr, tnode, j) {
337 if (!ebitmap_node_get_bit(tnode, j))
338 continue;
339 avkey.source_type = i + 1;
340 avkey.target_type = j + 1;
341 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
342 node != NULL;
343 node = avtab_search_node_next(node, avkey.specified)) {
344 if (node->key.specified == AVTAB_ALLOWED)
345 avd->allowed |= node->datum.data;
346 else if (node->key.specified == AVTAB_AUDITALLOW)
347 avd->auditallow |= node->datum.data;
348 else if (node->key.specified == AVTAB_AUDITDENY)
349 avd->auditdeny &= node->datum.data;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Stephen Smalley782ebb92005-09-03 15:55:16 -0700352 /* Check conditional av table for additional permissions */
353 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
354
355 }
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 /*
359 * Remove any permissions prohibited by a constraint (this includes
360 * the MLS policy).
361 */
362 constraint = tclass_datum->constraints;
363 while (constraint) {
364 if ((constraint->permissions & (avd->allowed)) &&
365 !constraint_expr_eval(scontext, tcontext, NULL,
366 constraint->expr)) {
367 avd->allowed = (avd->allowed) & ~(constraint->permissions);
368 }
369 constraint = constraint->next;
370 }
371
372 /*
373 * If checking process transition permission and the
374 * role is changing, then check the (current_role, new_role)
375 * pair.
376 */
377 if (tclass == SECCLASS_PROCESS &&
378 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
379 scontext->role != tcontext->role) {
380 for (ra = policydb.role_allow; ra; ra = ra->next) {
381 if (scontext->role == ra->role &&
382 tcontext->role == ra->new_role)
383 break;
384 }
385 if (!ra)
386 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
387 PROCESS__DYNTRANSITION);
388 }
389
390 return 0;
391}
392
393static int security_validtrans_handle_fail(struct context *ocontext,
394 struct context *ncontext,
395 struct context *tcontext,
396 u16 tclass)
397{
398 char *o = NULL, *n = NULL, *t = NULL;
399 u32 olen, nlen, tlen;
400
401 if (context_struct_to_string(ocontext, &o, &olen) < 0)
402 goto out;
403 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
404 goto out;
405 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
406 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100407 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 "security_validate_transition: denied for"
409 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
410 o, n, t, policydb.p_class_val_to_name[tclass-1]);
411out:
412 kfree(o);
413 kfree(n);
414 kfree(t);
415
416 if (!selinux_enforcing)
417 return 0;
418 return -EPERM;
419}
420
421int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
422 u16 tclass)
423{
424 struct context *ocontext;
425 struct context *ncontext;
426 struct context *tcontext;
427 struct class_datum *tclass_datum;
428 struct constraint_node *constraint;
429 int rc = 0;
430
431 if (!ss_initialized)
432 return 0;
433
434 POLICY_RDLOCK;
435
436 /*
437 * Remap extended Netlink classes for old policy versions.
438 * Do this here rather than socket_type_to_security_class()
439 * in case a newer policy version is loaded, allowing sockets
440 * to remain in the correct class.
441 */
442 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
443 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
444 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
445 tclass = SECCLASS_NETLINK_SOCKET;
446
447 if (!tclass || tclass > policydb.p_classes.nprim) {
448 printk(KERN_ERR "security_validate_transition: "
449 "unrecognized class %d\n", tclass);
450 rc = -EINVAL;
451 goto out;
452 }
453 tclass_datum = policydb.class_val_to_struct[tclass - 1];
454
455 ocontext = sidtab_search(&sidtab, oldsid);
456 if (!ocontext) {
457 printk(KERN_ERR "security_validate_transition: "
458 " unrecognized SID %d\n", oldsid);
459 rc = -EINVAL;
460 goto out;
461 }
462
463 ncontext = sidtab_search(&sidtab, newsid);
464 if (!ncontext) {
465 printk(KERN_ERR "security_validate_transition: "
466 " unrecognized SID %d\n", newsid);
467 rc = -EINVAL;
468 goto out;
469 }
470
471 tcontext = sidtab_search(&sidtab, tasksid);
472 if (!tcontext) {
473 printk(KERN_ERR "security_validate_transition: "
474 " unrecognized SID %d\n", tasksid);
475 rc = -EINVAL;
476 goto out;
477 }
478
479 constraint = tclass_datum->validatetrans;
480 while (constraint) {
481 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
482 constraint->expr)) {
483 rc = security_validtrans_handle_fail(ocontext, ncontext,
484 tcontext, tclass);
485 goto out;
486 }
487 constraint = constraint->next;
488 }
489
490out:
491 POLICY_RDUNLOCK;
492 return rc;
493}
494
495/**
496 * security_compute_av - Compute access vector decisions.
497 * @ssid: source security identifier
498 * @tsid: target security identifier
499 * @tclass: target security class
500 * @requested: requested permissions
501 * @avd: access vector decisions
502 *
503 * Compute a set of access vector decisions based on the
504 * SID pair (@ssid, @tsid) for the permissions in @tclass.
505 * Return -%EINVAL if any of the parameters are invalid or %0
506 * if the access vector decisions were computed successfully.
507 */
508int security_compute_av(u32 ssid,
509 u32 tsid,
510 u16 tclass,
511 u32 requested,
512 struct av_decision *avd)
513{
514 struct context *scontext = NULL, *tcontext = NULL;
515 int rc = 0;
516
517 if (!ss_initialized) {
Stephen Smalley4c443d12005-05-16 21:53:52 -0700518 avd->allowed = 0xffffffff;
519 avd->decided = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 avd->auditallow = 0;
521 avd->auditdeny = 0xffffffff;
522 avd->seqno = latest_granting;
523 return 0;
524 }
525
526 POLICY_RDLOCK;
527
528 scontext = sidtab_search(&sidtab, ssid);
529 if (!scontext) {
530 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
531 ssid);
532 rc = -EINVAL;
533 goto out;
534 }
535 tcontext = sidtab_search(&sidtab, tsid);
536 if (!tcontext) {
537 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
538 tsid);
539 rc = -EINVAL;
540 goto out;
541 }
542
543 rc = context_struct_compute_av(scontext, tcontext, tclass,
544 requested, avd);
545out:
546 POLICY_RDUNLOCK;
547 return rc;
548}
549
550/*
551 * Write the security context string representation of
552 * the context structure `context' into a dynamically
553 * allocated string of the correct size. Set `*scontext'
554 * to point to this string and set `*scontext_len' to
555 * the length of the string.
556 */
557static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
558{
559 char *scontextp;
560
561 *scontext = NULL;
562 *scontext_len = 0;
563
564 /* Compute the size of the context. */
565 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
566 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
567 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
568 *scontext_len += mls_compute_context_len(context);
569
570 /* Allocate space for the context; caller must free this space. */
571 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
572 if (!scontextp) {
573 return -ENOMEM;
574 }
575 *scontext = scontextp;
576
577 /*
578 * Copy the user name, role name and type name into the context.
579 */
580 sprintf(scontextp, "%s:%s:%s",
581 policydb.p_user_val_to_name[context->user - 1],
582 policydb.p_role_val_to_name[context->role - 1],
583 policydb.p_type_val_to_name[context->type - 1]);
584 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
585 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
586 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
587
588 mls_sid_to_context(context, &scontextp);
589
590 *scontextp = 0;
591
592 return 0;
593}
594
595#include "initial_sid_to_string.h"
596
597/**
598 * security_sid_to_context - Obtain a context for a given SID.
599 * @sid: security identifier, SID
600 * @scontext: security context
601 * @scontext_len: length in bytes
602 *
603 * Write the string representation of the context associated with @sid
604 * into a dynamically allocated string of the correct size. Set @scontext
605 * to point to this string and set @scontext_len to the length of the string.
606 */
607int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
608{
609 struct context *context;
610 int rc = 0;
611
Stephen Smalley4f4acf32007-02-26 12:02:34 -0500612 *scontext = NULL;
613 *scontext_len = 0;
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!ss_initialized) {
616 if (sid <= SECINITSID_NUM) {
617 char *scontextp;
618
619 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
620 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
Serge E. Hallyn0cccca02006-05-15 09:43:48 -0700621 if (!scontextp) {
622 rc = -ENOMEM;
623 goto out;
624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 strcpy(scontextp, initial_sid_to_string[sid]);
626 *scontext = scontextp;
627 goto out;
628 }
629 printk(KERN_ERR "security_sid_to_context: called before initial "
630 "load_policy on unknown SID %d\n", sid);
631 rc = -EINVAL;
632 goto out;
633 }
634 POLICY_RDLOCK;
635 context = sidtab_search(&sidtab, sid);
636 if (!context) {
637 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
638 "%d\n", sid);
639 rc = -EINVAL;
640 goto out_unlock;
641 }
642 rc = context_struct_to_string(context, scontext, scontext_len);
643out_unlock:
644 POLICY_RDUNLOCK;
645out:
646 return rc;
647
648}
649
James Morrisf5c1d5b2005-07-28 01:07:37 -0700650static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 char *scontext2;
653 struct context context;
654 struct role_datum *role;
655 struct type_datum *typdatum;
656 struct user_datum *usrdatum;
657 char *scontextp, *p, oldc;
658 int rc = 0;
659
660 if (!ss_initialized) {
661 int i;
662
663 for (i = 1; i < SECINITSID_NUM; i++) {
664 if (!strcmp(initial_sid_to_string[i], scontext)) {
665 *sid = i;
666 goto out;
667 }
668 }
669 *sid = SECINITSID_KERNEL;
670 goto out;
671 }
672 *sid = SECSID_NULL;
673
674 /* Copy the string so that we can modify the copy as we parse it.
675 The string should already by null terminated, but we append a
676 null suffix to the copy to avoid problems with the existing
677 attr package, which doesn't view the null terminator as part
678 of the attribute value. */
679 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
680 if (!scontext2) {
681 rc = -ENOMEM;
682 goto out;
683 }
684 memcpy(scontext2, scontext, scontext_len);
685 scontext2[scontext_len] = 0;
686
687 context_init(&context);
688 *sid = SECSID_NULL;
689
690 POLICY_RDLOCK;
691
692 /* Parse the security context. */
693
694 rc = -EINVAL;
695 scontextp = (char *) scontext2;
696
697 /* Extract the user. */
698 p = scontextp;
699 while (*p && *p != ':')
700 p++;
701
702 if (*p == 0)
703 goto out_unlock;
704
705 *p++ = 0;
706
707 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
708 if (!usrdatum)
709 goto out_unlock;
710
711 context.user = usrdatum->value;
712
713 /* Extract role. */
714 scontextp = p;
715 while (*p && *p != ':')
716 p++;
717
718 if (*p == 0)
719 goto out_unlock;
720
721 *p++ = 0;
722
723 role = hashtab_search(policydb.p_roles.table, scontextp);
724 if (!role)
725 goto out_unlock;
726 context.role = role->value;
727
728 /* Extract type. */
729 scontextp = p;
730 while (*p && *p != ':')
731 p++;
732 oldc = *p;
733 *p++ = 0;
734
735 typdatum = hashtab_search(policydb.p_types.table, scontextp);
736 if (!typdatum)
737 goto out_unlock;
738
739 context.type = typdatum->value;
740
James Morrisf5c1d5b2005-07-28 01:07:37 -0700741 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (rc)
743 goto out_unlock;
744
745 if ((p - scontext2) < scontext_len) {
746 rc = -EINVAL;
747 goto out_unlock;
748 }
749
750 /* Check the validity of the new context. */
751 if (!policydb_context_isvalid(&policydb, &context)) {
752 rc = -EINVAL;
753 goto out_unlock;
754 }
755 /* Obtain the new sid. */
756 rc = sidtab_context_to_sid(&sidtab, &context, sid);
757out_unlock:
758 POLICY_RDUNLOCK;
759 context_destroy(&context);
760 kfree(scontext2);
761out:
762 return rc;
763}
764
James Morrisf5c1d5b2005-07-28 01:07:37 -0700765/**
766 * security_context_to_sid - Obtain a SID for a given security context.
767 * @scontext: security context
768 * @scontext_len: length in bytes
769 * @sid: security identifier, SID
770 *
771 * Obtains a SID associated with the security context that
772 * has the string representation specified by @scontext.
773 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
774 * memory is available, or 0 on success.
775 */
776int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
777{
778 return security_context_to_sid_core(scontext, scontext_len,
779 sid, SECSID_NULL);
780}
781
782/**
783 * security_context_to_sid_default - Obtain a SID for a given security context,
784 * falling back to specified default if needed.
785 *
786 * @scontext: security context
787 * @scontext_len: length in bytes
788 * @sid: security identifier, SID
789 * @def_sid: default SID to assign on errror
790 *
791 * Obtains a SID associated with the security context that
792 * has the string representation specified by @scontext.
793 * The default SID is passed to the MLS layer to be used to allow
794 * kernel labeling of the MLS field if the MLS field is not present
795 * (for upgrading to MLS without full relabel).
796 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
797 * memory is available, or 0 on success.
798 */
799int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
800{
801 return security_context_to_sid_core(scontext, scontext_len,
802 sid, def_sid);
803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805static int compute_sid_handle_invalid_context(
806 struct context *scontext,
807 struct context *tcontext,
808 u16 tclass,
809 struct context *newcontext)
810{
811 char *s = NULL, *t = NULL, *n = NULL;
812 u32 slen, tlen, nlen;
813
814 if (context_struct_to_string(scontext, &s, &slen) < 0)
815 goto out;
816 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
817 goto out;
818 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
819 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100820 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 "security_compute_sid: invalid context %s"
822 " for scontext=%s"
823 " tcontext=%s"
824 " tclass=%s",
825 n, s, t, policydb.p_class_val_to_name[tclass-1]);
826out:
827 kfree(s);
828 kfree(t);
829 kfree(n);
830 if (!selinux_enforcing)
831 return 0;
832 return -EACCES;
833}
834
835static int security_compute_sid(u32 ssid,
836 u32 tsid,
837 u16 tclass,
838 u32 specified,
839 u32 *out_sid)
840{
841 struct context *scontext = NULL, *tcontext = NULL, newcontext;
842 struct role_trans *roletr = NULL;
843 struct avtab_key avkey;
844 struct avtab_datum *avdatum;
845 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int rc = 0;
847
848 if (!ss_initialized) {
849 switch (tclass) {
850 case SECCLASS_PROCESS:
851 *out_sid = ssid;
852 break;
853 default:
854 *out_sid = tsid;
855 break;
856 }
857 goto out;
858 }
859
Venkat Yekkirala851f8a62006-07-30 03:03:18 -0700860 context_init(&newcontext);
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 POLICY_RDLOCK;
863
864 scontext = sidtab_search(&sidtab, ssid);
865 if (!scontext) {
866 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
867 ssid);
868 rc = -EINVAL;
869 goto out_unlock;
870 }
871 tcontext = sidtab_search(&sidtab, tsid);
872 if (!tcontext) {
873 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
874 tsid);
875 rc = -EINVAL;
876 goto out_unlock;
877 }
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* Set the user identity. */
880 switch (specified) {
881 case AVTAB_TRANSITION:
882 case AVTAB_CHANGE:
883 /* Use the process user identity. */
884 newcontext.user = scontext->user;
885 break;
886 case AVTAB_MEMBER:
887 /* Use the related object owner. */
888 newcontext.user = tcontext->user;
889 break;
890 }
891
892 /* Set the role and type to default values. */
893 switch (tclass) {
894 case SECCLASS_PROCESS:
895 /* Use the current role and type of process. */
896 newcontext.role = scontext->role;
897 newcontext.type = scontext->type;
898 break;
899 default:
900 /* Use the well-defined object role. */
901 newcontext.role = OBJECT_R_VAL;
902 /* Use the type of the related object. */
903 newcontext.type = tcontext->type;
904 }
905
906 /* Look for a type transition/member/change rule. */
907 avkey.source_type = scontext->type;
908 avkey.target_type = tcontext->type;
909 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700910 avkey.specified = specified;
911 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /* If no permanent rule, also check for enabled conditional rules */
914 if(!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700915 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700917 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 avdatum = &node->datum;
919 break;
920 }
921 }
922 }
923
Stephen Smalley782ebb92005-09-03 15:55:16 -0700924 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -0700926 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
929 /* Check for class-specific changes. */
930 switch (tclass) {
931 case SECCLASS_PROCESS:
932 if (specified & AVTAB_TRANSITION) {
933 /* Look for a role transition rule. */
934 for (roletr = policydb.role_tr; roletr;
935 roletr = roletr->next) {
936 if (roletr->role == scontext->role &&
937 roletr->type == tcontext->type) {
938 /* Use the role transition rule. */
939 newcontext.role = roletr->new_role;
940 break;
941 }
942 }
943 }
944 break;
945 default:
946 break;
947 }
948
949 /* Set the MLS attributes.
950 This is done last because it may allocate memory. */
951 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
952 if (rc)
953 goto out_unlock;
954
955 /* Check the validity of the context. */
956 if (!policydb_context_isvalid(&policydb, &newcontext)) {
957 rc = compute_sid_handle_invalid_context(scontext,
958 tcontext,
959 tclass,
960 &newcontext);
961 if (rc)
962 goto out_unlock;
963 }
964 /* Obtain the sid for the context. */
965 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
966out_unlock:
967 POLICY_RDUNLOCK;
968 context_destroy(&newcontext);
969out:
970 return rc;
971}
972
973/**
974 * security_transition_sid - Compute the SID for a new subject/object.
975 * @ssid: source security identifier
976 * @tsid: target security identifier
977 * @tclass: target security class
978 * @out_sid: security identifier for new subject/object
979 *
980 * Compute a SID to use for labeling a new subject or object in the
981 * class @tclass based on a SID pair (@ssid, @tsid).
982 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
983 * if insufficient memory is available, or %0 if the new SID was
984 * computed successfully.
985 */
986int security_transition_sid(u32 ssid,
987 u32 tsid,
988 u16 tclass,
989 u32 *out_sid)
990{
991 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
992}
993
994/**
995 * security_member_sid - Compute the SID for member selection.
996 * @ssid: source security identifier
997 * @tsid: target security identifier
998 * @tclass: target security class
999 * @out_sid: security identifier for selected member
1000 *
1001 * Compute a SID to use when selecting a member of a polyinstantiated
1002 * object of class @tclass based on a SID pair (@ssid, @tsid).
1003 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1004 * if insufficient memory is available, or %0 if the SID was
1005 * computed successfully.
1006 */
1007int security_member_sid(u32 ssid,
1008 u32 tsid,
1009 u16 tclass,
1010 u32 *out_sid)
1011{
1012 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1013}
1014
1015/**
1016 * security_change_sid - Compute the SID for object relabeling.
1017 * @ssid: source security identifier
1018 * @tsid: target security identifier
1019 * @tclass: target security class
1020 * @out_sid: security identifier for selected member
1021 *
1022 * Compute a SID to use for relabeling an object of class @tclass
1023 * based on a SID pair (@ssid, @tsid).
1024 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1025 * if insufficient memory is available, or %0 if the SID was
1026 * computed successfully.
1027 */
1028int security_change_sid(u32 ssid,
1029 u32 tsid,
1030 u16 tclass,
1031 u32 *out_sid)
1032{
1033 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1034}
1035
Chad Sellersb94c7e62006-11-06 12:38:18 -05001036/*
1037 * Verify that each kernel class that is defined in the
1038 * policy is correct
1039 */
1040static int validate_classes(struct policydb *p)
1041{
1042 int i, j;
1043 struct class_datum *cladatum;
1044 struct perm_datum *perdatum;
1045 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1046 u16 class_val;
1047 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1048 const char *def_class, *def_perm, *pol_class;
1049 struct symtab *perms;
1050
1051 for (i = 1; i < kdefs->cts_len; i++) {
1052 def_class = kdefs->class_to_string[i];
1053 if (i > p->p_classes.nprim) {
1054 printk(KERN_INFO
1055 "security: class %s not defined in policy\n",
1056 def_class);
1057 continue;
1058 }
1059 pol_class = p->p_class_val_to_name[i-1];
1060 if (strcmp(pol_class, def_class)) {
1061 printk(KERN_ERR
1062 "security: class %d is incorrect, found %s but should be %s\n",
1063 i, pol_class, def_class);
1064 return -EINVAL;
1065 }
1066 }
1067 for (i = 0; i < kdefs->av_pts_len; i++) {
1068 class_val = kdefs->av_perm_to_string[i].tclass;
1069 perm_val = kdefs->av_perm_to_string[i].value;
1070 def_perm = kdefs->av_perm_to_string[i].name;
1071 if (class_val > p->p_classes.nprim)
1072 continue;
1073 pol_class = p->p_class_val_to_name[class_val-1];
1074 cladatum = hashtab_search(p->p_classes.table, pol_class);
1075 BUG_ON(!cladatum);
1076 perms = &cladatum->permissions;
1077 nprim = 1 << (perms->nprim - 1);
1078 if (perm_val > nprim) {
1079 printk(KERN_INFO
1080 "security: permission %s in class %s not defined in policy\n",
1081 def_perm, pol_class);
1082 continue;
1083 }
1084 perdatum = hashtab_search(perms->table, def_perm);
1085 if (perdatum == NULL) {
1086 printk(KERN_ERR
1087 "security: permission %s in class %s not found in policy\n",
1088 def_perm, pol_class);
1089 return -EINVAL;
1090 }
1091 pol_val = 1 << (perdatum->value - 1);
1092 if (pol_val != perm_val) {
1093 printk(KERN_ERR
1094 "security: permission %s in class %s has incorrect value\n",
1095 def_perm, pol_class);
1096 return -EINVAL;
1097 }
1098 }
1099 for (i = 0; i < kdefs->av_inherit_len; i++) {
1100 class_val = kdefs->av_inherit[i].tclass;
1101 if (class_val > p->p_classes.nprim)
1102 continue;
1103 pol_class = p->p_class_val_to_name[class_val-1];
1104 cladatum = hashtab_search(p->p_classes.table, pol_class);
1105 BUG_ON(!cladatum);
1106 if (!cladatum->comdatum) {
1107 printk(KERN_ERR
1108 "security: class %s should have an inherits clause but does not\n",
1109 pol_class);
1110 return -EINVAL;
1111 }
1112 tmp = kdefs->av_inherit[i].common_base;
1113 common_pts_len = 0;
1114 while (!(tmp & 0x01)) {
1115 common_pts_len++;
1116 tmp >>= 1;
1117 }
1118 perms = &cladatum->comdatum->permissions;
1119 for (j = 0; j < common_pts_len; j++) {
1120 def_perm = kdefs->av_inherit[i].common_pts[j];
1121 if (j >= perms->nprim) {
1122 printk(KERN_INFO
1123 "security: permission %s in class %s not defined in policy\n",
1124 def_perm, pol_class);
1125 continue;
1126 }
1127 perdatum = hashtab_search(perms->table, def_perm);
1128 if (perdatum == NULL) {
1129 printk(KERN_ERR
1130 "security: permission %s in class %s not found in policy\n",
1131 def_perm, pol_class);
1132 return -EINVAL;
1133 }
1134 if (perdatum->value != j + 1) {
1135 printk(KERN_ERR
1136 "security: permission %s in class %s has incorrect value\n",
1137 def_perm, pol_class);
1138 return -EINVAL;
1139 }
1140 }
1141 }
1142 return 0;
1143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145/* Clone the SID into the new SID table. */
1146static int clone_sid(u32 sid,
1147 struct context *context,
1148 void *arg)
1149{
1150 struct sidtab *s = arg;
1151
1152 return sidtab_insert(s, sid, context);
1153}
1154
1155static inline int convert_context_handle_invalid_context(struct context *context)
1156{
1157 int rc = 0;
1158
1159 if (selinux_enforcing) {
1160 rc = -EINVAL;
1161 } else {
1162 char *s;
1163 u32 len;
1164
1165 context_struct_to_string(context, &s, &len);
1166 printk(KERN_ERR "security: context %s is invalid\n", s);
1167 kfree(s);
1168 }
1169 return rc;
1170}
1171
1172struct convert_context_args {
1173 struct policydb *oldp;
1174 struct policydb *newp;
1175};
1176
1177/*
1178 * Convert the values in the security context
1179 * structure `c' from the values specified
1180 * in the policy `p->oldp' to the values specified
1181 * in the policy `p->newp'. Verify that the
1182 * context is valid under the new policy.
1183 */
1184static int convert_context(u32 key,
1185 struct context *c,
1186 void *p)
1187{
1188 struct convert_context_args *args;
1189 struct context oldc;
1190 struct role_datum *role;
1191 struct type_datum *typdatum;
1192 struct user_datum *usrdatum;
1193 char *s;
1194 u32 len;
1195 int rc;
1196
1197 args = p;
1198
1199 rc = context_cpy(&oldc, c);
1200 if (rc)
1201 goto out;
1202
1203 rc = -EINVAL;
1204
1205 /* Convert the user. */
1206 usrdatum = hashtab_search(args->newp->p_users.table,
1207 args->oldp->p_user_val_to_name[c->user - 1]);
1208 if (!usrdatum) {
1209 goto bad;
1210 }
1211 c->user = usrdatum->value;
1212
1213 /* Convert the role. */
1214 role = hashtab_search(args->newp->p_roles.table,
1215 args->oldp->p_role_val_to_name[c->role - 1]);
1216 if (!role) {
1217 goto bad;
1218 }
1219 c->role = role->value;
1220
1221 /* Convert the type. */
1222 typdatum = hashtab_search(args->newp->p_types.table,
1223 args->oldp->p_type_val_to_name[c->type - 1]);
1224 if (!typdatum) {
1225 goto bad;
1226 }
1227 c->type = typdatum->value;
1228
1229 rc = mls_convert_context(args->oldp, args->newp, c);
1230 if (rc)
1231 goto bad;
1232
1233 /* Check the validity of the new context. */
1234 if (!policydb_context_isvalid(args->newp, c)) {
1235 rc = convert_context_handle_invalid_context(&oldc);
1236 if (rc)
1237 goto bad;
1238 }
1239
1240 context_destroy(&oldc);
1241out:
1242 return rc;
1243bad:
1244 context_struct_to_string(&oldc, &s, &len);
1245 context_destroy(&oldc);
1246 printk(KERN_ERR "security: invalidating context %s\n", s);
1247 kfree(s);
1248 goto out;
1249}
1250
1251extern void selinux_complete_init(void);
1252
1253/**
1254 * security_load_policy - Load a security policy configuration.
1255 * @data: binary policy data
1256 * @len: length of data in bytes
1257 *
1258 * Load a new set of security policy configuration data,
1259 * validate it and convert the SID table as necessary.
1260 * This function will flush the access vector cache after
1261 * loading the new policy.
1262 */
1263int security_load_policy(void *data, size_t len)
1264{
1265 struct policydb oldpolicydb, newpolicydb;
1266 struct sidtab oldsidtab, newsidtab;
1267 struct convert_context_args args;
1268 u32 seqno;
1269 int rc = 0;
1270 struct policy_file file = { data, len }, *fp = &file;
1271
1272 LOAD_LOCK;
1273
1274 if (!ss_initialized) {
1275 avtab_cache_init();
1276 if (policydb_read(&policydb, fp)) {
1277 LOAD_UNLOCK;
1278 avtab_cache_destroy();
1279 return -EINVAL;
1280 }
1281 if (policydb_load_isids(&policydb, &sidtab)) {
1282 LOAD_UNLOCK;
1283 policydb_destroy(&policydb);
1284 avtab_cache_destroy();
1285 return -EINVAL;
1286 }
Chad Sellersb94c7e62006-11-06 12:38:18 -05001287 /* Verify that the kernel defined classes are correct. */
1288 if (validate_classes(&policydb)) {
1289 printk(KERN_ERR
1290 "security: the definition of a class is incorrect\n");
1291 LOAD_UNLOCK;
1292 sidtab_destroy(&sidtab);
1293 policydb_destroy(&policydb);
1294 avtab_cache_destroy();
1295 return -EINVAL;
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 policydb_loaded_version = policydb.policyvers;
1298 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001299 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 LOAD_UNLOCK;
1301 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001302 avc_ss_reset(seqno);
1303 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001304 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001305 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return 0;
1307 }
1308
1309#if 0
1310 sidtab_hash_eval(&sidtab, "sids");
1311#endif
1312
1313 if (policydb_read(&newpolicydb, fp)) {
1314 LOAD_UNLOCK;
1315 return -EINVAL;
1316 }
1317
1318 sidtab_init(&newsidtab);
1319
Chad Sellersb94c7e62006-11-06 12:38:18 -05001320 /* Verify that the kernel defined classes are correct. */
1321 if (validate_classes(&newpolicydb)) {
1322 printk(KERN_ERR
1323 "security: the definition of a class is incorrect\n");
1324 rc = -EINVAL;
1325 goto err;
1326 }
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /* Clone the SID table. */
1329 sidtab_shutdown(&sidtab);
1330 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1331 rc = -ENOMEM;
1332 goto err;
1333 }
1334
1335 /* Convert the internal representations of contexts
1336 in the new SID table and remove invalid SIDs. */
1337 args.oldp = &policydb;
1338 args.newp = &newpolicydb;
1339 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1340
1341 /* Save the old policydb and SID table to free later. */
1342 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1343 sidtab_set(&oldsidtab, &sidtab);
1344
1345 /* Install the new policydb and SID table. */
1346 POLICY_WRLOCK;
1347 memcpy(&policydb, &newpolicydb, sizeof policydb);
1348 sidtab_set(&sidtab, &newsidtab);
1349 seqno = ++latest_granting;
1350 policydb_loaded_version = policydb.policyvers;
1351 POLICY_WRUNLOCK;
1352 LOAD_UNLOCK;
1353
1354 /* Free the old policydb and SID table. */
1355 policydb_destroy(&oldpolicydb);
1356 sidtab_destroy(&oldsidtab);
1357
1358 avc_ss_reset(seqno);
1359 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001360 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001361 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 return 0;
1364
1365err:
1366 LOAD_UNLOCK;
1367 sidtab_destroy(&newsidtab);
1368 policydb_destroy(&newpolicydb);
1369 return rc;
1370
1371}
1372
1373/**
1374 * security_port_sid - Obtain the SID for a port.
1375 * @domain: communication domain aka address family
1376 * @type: socket type
1377 * @protocol: protocol number
1378 * @port: port number
1379 * @out_sid: security identifier
1380 */
1381int security_port_sid(u16 domain,
1382 u16 type,
1383 u8 protocol,
1384 u16 port,
1385 u32 *out_sid)
1386{
1387 struct ocontext *c;
1388 int rc = 0;
1389
1390 POLICY_RDLOCK;
1391
1392 c = policydb.ocontexts[OCON_PORT];
1393 while (c) {
1394 if (c->u.port.protocol == protocol &&
1395 c->u.port.low_port <= port &&
1396 c->u.port.high_port >= port)
1397 break;
1398 c = c->next;
1399 }
1400
1401 if (c) {
1402 if (!c->sid[0]) {
1403 rc = sidtab_context_to_sid(&sidtab,
1404 &c->context[0],
1405 &c->sid[0]);
1406 if (rc)
1407 goto out;
1408 }
1409 *out_sid = c->sid[0];
1410 } else {
1411 *out_sid = SECINITSID_PORT;
1412 }
1413
1414out:
1415 POLICY_RDUNLOCK;
1416 return rc;
1417}
1418
1419/**
1420 * security_netif_sid - Obtain the SID for a network interface.
1421 * @name: interface name
1422 * @if_sid: interface SID
1423 * @msg_sid: default SID for received packets
1424 */
1425int security_netif_sid(char *name,
1426 u32 *if_sid,
1427 u32 *msg_sid)
1428{
1429 int rc = 0;
1430 struct ocontext *c;
1431
1432 POLICY_RDLOCK;
1433
1434 c = policydb.ocontexts[OCON_NETIF];
1435 while (c) {
1436 if (strcmp(name, c->u.name) == 0)
1437 break;
1438 c = c->next;
1439 }
1440
1441 if (c) {
1442 if (!c->sid[0] || !c->sid[1]) {
1443 rc = sidtab_context_to_sid(&sidtab,
1444 &c->context[0],
1445 &c->sid[0]);
1446 if (rc)
1447 goto out;
1448 rc = sidtab_context_to_sid(&sidtab,
1449 &c->context[1],
1450 &c->sid[1]);
1451 if (rc)
1452 goto out;
1453 }
1454 *if_sid = c->sid[0];
1455 *msg_sid = c->sid[1];
1456 } else {
1457 *if_sid = SECINITSID_NETIF;
1458 *msg_sid = SECINITSID_NETMSG;
1459 }
1460
1461out:
1462 POLICY_RDUNLOCK;
1463 return rc;
1464}
1465
1466static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1467{
1468 int i, fail = 0;
1469
1470 for(i = 0; i < 4; i++)
1471 if(addr[i] != (input[i] & mask[i])) {
1472 fail = 1;
1473 break;
1474 }
1475
1476 return !fail;
1477}
1478
1479/**
1480 * security_node_sid - Obtain the SID for a node (host).
1481 * @domain: communication domain aka address family
1482 * @addrp: address
1483 * @addrlen: address length in bytes
1484 * @out_sid: security identifier
1485 */
1486int security_node_sid(u16 domain,
1487 void *addrp,
1488 u32 addrlen,
1489 u32 *out_sid)
1490{
1491 int rc = 0;
1492 struct ocontext *c;
1493
1494 POLICY_RDLOCK;
1495
1496 switch (domain) {
1497 case AF_INET: {
1498 u32 addr;
1499
1500 if (addrlen != sizeof(u32)) {
1501 rc = -EINVAL;
1502 goto out;
1503 }
1504
1505 addr = *((u32 *)addrp);
1506
1507 c = policydb.ocontexts[OCON_NODE];
1508 while (c) {
1509 if (c->u.node.addr == (addr & c->u.node.mask))
1510 break;
1511 c = c->next;
1512 }
1513 break;
1514 }
1515
1516 case AF_INET6:
1517 if (addrlen != sizeof(u64) * 2) {
1518 rc = -EINVAL;
1519 goto out;
1520 }
1521 c = policydb.ocontexts[OCON_NODE6];
1522 while (c) {
1523 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1524 c->u.node6.mask))
1525 break;
1526 c = c->next;
1527 }
1528 break;
1529
1530 default:
1531 *out_sid = SECINITSID_NODE;
1532 goto out;
1533 }
1534
1535 if (c) {
1536 if (!c->sid[0]) {
1537 rc = sidtab_context_to_sid(&sidtab,
1538 &c->context[0],
1539 &c->sid[0]);
1540 if (rc)
1541 goto out;
1542 }
1543 *out_sid = c->sid[0];
1544 } else {
1545 *out_sid = SECINITSID_NODE;
1546 }
1547
1548out:
1549 POLICY_RDUNLOCK;
1550 return rc;
1551}
1552
1553#define SIDS_NEL 25
1554
1555/**
1556 * security_get_user_sids - Obtain reachable SIDs for a user.
1557 * @fromsid: starting SID
1558 * @username: username
1559 * @sids: array of reachable SIDs for user
1560 * @nel: number of elements in @sids
1561 *
1562 * Generate the set of SIDs for legal security contexts
1563 * for a given user that can be reached by @fromsid.
1564 * Set *@sids to point to a dynamically allocated
1565 * array containing the set of SIDs. Set *@nel to the
1566 * number of elements in the array.
1567 */
1568
1569int security_get_user_sids(u32 fromsid,
1570 char *username,
1571 u32 **sids,
1572 u32 *nel)
1573{
1574 struct context *fromcon, usercon;
1575 u32 *mysids, *mysids2, sid;
1576 u32 mynel = 0, maxnel = SIDS_NEL;
1577 struct user_datum *user;
1578 struct role_datum *role;
1579 struct av_decision avd;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001580 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 int rc = 0, i, j;
1582
1583 if (!ss_initialized) {
1584 *sids = NULL;
1585 *nel = 0;
1586 goto out;
1587 }
1588
1589 POLICY_RDLOCK;
1590
1591 fromcon = sidtab_search(&sidtab, fromsid);
1592 if (!fromcon) {
1593 rc = -EINVAL;
1594 goto out_unlock;
1595 }
1596
1597 user = hashtab_search(policydb.p_users.table, username);
1598 if (!user) {
1599 rc = -EINVAL;
1600 goto out_unlock;
1601 }
1602 usercon.user = user->value;
1603
James Morris89d155e2005-10-30 14:59:21 -08001604 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (!mysids) {
1606 rc = -ENOMEM;
1607 goto out_unlock;
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Stephen Smalley782ebb92005-09-03 15:55:16 -07001610 ebitmap_for_each_bit(&user->roles, rnode, i) {
1611 if (!ebitmap_node_get_bit(rnode, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 continue;
1613 role = policydb.role_val_to_struct[i];
1614 usercon.role = i+1;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001615 ebitmap_for_each_bit(&role->types, tnode, j) {
1616 if (!ebitmap_node_get_bit(tnode, j))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 continue;
1618 usercon.type = j+1;
1619
1620 if (mls_setup_user_range(fromcon, user, &usercon))
1621 continue;
1622
1623 rc = context_struct_compute_av(fromcon, &usercon,
1624 SECCLASS_PROCESS,
1625 PROCESS__TRANSITION,
1626 &avd);
1627 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1628 continue;
1629 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1630 if (rc) {
1631 kfree(mysids);
1632 goto out_unlock;
1633 }
1634 if (mynel < maxnel) {
1635 mysids[mynel++] = sid;
1636 } else {
1637 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08001638 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (!mysids2) {
1640 rc = -ENOMEM;
1641 kfree(mysids);
1642 goto out_unlock;
1643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1645 kfree(mysids);
1646 mysids = mysids2;
1647 mysids[mynel++] = sid;
1648 }
1649 }
1650 }
1651
1652 *sids = mysids;
1653 *nel = mynel;
1654
1655out_unlock:
1656 POLICY_RDUNLOCK;
1657out:
1658 return rc;
1659}
1660
1661/**
1662 * security_genfs_sid - Obtain a SID for a file in a filesystem
1663 * @fstype: filesystem type
1664 * @path: path from root of mount
1665 * @sclass: file security class
1666 * @sid: SID for path
1667 *
1668 * Obtain a SID to use for a file in a filesystem that
1669 * cannot support xattr or use a fixed labeling behavior like
1670 * transition SIDs or task SIDs.
1671 */
1672int security_genfs_sid(const char *fstype,
1673 char *path,
1674 u16 sclass,
1675 u32 *sid)
1676{
1677 int len;
1678 struct genfs *genfs;
1679 struct ocontext *c;
1680 int rc = 0, cmp = 0;
1681
1682 POLICY_RDLOCK;
1683
1684 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1685 cmp = strcmp(fstype, genfs->fstype);
1686 if (cmp <= 0)
1687 break;
1688 }
1689
1690 if (!genfs || cmp) {
1691 *sid = SECINITSID_UNLABELED;
1692 rc = -ENOENT;
1693 goto out;
1694 }
1695
1696 for (c = genfs->head; c; c = c->next) {
1697 len = strlen(c->u.name);
1698 if ((!c->v.sclass || sclass == c->v.sclass) &&
1699 (strncmp(c->u.name, path, len) == 0))
1700 break;
1701 }
1702
1703 if (!c) {
1704 *sid = SECINITSID_UNLABELED;
1705 rc = -ENOENT;
1706 goto out;
1707 }
1708
1709 if (!c->sid[0]) {
1710 rc = sidtab_context_to_sid(&sidtab,
1711 &c->context[0],
1712 &c->sid[0]);
1713 if (rc)
1714 goto out;
1715 }
1716
1717 *sid = c->sid[0];
1718out:
1719 POLICY_RDUNLOCK;
1720 return rc;
1721}
1722
1723/**
1724 * security_fs_use - Determine how to handle labeling for a filesystem.
1725 * @fstype: filesystem type
1726 * @behavior: labeling behavior
1727 * @sid: SID for filesystem (superblock)
1728 */
1729int security_fs_use(
1730 const char *fstype,
1731 unsigned int *behavior,
1732 u32 *sid)
1733{
1734 int rc = 0;
1735 struct ocontext *c;
1736
1737 POLICY_RDLOCK;
1738
1739 c = policydb.ocontexts[OCON_FSUSE];
1740 while (c) {
1741 if (strcmp(fstype, c->u.name) == 0)
1742 break;
1743 c = c->next;
1744 }
1745
1746 if (c) {
1747 *behavior = c->v.behavior;
1748 if (!c->sid[0]) {
1749 rc = sidtab_context_to_sid(&sidtab,
1750 &c->context[0],
1751 &c->sid[0]);
1752 if (rc)
1753 goto out;
1754 }
1755 *sid = c->sid[0];
1756 } else {
1757 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1758 if (rc) {
1759 *behavior = SECURITY_FS_USE_NONE;
1760 rc = 0;
1761 } else {
1762 *behavior = SECURITY_FS_USE_GENFS;
1763 }
1764 }
1765
1766out:
1767 POLICY_RDUNLOCK;
1768 return rc;
1769}
1770
1771int security_get_bools(int *len, char ***names, int **values)
1772{
1773 int i, rc = -ENOMEM;
1774
1775 POLICY_RDLOCK;
1776 *names = NULL;
1777 *values = NULL;
1778
1779 *len = policydb.p_bools.nprim;
1780 if (!*len) {
1781 rc = 0;
1782 goto out;
1783 }
1784
Jesper Juhle0795cf2006-01-09 20:54:46 -08001785 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (!*names)
1787 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Jesper Juhle0795cf2006-01-09 20:54:46 -08001789 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (!*values)
1791 goto err;
1792
1793 for (i = 0; i < *len; i++) {
1794 size_t name_len;
1795 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1796 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Jesper Juhle0795cf2006-01-09 20:54:46 -08001797 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (!(*names)[i])
1799 goto err;
1800 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1801 (*names)[i][name_len - 1] = 0;
1802 }
1803 rc = 0;
1804out:
1805 POLICY_RDUNLOCK;
1806 return rc;
1807err:
1808 if (*names) {
1809 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001810 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001812 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 goto out;
1814}
1815
1816
1817int security_set_bools(int len, int *values)
1818{
1819 int i, rc = 0;
1820 int lenp, seqno = 0;
1821 struct cond_node *cur;
1822
1823 POLICY_WRLOCK;
1824
1825 lenp = policydb.p_bools.nprim;
1826 if (len != lenp) {
1827 rc = -EFAULT;
1828 goto out;
1829 }
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00001832 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1833 audit_log(current->audit_context, GFP_ATOMIC,
1834 AUDIT_MAC_CONFIG_CHANGE,
1835 "bool=%s val=%d old_val=%d auid=%u",
1836 policydb.p_bool_val_to_name[i],
1837 !!values[i],
1838 policydb.bool_val_to_struct[i]->state,
1839 audit_get_loginuid(current->audit_context));
1840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (values[i]) {
1842 policydb.bool_val_to_struct[i]->state = 1;
1843 } else {
1844 policydb.bool_val_to_struct[i]->state = 0;
1845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1849 rc = evaluate_cond_node(&policydb, cur);
1850 if (rc)
1851 goto out;
1852 }
1853
1854 seqno = ++latest_granting;
1855
1856out:
1857 POLICY_WRUNLOCK;
1858 if (!rc) {
1859 avc_ss_reset(seqno);
1860 selnl_notify_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001861 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863 return rc;
1864}
1865
1866int security_get_bool_value(int bool)
1867{
1868 int rc = 0;
1869 int len;
1870
1871 POLICY_RDLOCK;
1872
1873 len = policydb.p_bools.nprim;
1874 if (bool >= len) {
1875 rc = -EFAULT;
1876 goto out;
1877 }
1878
1879 rc = policydb.bool_val_to_struct[bool]->state;
1880out:
1881 POLICY_RDUNLOCK;
1882 return rc;
1883}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06001884
Venkat Yekkirala08554d62006-07-24 23:27:16 -07001885/*
1886 * security_sid_mls_copy() - computes a new sid based on the given
1887 * sid and the mls portion of mls_sid.
1888 */
1889int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
1890{
1891 struct context *context1;
1892 struct context *context2;
1893 struct context newcon;
1894 char *s;
1895 u32 len;
1896 int rc = 0;
1897
Venkat Yekkirala4eb327b2006-09-19 10:24:19 -07001898 if (!ss_initialized || !selinux_mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07001899 *new_sid = sid;
1900 goto out;
1901 }
1902
1903 context_init(&newcon);
1904
1905 POLICY_RDLOCK;
1906 context1 = sidtab_search(&sidtab, sid);
1907 if (!context1) {
1908 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1909 "%d\n", sid);
1910 rc = -EINVAL;
1911 goto out_unlock;
1912 }
1913
1914 context2 = sidtab_search(&sidtab, mls_sid);
1915 if (!context2) {
1916 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1917 "%d\n", mls_sid);
1918 rc = -EINVAL;
1919 goto out_unlock;
1920 }
1921
1922 newcon.user = context1->user;
1923 newcon.role = context1->role;
1924 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06001925 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07001926 if (rc)
1927 goto out_unlock;
1928
Venkat Yekkirala08554d62006-07-24 23:27:16 -07001929 /* Check the validity of the new context. */
1930 if (!policydb_context_isvalid(&policydb, &newcon)) {
1931 rc = convert_context_handle_invalid_context(&newcon);
1932 if (rc)
1933 goto bad;
1934 }
1935
1936 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
1937 goto out_unlock;
1938
1939bad:
1940 if (!context_struct_to_string(&newcon, &s, &len)) {
1941 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1942 "security_sid_mls_copy: invalid context %s", s);
1943 kfree(s);
1944 }
1945
1946out_unlock:
1947 POLICY_RDUNLOCK;
1948 context_destroy(&newcon);
1949out:
1950 return rc;
1951}
1952
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06001953struct selinux_audit_rule {
1954 u32 au_seqno;
1955 struct context au_ctxt;
1956};
1957
1958void selinux_audit_rule_free(struct selinux_audit_rule *rule)
1959{
1960 if (rule) {
1961 context_destroy(&rule->au_ctxt);
1962 kfree(rule);
1963 }
1964}
1965
1966int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
1967 struct selinux_audit_rule **rule)
1968{
1969 struct selinux_audit_rule *tmprule;
1970 struct role_datum *roledatum;
1971 struct type_datum *typedatum;
1972 struct user_datum *userdatum;
1973 int rc = 0;
1974
1975 *rule = NULL;
1976
1977 if (!ss_initialized)
1978 return -ENOTSUPP;
1979
1980 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05001981 case AUDIT_SUBJ_USER:
1982 case AUDIT_SUBJ_ROLE:
1983 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05001984 case AUDIT_OBJ_USER:
1985 case AUDIT_OBJ_ROLE:
1986 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06001987 /* only 'equals' and 'not equals' fit user, role, and type */
1988 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
1989 return -EINVAL;
1990 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05001991 case AUDIT_SUBJ_SEN:
1992 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05001993 case AUDIT_OBJ_LEV_LOW:
1994 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06001995 /* we do not allow a range, indicated by the presense of '-' */
1996 if (strchr(rulestr, '-'))
1997 return -EINVAL;
1998 break;
1999 default:
2000 /* only the above fields are valid */
2001 return -EINVAL;
2002 }
2003
2004 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2005 if (!tmprule)
2006 return -ENOMEM;
2007
2008 context_init(&tmprule->au_ctxt);
2009
2010 POLICY_RDLOCK;
2011
2012 tmprule->au_seqno = latest_granting;
2013
2014 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002015 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002016 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002017 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2018 if (!userdatum)
2019 rc = -EINVAL;
2020 else
2021 tmprule->au_ctxt.user = userdatum->value;
2022 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002023 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002024 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002025 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2026 if (!roledatum)
2027 rc = -EINVAL;
2028 else
2029 tmprule->au_ctxt.role = roledatum->value;
2030 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002031 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002032 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002033 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2034 if (!typedatum)
2035 rc = -EINVAL;
2036 else
2037 tmprule->au_ctxt.type = typedatum->value;
2038 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002039 case AUDIT_SUBJ_SEN:
2040 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002041 case AUDIT_OBJ_LEV_LOW:
2042 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002043 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2044 break;
2045 }
2046
2047 POLICY_RDUNLOCK;
2048
2049 if (rc) {
2050 selinux_audit_rule_free(tmprule);
2051 tmprule = NULL;
2052 }
2053
2054 *rule = tmprule;
2055
2056 return rc;
2057}
2058
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002059int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002060 struct selinux_audit_rule *rule,
2061 struct audit_context *actx)
2062{
2063 struct context *ctxt;
2064 struct mls_level *level;
2065 int match = 0;
2066
2067 if (!rule) {
2068 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2069 "selinux_audit_rule_match: missing rule\n");
2070 return -ENOENT;
2071 }
2072
2073 POLICY_RDLOCK;
2074
2075 if (rule->au_seqno < latest_granting) {
2076 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2077 "selinux_audit_rule_match: stale rule\n");
2078 match = -ESTALE;
2079 goto out;
2080 }
2081
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002082 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002083 if (!ctxt) {
2084 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2085 "selinux_audit_rule_match: unrecognized SID %d\n",
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002086 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002087 match = -ENOENT;
2088 goto out;
2089 }
2090
2091 /* a field/op pair that is not caught here will simply fall through
2092 without a match */
2093 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002094 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002095 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002096 switch (op) {
2097 case AUDIT_EQUAL:
2098 match = (ctxt->user == rule->au_ctxt.user);
2099 break;
2100 case AUDIT_NOT_EQUAL:
2101 match = (ctxt->user != rule->au_ctxt.user);
2102 break;
2103 }
2104 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002105 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002106 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002107 switch (op) {
2108 case AUDIT_EQUAL:
2109 match = (ctxt->role == rule->au_ctxt.role);
2110 break;
2111 case AUDIT_NOT_EQUAL:
2112 match = (ctxt->role != rule->au_ctxt.role);
2113 break;
2114 }
2115 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002116 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002117 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002118 switch (op) {
2119 case AUDIT_EQUAL:
2120 match = (ctxt->type == rule->au_ctxt.type);
2121 break;
2122 case AUDIT_NOT_EQUAL:
2123 match = (ctxt->type != rule->au_ctxt.type);
2124 break;
2125 }
2126 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002127 case AUDIT_SUBJ_SEN:
2128 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002129 case AUDIT_OBJ_LEV_LOW:
2130 case AUDIT_OBJ_LEV_HIGH:
2131 level = ((field == AUDIT_SUBJ_SEN ||
2132 field == AUDIT_OBJ_LEV_LOW) ?
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002133 &ctxt->range.level[0] : &ctxt->range.level[1]);
2134 switch (op) {
2135 case AUDIT_EQUAL:
2136 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2137 level);
2138 break;
2139 case AUDIT_NOT_EQUAL:
2140 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2141 level);
2142 break;
2143 case AUDIT_LESS_THAN:
2144 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2145 level) &&
2146 !mls_level_eq(&rule->au_ctxt.range.level[0],
2147 level));
2148 break;
2149 case AUDIT_LESS_THAN_OR_EQUAL:
2150 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2151 level);
2152 break;
2153 case AUDIT_GREATER_THAN:
2154 match = (mls_level_dom(level,
2155 &rule->au_ctxt.range.level[0]) &&
2156 !mls_level_eq(level,
2157 &rule->au_ctxt.range.level[0]));
2158 break;
2159 case AUDIT_GREATER_THAN_OR_EQUAL:
2160 match = mls_level_dom(level,
2161 &rule->au_ctxt.range.level[0]);
2162 break;
2163 }
2164 }
2165
2166out:
2167 POLICY_RDUNLOCK;
2168 return match;
2169}
2170
2171static int (*aurule_callback)(void) = NULL;
2172
2173static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2174 u16 class, u32 perms, u32 *retained)
2175{
2176 int err = 0;
2177
2178 if (event == AVC_CALLBACK_RESET && aurule_callback)
2179 err = aurule_callback();
2180 return err;
2181}
2182
2183static int __init aurule_init(void)
2184{
2185 int err;
2186
2187 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2188 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2189 if (err)
2190 panic("avc_add_callback() failed, error %d\n", err);
2191
2192 return err;
2193}
2194__initcall(aurule_init);
2195
2196void selinux_audit_set_callback(int (*callback)(void))
2197{
2198 aurule_callback = callback;
2199}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002200
Paul Moore3de4bab2006-11-17 17:38:54 -05002201/**
2202 * security_skb_extlbl_sid - Determine the external label of a packet
2203 * @skb: the packet
2204 * @base_sid: the SELinux SID to use as a context for MLS only external labels
2205 * @sid: the packet's SID
2206 *
2207 * Description:
2208 * Check the various different forms of external packet labeling and determine
2209 * the external SID for the packet.
2210 *
2211 */
2212void security_skb_extlbl_sid(struct sk_buff *skb, u32 base_sid, u32 *sid)
2213{
2214 u32 xfrm_sid;
2215 u32 nlbl_sid;
2216
2217 selinux_skb_xfrm_sid(skb, &xfrm_sid);
2218 if (selinux_netlbl_skbuff_getsid(skb,
2219 (xfrm_sid == SECSID_NULL ?
2220 base_sid : xfrm_sid),
2221 &nlbl_sid) != 0)
2222 nlbl_sid = SECSID_NULL;
2223
2224 *sid = (nlbl_sid == SECSID_NULL ? xfrm_sid : nlbl_sid);
2225}
2226
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002227#ifdef CONFIG_NETLABEL
2228/*
2229 * This is the structure we store inside the NetLabel cache block.
2230 */
2231#define NETLBL_CACHE(x) ((struct netlbl_cache *)(x))
2232#define NETLBL_CACHE_T_NONE 0
2233#define NETLBL_CACHE_T_SID 1
2234#define NETLBL_CACHE_T_MLS 2
2235struct netlbl_cache {
2236 u32 type;
2237 union {
2238 u32 sid;
2239 struct mls_range mls_label;
2240 } data;
2241};
2242
2243/**
2244 * selinux_netlbl_cache_free - Free the NetLabel cached data
2245 * @data: the data to free
2246 *
2247 * Description:
2248 * This function is intended to be used as the free() callback inside the
2249 * netlbl_lsm_cache structure.
2250 *
2251 */
2252static void selinux_netlbl_cache_free(const void *data)
2253{
paul.moore@hp.comffb733c2006-10-04 11:46:31 -04002254 struct netlbl_cache *cache;
2255
2256 if (data == NULL)
2257 return;
2258
2259 cache = NETLBL_CACHE(data);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002260 switch (cache->type) {
2261 case NETLBL_CACHE_T_MLS:
2262 ebitmap_destroy(&cache->data.mls_label.level[0].cat);
2263 break;
2264 }
2265 kfree(data);
2266}
2267
2268/**
2269 * selinux_netlbl_cache_add - Add an entry to the NetLabel cache
2270 * @skb: the packet
2271 * @ctx: the SELinux context
2272 *
2273 * Description:
2274 * Attempt to cache the context in @ctx, which was derived from the packet in
2275 * @skb, in the NetLabel subsystem cache.
2276 *
2277 */
2278static void selinux_netlbl_cache_add(struct sk_buff *skb, struct context *ctx)
2279{
2280 struct netlbl_cache *cache = NULL;
2281 struct netlbl_lsm_secattr secattr;
2282
2283 netlbl_secattr_init(&secattr);
paul.moore@hp.comffb733c2006-10-04 11:46:31 -04002284 secattr.cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2285 if (secattr.cache == NULL)
2286 goto netlbl_cache_add_return;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002287
2288 cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
2289 if (cache == NULL)
paul.moore@hp.comffb733c2006-10-04 11:46:31 -04002290 goto netlbl_cache_add_return;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002291
2292 cache->type = NETLBL_CACHE_T_MLS;
2293 if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
2294 &ctx->range.level[0].cat) != 0)
paul.moore@hp.comffb733c2006-10-04 11:46:31 -04002295 goto netlbl_cache_add_return;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002296 cache->data.mls_label.level[1].cat.highbit =
2297 cache->data.mls_label.level[0].cat.highbit;
2298 cache->data.mls_label.level[1].cat.node =
2299 cache->data.mls_label.level[0].cat.node;
2300 cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
2301 cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
2302
Paul Moore701a90b2006-11-17 17:38:46 -05002303 secattr.cache->free = selinux_netlbl_cache_free;
2304 secattr.cache->data = (void *)cache;
2305 secattr.flags = NETLBL_SECATTR_CACHE;
2306
paul.moore@hp.comffb733c2006-10-04 11:46:31 -04002307 netlbl_cache_add(skb, &secattr);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002308
paul.moore@hp.comffb733c2006-10-04 11:46:31 -04002309netlbl_cache_add_return:
2310 netlbl_secattr_destroy(&secattr);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002311}
2312
2313/**
2314 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
2315 *
2316 * Description:
2317 * Invalidate the NetLabel security attribute mapping cache.
2318 *
2319 */
2320void selinux_netlbl_cache_invalidate(void)
2321{
2322 netlbl_cache_invalidate();
2323}
2324
2325/**
2326 * selinux_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
2327 * @skb: the network packet
2328 * @secattr: the NetLabel packet security attributes
2329 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2330 * @sid: the SELinux SID
2331 *
2332 * Description:
2333 * Convert the given NetLabel packet security attributes in @secattr into a
2334 * SELinux SID. If the @secattr field does not contain a full SELinux
2335 * SID/context then use the context in @base_sid as the foundation. If @skb
2336 * is not NULL attempt to cache as much data as possibile. Returns zero on
2337 * success, negative values on failure.
2338 *
2339 */
2340static int selinux_netlbl_secattr_to_sid(struct sk_buff *skb,
2341 struct netlbl_lsm_secattr *secattr,
2342 u32 base_sid,
2343 u32 *sid)
2344{
2345 int rc = -EIDRM;
2346 struct context *ctx;
2347 struct context ctx_new;
2348 struct netlbl_cache *cache;
2349
2350 POLICY_RDLOCK;
2351
Paul Moore701a90b2006-11-17 17:38:46 -05002352 if (secattr->flags & NETLBL_SECATTR_CACHE) {
paul.moore@hp.comffb733c2006-10-04 11:46:31 -04002353 cache = NETLBL_CACHE(secattr->cache->data);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002354 switch (cache->type) {
2355 case NETLBL_CACHE_T_SID:
2356 *sid = cache->data.sid;
2357 rc = 0;
2358 break;
2359 case NETLBL_CACHE_T_MLS:
2360 ctx = sidtab_search(&sidtab, base_sid);
2361 if (ctx == NULL)
2362 goto netlbl_secattr_to_sid_return;
2363
2364 ctx_new.user = ctx->user;
2365 ctx_new.role = ctx->role;
2366 ctx_new.type = ctx->type;
2367 ctx_new.range.level[0].sens =
2368 cache->data.mls_label.level[0].sens;
2369 ctx_new.range.level[0].cat.highbit =
2370 cache->data.mls_label.level[0].cat.highbit;
2371 ctx_new.range.level[0].cat.node =
2372 cache->data.mls_label.level[0].cat.node;
2373 ctx_new.range.level[1].sens =
2374 cache->data.mls_label.level[1].sens;
2375 ctx_new.range.level[1].cat.highbit =
2376 cache->data.mls_label.level[1].cat.highbit;
2377 ctx_new.range.level[1].cat.node =
2378 cache->data.mls_label.level[1].cat.node;
2379
2380 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2381 break;
2382 default:
2383 goto netlbl_secattr_to_sid_return;
2384 }
Paul Moore701a90b2006-11-17 17:38:46 -05002385 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002386 ctx = sidtab_search(&sidtab, base_sid);
2387 if (ctx == NULL)
2388 goto netlbl_secattr_to_sid_return;
2389
2390 ctx_new.user = ctx->user;
2391 ctx_new.role = ctx->role;
2392 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05002393 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05002394 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05002395 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2396 secattr->mls_cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002397 goto netlbl_secattr_to_sid_return;
2398 ctx_new.range.level[1].cat.highbit =
2399 ctx_new.range.level[0].cat.highbit;
2400 ctx_new.range.level[1].cat.node =
2401 ctx_new.range.level[0].cat.node;
2402 } else {
2403 ebitmap_init(&ctx_new.range.level[0].cat);
2404 ebitmap_init(&ctx_new.range.level[1].cat);
2405 }
2406 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2407 goto netlbl_secattr_to_sid_return_cleanup;
2408
2409 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2410 if (rc != 0)
2411 goto netlbl_secattr_to_sid_return_cleanup;
2412
2413 if (skb != NULL)
2414 selinux_netlbl_cache_add(skb, &ctx_new);
2415 ebitmap_destroy(&ctx_new.range.level[0].cat);
2416 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002417 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002418 rc = 0;
2419 }
2420
2421netlbl_secattr_to_sid_return:
2422 POLICY_RDUNLOCK;
2423 return rc;
2424netlbl_secattr_to_sid_return_cleanup:
2425 ebitmap_destroy(&ctx_new.range.level[0].cat);
2426 goto netlbl_secattr_to_sid_return;
2427}
2428
2429/**
2430 * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
2431 * @skb: the packet
2432 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2433 * @sid: the SID
2434 *
2435 * Description:
2436 * Call the NetLabel mechanism to get the security attributes of the given
2437 * packet and use those attributes to determine the correct context/SID to
2438 * assign to the packet. Returns zero on success, negative values on failure.
2439 *
2440 */
Paul Moore3de4bab2006-11-17 17:38:54 -05002441int selinux_netlbl_skbuff_getsid(struct sk_buff *skb, u32 base_sid, u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002442{
2443 int rc;
2444 struct netlbl_lsm_secattr secattr;
2445
2446 netlbl_secattr_init(&secattr);
2447 rc = netlbl_skbuff_getattr(skb, &secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05002448 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002449 rc = selinux_netlbl_secattr_to_sid(skb,
2450 &secattr,
2451 base_sid,
2452 sid);
Paul Moore701a90b2006-11-17 17:38:46 -05002453 else
2454 *sid = SECSID_NULL;
paul.moore@hp.comffb733c2006-10-04 11:46:31 -04002455 netlbl_secattr_destroy(&secattr);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002456
2457 return rc;
2458}
2459
2460/**
2461 * selinux_netlbl_socket_setsid - Label a socket using the NetLabel mechanism
2462 * @sock: the socket to label
2463 * @sid: the SID to use
2464 *
2465 * Description:
2466 * Attempt to label a socket using the NetLabel mechanism using the given
Paul Moore9f2ad662006-11-17 17:38:53 -05002467 * SID. Returns zero values on success, negative values on failure. The
2468 * caller is responsibile for calling rcu_read_lock() before calling this
2469 * this function and rcu_read_unlock() after this function returns.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002470 *
2471 */
2472static int selinux_netlbl_socket_setsid(struct socket *sock, u32 sid)
2473{
2474 int rc = -ENOENT;
2475 struct sk_security_struct *sksec = sock->sk->sk_security;
2476 struct netlbl_lsm_secattr secattr;
2477 struct context *ctx;
2478
2479 if (!ss_initialized)
2480 return 0;
2481
Paul Moorebf0edf32006-10-11 19:10:48 -04002482 netlbl_secattr_init(&secattr);
2483
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002484 POLICY_RDLOCK;
2485
2486 ctx = sidtab_search(&sidtab, sid);
2487 if (ctx == NULL)
2488 goto netlbl_socket_setsid_return;
2489
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002490 secattr.domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2491 GFP_ATOMIC);
Paul Moore02752762006-11-29 13:18:18 -05002492 secattr.flags |= NETLBL_SECATTR_DOMAIN;
2493 mls_export_netlbl_lvl(ctx, &secattr);
2494 rc = mls_export_netlbl_cat(ctx, &secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04002495 if (rc != 0)
2496 goto netlbl_socket_setsid_return;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002497
2498 rc = netlbl_socket_setattr(sock, &secattr);
Paul Moore9f2ad662006-11-17 17:38:53 -05002499 if (rc == 0) {
Paul Moore79795122007-01-05 15:08:21 -05002500 spin_lock_bh(&sksec->nlbl_lock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002501 sksec->nlbl_state = NLBL_LABELED;
Paul Moore79795122007-01-05 15:08:21 -05002502 spin_unlock_bh(&sksec->nlbl_lock);
Paul Moore9f2ad662006-11-17 17:38:53 -05002503 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002504
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002505netlbl_socket_setsid_return:
2506 POLICY_RDUNLOCK;
Paul Moorebf0edf32006-10-11 19:10:48 -04002507 netlbl_secattr_destroy(&secattr);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002508 return rc;
2509}
2510
2511/**
Paul Moore9f2ad662006-11-17 17:38:53 -05002512 * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
2513 * @ssec: the sk_security_struct
2514 * @family: the socket family
2515 *
2516 * Description:
2517 * Called when the NetLabel state of a sk_security_struct needs to be reset.
2518 * The caller is responsibile for all the NetLabel sk_security_struct locking.
2519 *
2520 */
2521void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec,
2522 int family)
2523{
2524 if (family == PF_INET)
2525 ssec->nlbl_state = NLBL_REQUIRE;
2526 else
2527 ssec->nlbl_state = NLBL_UNSET;
2528}
2529
2530/**
Paul Moore99f59ed2006-08-29 17:53:48 -07002531 * selinux_netlbl_sk_security_init - Setup the NetLabel fields
2532 * @ssec: the sk_security_struct
2533 * @family: the socket family
2534 *
2535 * Description:
2536 * Called when a new sk_security_struct is allocated to initialize the NetLabel
2537 * fields.
2538 *
2539 */
2540void selinux_netlbl_sk_security_init(struct sk_security_struct *ssec,
2541 int family)
2542{
Paul Moore9f2ad662006-11-17 17:38:53 -05002543 /* No locking needed, we are the only one who has access to ssec */
2544 selinux_netlbl_sk_security_reset(ssec, family);
2545 spin_lock_init(&ssec->nlbl_lock);
Paul Moore99f59ed2006-08-29 17:53:48 -07002546}
2547
2548/**
Paul Moore9f2ad662006-11-17 17:38:53 -05002549 * selinux_netlbl_sk_security_clone - Copy the NetLabel fields
Paul Moore99f59ed2006-08-29 17:53:48 -07002550 * @ssec: the original sk_security_struct
2551 * @newssec: the cloned sk_security_struct
2552 *
2553 * Description:
2554 * Clone the NetLabel specific sk_security_struct fields from @ssec to
2555 * @newssec.
2556 *
2557 */
Paul Moore9f2ad662006-11-17 17:38:53 -05002558void selinux_netlbl_sk_security_clone(struct sk_security_struct *ssec,
Paul Moore99f59ed2006-08-29 17:53:48 -07002559 struct sk_security_struct *newssec)
2560{
Paul Moore9f2ad662006-11-17 17:38:53 -05002561 /* We don't need to take newssec->nlbl_lock because we are the only
2562 * thread with access to newssec, but we do need to take the RCU read
2563 * lock as other threads could have access to ssec */
2564 rcu_read_lock();
2565 selinux_netlbl_sk_security_reset(newssec, ssec->sk->sk_family);
Paul Moore99f59ed2006-08-29 17:53:48 -07002566 newssec->sclass = ssec->sclass;
Paul Moore9f2ad662006-11-17 17:38:53 -05002567 rcu_read_unlock();
Paul Moore99f59ed2006-08-29 17:53:48 -07002568}
2569
2570/**
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002571 * selinux_netlbl_socket_post_create - Label a socket using NetLabel
2572 * @sock: the socket to label
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002573 *
2574 * Description:
2575 * Attempt to label a socket using the NetLabel mechanism using the given
2576 * SID. Returns zero values on success, negative values on failure.
2577 *
2578 */
Paul Moore9f2ad662006-11-17 17:38:53 -05002579int selinux_netlbl_socket_post_create(struct socket *sock)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002580{
Paul Moore9f2ad662006-11-17 17:38:53 -05002581 int rc = 0;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002582 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
2583 struct sk_security_struct *sksec = sock->sk->sk_security;
2584
Paul Moore99f59ed2006-08-29 17:53:48 -07002585 sksec->sclass = isec->sclass;
2586
Paul Moore9f2ad662006-11-17 17:38:53 -05002587 rcu_read_lock();
2588 if (sksec->nlbl_state == NLBL_REQUIRE)
2589 rc = selinux_netlbl_socket_setsid(sock, sksec->sid);
2590 rcu_read_unlock();
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002591
Paul Moore9f2ad662006-11-17 17:38:53 -05002592 return rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002593}
2594
2595/**
2596 * selinux_netlbl_sock_graft - Netlabel the new socket
2597 * @sk: the new connection
2598 * @sock: the new socket
2599 *
2600 * Description:
2601 * The connection represented by @sk is being grafted onto @sock so set the
2602 * socket's NetLabel to match the SID of @sk.
2603 *
2604 */
2605void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
2606{
2607 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
2608 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore14a72f52006-09-25 15:52:01 -07002609 struct netlbl_lsm_secattr secattr;
2610 u32 nlbl_peer_sid;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002611
Paul Moore99f59ed2006-08-29 17:53:48 -07002612 sksec->sclass = isec->sclass;
2613
Paul Moore9f2ad662006-11-17 17:38:53 -05002614 rcu_read_lock();
2615
2616 if (sksec->nlbl_state != NLBL_REQUIRE) {
2617 rcu_read_unlock();
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002618 return;
Paul Moore9f2ad662006-11-17 17:38:53 -05002619 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002620
Paul Moore14a72f52006-09-25 15:52:01 -07002621 netlbl_secattr_init(&secattr);
2622 if (netlbl_sock_getattr(sk, &secattr) == 0 &&
Paul Moore701a90b2006-11-17 17:38:46 -05002623 secattr.flags != NETLBL_SECATTR_NONE &&
Paul Moore14a72f52006-09-25 15:52:01 -07002624 selinux_netlbl_secattr_to_sid(NULL,
2625 &secattr,
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002626 SECINITSID_UNLABELED,
Paul Moore14a72f52006-09-25 15:52:01 -07002627 &nlbl_peer_sid) == 0)
2628 sksec->peer_sid = nlbl_peer_sid;
paul.moore@hp.comffb733c2006-10-04 11:46:31 -04002629 netlbl_secattr_destroy(&secattr);
Paul Moore14a72f52006-09-25 15:52:01 -07002630
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002631 /* Try to set the NetLabel on the socket to save time later, if we fail
2632 * here we will pick up the pieces in later calls to
2633 * selinux_netlbl_inode_permission(). */
2634 selinux_netlbl_socket_setsid(sock, sksec->sid);
Paul Moore9f2ad662006-11-17 17:38:53 -05002635
2636 rcu_read_unlock();
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002637}
2638
2639/**
Paul Mooree448e932006-08-29 17:55:38 -07002640 * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002641 * @inode: the file descriptor's inode
2642 * @mask: the permission mask
2643 *
2644 * Description:
Paul Mooree448e932006-08-29 17:55:38 -07002645 * Looks at a file's inode and if it is marked as a socket protected by
2646 * NetLabel then verify that the socket has been labeled, if not try to label
2647 * the socket now with the inode's SID. Returns zero on success, negative
2648 * values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002649 *
2650 */
Paul Mooree448e932006-08-29 17:55:38 -07002651int selinux_netlbl_inode_permission(struct inode *inode, int mask)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002652{
2653 int rc;
Paul Mooree448e932006-08-29 17:55:38 -07002654 struct sk_security_struct *sksec;
2655 struct socket *sock;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002656
Paul Moore9f2ad662006-11-17 17:38:53 -05002657 if (!S_ISSOCK(inode->i_mode) ||
2658 ((mask & (MAY_WRITE | MAY_APPEND)) == 0))
Paul Mooree448e932006-08-29 17:55:38 -07002659 return 0;
Paul Mooree448e932006-08-29 17:55:38 -07002660 sock = SOCKET_I(inode);
Paul Mooree448e932006-08-29 17:55:38 -07002661 sksec = sock->sk->sk_security;
Paul Moore9f2ad662006-11-17 17:38:53 -05002662
2663 rcu_read_lock();
2664 if (sksec->nlbl_state != NLBL_REQUIRE) {
2665 rcu_read_unlock();
2666 return 0;
2667 }
Parag Warudkar9883a132007-01-02 21:09:31 +01002668 local_bh_disable();
2669 bh_lock_sock_nested(sock->sk);
Paul Moore9f2ad662006-11-17 17:38:53 -05002670 rc = selinux_netlbl_socket_setsid(sock, sksec->sid);
Parag Warudkar9883a132007-01-02 21:09:31 +01002671 bh_unlock_sock(sock->sk);
2672 local_bh_enable();
Paul Moore9f2ad662006-11-17 17:38:53 -05002673 rcu_read_unlock();
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002674
2675 return rc;
2676}
2677
2678/**
2679 * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
2680 * @sksec: the sock's sk_security_struct
2681 * @skb: the packet
2682 * @ad: the audit data
2683 *
2684 * Description:
2685 * Fetch the NetLabel security attributes from @skb and perform an access check
2686 * against the receiving socket. Returns zero on success, negative values on
2687 * error.
2688 *
2689 */
2690int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
2691 struct sk_buff *skb,
2692 struct avc_audit_data *ad)
2693{
2694 int rc;
2695 u32 netlbl_sid;
2696 u32 recv_perm;
2697
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002698 rc = selinux_netlbl_skbuff_getsid(skb,
2699 SECINITSID_UNLABELED,
2700 &netlbl_sid);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002701 if (rc != 0)
2702 return rc;
2703
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002704 if (netlbl_sid == SECSID_NULL)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002705 return 0;
2706
2707 switch (sksec->sclass) {
2708 case SECCLASS_UDP_SOCKET:
Paul Mooredf2115c2006-09-25 15:53:13 -07002709 recv_perm = UDP_SOCKET__RECVFROM;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002710 break;
2711 case SECCLASS_TCP_SOCKET:
Paul Mooredf2115c2006-09-25 15:53:13 -07002712 recv_perm = TCP_SOCKET__RECVFROM;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002713 break;
2714 default:
Paul Mooredf2115c2006-09-25 15:53:13 -07002715 recv_perm = RAWIP_SOCKET__RECVFROM;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002716 }
2717
2718 rc = avc_has_perm(sksec->sid,
2719 netlbl_sid,
2720 sksec->sclass,
2721 recv_perm,
2722 ad);
2723 if (rc == 0)
2724 return 0;
2725
2726 netlbl_skbuff_err(skb, rc);
2727 return rc;
2728}
2729
2730/**
Paul Mooref8687af2006-10-30 15:22:15 -08002731 * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
2732 * @sock: the socket
2733 * @level: the socket level or protocol
2734 * @optname: the socket option name
2735 *
2736 * Description:
2737 * Check the setsockopt() call and if the user is trying to replace the IP
2738 * options on a socket and a NetLabel is in place for the socket deny the
2739 * access; otherwise allow the access. Returns zero when the access is
2740 * allowed, -EACCES when denied, and other negative values on error.
2741 *
2742 */
2743int selinux_netlbl_socket_setsockopt(struct socket *sock,
2744 int level,
2745 int optname)
2746{
2747 int rc = 0;
Paul Mooref8687af2006-10-30 15:22:15 -08002748 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Mooref8687af2006-10-30 15:22:15 -08002749 struct netlbl_lsm_secattr secattr;
2750
Paul Moore9f2ad662006-11-17 17:38:53 -05002751 rcu_read_lock();
Paul Mooref8687af2006-10-30 15:22:15 -08002752 if (level == IPPROTO_IP && optname == IP_OPTIONS &&
2753 sksec->nlbl_state == NLBL_LABELED) {
2754 netlbl_secattr_init(&secattr);
2755 rc = netlbl_socket_getattr(sock, &secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05002756 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
Paul Mooref8687af2006-10-30 15:22:15 -08002757 rc = -EACCES;
2758 netlbl_secattr_destroy(&secattr);
2759 }
Paul Moore9f2ad662006-11-17 17:38:53 -05002760 rcu_read_unlock();
Paul Mooref8687af2006-10-30 15:22:15 -08002761
2762 return rc;
2763}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002764#endif /* CONFIG_NETLABEL */