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