spi-topcliff-pch: Fix issue for transmitting over 4KByte
[zen-stable.git] / security / smack / smackfs.c
blob5c32f36ff70618dfb08e3040c94060238dfa44da
1 /*
2 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
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.
8 * Authors:
9 * Casey Schaufler <casey@schaufler-ca.com>
10 * Ahmed S. Darwish <darwish.07@gmail.com>
12 * Special thanks to the authors of selinuxfs.
14 * Karl MacMillan <kmacmillan@tresys.com>
15 * James Morris <jmorris@redhat.com>
19 #include <linux/kernel.h>
20 #include <linux/vmalloc.h>
21 #include <linux/security.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <net/net_namespace.h>
25 #include <net/netlabel.h>
26 #include <net/cipso_ipv4.h>
27 #include <linux/seq_file.h>
28 #include <linux/ctype.h>
29 #include <linux/audit.h>
30 #include "smack.h"
33 * smackfs pseudo filesystem.
36 enum smk_inos {
37 SMK_ROOT_INO = 2,
38 SMK_LOAD = 3, /* load policy */
39 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
40 SMK_DOI = 5, /* CIPSO DOI */
41 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
42 SMK_AMBIENT = 7, /* internet ambient label */
43 SMK_NETLBLADDR = 8, /* single label hosts */
44 SMK_ONLYCAP = 9, /* the only "capable" label */
45 SMK_LOGGING = 10, /* logging */
46 SMK_LOAD_SELF = 11, /* task specific rules */
47 SMK_ACCESSES = 12, /* access policy */
51 * List locks
53 static DEFINE_MUTEX(smack_list_lock);
54 static DEFINE_MUTEX(smack_cipso_lock);
55 static DEFINE_MUTEX(smack_ambient_lock);
56 static DEFINE_MUTEX(smk_netlbladdr_lock);
59 * This is the "ambient" label for network traffic.
60 * If it isn't somehow marked, use this.
61 * It can be reset via smackfs/ambient
63 char *smack_net_ambient = smack_known_floor.smk_known;
66 * This is the level in a CIPSO header that indicates a
67 * smack label is contained directly in the category set.
68 * It can be reset via smackfs/direct
70 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
73 * Unless a process is running with this label even
74 * having CAP_MAC_OVERRIDE isn't enough to grant
75 * privilege to violate MAC policy. If no label is
76 * designated (the NULL case) capabilities apply to
77 * everyone. It is expected that the hat (^) label
78 * will be used if any label is used.
80 char *smack_onlycap;
83 * Certain IP addresses may be designated as single label hosts.
84 * Packets are sent there unlabeled, but only from tasks that
85 * can write to the specified label.
88 LIST_HEAD(smk_netlbladdr_list);
91 * Rule lists are maintained for each label.
92 * This master list is just for reading /smack/load.
94 struct smack_master_list {
95 struct list_head list;
96 struct smack_rule *smk_rule;
99 LIST_HEAD(smack_rule_list);
101 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
103 const char *smack_cipso_option = SMACK_CIPSO_OPTION;
106 * Values for parsing cipso rules
107 * SMK_DIGITLEN: Length of a digit field in a rule.
108 * SMK_CIPSOMIN: Minimum possible cipso rule length.
109 * SMK_CIPSOMAX: Maximum possible cipso rule length.
111 #define SMK_DIGITLEN 4
112 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
113 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
116 * Values for parsing MAC rules
117 * SMK_ACCESS: Maximum possible combination of access permissions
118 * SMK_ACCESSLEN: Maximum length for a rule access field
119 * SMK_LOADLEN: Smack rule length
121 #define SMK_OACCESS "rwxa"
122 #define SMK_ACCESS "rwxat"
123 #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
124 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
125 #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
126 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
129 * smk_netlabel_audit_set - fill a netlbl_audit struct
130 * @nap: structure to fill
132 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
134 nap->loginuid = audit_get_loginuid(current);
135 nap->sessionid = audit_get_sessionid(current);
136 nap->secid = smack_to_secid(smk_of_current());
140 * Values for parsing single label host rules
141 * "1.2.3.4 X"
142 * "192.168.138.129/32 abcdefghijklmnopqrstuvw"
144 #define SMK_NETLBLADDRMIN 9
145 #define SMK_NETLBLADDRMAX 42
148 * smk_set_access - add a rule to the rule list
149 * @srp: the new rule to add
150 * @rule_list: the list of rules
151 * @rule_lock: the rule list lock
153 * Looks through the current subject/object/access list for
154 * the subject/object pair and replaces the access that was
155 * there. If the pair isn't found add it with the specified
156 * access.
158 * Returns 1 if a rule was found to exist already, 0 if it is new
159 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
160 * during the allocation of the new pair to add.
162 static int smk_set_access(struct smack_rule *srp, struct list_head *rule_list,
163 struct mutex *rule_lock)
165 struct smack_rule *sp;
166 int found = 0;
168 mutex_lock(rule_lock);
171 * Because the object label is less likely to match
172 * than the subject label check it first
174 list_for_each_entry_rcu(sp, rule_list, list) {
175 if (sp->smk_object == srp->smk_object &&
176 sp->smk_subject == srp->smk_subject) {
177 found = 1;
178 sp->smk_access = srp->smk_access;
179 break;
182 if (found == 0)
183 list_add_rcu(&srp->list, rule_list);
185 mutex_unlock(rule_lock);
187 return found;
191 * smk_parse_rule - parse Smack rule from load string
192 * @data: string to be parsed whose size is SMK_LOADLEN
193 * @rule: Smack rule
194 * @import: if non-zero, import labels
196 static int smk_parse_rule(const char *data, struct smack_rule *rule, int import)
198 char smack[SMK_LABELLEN];
199 struct smack_known *skp;
201 if (import) {
202 rule->smk_subject = smk_import(data, 0);
203 if (rule->smk_subject == NULL)
204 return -1;
206 rule->smk_object = smk_import(data + SMK_LABELLEN, 0);
207 if (rule->smk_object == NULL)
208 return -1;
209 } else {
210 smk_parse_smack(data, 0, smack);
211 skp = smk_find_entry(smack);
212 if (skp == NULL)
213 return -1;
214 rule->smk_subject = skp->smk_known;
216 smk_parse_smack(data + SMK_LABELLEN, 0, smack);
217 skp = smk_find_entry(smack);
218 if (skp == NULL)
219 return -1;
220 rule->smk_object = skp->smk_known;
223 rule->smk_access = 0;
225 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
226 case '-':
227 break;
228 case 'r':
229 case 'R':
230 rule->smk_access |= MAY_READ;
231 break;
232 default:
233 return -1;
236 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
237 case '-':
238 break;
239 case 'w':
240 case 'W':
241 rule->smk_access |= MAY_WRITE;
242 break;
243 default:
244 return -1;
247 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
248 case '-':
249 break;
250 case 'x':
251 case 'X':
252 rule->smk_access |= MAY_EXEC;
253 break;
254 default:
255 return -1;
258 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
259 case '-':
260 break;
261 case 'a':
262 case 'A':
263 rule->smk_access |= MAY_APPEND;
264 break;
265 default:
266 return -1;
269 switch (data[SMK_LABELLEN + SMK_LABELLEN + 4]) {
270 case '-':
271 break;
272 case 't':
273 case 'T':
274 rule->smk_access |= MAY_TRANSMUTE;
275 break;
276 default:
277 return -1;
280 return 0;
284 * smk_write_load_list - write() for any /smack/load
285 * @file: file pointer, not actually used
286 * @buf: where to get the data from
287 * @count: bytes sent
288 * @ppos: where to start - must be 0
289 * @rule_list: the list of rules to write to
290 * @rule_lock: lock for the rule list
292 * Get one smack access rule from above.
293 * The format is exactly:
294 * char subject[SMK_LABELLEN]
295 * char object[SMK_LABELLEN]
296 * char access[SMK_ACCESSLEN]
298 * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
300 static ssize_t smk_write_load_list(struct file *file, const char __user *buf,
301 size_t count, loff_t *ppos,
302 struct list_head *rule_list,
303 struct mutex *rule_lock)
305 struct smack_master_list *smlp;
306 struct smack_known *skp;
307 struct smack_rule *rule;
308 char *data;
309 int rc = -EINVAL;
310 int load = 0;
313 * No partial writes.
314 * Enough data must be present.
316 if (*ppos != 0)
317 return -EINVAL;
319 * Minor hack for backward compatibility
321 if (count < (SMK_OLOADLEN) || count > SMK_LOADLEN)
322 return -EINVAL;
324 data = kzalloc(SMK_LOADLEN, GFP_KERNEL);
325 if (data == NULL)
326 return -ENOMEM;
328 if (copy_from_user(data, buf, count) != 0) {
329 rc = -EFAULT;
330 goto out;
334 * More on the minor hack for backward compatibility
336 if (count == (SMK_OLOADLEN))
337 data[SMK_OLOADLEN] = '-';
339 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
340 if (rule == NULL) {
341 rc = -ENOMEM;
342 goto out;
345 if (smk_parse_rule(data, rule, 1))
346 goto out_free_rule;
348 if (rule_list == NULL) {
349 load = 1;
350 skp = smk_find_entry(rule->smk_subject);
351 rule_list = &skp->smk_rules;
352 rule_lock = &skp->smk_rules_lock;
355 rc = count;
357 * If this is "load" as opposed to "load-self" and a new rule
358 * it needs to get added for reporting.
359 * smk_set_access returns true if there was already a rule
360 * for the subject/object pair, and false if it was new.
362 if (load && !smk_set_access(rule, rule_list, rule_lock)) {
363 smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
364 if (smlp != NULL) {
365 smlp->smk_rule = rule;
366 list_add_rcu(&smlp->list, &smack_rule_list);
367 } else
368 rc = -ENOMEM;
369 goto out;
372 out_free_rule:
373 kfree(rule);
374 out:
375 kfree(data);
376 return rc;
380 * Core logic for smackfs seq list operations.
383 static void *smk_seq_start(struct seq_file *s, loff_t *pos,
384 struct list_head *head)
386 struct list_head *list;
389 * This is 0 the first time through.
391 if (s->index == 0)
392 s->private = head;
394 if (s->private == NULL)
395 return NULL;
397 list = s->private;
398 if (list_empty(list))
399 return NULL;
401 if (s->index == 0)
402 return list->next;
403 return list;
406 static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
407 struct list_head *head)
409 struct list_head *list = v;
411 if (list_is_last(list, head)) {
412 s->private = NULL;
413 return NULL;
415 s->private = list->next;
416 return list->next;
419 static void smk_seq_stop(struct seq_file *s, void *v)
421 /* No-op */
425 * Seq_file read operations for /smack/load
428 static void *load_seq_start(struct seq_file *s, loff_t *pos)
430 return smk_seq_start(s, pos, &smack_rule_list);
433 static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
435 return smk_seq_next(s, v, pos, &smack_rule_list);
438 static int load_seq_show(struct seq_file *s, void *v)
440 struct list_head *list = v;
441 struct smack_master_list *smlp =
442 list_entry(list, struct smack_master_list, list);
443 struct smack_rule *srp = smlp->smk_rule;
445 seq_printf(s, "%s %s", (char *)srp->smk_subject,
446 (char *)srp->smk_object);
448 seq_putc(s, ' ');
450 if (srp->smk_access & MAY_READ)
451 seq_putc(s, 'r');
452 if (srp->smk_access & MAY_WRITE)
453 seq_putc(s, 'w');
454 if (srp->smk_access & MAY_EXEC)
455 seq_putc(s, 'x');
456 if (srp->smk_access & MAY_APPEND)
457 seq_putc(s, 'a');
458 if (srp->smk_access & MAY_TRANSMUTE)
459 seq_putc(s, 't');
460 if (srp->smk_access == 0)
461 seq_putc(s, '-');
463 seq_putc(s, '\n');
465 return 0;
468 static const struct seq_operations load_seq_ops = {
469 .start = load_seq_start,
470 .next = load_seq_next,
471 .show = load_seq_show,
472 .stop = smk_seq_stop,
476 * smk_open_load - open() for /smack/load
477 * @inode: inode structure representing file
478 * @file: "load" file pointer
480 * For reading, use load_seq_* seq_file reading operations.
482 static int smk_open_load(struct inode *inode, struct file *file)
484 return seq_open(file, &load_seq_ops);
488 * smk_write_load - write() for /smack/load
489 * @file: file pointer, not actually used
490 * @buf: where to get the data from
491 * @count: bytes sent
492 * @ppos: where to start - must be 0
495 static ssize_t smk_write_load(struct file *file, const char __user *buf,
496 size_t count, loff_t *ppos)
500 * Must have privilege.
501 * No partial writes.
502 * Enough data must be present.
504 if (!capable(CAP_MAC_ADMIN))
505 return -EPERM;
507 return smk_write_load_list(file, buf, count, ppos, NULL, NULL);
510 static const struct file_operations smk_load_ops = {
511 .open = smk_open_load,
512 .read = seq_read,
513 .llseek = seq_lseek,
514 .write = smk_write_load,
515 .release = seq_release,
519 * smk_cipso_doi - initialize the CIPSO domain
521 static void smk_cipso_doi(void)
523 int rc;
524 struct cipso_v4_doi *doip;
525 struct netlbl_audit nai;
527 smk_netlabel_audit_set(&nai);
529 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
530 if (rc != 0)
531 printk(KERN_WARNING "%s:%d remove rc = %d\n",
532 __func__, __LINE__, rc);
534 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
535 if (doip == NULL)
536 panic("smack: Failed to initialize cipso DOI.\n");
537 doip->map.std = NULL;
538 doip->doi = smk_cipso_doi_value;
539 doip->type = CIPSO_V4_MAP_PASS;
540 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
541 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
542 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
544 rc = netlbl_cfg_cipsov4_add(doip, &nai);
545 if (rc != 0) {
546 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
547 __func__, __LINE__, rc);
548 kfree(doip);
549 return;
551 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
552 if (rc != 0) {
553 printk(KERN_WARNING "%s:%d map add rc = %d\n",
554 __func__, __LINE__, rc);
555 kfree(doip);
556 return;
561 * smk_unlbl_ambient - initialize the unlabeled domain
562 * @oldambient: previous domain string
564 static void smk_unlbl_ambient(char *oldambient)
566 int rc;
567 struct netlbl_audit nai;
569 smk_netlabel_audit_set(&nai);
571 if (oldambient != NULL) {
572 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
573 if (rc != 0)
574 printk(KERN_WARNING "%s:%d remove rc = %d\n",
575 __func__, __LINE__, rc);
578 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
579 NULL, NULL, &nai);
580 if (rc != 0)
581 printk(KERN_WARNING "%s:%d add rc = %d\n",
582 __func__, __LINE__, rc);
586 * Seq_file read operations for /smack/cipso
589 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
591 return smk_seq_start(s, pos, &smack_known_list);
594 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
596 return smk_seq_next(s, v, pos, &smack_known_list);
600 * Print cipso labels in format:
601 * label level[/cat[,cat]]
603 static int cipso_seq_show(struct seq_file *s, void *v)
605 struct list_head *list = v;
606 struct smack_known *skp =
607 list_entry(list, struct smack_known, list);
608 struct smack_cipso *scp = skp->smk_cipso;
609 char *cbp;
610 char sep = '/';
611 int cat = 1;
612 int i;
613 unsigned char m;
615 if (scp == NULL)
616 return 0;
618 seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
620 cbp = scp->smk_catset;
621 for (i = 0; i < SMK_LABELLEN; i++)
622 for (m = 0x80; m != 0; m >>= 1) {
623 if (m & cbp[i]) {
624 seq_printf(s, "%c%d", sep, cat);
625 sep = ',';
627 cat++;
630 seq_putc(s, '\n');
632 return 0;
635 static const struct seq_operations cipso_seq_ops = {
636 .start = cipso_seq_start,
637 .next = cipso_seq_next,
638 .show = cipso_seq_show,
639 .stop = smk_seq_stop,
643 * smk_open_cipso - open() for /smack/cipso
644 * @inode: inode structure representing file
645 * @file: "cipso" file pointer
647 * Connect our cipso_seq_* operations with /smack/cipso
648 * file_operations
650 static int smk_open_cipso(struct inode *inode, struct file *file)
652 return seq_open(file, &cipso_seq_ops);
656 * smk_write_cipso - write() for /smack/cipso
657 * @file: file pointer, not actually used
658 * @buf: where to get the data from
659 * @count: bytes sent
660 * @ppos: where to start
662 * Accepts only one cipso rule per write call.
663 * Returns number of bytes written or error code, as appropriate
665 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
666 size_t count, loff_t *ppos)
668 struct smack_known *skp;
669 struct smack_cipso *scp = NULL;
670 char mapcatset[SMK_LABELLEN];
671 int maplevel;
672 int cat;
673 int catlen;
674 ssize_t rc = -EINVAL;
675 char *data = NULL;
676 char *rule;
677 int ret;
678 int i;
681 * Must have privilege.
682 * No partial writes.
683 * Enough data must be present.
685 if (!capable(CAP_MAC_ADMIN))
686 return -EPERM;
687 if (*ppos != 0)
688 return -EINVAL;
689 if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
690 return -EINVAL;
692 data = kzalloc(count + 1, GFP_KERNEL);
693 if (data == NULL)
694 return -ENOMEM;
696 if (copy_from_user(data, buf, count) != 0) {
697 rc = -EFAULT;
698 goto unlockedout;
701 /* labels cannot begin with a '-' */
702 if (data[0] == '-') {
703 rc = -EINVAL;
704 goto unlockedout;
706 data[count] = '\0';
707 rule = data;
709 * Only allow one writer at a time. Writes should be
710 * quite rare and small in any case.
712 mutex_lock(&smack_cipso_lock);
714 skp = smk_import_entry(rule, 0);
715 if (skp == NULL)
716 goto out;
718 rule += SMK_LABELLEN;
719 ret = sscanf(rule, "%d", &maplevel);
720 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
721 goto out;
723 rule += SMK_DIGITLEN;
724 ret = sscanf(rule, "%d", &catlen);
725 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
726 goto out;
728 if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
729 goto out;
731 memset(mapcatset, 0, sizeof(mapcatset));
733 for (i = 0; i < catlen; i++) {
734 rule += SMK_DIGITLEN;
735 ret = sscanf(rule, "%d", &cat);
736 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
737 goto out;
739 smack_catset_bit(cat, mapcatset);
742 if (skp->smk_cipso == NULL) {
743 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
744 if (scp == NULL) {
745 rc = -ENOMEM;
746 goto out;
750 spin_lock_bh(&skp->smk_cipsolock);
752 if (scp == NULL)
753 scp = skp->smk_cipso;
754 else
755 skp->smk_cipso = scp;
757 scp->smk_level = maplevel;
758 memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
760 spin_unlock_bh(&skp->smk_cipsolock);
762 rc = count;
763 out:
764 mutex_unlock(&smack_cipso_lock);
765 unlockedout:
766 kfree(data);
767 return rc;
770 static const struct file_operations smk_cipso_ops = {
771 .open = smk_open_cipso,
772 .read = seq_read,
773 .llseek = seq_lseek,
774 .write = smk_write_cipso,
775 .release = seq_release,
779 * Seq_file read operations for /smack/netlabel
782 static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
784 return smk_seq_start(s, pos, &smk_netlbladdr_list);
787 static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
789 return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
791 #define BEBITS (sizeof(__be32) * 8)
794 * Print host/label pairs
796 static int netlbladdr_seq_show(struct seq_file *s, void *v)
798 struct list_head *list = v;
799 struct smk_netlbladdr *skp =
800 list_entry(list, struct smk_netlbladdr, list);
801 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
802 int maskn;
803 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
805 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
807 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
808 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
810 return 0;
813 static const struct seq_operations netlbladdr_seq_ops = {
814 .start = netlbladdr_seq_start,
815 .next = netlbladdr_seq_next,
816 .show = netlbladdr_seq_show,
817 .stop = smk_seq_stop,
821 * smk_open_netlbladdr - open() for /smack/netlabel
822 * @inode: inode structure representing file
823 * @file: "netlabel" file pointer
825 * Connect our netlbladdr_seq_* operations with /smack/netlabel
826 * file_operations
828 static int smk_open_netlbladdr(struct inode *inode, struct file *file)
830 return seq_open(file, &netlbladdr_seq_ops);
834 * smk_netlbladdr_insert
835 * @new : netlabel to insert
837 * This helper insert netlabel in the smack_netlbladdrs list
838 * sorted by netmask length (longest to smallest)
839 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
842 static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
844 struct smk_netlbladdr *m, *m_next;
846 if (list_empty(&smk_netlbladdr_list)) {
847 list_add_rcu(&new->list, &smk_netlbladdr_list);
848 return;
851 m = list_entry_rcu(smk_netlbladdr_list.next,
852 struct smk_netlbladdr, list);
854 /* the comparison '>' is a bit hacky, but works */
855 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
856 list_add_rcu(&new->list, &smk_netlbladdr_list);
857 return;
860 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
861 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
862 list_add_rcu(&new->list, &m->list);
863 return;
865 m_next = list_entry_rcu(m->list.next,
866 struct smk_netlbladdr, list);
867 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
868 list_add_rcu(&new->list, &m->list);
869 return;
876 * smk_write_netlbladdr - write() for /smack/netlabel
877 * @file: file pointer, not actually used
878 * @buf: where to get the data from
879 * @count: bytes sent
880 * @ppos: where to start
882 * Accepts only one netlbladdr per write call.
883 * Returns number of bytes written or error code, as appropriate
885 static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
886 size_t count, loff_t *ppos)
888 struct smk_netlbladdr *skp;
889 struct sockaddr_in newname;
890 char smack[SMK_LABELLEN];
891 char *sp;
892 char data[SMK_NETLBLADDRMAX + 1];
893 char *host = (char *)&newname.sin_addr.s_addr;
894 int rc;
895 struct netlbl_audit audit_info;
896 struct in_addr mask;
897 unsigned int m;
898 int found;
899 u32 mask_bits = (1<<31);
900 __be32 nsa;
901 u32 temp_mask;
904 * Must have privilege.
905 * No partial writes.
906 * Enough data must be present.
907 * "<addr/mask, as a.b.c.d/e><space><label>"
908 * "<addr, as a.b.c.d><space><label>"
910 if (!capable(CAP_MAC_ADMIN))
911 return -EPERM;
912 if (*ppos != 0)
913 return -EINVAL;
914 if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX)
915 return -EINVAL;
916 if (copy_from_user(data, buf, count) != 0)
917 return -EFAULT;
919 data[count] = '\0';
921 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
922 &host[0], &host[1], &host[2], &host[3], &m, smack);
923 if (rc != 6) {
924 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
925 &host[0], &host[1], &host[2], &host[3], smack);
926 if (rc != 5)
927 return -EINVAL;
928 m = BEBITS;
930 if (m > BEBITS)
931 return -EINVAL;
933 /* if smack begins with '-', its an option, don't import it */
934 if (smack[0] != '-') {
935 sp = smk_import(smack, 0);
936 if (sp == NULL)
937 return -EINVAL;
938 } else {
939 /* check known options */
940 if (strcmp(smack, smack_cipso_option) == 0)
941 sp = (char *)smack_cipso_option;
942 else
943 return -EINVAL;
946 for (temp_mask = 0; m > 0; m--) {
947 temp_mask |= mask_bits;
948 mask_bits >>= 1;
950 mask.s_addr = cpu_to_be32(temp_mask);
952 newname.sin_addr.s_addr &= mask.s_addr;
954 * Only allow one writer at a time. Writes should be
955 * quite rare and small in any case.
957 mutex_lock(&smk_netlbladdr_lock);
959 nsa = newname.sin_addr.s_addr;
960 /* try to find if the prefix is already in the list */
961 found = 0;
962 list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
963 if (skp->smk_host.sin_addr.s_addr == nsa &&
964 skp->smk_mask.s_addr == mask.s_addr) {
965 found = 1;
966 break;
969 smk_netlabel_audit_set(&audit_info);
971 if (found == 0) {
972 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
973 if (skp == NULL)
974 rc = -ENOMEM;
975 else {
976 rc = 0;
977 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
978 skp->smk_mask.s_addr = mask.s_addr;
979 skp->smk_label = sp;
980 smk_netlbladdr_insert(skp);
982 } else {
983 /* we delete the unlabeled entry, only if the previous label
984 * wasn't the special CIPSO option */
985 if (skp->smk_label != smack_cipso_option)
986 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
987 &skp->smk_host.sin_addr, &skp->smk_mask,
988 PF_INET, &audit_info);
989 else
990 rc = 0;
991 skp->smk_label = sp;
995 * Now tell netlabel about the single label nature of
996 * this host so that incoming packets get labeled.
997 * but only if we didn't get the special CIPSO option
999 if (rc == 0 && sp != smack_cipso_option)
1000 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1001 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
1002 smack_to_secid(skp->smk_label), &audit_info);
1004 if (rc == 0)
1005 rc = count;
1007 mutex_unlock(&smk_netlbladdr_lock);
1009 return rc;
1012 static const struct file_operations smk_netlbladdr_ops = {
1013 .open = smk_open_netlbladdr,
1014 .read = seq_read,
1015 .llseek = seq_lseek,
1016 .write = smk_write_netlbladdr,
1017 .release = seq_release,
1021 * smk_read_doi - read() for /smack/doi
1022 * @filp: file pointer, not actually used
1023 * @buf: where to put the result
1024 * @count: maximum to send along
1025 * @ppos: where to start
1027 * Returns number of bytes read or error code, as appropriate
1029 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1030 size_t count, loff_t *ppos)
1032 char temp[80];
1033 ssize_t rc;
1035 if (*ppos != 0)
1036 return 0;
1038 sprintf(temp, "%d", smk_cipso_doi_value);
1039 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1041 return rc;
1045 * smk_write_doi - write() for /smack/doi
1046 * @file: file pointer, not actually used
1047 * @buf: where to get the data from
1048 * @count: bytes sent
1049 * @ppos: where to start
1051 * Returns number of bytes written or error code, as appropriate
1053 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1054 size_t count, loff_t *ppos)
1056 char temp[80];
1057 int i;
1059 if (!capable(CAP_MAC_ADMIN))
1060 return -EPERM;
1062 if (count >= sizeof(temp) || count == 0)
1063 return -EINVAL;
1065 if (copy_from_user(temp, buf, count) != 0)
1066 return -EFAULT;
1068 temp[count] = '\0';
1070 if (sscanf(temp, "%d", &i) != 1)
1071 return -EINVAL;
1073 smk_cipso_doi_value = i;
1075 smk_cipso_doi();
1077 return count;
1080 static const struct file_operations smk_doi_ops = {
1081 .read = smk_read_doi,
1082 .write = smk_write_doi,
1083 .llseek = default_llseek,
1087 * smk_read_direct - read() for /smack/direct
1088 * @filp: file pointer, not actually used
1089 * @buf: where to put the result
1090 * @count: maximum to send along
1091 * @ppos: where to start
1093 * Returns number of bytes read or error code, as appropriate
1095 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1096 size_t count, loff_t *ppos)
1098 char temp[80];
1099 ssize_t rc;
1101 if (*ppos != 0)
1102 return 0;
1104 sprintf(temp, "%d", smack_cipso_direct);
1105 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1107 return rc;
1111 * smk_write_direct - write() for /smack/direct
1112 * @file: file pointer, not actually used
1113 * @buf: where to get the data from
1114 * @count: bytes sent
1115 * @ppos: where to start
1117 * Returns number of bytes written or error code, as appropriate
1119 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1120 size_t count, loff_t *ppos)
1122 char temp[80];
1123 int i;
1125 if (!capable(CAP_MAC_ADMIN))
1126 return -EPERM;
1128 if (count >= sizeof(temp) || count == 0)
1129 return -EINVAL;
1131 if (copy_from_user(temp, buf, count) != 0)
1132 return -EFAULT;
1134 temp[count] = '\0';
1136 if (sscanf(temp, "%d", &i) != 1)
1137 return -EINVAL;
1139 smack_cipso_direct = i;
1141 return count;
1144 static const struct file_operations smk_direct_ops = {
1145 .read = smk_read_direct,
1146 .write = smk_write_direct,
1147 .llseek = default_llseek,
1151 * smk_read_ambient - read() for /smack/ambient
1152 * @filp: file pointer, not actually used
1153 * @buf: where to put the result
1154 * @cn: maximum to send along
1155 * @ppos: where to start
1157 * Returns number of bytes read or error code, as appropriate
1159 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1160 size_t cn, loff_t *ppos)
1162 ssize_t rc;
1163 int asize;
1165 if (*ppos != 0)
1166 return 0;
1168 * Being careful to avoid a problem in the case where
1169 * smack_net_ambient gets changed in midstream.
1171 mutex_lock(&smack_ambient_lock);
1173 asize = strlen(smack_net_ambient) + 1;
1175 if (cn >= asize)
1176 rc = simple_read_from_buffer(buf, cn, ppos,
1177 smack_net_ambient, asize);
1178 else
1179 rc = -EINVAL;
1181 mutex_unlock(&smack_ambient_lock);
1183 return rc;
1187 * smk_write_ambient - write() for /smack/ambient
1188 * @file: file pointer, not actually used
1189 * @buf: where to get the data from
1190 * @count: bytes sent
1191 * @ppos: where to start
1193 * Returns number of bytes written or error code, as appropriate
1195 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1196 size_t count, loff_t *ppos)
1198 char in[SMK_LABELLEN];
1199 char *oldambient;
1200 char *smack;
1202 if (!capable(CAP_MAC_ADMIN))
1203 return -EPERM;
1205 if (count >= SMK_LABELLEN)
1206 return -EINVAL;
1208 if (copy_from_user(in, buf, count) != 0)
1209 return -EFAULT;
1211 smack = smk_import(in, count);
1212 if (smack == NULL)
1213 return -EINVAL;
1215 mutex_lock(&smack_ambient_lock);
1217 oldambient = smack_net_ambient;
1218 smack_net_ambient = smack;
1219 smk_unlbl_ambient(oldambient);
1221 mutex_unlock(&smack_ambient_lock);
1223 return count;
1226 static const struct file_operations smk_ambient_ops = {
1227 .read = smk_read_ambient,
1228 .write = smk_write_ambient,
1229 .llseek = default_llseek,
1233 * smk_read_onlycap - read() for /smack/onlycap
1234 * @filp: file pointer, not actually used
1235 * @buf: where to put the result
1236 * @cn: maximum to send along
1237 * @ppos: where to start
1239 * Returns number of bytes read or error code, as appropriate
1241 static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1242 size_t cn, loff_t *ppos)
1244 char *smack = "";
1245 ssize_t rc = -EINVAL;
1246 int asize;
1248 if (*ppos != 0)
1249 return 0;
1251 if (smack_onlycap != NULL)
1252 smack = smack_onlycap;
1254 asize = strlen(smack) + 1;
1256 if (cn >= asize)
1257 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1259 return rc;
1263 * smk_write_onlycap - write() for /smack/onlycap
1264 * @file: file pointer, not actually used
1265 * @buf: where to get the data from
1266 * @count: bytes sent
1267 * @ppos: where to start
1269 * Returns number of bytes written or error code, as appropriate
1271 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1272 size_t count, loff_t *ppos)
1274 char in[SMK_LABELLEN];
1275 char *sp = smk_of_task(current->cred->security);
1277 if (!capable(CAP_MAC_ADMIN))
1278 return -EPERM;
1281 * This can be done using smk_access() but is done
1282 * explicitly for clarity. The smk_access() implementation
1283 * would use smk_access(smack_onlycap, MAY_WRITE)
1285 if (smack_onlycap != NULL && smack_onlycap != sp)
1286 return -EPERM;
1288 if (count >= SMK_LABELLEN)
1289 return -EINVAL;
1291 if (copy_from_user(in, buf, count) != 0)
1292 return -EFAULT;
1295 * Should the null string be passed in unset the onlycap value.
1296 * This seems like something to be careful with as usually
1297 * smk_import only expects to return NULL for errors. It
1298 * is usually the case that a nullstring or "\n" would be
1299 * bad to pass to smk_import but in fact this is useful here.
1301 smack_onlycap = smk_import(in, count);
1303 return count;
1306 static const struct file_operations smk_onlycap_ops = {
1307 .read = smk_read_onlycap,
1308 .write = smk_write_onlycap,
1309 .llseek = default_llseek,
1313 * smk_read_logging - read() for /smack/logging
1314 * @filp: file pointer, not actually used
1315 * @buf: where to put the result
1316 * @cn: maximum to send along
1317 * @ppos: where to start
1319 * Returns number of bytes read or error code, as appropriate
1321 static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1322 size_t count, loff_t *ppos)
1324 char temp[32];
1325 ssize_t rc;
1327 if (*ppos != 0)
1328 return 0;
1330 sprintf(temp, "%d\n", log_policy);
1331 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1332 return rc;
1336 * smk_write_logging - write() for /smack/logging
1337 * @file: file pointer, not actually used
1338 * @buf: where to get the data from
1339 * @count: bytes sent
1340 * @ppos: where to start
1342 * Returns number of bytes written or error code, as appropriate
1344 static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1345 size_t count, loff_t *ppos)
1347 char temp[32];
1348 int i;
1350 if (!capable(CAP_MAC_ADMIN))
1351 return -EPERM;
1353 if (count >= sizeof(temp) || count == 0)
1354 return -EINVAL;
1356 if (copy_from_user(temp, buf, count) != 0)
1357 return -EFAULT;
1359 temp[count] = '\0';
1361 if (sscanf(temp, "%d", &i) != 1)
1362 return -EINVAL;
1363 if (i < 0 || i > 3)
1364 return -EINVAL;
1365 log_policy = i;
1366 return count;
1371 static const struct file_operations smk_logging_ops = {
1372 .read = smk_read_logging,
1373 .write = smk_write_logging,
1374 .llseek = default_llseek,
1378 * Seq_file read operations for /smack/load-self
1381 static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
1383 struct task_smack *tsp = current_security();
1385 return smk_seq_start(s, pos, &tsp->smk_rules);
1388 static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
1390 struct task_smack *tsp = current_security();
1392 return smk_seq_next(s, v, pos, &tsp->smk_rules);
1395 static int load_self_seq_show(struct seq_file *s, void *v)
1397 struct list_head *list = v;
1398 struct smack_rule *srp =
1399 list_entry(list, struct smack_rule, list);
1401 seq_printf(s, "%s %s", (char *)srp->smk_subject,
1402 (char *)srp->smk_object);
1404 seq_putc(s, ' ');
1406 if (srp->smk_access & MAY_READ)
1407 seq_putc(s, 'r');
1408 if (srp->smk_access & MAY_WRITE)
1409 seq_putc(s, 'w');
1410 if (srp->smk_access & MAY_EXEC)
1411 seq_putc(s, 'x');
1412 if (srp->smk_access & MAY_APPEND)
1413 seq_putc(s, 'a');
1414 if (srp->smk_access & MAY_TRANSMUTE)
1415 seq_putc(s, 't');
1416 if (srp->smk_access == 0)
1417 seq_putc(s, '-');
1419 seq_putc(s, '\n');
1421 return 0;
1424 static const struct seq_operations load_self_seq_ops = {
1425 .start = load_self_seq_start,
1426 .next = load_self_seq_next,
1427 .show = load_self_seq_show,
1428 .stop = smk_seq_stop,
1433 * smk_open_load_self - open() for /smack/load-self
1434 * @inode: inode structure representing file
1435 * @file: "load" file pointer
1437 * For reading, use load_seq_* seq_file reading operations.
1439 static int smk_open_load_self(struct inode *inode, struct file *file)
1441 return seq_open(file, &load_self_seq_ops);
1445 * smk_write_load_self - write() for /smack/load-self
1446 * @file: file pointer, not actually used
1447 * @buf: where to get the data from
1448 * @count: bytes sent
1449 * @ppos: where to start - must be 0
1452 static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
1453 size_t count, loff_t *ppos)
1455 struct task_smack *tsp = current_security();
1457 return smk_write_load_list(file, buf, count, ppos, &tsp->smk_rules,
1458 &tsp->smk_rules_lock);
1461 static const struct file_operations smk_load_self_ops = {
1462 .open = smk_open_load_self,
1463 .read = seq_read,
1464 .llseek = seq_lseek,
1465 .write = smk_write_load_self,
1466 .release = seq_release,
1470 * smk_write_access - handle access check transaction
1471 * @file: file pointer
1472 * @buf: data from user space
1473 * @count: bytes sent
1474 * @ppos: where to start - must be 0
1476 static ssize_t smk_write_access(struct file *file, const char __user *buf,
1477 size_t count, loff_t *ppos)
1479 struct smack_rule rule;
1480 char *data;
1481 int res;
1483 data = simple_transaction_get(file, buf, count);
1484 if (IS_ERR(data))
1485 return PTR_ERR(data);
1487 if (count < SMK_LOADLEN || smk_parse_rule(data, &rule, 0))
1488 return -EINVAL;
1490 res = smk_access(rule.smk_subject, rule.smk_object, rule.smk_access,
1491 NULL);
1492 data[0] = res == 0 ? '1' : '0';
1493 data[1] = '\0';
1495 simple_transaction_set(file, 2);
1496 return SMK_LOADLEN;
1499 static const struct file_operations smk_access_ops = {
1500 .write = smk_write_access,
1501 .read = simple_transaction_read,
1502 .release = simple_transaction_release,
1503 .llseek = generic_file_llseek,
1507 * smk_fill_super - fill the /smackfs superblock
1508 * @sb: the empty superblock
1509 * @data: unused
1510 * @silent: unused
1512 * Fill in the well known entries for /smack
1514 * Returns 0 on success, an error code on failure
1516 static int smk_fill_super(struct super_block *sb, void *data, int silent)
1518 int rc;
1519 struct inode *root_inode;
1521 static struct tree_descr smack_files[] = {
1522 [SMK_LOAD] = {
1523 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
1524 [SMK_CIPSO] = {
1525 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
1526 [SMK_DOI] = {
1527 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
1528 [SMK_DIRECT] = {
1529 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
1530 [SMK_AMBIENT] = {
1531 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
1532 [SMK_NETLBLADDR] = {
1533 "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
1534 [SMK_ONLYCAP] = {
1535 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
1536 [SMK_LOGGING] = {
1537 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
1538 [SMK_LOAD_SELF] = {
1539 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
1540 [SMK_ACCESSES] = {
1541 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
1542 /* last one */
1543 {""}
1546 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
1547 if (rc != 0) {
1548 printk(KERN_ERR "%s failed %d while creating inodes\n",
1549 __func__, rc);
1550 return rc;
1553 root_inode = sb->s_root->d_inode;
1554 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
1556 return 0;
1560 * smk_mount - get the smackfs superblock
1561 * @fs_type: passed along without comment
1562 * @flags: passed along without comment
1563 * @dev_name: passed along without comment
1564 * @data: passed along without comment
1566 * Just passes everything along.
1568 * Returns what the lower level code does.
1570 static struct dentry *smk_mount(struct file_system_type *fs_type,
1571 int flags, const char *dev_name, void *data)
1573 return mount_single(fs_type, flags, data, smk_fill_super);
1576 static struct file_system_type smk_fs_type = {
1577 .name = "smackfs",
1578 .mount = smk_mount,
1579 .kill_sb = kill_litter_super,
1582 static struct vfsmount *smackfs_mount;
1585 * init_smk_fs - get the smackfs superblock
1587 * register the smackfs
1589 * Do not register smackfs if Smack wasn't enabled
1590 * on boot. We can not put this method normally under the
1591 * smack_init() code path since the security subsystem get
1592 * initialized before the vfs caches.
1594 * Returns true if we were not chosen on boot or if
1595 * we were chosen and filesystem registration succeeded.
1597 static int __init init_smk_fs(void)
1599 int err;
1601 if (!security_module_enable(&smack_ops))
1602 return 0;
1604 err = register_filesystem(&smk_fs_type);
1605 if (!err) {
1606 smackfs_mount = kern_mount(&smk_fs_type);
1607 if (IS_ERR(smackfs_mount)) {
1608 printk(KERN_ERR "smackfs: could not mount!\n");
1609 err = PTR_ERR(smackfs_mount);
1610 smackfs_mount = NULL;
1614 smk_cipso_doi();
1615 smk_unlbl_ambient(NULL);
1617 mutex_init(&smack_known_floor.smk_rules_lock);
1618 mutex_init(&smack_known_hat.smk_rules_lock);
1619 mutex_init(&smack_known_huh.smk_rules_lock);
1620 mutex_init(&smack_known_invalid.smk_rules_lock);
1621 mutex_init(&smack_known_star.smk_rules_lock);
1622 mutex_init(&smack_known_web.smk_rules_lock);
1624 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
1625 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
1626 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
1627 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
1628 INIT_LIST_HEAD(&smack_known_star.smk_rules);
1629 INIT_LIST_HEAD(&smack_known_web.smk_rules);
1631 return err;
1634 __initcall(init_smk_fs);