1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
6 * Casey Schaufler <casey@schaufler-ca.com>
7 * Ahmed S. Darwish <darwish.07@gmail.com>
9 * Special thanks to the authors of selinuxfs.
11 * Karl MacMillan <kmacmillan@tresys.com>
12 * James Morris <jmorris@redhat.com>
15 #include <linux/kernel.h>
16 #include <linux/vmalloc.h>
17 #include <linux/security.h>
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
20 #include <net/net_namespace.h>
21 #include <net/cipso_ipv4.h>
22 #include <linux/seq_file.h>
23 #include <linux/ctype.h>
24 #include <linux/audit.h>
25 #include <linux/magic.h>
26 #include <linux/mount.h>
27 #include <linux/fs_context.h>
30 #define BEBITS (sizeof(__be32) * 8)
32 * smackfs pseudo filesystem.
37 SMK_LOAD = 3, /* load policy */
38 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
39 SMK_DOI = 5, /* CIPSO DOI */
40 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
41 SMK_AMBIENT = 7, /* internet ambient label */
42 SMK_NET4ADDR = 8, /* single label hosts */
43 SMK_ONLYCAP = 9, /* the only "capable" label */
44 SMK_LOGGING = 10, /* logging */
45 SMK_LOAD_SELF = 11, /* task specific rules */
46 SMK_ACCESSES = 12, /* access policy */
47 SMK_MAPPED = 13, /* CIPSO level indicating mapped label */
48 SMK_LOAD2 = 14, /* load policy with long labels */
49 SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */
50 SMK_ACCESS2 = 16, /* make an access check with long labels */
51 SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */
52 SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */
53 SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */
54 SMK_SYSLOG = 20, /* change syslog label) */
55 SMK_PTRACE = 21, /* set ptrace rule */
56 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
57 SMK_UNCONFINED = 22, /* define an unconfined label */
59 #if IS_ENABLED(CONFIG_IPV6)
60 SMK_NET6ADDR = 23, /* single label IPv6 hosts */
61 #endif /* CONFIG_IPV6 */
62 SMK_RELABEL_SELF = 24, /* relabel possible without CAP_MAC_ADMIN */
68 static DEFINE_MUTEX(smack_cipso_lock);
69 static DEFINE_MUTEX(smack_ambient_lock);
70 static DEFINE_MUTEX(smk_net4addr_lock);
71 #if IS_ENABLED(CONFIG_IPV6)
72 static DEFINE_MUTEX(smk_net6addr_lock);
73 #endif /* CONFIG_IPV6 */
76 * This is the "ambient" label for network traffic.
77 * If it isn't somehow marked, use this.
78 * It can be reset via smackfs/ambient
80 struct smack_known *smack_net_ambient;
83 * This is the level in a CIPSO header that indicates a
84 * smack label is contained directly in the category set.
85 * It can be reset via smackfs/direct
87 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
90 * This is the level in a CIPSO header that indicates a
91 * secid is contained directly in the category set.
92 * It can be reset via smackfs/mapped
94 int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
96 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
98 * Allow one label to be unconfined. This is for
99 * debugging and application bring-up purposes only.
100 * It is bad and wrong, but everyone seems to expect
103 struct smack_known *smack_unconfined;
107 * If this value is set restrict syslog use to the label specified.
108 * It can be reset via smackfs/syslog
110 struct smack_known *smack_syslog_label;
113 * Ptrace current rule
114 * SMACK_PTRACE_DEFAULT regular smack ptrace rules (/proc based)
115 * SMACK_PTRACE_EXACT labels must match, but can be overriden with
117 * SMACK_PTRACE_DRACONIAN lables must match, CAP_SYS_PTRACE has no effect
119 int smack_ptrace_rule = SMACK_PTRACE_DEFAULT;
122 * Certain IP addresses may be designated as single label hosts.
123 * Packets are sent there unlabeled, but only from tasks that
124 * can write to the specified label.
127 LIST_HEAD(smk_net4addr_list);
128 #if IS_ENABLED(CONFIG_IPV6)
129 LIST_HEAD(smk_net6addr_list);
130 #endif /* CONFIG_IPV6 */
133 * Rule lists are maintained for each label.
135 struct smack_parsed_rule {
136 struct smack_known *smk_subject;
137 struct smack_known *smk_object;
142 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
145 * Values for parsing cipso rules
146 * SMK_DIGITLEN: Length of a digit field in a rule.
147 * SMK_CIPSOMIN: Minimum possible cipso rule length.
148 * SMK_CIPSOMAX: Maximum possible cipso rule length.
150 #define SMK_DIGITLEN 4
151 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
152 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
155 * Values for parsing MAC rules
156 * SMK_ACCESS: Maximum possible combination of access permissions
157 * SMK_ACCESSLEN: Maximum length for a rule access field
158 * SMK_LOADLEN: Smack rule length
160 #define SMK_OACCESS "rwxa"
161 #define SMK_ACCESS "rwxatl"
162 #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
163 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
164 #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
165 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
168 * Stricly for CIPSO level manipulation.
169 * Set the category bit number in a smack label sized buffer.
171 static inline void smack_catset_bit(unsigned int cat, char *catsetp)
173 if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
176 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
180 * smk_netlabel_audit_set - fill a netlbl_audit struct
181 * @nap: structure to fill
183 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
185 struct smack_known *skp = smk_of_current();
187 nap->loginuid = audit_get_loginuid(current);
188 nap->sessionid = audit_get_sessionid(current);
189 nap->secid = skp->smk_secid;
193 * Value for parsing single label host rules
196 #define SMK_NETLBLADDRMIN 9
199 * smk_set_access - add a rule to the rule list or replace an old rule
200 * @srp: the rule to add or replace
201 * @rule_list: the list of rules
202 * @rule_lock: the rule list lock
204 * Looks through the current subject/object/access list for
205 * the subject/object pair and replaces the access that was
206 * there. If the pair isn't found add it with the specified
209 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
210 * during the allocation of the new pair to add.
212 static int smk_set_access(struct smack_parsed_rule *srp,
213 struct list_head *rule_list,
214 struct mutex *rule_lock)
216 struct smack_rule *sp;
220 mutex_lock(rule_lock);
223 * Because the object label is less likely to match
224 * than the subject label check it first
226 list_for_each_entry_rcu(sp, rule_list, list) {
227 if (sp->smk_object == srp->smk_object &&
228 sp->smk_subject == srp->smk_subject) {
230 sp->smk_access |= srp->smk_access1;
231 sp->smk_access &= ~srp->smk_access2;
237 sp = kmem_cache_zalloc(smack_rule_cache, GFP_KERNEL);
243 sp->smk_subject = srp->smk_subject;
244 sp->smk_object = srp->smk_object;
245 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
247 list_add_rcu(&sp->list, rule_list);
251 mutex_unlock(rule_lock);
256 * smk_perm_from_str - parse smack accesses from a text string
257 * @string: a text string that contains a Smack accesses code
259 * Returns an integer with respective bits set for specified accesses.
261 static int smk_perm_from_str(const char *string)
266 for (cp = string; ; cp++)
288 perm |= MAY_TRANSMUTE;
304 * smk_fill_rule - Fill Smack rule from strings
305 * @subject: subject label string
306 * @object: object label string
307 * @access1: access string
308 * @access2: string with permissions to be removed
310 * @import: if non-zero, import labels
311 * @len: label length limit
313 * Returns 0 on success, appropriate error code on failure.
315 static int smk_fill_rule(const char *subject, const char *object,
316 const char *access1, const char *access2,
317 struct smack_parsed_rule *rule, int import,
321 struct smack_known *skp;
324 rule->smk_subject = smk_import_entry(subject, len);
325 if (IS_ERR(rule->smk_subject))
326 return PTR_ERR(rule->smk_subject);
328 rule->smk_object = smk_import_entry(object, len);
329 if (IS_ERR(rule->smk_object))
330 return PTR_ERR(rule->smk_object);
332 cp = smk_parse_smack(subject, len);
335 skp = smk_find_entry(cp);
339 rule->smk_subject = skp;
341 cp = smk_parse_smack(object, len);
344 skp = smk_find_entry(cp);
348 rule->smk_object = skp;
351 rule->smk_access1 = smk_perm_from_str(access1);
353 rule->smk_access2 = smk_perm_from_str(access2);
355 rule->smk_access2 = ~rule->smk_access1;
361 * smk_parse_rule - parse Smack rule from load string
362 * @data: string to be parsed whose size is SMK_LOADLEN
364 * @import: if non-zero, import labels
366 * Returns 0 on success, -1 on errors.
368 static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
373 rc = smk_fill_rule(data, data + SMK_LABELLEN,
374 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
375 import, SMK_LABELLEN);
380 * smk_parse_long_rule - parse Smack rule from rule string
381 * @data: string to be parsed, null terminated
382 * @rule: Will be filled with Smack parsed rule
383 * @import: if non-zero, import labels
384 * @tokens: number of substrings expected in data
386 * Returns number of processed bytes on success, -ERRNO on failure.
388 static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
389 int import, int tokens)
397 * Parsing the rule in-place, filling all white-spaces with '\0'
399 for (i = 0; i < tokens; ++i) {
400 while (isspace(data[cnt]))
403 if (data[cnt] == '\0')
404 /* Unexpected end of data */
409 while (data[cnt] && !isspace(data[cnt]))
412 while (isspace(data[cnt]))
418 rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
419 return rc == 0 ? cnt : rc;
422 #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
423 #define SMK_LONG_FMT 1 /* Variable long label format */
424 #define SMK_CHANGE_FMT 2 /* Rule modification format */
426 * smk_write_rules_list - write() for any /smack rule file
427 * @file: file pointer, not actually used
428 * @buf: where to get the data from
430 * @ppos: where to start - must be 0
431 * @rule_list: the list of rules to write to
432 * @rule_lock: lock for the rule list
433 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
435 * Get one smack access rule from above.
436 * The format for SMK_LONG_FMT is:
437 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
438 * The format for SMK_FIXED24_FMT is exactly:
439 * "subject object rwxat"
440 * The format for SMK_CHANGE_FMT is:
441 * "subject<whitespace>object<whitespace>
442 * acc_enable<whitespace>acc_disable[<whitespace>...]"
444 static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
445 size_t count, loff_t *ppos,
446 struct list_head *rule_list,
447 struct mutex *rule_lock, int format)
449 struct smack_parsed_rule rule;
458 * Enough data must be present.
463 if (format == SMK_FIXED24_FMT) {
465 * Minor hack for backward compatibility
467 if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
470 if (count >= PAGE_SIZE) {
471 count = PAGE_SIZE - 1;
476 data = memdup_user_nul(buf, count);
478 return PTR_ERR(data);
481 * In case of parsing only part of user buf,
482 * avoid having partial rule at the data buffer
485 while (count > 0 && (data[count - 1] != '\n'))
494 tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
495 while (cnt < count) {
496 if (format == SMK_FIXED24_FMT) {
497 rc = smk_parse_rule(data, &rule, 1);
502 rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
512 if (rule_list == NULL)
513 rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
514 &rule.smk_subject->smk_rules_lock);
516 rc = smk_set_access(&rule, rule_list, rule_lock);
529 * Core logic for smackfs seq list operations.
532 static void *smk_seq_start(struct seq_file *s, loff_t *pos,
533 struct list_head *head)
535 struct list_head *list;
539 for (list = rcu_dereference(list_next_rcu(head));
541 list = rcu_dereference(list_next_rcu(list))) {
549 static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
550 struct list_head *head)
552 struct list_head *list = v;
555 list = rcu_dereference(list_next_rcu(list));
557 return (list == head) ? NULL : list;
560 static void smk_seq_stop(struct seq_file *s, void *v)
565 static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
568 * Don't show any rules with label names too long for
569 * interface file (/smack/load or /smack/load2)
570 * because you should expect to be able to write
571 * anything you read back.
573 if (strlen(srp->smk_subject->smk_known) >= max ||
574 strlen(srp->smk_object->smk_known) >= max)
577 if (srp->smk_access == 0)
580 seq_printf(s, "%s %s",
581 srp->smk_subject->smk_known,
582 srp->smk_object->smk_known);
586 if (srp->smk_access & MAY_READ)
588 if (srp->smk_access & MAY_WRITE)
590 if (srp->smk_access & MAY_EXEC)
592 if (srp->smk_access & MAY_APPEND)
594 if (srp->smk_access & MAY_TRANSMUTE)
596 if (srp->smk_access & MAY_LOCK)
598 if (srp->smk_access & MAY_BRINGUP)
605 * Seq_file read operations for /smack/load
608 static void *load2_seq_start(struct seq_file *s, loff_t *pos)
610 return smk_seq_start(s, pos, &smack_known_list);
613 static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
615 return smk_seq_next(s, v, pos, &smack_known_list);
618 static int load_seq_show(struct seq_file *s, void *v)
620 struct list_head *list = v;
621 struct smack_rule *srp;
622 struct smack_known *skp =
623 list_entry_rcu(list, struct smack_known, list);
625 list_for_each_entry_rcu(srp, &skp->smk_rules, list)
626 smk_rule_show(s, srp, SMK_LABELLEN);
631 static const struct seq_operations load_seq_ops = {
632 .start = load2_seq_start,
633 .next = load2_seq_next,
634 .show = load_seq_show,
635 .stop = smk_seq_stop,
639 * smk_open_load - open() for /smack/load
640 * @inode: inode structure representing file
641 * @file: "load" file pointer
643 * For reading, use load_seq_* seq_file reading operations.
645 static int smk_open_load(struct inode *inode, struct file *file)
647 return seq_open(file, &load_seq_ops);
651 * smk_write_load - write() for /smack/load
652 * @file: file pointer, not actually used
653 * @buf: where to get the data from
655 * @ppos: where to start - must be 0
658 static ssize_t smk_write_load(struct file *file, const char __user *buf,
659 size_t count, loff_t *ppos)
662 * Must have privilege.
664 * Enough data must be present.
666 if (!smack_privileged(CAP_MAC_ADMIN))
669 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
673 static const struct file_operations smk_load_ops = {
674 .open = smk_open_load,
677 .write = smk_write_load,
678 .release = seq_release,
682 * smk_cipso_doi - initialize the CIPSO domain
684 static void smk_cipso_doi(void)
687 struct cipso_v4_doi *doip;
688 struct netlbl_audit nai;
690 smk_netlabel_audit_set(&nai);
692 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
694 printk(KERN_WARNING "%s:%d remove rc = %d\n",
695 __func__, __LINE__, rc);
697 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL | __GFP_NOFAIL);
698 doip->map.std = NULL;
699 doip->doi = smk_cipso_doi_value;
700 doip->type = CIPSO_V4_MAP_PASS;
701 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
702 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
703 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
705 rc = netlbl_cfg_cipsov4_add(doip, &nai);
707 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
708 __func__, __LINE__, rc);
712 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
714 printk(KERN_WARNING "%s:%d map add rc = %d\n",
715 __func__, __LINE__, rc);
716 netlbl_cfg_cipsov4_del(doip->doi, &nai);
722 * smk_unlbl_ambient - initialize the unlabeled domain
723 * @oldambient: previous domain string
725 static void smk_unlbl_ambient(char *oldambient)
728 struct netlbl_audit nai;
730 smk_netlabel_audit_set(&nai);
732 if (oldambient != NULL) {
733 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
735 printk(KERN_WARNING "%s:%d remove rc = %d\n",
736 __func__, __LINE__, rc);
738 if (smack_net_ambient == NULL)
739 smack_net_ambient = &smack_known_floor;
741 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
744 printk(KERN_WARNING "%s:%d add rc = %d\n",
745 __func__, __LINE__, rc);
749 * Seq_file read operations for /smack/cipso
752 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
754 return smk_seq_start(s, pos, &smack_known_list);
757 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
759 return smk_seq_next(s, v, pos, &smack_known_list);
763 * Print cipso labels in format:
764 * label level[/cat[,cat]]
766 static int cipso_seq_show(struct seq_file *s, void *v)
768 struct list_head *list = v;
769 struct smack_known *skp =
770 list_entry_rcu(list, struct smack_known, list);
771 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
776 * Don't show a label that could not have been set using
777 * /smack/cipso. This is in support of the notion that
778 * anything read from /smack/cipso ought to be writeable
781 * /smack/cipso2 should be used instead.
783 if (strlen(skp->smk_known) >= SMK_LABELLEN)
786 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
788 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
789 i = netlbl_catmap_walk(cmp, i + 1)) {
790 seq_printf(s, "%c%d", sep, i);
799 static const struct seq_operations cipso_seq_ops = {
800 .start = cipso_seq_start,
801 .next = cipso_seq_next,
802 .show = cipso_seq_show,
803 .stop = smk_seq_stop,
807 * smk_open_cipso - open() for /smack/cipso
808 * @inode: inode structure representing file
809 * @file: "cipso" file pointer
811 * Connect our cipso_seq_* operations with /smack/cipso
814 static int smk_open_cipso(struct inode *inode, struct file *file)
816 return seq_open(file, &cipso_seq_ops);
820 * smk_set_cipso - do the work for write() for cipso and cipso2
821 * @file: file pointer, not actually used
822 * @buf: where to get the data from
824 * @ppos: where to start
825 * @format: /smack/cipso or /smack/cipso2
827 * Accepts only one cipso rule per write call.
828 * Returns number of bytes written or error code, as appropriate
830 static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
831 size_t count, loff_t *ppos, int format)
833 struct netlbl_lsm_catmap *old_cat, *new_cat = NULL;
834 struct smack_known *skp;
835 struct netlbl_lsm_secattr ncats;
836 char mapcatset[SMK_CIPSOLEN];
840 ssize_t rc = -EINVAL;
847 * Must have privilege.
849 * Enough data must be present.
851 if (!smack_privileged(CAP_MAC_ADMIN))
855 if (format == SMK_FIXED24_FMT &&
856 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
858 if (count > PAGE_SIZE)
861 data = memdup_user_nul(buf, count);
863 return PTR_ERR(data);
867 * Only allow one writer at a time. Writes should be
868 * quite rare and small in any case.
870 mutex_lock(&smack_cipso_lock);
872 skp = smk_import_entry(rule, 0);
878 if (format == SMK_FIXED24_FMT)
879 rule += SMK_LABELLEN;
881 rule += strlen(skp->smk_known) + 1;
883 if (rule > data + count) {
888 ret = sscanf(rule, "%d", &maplevel);
889 if (ret != 1 || maplevel < 0 || maplevel > SMACK_CIPSO_MAXLEVEL)
892 rule += SMK_DIGITLEN;
893 if (rule > data + count) {
898 ret = sscanf(rule, "%d", &catlen);
899 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
902 if (format == SMK_FIXED24_FMT &&
903 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
906 memset(mapcatset, 0, sizeof(mapcatset));
908 for (i = 0; i < catlen; i++) {
909 rule += SMK_DIGITLEN;
910 if (rule > data + count) {
914 ret = sscanf(rule, "%u", &cat);
915 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
918 smack_catset_bit(cat, mapcatset);
922 ncats.attr.mls.cat = NULL;
923 ncats.attr.mls.lvl = maplevel;
924 new_cat = netlbl_catmap_alloc(GFP_ATOMIC);
926 new_cat->next = ncats.attr.mls.cat;
927 ncats.attr.mls.cat = new_cat;
928 skp->smk_netlabel.flags &= ~(1U << 3);
931 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
934 old_cat = skp->smk_netlabel.attr.mls.cat;
935 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
936 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
938 netlbl_catmap_free(old_cat);
941 * This mapping may have been cached, so clear the cache.
943 netlbl_cache_invalidate();
947 mutex_unlock(&smack_cipso_lock);
953 * smk_write_cipso - write() for /smack/cipso
954 * @file: file pointer, not actually used
955 * @buf: where to get the data from
957 * @ppos: where to start
959 * Accepts only one cipso rule per write call.
960 * Returns number of bytes written or error code, as appropriate
962 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
963 size_t count, loff_t *ppos)
965 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
968 static const struct file_operations smk_cipso_ops = {
969 .open = smk_open_cipso,
972 .write = smk_write_cipso,
973 .release = seq_release,
977 * Seq_file read operations for /smack/cipso2
981 * Print cipso labels in format:
982 * label level[/cat[,cat]]
984 static int cipso2_seq_show(struct seq_file *s, void *v)
986 struct list_head *list = v;
987 struct smack_known *skp =
988 list_entry_rcu(list, struct smack_known, list);
989 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
993 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
995 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
996 i = netlbl_catmap_walk(cmp, i + 1)) {
997 seq_printf(s, "%c%d", sep, i);
1006 static const struct seq_operations cipso2_seq_ops = {
1007 .start = cipso_seq_start,
1008 .next = cipso_seq_next,
1009 .show = cipso2_seq_show,
1010 .stop = smk_seq_stop,
1014 * smk_open_cipso2 - open() for /smack/cipso2
1015 * @inode: inode structure representing file
1016 * @file: "cipso2" file pointer
1018 * Connect our cipso_seq_* operations with /smack/cipso2
1021 static int smk_open_cipso2(struct inode *inode, struct file *file)
1023 return seq_open(file, &cipso2_seq_ops);
1027 * smk_write_cipso2 - write() for /smack/cipso2
1028 * @file: file pointer, not actually used
1029 * @buf: where to get the data from
1030 * @count: bytes sent
1031 * @ppos: where to start
1033 * Accepts only one cipso rule per write call.
1034 * Returns number of bytes written or error code, as appropriate
1036 static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1037 size_t count, loff_t *ppos)
1039 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1042 static const struct file_operations smk_cipso2_ops = {
1043 .open = smk_open_cipso2,
1045 .llseek = seq_lseek,
1046 .write = smk_write_cipso2,
1047 .release = seq_release,
1051 * Seq_file read operations for /smack/netlabel
1054 static void *net4addr_seq_start(struct seq_file *s, loff_t *pos)
1056 return smk_seq_start(s, pos, &smk_net4addr_list);
1059 static void *net4addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1061 return smk_seq_next(s, v, pos, &smk_net4addr_list);
1065 * Print host/label pairs
1067 static int net4addr_seq_show(struct seq_file *s, void *v)
1069 struct list_head *list = v;
1070 struct smk_net4addr *skp =
1071 list_entry_rcu(list, struct smk_net4addr, list);
1072 char *kp = SMACK_CIPSO_OPTION;
1074 if (skp->smk_label != NULL)
1075 kp = skp->smk_label->smk_known;
1076 seq_printf(s, "%pI4/%d %s\n", &skp->smk_host.s_addr,
1077 skp->smk_masks, kp);
1082 static const struct seq_operations net4addr_seq_ops = {
1083 .start = net4addr_seq_start,
1084 .next = net4addr_seq_next,
1085 .show = net4addr_seq_show,
1086 .stop = smk_seq_stop,
1090 * smk_open_net4addr - open() for /smack/netlabel
1091 * @inode: inode structure representing file
1092 * @file: "netlabel" file pointer
1094 * Connect our net4addr_seq_* operations with /smack/netlabel
1097 static int smk_open_net4addr(struct inode *inode, struct file *file)
1099 return seq_open(file, &net4addr_seq_ops);
1103 * smk_net4addr_insert
1104 * @new : netlabel to insert
1106 * This helper insert netlabel in the smack_net4addrs list
1107 * sorted by netmask length (longest to smallest)
1108 * locked by &smk_net4addr_lock in smk_write_net4addr
1111 static void smk_net4addr_insert(struct smk_net4addr *new)
1113 struct smk_net4addr *m;
1114 struct smk_net4addr *m_next;
1116 if (list_empty(&smk_net4addr_list)) {
1117 list_add_rcu(&new->list, &smk_net4addr_list);
1121 m = list_entry_rcu(smk_net4addr_list.next,
1122 struct smk_net4addr, list);
1124 /* the comparison '>' is a bit hacky, but works */
1125 if (new->smk_masks > m->smk_masks) {
1126 list_add_rcu(&new->list, &smk_net4addr_list);
1130 list_for_each_entry_rcu(m, &smk_net4addr_list, list) {
1131 if (list_is_last(&m->list, &smk_net4addr_list)) {
1132 list_add_rcu(&new->list, &m->list);
1135 m_next = list_entry_rcu(m->list.next,
1136 struct smk_net4addr, list);
1137 if (new->smk_masks > m_next->smk_masks) {
1138 list_add_rcu(&new->list, &m->list);
1146 * smk_write_net4addr - write() for /smack/netlabel
1147 * @file: file pointer, not actually used
1148 * @buf: where to get the data from
1149 * @count: bytes sent
1150 * @ppos: where to start
1152 * Accepts only one net4addr per write call.
1153 * Returns number of bytes written or error code, as appropriate
1155 static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
1156 size_t count, loff_t *ppos)
1158 struct smk_net4addr *snp;
1159 struct sockaddr_in newname;
1161 struct smack_known *skp = NULL;
1163 char *host = (char *)&newname.sin_addr.s_addr;
1165 struct netlbl_audit audit_info;
1166 struct in_addr mask;
1170 u32 mask_bits = (1<<31);
1175 * Must have privilege.
1176 * No partial writes.
1177 * Enough data must be present.
1178 * "<addr/mask, as a.b.c.d/e><space><label>"
1179 * "<addr, as a.b.c.d><space><label>"
1181 if (!smack_privileged(CAP_MAC_ADMIN))
1185 if (count < SMK_NETLBLADDRMIN || count > PAGE_SIZE - 1)
1188 data = memdup_user_nul(buf, count);
1190 return PTR_ERR(data);
1192 smack = kzalloc(count + 1, GFP_KERNEL);
1193 if (smack == NULL) {
1198 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
1199 &host[0], &host[1], &host[2], &host[3], &masks, smack);
1201 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1202 &host[0], &host[1], &host[2], &host[3], smack);
1209 if (masks > BEBITS) {
1215 * If smack begins with '-', it is an option, don't import it
1217 if (smack[0] != '-') {
1218 skp = smk_import_entry(smack, 0);
1225 * Only the -CIPSO option is supported for IPv4
1227 if (strcmp(smack, SMACK_CIPSO_OPTION) != 0) {
1233 for (m = masks, temp_mask = 0; m > 0; m--) {
1234 temp_mask |= mask_bits;
1237 mask.s_addr = cpu_to_be32(temp_mask);
1239 newname.sin_addr.s_addr &= mask.s_addr;
1241 * Only allow one writer at a time. Writes should be
1242 * quite rare and small in any case.
1244 mutex_lock(&smk_net4addr_lock);
1246 nsa = newname.sin_addr.s_addr;
1247 /* try to find if the prefix is already in the list */
1249 list_for_each_entry_rcu(snp, &smk_net4addr_list, list) {
1250 if (snp->smk_host.s_addr == nsa && snp->smk_masks == masks) {
1255 smk_netlabel_audit_set(&audit_info);
1258 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1263 snp->smk_host.s_addr = newname.sin_addr.s_addr;
1264 snp->smk_mask.s_addr = mask.s_addr;
1265 snp->smk_label = skp;
1266 snp->smk_masks = masks;
1267 smk_net4addr_insert(snp);
1271 * Delete the unlabeled entry, only if the previous label
1272 * wasn't the special CIPSO option
1274 if (snp->smk_label != NULL)
1275 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1276 &snp->smk_host, &snp->smk_mask,
1277 PF_INET, &audit_info);
1280 snp->smk_label = skp;
1284 * Now tell netlabel about the single label nature of
1285 * this host so that incoming packets get labeled.
1286 * but only if we didn't get the special CIPSO option
1288 if (rc == 0 && skp != NULL)
1289 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1290 &snp->smk_host, &snp->smk_mask, PF_INET,
1291 snp->smk_label->smk_secid, &audit_info);
1296 mutex_unlock(&smk_net4addr_lock);
1306 static const struct file_operations smk_net4addr_ops = {
1307 .open = smk_open_net4addr,
1309 .llseek = seq_lseek,
1310 .write = smk_write_net4addr,
1311 .release = seq_release,
1314 #if IS_ENABLED(CONFIG_IPV6)
1316 * Seq_file read operations for /smack/netlabel6
1319 static void *net6addr_seq_start(struct seq_file *s, loff_t *pos)
1321 return smk_seq_start(s, pos, &smk_net6addr_list);
1324 static void *net6addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1326 return smk_seq_next(s, v, pos, &smk_net6addr_list);
1330 * Print host/label pairs
1332 static int net6addr_seq_show(struct seq_file *s, void *v)
1334 struct list_head *list = v;
1335 struct smk_net6addr *skp =
1336 list_entry(list, struct smk_net6addr, list);
1338 if (skp->smk_label != NULL)
1339 seq_printf(s, "%pI6/%d %s\n", &skp->smk_host, skp->smk_masks,
1340 skp->smk_label->smk_known);
1345 static const struct seq_operations net6addr_seq_ops = {
1346 .start = net6addr_seq_start,
1347 .next = net6addr_seq_next,
1348 .show = net6addr_seq_show,
1349 .stop = smk_seq_stop,
1353 * smk_open_net6addr - open() for /smack/netlabel
1354 * @inode: inode structure representing file
1355 * @file: "netlabel" file pointer
1357 * Connect our net6addr_seq_* operations with /smack/netlabel
1360 static int smk_open_net6addr(struct inode *inode, struct file *file)
1362 return seq_open(file, &net6addr_seq_ops);
1366 * smk_net6addr_insert
1367 * @new : entry to insert
1369 * This inserts an entry in the smack_net6addrs list
1370 * sorted by netmask length (longest to smallest)
1371 * locked by &smk_net6addr_lock in smk_write_net6addr
1374 static void smk_net6addr_insert(struct smk_net6addr *new)
1376 struct smk_net6addr *m_next;
1377 struct smk_net6addr *m;
1379 if (list_empty(&smk_net6addr_list)) {
1380 list_add_rcu(&new->list, &smk_net6addr_list);
1384 m = list_entry_rcu(smk_net6addr_list.next,
1385 struct smk_net6addr, list);
1387 if (new->smk_masks > m->smk_masks) {
1388 list_add_rcu(&new->list, &smk_net6addr_list);
1392 list_for_each_entry_rcu(m, &smk_net6addr_list, list) {
1393 if (list_is_last(&m->list, &smk_net6addr_list)) {
1394 list_add_rcu(&new->list, &m->list);
1397 m_next = list_entry_rcu(m->list.next,
1398 struct smk_net6addr, list);
1399 if (new->smk_masks > m_next->smk_masks) {
1400 list_add_rcu(&new->list, &m->list);
1408 * smk_write_net6addr - write() for /smack/netlabel
1409 * @file: file pointer, not actually used
1410 * @buf: where to get the data from
1411 * @count: bytes sent
1412 * @ppos: where to start
1414 * Accepts only one net6addr per write call.
1415 * Returns number of bytes written or error code, as appropriate
1417 static ssize_t smk_write_net6addr(struct file *file, const char __user *buf,
1418 size_t count, loff_t *ppos)
1420 struct smk_net6addr *snp;
1421 struct in6_addr newname;
1422 struct in6_addr fullmask;
1423 struct smack_known *skp = NULL;
1429 unsigned int scanned[8];
1431 unsigned int mask = 128;
1434 * Must have privilege.
1435 * No partial writes.
1436 * Enough data must be present.
1437 * "<addr/mask, as a:b:c:d:e:f:g:h/e><space><label>"
1438 * "<addr, as a:b:c:d:e:f:g:h><space><label>"
1440 if (!smack_privileged(CAP_MAC_ADMIN))
1444 if (count < SMK_NETLBLADDRMIN || count > PAGE_SIZE - 1)
1447 data = memdup_user_nul(buf, count);
1449 return PTR_ERR(data);
1451 smack = kzalloc(count + 1, GFP_KERNEL);
1452 if (smack == NULL) {
1457 i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x/%u %s",
1458 &scanned[0], &scanned[1], &scanned[2], &scanned[3],
1459 &scanned[4], &scanned[5], &scanned[6], &scanned[7],
1462 i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x %s",
1463 &scanned[0], &scanned[1], &scanned[2],
1464 &scanned[3], &scanned[4], &scanned[5],
1465 &scanned[6], &scanned[7], smack);
1475 for (i = 0; i < 8; i++) {
1476 if (scanned[i] > 0xffff) {
1480 newname.s6_addr16[i] = htons(scanned[i]);
1484 * If smack begins with '-', it is an option, don't import it
1486 if (smack[0] != '-') {
1487 skp = smk_import_entry(smack, 0);
1494 * Only -DELETE is supported for IPv6
1496 if (strcmp(smack, SMACK_DELETE_OPTION) != 0) {
1502 for (i = 0, m = mask; i < 8; i++) {
1504 fullmask.s6_addr16[i] = 0xffff;
1507 fullmask.s6_addr16[i] = (1 << m) - 1;
1510 fullmask.s6_addr16[i] = 0;
1511 newname.s6_addr16[i] &= fullmask.s6_addr16[i];
1515 * Only allow one writer at a time. Writes should be
1516 * quite rare and small in any case.
1518 mutex_lock(&smk_net6addr_lock);
1520 * Try to find the prefix in the list
1522 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
1523 if (mask != snp->smk_masks)
1525 for (found = 1, i = 0; i < 8; i++) {
1526 if (newname.s6_addr16[i] !=
1527 snp->smk_host.s6_addr16[i]) {
1536 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1540 snp->smk_host = newname;
1541 snp->smk_mask = fullmask;
1542 snp->smk_masks = mask;
1543 snp->smk_label = skp;
1544 smk_net6addr_insert(snp);
1547 snp->smk_label = skp;
1553 mutex_unlock(&smk_net6addr_lock);
1563 static const struct file_operations smk_net6addr_ops = {
1564 .open = smk_open_net6addr,
1566 .llseek = seq_lseek,
1567 .write = smk_write_net6addr,
1568 .release = seq_release,
1570 #endif /* CONFIG_IPV6 */
1573 * smk_read_doi - read() for /smack/doi
1574 * @filp: file pointer, not actually used
1575 * @buf: where to put the result
1576 * @count: maximum to send along
1577 * @ppos: where to start
1579 * Returns number of bytes read or error code, as appropriate
1581 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1582 size_t count, loff_t *ppos)
1590 sprintf(temp, "%d", smk_cipso_doi_value);
1591 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1597 * smk_write_doi - write() for /smack/doi
1598 * @file: file pointer, not actually used
1599 * @buf: where to get the data from
1600 * @count: bytes sent
1601 * @ppos: where to start
1603 * Returns number of bytes written or error code, as appropriate
1605 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1606 size_t count, loff_t *ppos)
1611 if (!smack_privileged(CAP_MAC_ADMIN))
1614 if (count >= sizeof(temp) || count == 0)
1617 if (copy_from_user(temp, buf, count) != 0)
1622 if (sscanf(temp, "%d", &i) != 1)
1625 smk_cipso_doi_value = i;
1632 static const struct file_operations smk_doi_ops = {
1633 .read = smk_read_doi,
1634 .write = smk_write_doi,
1635 .llseek = default_llseek,
1639 * smk_read_direct - read() for /smack/direct
1640 * @filp: file pointer, not actually used
1641 * @buf: where to put the result
1642 * @count: maximum to send along
1643 * @ppos: where to start
1645 * Returns number of bytes read or error code, as appropriate
1647 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1648 size_t count, loff_t *ppos)
1656 sprintf(temp, "%d", smack_cipso_direct);
1657 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1663 * smk_write_direct - write() for /smack/direct
1664 * @file: file pointer, not actually used
1665 * @buf: where to get the data from
1666 * @count: bytes sent
1667 * @ppos: where to start
1669 * Returns number of bytes written or error code, as appropriate
1671 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1672 size_t count, loff_t *ppos)
1674 struct smack_known *skp;
1678 if (!smack_privileged(CAP_MAC_ADMIN))
1681 if (count >= sizeof(temp) || count == 0)
1684 if (copy_from_user(temp, buf, count) != 0)
1689 if (sscanf(temp, "%d", &i) != 1)
1693 * Don't do anything if the value hasn't actually changed.
1694 * If it is changing reset the level on entries that were
1695 * set up to be direct when they were created.
1697 if (smack_cipso_direct != i) {
1698 mutex_lock(&smack_known_lock);
1699 list_for_each_entry_rcu(skp, &smack_known_list, list)
1700 if (skp->smk_netlabel.attr.mls.lvl ==
1702 skp->smk_netlabel.attr.mls.lvl = i;
1703 smack_cipso_direct = i;
1704 mutex_unlock(&smack_known_lock);
1710 static const struct file_operations smk_direct_ops = {
1711 .read = smk_read_direct,
1712 .write = smk_write_direct,
1713 .llseek = default_llseek,
1717 * smk_read_mapped - read() for /smack/mapped
1718 * @filp: file pointer, not actually used
1719 * @buf: where to put the result
1720 * @count: maximum to send along
1721 * @ppos: where to start
1723 * Returns number of bytes read or error code, as appropriate
1725 static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1726 size_t count, loff_t *ppos)
1734 sprintf(temp, "%d", smack_cipso_mapped);
1735 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1741 * smk_write_mapped - write() for /smack/mapped
1742 * @file: file pointer, not actually used
1743 * @buf: where to get the data from
1744 * @count: bytes sent
1745 * @ppos: where to start
1747 * Returns number of bytes written or error code, as appropriate
1749 static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1750 size_t count, loff_t *ppos)
1752 struct smack_known *skp;
1756 if (!smack_privileged(CAP_MAC_ADMIN))
1759 if (count >= sizeof(temp) || count == 0)
1762 if (copy_from_user(temp, buf, count) != 0)
1767 if (sscanf(temp, "%d", &i) != 1)
1771 * Don't do anything if the value hasn't actually changed.
1772 * If it is changing reset the level on entries that were
1773 * set up to be mapped when they were created.
1775 if (smack_cipso_mapped != i) {
1776 mutex_lock(&smack_known_lock);
1777 list_for_each_entry_rcu(skp, &smack_known_list, list)
1778 if (skp->smk_netlabel.attr.mls.lvl ==
1780 skp->smk_netlabel.attr.mls.lvl = i;
1781 smack_cipso_mapped = i;
1782 mutex_unlock(&smack_known_lock);
1788 static const struct file_operations smk_mapped_ops = {
1789 .read = smk_read_mapped,
1790 .write = smk_write_mapped,
1791 .llseek = default_llseek,
1795 * smk_read_ambient - read() for /smack/ambient
1796 * @filp: file pointer, not actually used
1797 * @buf: where to put the result
1798 * @cn: maximum to send along
1799 * @ppos: where to start
1801 * Returns number of bytes read or error code, as appropriate
1803 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1804 size_t cn, loff_t *ppos)
1812 * Being careful to avoid a problem in the case where
1813 * smack_net_ambient gets changed in midstream.
1815 mutex_lock(&smack_ambient_lock);
1817 asize = strlen(smack_net_ambient->smk_known) + 1;
1820 rc = simple_read_from_buffer(buf, cn, ppos,
1821 smack_net_ambient->smk_known,
1826 mutex_unlock(&smack_ambient_lock);
1832 * smk_write_ambient - write() for /smack/ambient
1833 * @file: file pointer, not actually used
1834 * @buf: where to get the data from
1835 * @count: bytes sent
1836 * @ppos: where to start
1838 * Returns number of bytes written or error code, as appropriate
1840 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1841 size_t count, loff_t *ppos)
1843 struct smack_known *skp;
1848 if (!smack_privileged(CAP_MAC_ADMIN))
1851 /* Enough data must be present */
1852 if (count == 0 || count > PAGE_SIZE)
1855 data = memdup_user_nul(buf, count);
1857 return PTR_ERR(data);
1859 skp = smk_import_entry(data, count);
1865 mutex_lock(&smack_ambient_lock);
1867 oldambient = smack_net_ambient->smk_known;
1868 smack_net_ambient = skp;
1869 smk_unlbl_ambient(oldambient);
1871 mutex_unlock(&smack_ambient_lock);
1878 static const struct file_operations smk_ambient_ops = {
1879 .read = smk_read_ambient,
1880 .write = smk_write_ambient,
1881 .llseek = default_llseek,
1885 * Seq_file operations for /smack/onlycap
1887 static void *onlycap_seq_start(struct seq_file *s, loff_t *pos)
1889 return smk_seq_start(s, pos, &smack_onlycap_list);
1892 static void *onlycap_seq_next(struct seq_file *s, void *v, loff_t *pos)
1894 return smk_seq_next(s, v, pos, &smack_onlycap_list);
1897 static int onlycap_seq_show(struct seq_file *s, void *v)
1899 struct list_head *list = v;
1900 struct smack_known_list_elem *sklep =
1901 list_entry_rcu(list, struct smack_known_list_elem, list);
1903 seq_puts(s, sklep->smk_label->smk_known);
1909 static const struct seq_operations onlycap_seq_ops = {
1910 .start = onlycap_seq_start,
1911 .next = onlycap_seq_next,
1912 .show = onlycap_seq_show,
1913 .stop = smk_seq_stop,
1916 static int smk_open_onlycap(struct inode *inode, struct file *file)
1918 return seq_open(file, &onlycap_seq_ops);
1922 * smk_list_swap_rcu - swap public list with a private one in RCU-safe way
1923 * The caller must hold appropriate mutex to prevent concurrent modifications
1924 * to the public list.
1925 * Private list is assumed to be not accessible to other threads yet.
1927 * @public: public list
1928 * @private: private list
1930 static void smk_list_swap_rcu(struct list_head *public,
1931 struct list_head *private)
1933 struct list_head *first, *last;
1935 if (list_empty(public)) {
1936 list_splice_init_rcu(private, public, synchronize_rcu);
1938 /* Remember public list before replacing it */
1939 first = public->next;
1940 last = public->prev;
1942 /* Publish private list in place of public in RCU-safe way */
1943 private->prev->next = public;
1944 private->next->prev = public;
1945 rcu_assign_pointer(public->next, private->next);
1946 public->prev = private->prev;
1950 /* When all readers are done with the old public list,
1951 * attach it in place of private */
1952 private->next = first;
1953 private->prev = last;
1954 first->prev = private;
1955 last->next = private;
1960 * smk_parse_label_list - parse list of Smack labels, separated by spaces
1962 * @data: the string to parse
1963 * @list: destination list
1965 * Returns zero on success or error code, as appropriate
1967 static int smk_parse_label_list(char *data, struct list_head *list)
1970 struct smack_known *skp;
1971 struct smack_known_list_elem *sklep;
1973 while ((tok = strsep(&data, " ")) != NULL) {
1977 skp = smk_import_entry(tok, 0);
1979 return PTR_ERR(skp);
1981 sklep = kzalloc(sizeof(*sklep), GFP_KERNEL);
1985 sklep->smk_label = skp;
1986 list_add(&sklep->list, list);
1993 * smk_destroy_label_list - destroy a list of smack_known_list_elem
1994 * @list: header pointer of the list to destroy
1996 void smk_destroy_label_list(struct list_head *list)
1998 struct smack_known_list_elem *sklep;
1999 struct smack_known_list_elem *sklep2;
2001 list_for_each_entry_safe(sklep, sklep2, list, list)
2004 INIT_LIST_HEAD(list);
2008 * smk_write_onlycap - write() for smackfs/onlycap
2009 * @file: file pointer, not actually used
2010 * @buf: where to get the data from
2011 * @count: bytes sent
2012 * @ppos: where to start
2014 * Returns number of bytes written or error code, as appropriate
2016 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
2017 size_t count, loff_t *ppos)
2020 LIST_HEAD(list_tmp);
2023 if (!smack_privileged(CAP_MAC_ADMIN))
2026 if (count > PAGE_SIZE)
2029 data = memdup_user_nul(buf, count);
2031 return PTR_ERR(data);
2033 rc = smk_parse_label_list(data, &list_tmp);
2037 * Clear the smack_onlycap on invalid label errors. This means
2038 * that we can pass a null string to unset the onlycap value.
2040 * Importing will also reject a label beginning with '-',
2041 * so "-usecapabilities" will also work.
2043 * But do so only on invalid label, not on system errors.
2044 * The invalid label must be first to count as clearing attempt.
2046 if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
2047 mutex_lock(&smack_onlycap_lock);
2048 smk_list_swap_rcu(&smack_onlycap_list, &list_tmp);
2049 mutex_unlock(&smack_onlycap_lock);
2053 smk_destroy_label_list(&list_tmp);
2058 static const struct file_operations smk_onlycap_ops = {
2059 .open = smk_open_onlycap,
2061 .write = smk_write_onlycap,
2062 .llseek = seq_lseek,
2063 .release = seq_release,
2066 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
2068 * smk_read_unconfined - read() for smackfs/unconfined
2069 * @filp: file pointer, not actually used
2070 * @buf: where to put the result
2071 * @cn: maximum to send along
2072 * @ppos: where to start
2074 * Returns number of bytes read or error code, as appropriate
2076 static ssize_t smk_read_unconfined(struct file *filp, char __user *buf,
2077 size_t cn, loff_t *ppos)
2080 ssize_t rc = -EINVAL;
2086 if (smack_unconfined != NULL)
2087 smack = smack_unconfined->smk_known;
2089 asize = strlen(smack) + 1;
2092 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
2098 * smk_write_unconfined - write() for smackfs/unconfined
2099 * @file: file pointer, not actually used
2100 * @buf: where to get the data from
2101 * @count: bytes sent
2102 * @ppos: where to start
2104 * Returns number of bytes written or error code, as appropriate
2106 static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
2107 size_t count, loff_t *ppos)
2110 struct smack_known *skp;
2113 if (!smack_privileged(CAP_MAC_ADMIN))
2116 if (count > PAGE_SIZE)
2119 data = memdup_user_nul(buf, count);
2121 return PTR_ERR(data);
2124 * Clear the smack_unconfined on invalid label errors. This means
2125 * that we can pass a null string to unset the unconfined value.
2127 * Importing will also reject a label beginning with '-',
2128 * so "-confine" will also work.
2130 * But do so only on invalid label, not on system errors.
2132 skp = smk_import_entry(data, count);
2133 if (PTR_ERR(skp) == -EINVAL)
2135 else if (IS_ERR(skp)) {
2140 smack_unconfined = skp;
2147 static const struct file_operations smk_unconfined_ops = {
2148 .read = smk_read_unconfined,
2149 .write = smk_write_unconfined,
2150 .llseek = default_llseek,
2152 #endif /* CONFIG_SECURITY_SMACK_BRINGUP */
2155 * smk_read_logging - read() for /smack/logging
2156 * @filp: file pointer, not actually used
2157 * @buf: where to put the result
2158 * @count: maximum to send along
2159 * @ppos: where to start
2161 * Returns number of bytes read or error code, as appropriate
2163 static ssize_t smk_read_logging(struct file *filp, char __user *buf,
2164 size_t count, loff_t *ppos)
2172 sprintf(temp, "%d\n", log_policy);
2173 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2178 * smk_write_logging - write() for /smack/logging
2179 * @file: file pointer, not actually used
2180 * @buf: where to get the data from
2181 * @count: bytes sent
2182 * @ppos: where to start
2184 * Returns number of bytes written or error code, as appropriate
2186 static ssize_t smk_write_logging(struct file *file, const char __user *buf,
2187 size_t count, loff_t *ppos)
2192 if (!smack_privileged(CAP_MAC_ADMIN))
2195 if (count >= sizeof(temp) || count == 0)
2198 if (copy_from_user(temp, buf, count) != 0)
2203 if (sscanf(temp, "%d", &i) != 1)
2213 static const struct file_operations smk_logging_ops = {
2214 .read = smk_read_logging,
2215 .write = smk_write_logging,
2216 .llseek = default_llseek,
2220 * Seq_file read operations for /smack/load-self
2223 static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
2225 struct task_smack *tsp = smack_cred(current_cred());
2227 return smk_seq_start(s, pos, &tsp->smk_rules);
2230 static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
2232 struct task_smack *tsp = smack_cred(current_cred());
2234 return smk_seq_next(s, v, pos, &tsp->smk_rules);
2237 static int load_self_seq_show(struct seq_file *s, void *v)
2239 struct list_head *list = v;
2240 struct smack_rule *srp =
2241 list_entry_rcu(list, struct smack_rule, list);
2243 smk_rule_show(s, srp, SMK_LABELLEN);
2248 static const struct seq_operations load_self_seq_ops = {
2249 .start = load_self_seq_start,
2250 .next = load_self_seq_next,
2251 .show = load_self_seq_show,
2252 .stop = smk_seq_stop,
2257 * smk_open_load_self - open() for /smack/load-self2
2258 * @inode: inode structure representing file
2259 * @file: "load" file pointer
2261 * For reading, use load_seq_* seq_file reading operations.
2263 static int smk_open_load_self(struct inode *inode, struct file *file)
2265 return seq_open(file, &load_self_seq_ops);
2269 * smk_write_load_self - write() for /smack/load-self
2270 * @file: file pointer, not actually used
2271 * @buf: where to get the data from
2272 * @count: bytes sent
2273 * @ppos: where to start - must be 0
2276 static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
2277 size_t count, loff_t *ppos)
2279 struct task_smack *tsp = smack_cred(current_cred());
2281 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2282 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
2285 static const struct file_operations smk_load_self_ops = {
2286 .open = smk_open_load_self,
2288 .llseek = seq_lseek,
2289 .write = smk_write_load_self,
2290 .release = seq_release,
2294 * smk_user_access - handle access check transaction
2295 * @file: file pointer
2296 * @buf: data from user space
2297 * @count: bytes sent
2298 * @ppos: where to start - must be 0
2299 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
2301 static ssize_t smk_user_access(struct file *file, const char __user *buf,
2302 size_t count, loff_t *ppos, int format)
2304 struct smack_parsed_rule rule;
2308 data = simple_transaction_get(file, buf, count);
2310 return PTR_ERR(data);
2312 if (format == SMK_FIXED24_FMT) {
2313 if (count < SMK_LOADLEN)
2315 res = smk_parse_rule(data, &rule, 0);
2318 * simple_transaction_get() returns null-terminated data
2320 res = smk_parse_long_rule(data, &rule, 0, 3);
2324 res = smk_access(rule.smk_subject, rule.smk_object,
2325 rule.smk_access1, NULL);
2326 else if (res != -ENOENT)
2330 * smk_access() can return a value > 0 in the "bringup" case.
2332 data[0] = res >= 0 ? '1' : '0';
2335 simple_transaction_set(file, 2);
2337 if (format == SMK_FIXED24_FMT)
2343 * smk_write_access - handle access check transaction
2344 * @file: file pointer
2345 * @buf: data from user space
2346 * @count: bytes sent
2347 * @ppos: where to start - must be 0
2349 static ssize_t smk_write_access(struct file *file, const char __user *buf,
2350 size_t count, loff_t *ppos)
2352 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
2355 static const struct file_operations smk_access_ops = {
2356 .write = smk_write_access,
2357 .read = simple_transaction_read,
2358 .release = simple_transaction_release,
2359 .llseek = generic_file_llseek,
2364 * Seq_file read operations for /smack/load2
2367 static int load2_seq_show(struct seq_file *s, void *v)
2369 struct list_head *list = v;
2370 struct smack_rule *srp;
2371 struct smack_known *skp =
2372 list_entry_rcu(list, struct smack_known, list);
2374 list_for_each_entry_rcu(srp, &skp->smk_rules, list)
2375 smk_rule_show(s, srp, SMK_LONGLABEL);
2380 static const struct seq_operations load2_seq_ops = {
2381 .start = load2_seq_start,
2382 .next = load2_seq_next,
2383 .show = load2_seq_show,
2384 .stop = smk_seq_stop,
2388 * smk_open_load2 - open() for /smack/load2
2389 * @inode: inode structure representing file
2390 * @file: "load2" file pointer
2392 * For reading, use load2_seq_* seq_file reading operations.
2394 static int smk_open_load2(struct inode *inode, struct file *file)
2396 return seq_open(file, &load2_seq_ops);
2400 * smk_write_load2 - write() for /smack/load2
2401 * @file: file pointer, not actually used
2402 * @buf: where to get the data from
2403 * @count: bytes sent
2404 * @ppos: where to start - must be 0
2407 static ssize_t smk_write_load2(struct file *file, const char __user *buf,
2408 size_t count, loff_t *ppos)
2411 * Must have privilege.
2413 if (!smack_privileged(CAP_MAC_ADMIN))
2416 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2420 static const struct file_operations smk_load2_ops = {
2421 .open = smk_open_load2,
2423 .llseek = seq_lseek,
2424 .write = smk_write_load2,
2425 .release = seq_release,
2429 * Seq_file read operations for /smack/load-self2
2432 static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
2434 struct task_smack *tsp = smack_cred(current_cred());
2436 return smk_seq_start(s, pos, &tsp->smk_rules);
2439 static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
2441 struct task_smack *tsp = smack_cred(current_cred());
2443 return smk_seq_next(s, v, pos, &tsp->smk_rules);
2446 static int load_self2_seq_show(struct seq_file *s, void *v)
2448 struct list_head *list = v;
2449 struct smack_rule *srp =
2450 list_entry_rcu(list, struct smack_rule, list);
2452 smk_rule_show(s, srp, SMK_LONGLABEL);
2457 static const struct seq_operations load_self2_seq_ops = {
2458 .start = load_self2_seq_start,
2459 .next = load_self2_seq_next,
2460 .show = load_self2_seq_show,
2461 .stop = smk_seq_stop,
2465 * smk_open_load_self2 - open() for /smack/load-self2
2466 * @inode: inode structure representing file
2467 * @file: "load" file pointer
2469 * For reading, use load_seq_* seq_file reading operations.
2471 static int smk_open_load_self2(struct inode *inode, struct file *file)
2473 return seq_open(file, &load_self2_seq_ops);
2477 * smk_write_load_self2 - write() for /smack/load-self2
2478 * @file: file pointer, not actually used
2479 * @buf: where to get the data from
2480 * @count: bytes sent
2481 * @ppos: where to start - must be 0
2484 static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2485 size_t count, loff_t *ppos)
2487 struct task_smack *tsp = smack_cred(current_cred());
2489 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2490 &tsp->smk_rules_lock, SMK_LONG_FMT);
2493 static const struct file_operations smk_load_self2_ops = {
2494 .open = smk_open_load_self2,
2496 .llseek = seq_lseek,
2497 .write = smk_write_load_self2,
2498 .release = seq_release,
2502 * smk_write_access2 - handle access check transaction
2503 * @file: file pointer
2504 * @buf: data from user space
2505 * @count: bytes sent
2506 * @ppos: where to start - must be 0
2508 static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2509 size_t count, loff_t *ppos)
2511 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2514 static const struct file_operations smk_access2_ops = {
2515 .write = smk_write_access2,
2516 .read = simple_transaction_read,
2517 .release = simple_transaction_release,
2518 .llseek = generic_file_llseek,
2522 * smk_write_revoke_subj - write() for /smack/revoke-subject
2523 * @file: file pointer
2524 * @buf: data from user space
2525 * @count: bytes sent
2526 * @ppos: where to start - must be 0
2528 static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2529 size_t count, loff_t *ppos)
2533 struct smack_known *skp;
2534 struct smack_rule *sp;
2535 struct list_head *rule_list;
2536 struct mutex *rule_lock;
2542 if (!smack_privileged(CAP_MAC_ADMIN))
2545 if (count == 0 || count > SMK_LONGLABEL)
2548 data = memdup_user(buf, count);
2550 return PTR_ERR(data);
2552 cp = smk_parse_smack(data, count);
2558 skp = smk_find_entry(cp);
2562 rule_list = &skp->smk_rules;
2563 rule_lock = &skp->smk_rules_lock;
2565 mutex_lock(rule_lock);
2567 list_for_each_entry_rcu(sp, rule_list, list)
2570 mutex_unlock(rule_lock);
2580 static const struct file_operations smk_revoke_subj_ops = {
2581 .write = smk_write_revoke_subj,
2582 .read = simple_transaction_read,
2583 .release = simple_transaction_release,
2584 .llseek = generic_file_llseek,
2588 * smk_init_sysfs - initialize /sys/fs/smackfs
2591 static int smk_init_sysfs(void)
2593 return sysfs_create_mount_point(fs_kobj, "smackfs");
2597 * smk_write_change_rule - write() for /smack/change-rule
2598 * @file: file pointer
2599 * @buf: data from user space
2600 * @count: bytes sent
2601 * @ppos: where to start - must be 0
2603 static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2604 size_t count, loff_t *ppos)
2607 * Must have privilege.
2609 if (!smack_privileged(CAP_MAC_ADMIN))
2612 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2616 static const struct file_operations smk_change_rule_ops = {
2617 .write = smk_write_change_rule,
2618 .read = simple_transaction_read,
2619 .release = simple_transaction_release,
2620 .llseek = generic_file_llseek,
2624 * smk_read_syslog - read() for smackfs/syslog
2625 * @filp: file pointer, not actually used
2626 * @buf: where to put the result
2627 * @cn: maximum to send along
2628 * @ppos: where to start
2630 * Returns number of bytes read or error code, as appropriate
2632 static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
2633 size_t cn, loff_t *ppos)
2635 struct smack_known *skp;
2636 ssize_t rc = -EINVAL;
2642 if (smack_syslog_label == NULL)
2643 skp = &smack_known_star;
2645 skp = smack_syslog_label;
2647 asize = strlen(skp->smk_known) + 1;
2650 rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
2657 * smk_write_syslog - write() for smackfs/syslog
2658 * @file: file pointer, not actually used
2659 * @buf: where to get the data from
2660 * @count: bytes sent
2661 * @ppos: where to start
2663 * Returns number of bytes written or error code, as appropriate
2665 static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
2666 size_t count, loff_t *ppos)
2669 struct smack_known *skp;
2672 if (!smack_privileged(CAP_MAC_ADMIN))
2675 /* Enough data must be present */
2676 if (count == 0 || count > PAGE_SIZE)
2679 data = memdup_user_nul(buf, count);
2681 return PTR_ERR(data);
2683 skp = smk_import_entry(data, count);
2687 smack_syslog_label = skp;
2693 static const struct file_operations smk_syslog_ops = {
2694 .read = smk_read_syslog,
2695 .write = smk_write_syslog,
2696 .llseek = default_llseek,
2700 * Seq_file read operations for /smack/relabel-self
2703 static void *relabel_self_seq_start(struct seq_file *s, loff_t *pos)
2705 struct task_smack *tsp = smack_cred(current_cred());
2707 return smk_seq_start(s, pos, &tsp->smk_relabel);
2710 static void *relabel_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
2712 struct task_smack *tsp = smack_cred(current_cred());
2714 return smk_seq_next(s, v, pos, &tsp->smk_relabel);
2717 static int relabel_self_seq_show(struct seq_file *s, void *v)
2719 struct list_head *list = v;
2720 struct smack_known_list_elem *sklep =
2721 list_entry(list, struct smack_known_list_elem, list);
2723 seq_puts(s, sklep->smk_label->smk_known);
2729 static const struct seq_operations relabel_self_seq_ops = {
2730 .start = relabel_self_seq_start,
2731 .next = relabel_self_seq_next,
2732 .show = relabel_self_seq_show,
2733 .stop = smk_seq_stop,
2737 * smk_open_relabel_self - open() for /smack/relabel-self
2738 * @inode: inode structure representing file
2739 * @file: "relabel-self" file pointer
2741 * Connect our relabel_self_seq_* operations with /smack/relabel-self
2744 static int smk_open_relabel_self(struct inode *inode, struct file *file)
2746 return seq_open(file, &relabel_self_seq_ops);
2750 * smk_write_relabel_self - write() for /smack/relabel-self
2751 * @file: file pointer, not actually used
2752 * @buf: where to get the data from
2753 * @count: bytes sent
2754 * @ppos: where to start - must be 0
2757 static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
2758 size_t count, loff_t *ppos)
2762 LIST_HEAD(list_tmp);
2765 * Must have privilege.
2767 if (!smack_privileged(CAP_MAC_ADMIN))
2772 * Enough data must be present.
2776 if (count == 0 || count > PAGE_SIZE)
2779 data = memdup_user_nul(buf, count);
2781 return PTR_ERR(data);
2783 rc = smk_parse_label_list(data, &list_tmp);
2786 if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
2788 struct task_smack *tsp;
2790 new = prepare_creds();
2795 tsp = smack_cred(new);
2796 smk_destroy_label_list(&tsp->smk_relabel);
2797 list_splice(&list_tmp, &tsp->smk_relabel);
2802 smk_destroy_label_list(&list_tmp);
2806 static const struct file_operations smk_relabel_self_ops = {
2807 .open = smk_open_relabel_self,
2809 .llseek = seq_lseek,
2810 .write = smk_write_relabel_self,
2811 .release = seq_release,
2815 * smk_read_ptrace - read() for /smack/ptrace
2816 * @filp: file pointer, not actually used
2817 * @buf: where to put the result
2818 * @count: maximum to send along
2819 * @ppos: where to start
2821 * Returns number of bytes read or error code, as appropriate
2823 static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
2824 size_t count, loff_t *ppos)
2832 sprintf(temp, "%d\n", smack_ptrace_rule);
2833 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2838 * smk_write_ptrace - write() for /smack/ptrace
2839 * @file: file pointer
2840 * @buf: data from user space
2841 * @count: bytes sent
2842 * @ppos: where to start - must be 0
2844 static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
2845 size_t count, loff_t *ppos)
2850 if (!smack_privileged(CAP_MAC_ADMIN))
2853 if (*ppos != 0 || count >= sizeof(temp) || count == 0)
2856 if (copy_from_user(temp, buf, count) != 0)
2861 if (sscanf(temp, "%d", &i) != 1)
2863 if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
2865 smack_ptrace_rule = i;
2870 static const struct file_operations smk_ptrace_ops = {
2871 .write = smk_write_ptrace,
2872 .read = smk_read_ptrace,
2873 .llseek = default_llseek,
2877 * smk_fill_super - fill the smackfs superblock
2878 * @sb: the empty superblock
2881 * Fill in the well known entries for the smack filesystem
2883 * Returns 0 on success, an error code on failure
2885 static int smk_fill_super(struct super_block *sb, struct fs_context *fc)
2889 static const struct tree_descr smack_files[] = {
2891 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2893 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2895 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2897 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2899 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2901 "netlabel", &smk_net4addr_ops, S_IRUGO|S_IWUSR},
2903 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2905 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2907 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
2909 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
2911 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2913 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2914 [SMK_LOAD_SELF2] = {
2915 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2917 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2919 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
2920 [SMK_REVOKE_SUBJ] = {
2921 "revoke-subject", &smk_revoke_subj_ops,
2923 [SMK_CHANGE_RULE] = {
2924 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
2926 "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
2928 "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
2929 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
2930 [SMK_UNCONFINED] = {
2931 "unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR},
2933 #if IS_ENABLED(CONFIG_IPV6)
2935 "ipv6host", &smk_net6addr_ops, S_IRUGO|S_IWUSR},
2936 #endif /* CONFIG_IPV6 */
2937 [SMK_RELABEL_SELF] = {
2938 "relabel-self", &smk_relabel_self_ops,
2944 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2946 printk(KERN_ERR "%s failed %d while creating inodes\n",
2955 * smk_get_tree - get the smackfs superblock
2956 * @fc: The mount context, including any options
2958 * Just passes everything along.
2960 * Returns what the lower level code does.
2962 static int smk_get_tree(struct fs_context *fc)
2964 return get_tree_single(fc, smk_fill_super);
2967 static const struct fs_context_operations smk_context_ops = {
2968 .get_tree = smk_get_tree,
2972 * smk_init_fs_context - Initialise a filesystem context for smackfs
2973 * @fc: The blank mount context
2975 static int smk_init_fs_context(struct fs_context *fc)
2977 fc->ops = &smk_context_ops;
2981 static struct file_system_type smk_fs_type = {
2983 .init_fs_context = smk_init_fs_context,
2984 .kill_sb = kill_litter_super,
2987 static struct vfsmount *smackfs_mount;
2990 * init_smk_fs - get the smackfs superblock
2992 * register the smackfs
2994 * Do not register smackfs if Smack wasn't enabled
2995 * on boot. We can not put this method normally under the
2996 * smack_init() code path since the security subsystem get
2997 * initialized before the vfs caches.
2999 * Returns true if we were not chosen on boot or if
3000 * we were chosen and filesystem registration succeeded.
3002 static int __init init_smk_fs(void)
3007 if (smack_enabled == 0)
3010 err = smk_init_sysfs();
3012 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
3014 err = register_filesystem(&smk_fs_type);
3016 smackfs_mount = kern_mount(&smk_fs_type);
3017 if (IS_ERR(smackfs_mount)) {
3018 printk(KERN_ERR "smackfs: could not mount!\n");
3019 err = PTR_ERR(smackfs_mount);
3020 smackfs_mount = NULL;
3025 smk_unlbl_ambient(NULL);
3027 rc = smack_populate_secattr(&smack_known_floor);
3028 if (err == 0 && rc < 0)
3030 rc = smack_populate_secattr(&smack_known_hat);
3031 if (err == 0 && rc < 0)
3033 rc = smack_populate_secattr(&smack_known_huh);
3034 if (err == 0 && rc < 0)
3036 rc = smack_populate_secattr(&smack_known_star);
3037 if (err == 0 && rc < 0)
3039 rc = smack_populate_secattr(&smack_known_web);
3040 if (err == 0 && rc < 0)
3046 __initcall(init_smk_fs);