tizen 2.4 release
[kernel/linux-3.0.git] / security / smack / smack_access.c
1 /*
2  * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
3  *
4  *      This program is free software; you can redistribute it and/or modify
5  *      it under the terms of the GNU General Public License as published by
6  *      the Free Software Foundation, version 2.
7  *
8  * Author:
9  *      Casey Schaufler <casey@schaufler-ca.com>
10  *
11  */
12
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/fs.h>
16 #include <linux/sched.h>
17 #include "smack.h"
18
19 struct smack_known smack_known_huh = {
20         .smk_known      = "?",
21         .smk_secid      = 2,
22 };
23
24 struct smack_known smack_known_hat = {
25         .smk_known      = "^",
26         .smk_secid      = 3,
27 };
28
29 struct smack_known smack_known_star = {
30         .smk_known      = "*",
31         .smk_secid      = 4,
32 };
33
34 struct smack_known smack_known_floor = {
35         .smk_known      = "_",
36         .smk_secid      = 5,
37 };
38
39 struct smack_known smack_known_invalid = {
40         .smk_known      = "",
41         .smk_secid      = 6,
42 };
43
44 struct smack_known smack_known_web = {
45         .smk_known      = "@",
46         .smk_secid      = 7,
47 };
48
49 LIST_HEAD(smack_known_list);
50
51 /*
52  * The initial value needs to be bigger than any of the
53  * known values above.
54  */
55 static u32 smack_next_secid = 10;
56
57 /*
58  * are we running in permissive mode?
59  * can be overwritten at run-time by /smack/permissive
60  */
61 #ifdef CONFIG_SECURITY_SMACK_PERMISSIVE_MODE
62 int permissive_mode = SMACK_PERMISSIVE_ALLOWED;
63 #endif
64
65 /*
66  * what events do we log
67  * can be overwritten at run-time by /smack/logging
68  */
69 int log_policy = SMACK_AUDIT_DENIED;
70
71 /**
72  * smk_access_entry - look up matching access rule
73  * @subject_label: a pointer to the subject's Smack label
74  * @object_label: a pointer to the object's Smack label
75  * @rule_list: the list of rules to search
76  *
77  * This function looks up the subject/object pair in the
78  * access rule list and returns the access mode. If no
79  * entry is found returns -ENOENT.
80  *
81  * NOTE:
82  *
83  * Earlier versions of this function allowed for labels that
84  * were not on the label list. This was done to allow for
85  * labels to come over the network that had never been seen
86  * before on this host. Unless the receiving socket has the
87  * star label this will always result in a failure check. The
88  * star labeled socket case is now handled in the networking
89  * hooks so there is no case where the label is not on the
90  * label list. Checking to see if the address of two labels
91  * is the same is now a reliable test.
92  *
93  * Do the object check first because that is more
94  * likely to differ.
95  *
96  * Allowing write access implies allowing locking.
97  */
98 int smk_access_entry(char *subject_label, char *object_label,
99                         struct list_head *rule_list)
100 {
101         int may = -ENOENT;
102         struct smack_rule *srp;
103
104         list_for_each_entry_rcu(srp, rule_list, list) {
105                 if (srp->smk_object == object_label &&
106                     srp->smk_subject->smk_known == subject_label) {
107                         may = srp->smk_access;
108                         break;
109                 }
110         }
111
112         /*
113          * MAY_WRITE implies MAY_LOCK.
114          */
115         if ((may & MAY_WRITE) == MAY_WRITE)
116                 may |= MAY_LOCK;
117         return may;
118 }
119
120 /**
121  * smk_access - determine if a subject has a specific access to an object
122  * @subject_known: a pointer to the subject's Smack label entry
123  * @object_label: a pointer to the object's Smack label
124  * @request: the access requested, in "MAY" format
125  * @a : a pointer to the audit data
126  *
127  * This function looks up the subject/object pair in the
128  * access rule list and returns 0 if the access is permitted,
129  * non zero otherwise.
130  *
131  * Smack labels are shared on smack_list
132  */
133 int smk_access(struct smack_known *subject_known, char *object_label,
134                 int request, struct smk_audit_info *a)
135 {
136         int may = MAY_NOT;
137         int rc = 0;
138
139         /*
140          * Hardcoded comparisons.
141          *
142          * A star subject can't access any object.
143          */
144         if (subject_known == &smack_known_star) {
145                 rc = -EACCES;
146                 goto out_audit;
147         }
148         /*
149          * An internet object can be accessed by any subject.
150          * Tasks cannot be assigned the internet label.
151          * An internet subject can access any object.
152          */
153         if (object_label == smack_known_web.smk_known ||
154             subject_known == &smack_known_web)
155                 goto out_audit;
156         /*
157          * A star object can be accessed by any subject.
158          */
159         if (object_label == smack_known_star.smk_known)
160                 goto out_audit;
161         /*
162          * An object can be accessed in any way by a subject
163          * with the same label.
164          */
165         if (subject_known->smk_known == object_label)
166                 goto out_audit;
167         /*
168          * A hat subject can read or lock any object.
169          * A floor object can be read or locked by any subject.
170          */
171         if ((request & MAY_ANYREAD) == request ||
172             (request & MAY_LOCK) == request) {
173                 if (object_label == smack_known_floor.smk_known)
174                         goto out_audit;
175                 if (subject_known == &smack_known_hat)
176                         goto out_audit;
177         }
178         /*
179          * Beyond here an explicit relationship is required.
180          * If the requested access is contained in the available
181          * access (e.g. read is included in readwrite) it's
182          * good. A negative response from smk_access_entry()
183          * indicates there is no entry for this pair.
184          */
185         rcu_read_lock();
186         may = smk_access_entry(subject_known->smk_known, object_label,
187                                 &subject_known->smk_rules);
188         rcu_read_unlock();
189
190         if (may > 0 && (request & may) == request)
191                 goto out_audit;
192
193         rc = -EACCES;
194 out_audit:
195 #ifdef CONFIG_AUDIT
196         if (a)
197                 smack_log(subject_known->smk_known, object_label, request,
198                                 rc, a);
199 #endif
200 #ifdef CONFIG_SECURITY_SMACK_PERMISSIVE_MODE
201         if (permissive_mode == SMACK_PERMISSIVE_ALLOWED)
202                 return 0;
203 #endif
204         return rc;
205 }
206
207 /**
208  * smk_curacc - determine if current has a specific access to an object
209  * @obj_label: a pointer to the object's Smack label
210  * @mode: the access requested, in "MAY" format
211  * @a : common audit data
212  *
213  * This function checks the current subject label/object label pair
214  * in the access rule list and returns 0 if the access is permitted,
215  * non zero otherwise. It allows that current may have the capability
216  * to override the rules.
217  */
218 int smk_curacc(char *obj_label, u32 mode, struct smk_audit_info *a)
219 {
220         struct task_smack *tsp = current_security();
221         struct smack_known *skp = smk_of_task(tsp);
222         int may;
223         int rc;
224
225         /*
226          * Check the global rule list
227          */
228         rc = smk_access(skp, obj_label, mode, NULL);
229         if (rc == 0) {
230                 /*
231                  * If there is an entry in the task's rule list
232                  * it can further restrict access.
233                  */
234                 may = smk_access_entry(skp->smk_known, obj_label,
235                                         &tsp->smk_rules);
236                 if (may < 0)
237                         goto out_audit;
238                 if ((mode & may) == mode)
239                         goto out_audit;
240                 rc = -EACCES;
241         }
242
243         /*
244          * Allow for priviliged to override policy.
245          */
246         if (rc != 0 && smack_privileged(CAP_MAC_OVERRIDE))
247                 rc = 0;
248
249 out_audit:
250 #ifdef CONFIG_AUDIT
251         if (a)
252                 smack_log(skp->smk_known, obj_label, mode, rc, a);
253 #endif
254         return rc;
255 }
256
257 #ifdef CONFIG_AUDIT
258 /**
259  * smack_str_from_perm : helper to transalate an int to a
260  * readable string
261  * @string : the string to fill
262  * @access : the int
263  *
264  */
265 static inline void smack_str_from_perm(char *string, int access)
266 {
267         int i = 0;
268
269         if (access & MAY_READ)
270                 string[i++] = 'r';
271         if (access & MAY_WRITE)
272                 string[i++] = 'w';
273         if (access & MAY_EXEC)
274                 string[i++] = 'x';
275         if (access & MAY_APPEND)
276                 string[i++] = 'a';
277         if (access & MAY_TRANSMUTE)
278                 string[i++] = 't';
279         if (access & MAY_LOCK)
280                 string[i++] = 'l';
281         string[i] = '\0';
282 }
283 /**
284  * smack_log_callback - SMACK specific information
285  * will be called by generic audit code
286  * @ab : the audit_buffer
287  * @a  : audit_data
288  *
289  */
290 static void smack_log_callback(struct audit_buffer *ab, void *a)
291 {
292         struct common_audit_data *ad = a;
293         struct smack_audit_data *sad = &ad->smack_audit_data;
294         audit_log_format(ab, "lsm=SMACK fn=%s action=%s",
295                          ad->smack_audit_data.function,
296                          sad->result ? "denied" : "granted");
297         audit_log_format(ab, " subject=");
298         audit_log_untrustedstring(ab, sad->subject);
299         audit_log_format(ab, " object=");
300         audit_log_untrustedstring(ab, sad->object);
301         audit_log_format(ab, " requested=%s", sad->request);
302 }
303
304 /**
305  *  smack_log - Audit the granting or denial of permissions.
306  *  @subject_label : smack label of the requester
307  *  @object_label  : smack label of the object being accessed
308  *  @request: requested permissions
309  *  @result: result from smk_access
310  *  @a:  auxiliary audit data
311  *
312  * Audit the granting or denial of permissions in accordance
313  * with the policy.
314  */
315 void smack_log(char *subject_label, char *object_label, int request,
316                int result, struct smk_audit_info *ad)
317 {
318         char request_buffer[SMK_NUM_ACCESS_TYPE + 1];
319         struct smack_audit_data *sad;
320         struct common_audit_data *a = &ad->a;
321
322         /* check if we have to log the current event */
323         if (result != 0 && (log_policy & SMACK_AUDIT_DENIED) == 0)
324                 return;
325         if (result == 0 && (log_policy & SMACK_AUDIT_ACCEPT) == 0)
326                 return;
327
328         if (a->smack_audit_data.function == NULL)
329                 a->smack_audit_data.function = "unknown";
330
331         /* end preparing the audit data */
332         sad = &a->smack_audit_data;
333         smack_str_from_perm(request_buffer, request);
334         sad->subject = subject_label;
335         sad->object  = object_label;
336         sad->request = request_buffer;
337         sad->result  = result;
338         a->lsm_pre_audit = smack_log_callback;
339
340         common_lsm_audit(a);
341 }
342 #else /* #ifdef CONFIG_AUDIT */
343 void smack_log(char *subject_label, char *object_label, int request,
344                int result, struct smk_audit_info *ad)
345 {
346 }
347 #endif
348
349 DEFINE_MUTEX(smack_known_lock);
350
351 struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
352
353 /**
354  * smk_insert_entry - insert a smack label into a hash map,
355  *
356  * this function must be called under smack_known_lock
357  */
358 void smk_insert_entry(struct smack_known *skp)
359 {
360         unsigned int hash;
361         struct hlist_head *head;
362
363         hash = full_name_hash(skp->smk_known, strlen(skp->smk_known));
364         head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
365
366         hlist_add_head_rcu(&skp->smk_hashed, head);
367         list_add_rcu(&skp->list, &smack_known_list);
368 }
369
370 /**
371  * smk_find_entry - find a label on the list, return the list entry
372  * @string: a text string that might be a Smack label
373  *
374  * Returns a pointer to the entry in the label list that
375  * matches the passed string.
376  */
377 struct smack_known *smk_find_entry(const char *string)
378 {
379         unsigned int hash;
380         struct hlist_head *head;
381         struct smack_known *skp;
382         struct hlist_node *n;
383
384         hash = full_name_hash(string, strlen(string));
385         head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
386
387         hlist_for_each_entry_rcu(skp, n, head, smk_hashed)
388                 if (strcmp(skp->smk_known, string) == 0)
389                         return skp;
390
391         return NULL;
392 }
393
394 /**
395  * smk_parse_smack - parse smack label from a text string
396  * @string: a text string that might contain a Smack label
397  * @len: the maximum size, or zero if it is NULL terminated.
398  *
399  * Returns a pointer to the clean label, or NULL
400  */
401 char *smk_parse_smack(const char *string, int len)
402 {
403         char *smack;
404         int i;
405
406         if (len <= 0)
407                 len = strlen(string) + 1;
408
409         /*
410          * Reserve a leading '-' as an indicator that
411          * this isn't a label, but an option to interfaces
412          * including /smack/cipso and /smack/cipso2
413          */
414         if (string[0] == '-')
415                 return NULL;
416
417         for (i = 0; i < len; i++)
418                 if (string[i] > '~' || string[i] <= ' ' || string[i] == '/' ||
419                     string[i] == '"' || string[i] == '\\' || string[i] == '\'')
420                         break;
421
422         if (i == 0 || i >= SMK_LONGLABEL)
423                 return NULL;
424
425         smack = kzalloc(i + 1, GFP_KERNEL);
426         if (smack != NULL) {
427                 strncpy(smack, string, i + 1);
428                 smack[i] = '\0';
429         }
430         return smack;
431 }
432
433 /**
434  * smk_netlbl_mls - convert a catset to netlabel mls categories
435  * @catset: the Smack categories
436  * @sap: where to put the netlabel categories
437  *
438  * Allocates and fills attr.mls
439  * Returns 0 on success, error code on failure.
440  */
441 int smk_netlbl_mls(int level, char *catset, struct netlbl_lsm_secattr *sap,
442                         int len)
443 {
444         unsigned char *cp;
445         unsigned char m;
446         int cat;
447         int rc;
448         int byte;
449
450         sap->flags |= NETLBL_SECATTR_MLS_CAT;
451         sap->attr.mls.lvl = level;
452         sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
453         if (!sap->attr.mls.cat)
454                 return -ENOMEM;
455         sap->attr.mls.cat->startbit = 0;
456
457         for (cat = 1, cp = catset, byte = 0; byte < len; cp++, byte++)
458                 for (m = 0x80; m != 0; m >>= 1, cat++) {
459                         if ((m & *cp) == 0)
460                                 continue;
461                         rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
462                                                           cat, GFP_ATOMIC);
463                         if (rc < 0) {
464                                 netlbl_secattr_catmap_free(sap->attr.mls.cat);
465                                 return rc;
466                         }
467                 }
468
469         return 0;
470 }
471
472 /**
473  * smk_import_entry - import a label, return the list entry
474  * @string: a text string that might be a Smack label
475  * @len: the maximum size, or zero if it is NULL terminated.
476  *
477  * Returns a pointer to the entry in the label list that
478  * matches the passed string, adding it if necessary.
479  */
480 struct smack_known *smk_import_entry(const char *string, int len)
481 {
482         struct smack_known *skp;
483         char *smack;
484         int slen;
485         int rc;
486
487         smack = smk_parse_smack(string, len);
488         if (smack == NULL)
489                 return NULL;
490
491         mutex_lock(&smack_known_lock);
492
493         skp = smk_find_entry(smack);
494         if (skp != NULL)
495                 goto freeout;
496
497         skp = kzalloc(sizeof(*skp), GFP_KERNEL);
498         if (skp == NULL)
499                 goto freeout;
500
501         skp->smk_known = smack;
502         skp->smk_secid = smack_next_secid++;
503         skp->smk_netlabel.domain = skp->smk_known;
504         skp->smk_netlabel.flags =
505                 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
506         /*
507          * If direct labeling works use it.
508          * Otherwise use mapped labeling.
509          */
510         slen = strlen(smack);
511         if (slen < SMK_CIPSOLEN)
512                 rc = smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
513                                &skp->smk_netlabel, slen);
514         else
515                 rc = smk_netlbl_mls(smack_cipso_mapped, (char *)&skp->smk_secid,
516                                &skp->smk_netlabel, sizeof(skp->smk_secid));
517
518         if (rc >= 0) {
519                 INIT_LIST_HEAD(&skp->smk_rules);
520                 mutex_init(&skp->smk_rules_lock);
521                 /*
522                  * Make sure that the entry is actually
523                  * filled before putting it on the list.
524                  */
525                 smk_insert_entry(skp);
526                 goto unlockout;
527         }
528         /*
529          * smk_netlbl_mls failed.
530          */
531         kfree(skp);
532         skp = NULL;
533 freeout:
534         kfree(smack);
535 unlockout:
536         mutex_unlock(&smack_known_lock);
537
538         return skp;
539 }
540
541 /**
542  * smk_import - import a smack label
543  * @string: a text string that might be a Smack label
544  * @len: the maximum size, or zero if it is NULL terminated.
545  *
546  * Returns a pointer to the label in the label list that
547  * matches the passed string, adding it if necessary.
548  */
549 char *smk_import(const char *string, int len)
550 {
551         struct smack_known *skp;
552
553         /* labels cannot begin with a '-' */
554         if (string[0] == '-')
555                 return NULL;
556         skp = smk_import_entry(string, len);
557         if (skp == NULL)
558                 return NULL;
559         return skp->smk_known;
560 }
561
562 /**
563  * smack_from_secid - find the Smack label associated with a secid
564  * @secid: an integer that might be associated with a Smack label
565  *
566  * Returns a pointer to the appropriate Smack label entry if there is one,
567  * otherwise a pointer to the invalid Smack label.
568  */
569 struct smack_known *smack_from_secid(const u32 secid)
570 {
571         struct smack_known *skp;
572
573         rcu_read_lock();
574         list_for_each_entry_rcu(skp, &smack_known_list, list) {
575                 if (skp->smk_secid == secid) {
576                         rcu_read_unlock();
577                         return skp;
578                 }
579         }
580
581         /*
582          * If we got this far someone asked for the translation
583          * of a secid that is not on the list.
584          */
585         rcu_read_unlock();
586         return &smack_known_invalid;
587 }
588
589 /**
590  * smack_to_secid - find the secid associated with a Smack label
591  * @smack: the Smack label
592  *
593  * Returns the appropriate secid if there is one,
594  * otherwise 0
595  */
596 u32 smack_to_secid(const char *smack)
597 {
598         struct smack_known *skp = smk_find_entry(smack);
599
600         if (skp == NULL)
601                 return 0;
602         return skp->smk_secid;
603 }
604
605 /*
606  * Unless a process is running with one of these labels
607  * even having CAP_MAC_OVERRIDE isn't enough to grant
608  * privilege to violate MAC policy. If no labels are
609  * designated (the empty list case) capabilities apply to
610  * everyone.
611  */
612 LIST_HEAD(smack_onlycap_list);
613 DEFINE_MUTEX(smack_onlycap_lock);
614
615 /*
616  * Is the task privileged and allowed to be privileged
617  * by the onlycap rule.
618  *
619  * Returns 1 if the task is allowed to be privileged, 0 if it's not.
620  */
621 int smack_privileged(int cap)
622 {
623         struct smack_known *skp = smk_of_current();
624         struct smack_onlycap *sop;
625
626         if (!capable(cap))
627                 return 0;
628
629         rcu_read_lock();
630         if (list_empty(&smack_onlycap_list)) {
631                 rcu_read_unlock();
632                 return 1;
633         }
634
635         list_for_each_entry_rcu(sop, &smack_onlycap_list, list) {
636                 if (sop->smk_label == skp) {
637                         rcu_read_unlock();
638                         return 1;
639                 }
640         }
641         rcu_read_unlock();
642
643         return 0;
644 }