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 SMK_PTRACE = 21, /* set ptrace rule */
57 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
58 SMK_UNCONFINED = 22, /* define an unconfined label */
65 static DEFINE_MUTEX(smack_cipso_lock);
66 static DEFINE_MUTEX(smack_ambient_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;
100 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
102 * Allow one label to be unconfined. This is for
103 * debugging and application bring-up purposes only.
104 * It is bad and wrong, but everyone seems to expect
107 struct smack_known *smack_unconfined;
111 * If this value is set restrict syslog use to the label specified.
112 * It can be reset via smackfs/syslog
114 struct smack_known *smack_syslog_label;
117 * Ptrace current rule
118 * SMACK_PTRACE_DEFAULT regular smack ptrace rules (/proc based)
119 * SMACK_PTRACE_EXACT labels must match, but can be overriden with
121 * SMACK_PTRACE_DRACONIAN lables must match, CAP_SYS_PTRACE has no effect
123 int smack_ptrace_rule = SMACK_PTRACE_DEFAULT;
126 * Certain IP addresses may be designated as single label hosts.
127 * Packets are sent there unlabeled, but only from tasks that
128 * can write to the specified label.
131 LIST_HEAD(smk_netlbladdr_list);
134 * Rule lists are maintained for each label.
135 * This master list is just for reading /smack/load and /smack/load2.
137 struct smack_master_list {
138 struct list_head list;
139 struct smack_rule *smk_rule;
142 LIST_HEAD(smack_rule_list);
144 struct smack_parsed_rule {
145 struct smack_known *smk_subject;
146 struct smack_known *smk_object;
151 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
153 struct smack_known smack_cipso_option = {
154 .smk_known = SMACK_CIPSO_OPTION,
159 * Values for parsing cipso rules
160 * SMK_DIGITLEN: Length of a digit field in a rule.
161 * SMK_CIPSOMIN: Minimum possible cipso rule length.
162 * SMK_CIPSOMAX: Maximum possible cipso rule length.
164 #define SMK_DIGITLEN 4
165 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
166 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
169 * Values for parsing MAC rules
170 * SMK_ACCESS: Maximum possible combination of access permissions
171 * SMK_ACCESSLEN: Maximum length for a rule access field
172 * SMK_LOADLEN: Smack rule length
174 #define SMK_OACCESS "rwxa"
175 #define SMK_ACCESS "rwxatl"
176 #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
177 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
178 #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
179 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
182 * Stricly for CIPSO level manipulation.
183 * Set the category bit number in a smack label sized buffer.
185 static inline void smack_catset_bit(unsigned int cat, char *catsetp)
187 if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
190 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
194 * smk_netlabel_audit_set - fill a netlbl_audit struct
195 * @nap: structure to fill
197 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
199 struct smack_known *skp = smk_of_current();
201 nap->loginuid = audit_get_loginuid(current);
202 nap->sessionid = audit_get_sessionid(current);
203 nap->secid = skp->smk_secid;
207 * Value for parsing single label host rules
210 #define SMK_NETLBLADDRMIN 9
213 * smk_set_access - add a rule to the rule list or replace an old rule
214 * @srp: the rule to add or replace
215 * @rule_list: the list of rules
216 * @rule_lock: the rule list lock
217 * @global: if non-zero, indicates a global rule
219 * Looks through the current subject/object/access list for
220 * the subject/object pair and replaces the access that was
221 * there. If the pair isn't found add it with the specified
224 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
225 * during the allocation of the new pair to add.
227 static int smk_set_access(struct smack_parsed_rule *srp,
228 struct list_head *rule_list,
229 struct mutex *rule_lock, int global)
231 struct smack_rule *sp;
232 struct smack_master_list *smlp;
236 mutex_lock(rule_lock);
239 * Because the object label is less likely to match
240 * than the subject label check it first
242 list_for_each_entry_rcu(sp, rule_list, list) {
243 if (sp->smk_object == srp->smk_object &&
244 sp->smk_subject == srp->smk_subject) {
246 sp->smk_access |= srp->smk_access1;
247 sp->smk_access &= ~srp->smk_access2;
253 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
259 sp->smk_subject = srp->smk_subject;
260 sp->smk_object = srp->smk_object;
261 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
263 list_add_rcu(&sp->list, rule_list);
265 * If this is a global as opposed to self and a new rule
266 * it needs to get added for reporting.
269 smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
272 list_add_rcu(&smlp->list, &smack_rule_list);
279 mutex_unlock(rule_lock);
284 * smk_perm_from_str - parse smack accesses from a text string
285 * @string: a text string that contains a Smack accesses code
287 * Returns an integer with respective bits set for specified accesses.
289 static int smk_perm_from_str(const char *string)
294 for (cp = string; ; cp++)
316 perm |= MAY_TRANSMUTE;
332 * smk_fill_rule - Fill Smack rule from strings
333 * @subject: subject label string
334 * @object: object label string
335 * @access1: access string
336 * @access2: string with permissions to be removed
338 * @import: if non-zero, import labels
339 * @len: label length limit
341 * Returns 0 on success, -EINVAL on failure and -ENOENT when either subject
342 * or object is missing.
344 static int smk_fill_rule(const char *subject, const char *object,
345 const char *access1, const char *access2,
346 struct smack_parsed_rule *rule, int import,
350 struct smack_known *skp;
353 rule->smk_subject = smk_import_entry(subject, len);
354 if (rule->smk_subject == NULL)
357 rule->smk_object = smk_import_entry(object, len);
358 if (rule->smk_object == NULL)
361 cp = smk_parse_smack(subject, len);
364 skp = smk_find_entry(cp);
368 rule->smk_subject = skp;
370 cp = smk_parse_smack(object, len);
373 skp = smk_find_entry(cp);
377 rule->smk_object = skp;
380 rule->smk_access1 = smk_perm_from_str(access1);
382 rule->smk_access2 = smk_perm_from_str(access2);
384 rule->smk_access2 = ~rule->smk_access1;
390 * smk_parse_rule - parse Smack rule from load string
391 * @data: string to be parsed whose size is SMK_LOADLEN
393 * @import: if non-zero, import labels
395 * Returns 0 on success, -1 on errors.
397 static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
402 rc = smk_fill_rule(data, data + SMK_LABELLEN,
403 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
404 import, SMK_LABELLEN);
409 * smk_parse_long_rule - parse Smack rule from rule string
410 * @data: string to be parsed, null terminated
411 * @rule: Will be filled with Smack parsed rule
412 * @import: if non-zero, import labels
413 * @tokens: numer of substrings expected in data
415 * Returns number of processed bytes on success, -1 on failure.
417 static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
418 int import, int tokens)
426 * Parsing the rule in-place, filling all white-spaces with '\0'
428 for (i = 0; i < tokens; ++i) {
429 while (isspace(data[cnt]))
432 if (data[cnt] == '\0')
433 /* Unexpected end of data */
438 while (data[cnt] && !isspace(data[cnt]))
441 while (isspace(data[cnt]))
447 rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
448 return rc == 0 ? cnt : rc;
451 #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
452 #define SMK_LONG_FMT 1 /* Variable long label format */
453 #define SMK_CHANGE_FMT 2 /* Rule modification format */
455 * smk_write_rules_list - write() for any /smack rule file
456 * @file: file pointer, not actually used
457 * @buf: where to get the data from
459 * @ppos: where to start - must be 0
460 * @rule_list: the list of rules to write to
461 * @rule_lock: lock for the rule list
462 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
464 * Get one smack access rule from above.
465 * The format for SMK_LONG_FMT is:
466 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
467 * The format for SMK_FIXED24_FMT is exactly:
468 * "subject object rwxat"
469 * The format for SMK_CHANGE_FMT is:
470 * "subject<whitespace>object<whitespace>
471 * acc_enable<whitespace>acc_disable[<whitespace>...]"
473 static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
474 size_t count, loff_t *ppos,
475 struct list_head *rule_list,
476 struct mutex *rule_lock, int format)
478 struct smack_parsed_rule rule;
487 * Enough data must be present.
492 if (format == SMK_FIXED24_FMT) {
494 * Minor hack for backward compatibility
496 if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
499 if (count >= PAGE_SIZE) {
500 count = PAGE_SIZE - 1;
505 data = kmalloc(count + 1, GFP_KERNEL);
509 if (copy_from_user(data, buf, count) != 0) {
515 * In case of parsing only part of user buf,
516 * avoid having partial rule at the data buffer
519 while (count > 0 && (data[count - 1] != '\n'))
528 tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
529 while (cnt < count) {
530 if (format == SMK_FIXED24_FMT) {
531 rc = smk_parse_rule(data, &rule, 1);
538 rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
546 if (rule_list == NULL)
547 rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
548 &rule.smk_subject->smk_rules_lock, 1);
550 rc = smk_set_access(&rule, rule_list, rule_lock, 0);
563 * Core logic for smackfs seq list operations.
566 static void *smk_seq_start(struct seq_file *s, loff_t *pos,
567 struct list_head *head)
569 struct list_head *list;
572 * This is 0 the first time through.
577 if (s->private == NULL)
581 if (list_empty(list))
589 static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
590 struct list_head *head)
592 struct list_head *list = v;
594 if (list_is_last(list, head)) {
598 s->private = list->next;
602 static void smk_seq_stop(struct seq_file *s, void *v)
607 static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
610 * Don't show any rules with label names too long for
611 * interface file (/smack/load or /smack/load2)
612 * because you should expect to be able to write
613 * anything you read back.
615 if (strlen(srp->smk_subject->smk_known) >= max ||
616 strlen(srp->smk_object->smk_known) >= max)
619 if (srp->smk_access == 0)
622 seq_printf(s, "%s %s",
623 srp->smk_subject->smk_known,
624 srp->smk_object->smk_known);
628 if (srp->smk_access & MAY_READ)
630 if (srp->smk_access & MAY_WRITE)
632 if (srp->smk_access & MAY_EXEC)
634 if (srp->smk_access & MAY_APPEND)
636 if (srp->smk_access & MAY_TRANSMUTE)
638 if (srp->smk_access & MAY_LOCK)
640 if (srp->smk_access & MAY_BRINGUP)
647 * Seq_file read operations for /smack/load
650 static void *load2_seq_start(struct seq_file *s, loff_t *pos)
652 return smk_seq_start(s, pos, &smack_rule_list);
655 static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
657 return smk_seq_next(s, v, pos, &smack_rule_list);
660 static int load_seq_show(struct seq_file *s, void *v)
662 struct list_head *list = v;
663 struct smack_master_list *smlp =
664 list_entry(list, struct smack_master_list, list);
666 smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
671 static const struct seq_operations load_seq_ops = {
672 .start = load2_seq_start,
673 .next = load2_seq_next,
674 .show = load_seq_show,
675 .stop = smk_seq_stop,
679 * smk_open_load - open() for /smack/load
680 * @inode: inode structure representing file
681 * @file: "load" file pointer
683 * For reading, use load_seq_* seq_file reading operations.
685 static int smk_open_load(struct inode *inode, struct file *file)
687 return seq_open(file, &load_seq_ops);
691 * smk_write_load - write() for /smack/load
692 * @file: file pointer, not actually used
693 * @buf: where to get the data from
695 * @ppos: where to start - must be 0
698 static ssize_t smk_write_load(struct file *file, const char __user *buf,
699 size_t count, loff_t *ppos)
702 * Must have privilege.
704 * Enough data must be present.
706 if (!smack_privileged(CAP_MAC_ADMIN))
709 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
713 static const struct file_operations smk_load_ops = {
714 .open = smk_open_load,
717 .write = smk_write_load,
718 .release = seq_release,
722 * smk_cipso_doi - initialize the CIPSO domain
724 static void smk_cipso_doi(void)
727 struct cipso_v4_doi *doip;
728 struct netlbl_audit nai;
730 smk_netlabel_audit_set(&nai);
732 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
734 printk(KERN_WARNING "%s:%d remove rc = %d\n",
735 __func__, __LINE__, rc);
737 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
739 panic("smack: Failed to initialize cipso DOI.\n");
740 doip->map.std = NULL;
741 doip->doi = smk_cipso_doi_value;
742 doip->type = CIPSO_V4_MAP_PASS;
743 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
744 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
745 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
747 rc = netlbl_cfg_cipsov4_add(doip, &nai);
749 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
750 __func__, __LINE__, rc);
754 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
756 printk(KERN_WARNING "%s:%d map add rc = %d\n",
757 __func__, __LINE__, rc);
764 * smk_unlbl_ambient - initialize the unlabeled domain
765 * @oldambient: previous domain string
767 static void smk_unlbl_ambient(char *oldambient)
770 struct netlbl_audit nai;
772 smk_netlabel_audit_set(&nai);
774 if (oldambient != NULL) {
775 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
777 printk(KERN_WARNING "%s:%d remove rc = %d\n",
778 __func__, __LINE__, rc);
780 if (smack_net_ambient == NULL)
781 smack_net_ambient = &smack_known_floor;
783 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
786 printk(KERN_WARNING "%s:%d add rc = %d\n",
787 __func__, __LINE__, rc);
791 * Seq_file read operations for /smack/cipso
794 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
796 return smk_seq_start(s, pos, &smack_known_list);
799 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
801 return smk_seq_next(s, v, pos, &smack_known_list);
805 * Print cipso labels in format:
806 * label level[/cat[,cat]]
808 static int cipso_seq_show(struct seq_file *s, void *v)
810 struct list_head *list = v;
811 struct smack_known *skp =
812 list_entry(list, struct smack_known, list);
813 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
818 * Don't show a label that could not have been set using
819 * /smack/cipso. This is in support of the notion that
820 * anything read from /smack/cipso ought to be writeable
823 * /smack/cipso2 should be used instead.
825 if (strlen(skp->smk_known) >= SMK_LABELLEN)
828 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
830 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
831 i = netlbl_catmap_walk(cmp, i + 1)) {
832 seq_printf(s, "%c%d", sep, i);
841 static const struct seq_operations cipso_seq_ops = {
842 .start = cipso_seq_start,
843 .next = cipso_seq_next,
844 .show = cipso_seq_show,
845 .stop = smk_seq_stop,
849 * smk_open_cipso - open() for /smack/cipso
850 * @inode: inode structure representing file
851 * @file: "cipso" file pointer
853 * Connect our cipso_seq_* operations with /smack/cipso
856 static int smk_open_cipso(struct inode *inode, struct file *file)
858 return seq_open(file, &cipso_seq_ops);
862 * smk_set_cipso - do the work for write() for cipso and cipso2
863 * @file: file pointer, not actually used
864 * @buf: where to get the data from
866 * @ppos: where to start
867 * @format: /smack/cipso or /smack/cipso2
869 * Accepts only one cipso rule per write call.
870 * Returns number of bytes written or error code, as appropriate
872 static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
873 size_t count, loff_t *ppos, int format)
875 struct smack_known *skp;
876 struct netlbl_lsm_secattr ncats;
877 char mapcatset[SMK_CIPSOLEN];
881 ssize_t rc = -EINVAL;
888 * Must have privilege.
890 * Enough data must be present.
892 if (!smack_privileged(CAP_MAC_ADMIN))
896 if (format == SMK_FIXED24_FMT &&
897 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
900 data = kzalloc(count + 1, GFP_KERNEL);
904 if (copy_from_user(data, buf, count) != 0) {
912 * Only allow one writer at a time. Writes should be
913 * quite rare and small in any case.
915 mutex_lock(&smack_cipso_lock);
917 skp = smk_import_entry(rule, 0);
921 if (format == SMK_FIXED24_FMT)
922 rule += SMK_LABELLEN;
924 rule += strlen(skp->smk_known) + 1;
926 ret = sscanf(rule, "%d", &maplevel);
927 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
930 rule += SMK_DIGITLEN;
931 ret = sscanf(rule, "%d", &catlen);
932 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
935 if (format == SMK_FIXED24_FMT &&
936 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
939 memset(mapcatset, 0, sizeof(mapcatset));
941 for (i = 0; i < catlen; i++) {
942 rule += SMK_DIGITLEN;
943 ret = sscanf(rule, "%u", &cat);
944 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
947 smack_catset_bit(cat, mapcatset);
950 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
952 netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat);
953 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
954 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
959 mutex_unlock(&smack_cipso_lock);
966 * smk_write_cipso - write() for /smack/cipso
967 * @file: file pointer, not actually used
968 * @buf: where to get the data from
970 * @ppos: where to start
972 * Accepts only one cipso rule per write call.
973 * Returns number of bytes written or error code, as appropriate
975 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
976 size_t count, loff_t *ppos)
978 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
981 static const struct file_operations smk_cipso_ops = {
982 .open = smk_open_cipso,
985 .write = smk_write_cipso,
986 .release = seq_release,
990 * Seq_file read operations for /smack/cipso2
994 * Print cipso labels in format:
995 * label level[/cat[,cat]]
997 static int cipso2_seq_show(struct seq_file *s, void *v)
999 struct list_head *list = v;
1000 struct smack_known *skp =
1001 list_entry(list, struct smack_known, list);
1002 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
1006 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
1008 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
1009 i = netlbl_catmap_walk(cmp, i + 1)) {
1010 seq_printf(s, "%c%d", sep, i);
1019 static const struct seq_operations cipso2_seq_ops = {
1020 .start = cipso_seq_start,
1021 .next = cipso_seq_next,
1022 .show = cipso2_seq_show,
1023 .stop = smk_seq_stop,
1027 * smk_open_cipso2 - open() for /smack/cipso2
1028 * @inode: inode structure representing file
1029 * @file: "cipso2" file pointer
1031 * Connect our cipso_seq_* operations with /smack/cipso2
1034 static int smk_open_cipso2(struct inode *inode, struct file *file)
1036 return seq_open(file, &cipso2_seq_ops);
1040 * smk_write_cipso2 - write() for /smack/cipso2
1041 * @file: file pointer, not actually used
1042 * @buf: where to get the data from
1043 * @count: bytes sent
1044 * @ppos: where to start
1046 * Accepts only one cipso rule per write call.
1047 * Returns number of bytes written or error code, as appropriate
1049 static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1050 size_t count, loff_t *ppos)
1052 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1055 static const struct file_operations smk_cipso2_ops = {
1056 .open = smk_open_cipso2,
1058 .llseek = seq_lseek,
1059 .write = smk_write_cipso2,
1060 .release = seq_release,
1064 * Seq_file read operations for /smack/netlabel
1067 static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
1069 return smk_seq_start(s, pos, &smk_netlbladdr_list);
1072 static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1074 return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
1076 #define BEBITS (sizeof(__be32) * 8)
1079 * Print host/label pairs
1081 static int netlbladdr_seq_show(struct seq_file *s, void *v)
1083 struct list_head *list = v;
1084 struct smk_netlbladdr *skp =
1085 list_entry(list, struct smk_netlbladdr, list);
1086 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
1088 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
1090 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
1092 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
1093 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label->smk_known);
1098 static const struct seq_operations netlbladdr_seq_ops = {
1099 .start = netlbladdr_seq_start,
1100 .next = netlbladdr_seq_next,
1101 .show = netlbladdr_seq_show,
1102 .stop = smk_seq_stop,
1106 * smk_open_netlbladdr - open() for /smack/netlabel
1107 * @inode: inode structure representing file
1108 * @file: "netlabel" file pointer
1110 * Connect our netlbladdr_seq_* operations with /smack/netlabel
1113 static int smk_open_netlbladdr(struct inode *inode, struct file *file)
1115 return seq_open(file, &netlbladdr_seq_ops);
1119 * smk_netlbladdr_insert
1120 * @new : netlabel to insert
1122 * This helper insert netlabel in the smack_netlbladdrs list
1123 * sorted by netmask length (longest to smallest)
1124 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
1127 static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
1129 struct smk_netlbladdr *m, *m_next;
1131 if (list_empty(&smk_netlbladdr_list)) {
1132 list_add_rcu(&new->list, &smk_netlbladdr_list);
1136 m = list_entry_rcu(smk_netlbladdr_list.next,
1137 struct smk_netlbladdr, list);
1139 /* the comparison '>' is a bit hacky, but works */
1140 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
1141 list_add_rcu(&new->list, &smk_netlbladdr_list);
1145 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
1146 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
1147 list_add_rcu(&new->list, &m->list);
1150 m_next = list_entry_rcu(m->list.next,
1151 struct smk_netlbladdr, list);
1152 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
1153 list_add_rcu(&new->list, &m->list);
1161 * smk_write_netlbladdr - write() for /smack/netlabel
1162 * @file: file pointer, not actually used
1163 * @buf: where to get the data from
1164 * @count: bytes sent
1165 * @ppos: where to start
1167 * Accepts only one netlbladdr per write call.
1168 * Returns number of bytes written or error code, as appropriate
1170 static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
1171 size_t count, loff_t *ppos)
1173 struct smk_netlbladdr *snp;
1174 struct sockaddr_in newname;
1176 struct smack_known *skp;
1178 char *host = (char *)&newname.sin_addr.s_addr;
1180 struct netlbl_audit audit_info;
1181 struct in_addr mask;
1184 u32 mask_bits = (1<<31);
1189 * Must have privilege.
1190 * No partial writes.
1191 * Enough data must be present.
1192 * "<addr/mask, as a.b.c.d/e><space><label>"
1193 * "<addr, as a.b.c.d><space><label>"
1195 if (!smack_privileged(CAP_MAC_ADMIN))
1199 if (count < SMK_NETLBLADDRMIN)
1202 data = kzalloc(count + 1, GFP_KERNEL);
1206 if (copy_from_user(data, buf, count) != 0) {
1211 smack = kzalloc(count + 1, GFP_KERNEL);
1212 if (smack == NULL) {
1219 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
1220 &host[0], &host[1], &host[2], &host[3], &m, smack);
1222 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1223 &host[0], &host[1], &host[2], &host[3], smack);
1236 * If smack begins with '-', it is an option, don't import it
1238 if (smack[0] != '-') {
1239 skp = smk_import_entry(smack, 0);
1245 /* check known options */
1246 if (strcmp(smack, smack_cipso_option.smk_known) == 0)
1247 skp = &smack_cipso_option;
1254 for (temp_mask = 0; m > 0; m--) {
1255 temp_mask |= mask_bits;
1258 mask.s_addr = cpu_to_be32(temp_mask);
1260 newname.sin_addr.s_addr &= mask.s_addr;
1262 * Only allow one writer at a time. Writes should be
1263 * quite rare and small in any case.
1265 mutex_lock(&smk_netlbladdr_lock);
1267 nsa = newname.sin_addr.s_addr;
1268 /* try to find if the prefix is already in the list */
1270 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list) {
1271 if (snp->smk_host.sin_addr.s_addr == nsa &&
1272 snp->smk_mask.s_addr == mask.s_addr) {
1277 smk_netlabel_audit_set(&audit_info);
1280 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1285 snp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
1286 snp->smk_mask.s_addr = mask.s_addr;
1287 snp->smk_label = skp;
1288 smk_netlbladdr_insert(snp);
1291 /* we delete the unlabeled entry, only if the previous label
1292 * wasn't the special CIPSO option */
1293 if (snp->smk_label != &smack_cipso_option)
1294 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1295 &snp->smk_host.sin_addr, &snp->smk_mask,
1296 PF_INET, &audit_info);
1299 snp->smk_label = skp;
1303 * Now tell netlabel about the single label nature of
1304 * this host so that incoming packets get labeled.
1305 * but only if we didn't get the special CIPSO option
1307 if (rc == 0 && skp != &smack_cipso_option)
1308 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1309 &snp->smk_host.sin_addr, &snp->smk_mask, PF_INET,
1310 snp->smk_label->smk_secid, &audit_info);
1315 mutex_unlock(&smk_netlbladdr_lock);
1325 static const struct file_operations smk_netlbladdr_ops = {
1326 .open = smk_open_netlbladdr,
1328 .llseek = seq_lseek,
1329 .write = smk_write_netlbladdr,
1330 .release = seq_release,
1334 * smk_read_doi - read() for /smack/doi
1335 * @filp: file pointer, not actually used
1336 * @buf: where to put the result
1337 * @count: maximum to send along
1338 * @ppos: where to start
1340 * Returns number of bytes read or error code, as appropriate
1342 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1343 size_t count, loff_t *ppos)
1351 sprintf(temp, "%d", smk_cipso_doi_value);
1352 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1358 * smk_write_doi - write() for /smack/doi
1359 * @file: file pointer, not actually used
1360 * @buf: where to get the data from
1361 * @count: bytes sent
1362 * @ppos: where to start
1364 * Returns number of bytes written or error code, as appropriate
1366 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1367 size_t count, loff_t *ppos)
1372 if (!smack_privileged(CAP_MAC_ADMIN))
1375 if (count >= sizeof(temp) || count == 0)
1378 if (copy_from_user(temp, buf, count) != 0)
1383 if (sscanf(temp, "%d", &i) != 1)
1386 smk_cipso_doi_value = i;
1393 static const struct file_operations smk_doi_ops = {
1394 .read = smk_read_doi,
1395 .write = smk_write_doi,
1396 .llseek = default_llseek,
1400 * smk_read_direct - read() for /smack/direct
1401 * @filp: file pointer, not actually used
1402 * @buf: where to put the result
1403 * @count: maximum to send along
1404 * @ppos: where to start
1406 * Returns number of bytes read or error code, as appropriate
1408 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1409 size_t count, loff_t *ppos)
1417 sprintf(temp, "%d", smack_cipso_direct);
1418 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1424 * smk_write_direct - write() for /smack/direct
1425 * @file: file pointer, not actually used
1426 * @buf: where to get the data from
1427 * @count: bytes sent
1428 * @ppos: where to start
1430 * Returns number of bytes written or error code, as appropriate
1432 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1433 size_t count, loff_t *ppos)
1435 struct smack_known *skp;
1439 if (!smack_privileged(CAP_MAC_ADMIN))
1442 if (count >= sizeof(temp) || count == 0)
1445 if (copy_from_user(temp, buf, count) != 0)
1450 if (sscanf(temp, "%d", &i) != 1)
1454 * Don't do anything if the value hasn't actually changed.
1455 * If it is changing reset the level on entries that were
1456 * set up to be direct when they were created.
1458 if (smack_cipso_direct != i) {
1459 mutex_lock(&smack_known_lock);
1460 list_for_each_entry_rcu(skp, &smack_known_list, list)
1461 if (skp->smk_netlabel.attr.mls.lvl ==
1463 skp->smk_netlabel.attr.mls.lvl = i;
1464 smack_cipso_direct = i;
1465 mutex_unlock(&smack_known_lock);
1471 static const struct file_operations smk_direct_ops = {
1472 .read = smk_read_direct,
1473 .write = smk_write_direct,
1474 .llseek = default_llseek,
1478 * smk_read_mapped - read() for /smack/mapped
1479 * @filp: file pointer, not actually used
1480 * @buf: where to put the result
1481 * @count: maximum to send along
1482 * @ppos: where to start
1484 * Returns number of bytes read or error code, as appropriate
1486 static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1487 size_t count, loff_t *ppos)
1495 sprintf(temp, "%d", smack_cipso_mapped);
1496 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1502 * smk_write_mapped - write() for /smack/mapped
1503 * @file: file pointer, not actually used
1504 * @buf: where to get the data from
1505 * @count: bytes sent
1506 * @ppos: where to start
1508 * Returns number of bytes written or error code, as appropriate
1510 static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1511 size_t count, loff_t *ppos)
1513 struct smack_known *skp;
1517 if (!smack_privileged(CAP_MAC_ADMIN))
1520 if (count >= sizeof(temp) || count == 0)
1523 if (copy_from_user(temp, buf, count) != 0)
1528 if (sscanf(temp, "%d", &i) != 1)
1532 * Don't do anything if the value hasn't actually changed.
1533 * If it is changing reset the level on entries that were
1534 * set up to be mapped when they were created.
1536 if (smack_cipso_mapped != i) {
1537 mutex_lock(&smack_known_lock);
1538 list_for_each_entry_rcu(skp, &smack_known_list, list)
1539 if (skp->smk_netlabel.attr.mls.lvl ==
1541 skp->smk_netlabel.attr.mls.lvl = i;
1542 smack_cipso_mapped = i;
1543 mutex_unlock(&smack_known_lock);
1549 static const struct file_operations smk_mapped_ops = {
1550 .read = smk_read_mapped,
1551 .write = smk_write_mapped,
1552 .llseek = default_llseek,
1556 * smk_read_ambient - read() for /smack/ambient
1557 * @filp: file pointer, not actually used
1558 * @buf: where to put the result
1559 * @cn: maximum to send along
1560 * @ppos: where to start
1562 * Returns number of bytes read or error code, as appropriate
1564 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1565 size_t cn, loff_t *ppos)
1573 * Being careful to avoid a problem in the case where
1574 * smack_net_ambient gets changed in midstream.
1576 mutex_lock(&smack_ambient_lock);
1578 asize = strlen(smack_net_ambient->smk_known) + 1;
1581 rc = simple_read_from_buffer(buf, cn, ppos,
1582 smack_net_ambient->smk_known,
1587 mutex_unlock(&smack_ambient_lock);
1593 * smk_write_ambient - write() for /smack/ambient
1594 * @file: file pointer, not actually used
1595 * @buf: where to get the data from
1596 * @count: bytes sent
1597 * @ppos: where to start
1599 * Returns number of bytes written or error code, as appropriate
1601 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1602 size_t count, loff_t *ppos)
1604 struct smack_known *skp;
1609 if (!smack_privileged(CAP_MAC_ADMIN))
1612 data = kzalloc(count + 1, GFP_KERNEL);
1616 if (copy_from_user(data, buf, count) != 0) {
1621 skp = smk_import_entry(data, count);
1627 mutex_lock(&smack_ambient_lock);
1629 oldambient = smack_net_ambient->smk_known;
1630 smack_net_ambient = skp;
1631 smk_unlbl_ambient(oldambient);
1633 mutex_unlock(&smack_ambient_lock);
1640 static const struct file_operations smk_ambient_ops = {
1641 .read = smk_read_ambient,
1642 .write = smk_write_ambient,
1643 .llseek = default_llseek,
1647 * smk_read_onlycap - read() for smackfs/onlycap
1648 * @filp: file pointer, not actually used
1649 * @buf: where to put the result
1650 * @cn: maximum to send along
1651 * @ppos: where to start
1653 * Returns number of bytes read or error code, as appropriate
1655 static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1656 size_t cn, loff_t *ppos)
1659 ssize_t rc = -EINVAL;
1665 if (smack_onlycap != NULL)
1666 smack = smack_onlycap->smk_known;
1668 asize = strlen(smack) + 1;
1671 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1677 * smk_write_onlycap - write() for smackfs/onlycap
1678 * @file: file pointer, not actually used
1679 * @buf: where to get the data from
1680 * @count: bytes sent
1681 * @ppos: where to start
1683 * Returns number of bytes written or error code, as appropriate
1685 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1686 size_t count, loff_t *ppos)
1689 struct smack_known *skp = smk_of_task(current->cred->security);
1692 if (!smack_privileged(CAP_MAC_ADMIN))
1696 * This can be done using smk_access() but is done
1697 * explicitly for clarity. The smk_access() implementation
1698 * would use smk_access(smack_onlycap, MAY_WRITE)
1700 if (smack_onlycap != NULL && smack_onlycap != skp)
1703 data = kzalloc(count + 1, GFP_KERNEL);
1708 * Should the null string be passed in unset the onlycap value.
1709 * This seems like something to be careful with as usually
1710 * smk_import only expects to return NULL for errors. It
1711 * is usually the case that a nullstring or "\n" would be
1712 * bad to pass to smk_import but in fact this is useful here.
1714 * smk_import will also reject a label beginning with '-',
1715 * so "-usecapabilities" will also work.
1717 if (copy_from_user(data, buf, count) != 0)
1720 smack_onlycap = smk_import_entry(data, count);
1726 static const struct file_operations smk_onlycap_ops = {
1727 .read = smk_read_onlycap,
1728 .write = smk_write_onlycap,
1729 .llseek = default_llseek,
1732 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
1734 * smk_read_unconfined - read() for smackfs/unconfined
1735 * @filp: file pointer, not actually used
1736 * @buf: where to put the result
1737 * @cn: maximum to send along
1738 * @ppos: where to start
1740 * Returns number of bytes read or error code, as appropriate
1742 static ssize_t smk_read_unconfined(struct file *filp, char __user *buf,
1743 size_t cn, loff_t *ppos)
1746 ssize_t rc = -EINVAL;
1752 if (smack_unconfined != NULL)
1753 smack = smack_unconfined->smk_known;
1755 asize = strlen(smack) + 1;
1758 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1764 * smk_write_unconfined - write() for smackfs/unconfined
1765 * @file: file pointer, not actually used
1766 * @buf: where to get the data from
1767 * @count: bytes sent
1768 * @ppos: where to start
1770 * Returns number of bytes written or error code, as appropriate
1772 static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
1773 size_t count, loff_t *ppos)
1778 if (!smack_privileged(CAP_MAC_ADMIN))
1781 data = kzalloc(count + 1, GFP_KERNEL);
1786 * Should the null string be passed in unset the unconfined value.
1787 * This seems like something to be careful with as usually
1788 * smk_import only expects to return NULL for errors. It
1789 * is usually the case that a nullstring or "\n" would be
1790 * bad to pass to smk_import but in fact this is useful here.
1792 * smk_import will also reject a label beginning with '-',
1793 * so "-confine" will also work.
1795 if (copy_from_user(data, buf, count) != 0)
1798 smack_unconfined = smk_import_entry(data, count);
1804 static const struct file_operations smk_unconfined_ops = {
1805 .read = smk_read_unconfined,
1806 .write = smk_write_unconfined,
1807 .llseek = default_llseek,
1809 #endif /* CONFIG_SECURITY_SMACK_BRINGUP */
1812 * smk_read_logging - read() for /smack/logging
1813 * @filp: file pointer, not actually used
1814 * @buf: where to put the result
1815 * @cn: maximum to send along
1816 * @ppos: where to start
1818 * Returns number of bytes read or error code, as appropriate
1820 static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1821 size_t count, loff_t *ppos)
1829 sprintf(temp, "%d\n", log_policy);
1830 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1835 * smk_write_logging - write() for /smack/logging
1836 * @file: file pointer, not actually used
1837 * @buf: where to get the data from
1838 * @count: bytes sent
1839 * @ppos: where to start
1841 * Returns number of bytes written or error code, as appropriate
1843 static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1844 size_t count, loff_t *ppos)
1849 if (!smack_privileged(CAP_MAC_ADMIN))
1852 if (count >= sizeof(temp) || count == 0)
1855 if (copy_from_user(temp, buf, count) != 0)
1860 if (sscanf(temp, "%d", &i) != 1)
1870 static const struct file_operations smk_logging_ops = {
1871 .read = smk_read_logging,
1872 .write = smk_write_logging,
1873 .llseek = default_llseek,
1877 * Seq_file read operations for /smack/load-self
1880 static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
1882 struct task_smack *tsp = current_security();
1884 return smk_seq_start(s, pos, &tsp->smk_rules);
1887 static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
1889 struct task_smack *tsp = current_security();
1891 return smk_seq_next(s, v, pos, &tsp->smk_rules);
1894 static int load_self_seq_show(struct seq_file *s, void *v)
1896 struct list_head *list = v;
1897 struct smack_rule *srp =
1898 list_entry(list, struct smack_rule, list);
1900 smk_rule_show(s, srp, SMK_LABELLEN);
1905 static const struct seq_operations load_self_seq_ops = {
1906 .start = load_self_seq_start,
1907 .next = load_self_seq_next,
1908 .show = load_self_seq_show,
1909 .stop = smk_seq_stop,
1914 * smk_open_load_self - open() for /smack/load-self2
1915 * @inode: inode structure representing file
1916 * @file: "load" file pointer
1918 * For reading, use load_seq_* seq_file reading operations.
1920 static int smk_open_load_self(struct inode *inode, struct file *file)
1922 return seq_open(file, &load_self_seq_ops);
1926 * smk_write_load_self - write() for /smack/load-self
1927 * @file: file pointer, not actually used
1928 * @buf: where to get the data from
1929 * @count: bytes sent
1930 * @ppos: where to start - must be 0
1933 static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
1934 size_t count, loff_t *ppos)
1936 struct task_smack *tsp = current_security();
1938 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
1939 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
1942 static const struct file_operations smk_load_self_ops = {
1943 .open = smk_open_load_self,
1945 .llseek = seq_lseek,
1946 .write = smk_write_load_self,
1947 .release = seq_release,
1951 * smk_user_access - handle access check transaction
1952 * @file: file pointer
1953 * @buf: data from user space
1954 * @count: bytes sent
1955 * @ppos: where to start - must be 0
1957 static ssize_t smk_user_access(struct file *file, const char __user *buf,
1958 size_t count, loff_t *ppos, int format)
1960 struct smack_parsed_rule rule;
1964 data = simple_transaction_get(file, buf, count);
1966 return PTR_ERR(data);
1968 if (format == SMK_FIXED24_FMT) {
1969 if (count < SMK_LOADLEN)
1971 res = smk_parse_rule(data, &rule, 0);
1974 * simple_transaction_get() returns null-terminated data
1976 res = smk_parse_long_rule(data, &rule, 0, 3);
1980 res = smk_access(rule.smk_subject, rule.smk_object,
1981 rule.smk_access1, NULL);
1982 else if (res != -ENOENT)
1986 * smk_access() can return a value > 0 in the "bringup" case.
1988 data[0] = res >= 0 ? '1' : '0';
1991 simple_transaction_set(file, 2);
1993 if (format == SMK_FIXED24_FMT)
1999 * smk_write_access - handle access check transaction
2000 * @file: file pointer
2001 * @buf: data from user space
2002 * @count: bytes sent
2003 * @ppos: where to start - must be 0
2005 static ssize_t smk_write_access(struct file *file, const char __user *buf,
2006 size_t count, loff_t *ppos)
2008 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
2011 static const struct file_operations smk_access_ops = {
2012 .write = smk_write_access,
2013 .read = simple_transaction_read,
2014 .release = simple_transaction_release,
2015 .llseek = generic_file_llseek,
2020 * Seq_file read operations for /smack/load2
2023 static int load2_seq_show(struct seq_file *s, void *v)
2025 struct list_head *list = v;
2026 struct smack_master_list *smlp =
2027 list_entry(list, struct smack_master_list, list);
2029 smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
2034 static const struct seq_operations load2_seq_ops = {
2035 .start = load2_seq_start,
2036 .next = load2_seq_next,
2037 .show = load2_seq_show,
2038 .stop = smk_seq_stop,
2042 * smk_open_load2 - open() for /smack/load2
2043 * @inode: inode structure representing file
2044 * @file: "load2" file pointer
2046 * For reading, use load2_seq_* seq_file reading operations.
2048 static int smk_open_load2(struct inode *inode, struct file *file)
2050 return seq_open(file, &load2_seq_ops);
2054 * smk_write_load2 - write() for /smack/load2
2055 * @file: file pointer, not actually used
2056 * @buf: where to get the data from
2057 * @count: bytes sent
2058 * @ppos: where to start - must be 0
2061 static ssize_t smk_write_load2(struct file *file, const char __user *buf,
2062 size_t count, loff_t *ppos)
2065 * Must have privilege.
2067 if (!smack_privileged(CAP_MAC_ADMIN))
2070 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2074 static const struct file_operations smk_load2_ops = {
2075 .open = smk_open_load2,
2077 .llseek = seq_lseek,
2078 .write = smk_write_load2,
2079 .release = seq_release,
2083 * Seq_file read operations for /smack/load-self2
2086 static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
2088 struct task_smack *tsp = current_security();
2090 return smk_seq_start(s, pos, &tsp->smk_rules);
2093 static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
2095 struct task_smack *tsp = current_security();
2097 return smk_seq_next(s, v, pos, &tsp->smk_rules);
2100 static int load_self2_seq_show(struct seq_file *s, void *v)
2102 struct list_head *list = v;
2103 struct smack_rule *srp =
2104 list_entry(list, struct smack_rule, list);
2106 smk_rule_show(s, srp, SMK_LONGLABEL);
2111 static const struct seq_operations load_self2_seq_ops = {
2112 .start = load_self2_seq_start,
2113 .next = load_self2_seq_next,
2114 .show = load_self2_seq_show,
2115 .stop = smk_seq_stop,
2119 * smk_open_load_self2 - open() for /smack/load-self2
2120 * @inode: inode structure representing file
2121 * @file: "load" file pointer
2123 * For reading, use load_seq_* seq_file reading operations.
2125 static int smk_open_load_self2(struct inode *inode, struct file *file)
2127 return seq_open(file, &load_self2_seq_ops);
2131 * smk_write_load_self2 - write() for /smack/load-self2
2132 * @file: file pointer, not actually used
2133 * @buf: where to get the data from
2134 * @count: bytes sent
2135 * @ppos: where to start - must be 0
2138 static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2139 size_t count, loff_t *ppos)
2141 struct task_smack *tsp = current_security();
2143 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2144 &tsp->smk_rules_lock, SMK_LONG_FMT);
2147 static const struct file_operations smk_load_self2_ops = {
2148 .open = smk_open_load_self2,
2150 .llseek = seq_lseek,
2151 .write = smk_write_load_self2,
2152 .release = seq_release,
2156 * smk_write_access2 - handle access check transaction
2157 * @file: file pointer
2158 * @buf: data from user space
2159 * @count: bytes sent
2160 * @ppos: where to start - must be 0
2162 static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2163 size_t count, loff_t *ppos)
2165 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2168 static const struct file_operations smk_access2_ops = {
2169 .write = smk_write_access2,
2170 .read = simple_transaction_read,
2171 .release = simple_transaction_release,
2172 .llseek = generic_file_llseek,
2176 * smk_write_revoke_subj - write() for /smack/revoke-subject
2177 * @file: file pointer
2178 * @buf: data from user space
2179 * @count: bytes sent
2180 * @ppos: where to start - must be 0
2182 static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2183 size_t count, loff_t *ppos)
2186 const char *cp = NULL;
2187 struct smack_known *skp;
2188 struct smack_rule *sp;
2189 struct list_head *rule_list;
2190 struct mutex *rule_lock;
2196 if (!smack_privileged(CAP_MAC_ADMIN))
2199 if (count == 0 || count > SMK_LONGLABEL)
2202 data = kzalloc(count, GFP_KERNEL);
2206 if (copy_from_user(data, buf, count) != 0) {
2211 cp = smk_parse_smack(data, count);
2217 skp = smk_find_entry(cp);
2221 rule_list = &skp->smk_rules;
2222 rule_lock = &skp->smk_rules_lock;
2224 mutex_lock(rule_lock);
2226 list_for_each_entry_rcu(sp, rule_list, list)
2229 mutex_unlock(rule_lock);
2237 static const struct file_operations smk_revoke_subj_ops = {
2238 .write = smk_write_revoke_subj,
2239 .read = simple_transaction_read,
2240 .release = simple_transaction_release,
2241 .llseek = generic_file_llseek,
2244 static struct kset *smackfs_kset;
2246 * smk_init_sysfs - initialize /sys/fs/smackfs
2249 static int smk_init_sysfs(void)
2251 smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj);
2258 * smk_write_change_rule - write() for /smack/change-rule
2259 * @file: file pointer
2260 * @buf: data from user space
2261 * @count: bytes sent
2262 * @ppos: where to start - must be 0
2264 static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2265 size_t count, loff_t *ppos)
2268 * Must have privilege.
2270 if (!smack_privileged(CAP_MAC_ADMIN))
2273 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2277 static const struct file_operations smk_change_rule_ops = {
2278 .write = smk_write_change_rule,
2279 .read = simple_transaction_read,
2280 .release = simple_transaction_release,
2281 .llseek = generic_file_llseek,
2285 * smk_read_syslog - read() for smackfs/syslog
2286 * @filp: file pointer, not actually used
2287 * @buf: where to put the result
2288 * @cn: maximum to send along
2289 * @ppos: where to start
2291 * Returns number of bytes read or error code, as appropriate
2293 static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
2294 size_t cn, loff_t *ppos)
2296 struct smack_known *skp;
2297 ssize_t rc = -EINVAL;
2303 if (smack_syslog_label == NULL)
2304 skp = &smack_known_star;
2306 skp = smack_syslog_label;
2308 asize = strlen(skp->smk_known) + 1;
2311 rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
2318 * smk_write_syslog - write() for smackfs/syslog
2319 * @file: file pointer, not actually used
2320 * @buf: where to get the data from
2321 * @count: bytes sent
2322 * @ppos: where to start
2324 * Returns number of bytes written or error code, as appropriate
2326 static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
2327 size_t count, loff_t *ppos)
2330 struct smack_known *skp;
2333 if (!smack_privileged(CAP_MAC_ADMIN))
2336 data = kzalloc(count + 1, GFP_KERNEL);
2340 if (copy_from_user(data, buf, count) != 0)
2343 skp = smk_import_entry(data, count);
2347 smack_syslog_label = smk_import_entry(data, count);
2354 static const struct file_operations smk_syslog_ops = {
2355 .read = smk_read_syslog,
2356 .write = smk_write_syslog,
2357 .llseek = default_llseek,
2362 * smk_read_ptrace - read() for /smack/ptrace
2363 * @filp: file pointer, not actually used
2364 * @buf: where to put the result
2365 * @count: maximum to send along
2366 * @ppos: where to start
2368 * Returns number of bytes read or error code, as appropriate
2370 static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
2371 size_t count, loff_t *ppos)
2379 sprintf(temp, "%d\n", smack_ptrace_rule);
2380 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2385 * smk_write_ptrace - write() for /smack/ptrace
2386 * @file: file pointer
2387 * @buf: data from user space
2388 * @count: bytes sent
2389 * @ppos: where to start - must be 0
2391 static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
2392 size_t count, loff_t *ppos)
2397 if (!smack_privileged(CAP_MAC_ADMIN))
2400 if (*ppos != 0 || count >= sizeof(temp) || count == 0)
2403 if (copy_from_user(temp, buf, count) != 0)
2408 if (sscanf(temp, "%d", &i) != 1)
2410 if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
2412 smack_ptrace_rule = i;
2417 static const struct file_operations smk_ptrace_ops = {
2418 .write = smk_write_ptrace,
2419 .read = smk_read_ptrace,
2420 .llseek = default_llseek,
2424 * smk_fill_super - fill the smackfs superblock
2425 * @sb: the empty superblock
2429 * Fill in the well known entries for the smack filesystem
2431 * Returns 0 on success, an error code on failure
2433 static int smk_fill_super(struct super_block *sb, void *data, int silent)
2436 struct inode *root_inode;
2438 static struct tree_descr smack_files[] = {
2440 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2442 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2444 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2446 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2448 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2449 [SMK_NETLBLADDR] = {
2450 "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
2452 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2454 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2456 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
2458 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
2460 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2462 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2463 [SMK_LOAD_SELF2] = {
2464 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2466 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2468 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
2469 [SMK_REVOKE_SUBJ] = {
2470 "revoke-subject", &smk_revoke_subj_ops,
2472 [SMK_CHANGE_RULE] = {
2473 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
2475 "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
2477 "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
2478 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
2479 [SMK_UNCONFINED] = {
2480 "unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR},
2486 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2488 printk(KERN_ERR "%s failed %d while creating inodes\n",
2493 root_inode = sb->s_root->d_inode;
2499 * smk_mount - get the smackfs superblock
2500 * @fs_type: passed along without comment
2501 * @flags: passed along without comment
2502 * @dev_name: passed along without comment
2503 * @data: passed along without comment
2505 * Just passes everything along.
2507 * Returns what the lower level code does.
2509 static struct dentry *smk_mount(struct file_system_type *fs_type,
2510 int flags, const char *dev_name, void *data)
2512 return mount_single(fs_type, flags, data, smk_fill_super);
2515 static struct file_system_type smk_fs_type = {
2518 .kill_sb = kill_litter_super,
2521 static struct vfsmount *smackfs_mount;
2523 static int __init smk_preset_netlabel(struct smack_known *skp)
2525 skp->smk_netlabel.domain = skp->smk_known;
2526 skp->smk_netlabel.flags =
2527 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2528 return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2529 &skp->smk_netlabel, strlen(skp->smk_known));
2533 * init_smk_fs - get the smackfs superblock
2535 * register the smackfs
2537 * Do not register smackfs if Smack wasn't enabled
2538 * on boot. We can not put this method normally under the
2539 * smack_init() code path since the security subsystem get
2540 * initialized before the vfs caches.
2542 * Returns true if we were not chosen on boot or if
2543 * we were chosen and filesystem registration succeeded.
2545 static int __init init_smk_fs(void)
2550 if (!security_module_enable(&smack_ops))
2553 err = smk_init_sysfs();
2555 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2557 err = register_filesystem(&smk_fs_type);
2559 smackfs_mount = kern_mount(&smk_fs_type);
2560 if (IS_ERR(smackfs_mount)) {
2561 printk(KERN_ERR "smackfs: could not mount!\n");
2562 err = PTR_ERR(smackfs_mount);
2563 smackfs_mount = NULL;
2568 smk_unlbl_ambient(NULL);
2570 rc = smk_preset_netlabel(&smack_known_floor);
2571 if (err == 0 && rc < 0)
2573 rc = smk_preset_netlabel(&smack_known_hat);
2574 if (err == 0 && rc < 0)
2576 rc = smk_preset_netlabel(&smack_known_huh);
2577 if (err == 0 && rc < 0)
2579 rc = smk_preset_netlabel(&smack_known_invalid);
2580 if (err == 0 && rc < 0)
2582 rc = smk_preset_netlabel(&smack_known_star);
2583 if (err == 0 && rc < 0)
2585 rc = smk_preset_netlabel(&smack_known_web);
2586 if (err == 0 && rc < 0)
2592 __initcall(init_smk_fs);