vserver 1.9.5.x5
[linux-2.6.git] / security / selinux / ss / services.c
1 /*
2  * Implementation of the security services.
3  *
4  * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
5  *           James Morris <jmorris@redhat.com>
6  *
7  *  Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
8  *
9  *      This program is free software; you can redistribute it and/or modify
10  *      it under the terms of the GNU General Public License version 2,
11  *      as published by the Free Software Foundation.
12  *
13  * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
14  *
15  *      Added conditional policy language extensions
16  *
17  * Copyright (C) 2003 - 2004 Tresys Technology, LLC
18  *      This program is free software; you can redistribute it and/or modify
19  *      it under the terms of the GNU General Public License as published by
20  *      the Free Software Foundation, version 2.
21  */
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/spinlock.h>
26 #include <linux/errno.h>
27 #include <linux/in.h>
28 #include <linux/sched.h>
29 #include <linux/audit.h>
30 #include <asm/semaphore.h>
31 #include "flask.h"
32 #include "avc.h"
33 #include "avc_ss.h"
34 #include "security.h"
35 #include "context.h"
36 #include "policydb.h"
37 #include "sidtab.h"
38 #include "services.h"
39 #include "conditional.h"
40 #include "mls.h"
41
42 extern void selnl_notify_policyload(u32 seqno);
43 unsigned int policydb_loaded_version;
44
45 static DEFINE_RWLOCK(policy_rwlock);
46 #define POLICY_RDLOCK read_lock(&policy_rwlock)
47 #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
48 #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
49 #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
50
51 static DECLARE_MUTEX(load_sem);
52 #define LOAD_LOCK down(&load_sem)
53 #define LOAD_UNLOCK up(&load_sem)
54
55 struct sidtab sidtab;
56 struct policydb policydb;
57 int ss_initialized = 0;
58
59 /*
60  * The largest sequence number that has been used when
61  * providing an access decision to the access vector cache.
62  * The sequence number only changes when a policy change
63  * occurs.
64  */
65 static u32 latest_granting = 0;
66
67 /*
68  * Return the boolean value of a constraint expression
69  * when it is applied to the specified source and target
70  * security contexts.
71  */
72 static int constraint_expr_eval(struct context *scontext,
73                                 struct context *tcontext,
74                                 struct constraint_expr *cexpr)
75 {
76         u32 val1, val2;
77         struct context *c;
78         struct role_datum *r1, *r2;
79         struct constraint_expr *e;
80         int s[CEXPR_MAXDEPTH];
81         int sp = -1;
82
83         for (e = cexpr; e; e = e->next) {
84                 switch (e->expr_type) {
85                 case CEXPR_NOT:
86                         BUG_ON(sp < 0);
87                         s[sp] = !s[sp];
88                         break;
89                 case CEXPR_AND:
90                         BUG_ON(sp < 1);
91                         sp--;
92                         s[sp] &= s[sp+1];
93                         break;
94                 case CEXPR_OR:
95                         BUG_ON(sp < 1);
96                         sp--;
97                         s[sp] |= s[sp+1];
98                         break;
99                 case CEXPR_ATTR:
100                         if (sp == (CEXPR_MAXDEPTH-1))
101                                 return 0;
102                         switch (e->attr) {
103                         case CEXPR_USER:
104                                 val1 = scontext->user;
105                                 val2 = tcontext->user;
106                                 break;
107                         case CEXPR_TYPE:
108                                 val1 = scontext->type;
109                                 val2 = tcontext->type;
110                                 break;
111                         case CEXPR_ROLE:
112                                 val1 = scontext->role;
113                                 val2 = tcontext->role;
114                                 r1 = policydb.role_val_to_struct[val1 - 1];
115                                 r2 = policydb.role_val_to_struct[val2 - 1];
116                                 switch (e->op) {
117                                 case CEXPR_DOM:
118                                         s[++sp] = ebitmap_get_bit(&r1->dominates,
119                                                                   val2 - 1);
120                                         continue;
121                                 case CEXPR_DOMBY:
122                                         s[++sp] = ebitmap_get_bit(&r2->dominates,
123                                                                   val1 - 1);
124                                         continue;
125                                 case CEXPR_INCOMP:
126                                         s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
127                                                                      val2 - 1) &&
128                                                     !ebitmap_get_bit(&r2->dominates,
129                                                                      val1 - 1) );
130                                         continue;
131                                 default:
132                                         break;
133                                 }
134                                 break;
135                         default:
136                                 BUG();
137                                 return 0;
138                         }
139
140                         switch (e->op) {
141                         case CEXPR_EQ:
142                                 s[++sp] = (val1 == val2);
143                                 break;
144                         case CEXPR_NEQ:
145                                 s[++sp] = (val1 != val2);
146                                 break;
147                         default:
148                                 BUG();
149                                 return 0;
150                         }
151                         break;
152                 case CEXPR_NAMES:
153                         if (sp == (CEXPR_MAXDEPTH-1))
154                                 return 0;
155                         c = scontext;
156                         if (e->attr & CEXPR_TARGET)
157                                 c = tcontext;
158                         if (e->attr & CEXPR_USER)
159                                 val1 = c->user;
160                         else if (e->attr & CEXPR_ROLE)
161                                 val1 = c->role;
162                         else if (e->attr & CEXPR_TYPE)
163                                 val1 = c->type;
164                         else {
165                                 BUG();
166                                 return 0;
167                         }
168
169                         switch (e->op) {
170                         case CEXPR_EQ:
171                                 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
172                                 break;
173                         case CEXPR_NEQ:
174                                 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
175                                 break;
176                         default:
177                                 BUG();
178                                 return 0;
179                         }
180                         break;
181                 default:
182                         BUG();
183                         return 0;
184                 }
185         }
186
187         BUG_ON(sp != 0);
188         return s[0];
189 }
190
191 /*
192  * Compute access vectors based on a context structure pair for
193  * the permissions in a particular class.
194  */
195 static int context_struct_compute_av(struct context *scontext,
196                                      struct context *tcontext,
197                                      u16 tclass,
198                                      u32 requested,
199                                      struct av_decision *avd)
200 {
201         struct constraint_node *constraint;
202         struct role_allow *ra;
203         struct avtab_key avkey;
204         struct avtab_datum *avdatum;
205         struct class_datum *tclass_datum;
206
207         /*
208          * Remap extended Netlink classes for old policy versions.
209          * Do this here rather than socket_type_to_security_class()
210          * in case a newer policy version is loaded, allowing sockets
211          * to remain in the correct class.
212          */
213         if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
214                 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
215                     tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
216                         tclass = SECCLASS_NETLINK_SOCKET;
217
218         if (!tclass || tclass > policydb.p_classes.nprim) {
219                 printk(KERN_ERR "security_compute_av:  unrecognized class %d\n",
220                        tclass);
221                 return -EINVAL;
222         }
223         tclass_datum = policydb.class_val_to_struct[tclass - 1];
224
225         /*
226          * Initialize the access vectors to the default values.
227          */
228         avd->allowed = 0;
229         avd->decided = 0xffffffff;
230         avd->auditallow = 0;
231         avd->auditdeny = 0xffffffff;
232         avd->seqno = latest_granting;
233
234         /*
235          * If a specific type enforcement rule was defined for
236          * this permission check, then use it.
237          */
238         avkey.source_type = scontext->type;
239         avkey.target_type = tcontext->type;
240         avkey.target_class = tclass;
241         avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_AV);
242         if (avdatum) {
243                 if (avdatum->specified & AVTAB_ALLOWED)
244                         avd->allowed = avtab_allowed(avdatum);
245                 if (avdatum->specified & AVTAB_AUDITDENY)
246                         avd->auditdeny = avtab_auditdeny(avdatum);
247                 if (avdatum->specified & AVTAB_AUDITALLOW)
248                         avd->auditallow = avtab_auditallow(avdatum);
249         }
250
251         /* Check conditional av table for additional permissions */
252         cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
253
254         /*
255          * Remove any permissions prohibited by the MLS policy.
256          */
257         mls_compute_av(scontext, tcontext, tclass_datum, &avd->allowed);
258
259         /*
260          * Remove any permissions prohibited by a constraint.
261          */
262         constraint = tclass_datum->constraints;
263         while (constraint) {
264                 if ((constraint->permissions & (avd->allowed)) &&
265                     !constraint_expr_eval(scontext, tcontext,
266                                           constraint->expr)) {
267                         avd->allowed = (avd->allowed) & ~(constraint->permissions);
268                 }
269                 constraint = constraint->next;
270         }
271
272         /*
273          * If checking process transition permission and the
274          * role is changing, then check the (current_role, new_role)
275          * pair.
276          */
277         if (tclass == SECCLASS_PROCESS &&
278             (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
279             scontext->role != tcontext->role) {
280                 for (ra = policydb.role_allow; ra; ra = ra->next) {
281                         if (scontext->role == ra->role &&
282                             tcontext->role == ra->new_role)
283                                 break;
284                 }
285                 if (!ra)
286                         avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
287                                                         PROCESS__DYNTRANSITION);
288         }
289
290         return 0;
291 }
292
293 /**
294  * security_compute_av - Compute access vector decisions.
295  * @ssid: source security identifier
296  * @tsid: target security identifier
297  * @tclass: target security class
298  * @requested: requested permissions
299  * @avd: access vector decisions
300  *
301  * Compute a set of access vector decisions based on the
302  * SID pair (@ssid, @tsid) for the permissions in @tclass.
303  * Return -%EINVAL if any of the parameters are invalid or %0
304  * if the access vector decisions were computed successfully.
305  */
306 int security_compute_av(u32 ssid,
307                         u32 tsid,
308                         u16 tclass,
309                         u32 requested,
310                         struct av_decision *avd)
311 {
312         struct context *scontext = NULL, *tcontext = NULL;
313         int rc = 0;
314
315         if (!ss_initialized) {
316                 avd->allowed = requested;
317                 avd->decided = requested;
318                 avd->auditallow = 0;
319                 avd->auditdeny = 0xffffffff;
320                 avd->seqno = latest_granting;
321                 return 0;
322         }
323
324         POLICY_RDLOCK;
325
326         scontext = sidtab_search(&sidtab, ssid);
327         if (!scontext) {
328                 printk(KERN_ERR "security_compute_av:  unrecognized SID %d\n",
329                        ssid);
330                 rc = -EINVAL;
331                 goto out;
332         }
333         tcontext = sidtab_search(&sidtab, tsid);
334         if (!tcontext) {
335                 printk(KERN_ERR "security_compute_av:  unrecognized SID %d\n",
336                        tsid);
337                 rc = -EINVAL;
338                 goto out;
339         }
340
341         rc = context_struct_compute_av(scontext, tcontext, tclass,
342                                        requested, avd);
343 out:
344         POLICY_RDUNLOCK;
345         return rc;
346 }
347
348 /*
349  * Write the security context string representation of
350  * the context structure `context' into a dynamically
351  * allocated string of the correct size.  Set `*scontext'
352  * to point to this string and set `*scontext_len' to
353  * the length of the string.
354  */
355 int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
356 {
357         char *scontextp;
358
359         *scontext = NULL;
360         *scontext_len = 0;
361
362         /* Compute the size of the context. */
363         *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
364         *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
365         *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
366         *scontext_len += mls_compute_context_len(context);
367
368         /* Allocate space for the context; caller must free this space. */
369         scontextp = kmalloc(*scontext_len+1,GFP_ATOMIC);
370         if (!scontextp) {
371                 return -ENOMEM;
372         }
373         *scontext = scontextp;
374
375         /*
376          * Copy the user name, role name and type name into the context.
377          */
378         sprintf(scontextp, "%s:%s:%s:",
379                 policydb.p_user_val_to_name[context->user - 1],
380                 policydb.p_role_val_to_name[context->role - 1],
381                 policydb.p_type_val_to_name[context->type - 1]);
382         scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
383                      1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
384                      1 + strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
385
386         mls_sid_to_context(context, &scontextp);
387
388         scontextp--;
389         *scontextp = 0;
390
391         return 0;
392 }
393
394 #include "initial_sid_to_string.h"
395
396 /**
397  * security_sid_to_context - Obtain a context for a given SID.
398  * @sid: security identifier, SID
399  * @scontext: security context
400  * @scontext_len: length in bytes
401  *
402  * Write the string representation of the context associated with @sid
403  * into a dynamically allocated string of the correct size.  Set @scontext
404  * to point to this string and set @scontext_len to the length of the string.
405  */
406 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
407 {
408         struct context *context;
409         int rc = 0;
410
411         if (!ss_initialized) {
412                 if (sid <= SECINITSID_NUM) {
413                         char *scontextp;
414
415                         *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
416                         scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
417                         strcpy(scontextp, initial_sid_to_string[sid]);
418                         *scontext = scontextp;
419                         goto out;
420                 }
421                 printk(KERN_ERR "security_sid_to_context:  called before initial "
422                        "load_policy on unknown SID %d\n", sid);
423                 rc = -EINVAL;
424                 goto out;
425         }
426         POLICY_RDLOCK;
427         context = sidtab_search(&sidtab, sid);
428         if (!context) {
429                 printk(KERN_ERR "security_sid_to_context:  unrecognized SID "
430                        "%d\n", sid);
431                 rc = -EINVAL;
432                 goto out_unlock;
433         }
434         rc = context_struct_to_string(context, scontext, scontext_len);
435 out_unlock:
436         POLICY_RDUNLOCK;
437 out:
438         return rc;
439
440 }
441
442 /**
443  * security_context_to_sid - Obtain a SID for a given security context.
444  * @scontext: security context
445  * @scontext_len: length in bytes
446  * @sid: security identifier, SID
447  *
448  * Obtains a SID associated with the security context that
449  * has the string representation specified by @scontext.
450  * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
451  * memory is available, or 0 on success.
452  */
453 int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
454 {
455         char *scontext2;
456         struct context context;
457         struct role_datum *role;
458         struct type_datum *typdatum;
459         struct user_datum *usrdatum;
460         char *scontextp, *p, oldc;
461         int rc = 0;
462
463         if (!ss_initialized) {
464                 int i;
465
466                 for (i = 1; i < SECINITSID_NUM; i++) {
467                         if (!strcmp(initial_sid_to_string[i], scontext)) {
468                                 *sid = i;
469                                 goto out;
470                         }
471                 }
472                 *sid = SECINITSID_KERNEL;
473                 goto out;
474         }
475         *sid = SECSID_NULL;
476
477         /* Copy the string so that we can modify the copy as we parse it.
478            The string should already by null terminated, but we append a
479            null suffix to the copy to avoid problems with the existing
480            attr package, which doesn't view the null terminator as part
481            of the attribute value. */
482         scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
483         if (!scontext2) {
484                 rc = -ENOMEM;
485                 goto out;
486         }
487         memcpy(scontext2, scontext, scontext_len);
488         scontext2[scontext_len] = 0;
489
490         context_init(&context);
491         *sid = SECSID_NULL;
492
493         POLICY_RDLOCK;
494
495         /* Parse the security context. */
496
497         rc = -EINVAL;
498         scontextp = (char *) scontext2;
499
500         /* Extract the user. */
501         p = scontextp;
502         while (*p && *p != ':')
503                 p++;
504
505         if (*p == 0)
506                 goto out_unlock;
507
508         *p++ = 0;
509
510         usrdatum = hashtab_search(policydb.p_users.table, scontextp);
511         if (!usrdatum)
512                 goto out_unlock;
513
514         context.user = usrdatum->value;
515
516         /* Extract role. */
517         scontextp = p;
518         while (*p && *p != ':')
519                 p++;
520
521         if (*p == 0)
522                 goto out_unlock;
523
524         *p++ = 0;
525
526         role = hashtab_search(policydb.p_roles.table, scontextp);
527         if (!role)
528                 goto out_unlock;
529         context.role = role->value;
530
531         /* Extract type. */
532         scontextp = p;
533         while (*p && *p != ':')
534                 p++;
535         oldc = *p;
536         *p++ = 0;
537
538         typdatum = hashtab_search(policydb.p_types.table, scontextp);
539         if (!typdatum)
540                 goto out_unlock;
541
542         context.type = typdatum->value;
543
544         rc = mls_context_to_sid(oldc, &p, &context);
545         if (rc)
546                 goto out_unlock;
547
548         if ((p - scontext2) < scontext_len) {
549                 rc = -EINVAL;
550                 goto out_unlock;
551         }
552
553         /* Check the validity of the new context. */
554         if (!policydb_context_isvalid(&policydb, &context)) {
555                 rc = -EINVAL;
556                 goto out_unlock;
557         }
558         /* Obtain the new sid. */
559         rc = sidtab_context_to_sid(&sidtab, &context, sid);
560 out_unlock:
561         POLICY_RDUNLOCK;
562         context_destroy(&context);
563         kfree(scontext2);
564 out:
565         return rc;
566 }
567
568 static int compute_sid_handle_invalid_context(
569         struct context *scontext,
570         struct context *tcontext,
571         u16 tclass,
572         struct context *newcontext)
573 {
574         char *s = NULL, *t = NULL, *n = NULL;
575         u32 slen, tlen, nlen;
576
577         if (context_struct_to_string(scontext, &s, &slen) < 0)
578                 goto out;
579         if (context_struct_to_string(tcontext, &t, &tlen) < 0)
580                 goto out;
581         if (context_struct_to_string(newcontext, &n, &nlen) < 0)
582                 goto out;
583         audit_log(current->audit_context,
584                   "security_compute_sid:  invalid context %s"
585                   " for scontext=%s"
586                   " tcontext=%s"
587                   " tclass=%s",
588                   n, s, t, policydb.p_class_val_to_name[tclass-1]);
589 out:
590         kfree(s);
591         kfree(t);
592         kfree(n);
593         if (!selinux_enforcing)
594                 return 0;
595         return -EACCES;
596 }
597
598 static int security_compute_sid(u32 ssid,
599                                 u32 tsid,
600                                 u16 tclass,
601                                 u32 specified,
602                                 u32 *out_sid)
603 {
604         struct context *scontext = NULL, *tcontext = NULL, newcontext;
605         struct role_trans *roletr = NULL;
606         struct avtab_key avkey;
607         struct avtab_datum *avdatum;
608         struct avtab_node *node;
609         unsigned int type_change = 0;
610         int rc = 0;
611
612         if (!ss_initialized) {
613                 switch (tclass) {
614                 case SECCLASS_PROCESS:
615                         *out_sid = ssid;
616                         break;
617                 default:
618                         *out_sid = tsid;
619                         break;
620                 }
621                 goto out;
622         }
623
624         POLICY_RDLOCK;
625
626         scontext = sidtab_search(&sidtab, ssid);
627         if (!scontext) {
628                 printk(KERN_ERR "security_compute_sid:  unrecognized SID %d\n",
629                        ssid);
630                 rc = -EINVAL;
631                 goto out_unlock;
632         }
633         tcontext = sidtab_search(&sidtab, tsid);
634         if (!tcontext) {
635                 printk(KERN_ERR "security_compute_sid:  unrecognized SID %d\n",
636                        tsid);
637                 rc = -EINVAL;
638                 goto out_unlock;
639         }
640
641         context_init(&newcontext);
642
643         /* Set the user identity. */
644         switch (specified) {
645         case AVTAB_TRANSITION:
646         case AVTAB_CHANGE:
647                 /* Use the process user identity. */
648                 newcontext.user = scontext->user;
649                 break;
650         case AVTAB_MEMBER:
651                 /* Use the related object owner. */
652                 newcontext.user = tcontext->user;
653                 break;
654         }
655
656         /* Set the role and type to default values. */
657         switch (tclass) {
658         case SECCLASS_PROCESS:
659                 /* Use the current role and type of process. */
660                 newcontext.role = scontext->role;
661                 newcontext.type = scontext->type;
662                 break;
663         default:
664                 /* Use the well-defined object role. */
665                 newcontext.role = OBJECT_R_VAL;
666                 /* Use the type of the related object. */
667                 newcontext.type = tcontext->type;
668         }
669
670         /* Look for a type transition/member/change rule. */
671         avkey.source_type = scontext->type;
672         avkey.target_type = tcontext->type;
673         avkey.target_class = tclass;
674         avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_TYPE);
675
676         /* If no permanent rule, also check for enabled conditional rules */
677         if(!avdatum) {
678                 node = avtab_search_node(&policydb.te_cond_avtab, &avkey, specified);
679                 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
680                         if (node->datum.specified & AVTAB_ENABLED) {
681                                 avdatum = &node->datum;
682                                 break;
683                         }
684                 }
685         }
686
687         type_change = (avdatum && (avdatum->specified & specified));
688         if (type_change) {
689                 /* Use the type from the type transition/member/change rule. */
690                 switch (specified) {
691                 case AVTAB_TRANSITION:
692                         newcontext.type = avtab_transition(avdatum);
693                         break;
694                 case AVTAB_MEMBER:
695                         newcontext.type = avtab_member(avdatum);
696                         break;
697                 case AVTAB_CHANGE:
698                         newcontext.type = avtab_change(avdatum);
699                         break;
700                 }
701         }
702
703         /* Check for class-specific changes. */
704         switch (tclass) {
705         case SECCLASS_PROCESS:
706                 if (specified & AVTAB_TRANSITION) {
707                         /* Look for a role transition rule. */
708                         for (roletr = policydb.role_tr; roletr;
709                              roletr = roletr->next) {
710                                 if (roletr->role == scontext->role &&
711                                     roletr->type == tcontext->type) {
712                                         /* Use the role transition rule. */
713                                         newcontext.role = roletr->new_role;
714                                         break;
715                                 }
716                         }
717                 }
718
719                 if (!type_change && !roletr) {
720                         /* No change in process role or type. */
721                         *out_sid = ssid;
722                         goto out_unlock;
723
724                 }
725                 break;
726         default:
727                 if (!type_change &&
728                     (newcontext.user == tcontext->user) &&
729                     mls_context_cmp(scontext, tcontext)) {
730                         /* No change in object type, owner,
731                            or MLS attributes. */
732                         *out_sid = tsid;
733                         goto out_unlock;
734                 }
735                 break;
736         }
737
738         /* Set the MLS attributes.
739            This is done last because it may allocate memory. */
740         rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
741         if (rc)
742                 goto out_unlock;
743
744         /* Check the validity of the context. */
745         if (!policydb_context_isvalid(&policydb, &newcontext)) {
746                 rc = compute_sid_handle_invalid_context(scontext,
747                                                         tcontext,
748                                                         tclass,
749                                                         &newcontext);
750                 if (rc)
751                         goto out_unlock;
752         }
753         /* Obtain the sid for the context. */
754         rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
755 out_unlock:
756         POLICY_RDUNLOCK;
757         context_destroy(&newcontext);
758 out:
759         return rc;
760 }
761
762 /**
763  * security_transition_sid - Compute the SID for a new subject/object.
764  * @ssid: source security identifier
765  * @tsid: target security identifier
766  * @tclass: target security class
767  * @out_sid: security identifier for new subject/object
768  *
769  * Compute a SID to use for labeling a new subject or object in the
770  * class @tclass based on a SID pair (@ssid, @tsid).
771  * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
772  * if insufficient memory is available, or %0 if the new SID was
773  * computed successfully.
774  */
775 int security_transition_sid(u32 ssid,
776                             u32 tsid,
777                             u16 tclass,
778                             u32 *out_sid)
779 {
780         return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
781 }
782
783 /**
784  * security_member_sid - Compute the SID for member selection.
785  * @ssid: source security identifier
786  * @tsid: target security identifier
787  * @tclass: target security class
788  * @out_sid: security identifier for selected member
789  *
790  * Compute a SID to use when selecting a member of a polyinstantiated
791  * object of class @tclass based on a SID pair (@ssid, @tsid).
792  * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
793  * if insufficient memory is available, or %0 if the SID was
794  * computed successfully.
795  */
796 int security_member_sid(u32 ssid,
797                         u32 tsid,
798                         u16 tclass,
799                         u32 *out_sid)
800 {
801         return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
802 }
803
804 /**
805  * security_change_sid - Compute the SID for object relabeling.
806  * @ssid: source security identifier
807  * @tsid: target security identifier
808  * @tclass: target security class
809  * @out_sid: security identifier for selected member
810  *
811  * Compute a SID to use for relabeling an object of class @tclass
812  * based on a SID pair (@ssid, @tsid).
813  * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
814  * if insufficient memory is available, or %0 if the SID was
815  * computed successfully.
816  */
817 int security_change_sid(u32 ssid,
818                         u32 tsid,
819                         u16 tclass,
820                         u32 *out_sid)
821 {
822         return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
823 }
824
825 /*
826  * Verify that each permission that is defined under the
827  * existing policy is still defined with the same value
828  * in the new policy.
829  */
830 static int validate_perm(void *key, void *datum, void *p)
831 {
832         struct hashtab *h;
833         struct perm_datum *perdatum, *perdatum2;
834         int rc = 0;
835
836
837         h = p;
838         perdatum = datum;
839
840         perdatum2 = hashtab_search(h, key);
841         if (!perdatum2) {
842                 printk(KERN_ERR "security:  permission %s disappeared",
843                        (char *)key);
844                 rc = -ENOENT;
845                 goto out;
846         }
847         if (perdatum->value != perdatum2->value) {
848                 printk(KERN_ERR "security:  the value of permission %s changed",
849                        (char *)key);
850                 rc = -EINVAL;
851         }
852 out:
853         return rc;
854 }
855
856 /*
857  * Verify that each class that is defined under the
858  * existing policy is still defined with the same
859  * attributes in the new policy.
860  */
861 static int validate_class(void *key, void *datum, void *p)
862 {
863         struct policydb *newp;
864         struct class_datum *cladatum, *cladatum2;
865         int rc;
866
867         newp = p;
868         cladatum = datum;
869
870         cladatum2 = hashtab_search(newp->p_classes.table, key);
871         if (!cladatum2) {
872                 printk(KERN_ERR "security:  class %s disappeared\n",
873                        (char *)key);
874                 rc = -ENOENT;
875                 goto out;
876         }
877         if (cladatum->value != cladatum2->value) {
878                 printk(KERN_ERR "security:  the value of class %s changed\n",
879                        (char *)key);
880                 rc = -EINVAL;
881                 goto out;
882         }
883         if ((cladatum->comdatum && !cladatum2->comdatum) ||
884             (!cladatum->comdatum && cladatum2->comdatum)) {
885                 printk(KERN_ERR "security:  the inherits clause for the access "
886                        "vector definition for class %s changed\n", (char *)key);
887                 rc = -EINVAL;
888                 goto out;
889         }
890         if (cladatum->comdatum) {
891                 rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm,
892                                  cladatum2->comdatum->permissions.table);
893                 if (rc) {
894                         printk(" in the access vector definition for class "
895                                "%s\n", (char *)key);
896                         goto out;
897                 }
898         }
899         rc = hashtab_map(cladatum->permissions.table, validate_perm,
900                          cladatum2->permissions.table);
901         if (rc)
902                 printk(" in access vector definition for class %s\n",
903                        (char *)key);
904 out:
905         return rc;
906 }
907
908 /* Clone the SID into the new SID table. */
909 static int clone_sid(u32 sid,
910                      struct context *context,
911                      void *arg)
912 {
913         struct sidtab *s = arg;
914
915         return sidtab_insert(s, sid, context);
916 }
917
918 static inline int convert_context_handle_invalid_context(struct context *context)
919 {
920         int rc = 0;
921
922         if (selinux_enforcing) {
923                 rc = -EINVAL;
924         } else {
925                 char *s;
926                 u32 len;
927
928                 context_struct_to_string(context, &s, &len);
929                 printk(KERN_ERR "security:  context %s is invalid\n", s);
930                 kfree(s);
931         }
932         return rc;
933 }
934
935 struct convert_context_args {
936         struct policydb *oldp;
937         struct policydb *newp;
938 };
939
940 /*
941  * Convert the values in the security context
942  * structure `c' from the values specified
943  * in the policy `p->oldp' to the values specified
944  * in the policy `p->newp'.  Verify that the
945  * context is valid under the new policy.
946  */
947 static int convert_context(u32 key,
948                            struct context *c,
949                            void *p)
950 {
951         struct convert_context_args *args;
952         struct context oldc;
953         struct role_datum *role;
954         struct type_datum *typdatum;
955         struct user_datum *usrdatum;
956         char *s;
957         u32 len;
958         int rc;
959
960         args = p;
961
962         rc = context_cpy(&oldc, c);
963         if (rc)
964                 goto out;
965
966         rc = -EINVAL;
967
968         /* Convert the user. */
969         usrdatum = hashtab_search(args->newp->p_users.table,
970                                   args->oldp->p_user_val_to_name[c->user - 1]);
971         if (!usrdatum) {
972                 goto bad;
973         }
974         c->user = usrdatum->value;
975
976         /* Convert the role. */
977         role = hashtab_search(args->newp->p_roles.table,
978                               args->oldp->p_role_val_to_name[c->role - 1]);
979         if (!role) {
980                 goto bad;
981         }
982         c->role = role->value;
983
984         /* Convert the type. */
985         typdatum = hashtab_search(args->newp->p_types.table,
986                                   args->oldp->p_type_val_to_name[c->type - 1]);
987         if (!typdatum) {
988                 goto bad;
989         }
990         c->type = typdatum->value;
991
992         rc = mls_convert_context(args->oldp, args->newp, c);
993         if (rc)
994                 goto bad;
995
996         /* Check the validity of the new context. */
997         if (!policydb_context_isvalid(args->newp, c)) {
998                 rc = convert_context_handle_invalid_context(&oldc);
999                 if (rc)
1000                         goto bad;
1001         }
1002
1003         context_destroy(&oldc);
1004 out:
1005         return rc;
1006 bad:
1007         context_struct_to_string(&oldc, &s, &len);
1008         context_destroy(&oldc);
1009         printk(KERN_ERR "security:  invalidating context %s\n", s);
1010         kfree(s);
1011         goto out;
1012 }
1013
1014 extern void selinux_complete_init(void);
1015
1016 /**
1017  * security_load_policy - Load a security policy configuration.
1018  * @data: binary policy data
1019  * @len: length of data in bytes
1020  *
1021  * Load a new set of security policy configuration data,
1022  * validate it and convert the SID table as necessary.
1023  * This function will flush the access vector cache after
1024  * loading the new policy.
1025  */
1026 int security_load_policy(void *data, size_t len)
1027 {
1028         struct policydb oldpolicydb, newpolicydb;
1029         struct sidtab oldsidtab, newsidtab;
1030         struct convert_context_args args;
1031         u32 seqno;
1032         int rc = 0;
1033         struct policy_file file = { data, len }, *fp = &file;
1034
1035         LOAD_LOCK;
1036
1037         if (!ss_initialized) {
1038                 avtab_cache_init();
1039                 if (policydb_read(&policydb, fp)) {
1040                         LOAD_UNLOCK;
1041                         avtab_cache_destroy();
1042                         return -EINVAL;
1043                 }
1044                 if (policydb_load_isids(&policydb, &sidtab)) {
1045                         LOAD_UNLOCK;
1046                         policydb_destroy(&policydb);
1047                         avtab_cache_destroy();
1048                         return -EINVAL;
1049                 }
1050                 policydb_loaded_version = policydb.policyvers;
1051                 ss_initialized = 1;
1052
1053                 LOAD_UNLOCK;
1054                 selinux_complete_init();
1055                 return 0;
1056         }
1057
1058 #if 0
1059         sidtab_hash_eval(&sidtab, "sids");
1060 #endif
1061
1062         if (policydb_read(&newpolicydb, fp)) {
1063                 LOAD_UNLOCK;
1064                 return -EINVAL;
1065         }
1066
1067         sidtab_init(&newsidtab);
1068
1069         /* Verify that the existing classes did not change. */
1070         if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) {
1071                 printk(KERN_ERR "security:  the definition of an existing "
1072                        "class changed\n");
1073                 rc = -EINVAL;
1074                 goto err;
1075         }
1076
1077         /* Clone the SID table. */
1078         sidtab_shutdown(&sidtab);
1079         if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1080                 rc = -ENOMEM;
1081                 goto err;
1082         }
1083
1084         /* Convert the internal representations of contexts
1085            in the new SID table and remove invalid SIDs. */
1086         args.oldp = &policydb;
1087         args.newp = &newpolicydb;
1088         sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1089
1090         /* Save the old policydb and SID table to free later. */
1091         memcpy(&oldpolicydb, &policydb, sizeof policydb);
1092         sidtab_set(&oldsidtab, &sidtab);
1093
1094         /* Install the new policydb and SID table. */
1095         POLICY_WRLOCK;
1096         memcpy(&policydb, &newpolicydb, sizeof policydb);
1097         sidtab_set(&sidtab, &newsidtab);
1098         seqno = ++latest_granting;
1099         policydb_loaded_version = policydb.policyvers;
1100         POLICY_WRUNLOCK;
1101         LOAD_UNLOCK;
1102
1103         /* Free the old policydb and SID table. */
1104         policydb_destroy(&oldpolicydb);
1105         sidtab_destroy(&oldsidtab);
1106
1107         avc_ss_reset(seqno);
1108         selnl_notify_policyload(seqno);
1109
1110         return 0;
1111
1112 err:
1113         LOAD_UNLOCK;
1114         sidtab_destroy(&newsidtab);
1115         policydb_destroy(&newpolicydb);
1116         return rc;
1117
1118 }
1119
1120 /**
1121  * security_port_sid - Obtain the SID for a port.
1122  * @domain: communication domain aka address family
1123  * @type: socket type
1124  * @protocol: protocol number
1125  * @port: port number
1126  * @out_sid: security identifier
1127  */
1128 int security_port_sid(u16 domain,
1129                       u16 type,
1130                       u8 protocol,
1131                       u16 port,
1132                       u32 *out_sid)
1133 {
1134         struct ocontext *c;
1135         int rc = 0;
1136
1137         POLICY_RDLOCK;
1138
1139         c = policydb.ocontexts[OCON_PORT];
1140         while (c) {
1141                 if (c->u.port.protocol == protocol &&
1142                     c->u.port.low_port <= port &&
1143                     c->u.port.high_port >= port)
1144                         break;
1145                 c = c->next;
1146         }
1147
1148         if (c) {
1149                 if (!c->sid[0]) {
1150                         rc = sidtab_context_to_sid(&sidtab,
1151                                                    &c->context[0],
1152                                                    &c->sid[0]);
1153                         if (rc)
1154                                 goto out;
1155                 }
1156                 *out_sid = c->sid[0];
1157         } else {
1158                 *out_sid = SECINITSID_PORT;
1159         }
1160
1161 out:
1162         POLICY_RDUNLOCK;
1163         return rc;
1164 }
1165
1166 /**
1167  * security_netif_sid - Obtain the SID for a network interface.
1168  * @name: interface name
1169  * @if_sid: interface SID
1170  * @msg_sid: default SID for received packets
1171  */
1172 int security_netif_sid(char *name,
1173                        u32 *if_sid,
1174                        u32 *msg_sid)
1175 {
1176         int rc = 0;
1177         struct ocontext *c;
1178
1179         POLICY_RDLOCK;
1180
1181         c = policydb.ocontexts[OCON_NETIF];
1182         while (c) {
1183                 if (strcmp(name, c->u.name) == 0)
1184                         break;
1185                 c = c->next;
1186         }
1187
1188         if (c) {
1189                 if (!c->sid[0] || !c->sid[1]) {
1190                         rc = sidtab_context_to_sid(&sidtab,
1191                                                   &c->context[0],
1192                                                   &c->sid[0]);
1193                         if (rc)
1194                                 goto out;
1195                         rc = sidtab_context_to_sid(&sidtab,
1196                                                    &c->context[1],
1197                                                    &c->sid[1]);
1198                         if (rc)
1199                                 goto out;
1200                 }
1201                 *if_sid = c->sid[0];
1202                 *msg_sid = c->sid[1];
1203         } else {
1204                 *if_sid = SECINITSID_NETIF;
1205                 *msg_sid = SECINITSID_NETMSG;
1206         }
1207
1208 out:
1209         POLICY_RDUNLOCK;
1210         return rc;
1211 }
1212
1213 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1214 {
1215         int i, fail = 0;
1216
1217         for(i = 0; i < 4; i++)
1218                 if(addr[i] != (input[i] & mask[i])) {
1219                         fail = 1;
1220                         break;
1221                 }
1222
1223         return !fail;
1224 }
1225
1226 /**
1227  * security_node_sid - Obtain the SID for a node (host).
1228  * @domain: communication domain aka address family
1229  * @addrp: address
1230  * @addrlen: address length in bytes
1231  * @out_sid: security identifier
1232  */
1233 int security_node_sid(u16 domain,
1234                       void *addrp,
1235                       u32 addrlen,
1236                       u32 *out_sid)
1237 {
1238         int rc = 0;
1239         struct ocontext *c;
1240
1241         POLICY_RDLOCK;
1242
1243         switch (domain) {
1244         case AF_INET: {
1245                 u32 addr;
1246
1247                 if (addrlen != sizeof(u32)) {
1248                         rc = -EINVAL;
1249                         goto out;
1250                 }
1251
1252                 addr = *((u32 *)addrp);
1253
1254                 c = policydb.ocontexts[OCON_NODE];
1255                 while (c) {
1256                         if (c->u.node.addr == (addr & c->u.node.mask))
1257                                 break;
1258                         c = c->next;
1259                 }
1260                 break;
1261         }
1262
1263         case AF_INET6:
1264                 if (addrlen != sizeof(u64) * 2) {
1265                         rc = -EINVAL;
1266                         goto out;
1267                 }
1268                 c = policydb.ocontexts[OCON_NODE6];
1269                 while (c) {
1270                         if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1271                                                 c->u.node6.mask))
1272                                 break;
1273                         c = c->next;
1274                 }
1275                 break;
1276
1277         default:
1278                 *out_sid = SECINITSID_NODE;
1279                 goto out;
1280         }
1281
1282         if (c) {
1283                 if (!c->sid[0]) {
1284                         rc = sidtab_context_to_sid(&sidtab,
1285                                                    &c->context[0],
1286                                                    &c->sid[0]);
1287                         if (rc)
1288                                 goto out;
1289                 }
1290                 *out_sid = c->sid[0];
1291         } else {
1292                 *out_sid = SECINITSID_NODE;
1293         }
1294
1295 out:
1296         POLICY_RDUNLOCK;
1297         return rc;
1298 }
1299
1300 #define SIDS_NEL 25
1301
1302 /**
1303  * security_get_user_sids - Obtain reachable SIDs for a user.
1304  * @fromsid: starting SID
1305  * @username: username
1306  * @sids: array of reachable SIDs for user
1307  * @nel: number of elements in @sids
1308  *
1309  * Generate the set of SIDs for legal security contexts
1310  * for a given user that can be reached by @fromsid.
1311  * Set *@sids to point to a dynamically allocated
1312  * array containing the set of SIDs.  Set *@nel to the
1313  * number of elements in the array.
1314  */
1315
1316 int security_get_user_sids(u32 fromsid,
1317                            char *username,
1318                            u32 **sids,
1319                            u32 *nel)
1320 {
1321         struct context *fromcon, usercon;
1322         u32 *mysids, *mysids2, sid;
1323         u32 mynel = 0, maxnel = SIDS_NEL;
1324         struct user_datum *user;
1325         struct role_datum *role;
1326         struct av_decision avd;
1327         int rc = 0, i, j;
1328
1329         if (!ss_initialized) {
1330                 *sids = NULL;
1331                 *nel = 0;
1332                 goto out;
1333         }
1334
1335         POLICY_RDLOCK;
1336
1337         fromcon = sidtab_search(&sidtab, fromsid);
1338         if (!fromcon) {
1339                 rc = -EINVAL;
1340                 goto out_unlock;
1341         }
1342
1343         user = hashtab_search(policydb.p_users.table, username);
1344         if (!user) {
1345                 rc = -EINVAL;
1346                 goto out_unlock;
1347         }
1348         usercon.user = user->value;
1349
1350         mysids = kmalloc(maxnel*sizeof(*mysids), GFP_ATOMIC);
1351         if (!mysids) {
1352                 rc = -ENOMEM;
1353                 goto out_unlock;
1354         }
1355         memset(mysids, 0, maxnel*sizeof(*mysids));
1356
1357         for (i = ebitmap_startbit(&user->roles); i < ebitmap_length(&user->roles); i++) {
1358                 if (!ebitmap_get_bit(&user->roles, i))
1359                         continue;
1360                 role = policydb.role_val_to_struct[i];
1361                 usercon.role = i+1;
1362                 for (j = ebitmap_startbit(&role->types); j < ebitmap_length(&role->types); j++) {
1363                         if (!ebitmap_get_bit(&role->types, j))
1364                                 continue;
1365                         usercon.type = j+1;
1366                         mls_for_user_ranges(user,usercon) {
1367                                 rc = context_struct_compute_av(fromcon, &usercon,
1368                                                                SECCLASS_PROCESS,
1369                                                                PROCESS__TRANSITION,
1370                                                                &avd);
1371                                 if (rc ||  !(avd.allowed & PROCESS__TRANSITION))
1372                                         continue;
1373                                 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1374                                 if (rc) {
1375                                         kfree(mysids);
1376                                         goto out_unlock;
1377                                 }
1378                                 if (mynel < maxnel) {
1379                                         mysids[mynel++] = sid;
1380                                 } else {
1381                                         maxnel += SIDS_NEL;
1382                                         mysids2 = kmalloc(maxnel*sizeof(*mysids2), GFP_ATOMIC);
1383                                         if (!mysids2) {
1384                                                 rc = -ENOMEM;
1385                                                 kfree(mysids);
1386                                                 goto out_unlock;
1387                                         }
1388                                         memset(mysids2, 0, maxnel*sizeof(*mysids2));
1389                                         memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1390                                         kfree(mysids);
1391                                         mysids = mysids2;
1392                                         mysids[mynel++] = sid;
1393                                 }
1394                         }
1395                         mls_end_user_ranges;
1396                 }
1397         }
1398
1399         *sids = mysids;
1400         *nel = mynel;
1401
1402 out_unlock:
1403         POLICY_RDUNLOCK;
1404 out:
1405         return rc;
1406 }
1407
1408 /**
1409  * security_genfs_sid - Obtain a SID for a file in a filesystem
1410  * @fstype: filesystem type
1411  * @path: path from root of mount
1412  * @sclass: file security class
1413  * @sid: SID for path
1414  *
1415  * Obtain a SID to use for a file in a filesystem that
1416  * cannot support xattr or use a fixed labeling behavior like
1417  * transition SIDs or task SIDs.
1418  */
1419 int security_genfs_sid(const char *fstype,
1420                        char *path,
1421                        u16 sclass,
1422                        u32 *sid)
1423 {
1424         int len;
1425         struct genfs *genfs;
1426         struct ocontext *c;
1427         int rc = 0, cmp = 0;
1428
1429         POLICY_RDLOCK;
1430
1431         for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1432                 cmp = strcmp(fstype, genfs->fstype);
1433                 if (cmp <= 0)
1434                         break;
1435         }
1436
1437         if (!genfs || cmp) {
1438                 *sid = SECINITSID_UNLABELED;
1439                 rc = -ENOENT;
1440                 goto out;
1441         }
1442
1443         for (c = genfs->head; c; c = c->next) {
1444                 len = strlen(c->u.name);
1445                 if ((!c->v.sclass || sclass == c->v.sclass) &&
1446                     (strncmp(c->u.name, path, len) == 0))
1447                         break;
1448         }
1449
1450         if (!c) {
1451                 *sid = SECINITSID_UNLABELED;
1452                 rc = -ENOENT;
1453                 goto out;
1454         }
1455
1456         if (!c->sid[0]) {
1457                 rc = sidtab_context_to_sid(&sidtab,
1458                                            &c->context[0],
1459                                            &c->sid[0]);
1460                 if (rc)
1461                         goto out;
1462         }
1463
1464         *sid = c->sid[0];
1465 out:
1466         POLICY_RDUNLOCK;
1467         return rc;
1468 }
1469
1470 /**
1471  * security_fs_use - Determine how to handle labeling for a filesystem.
1472  * @fstype: filesystem type
1473  * @behavior: labeling behavior
1474  * @sid: SID for filesystem (superblock)
1475  */
1476 int security_fs_use(
1477         const char *fstype,
1478         unsigned int *behavior,
1479         u32 *sid)
1480 {
1481         int rc = 0;
1482         struct ocontext *c;
1483
1484         POLICY_RDLOCK;
1485
1486         c = policydb.ocontexts[OCON_FSUSE];
1487         while (c) {
1488                 if (strcmp(fstype, c->u.name) == 0)
1489                         break;
1490                 c = c->next;
1491         }
1492
1493         if (c) {
1494                 *behavior = c->v.behavior;
1495                 if (!c->sid[0]) {
1496                         rc = sidtab_context_to_sid(&sidtab,
1497                                                    &c->context[0],
1498                                                    &c->sid[0]);
1499                         if (rc)
1500                                 goto out;
1501                 }
1502                 *sid = c->sid[0];
1503         } else {
1504                 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1505                 if (rc) {
1506                         *behavior = SECURITY_FS_USE_NONE;
1507                         rc = 0;
1508                 } else {
1509                         *behavior = SECURITY_FS_USE_GENFS;
1510                 }
1511         }
1512
1513 out:
1514         POLICY_RDUNLOCK;
1515         return rc;
1516 }
1517
1518 int security_get_bools(int *len, char ***names, int **values)
1519 {
1520         int i, rc = -ENOMEM;
1521
1522         POLICY_RDLOCK;
1523         *names = NULL;
1524         *values = NULL;
1525
1526         *len = policydb.p_bools.nprim;
1527         if (!*len) {
1528                 rc = 0;
1529                 goto out;
1530         }
1531
1532         *names = (char**)kmalloc(sizeof(char*) * *len, GFP_ATOMIC);
1533         if (!*names)
1534                 goto err;
1535         memset(*names, 0, sizeof(char*) * *len);
1536
1537         *values = (int*)kmalloc(sizeof(int) * *len, GFP_ATOMIC);
1538         if (!*values)
1539                 goto err;
1540
1541         for (i = 0; i < *len; i++) {
1542                 size_t name_len;
1543                 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1544                 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1545                 (*names)[i] = (char*)kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1546                 if (!(*names)[i])
1547                         goto err;
1548                 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1549                 (*names)[i][name_len - 1] = 0;
1550         }
1551         rc = 0;
1552 out:
1553         POLICY_RDUNLOCK;
1554         return rc;
1555 err:
1556         if (*names) {
1557                 for (i = 0; i < *len; i++)
1558                         if ((*names)[i])
1559                                 kfree((*names)[i]);
1560         }
1561         if (*values)
1562                 kfree(*values);
1563         goto out;
1564 }
1565
1566
1567 int security_set_bools(int len, int *values)
1568 {
1569         int i, rc = 0;
1570         int lenp, seqno = 0;
1571         struct cond_node *cur;
1572
1573         POLICY_WRLOCK;
1574
1575         lenp = policydb.p_bools.nprim;
1576         if (len != lenp) {
1577                 rc = -EFAULT;
1578                 goto out;
1579         }
1580
1581         printk(KERN_INFO "security: committed booleans { ");
1582         for (i = 0; i < len; i++) {
1583                 if (values[i]) {
1584                         policydb.bool_val_to_struct[i]->state = 1;
1585                 } else {
1586                         policydb.bool_val_to_struct[i]->state = 0;
1587                 }
1588                 if (i != 0)
1589                         printk(", ");
1590                 printk("%s:%d", policydb.p_bool_val_to_name[i],
1591                        policydb.bool_val_to_struct[i]->state);
1592         }
1593         printk(" }\n");
1594
1595         for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1596                 rc = evaluate_cond_node(&policydb, cur);
1597                 if (rc)
1598                         goto out;
1599         }
1600
1601         seqno = ++latest_granting;
1602
1603 out:
1604         POLICY_WRUNLOCK;
1605         if (!rc) {
1606                 avc_ss_reset(seqno);
1607                 selnl_notify_policyload(seqno);
1608         }
1609         return rc;
1610 }
1611
1612 int security_get_bool_value(int bool)
1613 {
1614         int rc = 0;
1615         int len;
1616
1617         POLICY_RDLOCK;
1618
1619         len = policydb.p_bools.nprim;
1620         if (bool >= len) {
1621                 rc = -EFAULT;
1622                 goto out;
1623         }
1624
1625         rc = policydb.bool_val_to_struct[bool]->state;
1626 out:
1627         POLICY_RDUNLOCK;
1628         return rc;
1629 }