xref: /linux/security/smack/smackfs.c (revision cbad8981fa3b1fc726e6e5652e92fd76c01e543c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
4  *
5  * Authors:
6  * 	Casey Schaufler <casey@schaufler-ca.com>
7  * 	Ahmed S. Darwish <darwish.07@gmail.com>
8  *
9  * Special thanks to the authors of selinuxfs.
10  *
11  *	Karl MacMillan <kmacmillan@tresys.com>
12  *	James Morris <jmorris@redhat.com>
13  */
14 
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>
28 #include "smack.h"
29 
30 #define BEBITS	(sizeof(__be32) * 8)
31 /*
32  * smackfs pseudo filesystem.
33  */
34 
35 enum smk_inos {
36 	SMK_ROOT_INO	= 2,
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 #ifdef CONFIG_AUDIT
45 	SMK_LOGGING	= 10,	/* logging */
46 #endif /* CONFIG_AUDIT */
47 	SMK_LOAD_SELF	= 11,	/* task specific rules */
48 	SMK_ACCESSES	= 12,	/* access policy */
49 	SMK_MAPPED	= 13,	/* CIPSO level indicating mapped label */
50 	SMK_LOAD2	= 14,	/* load policy with long labels */
51 	SMK_LOAD_SELF2	= 15,	/* load task specific rules with long labels */
52 	SMK_ACCESS2	= 16,	/* make an access check with long labels */
53 	SMK_CIPSO2	= 17,	/* load long label -> CIPSO mapping */
54 	SMK_REVOKE_SUBJ	= 18,	/* set rules with subject label to '-' */
55 	SMK_CHANGE_RULE	= 19,	/* change or add rules (long labels) */
56 	SMK_SYSLOG	= 20,	/* change syslog label) */
57 	SMK_PTRACE	= 21,	/* set ptrace rule */
58 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
59 	SMK_UNCONFINED	= 22,	/* define an unconfined label */
60 #endif
61 #if IS_ENABLED(CONFIG_IPV6)
62 	SMK_NET6ADDR	= 23,	/* single label IPv6 hosts */
63 #endif /* CONFIG_IPV6 */
64 	SMK_RELABEL_SELF = 24, /* relabel possible without CAP_MAC_ADMIN */
65 };
66 
67 /*
68  * List locks
69  */
70 static DEFINE_MUTEX(smack_cipso_lock);
71 static DEFINE_MUTEX(smack_ambient_lock);
72 static DEFINE_MUTEX(smk_net4addr_lock);
73 static DEFINE_MUTEX(smk_cipso_doi_lock);
74 #if IS_ENABLED(CONFIG_IPV6)
75 static DEFINE_MUTEX(smk_net6addr_lock);
76 #endif /* CONFIG_IPV6 */
77 
78 /*
79  * This is the "ambient" label for network traffic.
80  * If it isn't somehow marked, use this.
81  * It can be reset via smackfs/ambient
82  */
83 struct smack_known *smack_net_ambient;
84 
85 /*
86  * Sensitivity levels for automatically created CIPSO labels.
87  * See smack_access.c`smack_populate_secattr()
88  *
89  * [0] "direct" labeling, label length < SMK_CIPSOLEN(24):
90  * smack label is contained directly in the category set.
91  * It can be reset via smackfs/direct
92  *
93  * [1] "mapped" labeling, label length >= SMK_CIPSOLEN(24):
94  * secid is contained directly in the category set.
95  * It can be reset via smackfs/mapped
96  */
97 u8 smack_cipso_auto_level[2] = {
98 	SMACK_CIPSO_DIRECT_DEFAULT,
99 	SMACK_CIPSO_MAPPED_DEFAULT,
100 };
101 
102 static int
103 smk_cipso_auto_level_idx(const struct file *file)
104 {
105 	return (file_inode(file)->i_ino != SMK_DIRECT);
106 }
107 
108 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
109 /*
110  * Allow one label to be unconfined. This is for
111  * debugging and application bring-up purposes only.
112  * It is bad and wrong, but everyone seems to expect
113  * to have it.
114  */
115 struct smack_known *smack_unconfined;
116 #endif
117 
118 /*
119  * If this value is set restrict syslog use to the label specified.
120  * It can be reset via smackfs/syslog
121  */
122 struct smack_known *smack_syslog_label;
123 
124 /*
125  * Ptrace current rule
126  * SMACK_PTRACE_DEFAULT    regular smack ptrace rules (/proc based)
127  * SMACK_PTRACE_EXACT      labels must match, but can be overridden with
128  *			   CAP_SYS_PTRACE
129  * SMACK_PTRACE_DRACONIAN  labels must match, CAP_SYS_PTRACE has no effect
130  */
131 int smack_ptrace_rule = SMACK_PTRACE_DEFAULT;
132 
133 /*
134  * Certain IP addresses may be designated as single label hosts.
135  * Packets are sent there unlabeled, but only from tasks that
136  * can write to the specified label.
137  */
138 
139 LIST_HEAD(smk_net4addr_list);
140 #if IS_ENABLED(CONFIG_IPV6)
141 LIST_HEAD(smk_net6addr_list);
142 #endif /* CONFIG_IPV6 */
143 
144 /*
145  * Rule lists are maintained for each label.
146  */
147 struct smack_parsed_rule {
148 	struct smack_known	*smk_subject;
149 	struct smack_known	*smk_object;
150 	int			smk_access1;
151 	int			smk_access2;
152 };
153 
154 static u32 smk_cipso_doi_value = CIPSO_V4_DOI_UNKNOWN;
155 
156 /*
157  * Values for parsing cipso rules
158  * SMK_DIGITLEN: Length of a digit field in a rule.
159  * SMK_CIPSOMIN: Minimum possible cipso rule length.
160  * SMK_CIPSOMAX: Maximum possible cipso rule length.
161  */
162 #define SMK_DIGITLEN 4
163 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
164 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
165 
166 /*
167  * Values for parsing MAC rules
168  * SMK_ACCESS: Maximum possible combination of access permissions
169  * SMK_ACCESSLEN: Maximum length for a rule access field
170  * SMK_LOADLEN: Smack rule length
171  */
172 #define SMK_OACCESS	"rwxa"
173 #define SMK_ACCESS	"rwxatl"
174 #define SMK_OACCESSLEN	(sizeof(SMK_OACCESS) - 1)
175 #define SMK_ACCESSLEN	(sizeof(SMK_ACCESS) - 1)
176 #define SMK_OLOADLEN	(SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
177 #define SMK_LOADLEN	(SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
178 
179 /*
180  * Strictly for CIPSO level manipulation.
181  * Set the category bit number in a smack label sized buffer.
182  */
183 static inline void smack_catset_bit(unsigned int cat, char *catsetp)
184 {
185 	if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
186 		return;
187 
188 	catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
189 }
190 
191 /**
192  * smk_netlabel_audit_set - fill a netlbl_audit struct
193  * @nap: structure to fill
194  */
195 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
196 {
197 	nap->loginuid = audit_get_loginuid(current);
198 	nap->sessionid = audit_get_sessionid(current);
199 	nap->prop.smack.skp = smk_of_current();
200 }
201 
202 /*
203  * Value for parsing single label host rules
204  * "1.2.3.4 X"
205  */
206 #define SMK_NETLBLADDRMIN	9
207 
208 /**
209  * smk_set_access - add a rule to the rule list or replace an old rule
210  * @srp: the rule to add or replace
211  * @rule_list: the list of rules
212  * @rule_lock: the rule list lock
213  *
214  * Looks through the current subject/object/access list for
215  * the subject/object pair and replaces the access that was
216  * there. If the pair isn't found add it with the specified
217  * access.
218  *
219  * Returns 0 if nothing goes wrong or -ENOMEM if it fails
220  * during the allocation of the new pair to add.
221  */
222 static int smk_set_access(struct smack_parsed_rule *srp,
223 				struct list_head *rule_list,
224 				struct mutex *rule_lock)
225 {
226 	struct smack_rule *sp;
227 	int found = 0;
228 	int rc = 0;
229 
230 	mutex_lock(rule_lock);
231 
232 	/*
233 	 * Because the object label is less likely to match
234 	 * than the subject label check it first
235 	 */
236 	list_for_each_entry_rcu(sp, rule_list, list) {
237 		if (sp->smk_object == srp->smk_object &&
238 		    sp->smk_subject == srp->smk_subject) {
239 			found = 1;
240 			sp->smk_access |= srp->smk_access1;
241 			sp->smk_access &= ~srp->smk_access2;
242 			break;
243 		}
244 	}
245 
246 	if (found == 0) {
247 		sp = kmem_cache_zalloc(smack_rule_cache, GFP_KERNEL);
248 		if (sp == NULL) {
249 			rc = -ENOMEM;
250 			goto out;
251 		}
252 
253 		sp->smk_subject = srp->smk_subject;
254 		sp->smk_object = srp->smk_object;
255 		sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
256 
257 		list_add_rcu(&sp->list, rule_list);
258 	}
259 
260 out:
261 	mutex_unlock(rule_lock);
262 	return rc;
263 }
264 
265 /**
266  * smk_perm_from_str - parse smack accesses from a text string
267  * @string: a text string that contains a Smack accesses code
268  *
269  * Returns an integer with respective bits set for specified accesses.
270  */
271 static int smk_perm_from_str(const char *string)
272 {
273 	int perm = 0;
274 	const char *cp;
275 
276 	for (cp = string; ; cp++)
277 		switch (*cp) {
278 		case '-':
279 			break;
280 		case 'r':
281 		case 'R':
282 			perm |= MAY_READ;
283 			break;
284 		case 'w':
285 		case 'W':
286 			perm |= MAY_WRITE;
287 			break;
288 		case 'x':
289 		case 'X':
290 			perm |= MAY_EXEC;
291 			break;
292 		case 'a':
293 		case 'A':
294 			perm |= MAY_APPEND;
295 			break;
296 		case 't':
297 		case 'T':
298 			perm |= MAY_TRANSMUTE;
299 			break;
300 		case 'l':
301 		case 'L':
302 			perm |= MAY_LOCK;
303 			break;
304 		case 'b':
305 		case 'B':
306 			perm |= MAY_BRINGUP;
307 			break;
308 		default:
309 			return perm;
310 		}
311 }
312 
313 /**
314  * smk_fill_rule - Fill Smack rule from strings
315  * @subject: subject label string
316  * @object: object label string
317  * @access1: access string
318  * @access2: string with permissions to be removed
319  * @rule: Smack rule
320  * @import: if non-zero, import labels
321  * @len: label length limit
322  *
323  * Returns 0 on success, appropriate error code on failure.
324  */
325 static int smk_fill_rule(const char *subject, const char *object,
326 				const char *access1, const char *access2,
327 				struct smack_parsed_rule *rule, int import,
328 				int len)
329 {
330 	const char *cp;
331 	struct smack_known *skp;
332 
333 	if (import) {
334 		rule->smk_subject = smk_import_entry(subject, len);
335 		if (IS_ERR(rule->smk_subject))
336 			return PTR_ERR(rule->smk_subject);
337 
338 		rule->smk_object = smk_import_entry(object, len);
339 		if (IS_ERR(rule->smk_object))
340 			return PTR_ERR(rule->smk_object);
341 	} else {
342 		cp = smk_parse_smack(subject, len);
343 		if (IS_ERR(cp))
344 			return PTR_ERR(cp);
345 		skp = smk_find_entry(cp);
346 		kfree(cp);
347 		if (skp == NULL)
348 			return -ENOENT;
349 		rule->smk_subject = skp;
350 
351 		cp = smk_parse_smack(object, len);
352 		if (IS_ERR(cp))
353 			return PTR_ERR(cp);
354 		skp = smk_find_entry(cp);
355 		kfree(cp);
356 		if (skp == NULL)
357 			return -ENOENT;
358 		rule->smk_object = skp;
359 	}
360 
361 	rule->smk_access1 = smk_perm_from_str(access1);
362 	if (access2)
363 		rule->smk_access2 = smk_perm_from_str(access2);
364 	else
365 		rule->smk_access2 = ~rule->smk_access1;
366 
367 	return 0;
368 }
369 
370 /**
371  * smk_parse_rule - parse Smack rule from load string
372  * @data: string to be parsed whose size is SMK_LOADLEN
373  * @rule: Smack rule
374  * @import: if non-zero, import labels
375  *
376  * Returns 0 on success, -1 on errors.
377  */
378 static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
379 				int import)
380 {
381 	int rc;
382 
383 	rc = smk_fill_rule(data, data + SMK_LABELLEN,
384 			   data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
385 			   import, SMK_LABELLEN);
386 	return rc;
387 }
388 
389 /**
390  * smk_parse_long_rule - parse Smack rule from rule string
391  * @data: string to be parsed, null terminated
392  * @rule: Will be filled with Smack parsed rule
393  * @import: if non-zero, import labels
394  * @tokens: number of substrings expected in data
395  *
396  * Returns number of processed bytes on success, -ERRNO on failure.
397  */
398 static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
399 				int import, int tokens)
400 {
401 	ssize_t cnt = 0;
402 	char *tok[4];
403 	int rc;
404 	int i;
405 
406 	/*
407 	 * Parsing the rule in-place, filling all white-spaces with '\0'
408 	 */
409 	for (i = 0; i < tokens; ++i) {
410 		while (isspace(data[cnt]))
411 			data[cnt++] = '\0';
412 
413 		if (data[cnt] == '\0')
414 			/* Unexpected end of data */
415 			return -EINVAL;
416 
417 		tok[i] = data + cnt;
418 
419 		while (data[cnt] && !isspace(data[cnt]))
420 			++cnt;
421 	}
422 	while (isspace(data[cnt]))
423 		data[cnt++] = '\0';
424 
425 	while (i < 4)
426 		tok[i++] = NULL;
427 
428 	rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
429 	return rc == 0 ? cnt : rc;
430 }
431 
432 #define SMK_FIXED24_FMT	0	/* Fixed 24byte label format */
433 #define SMK_LONG_FMT	1	/* Variable long label format */
434 #define SMK_CHANGE_FMT	2	/* Rule modification format */
435 /**
436  * smk_write_rules_list - write() for any /smack rule file
437  * @file: file pointer, not actually used
438  * @buf: where to get the data from
439  * @count: bytes sent
440  * @ppos: where to start - must be 0
441  * @rule_list: the list of rules to write to
442  * @rule_lock: lock for the rule list
443  * @format: /smack/load or /smack/load2 or /smack/change-rule format.
444  *
445  * Get one smack access rule from above.
446  * The format for SMK_LONG_FMT is:
447  *	"subject<whitespace>object<whitespace>access[<whitespace>...]"
448  * The format for SMK_FIXED24_FMT is exactly:
449  *	"subject                 object                  rwxat"
450  * The format for SMK_CHANGE_FMT is:
451  *	"subject<whitespace>object<whitespace>
452  *	 acc_enable<whitespace>acc_disable[<whitespace>...]"
453  */
454 static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
455 					size_t count, loff_t *ppos,
456 					struct list_head *rule_list,
457 					struct mutex *rule_lock, int format)
458 {
459 	struct smack_parsed_rule rule;
460 	char *data;
461 	int rc;
462 	int trunc = 0;
463 	int tokens;
464 	ssize_t cnt = 0;
465 
466 	/*
467 	 * No partial writes.
468 	 * Enough data must be present.
469 	 */
470 	if (*ppos != 0)
471 		return -EINVAL;
472 
473 	if (format == SMK_FIXED24_FMT) {
474 		/*
475 		 * Minor hack for backward compatibility
476 		 */
477 		if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
478 			return -EINVAL;
479 	} else {
480 		if (count >= PAGE_SIZE) {
481 			count = PAGE_SIZE - 1;
482 			trunc = 1;
483 		}
484 	}
485 
486 	data = memdup_user_nul(buf, count);
487 	if (IS_ERR(data))
488 		return PTR_ERR(data);
489 
490 	/*
491 	 * In case of parsing only part of user buf,
492 	 * avoid having partial rule at the data buffer
493 	 */
494 	if (trunc) {
495 		while (count > 0 && (data[count - 1] != '\n'))
496 			--count;
497 		if (count == 0) {
498 			rc = -EINVAL;
499 			goto out;
500 		}
501 	}
502 
503 	data[count] = '\0';
504 	tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
505 	while (cnt < count) {
506 		if (format == SMK_FIXED24_FMT) {
507 			rc = smk_parse_rule(data, &rule, 1);
508 			if (rc < 0)
509 				goto out;
510 			cnt = count;
511 		} else {
512 			rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
513 			if (rc < 0)
514 				goto out;
515 			if (rc == 0) {
516 				rc = -EINVAL;
517 				goto out;
518 			}
519 			cnt += rc;
520 		}
521 
522 		if (rule_list == NULL)
523 			rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
524 				&rule.smk_subject->smk_rules_lock);
525 		else
526 			rc = smk_set_access(&rule, rule_list, rule_lock);
527 
528 		if (rc)
529 			goto out;
530 	}
531 
532 	rc = cnt;
533 out:
534 	kfree(data);
535 	return rc;
536 }
537 
538 /*
539  * Core logic for smackfs seq list operations.
540  */
541 
542 static void *smk_seq_start(struct seq_file *s, loff_t *pos,
543 				struct list_head *head)
544 {
545 	struct list_head *list;
546 	int i = *pos;
547 
548 	rcu_read_lock();
549 	for (list = rcu_dereference(list_next_rcu(head));
550 		list != head;
551 		list = rcu_dereference(list_next_rcu(list))) {
552 		if (i-- == 0)
553 			return list;
554 	}
555 
556 	return NULL;
557 }
558 
559 static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
560 				struct list_head *head)
561 {
562 	struct list_head *list = v;
563 
564 	++*pos;
565 	list = rcu_dereference(list_next_rcu(list));
566 
567 	return (list == head) ? NULL : list;
568 }
569 
570 static void smk_seq_stop(struct seq_file *s, void *v)
571 {
572 	rcu_read_unlock();
573 }
574 
575 static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
576 {
577 	char acc[SMK_NUM_ACCESS_TYPE + 1];
578 	/*
579 	 * Don't show any rules with label names too long for
580 	 * interface file (/smack/load or /smack/load2)
581 	 * because you should expect to be able to write
582 	 * anything you read back.
583 	 */
584 	if (strlen(srp->smk_subject->smk_known) >= max ||
585 	    strlen(srp->smk_object->smk_known) >= max)
586 		return;
587 
588 	if (srp->smk_access == 0)
589 		return;
590 
591 	smack_str_from_perm(acc, srp->smk_access);
592 	seq_printf(s, "%s %s %s\n",
593 		   srp->smk_subject->smk_known,
594 		   srp->smk_object->smk_known,
595 		   acc);
596 }
597 
598 /*
599  * Seq_file read operations for /smack/load
600  */
601 
602 static void *load2_seq_start(struct seq_file *s, loff_t *pos)
603 {
604 	return smk_seq_start(s, pos, &smack_known_list);
605 }
606 
607 static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
608 {
609 	return smk_seq_next(s, v, pos, &smack_known_list);
610 }
611 
612 static int load_seq_show(struct seq_file *s, void *v)
613 {
614 	struct list_head *list = v;
615 	struct smack_rule *srp;
616 	struct smack_known *skp =
617 		list_entry_rcu(list, struct smack_known, list);
618 
619 	list_for_each_entry_rcu(srp, &skp->smk_rules, list)
620 		smk_rule_show(s, srp, SMK_LABELLEN);
621 
622 	return 0;
623 }
624 
625 static const struct seq_operations load_seq_ops = {
626 	.start = load2_seq_start,
627 	.next  = load2_seq_next,
628 	.show  = load_seq_show,
629 	.stop  = smk_seq_stop,
630 };
631 
632 /**
633  * smk_open_load - open() for /smack/load
634  * @inode: inode structure representing file
635  * @file: "load" file pointer
636  *
637  * For reading, use load_seq_* seq_file reading operations.
638  */
639 static int smk_open_load(struct inode *inode, struct file *file)
640 {
641 	return seq_open(file, &load_seq_ops);
642 }
643 
644 /**
645  * smk_write_load - write() for /smack/load
646  * @file: file pointer, not actually used
647  * @buf: where to get the data from
648  * @count: bytes sent
649  * @ppos: where to start - must be 0
650  *
651  */
652 static ssize_t smk_write_load(struct file *file, const char __user *buf,
653 			      size_t count, loff_t *ppos)
654 {
655 	/*
656 	 * Must have privilege.
657 	 * No partial writes.
658 	 * Enough data must be present.
659 	 */
660 	if (!smack_privileged(CAP_MAC_ADMIN))
661 		return -EPERM;
662 
663 	return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
664 				    SMK_FIXED24_FMT);
665 }
666 
667 static const struct file_operations smk_load_ops = {
668 	.open           = smk_open_load,
669 	.read		= seq_read,
670 	.llseek         = seq_lseek,
671 	.write		= smk_write_load,
672 	.release        = seq_release,
673 };
674 
675 /**
676  * smk_cipso_doi - set netlabel maps
677  * @ndoi: new value for our CIPSO DOI
678  * @gfp_flags: kmalloc allocation context
679  */
680 static int
681 smk_cipso_doi(u32 ndoi, gfp_t gfp_flags)
682 {
683 	int rc = 0;
684 	struct cipso_v4_doi *doip;
685 	struct netlbl_audit nai;
686 
687 	mutex_lock(&smk_cipso_doi_lock);
688 
689 	if (smk_cipso_doi_value == ndoi)
690 		goto clr_doi_lock;
691 
692 	smk_netlabel_audit_set(&nai);
693 
694 	doip = kmalloc_obj(struct cipso_v4_doi, gfp_flags);
695 	if (!doip) {
696 		rc = -ENOMEM;
697 		goto clr_doi_lock;
698 	}
699 	doip->map.std = NULL;
700 	doip->doi = ndoi;
701 	doip->type = CIPSO_V4_MAP_PASS;
702 	doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
703 	for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
704 		doip->tags[rc] = CIPSO_V4_TAG_INVALID;
705 
706 	rc = netlbl_cfg_cipsov4_add(doip, &nai);
707 	if (rc) {
708 		kfree(doip);
709 		goto clr_doi_lock;
710 	}
711 
712 	if (smk_cipso_doi_value != CIPSO_V4_DOI_UNKNOWN) {
713 		rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
714 		if (rc && rc != -ENOENT)
715 			goto clr_ndoi_def;
716 
717 		netlbl_cfg_cipsov4_del(smk_cipso_doi_value, &nai);
718 	}
719 
720 	rc = netlbl_cfg_cipsov4_map_add(ndoi, NULL, NULL, NULL, &nai);
721 	if (rc) {
722 		smk_cipso_doi_value = CIPSO_V4_DOI_UNKNOWN; // no default map
723 clr_ndoi_def:	netlbl_cfg_cipsov4_del(ndoi, &nai);
724 	} else
725 		smk_cipso_doi_value = ndoi;
726 
727 clr_doi_lock:
728 	mutex_unlock(&smk_cipso_doi_lock);
729 	return rc;
730 }
731 
732 /**
733  * smk_unlbl_ambient - initialize the unlabeled domain
734  * @oldambient: previous domain string
735  */
736 static void smk_unlbl_ambient(char *oldambient)
737 {
738 	int rc;
739 	struct netlbl_audit nai;
740 
741 	smk_netlabel_audit_set(&nai);
742 
743 	if (oldambient != NULL) {
744 		rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
745 		if (rc != 0)
746 			printk(KERN_WARNING "%s:%d remove rc = %d\n",
747 			       __func__, __LINE__, rc);
748 	}
749 	if (smack_net_ambient == NULL)
750 		smack_net_ambient = &smack_known_floor;
751 
752 	rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
753 				      NULL, NULL, &nai);
754 	if (rc != 0)
755 		printk(KERN_WARNING "%s:%d add rc = %d\n",
756 		       __func__, __LINE__, rc);
757 }
758 
759 /*
760  * Seq_file read operations for /smack/cipso
761  */
762 
763 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
764 {
765 	return smk_seq_start(s, pos, &smack_known_list);
766 }
767 
768 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
769 {
770 	return smk_seq_next(s, v, pos, &smack_known_list);
771 }
772 
773 /*
774  * Print cipso labels in format:
775  * label level[/cat[,cat]]
776  */
777 static int cipso_seq_show(struct seq_file *s, void *v)
778 {
779 	struct list_head  *list = v;
780 	struct smack_known *skp =
781 		list_entry_rcu(list, struct smack_known, list);
782 	struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
783 	char sep = '/';
784 	int i;
785 
786 	/*
787 	 * Don't show a label that could not have been set using
788 	 * /smack/cipso. This is in support of the notion that
789 	 * anything read from /smack/cipso ought to be writeable
790 	 * to /smack/cipso.
791 	 *
792 	 * /smack/cipso2 should be used instead.
793 	 */
794 	if (strlen(skp->smk_known) >= SMK_LABELLEN)
795 		return 0;
796 
797 	seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
798 
799 	for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
800 	     i = netlbl_catmap_walk(cmp, i + 1)) {
801 		seq_printf(s, "%c%d", sep, i);
802 		sep = ',';
803 	}
804 
805 	seq_putc(s, '\n');
806 
807 	return 0;
808 }
809 
810 static const struct seq_operations cipso_seq_ops = {
811 	.start = cipso_seq_start,
812 	.next  = cipso_seq_next,
813 	.show  = cipso_seq_show,
814 	.stop  = smk_seq_stop,
815 };
816 
817 /**
818  * smk_open_cipso - open() for /smack/cipso
819  * @inode: inode structure representing file
820  * @file: "cipso" file pointer
821  *
822  * Connect our cipso_seq_* operations with /smack/cipso
823  * file_operations
824  */
825 static int smk_open_cipso(struct inode *inode, struct file *file)
826 {
827 	return seq_open(file, &cipso_seq_ops);
828 }
829 
830 /**
831  * smk_set_cipso - do the work for write() for cipso and cipso2
832  * @file: file pointer, not actually used
833  * @buf: where to get the data from
834  * @count: bytes sent
835  * @ppos: where to start
836  * @format: /smack/cipso or /smack/cipso2
837  *
838  * Accepts only one cipso rule per write call.
839  * Returns number of bytes written or error code, as appropriate
840  */
841 static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
842 				size_t count, loff_t *ppos, int format)
843 {
844 	struct netlbl_lsm_catmap *old_cat;
845 	struct smack_known *skp;
846 	struct netlbl_lsm_secattr ncats;
847 	char mapcatset[SMK_CIPSOLEN];
848 	int maplevel;
849 	unsigned int cat;
850 	int catlen;
851 	ssize_t rc = -EINVAL;
852 	char *data = NULL;
853 	char *rule;
854 	int ret;
855 	int i;
856 
857 	/*
858 	 * Must have privilege.
859 	 * No partial writes.
860 	 * Enough data must be present.
861 	 */
862 	if (!smack_privileged(CAP_MAC_ADMIN))
863 		return -EPERM;
864 	if (*ppos != 0)
865 		return -EINVAL;
866 	if (format == SMK_FIXED24_FMT &&
867 	    (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
868 		return -EINVAL;
869 	if (count > PAGE_SIZE)
870 		return -EINVAL;
871 
872 	data = memdup_user_nul(buf, count);
873 	if (IS_ERR(data))
874 		return PTR_ERR(data);
875 
876 	rule = data;
877 	/*
878 	 * Only allow one writer at a time. Writes should be
879 	 * quite rare and small in any case.
880 	 */
881 	mutex_lock(&smack_cipso_lock);
882 
883 	skp = smk_import_entry(rule, 0);
884 	if (IS_ERR(skp)) {
885 		rc = PTR_ERR(skp);
886 		goto out;
887 	}
888 
889 	if (format == SMK_FIXED24_FMT)
890 		rule += SMK_LABELLEN;
891 	else
892 		rule += strlen(skp->smk_known) + 1;
893 
894 	if (rule > data + count) {
895 		rc = -EOVERFLOW;
896 		goto out;
897 	}
898 
899 	ret = sscanf(rule, "%d", &maplevel);
900 	if (ret != 1 || maplevel < 0 || maplevel > SMACK_CIPSO_MAXLEVEL)
901 		goto out;
902 
903 	rule += SMK_DIGITLEN;
904 	if (rule > data + count) {
905 		rc = -EOVERFLOW;
906 		goto out;
907 	}
908 
909 	ret = sscanf(rule, "%d", &catlen);
910 	if (ret != 1 || catlen < 0 || catlen > SMACK_CIPSO_MAXCATNUM)
911 		goto out;
912 
913 	if (format == SMK_FIXED24_FMT &&
914 	    count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
915 		goto out;
916 
917 	memset(mapcatset, 0, sizeof(mapcatset));
918 
919 	for (i = 0; i < catlen; i++) {
920 		rule += SMK_DIGITLEN;
921 		if (rule > data + count) {
922 			rc = -EOVERFLOW;
923 			goto out;
924 		}
925 		ret = sscanf(rule, "%u", &cat);
926 		if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
927 			goto out;
928 
929 		smack_catset_bit(cat, mapcatset);
930 	}
931 
932 	rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
933 	if (rc >= 0) {
934 		old_cat = skp->smk_netlabel.attr.mls.cat;
935 		rcu_assign_pointer(skp->smk_netlabel.attr.mls.cat, ncats.attr.mls.cat);
936 		if (ncats.attr.mls.cat)
937 			skp->smk_netlabel.flags |= NETLBL_SECATTR_MLS_CAT;
938 		else
939 			skp->smk_netlabel.flags &= ~(u32)NETLBL_SECATTR_MLS_CAT;
940 		skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
941 		synchronize_rcu();
942 		netlbl_catmap_free(old_cat);
943 		rc = count;
944 		/*
945 		 * This mapping may have been cached, so clear the cache.
946 		 */
947 		netlbl_cache_invalidate();
948 	}
949 
950 out:
951 	mutex_unlock(&smack_cipso_lock);
952 	kfree(data);
953 	return rc;
954 }
955 
956 /**
957  * smk_write_cipso - write() for /smack/cipso
958  * @file: file pointer, not actually used
959  * @buf: where to get the data from
960  * @count: bytes sent
961  * @ppos: where to start
962  *
963  * Accepts only one cipso rule per write call.
964  * Returns number of bytes written or error code, as appropriate
965  */
966 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
967 			       size_t count, loff_t *ppos)
968 {
969 	return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
970 }
971 
972 static const struct file_operations smk_cipso_ops = {
973 	.open           = smk_open_cipso,
974 	.read		= seq_read,
975 	.llseek         = seq_lseek,
976 	.write		= smk_write_cipso,
977 	.release        = seq_release,
978 };
979 
980 /*
981  * Seq_file read operations for /smack/cipso2
982  */
983 
984 /*
985  * Print cipso labels in format:
986  * label level[/cat[,cat]]
987  */
988 static int cipso2_seq_show(struct seq_file *s, void *v)
989 {
990 	struct list_head  *list = v;
991 	struct smack_known *skp =
992 		list_entry_rcu(list, struct smack_known, list);
993 	struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
994 	char sep = '/';
995 	int i;
996 
997 	seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
998 
999 	for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
1000 	     i = netlbl_catmap_walk(cmp, i + 1)) {
1001 		seq_printf(s, "%c%d", sep, i);
1002 		sep = ',';
1003 	}
1004 
1005 	seq_putc(s, '\n');
1006 
1007 	return 0;
1008 }
1009 
1010 static const struct seq_operations cipso2_seq_ops = {
1011 	.start = cipso_seq_start,
1012 	.next  = cipso_seq_next,
1013 	.show  = cipso2_seq_show,
1014 	.stop  = smk_seq_stop,
1015 };
1016 
1017 /**
1018  * smk_open_cipso2 - open() for /smack/cipso2
1019  * @inode: inode structure representing file
1020  * @file: "cipso2" file pointer
1021  *
1022  * Connect our cipso_seq_* operations with /smack/cipso2
1023  * file_operations
1024  */
1025 static int smk_open_cipso2(struct inode *inode, struct file *file)
1026 {
1027 	return seq_open(file, &cipso2_seq_ops);
1028 }
1029 
1030 /**
1031  * smk_write_cipso2 - write() for /smack/cipso2
1032  * @file: file pointer, not actually used
1033  * @buf: where to get the data from
1034  * @count: bytes sent
1035  * @ppos: where to start
1036  *
1037  * Accepts only one cipso rule per write call.
1038  * Returns number of bytes written or error code, as appropriate
1039  */
1040 static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1041 			      size_t count, loff_t *ppos)
1042 {
1043 	return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1044 }
1045 
1046 static const struct file_operations smk_cipso2_ops = {
1047 	.open           = smk_open_cipso2,
1048 	.read		= seq_read,
1049 	.llseek         = seq_lseek,
1050 	.write		= smk_write_cipso2,
1051 	.release        = seq_release,
1052 };
1053 
1054 /*
1055  * Seq_file read operations for /smack/netlabel
1056  */
1057 
1058 static void *net4addr_seq_start(struct seq_file *s, loff_t *pos)
1059 {
1060 	return smk_seq_start(s, pos, &smk_net4addr_list);
1061 }
1062 
1063 static void *net4addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1064 {
1065 	return smk_seq_next(s, v, pos, &smk_net4addr_list);
1066 }
1067 
1068 /*
1069  * Print host/label pairs
1070  */
1071 static int net4addr_seq_show(struct seq_file *s, void *v)
1072 {
1073 	struct list_head *list = v;
1074 	struct smk_net4addr *skp =
1075 			list_entry_rcu(list, struct smk_net4addr, list);
1076 	char *kp = SMACK_CIPSO_OPTION;
1077 
1078 	if (skp->smk_label != NULL)
1079 		kp = skp->smk_label->smk_known;
1080 	seq_printf(s, "%pI4/%d %s\n", &skp->smk_host.s_addr,
1081 			skp->smk_masks, kp);
1082 
1083 	return 0;
1084 }
1085 
1086 static const struct seq_operations net4addr_seq_ops = {
1087 	.start = net4addr_seq_start,
1088 	.next  = net4addr_seq_next,
1089 	.show  = net4addr_seq_show,
1090 	.stop  = smk_seq_stop,
1091 };
1092 
1093 /**
1094  * smk_open_net4addr - open() for /smack/netlabel
1095  * @inode: inode structure representing file
1096  * @file: "netlabel" file pointer
1097  *
1098  * Connect our net4addr_seq_* operations with /smack/netlabel
1099  * file_operations
1100  */
1101 static int smk_open_net4addr(struct inode *inode, struct file *file)
1102 {
1103 	return seq_open(file, &net4addr_seq_ops);
1104 }
1105 
1106 /**
1107  * smk_net4addr_insert - insert a new entry into the net4addrs list
1108  * @new : netlabel to insert
1109  *
1110  * This helper inserts netlabel in the smack_net4addrs list
1111  * sorted by netmask length (longest to smallest)
1112  * locked by &smk_net4addr_lock in smk_write_net4addr.
1113  */
1114 static void smk_net4addr_insert(struct smk_net4addr *new)
1115 {
1116 	struct smk_net4addr *m;
1117 	struct smk_net4addr *m_next;
1118 
1119 	if (list_empty(&smk_net4addr_list)) {
1120 		list_add_rcu(&new->list, &smk_net4addr_list);
1121 		return;
1122 	}
1123 
1124 	m = list_entry_rcu(smk_net4addr_list.next,
1125 			   struct smk_net4addr, list);
1126 
1127 	/* the comparison '>' is a bit hacky, but works */
1128 	if (new->smk_masks > m->smk_masks) {
1129 		list_add_rcu(&new->list, &smk_net4addr_list);
1130 		return;
1131 	}
1132 
1133 	list_for_each_entry_rcu(m, &smk_net4addr_list, list) {
1134 		if (list_is_last(&m->list, &smk_net4addr_list)) {
1135 			list_add_rcu(&new->list, &m->list);
1136 			return;
1137 		}
1138 		m_next = list_entry_rcu(m->list.next,
1139 					struct smk_net4addr, list);
1140 		if (new->smk_masks > m_next->smk_masks) {
1141 			list_add_rcu(&new->list, &m->list);
1142 			return;
1143 		}
1144 	}
1145 }
1146 
1147 
1148 /**
1149  * smk_write_net4addr - write() for /smack/netlabel
1150  * @file: file pointer, not actually used
1151  * @buf: where to get the data from
1152  * @count: bytes sent
1153  * @ppos: where to start
1154  *
1155  * Accepts only one net4addr per write call.
1156  * Returns number of bytes written or error code, as appropriate
1157  */
1158 static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
1159 				size_t count, loff_t *ppos)
1160 {
1161 	struct smk_net4addr *snp;
1162 	struct sockaddr_in newname;
1163 	char *smack;
1164 	struct smack_known *skp = NULL;
1165 	char *data;
1166 	char *host = (char *)&newname.sin_addr.s_addr;
1167 	int rc;
1168 	struct netlbl_audit audit_info;
1169 	struct in_addr mask;
1170 	unsigned int m;
1171 	unsigned int masks;
1172 	int found;
1173 	u32 mask_bits = (1<<31);
1174 	__be32 nsa;
1175 	u32 temp_mask;
1176 
1177 	/*
1178 	 * Must have privilege.
1179 	 * No partial writes.
1180 	 * Enough data must be present.
1181 	 * "<addr/mask, as a.b.c.d/e><space><label>"
1182 	 * "<addr, as a.b.c.d><space><label>"
1183 	 */
1184 	if (!smack_privileged(CAP_MAC_ADMIN))
1185 		return -EPERM;
1186 	if (*ppos != 0)
1187 		return -EINVAL;
1188 	if (count < SMK_NETLBLADDRMIN || count > PAGE_SIZE - 1)
1189 		return -EINVAL;
1190 
1191 	data = memdup_user_nul(buf, count);
1192 	if (IS_ERR(data))
1193 		return PTR_ERR(data);
1194 
1195 	smack = kzalloc(count + 1, GFP_KERNEL);
1196 	if (smack == NULL) {
1197 		rc = -ENOMEM;
1198 		goto free_data_out;
1199 	}
1200 
1201 	rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
1202 		&host[0], &host[1], &host[2], &host[3], &masks, smack);
1203 	if (rc != 6) {
1204 		rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1205 			&host[0], &host[1], &host[2], &host[3], smack);
1206 		if (rc != 5) {
1207 			rc = -EINVAL;
1208 			goto free_out;
1209 		}
1210 		masks = 32;
1211 	}
1212 	if (masks > BEBITS) {
1213 		rc = -EINVAL;
1214 		goto free_out;
1215 	}
1216 
1217 	/*
1218 	 * If smack begins with '-', it is an option, don't import it
1219 	 */
1220 	if (smack[0] != '-') {
1221 		skp = smk_import_entry(smack, 0);
1222 		if (IS_ERR(skp)) {
1223 			rc = PTR_ERR(skp);
1224 			goto free_out;
1225 		}
1226 	} else {
1227 		/*
1228 		 * Only the -CIPSO option is supported for IPv4
1229 		 */
1230 		if (strcmp(smack, SMACK_CIPSO_OPTION) != 0) {
1231 			rc = -EINVAL;
1232 			goto free_out;
1233 		}
1234 	}
1235 
1236 	for (m = masks, temp_mask = 0; m > 0; m--) {
1237 		temp_mask |= mask_bits;
1238 		mask_bits >>= 1;
1239 	}
1240 	mask.s_addr = cpu_to_be32(temp_mask);
1241 
1242 	newname.sin_addr.s_addr &= mask.s_addr;
1243 	/*
1244 	 * Only allow one writer at a time. Writes should be
1245 	 * quite rare and small in any case.
1246 	 */
1247 	mutex_lock(&smk_net4addr_lock);
1248 
1249 	nsa = newname.sin_addr.s_addr;
1250 	/* try to find if the prefix is already in the list */
1251 	found = 0;
1252 	list_for_each_entry_rcu(snp, &smk_net4addr_list, list) {
1253 		if (snp->smk_host.s_addr == nsa && snp->smk_masks == masks) {
1254 			found = 1;
1255 			break;
1256 		}
1257 	}
1258 	smk_netlabel_audit_set(&audit_info);
1259 
1260 	if (found == 0) {
1261 		snp = kzalloc_obj(*snp);
1262 		if (snp == NULL)
1263 			rc = -ENOMEM;
1264 		else {
1265 			rc = 0;
1266 			snp->smk_host.s_addr = newname.sin_addr.s_addr;
1267 			snp->smk_mask.s_addr = mask.s_addr;
1268 			snp->smk_label = skp;
1269 			snp->smk_masks = masks;
1270 			smk_net4addr_insert(snp);
1271 		}
1272 	} else {
1273 		/*
1274 		 * Delete the unlabeled entry, only if the previous label
1275 		 * wasn't the special CIPSO option
1276 		 */
1277 		if (snp->smk_label != NULL)
1278 			rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1279 					&snp->smk_host, &snp->smk_mask,
1280 					PF_INET, &audit_info);
1281 		else
1282 			rc = 0;
1283 		snp->smk_label = skp;
1284 	}
1285 
1286 	/*
1287 	 * Now tell netlabel about the single label nature of
1288 	 * this host so that incoming packets get labeled.
1289 	 * but only if we didn't get the special CIPSO option
1290 	 */
1291 	if (rc == 0 && skp != NULL)
1292 		rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1293 			&snp->smk_host, &snp->smk_mask, PF_INET,
1294 			snp->smk_label->smk_secid, &audit_info);
1295 
1296 	if (rc == 0)
1297 		rc = count;
1298 
1299 	mutex_unlock(&smk_net4addr_lock);
1300 
1301 free_out:
1302 	kfree(smack);
1303 free_data_out:
1304 	kfree(data);
1305 
1306 	return rc;
1307 }
1308 
1309 static const struct file_operations smk_net4addr_ops = {
1310 	.open           = smk_open_net4addr,
1311 	.read		= seq_read,
1312 	.llseek         = seq_lseek,
1313 	.write		= smk_write_net4addr,
1314 	.release        = seq_release,
1315 };
1316 
1317 #if IS_ENABLED(CONFIG_IPV6)
1318 /*
1319  * Seq_file read operations for /smack/netlabel6
1320  */
1321 
1322 static void *net6addr_seq_start(struct seq_file *s, loff_t *pos)
1323 {
1324 	return smk_seq_start(s, pos, &smk_net6addr_list);
1325 }
1326 
1327 static void *net6addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1328 {
1329 	return smk_seq_next(s, v, pos, &smk_net6addr_list);
1330 }
1331 
1332 /*
1333  * Print host/label pairs
1334  */
1335 static int net6addr_seq_show(struct seq_file *s, void *v)
1336 {
1337 	struct list_head *list = v;
1338 	struct smk_net6addr *skp =
1339 			 list_entry(list, struct smk_net6addr, list);
1340 
1341 	if (skp->smk_label != NULL)
1342 		seq_printf(s, "%pI6/%d %s\n", &skp->smk_host, skp->smk_masks,
1343 				skp->smk_label->smk_known);
1344 
1345 	return 0;
1346 }
1347 
1348 static const struct seq_operations net6addr_seq_ops = {
1349 	.start = net6addr_seq_start,
1350 	.next  = net6addr_seq_next,
1351 	.show  = net6addr_seq_show,
1352 	.stop  = smk_seq_stop,
1353 };
1354 
1355 /**
1356  * smk_open_net6addr - open() for /smack/netlabel
1357  * @inode: inode structure representing file
1358  * @file: "netlabel" file pointer
1359  *
1360  * Connect our net6addr_seq_* operations with /smack/netlabel
1361  * file_operations
1362  */
1363 static int smk_open_net6addr(struct inode *inode, struct file *file)
1364 {
1365 	return seq_open(file, &net6addr_seq_ops);
1366 }
1367 
1368 /**
1369  * smk_net6addr_insert - insert a new entry into the net6addrs list
1370  * @new : entry to insert
1371  *
1372  * This inserts an entry in the smack_net6addrs list
1373  * sorted by netmask length (longest to smallest)
1374  * locked by &smk_net6addr_lock in smk_write_net6addr.
1375  */
1376 static void smk_net6addr_insert(struct smk_net6addr *new)
1377 {
1378 	struct smk_net6addr *m_next;
1379 	struct smk_net6addr *m;
1380 
1381 	if (list_empty(&smk_net6addr_list)) {
1382 		list_add_rcu(&new->list, &smk_net6addr_list);
1383 		return;
1384 	}
1385 
1386 	m = list_entry_rcu(smk_net6addr_list.next,
1387 			   struct smk_net6addr, list);
1388 
1389 	if (new->smk_masks > m->smk_masks) {
1390 		list_add_rcu(&new->list, &smk_net6addr_list);
1391 		return;
1392 	}
1393 
1394 	list_for_each_entry_rcu(m, &smk_net6addr_list, list) {
1395 		if (list_is_last(&m->list, &smk_net6addr_list)) {
1396 			list_add_rcu(&new->list, &m->list);
1397 			return;
1398 		}
1399 		m_next = list_entry_rcu(m->list.next,
1400 					struct smk_net6addr, list);
1401 		if (new->smk_masks > m_next->smk_masks) {
1402 			list_add_rcu(&new->list, &m->list);
1403 			return;
1404 		}
1405 	}
1406 }
1407 
1408 
1409 /**
1410  * smk_write_net6addr - write() for /smack/netlabel
1411  * @file: file pointer, not actually used
1412  * @buf: where to get the data from
1413  * @count: bytes sent
1414  * @ppos: where to start
1415  *
1416  * Accepts only one net6addr per write call.
1417  * Returns number of bytes written or error code, as appropriate
1418  */
1419 static ssize_t smk_write_net6addr(struct file *file, const char __user *buf,
1420 				size_t count, loff_t *ppos)
1421 {
1422 	struct smk_net6addr *snp;
1423 	struct in6_addr newname;
1424 	struct in6_addr fullmask;
1425 	struct smack_known *skp = NULL;
1426 	char *smack;
1427 	char *data;
1428 	int rc = 0;
1429 	int found = 0;
1430 	int i;
1431 	unsigned int scanned[8];
1432 	unsigned int m;
1433 	unsigned int mask = 128;
1434 
1435 	/*
1436 	 * Must have privilege.
1437 	 * No partial writes.
1438 	 * Enough data must be present.
1439 	 * "<addr/mask, as a:b:c:d:e:f:g:h/e><space><label>"
1440 	 * "<addr, as a:b:c:d:e:f:g:h><space><label>"
1441 	 */
1442 	if (!smack_privileged(CAP_MAC_ADMIN))
1443 		return -EPERM;
1444 	if (*ppos != 0)
1445 		return -EINVAL;
1446 	if (count < SMK_NETLBLADDRMIN || count > PAGE_SIZE - 1)
1447 		return -EINVAL;
1448 
1449 	data = memdup_user_nul(buf, count);
1450 	if (IS_ERR(data))
1451 		return PTR_ERR(data);
1452 
1453 	smack = kzalloc(count + 1, GFP_KERNEL);
1454 	if (smack == NULL) {
1455 		rc = -ENOMEM;
1456 		goto free_data_out;
1457 	}
1458 
1459 	i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x/%u %s",
1460 			&scanned[0], &scanned[1], &scanned[2], &scanned[3],
1461 			&scanned[4], &scanned[5], &scanned[6], &scanned[7],
1462 			&mask, smack);
1463 	if (i != 10) {
1464 		i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x %s",
1465 				&scanned[0], &scanned[1], &scanned[2],
1466 				&scanned[3], &scanned[4], &scanned[5],
1467 				&scanned[6], &scanned[7], smack);
1468 		if (i != 9) {
1469 			rc = -EINVAL;
1470 			goto free_out;
1471 		}
1472 	}
1473 	if (mask > 128) {
1474 		rc = -EINVAL;
1475 		goto free_out;
1476 	}
1477 	for (i = 0; i < 8; i++) {
1478 		if (scanned[i] > 0xffff) {
1479 			rc = -EINVAL;
1480 			goto free_out;
1481 		}
1482 		newname.s6_addr16[i] = htons(scanned[i]);
1483 	}
1484 
1485 	/*
1486 	 * If smack begins with '-', it is an option, don't import it
1487 	 */
1488 	if (smack[0] != '-') {
1489 		skp = smk_import_entry(smack, 0);
1490 		if (IS_ERR(skp)) {
1491 			rc = PTR_ERR(skp);
1492 			goto free_out;
1493 		}
1494 	} else {
1495 		/*
1496 		 * Only -DELETE is supported for IPv6
1497 		 */
1498 		if (strcmp(smack, SMACK_DELETE_OPTION) != 0) {
1499 			rc = -EINVAL;
1500 			goto free_out;
1501 		}
1502 	}
1503 
1504 	for (i = 0, m = mask; i < 8; i++) {
1505 		if (m >= 16) {
1506 			fullmask.s6_addr16[i] = 0xffff;
1507 			m -= 16;
1508 		} else if (m > 0) {
1509 			fullmask.s6_addr16[i] = (1 << m) - 1;
1510 			m = 0;
1511 		} else
1512 			fullmask.s6_addr16[i] = 0;
1513 		newname.s6_addr16[i] &= fullmask.s6_addr16[i];
1514 	}
1515 
1516 	/*
1517 	 * Only allow one writer at a time. Writes should be
1518 	 * quite rare and small in any case.
1519 	 */
1520 	mutex_lock(&smk_net6addr_lock);
1521 	/*
1522 	 * Try to find the prefix in the list
1523 	 */
1524 	list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
1525 		if (mask != snp->smk_masks)
1526 			continue;
1527 		for (found = 1, i = 0; i < 8; i++) {
1528 			if (newname.s6_addr16[i] !=
1529 			    snp->smk_host.s6_addr16[i]) {
1530 				found = 0;
1531 				break;
1532 			}
1533 		}
1534 		if (found == 1)
1535 			break;
1536 	}
1537 	if (found == 0) {
1538 		snp = kzalloc_obj(*snp);
1539 		if (snp == NULL)
1540 			rc = -ENOMEM;
1541 		else {
1542 			snp->smk_host = newname;
1543 			snp->smk_mask = fullmask;
1544 			snp->smk_masks = mask;
1545 			snp->smk_label = skp;
1546 			smk_net6addr_insert(snp);
1547 		}
1548 	} else {
1549 		snp->smk_label = skp;
1550 	}
1551 
1552 	if (rc == 0)
1553 		rc = count;
1554 
1555 	mutex_unlock(&smk_net6addr_lock);
1556 
1557 free_out:
1558 	kfree(smack);
1559 free_data_out:
1560 	kfree(data);
1561 
1562 	return rc;
1563 }
1564 
1565 static const struct file_operations smk_net6addr_ops = {
1566 	.open           = smk_open_net6addr,
1567 	.read		= seq_read,
1568 	.llseek         = seq_lseek,
1569 	.write		= smk_write_net6addr,
1570 	.release        = seq_release,
1571 };
1572 #endif /* CONFIG_IPV6 */
1573 
1574 /**
1575  * smk_read_doi - read() for /smack/doi
1576  * @filp: file pointer, not actually used
1577  * @buf: where to put the result
1578  * @count: maximum to send along
1579  * @ppos: where to start
1580  *
1581  * Returns number of bytes read or error code, as appropriate
1582  */
1583 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1584 			    size_t count, loff_t *ppos)
1585 {
1586 	char temp[80];
1587 	ssize_t rc;
1588 
1589 	if (*ppos != 0)
1590 		return 0;
1591 
1592 	sprintf(temp, "%lu", (unsigned long)smk_cipso_doi_value);
1593 	rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1594 
1595 	return rc;
1596 }
1597 
1598 /**
1599  * smk_write_doi - write() for /smack/doi
1600  * @file: file pointer, not actually used
1601  * @buf: where to get the data from
1602  * @count: bytes sent
1603  * @ppos: where to start
1604  *
1605  * Returns number of bytes written or error code, as appropriate
1606  */
1607 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1608 			     size_t count, loff_t *ppos)
1609 {
1610 	int ret;
1611 	u32 u;
1612 
1613 	if (!smack_privileged(CAP_MAC_ADMIN))
1614 		return -EPERM;
1615 
1616 	ret = kstrtou32_from_user(buf, count, 10, &u);
1617 	if (unlikely(ret))
1618 		return ret;
1619 
1620 	if (u == CIPSO_V4_DOI_UNKNOWN)
1621 		return -EINVAL;
1622 
1623 	return smk_cipso_doi(u, GFP_KERNEL) ? : count;
1624 }
1625 
1626 static const struct file_operations smk_doi_ops = {
1627 	.read		= smk_read_doi,
1628 	.write		= smk_write_doi,
1629 	.llseek		= default_llseek,
1630 };
1631 
1632 /**
1633  * smk_read_cipso_auto_level - read() for smackfs/direct and smackfs/mapped
1634  * @filp: file pointer
1635  * @buf: where to put the result
1636  * @count: maximum to send along
1637  * @ppos: where to start
1638  *
1639  * Returns number of bytes read or error code, as appropriate
1640  */
1641 static ssize_t smk_read_cipso_auto_level(struct file *filp, char __user *buf,
1642 			       size_t count, loff_t *ppos)
1643 {
1644 	char temp[sizeof "255"];
1645 	int n;
1646 
1647 	if (*ppos != 0)
1648 		return 0;
1649 
1650 	n = sprintf(temp, "%u", (unsigned int)smack_cipso_auto_level[
1651 		smk_cipso_auto_level_idx(filp)]);
1652 	return simple_read_from_buffer(buf, count, ppos, temp, n);
1653 }
1654 
1655 /**
1656  * smk_write_cipso_auto_level - write() for smackfs/direct and smackfs/mapped
1657  * @filp: file pointer
1658  * @buf: where to get the data from
1659  * @count: bytes sent
1660  * @ppos: where to start
1661  *
1662  * Returns number of bytes written or error code, as appropriate
1663  */
1664 static ssize_t
1665 smk_write_cipso_auto_level(struct file *filp, const char __user *buf,
1666 			   size_t count, loff_t *ppos)
1667 {
1668 	int ret, idx;
1669 	u8  i, old_lvl;
1670 
1671 	if (!smack_privileged(CAP_MAC_ADMIN))
1672 		return -EPERM;
1673 	/*
1674 	 * draft-ietf-cipso-ipsecurity-01 (CIPSO 2.2), 3.4.2.4:
1675 	 * "Sensitivity Level is 1 octet in length. Its value is from 0 to 255"
1676 	 */
1677 	ret = kstrtou8_from_user(buf, count, 10, &i);
1678 	if (unlikely(ret))
1679 		return ret;
1680 
1681 	/*
1682 	 * Don't do anything if the value hasn't actually changed.
1683 	 * If it is changing reset the level on entries that were
1684 	 * set up to be "auto" level when they were created.
1685 	 */
1686 	idx = smk_cipso_auto_level_idx(filp);
1687 	old_lvl = smack_cipso_auto_level[idx];
1688 
1689 	if (old_lvl != i) {
1690 		struct smack_known *skp;
1691 		mutex_lock(&smack_known_lock);
1692 		list_for_each_entry_rcu(skp, &smack_known_list, list)
1693 			if (skp->smk_netlabel.attr.mls.lvl ==
1694 			    old_lvl)
1695 				skp->smk_netlabel.attr.mls.lvl = i;
1696 		smack_cipso_auto_level[idx] = i;
1697 		mutex_unlock(&smack_known_lock);
1698 	}
1699 
1700 	return count;
1701 }
1702 
1703 static const struct file_operations
1704 smk_cipso_auto_level_ops = {
1705 	.read		= smk_read_cipso_auto_level,
1706 	.write		= smk_write_cipso_auto_level,
1707 	.llseek		= default_llseek,
1708 };
1709 
1710 /**
1711  * smk_read_ambient - read() for /smack/ambient
1712  * @filp: file pointer, not actually used
1713  * @buf: where to put the result
1714  * @cn: maximum to send along
1715  * @ppos: where to start
1716  *
1717  * Returns number of bytes read or error code, as appropriate
1718  */
1719 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1720 				size_t cn, loff_t *ppos)
1721 {
1722 	ssize_t rc;
1723 	int asize;
1724 
1725 	if (*ppos != 0)
1726 		return 0;
1727 	/*
1728 	 * Being careful to avoid a problem in the case where
1729 	 * smack_net_ambient gets changed in midstream.
1730 	 */
1731 	mutex_lock(&smack_ambient_lock);
1732 
1733 	asize = strlen(smack_net_ambient->smk_known) + 1;
1734 
1735 	if (cn >= asize)
1736 		rc = simple_read_from_buffer(buf, cn, ppos,
1737 					     smack_net_ambient->smk_known,
1738 					     asize);
1739 	else
1740 		rc = -EINVAL;
1741 
1742 	mutex_unlock(&smack_ambient_lock);
1743 
1744 	return rc;
1745 }
1746 
1747 /**
1748  * smk_write_ambient - write() for /smack/ambient
1749  * @file: file pointer, not actually used
1750  * @buf: where to get the data from
1751  * @count: bytes sent
1752  * @ppos: where to start
1753  *
1754  * Returns number of bytes written or error code, as appropriate
1755  */
1756 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1757 				 size_t count, loff_t *ppos)
1758 {
1759 	struct smack_known *skp;
1760 	char *oldambient;
1761 	char *data;
1762 	int rc = count;
1763 
1764 	if (!smack_privileged(CAP_MAC_ADMIN))
1765 		return -EPERM;
1766 
1767 	/* Enough data must be present */
1768 	if (count == 0 || count > PAGE_SIZE)
1769 		return -EINVAL;
1770 
1771 	data = memdup_user_nul(buf, count);
1772 	if (IS_ERR(data))
1773 		return PTR_ERR(data);
1774 
1775 	skp = smk_import_entry(data, count);
1776 	if (IS_ERR(skp)) {
1777 		rc = PTR_ERR(skp);
1778 		goto out;
1779 	}
1780 
1781 	mutex_lock(&smack_ambient_lock);
1782 
1783 	oldambient = smack_net_ambient->smk_known;
1784 	smack_net_ambient = skp;
1785 	smk_unlbl_ambient(oldambient);
1786 
1787 	mutex_unlock(&smack_ambient_lock);
1788 
1789 out:
1790 	kfree(data);
1791 	return rc;
1792 }
1793 
1794 static const struct file_operations smk_ambient_ops = {
1795 	.read		= smk_read_ambient,
1796 	.write		= smk_write_ambient,
1797 	.llseek		= default_llseek,
1798 };
1799 
1800 /*
1801  * Seq_file operations for /smack/onlycap
1802  */
1803 static void *onlycap_seq_start(struct seq_file *s, loff_t *pos)
1804 {
1805 	return smk_seq_start(s, pos, &smack_onlycap_list);
1806 }
1807 
1808 static void *onlycap_seq_next(struct seq_file *s, void *v, loff_t *pos)
1809 {
1810 	return smk_seq_next(s, v, pos, &smack_onlycap_list);
1811 }
1812 
1813 static int onlycap_seq_show(struct seq_file *s, void *v)
1814 {
1815 	struct list_head *list = v;
1816 	struct smack_known_list_elem *sklep =
1817 		list_entry_rcu(list, struct smack_known_list_elem, list);
1818 
1819 	seq_puts(s, sklep->smk_label->smk_known);
1820 	seq_putc(s, ' ');
1821 
1822 	return 0;
1823 }
1824 
1825 static const struct seq_operations onlycap_seq_ops = {
1826 	.start = onlycap_seq_start,
1827 	.next  = onlycap_seq_next,
1828 	.show  = onlycap_seq_show,
1829 	.stop  = smk_seq_stop,
1830 };
1831 
1832 static int smk_open_onlycap(struct inode *inode, struct file *file)
1833 {
1834 	return seq_open(file, &onlycap_seq_ops);
1835 }
1836 
1837 /**
1838  * smk_list_swap_rcu - swap public list with a private one in RCU-safe way
1839  * The caller must hold appropriate mutex to prevent concurrent modifications
1840  * to the public list.
1841  * Private list is assumed to be not accessible to other threads yet.
1842  *
1843  * @public: public list
1844  * @private: private list
1845  */
1846 static void smk_list_swap_rcu(struct list_head *public,
1847 			      struct list_head *private)
1848 {
1849 	struct list_head *first, *last;
1850 
1851 	if (list_empty(public)) {
1852 		list_splice_init_rcu(private, public, synchronize_rcu);
1853 	} else {
1854 		/* Remember public list before replacing it */
1855 		first = public->next;
1856 		last = public->prev;
1857 
1858 		/* Publish private list in place of public in RCU-safe way */
1859 		private->prev->next = public;
1860 		private->next->prev = public;
1861 		rcu_assign_pointer(public->next, private->next);
1862 		public->prev = private->prev;
1863 
1864 		synchronize_rcu();
1865 
1866 		/* When all readers are done with the old public list,
1867 		 * attach it in place of private */
1868 		private->next = first;
1869 		private->prev = last;
1870 		first->prev = private;
1871 		last->next = private;
1872 	}
1873 }
1874 
1875 /**
1876  * smk_parse_label_list - parse list of Smack labels, separated by spaces
1877  *
1878  * @data: the string to parse
1879  * @list: destination list
1880  *
1881  * Returns zero on success or error code, as appropriate
1882  */
1883 static int smk_parse_label_list(char *data, struct list_head *list)
1884 {
1885 	char *tok;
1886 	struct smack_known *skp;
1887 	struct smack_known_list_elem *sklep;
1888 
1889 	while ((tok = strsep(&data, " ")) != NULL) {
1890 		if (!*tok)
1891 			continue;
1892 
1893 		skp = smk_import_entry(tok, 0);
1894 		if (IS_ERR(skp))
1895 			return PTR_ERR(skp);
1896 
1897 		sklep = kzalloc_obj(*sklep);
1898 		if (sklep == NULL)
1899 			return -ENOMEM;
1900 
1901 		sklep->smk_label = skp;
1902 		list_add(&sklep->list, list);
1903 	}
1904 
1905 	return 0;
1906 }
1907 
1908 /**
1909  * smk_destroy_label_list - destroy a list of smack_known_list_elem
1910  * @list: header pointer of the list to destroy
1911  */
1912 void smk_destroy_label_list(struct list_head *list)
1913 {
1914 	struct smack_known_list_elem *sklep;
1915 	struct smack_known_list_elem *sklep2;
1916 
1917 	list_for_each_entry_safe(sklep, sklep2, list, list)
1918 		kfree(sklep);
1919 
1920 	INIT_LIST_HEAD(list);
1921 }
1922 
1923 /**
1924  * smk_write_onlycap - write() for smackfs/onlycap
1925  * @file: file pointer, not actually used
1926  * @buf: where to get the data from
1927  * @count: bytes sent
1928  * @ppos: where to start
1929  *
1930  * Returns number of bytes written or error code, as appropriate
1931  */
1932 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1933 				 size_t count, loff_t *ppos)
1934 {
1935 	char *data;
1936 	LIST_HEAD(list_tmp);
1937 	int rc;
1938 
1939 	if (!smack_privileged(CAP_MAC_ADMIN))
1940 		return -EPERM;
1941 
1942 	if (count > PAGE_SIZE)
1943 		return -EINVAL;
1944 
1945 	data = memdup_user_nul(buf, count);
1946 	if (IS_ERR(data))
1947 		return PTR_ERR(data);
1948 
1949 	rc = smk_parse_label_list(data, &list_tmp);
1950 	kfree(data);
1951 
1952 	/*
1953 	 * Clear the smack_onlycap on invalid label errors. This means
1954 	 * that we can pass a null string to unset the onlycap value.
1955 	 *
1956 	 * Importing will also reject a label beginning with '-',
1957 	 * so "-usecapabilities" will also work.
1958 	 *
1959 	 * But do so only on invalid label, not on system errors.
1960 	 * The invalid label must be first to count as clearing attempt.
1961 	 */
1962 	if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
1963 		mutex_lock(&smack_onlycap_lock);
1964 		smk_list_swap_rcu(&smack_onlycap_list, &list_tmp);
1965 		mutex_unlock(&smack_onlycap_lock);
1966 		rc = count;
1967 	}
1968 
1969 	smk_destroy_label_list(&list_tmp);
1970 
1971 	return rc;
1972 }
1973 
1974 static const struct file_operations smk_onlycap_ops = {
1975 	.open		= smk_open_onlycap,
1976 	.read		= seq_read,
1977 	.write		= smk_write_onlycap,
1978 	.llseek		= seq_lseek,
1979 	.release	= seq_release,
1980 };
1981 
1982 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
1983 /**
1984  * smk_read_unconfined - read() for smackfs/unconfined
1985  * @filp: file pointer, not actually used
1986  * @buf: where to put the result
1987  * @cn: maximum to send along
1988  * @ppos: where to start
1989  *
1990  * Returns number of bytes read or error code, as appropriate
1991  */
1992 static ssize_t smk_read_unconfined(struct file *filp, char __user *buf,
1993 					size_t cn, loff_t *ppos)
1994 {
1995 	char *smack = "";
1996 	ssize_t rc = -EINVAL;
1997 	int asize;
1998 
1999 	if (*ppos != 0)
2000 		return 0;
2001 
2002 	if (smack_unconfined != NULL)
2003 		smack = smack_unconfined->smk_known;
2004 
2005 	asize = strlen(smack) + 1;
2006 
2007 	if (cn >= asize)
2008 		rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
2009 
2010 	return rc;
2011 }
2012 
2013 /**
2014  * smk_write_unconfined - write() for smackfs/unconfined
2015  * @file: file pointer, not actually used
2016  * @buf: where to get the data from
2017  * @count: bytes sent
2018  * @ppos: where to start
2019  *
2020  * Returns number of bytes written or error code, as appropriate
2021  */
2022 static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
2023 					size_t count, loff_t *ppos)
2024 {
2025 	char *data;
2026 	struct smack_known *skp;
2027 	int rc = count;
2028 
2029 	if (!smack_privileged(CAP_MAC_ADMIN))
2030 		return -EPERM;
2031 
2032 	if (count > PAGE_SIZE)
2033 		return -EINVAL;
2034 
2035 	data = memdup_user_nul(buf, count);
2036 	if (IS_ERR(data))
2037 		return PTR_ERR(data);
2038 
2039 	/*
2040 	 * Clear the smack_unconfined on invalid label errors. This means
2041 	 * that we can pass a null string to unset the unconfined value.
2042 	 *
2043 	 * Importing will also reject a label beginning with '-',
2044 	 * so "-confine" will also work.
2045 	 *
2046 	 * But do so only on invalid label, not on system errors.
2047 	 */
2048 	skp = smk_import_entry(data, count);
2049 	if (PTR_ERR(skp) == -EINVAL)
2050 		skp = NULL;
2051 	else if (IS_ERR(skp)) {
2052 		rc = PTR_ERR(skp);
2053 		goto freeout;
2054 	}
2055 
2056 	smack_unconfined = skp;
2057 
2058 freeout:
2059 	kfree(data);
2060 	return rc;
2061 }
2062 
2063 static const struct file_operations smk_unconfined_ops = {
2064 	.read		= smk_read_unconfined,
2065 	.write		= smk_write_unconfined,
2066 	.llseek		= default_llseek,
2067 };
2068 #endif /* CONFIG_SECURITY_SMACK_BRINGUP */
2069 
2070 #ifdef CONFIG_AUDIT
2071 /**
2072  * smk_read_logging - read() for /smack/logging
2073  * @filp: file pointer, not actually used
2074  * @buf: where to put the result
2075  * @count: maximum to send along
2076  * @ppos: where to start
2077  *
2078  * Returns number of bytes read or error code, as appropriate
2079  */
2080 static ssize_t smk_read_logging(struct file *filp, char __user *buf,
2081 				size_t count, loff_t *ppos)
2082 {
2083 	char temp[32];
2084 	ssize_t rc;
2085 
2086 	if (*ppos != 0)
2087 		return 0;
2088 
2089 	sprintf(temp, "%d\n", log_policy);
2090 	rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2091 	return rc;
2092 }
2093 
2094 /**
2095  * smk_write_logging - write() for /smack/logging
2096  * @file: file pointer, not actually used
2097  * @buf: where to get the data from
2098  * @count: bytes sent
2099  * @ppos: where to start
2100  *
2101  * Returns number of bytes written or error code, as appropriate
2102  */
2103 static ssize_t smk_write_logging(struct file *file, const char __user *buf,
2104 				size_t count, loff_t *ppos)
2105 {
2106 	int i, ret;
2107 
2108 	if (!smack_privileged(CAP_MAC_ADMIN))
2109 		return -EPERM;
2110 
2111 	ret = kstrtos32_from_user(buf, count, 10, &i);
2112 	if (unlikely(ret))
2113 		return ret;
2114 
2115 	if (i < 0 || i > 3)
2116 		return -EINVAL;
2117 	log_policy = i;
2118 	return count;
2119 }
2120 
2121 
2122 
2123 static const struct file_operations smk_logging_ops = {
2124 	.read		= smk_read_logging,
2125 	.write		= smk_write_logging,
2126 	.llseek		= default_llseek,
2127 };
2128 #endif /* CONFIG_AUDIT */
2129 
2130 /*
2131  * Seq_file read operations for /smack/load-self
2132  */
2133 
2134 static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
2135 {
2136 	struct task_smack *tsp = smack_cred(current_cred());
2137 
2138 	return smk_seq_start(s, pos, &tsp->smk_rules);
2139 }
2140 
2141 static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
2142 {
2143 	struct task_smack *tsp = smack_cred(current_cred());
2144 
2145 	return smk_seq_next(s, v, pos, &tsp->smk_rules);
2146 }
2147 
2148 static int load_self_seq_show(struct seq_file *s, void *v)
2149 {
2150 	struct list_head *list = v;
2151 	struct smack_rule *srp =
2152 		list_entry_rcu(list, struct smack_rule, list);
2153 
2154 	smk_rule_show(s, srp, SMK_LABELLEN);
2155 
2156 	return 0;
2157 }
2158 
2159 static const struct seq_operations load_self_seq_ops = {
2160 	.start = load_self_seq_start,
2161 	.next  = load_self_seq_next,
2162 	.show  = load_self_seq_show,
2163 	.stop  = smk_seq_stop,
2164 };
2165 
2166 
2167 /**
2168  * smk_open_load_self - open() for /smack/load-self2
2169  * @inode: inode structure representing file
2170  * @file: "load" file pointer
2171  *
2172  * For reading, use load_seq_* seq_file reading operations.
2173  */
2174 static int smk_open_load_self(struct inode *inode, struct file *file)
2175 {
2176 	return seq_open(file, &load_self_seq_ops);
2177 }
2178 
2179 /**
2180  * smk_write_load_self - write() for /smack/load-self
2181  * @file: file pointer, not actually used
2182  * @buf: where to get the data from
2183  * @count: bytes sent
2184  * @ppos: where to start - must be 0
2185  *
2186  */
2187 static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
2188 			      size_t count, loff_t *ppos)
2189 {
2190 	struct task_smack *tsp = smack_cred(current_cred());
2191 
2192 	return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2193 				    &tsp->smk_rules_lock, SMK_FIXED24_FMT);
2194 }
2195 
2196 static const struct file_operations smk_load_self_ops = {
2197 	.open           = smk_open_load_self,
2198 	.read		= seq_read,
2199 	.llseek         = seq_lseek,
2200 	.write		= smk_write_load_self,
2201 	.release        = seq_release,
2202 };
2203 
2204 /**
2205  * smk_user_access - handle access check transaction
2206  * @file: file pointer
2207  * @buf: data from user space
2208  * @count: bytes sent
2209  * @ppos: where to start - must be 0
2210  * @format: /smack/load or /smack/load2 or /smack/change-rule format.
2211  */
2212 static ssize_t smk_user_access(struct file *file, const char __user *buf,
2213 				size_t count, loff_t *ppos, int format)
2214 {
2215 	struct smack_parsed_rule rule;
2216 	char *data;
2217 	int res;
2218 
2219 	data = simple_transaction_get(file, buf, count);
2220 	if (IS_ERR(data))
2221 		return PTR_ERR(data);
2222 
2223 	if (format == SMK_FIXED24_FMT) {
2224 		if (count < SMK_LOADLEN)
2225 			return -EINVAL;
2226 		res = smk_parse_rule(data, &rule, 0);
2227 	} else {
2228 		/*
2229 		 * simple_transaction_get() returns null-terminated data
2230 		 */
2231 		res = smk_parse_long_rule(data, &rule, 0, 3);
2232 	}
2233 
2234 	if (res >= 0)
2235 		res = smk_access(rule.smk_subject, rule.smk_object,
2236 				 rule.smk_access1, NULL);
2237 	else if (res != -ENOENT)
2238 		return res;
2239 
2240 	/*
2241 	 * smk_access() can return a value > 0 in the "bringup" case.
2242 	 */
2243 	data[0] = res >= 0 ? '1' : '0';
2244 	data[1] = '\0';
2245 
2246 	simple_transaction_set(file, 2);
2247 
2248 	if (format == SMK_FIXED24_FMT)
2249 		return SMK_LOADLEN;
2250 	return count;
2251 }
2252 
2253 /**
2254  * smk_write_access - handle access check transaction
2255  * @file: file pointer
2256  * @buf: data from user space
2257  * @count: bytes sent
2258  * @ppos: where to start - must be 0
2259  */
2260 static ssize_t smk_write_access(struct file *file, const char __user *buf,
2261 				size_t count, loff_t *ppos)
2262 {
2263 	return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
2264 }
2265 
2266 static const struct file_operations smk_access_ops = {
2267 	.write		= smk_write_access,
2268 	.read		= simple_transaction_read,
2269 	.release	= simple_transaction_release,
2270 	.llseek		= generic_file_llseek,
2271 };
2272 
2273 
2274 /*
2275  * Seq_file read operations for /smack/load2
2276  */
2277 
2278 static int load2_seq_show(struct seq_file *s, void *v)
2279 {
2280 	struct list_head *list = v;
2281 	struct smack_rule *srp;
2282 	struct smack_known *skp =
2283 		list_entry_rcu(list, struct smack_known, list);
2284 
2285 	list_for_each_entry_rcu(srp, &skp->smk_rules, list)
2286 		smk_rule_show(s, srp, SMK_LONGLABEL);
2287 
2288 	return 0;
2289 }
2290 
2291 static const struct seq_operations load2_seq_ops = {
2292 	.start = load2_seq_start,
2293 	.next  = load2_seq_next,
2294 	.show  = load2_seq_show,
2295 	.stop  = smk_seq_stop,
2296 };
2297 
2298 /**
2299  * smk_open_load2 - open() for /smack/load2
2300  * @inode: inode structure representing file
2301  * @file: "load2" file pointer
2302  *
2303  * For reading, use load2_seq_* seq_file reading operations.
2304  */
2305 static int smk_open_load2(struct inode *inode, struct file *file)
2306 {
2307 	return seq_open(file, &load2_seq_ops);
2308 }
2309 
2310 /**
2311  * smk_write_load2 - write() for /smack/load2
2312  * @file: file pointer, not actually used
2313  * @buf: where to get the data from
2314  * @count: bytes sent
2315  * @ppos: where to start - must be 0
2316  *
2317  */
2318 static ssize_t smk_write_load2(struct file *file, const char __user *buf,
2319 				size_t count, loff_t *ppos)
2320 {
2321 	/*
2322 	 * Must have privilege.
2323 	 */
2324 	if (!smack_privileged(CAP_MAC_ADMIN))
2325 		return -EPERM;
2326 
2327 	return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2328 				    SMK_LONG_FMT);
2329 }
2330 
2331 static const struct file_operations smk_load2_ops = {
2332 	.open           = smk_open_load2,
2333 	.read		= seq_read,
2334 	.llseek         = seq_lseek,
2335 	.write		= smk_write_load2,
2336 	.release        = seq_release,
2337 };
2338 
2339 /*
2340  * Seq_file read operations for /smack/load-self2
2341  */
2342 
2343 static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
2344 {
2345 	struct task_smack *tsp = smack_cred(current_cred());
2346 
2347 	return smk_seq_start(s, pos, &tsp->smk_rules);
2348 }
2349 
2350 static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
2351 {
2352 	struct task_smack *tsp = smack_cred(current_cred());
2353 
2354 	return smk_seq_next(s, v, pos, &tsp->smk_rules);
2355 }
2356 
2357 static int load_self2_seq_show(struct seq_file *s, void *v)
2358 {
2359 	struct list_head *list = v;
2360 	struct smack_rule *srp =
2361 		list_entry_rcu(list, struct smack_rule, list);
2362 
2363 	smk_rule_show(s, srp, SMK_LONGLABEL);
2364 
2365 	return 0;
2366 }
2367 
2368 static const struct seq_operations load_self2_seq_ops = {
2369 	.start = load_self2_seq_start,
2370 	.next  = load_self2_seq_next,
2371 	.show  = load_self2_seq_show,
2372 	.stop  = smk_seq_stop,
2373 };
2374 
2375 /**
2376  * smk_open_load_self2 - open() for /smack/load-self2
2377  * @inode: inode structure representing file
2378  * @file: "load" file pointer
2379  *
2380  * For reading, use load_seq_* seq_file reading operations.
2381  */
2382 static int smk_open_load_self2(struct inode *inode, struct file *file)
2383 {
2384 	return seq_open(file, &load_self2_seq_ops);
2385 }
2386 
2387 /**
2388  * smk_write_load_self2 - write() for /smack/load-self2
2389  * @file: file pointer, not actually used
2390  * @buf: where to get the data from
2391  * @count: bytes sent
2392  * @ppos: where to start - must be 0
2393  *
2394  */
2395 static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2396 			      size_t count, loff_t *ppos)
2397 {
2398 	struct task_smack *tsp = smack_cred(current_cred());
2399 
2400 	return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2401 				    &tsp->smk_rules_lock, SMK_LONG_FMT);
2402 }
2403 
2404 static const struct file_operations smk_load_self2_ops = {
2405 	.open           = smk_open_load_self2,
2406 	.read		= seq_read,
2407 	.llseek         = seq_lseek,
2408 	.write		= smk_write_load_self2,
2409 	.release        = seq_release,
2410 };
2411 
2412 /**
2413  * smk_write_access2 - handle access check transaction
2414  * @file: file pointer
2415  * @buf: data from user space
2416  * @count: bytes sent
2417  * @ppos: where to start - must be 0
2418  */
2419 static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2420 					size_t count, loff_t *ppos)
2421 {
2422 	return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2423 }
2424 
2425 static const struct file_operations smk_access2_ops = {
2426 	.write		= smk_write_access2,
2427 	.read		= simple_transaction_read,
2428 	.release	= simple_transaction_release,
2429 	.llseek		= generic_file_llseek,
2430 };
2431 
2432 /**
2433  * smk_write_revoke_subj - write() for /smack/revoke-subject
2434  * @file: file pointer
2435  * @buf: data from user space
2436  * @count: bytes sent
2437  * @ppos: where to start - must be 0
2438  */
2439 static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2440 				size_t count, loff_t *ppos)
2441 {
2442 	char *data;
2443 	const char *cp;
2444 	struct smack_known *skp;
2445 	struct smack_rule *sp;
2446 	struct list_head *rule_list;
2447 	struct mutex *rule_lock;
2448 	int rc = count;
2449 
2450 	if (*ppos != 0)
2451 		return -EINVAL;
2452 
2453 	if (!smack_privileged(CAP_MAC_ADMIN))
2454 		return -EPERM;
2455 
2456 	if (count == 0 || count > SMK_LONGLABEL)
2457 		return -EINVAL;
2458 
2459 	data = memdup_user(buf, count);
2460 	if (IS_ERR(data))
2461 		return PTR_ERR(data);
2462 
2463 	cp = smk_parse_smack(data, count);
2464 	if (IS_ERR(cp)) {
2465 		rc = PTR_ERR(cp);
2466 		goto out_data;
2467 	}
2468 
2469 	skp = smk_find_entry(cp);
2470 	if (skp == NULL)
2471 		goto out_cp;
2472 
2473 	rule_list = &skp->smk_rules;
2474 	rule_lock = &skp->smk_rules_lock;
2475 
2476 	mutex_lock(rule_lock);
2477 
2478 	list_for_each_entry_rcu(sp, rule_list, list)
2479 		sp->smk_access = 0;
2480 
2481 	mutex_unlock(rule_lock);
2482 
2483 out_cp:
2484 	kfree(cp);
2485 out_data:
2486 	kfree(data);
2487 
2488 	return rc;
2489 }
2490 
2491 static const struct file_operations smk_revoke_subj_ops = {
2492 	.write		= smk_write_revoke_subj,
2493 	.read		= simple_transaction_read,
2494 	.release	= simple_transaction_release,
2495 	.llseek		= generic_file_llseek,
2496 };
2497 
2498 /**
2499  * smk_init_sysfs - initialize /sys/fs/smackfs
2500  *
2501  */
2502 static int smk_init_sysfs(void)
2503 {
2504 	return sysfs_create_mount_point(fs_kobj, "smackfs");
2505 }
2506 
2507 /**
2508  * smk_write_change_rule - write() for /smack/change-rule
2509  * @file: file pointer
2510  * @buf: data from user space
2511  * @count: bytes sent
2512  * @ppos: where to start - must be 0
2513  */
2514 static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2515 				size_t count, loff_t *ppos)
2516 {
2517 	/*
2518 	 * Must have privilege.
2519 	 */
2520 	if (!smack_privileged(CAP_MAC_ADMIN))
2521 		return -EPERM;
2522 
2523 	return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2524 				    SMK_CHANGE_FMT);
2525 }
2526 
2527 static const struct file_operations smk_change_rule_ops = {
2528 	.write		= smk_write_change_rule,
2529 	.read		= simple_transaction_read,
2530 	.release	= simple_transaction_release,
2531 	.llseek		= generic_file_llseek,
2532 };
2533 
2534 /**
2535  * smk_read_syslog - read() for smackfs/syslog
2536  * @filp: file pointer, not actually used
2537  * @buf: where to put the result
2538  * @cn: maximum to send along
2539  * @ppos: where to start
2540  *
2541  * Returns number of bytes read or error code, as appropriate
2542  */
2543 static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
2544 				size_t cn, loff_t *ppos)
2545 {
2546 	struct smack_known *skp;
2547 	ssize_t rc = -EINVAL;
2548 	int asize;
2549 
2550 	if (*ppos != 0)
2551 		return 0;
2552 
2553 	if (smack_syslog_label == NULL)
2554 		skp = &smack_known_star;
2555 	else
2556 		skp = smack_syslog_label;
2557 
2558 	asize = strlen(skp->smk_known) + 1;
2559 
2560 	if (cn >= asize)
2561 		rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
2562 						asize);
2563 
2564 	return rc;
2565 }
2566 
2567 /**
2568  * smk_write_syslog - write() for smackfs/syslog
2569  * @file: file pointer, not actually used
2570  * @buf: where to get the data from
2571  * @count: bytes sent
2572  * @ppos: where to start
2573  *
2574  * Returns number of bytes written or error code, as appropriate
2575  */
2576 static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
2577 				size_t count, loff_t *ppos)
2578 {
2579 	char *data;
2580 	struct smack_known *skp;
2581 	int rc = count;
2582 
2583 	if (!smack_privileged(CAP_MAC_ADMIN))
2584 		return -EPERM;
2585 
2586 	/* Enough data must be present */
2587 	if (count == 0 || count > PAGE_SIZE)
2588 		return -EINVAL;
2589 
2590 	data = memdup_user_nul(buf, count);
2591 	if (IS_ERR(data))
2592 		return PTR_ERR(data);
2593 
2594 	skp = smk_import_entry(data, count);
2595 	if (IS_ERR(skp))
2596 		rc = PTR_ERR(skp);
2597 	else
2598 		smack_syslog_label = skp;
2599 
2600 	kfree(data);
2601 	return rc;
2602 }
2603 
2604 static const struct file_operations smk_syslog_ops = {
2605 	.read		= smk_read_syslog,
2606 	.write		= smk_write_syslog,
2607 	.llseek		= default_llseek,
2608 };
2609 
2610 /*
2611  * Seq_file read operations for /smack/relabel-self
2612  */
2613 
2614 static void *relabel_self_seq_start(struct seq_file *s, loff_t *pos)
2615 {
2616 	struct task_smack *tsp = smack_cred(current_cred());
2617 
2618 	return smk_seq_start(s, pos, &tsp->smk_relabel);
2619 }
2620 
2621 static void *relabel_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
2622 {
2623 	struct task_smack *tsp = smack_cred(current_cred());
2624 
2625 	return smk_seq_next(s, v, pos, &tsp->smk_relabel);
2626 }
2627 
2628 static int relabel_self_seq_show(struct seq_file *s, void *v)
2629 {
2630 	struct list_head *list = v;
2631 	struct smack_known_list_elem *sklep =
2632 		list_entry(list, struct smack_known_list_elem, list);
2633 
2634 	seq_puts(s, sklep->smk_label->smk_known);
2635 	seq_putc(s, ' ');
2636 
2637 	return 0;
2638 }
2639 
2640 static const struct seq_operations relabel_self_seq_ops = {
2641 	.start = relabel_self_seq_start,
2642 	.next  = relabel_self_seq_next,
2643 	.show  = relabel_self_seq_show,
2644 	.stop  = smk_seq_stop,
2645 };
2646 
2647 /**
2648  * smk_open_relabel_self - open() for /smack/relabel-self
2649  * @inode: inode structure representing file
2650  * @file: "relabel-self" file pointer
2651  *
2652  * Connect our relabel_self_seq_* operations with /smack/relabel-self
2653  * file_operations
2654  */
2655 static int smk_open_relabel_self(struct inode *inode, struct file *file)
2656 {
2657 	return seq_open(file, &relabel_self_seq_ops);
2658 }
2659 
2660 /**
2661  * smk_write_relabel_self - write() for /smack/relabel-self
2662  * @file: file pointer, not actually used
2663  * @buf: where to get the data from
2664  * @count: bytes sent
2665  * @ppos: where to start - must be 0
2666  *
2667  */
2668 static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
2669 				size_t count, loff_t *ppos)
2670 {
2671 	char *data;
2672 	int rc;
2673 	LIST_HEAD(list_tmp);
2674 
2675 	/*
2676 	 * Must have privilege.
2677 	 */
2678 	if (!smack_privileged(CAP_MAC_ADMIN))
2679 		return -EPERM;
2680 
2681 	/*
2682 	 * No partial write.
2683 	 * Enough data must be present.
2684 	 */
2685 	if (*ppos != 0)
2686 		return -EINVAL;
2687 	if (count == 0 || count > PAGE_SIZE)
2688 		return -EINVAL;
2689 
2690 	data = memdup_user_nul(buf, count);
2691 	if (IS_ERR(data))
2692 		return PTR_ERR(data);
2693 
2694 	rc = smk_parse_label_list(data, &list_tmp);
2695 	kfree(data);
2696 
2697 	if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
2698 		struct cred *new;
2699 		struct task_smack *tsp;
2700 
2701 		new = prepare_creds();
2702 		if (!new) {
2703 			rc = -ENOMEM;
2704 			goto out;
2705 		}
2706 		tsp = smack_cred(new);
2707 		smk_destroy_label_list(&tsp->smk_relabel);
2708 		list_splice(&list_tmp, &tsp->smk_relabel);
2709 		commit_creds(new);
2710 		return count;
2711 	}
2712 out:
2713 	smk_destroy_label_list(&list_tmp);
2714 	return rc;
2715 }
2716 
2717 static const struct file_operations smk_relabel_self_ops = {
2718 	.open		= smk_open_relabel_self,
2719 	.read		= seq_read,
2720 	.llseek		= seq_lseek,
2721 	.write		= smk_write_relabel_self,
2722 	.release	= seq_release,
2723 };
2724 
2725 /**
2726  * smk_read_ptrace - read() for /smack/ptrace
2727  * @filp: file pointer, not actually used
2728  * @buf: where to put the result
2729  * @count: maximum to send along
2730  * @ppos: where to start
2731  *
2732  * Returns number of bytes read or error code, as appropriate
2733  */
2734 static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
2735 			       size_t count, loff_t *ppos)
2736 {
2737 	char temp[32];
2738 	ssize_t rc;
2739 
2740 	if (*ppos != 0)
2741 		return 0;
2742 
2743 	sprintf(temp, "%d\n", smack_ptrace_rule);
2744 	rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2745 	return rc;
2746 }
2747 
2748 /**
2749  * smk_write_ptrace - write() for /smack/ptrace
2750  * @file: file pointer
2751  * @buf: data from user space
2752  * @count: bytes sent
2753  * @ppos: where to start - must be 0
2754  */
2755 static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
2756 				size_t count, loff_t *ppos)
2757 {
2758 	int i, ret;
2759 
2760 	if (!smack_privileged(CAP_MAC_ADMIN))
2761 		return -EPERM;
2762 
2763 	ret = kstrtos32_from_user(buf, count, 10, &i);
2764 	if (unlikely(ret))
2765 		return ret;
2766 
2767 	if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
2768 		return -EINVAL;
2769 	smack_ptrace_rule = i;
2770 
2771 	return count;
2772 }
2773 
2774 static const struct file_operations smk_ptrace_ops = {
2775 	.write		= smk_write_ptrace,
2776 	.read		= smk_read_ptrace,
2777 	.llseek		= default_llseek,
2778 };
2779 
2780 /**
2781  * smk_fill_super - fill the smackfs superblock
2782  * @sb: the empty superblock
2783  * @fc: unused
2784  *
2785  * Fill in the well known entries for the smack filesystem
2786  *
2787  * Returns 0 on success, an error code on failure
2788  */
2789 static int smk_fill_super(struct super_block *sb, struct fs_context *fc)
2790 {
2791 	int rc;
2792 
2793 	static const struct tree_descr smack_files[] = {
2794 		[SMK_LOAD] = {
2795 			"load", &smk_load_ops, S_IRUGO|S_IWUSR},
2796 		[SMK_CIPSO] = {
2797 			"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2798 		[SMK_DOI] = {
2799 			"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2800 		[SMK_DIRECT] = {
2801 			"direct", &smk_cipso_auto_level_ops, 0644},
2802 		[SMK_AMBIENT] = {
2803 			"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2804 		[SMK_NET4ADDR] = {
2805 			"netlabel", &smk_net4addr_ops, S_IRUGO|S_IWUSR},
2806 		[SMK_ONLYCAP] = {
2807 			"onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2808 #ifdef CONFIG_AUDIT
2809 		[SMK_LOGGING] = {
2810 			"logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2811 #endif /* CONFIG_AUDIT */
2812 		[SMK_LOAD_SELF] = {
2813 			"load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
2814 		[SMK_ACCESSES] = {
2815 			"access", &smk_access_ops, S_IRUGO|S_IWUGO},
2816 		[SMK_MAPPED] = {
2817 			"mapped", &smk_cipso_auto_level_ops, 0644},
2818 		[SMK_LOAD2] = {
2819 			"load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2820 		[SMK_LOAD_SELF2] = {
2821 			"load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2822 		[SMK_ACCESS2] = {
2823 			"access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2824 		[SMK_CIPSO2] = {
2825 			"cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
2826 		[SMK_REVOKE_SUBJ] = {
2827 			"revoke-subject", &smk_revoke_subj_ops,
2828 			S_IRUGO|S_IWUSR},
2829 		[SMK_CHANGE_RULE] = {
2830 			"change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
2831 		[SMK_SYSLOG] = {
2832 			"syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
2833 		[SMK_PTRACE] = {
2834 			"ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
2835 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
2836 		[SMK_UNCONFINED] = {
2837 			"unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR},
2838 #endif
2839 #if IS_ENABLED(CONFIG_IPV6)
2840 		[SMK_NET6ADDR] = {
2841 			"ipv6host", &smk_net6addr_ops, S_IRUGO|S_IWUSR},
2842 #endif /* CONFIG_IPV6 */
2843 		[SMK_RELABEL_SELF] = {
2844 			"relabel-self", &smk_relabel_self_ops,
2845 				S_IRUGO|S_IWUGO},
2846 		/* last one */
2847 			{""}
2848 	};
2849 
2850 	rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2851 	if (rc != 0) {
2852 		printk(KERN_ERR "%s failed %d while creating inodes\n",
2853 			__func__, rc);
2854 		return rc;
2855 	}
2856 
2857 	return 0;
2858 }
2859 
2860 /**
2861  * smk_get_tree - get the smackfs superblock
2862  * @fc: The mount context, including any options
2863  *
2864  * Just passes everything along.
2865  *
2866  * Returns what the lower level code does.
2867  */
2868 static int smk_get_tree(struct fs_context *fc)
2869 {
2870 	return get_tree_single(fc, smk_fill_super);
2871 }
2872 
2873 static const struct fs_context_operations smk_context_ops = {
2874 	.get_tree	= smk_get_tree,
2875 };
2876 
2877 /**
2878  * smk_init_fs_context - Initialise a filesystem context for smackfs
2879  * @fc: The blank mount context
2880  */
2881 static int smk_init_fs_context(struct fs_context *fc)
2882 {
2883 	fc->ops = &smk_context_ops;
2884 	return 0;
2885 }
2886 
2887 static struct file_system_type smk_fs_type = {
2888 	.name		= "smackfs",
2889 	.init_fs_context = smk_init_fs_context,
2890 	.kill_sb	= kill_anon_super,
2891 };
2892 
2893 static struct vfsmount *smackfs_mount;
2894 
2895 /**
2896  * init_smk_fs - get the smackfs superblock
2897  *
2898  * register the smackfs
2899  *
2900  * Do not register smackfs if Smack wasn't enabled
2901  * on boot. We can not put this method normally under the
2902  * smack_init() code path since the security subsystem get
2903  * initialized before the vfs caches.
2904  *
2905  * Returns true if we were not chosen on boot or if
2906  * we were chosen and filesystem registration succeeded.
2907  */
2908 int __init init_smk_fs(void)
2909 {
2910 	int err;
2911 	int rc;
2912 	struct netlbl_audit nai;
2913 
2914 	if (smack_enabled == 0)
2915 		return 0;
2916 
2917 	err = smk_init_sysfs();
2918 	if (err)
2919 		printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2920 
2921 	err = register_filesystem(&smk_fs_type);
2922 	if (!err) {
2923 		smackfs_mount = kern_mount(&smk_fs_type);
2924 		if (IS_ERR(smackfs_mount)) {
2925 			printk(KERN_ERR "smackfs:  could not mount!\n");
2926 			err = PTR_ERR(smackfs_mount);
2927 			smackfs_mount = NULL;
2928 		}
2929 	}
2930 
2931 	smk_netlabel_audit_set(&nai);
2932 	(void) netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
2933 	(void) smk_cipso_doi(SMACK_CIPSO_DOI_DEFAULT,
2934 			     GFP_KERNEL | __GFP_NOFAIL);
2935 	smk_unlbl_ambient(NULL);
2936 
2937 	rc = smack_populate_secattr(&smack_known_floor);
2938 	if (err == 0 && rc < 0)
2939 		err = rc;
2940 	rc = smack_populate_secattr(&smack_known_hat);
2941 	if (err == 0 && rc < 0)
2942 		err = rc;
2943 	rc = smack_populate_secattr(&smack_known_huh);
2944 	if (err == 0 && rc < 0)
2945 		err = rc;
2946 	rc = smack_populate_secattr(&smack_known_star);
2947 	if (err == 0 && rc < 0)
2948 		err = rc;
2949 	rc = smack_populate_secattr(&smack_known_web);
2950 	if (err == 0 && rc < 0)
2951 		err = rc;
2952 
2953 	return err;
2954 }
2955