patch-2_6_7-vs1_9_1_12
[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         if ((p - scontext2) < scontext_len) {
536                 rc = -EINVAL;
537                 goto out_unlock;
538         }
539
540         /* Check the validity of the new context. */
541         if (!policydb_context_isvalid(&policydb, &context)) {
542                 rc = -EINVAL;
543                 goto out_unlock;
544         }
545         /* Obtain the new sid. */
546         rc = sidtab_context_to_sid(&sidtab, &context, sid);
547 out_unlock:
548         POLICY_RDUNLOCK;
549         context_destroy(&context);
550         kfree(scontext2);
551 out:
552         return rc;
553 }
554
555 static int compute_sid_handle_invalid_context(
556         struct context *scontext,
557         struct context *tcontext,
558         u16 tclass,
559         struct context *newcontext)
560 {
561         char *s = NULL, *t = NULL, *n = NULL;
562         u32 slen, tlen, nlen;
563
564         if (context_struct_to_string(scontext, &s, &slen) < 0)
565                 goto out;
566         if (context_struct_to_string(tcontext, &t, &tlen) < 0)
567                 goto out;
568         if (context_struct_to_string(newcontext, &n, &nlen) < 0)
569                 goto out;
570         audit_log(current->audit_context,
571                   "security_compute_sid:  invalid context %s"
572                   " for scontext=%s"
573                   " tcontext=%s"
574                   " tclass=%s",
575                   n, s, t, policydb.p_class_val_to_name[tclass-1]);
576 out:
577         kfree(s);
578         kfree(t);
579         kfree(n);
580         if (!selinux_enforcing)
581                 return 0;
582         return -EACCES;
583 }
584
585 static int security_compute_sid(u32 ssid,
586                                 u32 tsid,
587                                 u16 tclass,
588                                 u32 specified,
589                                 u32 *out_sid)
590 {
591         struct context *scontext = 0, *tcontext = 0, newcontext;
592         struct role_trans *roletr = 0;
593         struct avtab_key avkey;
594         struct avtab_datum *avdatum;
595         struct avtab_node *node;
596         unsigned int type_change = 0;
597         int rc = 0;
598
599         if (!ss_initialized) {
600                 switch (tclass) {
601                 case SECCLASS_PROCESS:
602                         *out_sid = ssid;
603                         break;
604                 default:
605                         *out_sid = tsid;
606                         break;
607                 }
608                 goto out;
609         }
610
611         POLICY_RDLOCK;
612
613         scontext = sidtab_search(&sidtab, ssid);
614         if (!scontext) {
615                 printk(KERN_ERR "security_compute_sid:  unrecognized SID %d\n",
616                        ssid);
617                 rc = -EINVAL;
618                 goto out_unlock;
619         }
620         tcontext = sidtab_search(&sidtab, tsid);
621         if (!tcontext) {
622                 printk(KERN_ERR "security_compute_sid:  unrecognized SID %d\n",
623                        tsid);
624                 rc = -EINVAL;
625                 goto out_unlock;
626         }
627
628         context_init(&newcontext);
629
630         /* Set the user identity. */
631         switch (specified) {
632         case AVTAB_TRANSITION:
633         case AVTAB_CHANGE:
634                 /* Use the process user identity. */
635                 newcontext.user = scontext->user;
636                 break;
637         case AVTAB_MEMBER:
638                 /* Use the related object owner. */
639                 newcontext.user = tcontext->user;
640                 break;
641         }
642
643         /* Set the role and type to default values. */
644         switch (tclass) {
645         case SECCLASS_PROCESS:
646                 /* Use the current role and type of process. */
647                 newcontext.role = scontext->role;
648                 newcontext.type = scontext->type;
649                 break;
650         default:
651                 /* Use the well-defined object role. */
652                 newcontext.role = OBJECT_R_VAL;
653                 /* Use the type of the related object. */
654                 newcontext.type = tcontext->type;
655         }
656
657         /* Look for a type transition/member/change rule. */
658         avkey.source_type = scontext->type;
659         avkey.target_type = tcontext->type;
660         avkey.target_class = tclass;
661         avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_TYPE);
662
663         /* If no permanent rule, also check for enabled conditional rules */
664         if(!avdatum) {
665                 node = avtab_search_node(&policydb.te_cond_avtab, &avkey, specified);
666                 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
667                         if (node->datum.specified & AVTAB_ENABLED) {
668                                 avdatum = &node->datum;
669                                 break;
670                         }
671                 }
672         }
673
674         type_change = (avdatum && (avdatum->specified & specified));
675         if (type_change) {
676                 /* Use the type from the type transition/member/change rule. */
677                 switch (specified) {
678                 case AVTAB_TRANSITION:
679                         newcontext.type = avtab_transition(avdatum);
680                         break;
681                 case AVTAB_MEMBER:
682                         newcontext.type = avtab_member(avdatum);
683                         break;
684                 case AVTAB_CHANGE:
685                         newcontext.type = avtab_change(avdatum);
686                         break;
687                 }
688         }
689
690         /* Check for class-specific changes. */
691         switch (tclass) {
692         case SECCLASS_PROCESS:
693                 if (specified & AVTAB_TRANSITION) {
694                         /* Look for a role transition rule. */
695                         for (roletr = policydb.role_tr; roletr;
696                              roletr = roletr->next) {
697                                 if (roletr->role == scontext->role &&
698                                     roletr->type == tcontext->type) {
699                                         /* Use the role transition rule. */
700                                         newcontext.role = roletr->new_role;
701                                         break;
702                                 }
703                         }
704                 }
705
706                 if (!type_change && !roletr) {
707                         /* No change in process role or type. */
708                         *out_sid = ssid;
709                         goto out_unlock;
710
711                 }
712                 break;
713         default:
714                 if (!type_change &&
715                     (newcontext.user == tcontext->user) &&
716                     mls_context_cmp(scontext, tcontext)) {
717                         /* No change in object type, owner,
718                            or MLS attributes. */
719                         *out_sid = tsid;
720                         goto out_unlock;
721                 }
722                 break;
723         }
724
725         /* Set the MLS attributes.
726            This is done last because it may allocate memory. */
727         rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
728         if (rc)
729                 goto out_unlock;
730
731         /* Check the validity of the context. */
732         if (!policydb_context_isvalid(&policydb, &newcontext)) {
733                 rc = compute_sid_handle_invalid_context(scontext,
734                                                         tcontext,
735                                                         tclass,
736                                                         &newcontext);
737                 if (rc)
738                         goto out_unlock;
739         }
740         /* Obtain the sid for the context. */
741         rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
742 out_unlock:
743         POLICY_RDUNLOCK;
744         context_destroy(&newcontext);
745 out:
746         return rc;
747 }
748
749 /**
750  * security_transition_sid - Compute the SID for a new subject/object.
751  * @ssid: source security identifier
752  * @tsid: target security identifier
753  * @tclass: target security class
754  * @out_sid: security identifier for new subject/object
755  *
756  * Compute a SID to use for labeling a new subject or object in the
757  * class @tclass based on a SID pair (@ssid, @tsid).
758  * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
759  * if insufficient memory is available, or %0 if the new SID was
760  * computed successfully.
761  */
762 int security_transition_sid(u32 ssid,
763                             u32 tsid,
764                             u16 tclass,
765                             u32 *out_sid)
766 {
767         return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
768 }
769
770 /**
771  * security_member_sid - Compute the SID for member selection.
772  * @ssid: source security identifier
773  * @tsid: target security identifier
774  * @tclass: target security class
775  * @out_sid: security identifier for selected member
776  *
777  * Compute a SID to use when selecting a member of a polyinstantiated
778  * object of class @tclass based on a SID pair (@ssid, @tsid).
779  * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
780  * if insufficient memory is available, or %0 if the SID was
781  * computed successfully.
782  */
783 int security_member_sid(u32 ssid,
784                         u32 tsid,
785                         u16 tclass,
786                         u32 *out_sid)
787 {
788         return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
789 }
790
791 /**
792  * security_change_sid - Compute the SID for object relabeling.
793  * @ssid: source security identifier
794  * @tsid: target security identifier
795  * @tclass: target security class
796  * @out_sid: security identifier for selected member
797  *
798  * Compute a SID to use for relabeling an object of class @tclass
799  * based on a SID pair (@ssid, @tsid).
800  * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
801  * if insufficient memory is available, or %0 if the SID was
802  * computed successfully.
803  */
804 int security_change_sid(u32 ssid,
805                         u32 tsid,
806                         u16 tclass,
807                         u32 *out_sid)
808 {
809         return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
810 }
811
812 /*
813  * Verify that each permission that is defined under the
814  * existing policy is still defined with the same value
815  * in the new policy.
816  */
817 static int validate_perm(void *key, void *datum, void *p)
818 {
819         struct hashtab *h;
820         struct perm_datum *perdatum, *perdatum2;
821         int rc = 0;
822
823
824         h = p;
825         perdatum = datum;
826
827         perdatum2 = hashtab_search(h, key);
828         if (!perdatum2) {
829                 printk(KERN_ERR "security:  permission %s disappeared",
830                        (char *)key);
831                 rc = -ENOENT;
832                 goto out;
833         }
834         if (perdatum->value != perdatum2->value) {
835                 printk(KERN_ERR "security:  the value of permission %s changed",
836                        (char *)key);
837                 rc = -EINVAL;
838         }
839 out:
840         return rc;
841 }
842
843 /*
844  * Verify that each class that is defined under the
845  * existing policy is still defined with the same
846  * attributes in the new policy.
847  */
848 static int validate_class(void *key, void *datum, void *p)
849 {
850         struct policydb *newp;
851         struct class_datum *cladatum, *cladatum2;
852         int rc;
853
854         newp = p;
855         cladatum = datum;
856
857         cladatum2 = hashtab_search(newp->p_classes.table, key);
858         if (!cladatum2) {
859                 printk(KERN_ERR "security:  class %s disappeared\n",
860                        (char *)key);
861                 rc = -ENOENT;
862                 goto out;
863         }
864         if (cladatum->value != cladatum2->value) {
865                 printk(KERN_ERR "security:  the value of class %s changed\n",
866                        (char *)key);
867                 rc = -EINVAL;
868                 goto out;
869         }
870         if ((cladatum->comdatum && !cladatum2->comdatum) ||
871             (!cladatum->comdatum && cladatum2->comdatum)) {
872                 printk(KERN_ERR "security:  the inherits clause for the access "
873                        "vector definition for class %s changed\n", (char *)key);
874                 rc = -EINVAL;
875                 goto out;
876         }
877         if (cladatum->comdatum) {
878                 rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm,
879                                  cladatum2->comdatum->permissions.table);
880                 if (rc) {
881                         printk(" in the access vector definition for class "
882                                "%s\n", (char *)key);
883                         goto out;
884                 }
885         }
886         rc = hashtab_map(cladatum->permissions.table, validate_perm,
887                          cladatum2->permissions.table);
888         if (rc)
889                 printk(" in access vector definition for class %s\n",
890                        (char *)key);
891 out:
892         return rc;
893 }
894
895 /* Clone the SID into the new SID table. */
896 static int clone_sid(u32 sid,
897                      struct context *context,
898                      void *arg)
899 {
900         struct sidtab *s = arg;
901
902         return sidtab_insert(s, sid, context);
903 }
904
905 static inline int convert_context_handle_invalid_context(struct context *context)
906 {
907         int rc = 0;
908
909         if (selinux_enforcing) {
910                 rc = -EINVAL;
911         } else {
912                 char *s;
913                 u32 len;
914
915                 context_struct_to_string(context, &s, &len);
916                 printk(KERN_ERR "security:  context %s is invalid\n", s);
917                 kfree(s);
918         }
919         return rc;
920 }
921
922 struct convert_context_args {
923         struct policydb *oldp;
924         struct policydb *newp;
925 };
926
927 /*
928  * Convert the values in the security context
929  * structure `c' from the values specified
930  * in the policy `p->oldp' to the values specified
931  * in the policy `p->newp'.  Verify that the
932  * context is valid under the new policy.
933  */
934 static int convert_context(u32 key,
935                            struct context *c,
936                            void *p)
937 {
938         struct convert_context_args *args;
939         struct context oldc;
940         struct role_datum *role;
941         struct type_datum *typdatum;
942         struct user_datum *usrdatum;
943         char *s;
944         u32 len;
945         int rc;
946
947         args = p;
948
949         rc = context_cpy(&oldc, c);
950         if (rc)
951                 goto out;
952
953         rc = -EINVAL;
954
955         /* Convert the user. */
956         usrdatum = hashtab_search(args->newp->p_users.table,
957                                   args->oldp->p_user_val_to_name[c->user - 1]);
958         if (!usrdatum) {
959                 goto bad;
960         }
961         c->user = usrdatum->value;
962
963         /* Convert the role. */
964         role = hashtab_search(args->newp->p_roles.table,
965                               args->oldp->p_role_val_to_name[c->role - 1]);
966         if (!role) {
967                 goto bad;
968         }
969         c->role = role->value;
970
971         /* Convert the type. */
972         typdatum = hashtab_search(args->newp->p_types.table,
973                                   args->oldp->p_type_val_to_name[c->type - 1]);
974         if (!typdatum) {
975                 goto bad;
976         }
977         c->type = typdatum->value;
978
979         rc = mls_convert_context(args->oldp, args->newp, c);
980         if (rc)
981                 goto bad;
982
983         /* Check the validity of the new context. */
984         if (!policydb_context_isvalid(args->newp, c)) {
985                 rc = convert_context_handle_invalid_context(&oldc);
986                 if (rc)
987                         goto bad;
988         }
989
990         context_destroy(&oldc);
991 out:
992         return rc;
993 bad:
994         context_struct_to_string(&oldc, &s, &len);
995         context_destroy(&oldc);
996         printk(KERN_ERR "security:  invalidating context %s\n", s);
997         kfree(s);
998         goto out;
999 }
1000
1001 extern void selinux_complete_init(void);
1002
1003 /**
1004  * security_load_policy - Load a security policy configuration.
1005  * @data: binary policy data
1006  * @len: length of data in bytes
1007  *
1008  * Load a new set of security policy configuration data,
1009  * validate it and convert the SID table as necessary.
1010  * This function will flush the access vector cache after
1011  * loading the new policy.
1012  */
1013 int security_load_policy(void *data, size_t len)
1014 {
1015         struct policydb oldpolicydb, newpolicydb;
1016         struct sidtab oldsidtab, newsidtab;
1017         struct convert_context_args args;
1018         u32 seqno;
1019         int rc = 0;
1020         struct policy_file file = { data, len }, *fp = &file;
1021
1022         LOAD_LOCK;
1023
1024         if (!ss_initialized) {
1025                 if (policydb_read(&policydb, fp)) {
1026                         LOAD_UNLOCK;
1027                         return -EINVAL;
1028                 }
1029                 if (policydb_load_isids(&policydb, &sidtab)) {
1030                         LOAD_UNLOCK;
1031                         policydb_destroy(&policydb);
1032                         return -EINVAL;
1033                 }
1034                 ss_initialized = 1;
1035
1036                 LOAD_UNLOCK;
1037                 selinux_complete_init();
1038                 return 0;
1039         }
1040
1041 #if 0
1042         sidtab_hash_eval(&sidtab, "sids");
1043 #endif
1044
1045         if (policydb_read(&newpolicydb, fp)) {
1046                 LOAD_UNLOCK;
1047                 return -EINVAL;
1048         }
1049
1050         sidtab_init(&newsidtab);
1051
1052         /* Verify that the existing classes did not change. */
1053         if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) {
1054                 printk(KERN_ERR "security:  the definition of an existing "
1055                        "class changed\n");
1056                 rc = -EINVAL;
1057                 goto err;
1058         }
1059
1060         /* Clone the SID table. */
1061         sidtab_shutdown(&sidtab);
1062         if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1063                 rc = -ENOMEM;
1064                 goto err;
1065         }
1066
1067         /* Convert the internal representations of contexts
1068            in the new SID table and remove invalid SIDs. */
1069         args.oldp = &policydb;
1070         args.newp = &newpolicydb;
1071         sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1072
1073         /* Save the old policydb and SID table to free later. */
1074         memcpy(&oldpolicydb, &policydb, sizeof policydb);
1075         sidtab_set(&oldsidtab, &sidtab);
1076
1077         /* Install the new policydb and SID table. */
1078         POLICY_WRLOCK;
1079         memcpy(&policydb, &newpolicydb, sizeof policydb);
1080         sidtab_set(&sidtab, &newsidtab);
1081         seqno = ++latest_granting;
1082
1083         POLICY_WRUNLOCK;
1084         LOAD_UNLOCK;
1085
1086         /* Free the old policydb and SID table. */
1087         policydb_destroy(&oldpolicydb);
1088         sidtab_destroy(&oldsidtab);
1089
1090         avc_ss_reset(seqno);
1091         selnl_notify_policyload(seqno);
1092
1093         return 0;
1094
1095 err:
1096         LOAD_UNLOCK;
1097         sidtab_destroy(&newsidtab);
1098         policydb_destroy(&newpolicydb);
1099         return rc;
1100
1101 }
1102
1103 /**
1104  * security_port_sid - Obtain the SID for a port.
1105  * @domain: communication domain aka address family
1106  * @type: socket type
1107  * @protocol: protocol number
1108  * @port: port number
1109  * @out_sid: security identifier
1110  */
1111 int security_port_sid(u16 domain,
1112                       u16 type,
1113                       u8 protocol,
1114                       u16 port,
1115                       u32 *out_sid)
1116 {
1117         struct ocontext *c;
1118         int rc = 0;
1119
1120         POLICY_RDLOCK;
1121
1122         c = policydb.ocontexts[OCON_PORT];
1123         while (c) {
1124                 if (c->u.port.protocol == protocol &&
1125                     c->u.port.low_port <= port &&
1126                     c->u.port.high_port >= port)
1127                         break;
1128                 c = c->next;
1129         }
1130
1131         if (c) {
1132                 if (!c->sid[0]) {
1133                         rc = sidtab_context_to_sid(&sidtab,
1134                                                    &c->context[0],
1135                                                    &c->sid[0]);
1136                         if (rc)
1137                                 goto out;
1138                 }
1139                 *out_sid = c->sid[0];
1140         } else {
1141                 *out_sid = SECINITSID_PORT;
1142         }
1143
1144 out:
1145         POLICY_RDUNLOCK;
1146         return rc;
1147 }
1148
1149 /**
1150  * security_netif_sid - Obtain the SID for a network interface.
1151  * @name: interface name
1152  * @if_sid: interface SID
1153  * @msg_sid: default SID for received packets
1154  */
1155 int security_netif_sid(char *name,
1156                        u32 *if_sid,
1157                        u32 *msg_sid)
1158 {
1159         int rc = 0;
1160         struct ocontext *c;
1161
1162         POLICY_RDLOCK;
1163
1164         c = policydb.ocontexts[OCON_NETIF];
1165         while (c) {
1166                 if (strcmp(name, c->u.name) == 0)
1167                         break;
1168                 c = c->next;
1169         }
1170
1171         if (c) {
1172                 if (!c->sid[0] || !c->sid[1]) {
1173                         rc = sidtab_context_to_sid(&sidtab,
1174                                                   &c->context[0],
1175                                                   &c->sid[0]);
1176                         if (rc)
1177                                 goto out;
1178                         rc = sidtab_context_to_sid(&sidtab,
1179                                                    &c->context[1],
1180                                                    &c->sid[1]);
1181                         if (rc)
1182                                 goto out;
1183                 }
1184                 *if_sid = c->sid[0];
1185                 *msg_sid = c->sid[1];
1186         } else {
1187                 *if_sid = SECINITSID_NETIF;
1188                 *msg_sid = SECINITSID_NETMSG;
1189         }
1190
1191 out:
1192         POLICY_RDUNLOCK;
1193         return rc;
1194 }
1195
1196 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1197 {
1198         int i, fail = 0;
1199
1200         for(i = 0; i < 4; i++)
1201                 if(addr[i] != (input[i] & mask[i])) {
1202                         fail = 1;
1203                         break;
1204                 }
1205
1206         return !fail;
1207 }
1208
1209 /**
1210  * security_node_sid - Obtain the SID for a node (host).
1211  * @domain: communication domain aka address family
1212  * @addrp: address
1213  * @addrlen: address length in bytes
1214  * @out_sid: security identifier
1215  */
1216 int security_node_sid(u16 domain,
1217                       void *addrp,
1218                       u32 addrlen,
1219                       u32 *out_sid)
1220 {
1221         int rc = 0;
1222         struct ocontext *c;
1223
1224         POLICY_RDLOCK;
1225
1226         switch (domain) {
1227         case AF_INET: {
1228                 u32 addr;
1229
1230                 if (addrlen != sizeof(u32)) {
1231                         rc = -EINVAL;
1232                         goto out;
1233                 }
1234
1235                 addr = *((u32 *)addrp);
1236
1237                 c = policydb.ocontexts[OCON_NODE];
1238                 while (c) {
1239                         if (c->u.node.addr == (addr & c->u.node.mask))
1240                                 break;
1241                         c = c->next;
1242                 }
1243                 break;
1244         }
1245
1246         case AF_INET6:
1247                 if (addrlen != sizeof(u64) * 2) {
1248                         rc = -EINVAL;
1249                         goto out;
1250                 }
1251                 c = policydb.ocontexts[OCON_NODE6];
1252                 while (c) {
1253                         if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1254                                                 c->u.node6.mask))
1255                                 break;
1256                         c = c->next;
1257                 }
1258                 break;
1259
1260         default:
1261                 *out_sid = SECINITSID_NODE;
1262                 goto out;
1263         }
1264
1265         if (c) {
1266                 if (!c->sid[0]) {
1267                         rc = sidtab_context_to_sid(&sidtab,
1268                                                    &c->context[0],
1269                                                    &c->sid[0]);
1270                         if (rc)
1271                                 goto out;
1272                 }
1273                 *out_sid = c->sid[0];
1274         } else {
1275                 *out_sid = SECINITSID_NODE;
1276         }
1277
1278 out:
1279         POLICY_RDUNLOCK;
1280         return rc;
1281 }
1282
1283 #define SIDS_NEL 25
1284
1285 /**
1286  * security_get_user_sids - Obtain reachable SIDs for a user.
1287  * @fromsid: starting SID
1288  * @username: username
1289  * @sids: array of reachable SIDs for user
1290  * @nel: number of elements in @sids
1291  *
1292  * Generate the set of SIDs for legal security contexts
1293  * for a given user that can be reached by @fromsid.
1294  * Set *@sids to point to a dynamically allocated
1295  * array containing the set of SIDs.  Set *@nel to the
1296  * number of elements in the array.
1297  */
1298
1299 int security_get_user_sids(u32 fromsid,
1300                            char *username,
1301                            u32 **sids,
1302                            u32 *nel)
1303 {
1304         struct context *fromcon, usercon;
1305         u32 *mysids, *mysids2, sid;
1306         u32 mynel = 0, maxnel = SIDS_NEL;
1307         struct user_datum *user;
1308         struct role_datum *role;
1309         struct av_decision avd;
1310         int rc = 0, i, j;
1311
1312         if (!ss_initialized) {
1313                 *sids = NULL;
1314                 *nel = 0;
1315                 goto out;
1316         }
1317
1318         POLICY_RDLOCK;
1319
1320         fromcon = sidtab_search(&sidtab, fromsid);
1321         if (!fromcon) {
1322                 rc = -EINVAL;
1323                 goto out_unlock;
1324         }
1325
1326         user = hashtab_search(policydb.p_users.table, username);
1327         if (!user) {
1328                 rc = -EINVAL;
1329                 goto out_unlock;
1330         }
1331         usercon.user = user->value;
1332
1333         mysids = kmalloc(maxnel*sizeof(*mysids), GFP_ATOMIC);
1334         if (!mysids) {
1335                 rc = -ENOMEM;
1336                 goto out_unlock;
1337         }
1338         memset(mysids, 0, maxnel*sizeof(*mysids));
1339
1340         for (i = ebitmap_startbit(&user->roles); i < ebitmap_length(&user->roles); i++) {
1341                 if (!ebitmap_get_bit(&user->roles, i))
1342                         continue;
1343                 role = policydb.role_val_to_struct[i];
1344                 usercon.role = i+1;
1345                 for (j = ebitmap_startbit(&role->types); j < ebitmap_length(&role->types); j++) {
1346                         if (!ebitmap_get_bit(&role->types, j))
1347                                 continue;
1348                         usercon.type = j+1;
1349                         mls_for_user_ranges(user,usercon) {
1350                                 rc = context_struct_compute_av(fromcon, &usercon,
1351                                                                SECCLASS_PROCESS,
1352                                                                PROCESS__TRANSITION,
1353                                                                &avd);
1354                                 if (rc ||  !(avd.allowed & PROCESS__TRANSITION))
1355                                         continue;
1356                                 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1357                                 if (rc) {
1358                                         kfree(mysids);
1359                                         goto out_unlock;
1360                                 }
1361                                 if (mynel < maxnel) {
1362                                         mysids[mynel++] = sid;
1363                                 } else {
1364                                         maxnel += SIDS_NEL;
1365                                         mysids2 = kmalloc(maxnel*sizeof(*mysids2), GFP_ATOMIC);
1366                                         if (!mysids2) {
1367                                                 rc = -ENOMEM;
1368                                                 kfree(mysids);
1369                                                 goto out_unlock;
1370                                         }
1371                                         memset(mysids2, 0, maxnel*sizeof(*mysids2));
1372                                         memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1373                                         kfree(mysids);
1374                                         mysids = mysids2;
1375                                         mysids[mynel++] = sid;
1376                                 }
1377                         }
1378                         mls_end_user_ranges;
1379                 }
1380         }
1381
1382         *sids = mysids;
1383         *nel = mynel;
1384
1385 out_unlock:
1386         POLICY_RDUNLOCK;
1387 out:
1388         return rc;
1389 }
1390
1391 /**
1392  * security_genfs_sid - Obtain a SID for a file in a filesystem
1393  * @fstype: filesystem type
1394  * @path: path from root of mount
1395  * @sclass: file security class
1396  * @sid: SID for path
1397  *
1398  * Obtain a SID to use for a file in a filesystem that
1399  * cannot support xattr or use a fixed labeling behavior like
1400  * transition SIDs or task SIDs.
1401  */
1402 int security_genfs_sid(const char *fstype,
1403                        char *path,
1404                        u16 sclass,
1405                        u32 *sid)
1406 {
1407         int len;
1408         struct genfs *genfs;
1409         struct ocontext *c;
1410         int rc = 0, cmp = 0;
1411
1412         POLICY_RDLOCK;
1413
1414         for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1415                 cmp = strcmp(fstype, genfs->fstype);
1416                 if (cmp <= 0)
1417                         break;
1418         }
1419
1420         if (!genfs || cmp) {
1421                 *sid = SECINITSID_UNLABELED;
1422                 rc = -ENOENT;
1423                 goto out;
1424         }
1425
1426         for (c = genfs->head; c; c = c->next) {
1427                 len = strlen(c->u.name);
1428                 if ((!c->v.sclass || sclass == c->v.sclass) &&
1429                     (strncmp(c->u.name, path, len) == 0))
1430                         break;
1431         }
1432
1433         if (!c) {
1434                 *sid = SECINITSID_UNLABELED;
1435                 rc = -ENOENT;
1436                 goto out;
1437         }
1438
1439         if (!c->sid[0]) {
1440                 rc = sidtab_context_to_sid(&sidtab,
1441                                            &c->context[0],
1442                                            &c->sid[0]);
1443                 if (rc)
1444                         goto out;
1445         }
1446
1447         *sid = c->sid[0];
1448 out:
1449         POLICY_RDUNLOCK;
1450         return rc;
1451 }
1452
1453 /**
1454  * security_fs_use - Determine how to handle labeling for a filesystem.
1455  * @fstype: filesystem type
1456  * @behavior: labeling behavior
1457  * @sid: SID for filesystem (superblock)
1458  */
1459 int security_fs_use(
1460         const char *fstype,
1461         unsigned int *behavior,
1462         u32 *sid)
1463 {
1464         int rc = 0;
1465         struct ocontext *c;
1466
1467         POLICY_RDLOCK;
1468
1469         c = policydb.ocontexts[OCON_FSUSE];
1470         while (c) {
1471                 if (strcmp(fstype, c->u.name) == 0)
1472                         break;
1473                 c = c->next;
1474         }
1475
1476         if (c) {
1477                 *behavior = c->v.behavior;
1478                 if (!c->sid[0]) {
1479                         rc = sidtab_context_to_sid(&sidtab,
1480                                                    &c->context[0],
1481                                                    &c->sid[0]);
1482                         if (rc)
1483                                 goto out;
1484                 }
1485                 *sid = c->sid[0];
1486         } else {
1487                 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1488                 if (rc) {
1489                         *behavior = SECURITY_FS_USE_NONE;
1490                         rc = 0;
1491                 } else {
1492                         *behavior = SECURITY_FS_USE_GENFS;
1493                 }
1494         }
1495
1496 out:
1497         POLICY_RDUNLOCK;
1498         return rc;
1499 }
1500
1501 int security_get_bools(int *len, char ***names, int **values)
1502 {
1503         int i, rc = -ENOMEM;
1504
1505         POLICY_RDLOCK;
1506         *names = NULL;
1507         *values = NULL;
1508
1509         *len = policydb.p_bools.nprim;
1510         if (!*len) {
1511                 rc = 0;
1512                 goto out;
1513         }
1514
1515         *names = (char**)kmalloc(sizeof(char*) * *len, GFP_ATOMIC);
1516         if (!*names)
1517                 goto err;
1518         memset(*names, 0, sizeof(char*) * *len);
1519
1520         *values = (int*)kmalloc(sizeof(int) * *len, GFP_ATOMIC);
1521         if (!*values)
1522                 goto err;
1523
1524         for (i = 0; i < *len; i++) {
1525                 size_t name_len;
1526                 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1527                 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
1528                 (*names)[i] = (char*)kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1529                 if (!(*names)[i])
1530                         goto err;
1531                 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1532                 (*names)[i][name_len - 1] = 0;
1533         }
1534         rc = 0;
1535 out:
1536         POLICY_RDUNLOCK;
1537         return rc;
1538 err:
1539         if (*names) {
1540                 for (i = 0; i < *len; i++)
1541                         if ((*names)[i])
1542                                 kfree((*names)[i]);
1543         }
1544         if (*values)
1545                 kfree(*values);
1546         goto out;
1547 }
1548
1549
1550 int security_set_bools(int len, int *values)
1551 {
1552         int i, rc = 0;
1553         int lenp, seqno = 0;
1554         struct cond_node *cur;
1555
1556         POLICY_WRLOCK;
1557
1558         lenp = policydb.p_bools.nprim;
1559         if (len != lenp) {
1560                 rc = -EFAULT;
1561                 goto out;
1562         }
1563
1564         printk(KERN_INFO "security: committed booleans { ");
1565         for (i = 0; i < len; i++) {
1566                 if (values[i]) {
1567                         policydb.bool_val_to_struct[i]->state = 1;
1568                 } else {
1569                         policydb.bool_val_to_struct[i]->state = 0;
1570                 }
1571                 if (i != 0)
1572                         printk(", ");
1573                 printk("%s:%d", policydb.p_bool_val_to_name[i],
1574                        policydb.bool_val_to_struct[i]->state);
1575         }
1576         printk(" }\n");
1577
1578         for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1579                 rc = evaluate_cond_node(&policydb, cur);
1580                 if (rc)
1581                         goto out;
1582         }
1583
1584         seqno = ++latest_granting;
1585
1586 out:
1587         POLICY_WRUNLOCK;
1588         if (!rc) {
1589                 avc_ss_reset(seqno);
1590                 selnl_notify_policyload(seqno);
1591         }
1592         return rc;
1593 }
1594
1595 int security_get_bool_value(int bool)
1596 {
1597         int rc = 0;
1598         int len;
1599
1600         POLICY_RDLOCK;
1601
1602         len = policydb.p_bools.nprim;
1603         if (bool >= len) {
1604                 rc = -EFAULT;
1605                 goto out;
1606         }
1607
1608         rc = policydb.bool_val_to_struct[bool]->state;
1609 out:
1610         POLICY_RDUNLOCK;
1611         return rc;
1612 }