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