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.
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/cipso_ipv4.h>
26 #include <linux/seq_file.h>
27 #include <linux/ctype.h>
28 #include <linux/audit.h>
29 #include <linux/magic.h>
33 * smackfs pseudo filesystem.
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 */
48 SMK_MAPPED = 13, /* CIPSO level indicating mapped label */
49 SMK_LOAD2 = 14, /* load policy with long labels */
50 SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */
51 SMK_ACCESS2 = 16, /* make an access check with long labels */
52 SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */
53 SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */
54 SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */
60 static DEFINE_MUTEX(smack_cipso_lock);
61 static DEFINE_MUTEX(smack_ambient_lock);
62 static DEFINE_MUTEX(smk_netlbladdr_lock);
65 * This is the "ambient" label for network traffic.
66 * If it isn't somehow marked, use this.
67 * It can be reset via smackfs/ambient
69 char *smack_net_ambient;
72 * This is the level in a CIPSO header that indicates a
73 * smack label is contained directly in the category set.
74 * It can be reset via smackfs/direct
76 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
79 * This is the level in a CIPSO header that indicates a
80 * secid is contained directly in the category set.
81 * It can be reset via smackfs/mapped
83 int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
86 * Unless a process is running with this label even
87 * having CAP_MAC_OVERRIDE isn't enough to grant
88 * privilege to violate MAC policy. If no label is
89 * designated (the NULL case) capabilities apply to
90 * everyone. It is expected that the hat (^) label
91 * will be used if any label is used.
96 * Certain IP addresses may be designated as single label hosts.
97 * Packets are sent there unlabeled, but only from tasks that
98 * can write to the specified label.
101 LIST_HEAD(smk_netlbladdr_list);
104 * Rule lists are maintained for each label.
105 * This master list is just for reading /smack/load and /smack/load2.
107 struct smack_master_list {
108 struct list_head list;
109 struct smack_rule *smk_rule;
112 LIST_HEAD(smack_rule_list);
114 struct smack_parsed_rule {
121 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
123 const char *smack_cipso_option = SMACK_CIPSO_OPTION;
126 * Values for parsing cipso rules
127 * SMK_DIGITLEN: Length of a digit field in a rule.
128 * SMK_CIPSOMIN: Minimum possible cipso rule length.
129 * SMK_CIPSOMAX: Maximum possible cipso rule length.
131 #define SMK_DIGITLEN 4
132 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
133 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
136 * Values for parsing MAC rules
137 * SMK_ACCESS: Maximum possible combination of access permissions
138 * SMK_ACCESSLEN: Maximum length for a rule access field
139 * SMK_LOADLEN: Smack rule length
141 #define SMK_OACCESS "rwxa"
142 #define SMK_ACCESS "rwxat"
143 #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
144 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
145 #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
146 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
149 * Stricly for CIPSO level manipulation.
150 * Set the category bit number in a smack label sized buffer.
152 static inline void smack_catset_bit(unsigned int cat, char *catsetp)
154 if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
157 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
161 * smk_netlabel_audit_set - fill a netlbl_audit struct
162 * @nap: structure to fill
164 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
166 nap->loginuid = audit_get_loginuid(current);
167 nap->sessionid = audit_get_sessionid(current);
168 nap->secid = smack_to_secid(smk_of_current());
172 * Value for parsing single label host rules
175 #define SMK_NETLBLADDRMIN 9
178 * smk_set_access - add a rule to the rule list or replace an old rule
179 * @srp: the rule to add or replace
180 * @rule_list: the list of rules
181 * @rule_lock: the rule list lock
182 * @global: if non-zero, indicates a global rule
184 * Looks through the current subject/object/access list for
185 * the subject/object pair and replaces the access that was
186 * there. If the pair isn't found add it with the specified
189 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
190 * during the allocation of the new pair to add.
192 static int smk_set_access(struct smack_parsed_rule *srp,
193 struct list_head *rule_list,
194 struct mutex *rule_lock, int global)
196 struct smack_rule *sp;
197 struct smack_master_list *smlp;
201 mutex_lock(rule_lock);
204 * Because the object label is less likely to match
205 * than the subject label check it first
207 list_for_each_entry_rcu(sp, rule_list, list) {
208 if (sp->smk_object == srp->smk_object &&
209 sp->smk_subject == srp->smk_subject) {
211 sp->smk_access |= srp->smk_access1;
212 sp->smk_access &= ~srp->smk_access2;
218 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
224 sp->smk_subject = srp->smk_subject;
225 sp->smk_object = srp->smk_object;
226 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
228 list_add_rcu(&sp->list, rule_list);
230 * If this is a global as opposed to self and a new rule
231 * it needs to get added for reporting.
234 smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
237 list_add_rcu(&smlp->list, &smack_rule_list);
244 mutex_unlock(rule_lock);
249 * smk_perm_from_str - parse smack accesses from a text string
250 * @string: a text string that contains a Smack accesses code
252 * Returns an integer with respective bits set for specified accesses.
254 static int smk_perm_from_str(const char *string)
259 for (cp = string; ; cp++)
281 perm |= MAY_TRANSMUTE;
289 * smk_fill_rule - Fill Smack rule from strings
290 * @subject: subject label string
291 * @object: object label string
292 * @access1: access string
293 * @access2: string with permissions to be removed
295 * @import: if non-zero, import labels
296 * @len: label length limit
298 * Returns 0 on success, -1 on failure
300 static int smk_fill_rule(const char *subject, const char *object,
301 const char *access1, const char *access2,
302 struct smack_parsed_rule *rule, int import,
306 struct smack_known *skp;
309 rule->smk_subject = smk_import(subject, len);
310 if (rule->smk_subject == NULL)
313 rule->smk_object = smk_import(object, len);
314 if (rule->smk_object == NULL)
317 cp = smk_parse_smack(subject, len);
320 skp = smk_find_entry(cp);
324 rule->smk_subject = skp->smk_known;
326 cp = smk_parse_smack(object, len);
329 skp = smk_find_entry(cp);
333 rule->smk_object = skp->smk_known;
336 rule->smk_access1 = smk_perm_from_str(access1);
338 rule->smk_access2 = smk_perm_from_str(access2);
340 rule->smk_access2 = ~rule->smk_access1;
346 * smk_parse_rule - parse Smack rule from load string
347 * @data: string to be parsed whose size is SMK_LOADLEN
349 * @import: if non-zero, import labels
351 * Returns 0 on success, -1 on errors.
353 static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
358 rc = smk_fill_rule(data, data + SMK_LABELLEN,
359 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
360 import, SMK_LABELLEN);
365 * smk_parse_long_rule - parse Smack rule from rule string
366 * @data: string to be parsed, null terminated
367 * @rule: Will be filled with Smack parsed rule
368 * @import: if non-zero, import labels
369 * @change: if non-zero, data is from /smack/change-rule
371 * Returns 0 on success, -1 on failure
373 static int smk_parse_long_rule(const char *data, struct smack_parsed_rule *rule,
374 int import, int change)
383 /* This is inefficient */
384 datalen = strlen(data);
386 /* Our first element can be 64 + \0 with no spaces */
387 subject = kzalloc(datalen + 1, GFP_KERNEL);
390 object = kzalloc(datalen, GFP_KERNEL);
393 access1 = kzalloc(datalen, GFP_KERNEL);
396 access2 = kzalloc(datalen, GFP_KERNEL);
401 if (sscanf(data, "%s %s %s %s",
402 subject, object, access1, access2) == 4)
403 rc = smk_fill_rule(subject, object, access1, access2,
406 if (sscanf(data, "%s %s %s", subject, object, access1) == 3)
407 rc = smk_fill_rule(subject, object, access1, NULL,
421 #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
422 #define SMK_LONG_FMT 1 /* Variable long label format */
423 #define SMK_CHANGE_FMT 2 /* Rule modification format */
425 * smk_write_rules_list - write() for any /smack rule file
426 * @file: file pointer, not actually used
427 * @buf: where to get the data from
429 * @ppos: where to start - must be 0
430 * @rule_list: the list of rules to write to
431 * @rule_lock: lock for the rule list
432 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
434 * Get one smack access rule from above.
435 * The format for SMK_LONG_FMT is:
436 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
437 * The format for SMK_FIXED24_FMT is exactly:
438 * "subject object rwxat"
439 * The format for SMK_CHANGE_FMT is:
440 * "subject<whitespace>object<whitespace>
441 * acc_enable<whitespace>acc_disable[<whitespace>...]"
443 static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
444 size_t count, loff_t *ppos,
445 struct list_head *rule_list,
446 struct mutex *rule_lock, int format)
448 struct smack_known *skp;
449 struct smack_parsed_rule *rule;
457 * Enough data must be present.
462 if (format == SMK_FIXED24_FMT) {
464 * Minor hack for backward compatibility
466 if (count != SMK_OLOADLEN && count != SMK_LOADLEN)
468 datalen = SMK_LOADLEN;
472 data = kzalloc(datalen, GFP_KERNEL);
476 if (copy_from_user(data, buf, count) != 0) {
481 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
487 if (format == SMK_LONG_FMT) {
489 * Be sure the data string is terminated.
492 if (smk_parse_long_rule(data, rule, 1, 0))
494 } else if (format == SMK_CHANGE_FMT) {
496 if (smk_parse_long_rule(data, rule, 1, 1))
500 * More on the minor hack for backward compatibility
502 if (count == (SMK_OLOADLEN))
503 data[SMK_OLOADLEN] = '-';
504 if (smk_parse_rule(data, rule, 1))
509 if (rule_list == NULL) {
511 skp = smk_find_entry(rule->smk_subject);
512 rule_list = &skp->smk_rules;
513 rule_lock = &skp->smk_rules_lock;
516 rc = smk_set_access(rule, rule_list, rule_lock, load);
530 * Core logic for smackfs seq list operations.
533 static void *smk_seq_start(struct seq_file *s, loff_t *pos,
534 struct list_head *head)
536 struct list_head *list;
539 * This is 0 the first time through.
544 if (s->private == NULL)
548 if (list_empty(list))
556 static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
557 struct list_head *head)
559 struct list_head *list = v;
561 if (list_is_last(list, head)) {
565 s->private = list->next;
569 static void smk_seq_stop(struct seq_file *s, void *v)
574 static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
577 * Don't show any rules with label names too long for
578 * interface file (/smack/load or /smack/load2)
579 * because you should expect to be able to write
580 * anything you read back.
582 if (strlen(srp->smk_subject) >= max || strlen(srp->smk_object) >= max)
585 if (srp->smk_access == 0)
588 seq_printf(s, "%s %s", srp->smk_subject, srp->smk_object);
592 if (srp->smk_access & MAY_READ)
594 if (srp->smk_access & MAY_WRITE)
596 if (srp->smk_access & MAY_EXEC)
598 if (srp->smk_access & MAY_APPEND)
600 if (srp->smk_access & MAY_TRANSMUTE)
607 * Seq_file read operations for /smack/load
610 static void *load2_seq_start(struct seq_file *s, loff_t *pos)
612 return smk_seq_start(s, pos, &smack_rule_list);
615 static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
617 return smk_seq_next(s, v, pos, &smack_rule_list);
620 static int load_seq_show(struct seq_file *s, void *v)
622 struct list_head *list = v;
623 struct smack_master_list *smlp =
624 list_entry(list, struct smack_master_list, list);
626 smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
631 static const struct seq_operations load_seq_ops = {
632 .start = load2_seq_start,
633 .next = load2_seq_next,
634 .show = load_seq_show,
635 .stop = smk_seq_stop,
639 * smk_open_load - open() for /smack/load
640 * @inode: inode structure representing file
641 * @file: "load" file pointer
643 * For reading, use load_seq_* seq_file reading operations.
645 static int smk_open_load(struct inode *inode, struct file *file)
647 return seq_open(file, &load_seq_ops);
651 * smk_write_load - write() for /smack/load
652 * @file: file pointer, not actually used
653 * @buf: where to get the data from
655 * @ppos: where to start - must be 0
658 static ssize_t smk_write_load(struct file *file, const char __user *buf,
659 size_t count, loff_t *ppos)
662 * Must have privilege.
664 * Enough data must be present.
666 if (!smack_privileged(CAP_MAC_ADMIN))
669 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
673 static const struct file_operations smk_load_ops = {
674 .open = smk_open_load,
677 .write = smk_write_load,
678 .release = seq_release,
682 * smk_cipso_doi - initialize the CIPSO domain
684 static void smk_cipso_doi(void)
687 struct cipso_v4_doi *doip;
688 struct netlbl_audit nai;
690 smk_netlabel_audit_set(&nai);
692 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
694 printk(KERN_WARNING "%s:%d remove rc = %d\n",
695 __func__, __LINE__, rc);
697 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
699 panic("smack: Failed to initialize cipso DOI.\n");
700 doip->map.std = NULL;
701 doip->doi = smk_cipso_doi_value;
702 doip->type = CIPSO_V4_MAP_PASS;
703 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
704 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
705 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
707 rc = netlbl_cfg_cipsov4_add(doip, &nai);
709 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
710 __func__, __LINE__, rc);
714 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
716 printk(KERN_WARNING "%s:%d map add rc = %d\n",
717 __func__, __LINE__, rc);
724 * smk_unlbl_ambient - initialize the unlabeled domain
725 * @oldambient: previous domain string
727 static void smk_unlbl_ambient(char *oldambient)
730 struct netlbl_audit nai;
732 smk_netlabel_audit_set(&nai);
734 if (oldambient != NULL) {
735 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
737 printk(KERN_WARNING "%s:%d remove rc = %d\n",
738 __func__, __LINE__, rc);
740 if (smack_net_ambient == NULL)
741 smack_net_ambient = smack_known_floor.smk_known;
743 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
746 printk(KERN_WARNING "%s:%d add rc = %d\n",
747 __func__, __LINE__, rc);
751 * Seq_file read operations for /smack/cipso
754 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
756 return smk_seq_start(s, pos, &smack_known_list);
759 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
761 return smk_seq_next(s, v, pos, &smack_known_list);
765 * Print cipso labels in format:
766 * label level[/cat[,cat]]
768 static int cipso_seq_show(struct seq_file *s, void *v)
770 struct list_head *list = v;
771 struct smack_known *skp =
772 list_entry(list, struct smack_known, list);
773 struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
778 * Don't show a label that could not have been set using
779 * /smack/cipso. This is in support of the notion that
780 * anything read from /smack/cipso ought to be writeable
783 * /smack/cipso2 should be used instead.
785 if (strlen(skp->smk_known) >= SMK_LABELLEN)
788 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
790 for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
791 i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
792 seq_printf(s, "%c%d", sep, i);
801 static const struct seq_operations cipso_seq_ops = {
802 .start = cipso_seq_start,
803 .next = cipso_seq_next,
804 .show = cipso_seq_show,
805 .stop = smk_seq_stop,
809 * smk_open_cipso - open() for /smack/cipso
810 * @inode: inode structure representing file
811 * @file: "cipso" file pointer
813 * Connect our cipso_seq_* operations with /smack/cipso
816 static int smk_open_cipso(struct inode *inode, struct file *file)
818 return seq_open(file, &cipso_seq_ops);
822 * smk_set_cipso - do the work for write() for cipso and cipso2
823 * @file: file pointer, not actually used
824 * @buf: where to get the data from
826 * @ppos: where to start
827 * @format: /smack/cipso or /smack/cipso2
829 * Accepts only one cipso rule per write call.
830 * Returns number of bytes written or error code, as appropriate
832 static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
833 size_t count, loff_t *ppos, int format)
835 struct smack_known *skp;
836 struct netlbl_lsm_secattr ncats;
837 char mapcatset[SMK_CIPSOLEN];
841 ssize_t rc = -EINVAL;
848 * Must have privilege.
850 * Enough data must be present.
852 if (!smack_privileged(CAP_MAC_ADMIN))
856 if (format == SMK_FIXED24_FMT &&
857 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
860 data = kzalloc(count + 1, GFP_KERNEL);
864 if (copy_from_user(data, buf, count) != 0) {
872 * Only allow one writer at a time. Writes should be
873 * quite rare and small in any case.
875 mutex_lock(&smack_cipso_lock);
877 skp = smk_import_entry(rule, 0);
881 if (format == SMK_FIXED24_FMT)
882 rule += SMK_LABELLEN;
884 rule += strlen(skp->smk_known);
886 ret = sscanf(rule, "%d", &maplevel);
887 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
890 rule += SMK_DIGITLEN;
891 ret = sscanf(rule, "%d", &catlen);
892 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
895 if (format == SMK_FIXED24_FMT &&
896 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
899 memset(mapcatset, 0, sizeof(mapcatset));
901 for (i = 0; i < catlen; i++) {
902 rule += SMK_DIGITLEN;
903 ret = sscanf(rule, "%u", &cat);
904 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
907 smack_catset_bit(cat, mapcatset);
910 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
912 netlbl_secattr_catmap_free(skp->smk_netlabel.attr.mls.cat);
913 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
914 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
919 mutex_unlock(&smack_cipso_lock);
926 * smk_write_cipso - write() for /smack/cipso
927 * @file: file pointer, not actually used
928 * @buf: where to get the data from
930 * @ppos: where to start
932 * Accepts only one cipso rule per write call.
933 * Returns number of bytes written or error code, as appropriate
935 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
936 size_t count, loff_t *ppos)
938 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
941 static const struct file_operations smk_cipso_ops = {
942 .open = smk_open_cipso,
945 .write = smk_write_cipso,
946 .release = seq_release,
950 * Seq_file read operations for /smack/cipso2
954 * Print cipso labels in format:
955 * label level[/cat[,cat]]
957 static int cipso2_seq_show(struct seq_file *s, void *v)
959 struct list_head *list = v;
960 struct smack_known *skp =
961 list_entry(list, struct smack_known, list);
962 struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
966 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
968 for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
969 i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
970 seq_printf(s, "%c%d", sep, i);
979 static const struct seq_operations cipso2_seq_ops = {
980 .start = cipso_seq_start,
981 .next = cipso_seq_next,
982 .show = cipso2_seq_show,
983 .stop = smk_seq_stop,
987 * smk_open_cipso2 - open() for /smack/cipso2
988 * @inode: inode structure representing file
989 * @file: "cipso2" file pointer
991 * Connect our cipso_seq_* operations with /smack/cipso2
994 static int smk_open_cipso2(struct inode *inode, struct file *file)
996 return seq_open(file, &cipso2_seq_ops);
1000 * smk_write_cipso2 - write() for /smack/cipso2
1001 * @file: file pointer, not actually used
1002 * @buf: where to get the data from
1003 * @count: bytes sent
1004 * @ppos: where to start
1006 * Accepts only one cipso rule per write call.
1007 * Returns number of bytes written or error code, as appropriate
1009 static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1010 size_t count, loff_t *ppos)
1012 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1015 static const struct file_operations smk_cipso2_ops = {
1016 .open = smk_open_cipso2,
1018 .llseek = seq_lseek,
1019 .write = smk_write_cipso2,
1020 .release = seq_release,
1024 * Seq_file read operations for /smack/netlabel
1027 static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
1029 return smk_seq_start(s, pos, &smk_netlbladdr_list);
1032 static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1034 return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
1036 #define BEBITS (sizeof(__be32) * 8)
1039 * Print host/label pairs
1041 static int netlbladdr_seq_show(struct seq_file *s, void *v)
1043 struct list_head *list = v;
1044 struct smk_netlbladdr *skp =
1045 list_entry(list, struct smk_netlbladdr, list);
1046 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
1048 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
1050 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
1052 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
1053 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
1058 static const struct seq_operations netlbladdr_seq_ops = {
1059 .start = netlbladdr_seq_start,
1060 .next = netlbladdr_seq_next,
1061 .show = netlbladdr_seq_show,
1062 .stop = smk_seq_stop,
1066 * smk_open_netlbladdr - open() for /smack/netlabel
1067 * @inode: inode structure representing file
1068 * @file: "netlabel" file pointer
1070 * Connect our netlbladdr_seq_* operations with /smack/netlabel
1073 static int smk_open_netlbladdr(struct inode *inode, struct file *file)
1075 return seq_open(file, &netlbladdr_seq_ops);
1079 * smk_netlbladdr_insert
1080 * @new : netlabel to insert
1082 * This helper insert netlabel in the smack_netlbladdrs list
1083 * sorted by netmask length (longest to smallest)
1084 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
1087 static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
1089 struct smk_netlbladdr *m, *m_next;
1091 if (list_empty(&smk_netlbladdr_list)) {
1092 list_add_rcu(&new->list, &smk_netlbladdr_list);
1096 m = list_entry_rcu(smk_netlbladdr_list.next,
1097 struct smk_netlbladdr, list);
1099 /* the comparison '>' is a bit hacky, but works */
1100 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
1101 list_add_rcu(&new->list, &smk_netlbladdr_list);
1105 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
1106 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
1107 list_add_rcu(&new->list, &m->list);
1110 m_next = list_entry_rcu(m->list.next,
1111 struct smk_netlbladdr, list);
1112 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
1113 list_add_rcu(&new->list, &m->list);
1121 * smk_write_netlbladdr - write() for /smack/netlabel
1122 * @file: file pointer, not actually used
1123 * @buf: where to get the data from
1124 * @count: bytes sent
1125 * @ppos: where to start
1127 * Accepts only one netlbladdr per write call.
1128 * Returns number of bytes written or error code, as appropriate
1130 static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
1131 size_t count, loff_t *ppos)
1133 struct smk_netlbladdr *skp;
1134 struct sockaddr_in newname;
1138 char *host = (char *)&newname.sin_addr.s_addr;
1140 struct netlbl_audit audit_info;
1141 struct in_addr mask;
1144 u32 mask_bits = (1<<31);
1149 * Must have privilege.
1150 * No partial writes.
1151 * Enough data must be present.
1152 * "<addr/mask, as a.b.c.d/e><space><label>"
1153 * "<addr, as a.b.c.d><space><label>"
1155 if (!smack_privileged(CAP_MAC_ADMIN))
1159 if (count < SMK_NETLBLADDRMIN)
1162 data = kzalloc(count + 1, GFP_KERNEL);
1166 if (copy_from_user(data, buf, count) != 0) {
1171 smack = kzalloc(count + 1, GFP_KERNEL);
1172 if (smack == NULL) {
1179 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
1180 &host[0], &host[1], &host[2], &host[3], &m, smack);
1182 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1183 &host[0], &host[1], &host[2], &host[3], smack);
1196 * If smack begins with '-', it is an option, don't import it
1198 if (smack[0] != '-') {
1199 sp = smk_import(smack, 0);
1205 /* check known options */
1206 if (strcmp(smack, smack_cipso_option) == 0)
1207 sp = (char *)smack_cipso_option;
1214 for (temp_mask = 0; m > 0; m--) {
1215 temp_mask |= mask_bits;
1218 mask.s_addr = cpu_to_be32(temp_mask);
1220 newname.sin_addr.s_addr &= mask.s_addr;
1222 * Only allow one writer at a time. Writes should be
1223 * quite rare and small in any case.
1225 mutex_lock(&smk_netlbladdr_lock);
1227 nsa = newname.sin_addr.s_addr;
1228 /* try to find if the prefix is already in the list */
1230 list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
1231 if (skp->smk_host.sin_addr.s_addr == nsa &&
1232 skp->smk_mask.s_addr == mask.s_addr) {
1237 smk_netlabel_audit_set(&audit_info);
1240 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
1245 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
1246 skp->smk_mask.s_addr = mask.s_addr;
1247 skp->smk_label = sp;
1248 smk_netlbladdr_insert(skp);
1251 /* we delete the unlabeled entry, only if the previous label
1252 * wasn't the special CIPSO option */
1253 if (skp->smk_label != smack_cipso_option)
1254 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1255 &skp->smk_host.sin_addr, &skp->smk_mask,
1256 PF_INET, &audit_info);
1259 skp->smk_label = sp;
1263 * Now tell netlabel about the single label nature of
1264 * this host so that incoming packets get labeled.
1265 * but only if we didn't get the special CIPSO option
1267 if (rc == 0 && sp != smack_cipso_option)
1268 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1269 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
1270 smack_to_secid(skp->smk_label), &audit_info);
1275 mutex_unlock(&smk_netlbladdr_lock);
1285 static const struct file_operations smk_netlbladdr_ops = {
1286 .open = smk_open_netlbladdr,
1288 .llseek = seq_lseek,
1289 .write = smk_write_netlbladdr,
1290 .release = seq_release,
1294 * smk_read_doi - read() for /smack/doi
1295 * @filp: file pointer, not actually used
1296 * @buf: where to put the result
1297 * @count: maximum to send along
1298 * @ppos: where to start
1300 * Returns number of bytes read or error code, as appropriate
1302 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1303 size_t count, loff_t *ppos)
1311 sprintf(temp, "%d", smk_cipso_doi_value);
1312 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1318 * smk_write_doi - write() for /smack/doi
1319 * @file: file pointer, not actually used
1320 * @buf: where to get the data from
1321 * @count: bytes sent
1322 * @ppos: where to start
1324 * Returns number of bytes written or error code, as appropriate
1326 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1327 size_t count, loff_t *ppos)
1332 if (!smack_privileged(CAP_MAC_ADMIN))
1335 if (count >= sizeof(temp) || count == 0)
1338 if (copy_from_user(temp, buf, count) != 0)
1343 if (sscanf(temp, "%d", &i) != 1)
1346 smk_cipso_doi_value = i;
1353 static const struct file_operations smk_doi_ops = {
1354 .read = smk_read_doi,
1355 .write = smk_write_doi,
1356 .llseek = default_llseek,
1360 * smk_read_direct - read() for /smack/direct
1361 * @filp: file pointer, not actually used
1362 * @buf: where to put the result
1363 * @count: maximum to send along
1364 * @ppos: where to start
1366 * Returns number of bytes read or error code, as appropriate
1368 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1369 size_t count, loff_t *ppos)
1377 sprintf(temp, "%d", smack_cipso_direct);
1378 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1384 * smk_write_direct - write() for /smack/direct
1385 * @file: file pointer, not actually used
1386 * @buf: where to get the data from
1387 * @count: bytes sent
1388 * @ppos: where to start
1390 * Returns number of bytes written or error code, as appropriate
1392 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1393 size_t count, loff_t *ppos)
1395 struct smack_known *skp;
1399 if (!smack_privileged(CAP_MAC_ADMIN))
1402 if (count >= sizeof(temp) || count == 0)
1405 if (copy_from_user(temp, buf, count) != 0)
1410 if (sscanf(temp, "%d", &i) != 1)
1414 * Don't do anything if the value hasn't actually changed.
1415 * If it is changing reset the level on entries that were
1416 * set up to be direct when they were created.
1418 if (smack_cipso_direct != i) {
1419 mutex_lock(&smack_known_lock);
1420 list_for_each_entry_rcu(skp, &smack_known_list, list)
1421 if (skp->smk_netlabel.attr.mls.lvl ==
1423 skp->smk_netlabel.attr.mls.lvl = i;
1424 smack_cipso_direct = i;
1425 mutex_unlock(&smack_known_lock);
1431 static const struct file_operations smk_direct_ops = {
1432 .read = smk_read_direct,
1433 .write = smk_write_direct,
1434 .llseek = default_llseek,
1438 * smk_read_mapped - read() for /smack/mapped
1439 * @filp: file pointer, not actually used
1440 * @buf: where to put the result
1441 * @count: maximum to send along
1442 * @ppos: where to start
1444 * Returns number of bytes read or error code, as appropriate
1446 static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1447 size_t count, loff_t *ppos)
1455 sprintf(temp, "%d", smack_cipso_mapped);
1456 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1462 * smk_write_mapped - write() for /smack/mapped
1463 * @file: file pointer, not actually used
1464 * @buf: where to get the data from
1465 * @count: bytes sent
1466 * @ppos: where to start
1468 * Returns number of bytes written or error code, as appropriate
1470 static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1471 size_t count, loff_t *ppos)
1473 struct smack_known *skp;
1477 if (!smack_privileged(CAP_MAC_ADMIN))
1480 if (count >= sizeof(temp) || count == 0)
1483 if (copy_from_user(temp, buf, count) != 0)
1488 if (sscanf(temp, "%d", &i) != 1)
1492 * Don't do anything if the value hasn't actually changed.
1493 * If it is changing reset the level on entries that were
1494 * set up to be mapped when they were created.
1496 if (smack_cipso_mapped != i) {
1497 mutex_lock(&smack_known_lock);
1498 list_for_each_entry_rcu(skp, &smack_known_list, list)
1499 if (skp->smk_netlabel.attr.mls.lvl ==
1501 skp->smk_netlabel.attr.mls.lvl = i;
1502 smack_cipso_mapped = i;
1503 mutex_unlock(&smack_known_lock);
1509 static const struct file_operations smk_mapped_ops = {
1510 .read = smk_read_mapped,
1511 .write = smk_write_mapped,
1512 .llseek = default_llseek,
1516 * smk_read_ambient - read() for /smack/ambient
1517 * @filp: file pointer, not actually used
1518 * @buf: where to put the result
1519 * @cn: maximum to send along
1520 * @ppos: where to start
1522 * Returns number of bytes read or error code, as appropriate
1524 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1525 size_t cn, loff_t *ppos)
1533 * Being careful to avoid a problem in the case where
1534 * smack_net_ambient gets changed in midstream.
1536 mutex_lock(&smack_ambient_lock);
1538 asize = strlen(smack_net_ambient) + 1;
1541 rc = simple_read_from_buffer(buf, cn, ppos,
1542 smack_net_ambient, asize);
1546 mutex_unlock(&smack_ambient_lock);
1552 * smk_write_ambient - write() for /smack/ambient
1553 * @file: file pointer, not actually used
1554 * @buf: where to get the data from
1555 * @count: bytes sent
1556 * @ppos: where to start
1558 * Returns number of bytes written or error code, as appropriate
1560 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1561 size_t count, loff_t *ppos)
1568 if (!smack_privileged(CAP_MAC_ADMIN))
1571 data = kzalloc(count + 1, GFP_KERNEL);
1575 if (copy_from_user(data, buf, count) != 0) {
1580 smack = smk_import(data, count);
1581 if (smack == NULL) {
1586 mutex_lock(&smack_ambient_lock);
1588 oldambient = smack_net_ambient;
1589 smack_net_ambient = smack;
1590 smk_unlbl_ambient(oldambient);
1592 mutex_unlock(&smack_ambient_lock);
1599 static const struct file_operations smk_ambient_ops = {
1600 .read = smk_read_ambient,
1601 .write = smk_write_ambient,
1602 .llseek = default_llseek,
1606 * smk_read_onlycap - read() for /smack/onlycap
1607 * @filp: file pointer, not actually used
1608 * @buf: where to put the result
1609 * @cn: maximum to send along
1610 * @ppos: where to start
1612 * Returns number of bytes read or error code, as appropriate
1614 static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1615 size_t cn, loff_t *ppos)
1618 ssize_t rc = -EINVAL;
1624 if (smack_onlycap != NULL)
1625 smack = smack_onlycap;
1627 asize = strlen(smack) + 1;
1630 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1636 * smk_write_onlycap - write() for /smack/onlycap
1637 * @file: file pointer, not actually used
1638 * @buf: where to get the data from
1639 * @count: bytes sent
1640 * @ppos: where to start
1642 * Returns number of bytes written or error code, as appropriate
1644 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1645 size_t count, loff_t *ppos)
1648 char *sp = smk_of_task(current->cred->security);
1651 if (!smack_privileged(CAP_MAC_ADMIN))
1655 * This can be done using smk_access() but is done
1656 * explicitly for clarity. The smk_access() implementation
1657 * would use smk_access(smack_onlycap, MAY_WRITE)
1659 if (smack_onlycap != NULL && smack_onlycap != sp)
1662 data = kzalloc(count, GFP_KERNEL);
1667 * Should the null string be passed in unset the onlycap value.
1668 * This seems like something to be careful with as usually
1669 * smk_import only expects to return NULL for errors. It
1670 * is usually the case that a nullstring or "\n" would be
1671 * bad to pass to smk_import but in fact this is useful here.
1673 * smk_import will also reject a label beginning with '-',
1674 * so "-usecapabilities" will also work.
1676 if (copy_from_user(data, buf, count) != 0)
1679 smack_onlycap = smk_import(data, count);
1685 static const struct file_operations smk_onlycap_ops = {
1686 .read = smk_read_onlycap,
1687 .write = smk_write_onlycap,
1688 .llseek = default_llseek,
1692 * smk_read_logging - read() for /smack/logging
1693 * @filp: file pointer, not actually used
1694 * @buf: where to put the result
1695 * @cn: maximum to send along
1696 * @ppos: where to start
1698 * Returns number of bytes read or error code, as appropriate
1700 static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1701 size_t count, loff_t *ppos)
1709 sprintf(temp, "%d\n", log_policy);
1710 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1715 * smk_write_logging - write() for /smack/logging
1716 * @file: file pointer, not actually used
1717 * @buf: where to get the data from
1718 * @count: bytes sent
1719 * @ppos: where to start
1721 * Returns number of bytes written or error code, as appropriate
1723 static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1724 size_t count, loff_t *ppos)
1729 if (!smack_privileged(CAP_MAC_ADMIN))
1732 if (count >= sizeof(temp) || count == 0)
1735 if (copy_from_user(temp, buf, count) != 0)
1740 if (sscanf(temp, "%d", &i) != 1)
1750 static const struct file_operations smk_logging_ops = {
1751 .read = smk_read_logging,
1752 .write = smk_write_logging,
1753 .llseek = default_llseek,
1757 * Seq_file read operations for /smack/load-self
1760 static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
1762 struct task_smack *tsp = current_security();
1764 return smk_seq_start(s, pos, &tsp->smk_rules);
1767 static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
1769 struct task_smack *tsp = current_security();
1771 return smk_seq_next(s, v, pos, &tsp->smk_rules);
1774 static int load_self_seq_show(struct seq_file *s, void *v)
1776 struct list_head *list = v;
1777 struct smack_rule *srp =
1778 list_entry(list, struct smack_rule, list);
1780 smk_rule_show(s, srp, SMK_LABELLEN);
1785 static const struct seq_operations load_self_seq_ops = {
1786 .start = load_self_seq_start,
1787 .next = load_self_seq_next,
1788 .show = load_self_seq_show,
1789 .stop = smk_seq_stop,
1794 * smk_open_load_self - open() for /smack/load-self2
1795 * @inode: inode structure representing file
1796 * @file: "load" file pointer
1798 * For reading, use load_seq_* seq_file reading operations.
1800 static int smk_open_load_self(struct inode *inode, struct file *file)
1802 return seq_open(file, &load_self_seq_ops);
1806 * smk_write_load_self - write() for /smack/load-self
1807 * @file: file pointer, not actually used
1808 * @buf: where to get the data from
1809 * @count: bytes sent
1810 * @ppos: where to start - must be 0
1813 static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
1814 size_t count, loff_t *ppos)
1816 struct task_smack *tsp = current_security();
1818 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
1819 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
1822 static const struct file_operations smk_load_self_ops = {
1823 .open = smk_open_load_self,
1825 .llseek = seq_lseek,
1826 .write = smk_write_load_self,
1827 .release = seq_release,
1831 * smk_user_access - handle access check transaction
1832 * @file: file pointer
1833 * @buf: data from user space
1834 * @count: bytes sent
1835 * @ppos: where to start - must be 0
1837 static ssize_t smk_user_access(struct file *file, const char __user *buf,
1838 size_t count, loff_t *ppos, int format)
1840 struct smack_parsed_rule rule;
1845 data = simple_transaction_get(file, buf, count);
1847 return PTR_ERR(data);
1849 if (format == SMK_FIXED24_FMT) {
1850 if (count < SMK_LOADLEN)
1852 res = smk_parse_rule(data, &rule, 0);
1855 * Copy the data to make sure the string is terminated.
1857 cod = kzalloc(count + 1, GFP_KERNEL);
1860 memcpy(cod, data, count);
1862 res = smk_parse_long_rule(cod, &rule, 0, 0);
1869 res = smk_access(rule.smk_subject, rule.smk_object, rule.smk_access1,
1871 data[0] = res == 0 ? '1' : '0';
1874 simple_transaction_set(file, 2);
1876 if (format == SMK_FIXED24_FMT)
1882 * smk_write_access - handle access check transaction
1883 * @file: file pointer
1884 * @buf: data from user space
1885 * @count: bytes sent
1886 * @ppos: where to start - must be 0
1888 static ssize_t smk_write_access(struct file *file, const char __user *buf,
1889 size_t count, loff_t *ppos)
1891 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
1894 static const struct file_operations smk_access_ops = {
1895 .write = smk_write_access,
1896 .read = simple_transaction_read,
1897 .release = simple_transaction_release,
1898 .llseek = generic_file_llseek,
1903 * Seq_file read operations for /smack/load2
1906 static int load2_seq_show(struct seq_file *s, void *v)
1908 struct list_head *list = v;
1909 struct smack_master_list *smlp =
1910 list_entry(list, struct smack_master_list, list);
1912 smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
1917 static const struct seq_operations load2_seq_ops = {
1918 .start = load2_seq_start,
1919 .next = load2_seq_next,
1920 .show = load2_seq_show,
1921 .stop = smk_seq_stop,
1925 * smk_open_load2 - open() for /smack/load2
1926 * @inode: inode structure representing file
1927 * @file: "load2" file pointer
1929 * For reading, use load2_seq_* seq_file reading operations.
1931 static int smk_open_load2(struct inode *inode, struct file *file)
1933 return seq_open(file, &load2_seq_ops);
1937 * smk_write_load2 - write() for /smack/load2
1938 * @file: file pointer, not actually used
1939 * @buf: where to get the data from
1940 * @count: bytes sent
1941 * @ppos: where to start - must be 0
1944 static ssize_t smk_write_load2(struct file *file, const char __user *buf,
1945 size_t count, loff_t *ppos)
1948 * Must have privilege.
1950 if (!smack_privileged(CAP_MAC_ADMIN))
1953 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
1957 static const struct file_operations smk_load2_ops = {
1958 .open = smk_open_load2,
1960 .llseek = seq_lseek,
1961 .write = smk_write_load2,
1962 .release = seq_release,
1966 * Seq_file read operations for /smack/load-self2
1969 static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
1971 struct task_smack *tsp = current_security();
1973 return smk_seq_start(s, pos, &tsp->smk_rules);
1976 static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
1978 struct task_smack *tsp = current_security();
1980 return smk_seq_next(s, v, pos, &tsp->smk_rules);
1983 static int load_self2_seq_show(struct seq_file *s, void *v)
1985 struct list_head *list = v;
1986 struct smack_rule *srp =
1987 list_entry(list, struct smack_rule, list);
1989 smk_rule_show(s, srp, SMK_LONGLABEL);
1994 static const struct seq_operations load_self2_seq_ops = {
1995 .start = load_self2_seq_start,
1996 .next = load_self2_seq_next,
1997 .show = load_self2_seq_show,
1998 .stop = smk_seq_stop,
2002 * smk_open_load_self2 - open() for /smack/load-self2
2003 * @inode: inode structure representing file
2004 * @file: "load" file pointer
2006 * For reading, use load_seq_* seq_file reading operations.
2008 static int smk_open_load_self2(struct inode *inode, struct file *file)
2010 return seq_open(file, &load_self2_seq_ops);
2014 * smk_write_load_self2 - write() for /smack/load-self2
2015 * @file: file pointer, not actually used
2016 * @buf: where to get the data from
2017 * @count: bytes sent
2018 * @ppos: where to start - must be 0
2021 static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2022 size_t count, loff_t *ppos)
2024 struct task_smack *tsp = current_security();
2026 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2027 &tsp->smk_rules_lock, SMK_LONG_FMT);
2030 static const struct file_operations smk_load_self2_ops = {
2031 .open = smk_open_load_self2,
2033 .llseek = seq_lseek,
2034 .write = smk_write_load_self2,
2035 .release = seq_release,
2039 * smk_write_access2 - handle access check transaction
2040 * @file: file pointer
2041 * @buf: data from user space
2042 * @count: bytes sent
2043 * @ppos: where to start - must be 0
2045 static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2046 size_t count, loff_t *ppos)
2048 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2051 static const struct file_operations smk_access2_ops = {
2052 .write = smk_write_access2,
2053 .read = simple_transaction_read,
2054 .release = simple_transaction_release,
2055 .llseek = generic_file_llseek,
2059 * smk_write_revoke_subj - write() for /smack/revoke-subject
2060 * @file: file pointer
2061 * @buf: data from user space
2062 * @count: bytes sent
2063 * @ppos: where to start - must be 0
2065 static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2066 size_t count, loff_t *ppos)
2069 const char *cp = NULL;
2070 struct smack_known *skp;
2071 struct smack_rule *sp;
2072 struct list_head *rule_list;
2073 struct mutex *rule_lock;
2079 if (!smack_privileged(CAP_MAC_ADMIN))
2082 if (count == 0 || count > SMK_LONGLABEL)
2085 data = kzalloc(count, GFP_KERNEL);
2089 if (copy_from_user(data, buf, count) != 0) {
2094 cp = smk_parse_smack(data, count);
2100 skp = smk_find_entry(cp);
2104 rule_list = &skp->smk_rules;
2105 rule_lock = &skp->smk_rules_lock;
2107 mutex_lock(rule_lock);
2109 list_for_each_entry_rcu(sp, rule_list, list)
2112 mutex_unlock(rule_lock);
2120 static const struct file_operations smk_revoke_subj_ops = {
2121 .write = smk_write_revoke_subj,
2122 .read = simple_transaction_read,
2123 .release = simple_transaction_release,
2124 .llseek = generic_file_llseek,
2127 static struct kset *smackfs_kset;
2129 * smk_init_sysfs - initialize /sys/fs/smackfs
2132 static int smk_init_sysfs(void)
2134 smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj);
2141 * smk_write_change_rule - write() for /smack/change-rule
2142 * @file: file pointer
2143 * @buf: data from user space
2144 * @count: bytes sent
2145 * @ppos: where to start - must be 0
2147 static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2148 size_t count, loff_t *ppos)
2151 * Must have privilege.
2153 if (!capable(CAP_MAC_ADMIN))
2156 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2160 static const struct file_operations smk_change_rule_ops = {
2161 .write = smk_write_change_rule,
2162 .read = simple_transaction_read,
2163 .release = simple_transaction_release,
2164 .llseek = generic_file_llseek,
2168 * smk_fill_super - fill the /smackfs superblock
2169 * @sb: the empty superblock
2173 * Fill in the well known entries for /smack
2175 * Returns 0 on success, an error code on failure
2177 static int smk_fill_super(struct super_block *sb, void *data, int silent)
2180 struct inode *root_inode;
2182 static struct tree_descr smack_files[] = {
2184 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2186 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2188 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2190 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2192 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2193 [SMK_NETLBLADDR] = {
2194 "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
2196 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2198 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2200 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
2202 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
2204 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2206 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2207 [SMK_LOAD_SELF2] = {
2208 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2210 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2212 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
2213 [SMK_REVOKE_SUBJ] = {
2214 "revoke-subject", &smk_revoke_subj_ops,
2216 [SMK_CHANGE_RULE] = {
2217 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
2222 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2224 printk(KERN_ERR "%s failed %d while creating inodes\n",
2229 root_inode = sb->s_root->d_inode;
2235 * smk_mount - get the smackfs superblock
2236 * @fs_type: passed along without comment
2237 * @flags: passed along without comment
2238 * @dev_name: passed along without comment
2239 * @data: passed along without comment
2241 * Just passes everything along.
2243 * Returns what the lower level code does.
2245 static struct dentry *smk_mount(struct file_system_type *fs_type,
2246 int flags, const char *dev_name, void *data)
2248 return mount_single(fs_type, flags, data, smk_fill_super);
2251 static struct file_system_type smk_fs_type = {
2254 .kill_sb = kill_litter_super,
2257 static struct vfsmount *smackfs_mount;
2259 static int __init smk_preset_netlabel(struct smack_known *skp)
2261 skp->smk_netlabel.domain = skp->smk_known;
2262 skp->smk_netlabel.flags =
2263 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2264 return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2265 &skp->smk_netlabel, strlen(skp->smk_known));
2269 * init_smk_fs - get the smackfs superblock
2271 * register the smackfs
2273 * Do not register smackfs if Smack wasn't enabled
2274 * on boot. We can not put this method normally under the
2275 * smack_init() code path since the security subsystem get
2276 * initialized before the vfs caches.
2278 * Returns true if we were not chosen on boot or if
2279 * we were chosen and filesystem registration succeeded.
2281 static int __init init_smk_fs(void)
2286 if (!security_module_enable(&smack_ops))
2289 err = smk_init_sysfs();
2291 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2293 err = register_filesystem(&smk_fs_type);
2295 smackfs_mount = kern_mount(&smk_fs_type);
2296 if (IS_ERR(smackfs_mount)) {
2297 printk(KERN_ERR "smackfs: could not mount!\n");
2298 err = PTR_ERR(smackfs_mount);
2299 smackfs_mount = NULL;
2304 smk_unlbl_ambient(NULL);
2306 rc = smk_preset_netlabel(&smack_known_floor);
2307 if (err == 0 && rc < 0)
2309 rc = smk_preset_netlabel(&smack_known_hat);
2310 if (err == 0 && rc < 0)
2312 rc = smk_preset_netlabel(&smack_known_huh);
2313 if (err == 0 && rc < 0)
2315 rc = smk_preset_netlabel(&smack_known_invalid);
2316 if (err == 0 && rc < 0)
2318 rc = smk_preset_netlabel(&smack_known_star);
2319 if (err == 0 && rc < 0)
2321 rc = smk_preset_netlabel(&smack_known_web);
2322 if (err == 0 && rc < 0)
2328 __initcall(init_smk_fs);