smack: add permissive mode for debugging purpose
[platform/kernel/linux-arm64.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 any object.
169          * A floor object can be read by any subject.
170          */
171         if ((request & MAY_ANYREAD) == request) {
172                 if (object_label == smack_known_floor.smk_known)
173                         goto out_audit;
174                 if (subject_known == &smack_known_hat)
175                         goto out_audit;
176         }
177         /*
178          * Beyond here an explicit relationship is required.
179          * If the requested access is contained in the available
180          * access (e.g. read is included in readwrite) it's
181          * good. A negative response from smk_access_entry()
182          * indicates there is no entry for this pair.
183          */
184         rcu_read_lock();
185         may = smk_access_entry(subject_known->smk_known, object_label,
186                                 &subject_known->smk_rules);
187         rcu_read_unlock();
188
189         if (may > 0 && (request & may) == request)
190                 goto out_audit;
191
192         rc = -EACCES;
193 out_audit:
194 #ifdef CONFIG_AUDIT
195         if (a)
196                 smack_log(subject_known->smk_known, object_label, request,
197                                 rc, a);
198 #endif
199 #ifdef CONFIG_SECURITY_SMACK_PERMISSIVE_MODE
200         if (permissive_mode == SMACK_PERMISSIVE_ALLOWED)
201                 return 0;
202 #endif
203         return rc;
204 }
205
206 /**
207  * smk_curacc - determine if current has a specific access to an object
208  * @obj_label: a pointer to the object's Smack label
209  * @mode: the access requested, in "MAY" format
210  * @a : common audit data
211  *
212  * This function checks the current subject label/object label pair
213  * in the access rule list and returns 0 if the access is permitted,
214  * non zero otherwise. It allows that current may have the capability
215  * to override the rules.
216  */
217 int smk_curacc(char *obj_label, u32 mode, struct smk_audit_info *a)
218 {
219         struct task_smack *tsp = current_security();
220         struct smack_known *skp = smk_of_task(tsp);
221         int may;
222         int rc;
223
224         /*
225          * Check the global rule list
226          */
227         rc = smk_access(skp, obj_label, mode, NULL);
228         if (rc == 0) {
229                 /*
230                  * If there is an entry in the task's rule list
231                  * it can further restrict access.
232                  */
233                 may = smk_access_entry(skp->smk_known, obj_label,
234                                         &tsp->smk_rules);
235                 if (may < 0)
236                         goto out_audit;
237                 if ((mode & may) == mode)
238                         goto out_audit;
239                 rc = -EACCES;
240         }
241
242         /*
243          * Allow for priviliged to override policy.
244          */
245         if (rc != 0 && smack_privileged(CAP_MAC_OVERRIDE))
246                 rc = 0;
247
248 out_audit:
249 #ifdef CONFIG_AUDIT
250         if (a)
251                 smack_log(skp->smk_known, obj_label, mode, rc, a);
252 #endif
253         return rc;
254 }
255
256 #ifdef CONFIG_AUDIT
257 /**
258  * smack_str_from_perm : helper to transalate an int to a
259  * readable string
260  * @string : the string to fill
261  * @access : the int
262  *
263  */
264 static inline void smack_str_from_perm(char *string, int access)
265 {
266         int i = 0;
267
268         if (access & MAY_READ)
269                 string[i++] = 'r';
270         if (access & MAY_WRITE)
271                 string[i++] = 'w';
272         if (access & MAY_EXEC)
273                 string[i++] = 'x';
274         if (access & MAY_APPEND)
275                 string[i++] = 'a';
276         if (access & MAY_TRANSMUTE)
277                 string[i++] = 't';
278         if (access & MAY_LOCK)
279                 string[i++] = 'l';
280         string[i] = '\0';
281 }
282 /**
283  * smack_log_callback - SMACK specific information
284  * will be called by generic audit code
285  * @ab : the audit_buffer
286  * @a  : audit_data
287  *
288  */
289 static void smack_log_callback(struct audit_buffer *ab, void *a)
290 {
291         struct common_audit_data *ad = a;
292         struct smack_audit_data *sad = ad->smack_audit_data;
293         audit_log_format(ab, "lsm=SMACK fn=%s action=%s",
294                          ad->smack_audit_data->function,
295                          sad->result ? "denied" : "granted");
296         audit_log_format(ab, " subject=");
297         audit_log_untrustedstring(ab, sad->subject);
298         audit_log_format(ab, " object=");
299         audit_log_untrustedstring(ab, sad->object);
300         audit_log_format(ab, " requested=%s", sad->request);
301 }
302
303 /**
304  *  smack_log - Audit the granting or denial of permissions.
305  *  @subject_label : smack label of the requester
306  *  @object_label  : smack label of the object being accessed
307  *  @request: requested permissions
308  *  @result: result from smk_access
309  *  @a:  auxiliary audit data
310  *
311  * Audit the granting or denial of permissions in accordance
312  * with the policy.
313  */
314 void smack_log(char *subject_label, char *object_label, int request,
315                int result, struct smk_audit_info *ad)
316 {
317         char request_buffer[SMK_NUM_ACCESS_TYPE + 1];
318         struct smack_audit_data *sad;
319         struct common_audit_data *a = &ad->a;
320
321         /* check if we have to log the current event */
322         if (result != 0 && (log_policy & SMACK_AUDIT_DENIED) == 0)
323                 return;
324         if (result == 0 && (log_policy & SMACK_AUDIT_ACCEPT) == 0)
325                 return;
326
327         sad = a->smack_audit_data;
328
329         if (sad->function == NULL)
330                 sad->function = "unknown";
331
332         /* end preparing the 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
339         common_lsm_audit(a, smack_log_callback, NULL);
340 }
341 #else /* #ifdef CONFIG_AUDIT */
342 void smack_log(char *subject_label, char *object_label, int request,
343                int result, struct smk_audit_info *ad)
344 {
345 }
346 #endif
347
348 DEFINE_MUTEX(smack_known_lock);
349
350 struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
351
352 /**
353  * smk_insert_entry - insert a smack label into a hash map,
354  *
355  * this function must be called under smack_known_lock
356  */
357 void smk_insert_entry(struct smack_known *skp)
358 {
359         unsigned int hash;
360         struct hlist_head *head;
361
362         hash = full_name_hash(skp->smk_known, strlen(skp->smk_known));
363         head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
364
365         hlist_add_head_rcu(&skp->smk_hashed, head);
366         list_add_rcu(&skp->list, &smack_known_list);
367 }
368
369 /**
370  * smk_find_entry - find a label on the list, return the list entry
371  * @string: a text string that might be a Smack label
372  *
373  * Returns a pointer to the entry in the label list that
374  * matches the passed string.
375  */
376 struct smack_known *smk_find_entry(const char *string)
377 {
378         unsigned int hash;
379         struct hlist_head *head;
380         struct smack_known *skp;
381
382         hash = full_name_hash(string, strlen(string));
383         head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
384
385         hlist_for_each_entry_rcu(skp, head, smk_hashed)
386                 if (strcmp(skp->smk_known, string) == 0)
387                         return skp;
388
389         return NULL;
390 }
391
392 /**
393  * smk_parse_smack - parse smack label from a text string
394  * @string: a text string that might contain a Smack label
395  * @len: the maximum size, or zero if it is NULL terminated.
396  *
397  * Returns a pointer to the clean label, or NULL
398  */
399 char *smk_parse_smack(const char *string, int len)
400 {
401         char *smack;
402         int i;
403
404         if (len <= 0)
405                 len = strlen(string) + 1;
406
407         /*
408          * Reserve a leading '-' as an indicator that
409          * this isn't a label, but an option to interfaces
410          * including /smack/cipso and /smack/cipso2
411          */
412         if (string[0] == '-')
413                 return NULL;
414
415         for (i = 0; i < len; i++)
416                 if (string[i] > '~' || string[i] <= ' ' || string[i] == '/' ||
417                     string[i] == '"' || string[i] == '\\' || string[i] == '\'')
418                         break;
419
420         if (i == 0 || i >= SMK_LONGLABEL)
421                 return NULL;
422
423         smack = kzalloc(i + 1, GFP_KERNEL);
424         if (smack != NULL) {
425                 strncpy(smack, string, i + 1);
426                 smack[i] = '\0';
427         }
428         return smack;
429 }
430
431 /**
432  * smk_netlbl_mls - convert a catset to netlabel mls categories
433  * @catset: the Smack categories
434  * @sap: where to put the netlabel categories
435  *
436  * Allocates and fills attr.mls
437  * Returns 0 on success, error code on failure.
438  */
439 int smk_netlbl_mls(int level, char *catset, struct netlbl_lsm_secattr *sap,
440                         int len)
441 {
442         unsigned char *cp;
443         unsigned char m;
444         int cat;
445         int rc;
446         int byte;
447
448         sap->flags |= NETLBL_SECATTR_MLS_CAT;
449         sap->attr.mls.lvl = level;
450         sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
451         if (!sap->attr.mls.cat)
452                 return -ENOMEM;
453         sap->attr.mls.cat->startbit = 0;
454
455         for (cat = 1, cp = catset, byte = 0; byte < len; cp++, byte++)
456                 for (m = 0x80; m != 0; m >>= 1, cat++) {
457                         if ((m & *cp) == 0)
458                                 continue;
459                         rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
460                                                           cat, GFP_ATOMIC);
461                         if (rc < 0) {
462                                 netlbl_secattr_catmap_free(sap->attr.mls.cat);
463                                 return rc;
464                         }
465                 }
466
467         return 0;
468 }
469
470 /**
471  * smk_import_entry - import a label, return the list entry
472  * @string: a text string that might be a Smack label
473  * @len: the maximum size, or zero if it is NULL terminated.
474  *
475  * Returns a pointer to the entry in the label list that
476  * matches the passed string, adding it if necessary.
477  */
478 struct smack_known *smk_import_entry(const char *string, int len)
479 {
480         struct smack_known *skp;
481         char *smack;
482         int slen;
483         int rc;
484
485         smack = smk_parse_smack(string, len);
486         if (smack == NULL)
487                 return NULL;
488
489         mutex_lock(&smack_known_lock);
490
491         skp = smk_find_entry(smack);
492         if (skp != NULL)
493                 goto freeout;
494
495         skp = kzalloc(sizeof(*skp), GFP_KERNEL);
496         if (skp == NULL)
497                 goto freeout;
498
499         skp->smk_known = smack;
500         skp->smk_secid = smack_next_secid++;
501         skp->smk_netlabel.domain = skp->smk_known;
502         skp->smk_netlabel.flags =
503                 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
504         /*
505          * If direct labeling works use it.
506          * Otherwise use mapped labeling.
507          */
508         slen = strlen(smack);
509         if (slen < SMK_CIPSOLEN)
510                 rc = smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
511                                &skp->smk_netlabel, slen);
512         else
513                 rc = smk_netlbl_mls(smack_cipso_mapped, (char *)&skp->smk_secid,
514                                &skp->smk_netlabel, sizeof(skp->smk_secid));
515
516         if (rc >= 0) {
517                 INIT_LIST_HEAD(&skp->smk_rules);
518                 mutex_init(&skp->smk_rules_lock);
519                 /*
520                  * Make sure that the entry is actually
521                  * filled before putting it on the list.
522                  */
523                 smk_insert_entry(skp);
524                 goto unlockout;
525         }
526         /*
527          * smk_netlbl_mls failed.
528          */
529         kfree(skp);
530         skp = NULL;
531 freeout:
532         kfree(smack);
533 unlockout:
534         mutex_unlock(&smack_known_lock);
535
536         return skp;
537 }
538
539 /**
540  * smk_import - import a smack label
541  * @string: a text string that might be a Smack label
542  * @len: the maximum size, or zero if it is NULL terminated.
543  *
544  * Returns a pointer to the label in the label list that
545  * matches the passed string, adding it if necessary.
546  */
547 char *smk_import(const char *string, int len)
548 {
549         struct smack_known *skp;
550
551         /* labels cannot begin with a '-' */
552         if (string[0] == '-')
553                 return NULL;
554         skp = smk_import_entry(string, len);
555         if (skp == NULL)
556                 return NULL;
557         return skp->smk_known;
558 }
559
560 /**
561  * smack_from_secid - find the Smack label associated with a secid
562  * @secid: an integer that might be associated with a Smack label
563  *
564  * Returns a pointer to the appropriate Smack label entry if there is one,
565  * otherwise a pointer to the invalid Smack label.
566  */
567 struct smack_known *smack_from_secid(const u32 secid)
568 {
569         struct smack_known *skp;
570
571         rcu_read_lock();
572         list_for_each_entry_rcu(skp, &smack_known_list, list) {
573                 if (skp->smk_secid == secid) {
574                         rcu_read_unlock();
575                         return skp;
576                 }
577         }
578
579         /*
580          * If we got this far someone asked for the translation
581          * of a secid that is not on the list.
582          */
583         rcu_read_unlock();
584         return &smack_known_invalid;
585 }
586
587 /**
588  * smack_to_secid - find the secid associated with a Smack label
589  * @smack: the Smack label
590  *
591  * Returns the appropriate secid if there is one,
592  * otherwise 0
593  */
594 u32 smack_to_secid(const char *smack)
595 {
596         struct smack_known *skp = smk_find_entry(smack);
597
598         if (skp == NULL)
599                 return 0;
600         return skp->smk_secid;
601 }