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) */
55 SMK_SYSLOG = 20, /* change syslog label) */
56 #ifdef CONFIG_SECURITY_SMACK_PERMISSIVE_MODE
57 SMK_PERMISSIVE = 21, /* permissive mode */
64 static DEFINE_MUTEX(smack_cipso_lock);
65 static DEFINE_MUTEX(smack_ambient_lock);
66 static DEFINE_MUTEX(smack_syslog_lock);
67 static DEFINE_MUTEX(smk_netlbladdr_lock);
70 * This is the "ambient" label for network traffic.
71 * If it isn't somehow marked, use this.
72 * It can be reset via smackfs/ambient
74 struct smack_known *smack_net_ambient;
77 * This is the level in a CIPSO header that indicates a
78 * smack label is contained directly in the category set.
79 * It can be reset via smackfs/direct
81 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
84 * This is the level in a CIPSO header that indicates a
85 * secid is contained directly in the category set.
86 * It can be reset via smackfs/mapped
88 int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
91 * Unless a process is running with this label even
92 * having CAP_MAC_OVERRIDE isn't enough to grant
93 * privilege to violate MAC policy. If no label is
94 * designated (the NULL case) capabilities apply to
95 * everyone. It is expected that the hat (^) label
96 * will be used if any label is used.
98 struct smack_known *smack_onlycap;
101 * If this value is set restrict syslog use to the label specified.
102 * It can be reset via smackfs/syslog
104 struct smack_known *smack_syslog_label;
107 * Certain IP addresses may be designated as single label hosts.
108 * Packets are sent there unlabeled, but only from tasks that
109 * can write to the specified label.
112 LIST_HEAD(smk_netlbladdr_list);
115 * Rule lists are maintained for each label.
116 * This master list is just for reading /smack/load and /smack/load2.
118 struct smack_master_list {
119 struct list_head list;
120 struct smack_rule *smk_rule;
123 LIST_HEAD(smack_rule_list);
125 struct smack_parsed_rule {
126 struct smack_known *smk_subject;
132 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
134 const char *smack_cipso_option = SMACK_CIPSO_OPTION;
137 * Values for parsing cipso rules
138 * SMK_DIGITLEN: Length of a digit field in a rule.
139 * SMK_CIPSOMIN: Minimum possible cipso rule length.
140 * SMK_CIPSOMAX: Maximum possible cipso rule length.
142 #define SMK_DIGITLEN 4
143 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
144 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
147 * Values for parsing MAC rules
148 * SMK_ACCESS: Maximum possible combination of access permissions
149 * SMK_ACCESSLEN: Maximum length for a rule access field
150 * SMK_LOADLEN: Smack rule length
152 #define SMK_OACCESS "rwxa"
153 #define SMK_ACCESS "rwxatl"
154 #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
155 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
156 #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
157 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
160 * Stricly for CIPSO level manipulation.
161 * Set the category bit number in a smack label sized buffer.
163 static inline void smack_catset_bit(unsigned int cat, char *catsetp)
165 if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
168 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
172 * smk_netlabel_audit_set - fill a netlbl_audit struct
173 * @nap: structure to fill
175 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
177 struct smack_known *skp = smk_of_current();
179 nap->loginuid = audit_get_loginuid(current);
180 nap->sessionid = audit_get_sessionid(current);
181 nap->secid = skp->smk_secid;
185 * Value for parsing single label host rules
188 #define SMK_NETLBLADDRMIN 9
191 * smk_set_access - add a rule to the rule list or replace an old rule
192 * @srp: the rule to add or replace
193 * @rule_list: the list of rules
194 * @rule_lock: the rule list lock
195 * @global: if non-zero, indicates a global rule
197 * Looks through the current subject/object/access list for
198 * the subject/object pair and replaces the access that was
199 * there. If the pair isn't found add it with the specified
202 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
203 * during the allocation of the new pair to add.
205 static int smk_set_access(struct smack_parsed_rule *srp,
206 struct list_head *rule_list,
207 struct mutex *rule_lock, int global)
209 struct smack_rule *sp;
210 struct smack_master_list *smlp;
214 mutex_lock(rule_lock);
217 * Because the object label is less likely to match
218 * than the subject label check it first
220 list_for_each_entry_rcu(sp, rule_list, list) {
221 if (sp->smk_object == srp->smk_object &&
222 sp->smk_subject == srp->smk_subject) {
224 sp->smk_access |= srp->smk_access1;
225 sp->smk_access &= ~srp->smk_access2;
231 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
237 sp->smk_subject = srp->smk_subject;
238 sp->smk_object = srp->smk_object;
239 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
241 list_add_rcu(&sp->list, rule_list);
243 * If this is a global as opposed to self and a new rule
244 * it needs to get added for reporting.
247 smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
250 list_add_rcu(&smlp->list, &smack_rule_list);
257 mutex_unlock(rule_lock);
262 * smk_perm_from_str - parse smack accesses from a text string
263 * @string: a text string that contains a Smack accesses code
265 * Returns an integer with respective bits set for specified accesses.
267 static int smk_perm_from_str(const char *string)
272 for (cp = string; ; cp++)
294 perm |= MAY_TRANSMUTE;
306 * smk_fill_rule - Fill Smack rule from strings
307 * @subject: subject label string
308 * @object: object label string
309 * @access1: access string
310 * @access2: string with permissions to be removed
312 * @import: if non-zero, import labels
313 * @len: label length limit
315 * Returns 0 on success, -EINVAL on failure and -ENOENT when either subject
316 * or object is missing.
318 static int smk_fill_rule(const char *subject, const char *object,
319 const char *access1, const char *access2,
320 struct smack_parsed_rule *rule, int import,
324 struct smack_known *skp;
327 rule->smk_subject = smk_import_entry(subject, len);
328 if (rule->smk_subject == NULL)
331 rule->smk_object = smk_import(object, len);
332 if (rule->smk_object == NULL)
335 cp = smk_parse_smack(subject, len);
338 skp = smk_find_entry(cp);
342 rule->smk_subject = skp;
344 cp = smk_parse_smack(object, len);
347 skp = smk_find_entry(cp);
351 rule->smk_object = skp->smk_known;
354 rule->smk_access1 = smk_perm_from_str(access1);
356 rule->smk_access2 = smk_perm_from_str(access2);
358 rule->smk_access2 = ~rule->smk_access1;
364 * smk_parse_rule - parse Smack rule from load string
365 * @data: string to be parsed whose size is SMK_LOADLEN
367 * @import: if non-zero, import labels
369 * Returns 0 on success, -1 on errors.
371 static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
376 rc = smk_fill_rule(data, data + SMK_LABELLEN,
377 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
378 import, SMK_LABELLEN);
383 * smk_parse_long_rule - parse Smack rule from rule string
384 * @data: string to be parsed, null terminated
385 * @rule: Will be filled with Smack parsed rule
386 * @import: if non-zero, import labels
387 * @tokens: numer of substrings expected in data
389 * Returns number of processed bytes on success, -1 on failure.
391 static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
392 int import, int tokens)
400 * Parsing the rule in-place, filling all white-spaces with '\0'
402 for (i = 0; i < tokens; ++i) {
403 while (isspace(data[cnt]))
406 if (data[cnt] == '\0')
407 /* Unexpected end of data */
412 while (data[cnt] && !isspace(data[cnt]))
415 while (isspace(data[cnt]))
421 rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
422 return rc == 0 ? cnt : rc;
425 #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
426 #define SMK_LONG_FMT 1 /* Variable long label format */
427 #define SMK_CHANGE_FMT 2 /* Rule modification format */
429 * smk_write_rules_list - write() for any /smack rule file
430 * @file: file pointer, not actually used
431 * @buf: where to get the data from
433 * @ppos: where to start - must be 0
434 * @rule_list: the list of rules to write to
435 * @rule_lock: lock for the rule list
436 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
438 * Get one smack access rule from above.
439 * The format for SMK_LONG_FMT is:
440 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
441 * The format for SMK_FIXED24_FMT is exactly:
442 * "subject object rwxat"
443 * The format for SMK_CHANGE_FMT is:
444 * "subject<whitespace>object<whitespace>
445 * acc_enable<whitespace>acc_disable[<whitespace>...]"
447 static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
448 size_t count, loff_t *ppos,
449 struct list_head *rule_list,
450 struct mutex *rule_lock, int format)
452 struct smack_parsed_rule rule;
461 * Enough data must be present.
466 if (format == SMK_FIXED24_FMT) {
468 * Minor hack for backward compatibility
470 if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
473 if (count >= PAGE_SIZE) {
474 count = PAGE_SIZE - 1;
479 data = kmalloc(count + 1, GFP_KERNEL);
483 if (copy_from_user(data, buf, count) != 0) {
489 * In case of parsing only part of user buf,
490 * avoid having partial rule at the data buffer
493 while (count > 0 && (data[count - 1] != '\n'))
502 tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
503 while (cnt < count) {
504 if (format == SMK_FIXED24_FMT) {
505 rc = smk_parse_rule(data, &rule, 1);
512 rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
520 if (rule_list == NULL)
521 rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
522 &rule.smk_subject->smk_rules_lock, 1);
524 rc = smk_set_access(&rule, rule_list, rule_lock, 0);
537 * Core logic for smackfs seq list operations.
540 static void *smk_seq_start(struct seq_file *s, loff_t *pos,
541 struct list_head *head)
543 struct list_head *list;
546 * This is 0 the first time through.
551 if (s->private == NULL)
555 if (list_empty(list))
563 static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
564 struct list_head *head)
566 struct list_head *list = v;
568 if (list_is_last(list, head)) {
572 s->private = list->next;
576 static void smk_seq_stop(struct seq_file *s, void *v)
581 static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
584 * Don't show any rules with label names too long for
585 * interface file (/smack/load or /smack/load2)
586 * because you should expect to be able to write
587 * anything you read back.
589 if (strlen(srp->smk_subject->smk_known) >= max ||
590 strlen(srp->smk_object) >= max)
593 if (srp->smk_access == 0)
596 seq_printf(s, "%s %s", srp->smk_subject->smk_known, srp->smk_object);
600 if (srp->smk_access & MAY_READ)
602 if (srp->smk_access & MAY_WRITE)
604 if (srp->smk_access & MAY_EXEC)
606 if (srp->smk_access & MAY_APPEND)
608 if (srp->smk_access & MAY_TRANSMUTE)
610 if (srp->smk_access & MAY_LOCK)
617 * Seq_file read operations for /smack/load
620 static void *load2_seq_start(struct seq_file *s, loff_t *pos)
622 return smk_seq_start(s, pos, &smack_rule_list);
625 static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
627 return smk_seq_next(s, v, pos, &smack_rule_list);
630 static int load_seq_show(struct seq_file *s, void *v)
632 struct list_head *list = v;
633 struct smack_master_list *smlp =
634 list_entry(list, struct smack_master_list, list);
636 smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
641 static const struct seq_operations load_seq_ops = {
642 .start = load2_seq_start,
643 .next = load2_seq_next,
644 .show = load_seq_show,
645 .stop = smk_seq_stop,
649 * smk_open_load - open() for /smack/load
650 * @inode: inode structure representing file
651 * @file: "load" file pointer
653 * For reading, use load_seq_* seq_file reading operations.
655 static int smk_open_load(struct inode *inode, struct file *file)
657 return seq_open(file, &load_seq_ops);
661 * smk_write_load - write() for /smack/load
662 * @file: file pointer, not actually used
663 * @buf: where to get the data from
665 * @ppos: where to start - must be 0
668 static ssize_t smk_write_load(struct file *file, const char __user *buf,
669 size_t count, loff_t *ppos)
672 * Must have privilege.
674 * Enough data must be present.
676 if (!smack_privileged(CAP_MAC_ADMIN))
679 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
683 static const struct file_operations smk_load_ops = {
684 .open = smk_open_load,
687 .write = smk_write_load,
688 .release = seq_release,
691 #ifdef CONFIG_SECURITY_SMACK_PERMISSIVE_MODE
693 * smk_read_permissive - read() for /smack/permissive
694 * @filp: file pointer, not actually used
695 * @buf: where to put the result
696 * @cn: maximum to send along
697 * @ppos: where to start
699 * Returns number of bytes read or error code, as appropriate
701 static ssize_t smk_read_permissive(struct file *filp, char __user *buf,
702 size_t count, loff_t *ppos)
710 sprintf(temp, "%d\n", permissive_mode);
711 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
716 * smk_write_permissive - write() for /smack/permissive
717 * @file: file pointer, not actually used
718 * @buf: where to get the data from
720 * @ppos: where to start
722 * Returns number of bytes written or error code, as appropriate
724 static ssize_t smk_write_permissive(struct file *file, const char __user *buf,
725 size_t count, loff_t *ppos)
730 if (!capable(CAP_MAC_ADMIN))
733 if (count >= sizeof(temp) || count == 0)
736 if (copy_from_user(temp, buf, count) != 0)
741 if (sscanf(temp, "%d", &i) != 1)
749 static const struct file_operations smk_permissive_ops = {
750 .read = smk_read_permissive,
751 .write = smk_write_permissive,
752 .llseek = default_llseek,
754 #endif /* End of CONFIG_SECURITY_SMACK_PERMISSIVE_MODE */
757 * smk_cipso_doi - initialize the CIPSO domain
759 static void smk_cipso_doi(void)
762 struct cipso_v4_doi *doip;
763 struct netlbl_audit nai;
765 smk_netlabel_audit_set(&nai);
767 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
769 printk(KERN_WARNING "%s:%d remove rc = %d\n",
770 __func__, __LINE__, rc);
772 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
774 panic("smack: Failed to initialize cipso DOI.\n");
775 doip->map.std = NULL;
776 doip->doi = smk_cipso_doi_value;
777 doip->type = CIPSO_V4_MAP_PASS;
778 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
779 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
780 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
782 rc = netlbl_cfg_cipsov4_add(doip, &nai);
784 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
785 __func__, __LINE__, rc);
789 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
791 printk(KERN_WARNING "%s:%d map add rc = %d\n",
792 __func__, __LINE__, rc);
799 * smk_unlbl_ambient - initialize the unlabeled domain
800 * @oldambient: previous domain string
802 static void smk_unlbl_ambient(char *oldambient)
805 struct netlbl_audit nai;
807 smk_netlabel_audit_set(&nai);
809 if (oldambient != NULL) {
810 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
812 printk(KERN_WARNING "%s:%d remove rc = %d\n",
813 __func__, __LINE__, rc);
815 if (smack_net_ambient == NULL)
816 smack_net_ambient = &smack_known_floor;
818 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
821 printk(KERN_WARNING "%s:%d add rc = %d\n",
822 __func__, __LINE__, rc);
826 * Seq_file read operations for /smack/cipso
829 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
831 return smk_seq_start(s, pos, &smack_known_list);
834 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
836 return smk_seq_next(s, v, pos, &smack_known_list);
840 * Print cipso labels in format:
841 * label level[/cat[,cat]]
843 static int cipso_seq_show(struct seq_file *s, void *v)
845 struct list_head *list = v;
846 struct smack_known *skp =
847 list_entry(list, struct smack_known, list);
848 struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
853 * Don't show a label that could not have been set using
854 * /smack/cipso. This is in support of the notion that
855 * anything read from /smack/cipso ought to be writeable
858 * /smack/cipso2 should be used instead.
860 if (strlen(skp->smk_known) >= SMK_LABELLEN)
863 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
865 for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
866 i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
867 seq_printf(s, "%c%d", sep, i);
876 static const struct seq_operations cipso_seq_ops = {
877 .start = cipso_seq_start,
878 .next = cipso_seq_next,
879 .show = cipso_seq_show,
880 .stop = smk_seq_stop,
884 * smk_open_cipso - open() for /smack/cipso
885 * @inode: inode structure representing file
886 * @file: "cipso" file pointer
888 * Connect our cipso_seq_* operations with /smack/cipso
891 static int smk_open_cipso(struct inode *inode, struct file *file)
893 return seq_open(file, &cipso_seq_ops);
897 * smk_set_cipso - do the work for write() for cipso and cipso2
898 * @file: file pointer, not actually used
899 * @buf: where to get the data from
901 * @ppos: where to start
902 * @format: /smack/cipso or /smack/cipso2
904 * Accepts only one cipso rule per write call.
905 * Returns number of bytes written or error code, as appropriate
907 static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
908 size_t count, loff_t *ppos, int format)
910 struct smack_known *skp;
911 struct netlbl_lsm_secattr ncats;
912 char mapcatset[SMK_CIPSOLEN];
916 ssize_t rc = -EINVAL;
923 * Must have privilege.
925 * Enough data must be present.
927 if (!smack_privileged(CAP_MAC_ADMIN))
931 if (format == SMK_FIXED24_FMT &&
932 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
935 data = kzalloc(count + 1, GFP_KERNEL);
939 if (copy_from_user(data, buf, count) != 0) {
947 * Only allow one writer at a time. Writes should be
948 * quite rare and small in any case.
950 mutex_lock(&smack_cipso_lock);
952 skp = smk_import_entry(rule, 0);
956 if (format == SMK_FIXED24_FMT)
957 rule += SMK_LABELLEN;
959 rule += strlen(skp->smk_known) + 1;
961 ret = sscanf(rule, "%d", &maplevel);
962 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
965 rule += SMK_DIGITLEN;
966 ret = sscanf(rule, "%d", &catlen);
967 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
970 if (format == SMK_FIXED24_FMT &&
971 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
974 memset(mapcatset, 0, sizeof(mapcatset));
976 for (i = 0; i < catlen; i++) {
977 rule += SMK_DIGITLEN;
978 ret = sscanf(rule, "%u", &cat);
979 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
982 smack_catset_bit(cat, mapcatset);
985 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
987 netlbl_secattr_catmap_free(skp->smk_netlabel.attr.mls.cat);
988 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
989 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
994 mutex_unlock(&smack_cipso_lock);
1001 * smk_write_cipso - write() for /smack/cipso
1002 * @file: file pointer, not actually used
1003 * @buf: where to get the data from
1004 * @count: bytes sent
1005 * @ppos: where to start
1007 * Accepts only one cipso rule per write call.
1008 * Returns number of bytes written or error code, as appropriate
1010 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
1011 size_t count, loff_t *ppos)
1013 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
1016 static const struct file_operations smk_cipso_ops = {
1017 .open = smk_open_cipso,
1019 .llseek = seq_lseek,
1020 .write = smk_write_cipso,
1021 .release = seq_release,
1025 * Seq_file read operations for /smack/cipso2
1029 * Print cipso labels in format:
1030 * label level[/cat[,cat]]
1032 static int cipso2_seq_show(struct seq_file *s, void *v)
1034 struct list_head *list = v;
1035 struct smack_known *skp =
1036 list_entry(list, struct smack_known, list);
1037 struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
1041 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
1043 for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
1044 i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
1045 seq_printf(s, "%c%d", sep, i);
1054 static const struct seq_operations cipso2_seq_ops = {
1055 .start = cipso_seq_start,
1056 .next = cipso_seq_next,
1057 .show = cipso2_seq_show,
1058 .stop = smk_seq_stop,
1062 * smk_open_cipso2 - open() for /smack/cipso2
1063 * @inode: inode structure representing file
1064 * @file: "cipso2" file pointer
1066 * Connect our cipso_seq_* operations with /smack/cipso2
1069 static int smk_open_cipso2(struct inode *inode, struct file *file)
1071 return seq_open(file, &cipso2_seq_ops);
1075 * smk_write_cipso2 - write() for /smack/cipso2
1076 * @file: file pointer, not actually used
1077 * @buf: where to get the data from
1078 * @count: bytes sent
1079 * @ppos: where to start
1081 * Accepts only one cipso rule per write call.
1082 * Returns number of bytes written or error code, as appropriate
1084 static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1085 size_t count, loff_t *ppos)
1087 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1090 static const struct file_operations smk_cipso2_ops = {
1091 .open = smk_open_cipso2,
1093 .llseek = seq_lseek,
1094 .write = smk_write_cipso2,
1095 .release = seq_release,
1099 * Seq_file read operations for /smack/netlabel
1102 static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
1104 return smk_seq_start(s, pos, &smk_netlbladdr_list);
1107 static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1109 return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
1111 #define BEBITS (sizeof(__be32) * 8)
1114 * Print host/label pairs
1116 static int netlbladdr_seq_show(struct seq_file *s, void *v)
1118 struct list_head *list = v;
1119 struct smk_netlbladdr *skp =
1120 list_entry(list, struct smk_netlbladdr, list);
1121 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
1123 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
1125 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
1127 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
1128 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
1133 static const struct seq_operations netlbladdr_seq_ops = {
1134 .start = netlbladdr_seq_start,
1135 .next = netlbladdr_seq_next,
1136 .show = netlbladdr_seq_show,
1137 .stop = smk_seq_stop,
1141 * smk_open_netlbladdr - open() for /smack/netlabel
1142 * @inode: inode structure representing file
1143 * @file: "netlabel" file pointer
1145 * Connect our netlbladdr_seq_* operations with /smack/netlabel
1148 static int smk_open_netlbladdr(struct inode *inode, struct file *file)
1150 return seq_open(file, &netlbladdr_seq_ops);
1154 * smk_netlbladdr_insert
1155 * @new : netlabel to insert
1157 * This helper insert netlabel in the smack_netlbladdrs list
1158 * sorted by netmask length (longest to smallest)
1159 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
1162 static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
1164 struct smk_netlbladdr *m, *m_next;
1166 if (list_empty(&smk_netlbladdr_list)) {
1167 list_add_rcu(&new->list, &smk_netlbladdr_list);
1171 m = list_entry_rcu(smk_netlbladdr_list.next,
1172 struct smk_netlbladdr, list);
1174 /* the comparison '>' is a bit hacky, but works */
1175 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
1176 list_add_rcu(&new->list, &smk_netlbladdr_list);
1180 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
1181 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
1182 list_add_rcu(&new->list, &m->list);
1185 m_next = list_entry_rcu(m->list.next,
1186 struct smk_netlbladdr, list);
1187 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
1188 list_add_rcu(&new->list, &m->list);
1196 * smk_write_netlbladdr - write() for /smack/netlabel
1197 * @file: file pointer, not actually used
1198 * @buf: where to get the data from
1199 * @count: bytes sent
1200 * @ppos: where to start
1202 * Accepts only one netlbladdr per write call.
1203 * Returns number of bytes written or error code, as appropriate
1205 static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
1206 size_t count, loff_t *ppos)
1208 struct smk_netlbladdr *skp;
1209 struct sockaddr_in newname;
1213 char *host = (char *)&newname.sin_addr.s_addr;
1215 struct netlbl_audit audit_info;
1216 struct in_addr mask;
1219 u32 mask_bits = (1<<31);
1224 * Must have privilege.
1225 * No partial writes.
1226 * Enough data must be present.
1227 * "<addr/mask, as a.b.c.d/e><space><label>"
1228 * "<addr, as a.b.c.d><space><label>"
1230 if (!smack_privileged(CAP_MAC_ADMIN))
1234 if (count < SMK_NETLBLADDRMIN)
1237 data = kzalloc(count + 1, GFP_KERNEL);
1241 if (copy_from_user(data, buf, count) != 0) {
1246 smack = kzalloc(count + 1, GFP_KERNEL);
1247 if (smack == NULL) {
1254 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
1255 &host[0], &host[1], &host[2], &host[3], &m, smack);
1257 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1258 &host[0], &host[1], &host[2], &host[3], smack);
1271 * If smack begins with '-', it is an option, don't import it
1273 if (smack[0] != '-') {
1274 sp = smk_import(smack, 0);
1280 /* check known options */
1281 if (strcmp(smack, smack_cipso_option) == 0)
1282 sp = (char *)smack_cipso_option;
1289 for (temp_mask = 0; m > 0; m--) {
1290 temp_mask |= mask_bits;
1293 mask.s_addr = cpu_to_be32(temp_mask);
1295 newname.sin_addr.s_addr &= mask.s_addr;
1297 * Only allow one writer at a time. Writes should be
1298 * quite rare and small in any case.
1300 mutex_lock(&smk_netlbladdr_lock);
1302 nsa = newname.sin_addr.s_addr;
1303 /* try to find if the prefix is already in the list */
1305 list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
1306 if (skp->smk_host.sin_addr.s_addr == nsa &&
1307 skp->smk_mask.s_addr == mask.s_addr) {
1312 smk_netlabel_audit_set(&audit_info);
1315 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
1320 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
1321 skp->smk_mask.s_addr = mask.s_addr;
1322 skp->smk_label = sp;
1323 smk_netlbladdr_insert(skp);
1326 /* we delete the unlabeled entry, only if the previous label
1327 * wasn't the special CIPSO option */
1328 if (skp->smk_label != smack_cipso_option)
1329 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1330 &skp->smk_host.sin_addr, &skp->smk_mask,
1331 PF_INET, &audit_info);
1334 skp->smk_label = sp;
1338 * Now tell netlabel about the single label nature of
1339 * this host so that incoming packets get labeled.
1340 * but only if we didn't get the special CIPSO option
1342 if (rc == 0 && sp != smack_cipso_option)
1343 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1344 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
1345 smack_to_secid(skp->smk_label), &audit_info);
1350 mutex_unlock(&smk_netlbladdr_lock);
1360 static const struct file_operations smk_netlbladdr_ops = {
1361 .open = smk_open_netlbladdr,
1363 .llseek = seq_lseek,
1364 .write = smk_write_netlbladdr,
1365 .release = seq_release,
1369 * smk_read_doi - read() for /smack/doi
1370 * @filp: file pointer, not actually used
1371 * @buf: where to put the result
1372 * @count: maximum to send along
1373 * @ppos: where to start
1375 * Returns number of bytes read or error code, as appropriate
1377 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1378 size_t count, loff_t *ppos)
1386 sprintf(temp, "%d", smk_cipso_doi_value);
1387 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1393 * smk_write_doi - write() for /smack/doi
1394 * @file: file pointer, not actually used
1395 * @buf: where to get the data from
1396 * @count: bytes sent
1397 * @ppos: where to start
1399 * Returns number of bytes written or error code, as appropriate
1401 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1402 size_t count, loff_t *ppos)
1407 if (!smack_privileged(CAP_MAC_ADMIN))
1410 if (count >= sizeof(temp) || count == 0)
1413 if (copy_from_user(temp, buf, count) != 0)
1418 if (sscanf(temp, "%d", &i) != 1)
1421 smk_cipso_doi_value = i;
1428 static const struct file_operations smk_doi_ops = {
1429 .read = smk_read_doi,
1430 .write = smk_write_doi,
1431 .llseek = default_llseek,
1435 * smk_read_direct - read() for /smack/direct
1436 * @filp: file pointer, not actually used
1437 * @buf: where to put the result
1438 * @count: maximum to send along
1439 * @ppos: where to start
1441 * Returns number of bytes read or error code, as appropriate
1443 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1444 size_t count, loff_t *ppos)
1452 sprintf(temp, "%d", smack_cipso_direct);
1453 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1459 * smk_write_direct - write() for /smack/direct
1460 * @file: file pointer, not actually used
1461 * @buf: where to get the data from
1462 * @count: bytes sent
1463 * @ppos: where to start
1465 * Returns number of bytes written or error code, as appropriate
1467 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1468 size_t count, loff_t *ppos)
1470 struct smack_known *skp;
1474 if (!smack_privileged(CAP_MAC_ADMIN))
1477 if (count >= sizeof(temp) || count == 0)
1480 if (copy_from_user(temp, buf, count) != 0)
1485 if (sscanf(temp, "%d", &i) != 1)
1489 * Don't do anything if the value hasn't actually changed.
1490 * If it is changing reset the level on entries that were
1491 * set up to be direct when they were created.
1493 if (smack_cipso_direct != i) {
1494 mutex_lock(&smack_known_lock);
1495 list_for_each_entry_rcu(skp, &smack_known_list, list)
1496 if (skp->smk_netlabel.attr.mls.lvl ==
1498 skp->smk_netlabel.attr.mls.lvl = i;
1499 smack_cipso_direct = i;
1500 mutex_unlock(&smack_known_lock);
1506 static const struct file_operations smk_direct_ops = {
1507 .read = smk_read_direct,
1508 .write = smk_write_direct,
1509 .llseek = default_llseek,
1513 * smk_read_mapped - read() for /smack/mapped
1514 * @filp: file pointer, not actually used
1515 * @buf: where to put the result
1516 * @count: maximum to send along
1517 * @ppos: where to start
1519 * Returns number of bytes read or error code, as appropriate
1521 static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1522 size_t count, loff_t *ppos)
1530 sprintf(temp, "%d", smack_cipso_mapped);
1531 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1537 * smk_write_mapped - write() for /smack/mapped
1538 * @file: file pointer, not actually used
1539 * @buf: where to get the data from
1540 * @count: bytes sent
1541 * @ppos: where to start
1543 * Returns number of bytes written or error code, as appropriate
1545 static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1546 size_t count, loff_t *ppos)
1548 struct smack_known *skp;
1552 if (!smack_privileged(CAP_MAC_ADMIN))
1555 if (count >= sizeof(temp) || count == 0)
1558 if (copy_from_user(temp, buf, count) != 0)
1563 if (sscanf(temp, "%d", &i) != 1)
1567 * Don't do anything if the value hasn't actually changed.
1568 * If it is changing reset the level on entries that were
1569 * set up to be mapped when they were created.
1571 if (smack_cipso_mapped != i) {
1572 mutex_lock(&smack_known_lock);
1573 list_for_each_entry_rcu(skp, &smack_known_list, list)
1574 if (skp->smk_netlabel.attr.mls.lvl ==
1576 skp->smk_netlabel.attr.mls.lvl = i;
1577 smack_cipso_mapped = i;
1578 mutex_unlock(&smack_known_lock);
1584 static const struct file_operations smk_mapped_ops = {
1585 .read = smk_read_mapped,
1586 .write = smk_write_mapped,
1587 .llseek = default_llseek,
1591 * smk_read_ambient - read() for /smack/ambient
1592 * @filp: file pointer, not actually used
1593 * @buf: where to put the result
1594 * @cn: maximum to send along
1595 * @ppos: where to start
1597 * Returns number of bytes read or error code, as appropriate
1599 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1600 size_t cn, loff_t *ppos)
1608 * Being careful to avoid a problem in the case where
1609 * smack_net_ambient gets changed in midstream.
1611 mutex_lock(&smack_ambient_lock);
1613 asize = strlen(smack_net_ambient->smk_known) + 1;
1616 rc = simple_read_from_buffer(buf, cn, ppos,
1617 smack_net_ambient->smk_known,
1622 mutex_unlock(&smack_ambient_lock);
1628 * smk_write_ambient - write() for /smack/ambient
1629 * @file: file pointer, not actually used
1630 * @buf: where to get the data from
1631 * @count: bytes sent
1632 * @ppos: where to start
1634 * Returns number of bytes written or error code, as appropriate
1636 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1637 size_t count, loff_t *ppos)
1639 struct smack_known *skp;
1644 if (!smack_privileged(CAP_MAC_ADMIN))
1647 data = kzalloc(count + 1, GFP_KERNEL);
1651 if (copy_from_user(data, buf, count) != 0) {
1656 skp = smk_import_entry(data, count);
1662 mutex_lock(&smack_ambient_lock);
1664 oldambient = smack_net_ambient->smk_known;
1665 smack_net_ambient = skp;
1666 smk_unlbl_ambient(oldambient);
1668 mutex_unlock(&smack_ambient_lock);
1675 static const struct file_operations smk_ambient_ops = {
1676 .read = smk_read_ambient,
1677 .write = smk_write_ambient,
1678 .llseek = default_llseek,
1682 * smk_read_onlycap - read() for smackfs/onlycap
1683 * @filp: file pointer, not actually used
1684 * @buf: where to put the result
1685 * @cn: maximum to send along
1686 * @ppos: where to start
1688 * Returns number of bytes read or error code, as appropriate
1690 static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1691 size_t cn, loff_t *ppos)
1694 ssize_t rc = -EINVAL;
1700 if (smack_onlycap != NULL)
1701 smack = smack_onlycap->smk_known;
1703 asize = strlen(smack) + 1;
1706 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1712 * smk_write_onlycap - write() for smackfs/onlycap
1713 * @file: file pointer, not actually used
1714 * @buf: where to get the data from
1715 * @count: bytes sent
1716 * @ppos: where to start
1718 * Returns number of bytes written or error code, as appropriate
1720 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1721 size_t count, loff_t *ppos)
1724 struct smack_known *skp = smk_of_task(current->cred->security);
1727 if (!smack_privileged(CAP_MAC_ADMIN))
1731 * This can be done using smk_access() but is done
1732 * explicitly for clarity. The smk_access() implementation
1733 * would use smk_access(smack_onlycap, MAY_WRITE)
1735 if (smack_onlycap != NULL && smack_onlycap != skp)
1738 data = kzalloc(count, GFP_KERNEL);
1743 * Should the null string be passed in unset the onlycap value.
1744 * This seems like something to be careful with as usually
1745 * smk_import only expects to return NULL for errors. It
1746 * is usually the case that a nullstring or "\n" would be
1747 * bad to pass to smk_import but in fact this is useful here.
1749 * smk_import will also reject a label beginning with '-',
1750 * so "-usecapabilities" will also work.
1752 if (copy_from_user(data, buf, count) != 0)
1755 smack_onlycap = smk_import_entry(data, count);
1761 static const struct file_operations smk_onlycap_ops = {
1762 .read = smk_read_onlycap,
1763 .write = smk_write_onlycap,
1764 .llseek = default_llseek,
1768 * smk_read_logging - read() for /smack/logging
1769 * @filp: file pointer, not actually used
1770 * @buf: where to put the result
1771 * @cn: maximum to send along
1772 * @ppos: where to start
1774 * Returns number of bytes read or error code, as appropriate
1776 static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1777 size_t count, loff_t *ppos)
1785 sprintf(temp, "%d\n", log_policy);
1786 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1791 * smk_write_logging - write() for /smack/logging
1792 * @file: file pointer, not actually used
1793 * @buf: where to get the data from
1794 * @count: bytes sent
1795 * @ppos: where to start
1797 * Returns number of bytes written or error code, as appropriate
1799 static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1800 size_t count, loff_t *ppos)
1805 if (!smack_privileged(CAP_MAC_ADMIN))
1808 if (count >= sizeof(temp) || count == 0)
1811 if (copy_from_user(temp, buf, count) != 0)
1816 if (sscanf(temp, "%d", &i) != 1)
1826 static const struct file_operations smk_logging_ops = {
1827 .read = smk_read_logging,
1828 .write = smk_write_logging,
1829 .llseek = default_llseek,
1833 * Seq_file read operations for /smack/load-self
1836 static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
1838 struct task_smack *tsp = current_security();
1840 return smk_seq_start(s, pos, &tsp->smk_rules);
1843 static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
1845 struct task_smack *tsp = current_security();
1847 return smk_seq_next(s, v, pos, &tsp->smk_rules);
1850 static int load_self_seq_show(struct seq_file *s, void *v)
1852 struct list_head *list = v;
1853 struct smack_rule *srp =
1854 list_entry(list, struct smack_rule, list);
1856 smk_rule_show(s, srp, SMK_LABELLEN);
1861 static const struct seq_operations load_self_seq_ops = {
1862 .start = load_self_seq_start,
1863 .next = load_self_seq_next,
1864 .show = load_self_seq_show,
1865 .stop = smk_seq_stop,
1870 * smk_open_load_self - open() for /smack/load-self2
1871 * @inode: inode structure representing file
1872 * @file: "load" file pointer
1874 * For reading, use load_seq_* seq_file reading operations.
1876 static int smk_open_load_self(struct inode *inode, struct file *file)
1878 return seq_open(file, &load_self_seq_ops);
1882 * smk_write_load_self - write() for /smack/load-self
1883 * @file: file pointer, not actually used
1884 * @buf: where to get the data from
1885 * @count: bytes sent
1886 * @ppos: where to start - must be 0
1889 static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
1890 size_t count, loff_t *ppos)
1892 struct task_smack *tsp = current_security();
1894 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
1895 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
1898 static const struct file_operations smk_load_self_ops = {
1899 .open = smk_open_load_self,
1901 .llseek = seq_lseek,
1902 .write = smk_write_load_self,
1903 .release = seq_release,
1907 * smk_user_access - handle access check transaction
1908 * @file: file pointer
1909 * @buf: data from user space
1910 * @count: bytes sent
1911 * @ppos: where to start - must be 0
1913 static ssize_t smk_user_access(struct file *file, const char __user *buf,
1914 size_t count, loff_t *ppos, int format)
1916 struct smack_parsed_rule rule;
1920 data = simple_transaction_get(file, buf, count);
1922 return PTR_ERR(data);
1924 if (format == SMK_FIXED24_FMT) {
1925 if (count < SMK_LOADLEN)
1927 res = smk_parse_rule(data, &rule, 0);
1930 * simple_transaction_get() returns null-terminated data
1932 res = smk_parse_long_rule(data, &rule, 0, 3);
1936 res = smk_access(rule.smk_subject, rule.smk_object,
1937 rule.smk_access1, NULL);
1938 else if (res != -ENOENT)
1941 data[0] = res == 0 ? '1' : '0';
1944 simple_transaction_set(file, 2);
1946 if (format == SMK_FIXED24_FMT)
1952 * smk_write_access - handle access check transaction
1953 * @file: file pointer
1954 * @buf: data from user space
1955 * @count: bytes sent
1956 * @ppos: where to start - must be 0
1958 static ssize_t smk_write_access(struct file *file, const char __user *buf,
1959 size_t count, loff_t *ppos)
1961 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
1964 static const struct file_operations smk_access_ops = {
1965 .write = smk_write_access,
1966 .read = simple_transaction_read,
1967 .release = simple_transaction_release,
1968 .llseek = generic_file_llseek,
1973 * Seq_file read operations for /smack/load2
1976 static int load2_seq_show(struct seq_file *s, void *v)
1978 struct list_head *list = v;
1979 struct smack_master_list *smlp =
1980 list_entry(list, struct smack_master_list, list);
1982 smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
1987 static const struct seq_operations load2_seq_ops = {
1988 .start = load2_seq_start,
1989 .next = load2_seq_next,
1990 .show = load2_seq_show,
1991 .stop = smk_seq_stop,
1995 * smk_open_load2 - open() for /smack/load2
1996 * @inode: inode structure representing file
1997 * @file: "load2" file pointer
1999 * For reading, use load2_seq_* seq_file reading operations.
2001 static int smk_open_load2(struct inode *inode, struct file *file)
2003 return seq_open(file, &load2_seq_ops);
2007 * smk_write_load2 - write() for /smack/load2
2008 * @file: file pointer, not actually used
2009 * @buf: where to get the data from
2010 * @count: bytes sent
2011 * @ppos: where to start - must be 0
2014 static ssize_t smk_write_load2(struct file *file, const char __user *buf,
2015 size_t count, loff_t *ppos)
2018 * Must have privilege.
2020 if (!smack_privileged(CAP_MAC_ADMIN))
2023 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2027 static const struct file_operations smk_load2_ops = {
2028 .open = smk_open_load2,
2030 .llseek = seq_lseek,
2031 .write = smk_write_load2,
2032 .release = seq_release,
2036 * Seq_file read operations for /smack/load-self2
2039 static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
2041 struct task_smack *tsp = current_security();
2043 return smk_seq_start(s, pos, &tsp->smk_rules);
2046 static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
2048 struct task_smack *tsp = current_security();
2050 return smk_seq_next(s, v, pos, &tsp->smk_rules);
2053 static int load_self2_seq_show(struct seq_file *s, void *v)
2055 struct list_head *list = v;
2056 struct smack_rule *srp =
2057 list_entry(list, struct smack_rule, list);
2059 smk_rule_show(s, srp, SMK_LONGLABEL);
2064 static const struct seq_operations load_self2_seq_ops = {
2065 .start = load_self2_seq_start,
2066 .next = load_self2_seq_next,
2067 .show = load_self2_seq_show,
2068 .stop = smk_seq_stop,
2072 * smk_open_load_self2 - open() for /smack/load-self2
2073 * @inode: inode structure representing file
2074 * @file: "load" file pointer
2076 * For reading, use load_seq_* seq_file reading operations.
2078 static int smk_open_load_self2(struct inode *inode, struct file *file)
2080 return seq_open(file, &load_self2_seq_ops);
2084 * smk_write_load_self2 - write() for /smack/load-self2
2085 * @file: file pointer, not actually used
2086 * @buf: where to get the data from
2087 * @count: bytes sent
2088 * @ppos: where to start - must be 0
2091 static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2092 size_t count, loff_t *ppos)
2094 struct task_smack *tsp = current_security();
2096 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2097 &tsp->smk_rules_lock, SMK_LONG_FMT);
2100 static const struct file_operations smk_load_self2_ops = {
2101 .open = smk_open_load_self2,
2103 .llseek = seq_lseek,
2104 .write = smk_write_load_self2,
2105 .release = seq_release,
2109 * smk_write_access2 - handle access check transaction
2110 * @file: file pointer
2111 * @buf: data from user space
2112 * @count: bytes sent
2113 * @ppos: where to start - must be 0
2115 static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2116 size_t count, loff_t *ppos)
2118 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2121 static const struct file_operations smk_access2_ops = {
2122 .write = smk_write_access2,
2123 .read = simple_transaction_read,
2124 .release = simple_transaction_release,
2125 .llseek = generic_file_llseek,
2129 * smk_write_revoke_subj - write() for /smack/revoke-subject
2130 * @file: file pointer
2131 * @buf: data from user space
2132 * @count: bytes sent
2133 * @ppos: where to start - must be 0
2135 static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2136 size_t count, loff_t *ppos)
2139 const char *cp = NULL;
2140 struct smack_known *skp;
2141 struct smack_rule *sp;
2142 struct list_head *rule_list;
2143 struct mutex *rule_lock;
2149 if (!smack_privileged(CAP_MAC_ADMIN))
2152 if (count == 0 || count > SMK_LONGLABEL)
2155 data = kzalloc(count, GFP_KERNEL);
2159 if (copy_from_user(data, buf, count) != 0) {
2164 cp = smk_parse_smack(data, count);
2170 skp = smk_find_entry(cp);
2174 rule_list = &skp->smk_rules;
2175 rule_lock = &skp->smk_rules_lock;
2177 mutex_lock(rule_lock);
2179 list_for_each_entry_rcu(sp, rule_list, list)
2182 mutex_unlock(rule_lock);
2190 static const struct file_operations smk_revoke_subj_ops = {
2191 .write = smk_write_revoke_subj,
2192 .read = simple_transaction_read,
2193 .release = simple_transaction_release,
2194 .llseek = generic_file_llseek,
2197 static struct kset *smackfs_kset;
2199 * smk_init_sysfs - initialize /sys/fs/smackfs
2202 static int smk_init_sysfs(void)
2204 smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj);
2211 * smk_write_change_rule - write() for /smack/change-rule
2212 * @file: file pointer
2213 * @buf: data from user space
2214 * @count: bytes sent
2215 * @ppos: where to start - must be 0
2217 static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2218 size_t count, loff_t *ppos)
2221 * Must have privilege.
2223 if (!smack_privileged(CAP_MAC_ADMIN))
2226 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2230 static const struct file_operations smk_change_rule_ops = {
2231 .write = smk_write_change_rule,
2232 .read = simple_transaction_read,
2233 .release = simple_transaction_release,
2234 .llseek = generic_file_llseek,
2238 * smk_read_syslog - read() for smackfs/syslog
2239 * @filp: file pointer, not actually used
2240 * @buf: where to put the result
2241 * @cn: maximum to send along
2242 * @ppos: where to start
2244 * Returns number of bytes read or error code, as appropriate
2246 static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
2247 size_t cn, loff_t *ppos)
2249 struct smack_known *skp;
2250 ssize_t rc = -EINVAL;
2256 if (smack_syslog_label == NULL)
2257 skp = &smack_known_star;
2259 skp = smack_syslog_label;
2261 asize = strlen(skp->smk_known) + 1;
2264 rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
2271 * smk_write_syslog - write() for smackfs/syslog
2272 * @file: file pointer, not actually used
2273 * @buf: where to get the data from
2274 * @count: bytes sent
2275 * @ppos: where to start
2277 * Returns number of bytes written or error code, as appropriate
2279 static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
2280 size_t count, loff_t *ppos)
2283 struct smack_known *skp;
2286 if (!smack_privileged(CAP_MAC_ADMIN))
2289 data = kzalloc(count, GFP_KERNEL);
2293 if (copy_from_user(data, buf, count) != 0)
2296 skp = smk_import_entry(data, count);
2300 smack_syslog_label = smk_import_entry(data, count);
2307 static const struct file_operations smk_syslog_ops = {
2308 .read = smk_read_syslog,
2309 .write = smk_write_syslog,
2310 .llseek = default_llseek,
2315 * smk_fill_super - fill the smackfs superblock
2316 * @sb: the empty superblock
2320 * Fill in the well known entries for the smack filesystem
2322 * Returns 0 on success, an error code on failure
2324 static int smk_fill_super(struct super_block *sb, void *data, int silent)
2327 struct inode *root_inode;
2329 static struct tree_descr smack_files[] = {
2331 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2333 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2335 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2337 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2339 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2340 [SMK_NETLBLADDR] = {
2341 "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
2343 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2345 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2347 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
2349 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
2351 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2353 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2354 [SMK_LOAD_SELF2] = {
2355 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2357 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2359 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
2360 [SMK_REVOKE_SUBJ] = {
2361 "revoke-subject", &smk_revoke_subj_ops,
2363 [SMK_CHANGE_RULE] = {
2364 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
2366 "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
2367 #ifdef CONFIG_SECURITY_SMACK_PERMISSIVE_MODE
2368 [SMK_PERMISSIVE] = {
2369 "permissive", &smk_permissive_ops, S_IRUGO|S_IWUSR},
2375 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2377 printk(KERN_ERR "%s failed %d while creating inodes\n",
2382 root_inode = sb->s_root->d_inode;
2388 * smk_mount - get the smackfs superblock
2389 * @fs_type: passed along without comment
2390 * @flags: passed along without comment
2391 * @dev_name: passed along without comment
2392 * @data: passed along without comment
2394 * Just passes everything along.
2396 * Returns what the lower level code does.
2398 static struct dentry *smk_mount(struct file_system_type *fs_type,
2399 int flags, const char *dev_name, void *data)
2401 return mount_single(fs_type, flags, data, smk_fill_super);
2404 static struct file_system_type smk_fs_type = {
2407 .kill_sb = kill_litter_super,
2410 static struct vfsmount *smackfs_mount;
2412 static int __init smk_preset_netlabel(struct smack_known *skp)
2414 skp->smk_netlabel.domain = skp->smk_known;
2415 skp->smk_netlabel.flags =
2416 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2417 return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2418 &skp->smk_netlabel, strlen(skp->smk_known));
2422 * init_smk_fs - get the smackfs superblock
2424 * register the smackfs
2426 * Do not register smackfs if Smack wasn't enabled
2427 * on boot. We can not put this method normally under the
2428 * smack_init() code path since the security subsystem get
2429 * initialized before the vfs caches.
2431 * Returns true if we were not chosen on boot or if
2432 * we were chosen and filesystem registration succeeded.
2434 static int __init init_smk_fs(void)
2439 if (!security_module_enable(&smack_ops))
2442 err = smk_init_sysfs();
2444 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2446 err = register_filesystem(&smk_fs_type);
2448 smackfs_mount = kern_mount(&smk_fs_type);
2449 if (IS_ERR(smackfs_mount)) {
2450 printk(KERN_ERR "smackfs: could not mount!\n");
2451 err = PTR_ERR(smackfs_mount);
2452 smackfs_mount = NULL;
2457 smk_unlbl_ambient(NULL);
2459 rc = smk_preset_netlabel(&smack_known_floor);
2460 if (err == 0 && rc < 0)
2462 rc = smk_preset_netlabel(&smack_known_hat);
2463 if (err == 0 && rc < 0)
2465 rc = smk_preset_netlabel(&smack_known_huh);
2466 if (err == 0 && rc < 0)
2468 rc = smk_preset_netlabel(&smack_known_invalid);
2469 if (err == 0 && rc < 0)
2471 rc = smk_preset_netlabel(&smack_known_star);
2472 if (err == 0 && rc < 0)
2474 rc = smk_preset_netlabel(&smack_known_web);
2475 if (err == 0 && rc < 0)
2481 __initcall(init_smk_fs);