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