mm/slub.c: fix corrupted freechain in deactivate_slab()
[linux/fpc-iii.git] / security / smack / smackfs.c
blob3397b216bc6ca969f04fd1e29dfa23aab3585393
1 /*
2 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2.
8 * Authors:
9 * Casey Schaufler <casey@schaufler-ca.com>
10 * Ahmed S. Darwish <darwish.07@gmail.com>
12 * Special thanks to the authors of selinuxfs.
14 * Karl MacMillan <kmacmillan@tresys.com>
15 * James Morris <jmorris@redhat.com>
19 #include <linux/kernel.h>
20 #include <linux/vmalloc.h>
21 #include <linux/security.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <net/net_namespace.h>
25 #include <net/cipso_ipv4.h>
26 #include <linux/seq_file.h>
27 #include <linux/ctype.h>
28 #include <linux/audit.h>
29 #include <linux/magic.h>
30 #include "smack.h"
32 #define BEBITS (sizeof(__be32) * 8)
34 * smackfs pseudo filesystem.
37 enum smk_inos {
38 SMK_ROOT_INO = 2,
39 SMK_LOAD = 3, /* load policy */
40 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
41 SMK_DOI = 5, /* CIPSO DOI */
42 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
43 SMK_AMBIENT = 7, /* internet ambient label */
44 SMK_NET4ADDR = 8, /* single label hosts */
45 SMK_ONLYCAP = 9, /* the only "capable" label */
46 SMK_LOGGING = 10, /* logging */
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 */
68 * List locks
70 static DEFINE_MUTEX(smack_cipso_lock);
71 static DEFINE_MUTEX(smack_ambient_lock);
72 static DEFINE_MUTEX(smk_net4addr_lock);
73 #if IS_ENABLED(CONFIG_IPV6)
74 static DEFINE_MUTEX(smk_net6addr_lock);
75 #endif /* CONFIG_IPV6 */
78 * This is the "ambient" label for network traffic.
79 * If it isn't somehow marked, use this.
80 * It can be reset via smackfs/ambient
82 struct smack_known *smack_net_ambient;
85 * This is the level in a CIPSO header that indicates a
86 * smack label is contained directly in the category set.
87 * It can be reset via smackfs/direct
89 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
92 * This is the level in a CIPSO header that indicates a
93 * secid is contained directly in the category set.
94 * It can be reset via smackfs/mapped
96 int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
98 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
100 * Allow one label to be unconfined. This is for
101 * debugging and application bring-up purposes only.
102 * It is bad and wrong, but everyone seems to expect
103 * to have it.
105 struct smack_known *smack_unconfined;
106 #endif
109 * If this value is set restrict syslog use to the label specified.
110 * It can be reset via smackfs/syslog
112 struct smack_known *smack_syslog_label;
115 * Ptrace current rule
116 * SMACK_PTRACE_DEFAULT regular smack ptrace rules (/proc based)
117 * SMACK_PTRACE_EXACT labels must match, but can be overriden with
118 * CAP_SYS_PTRACE
119 * SMACK_PTRACE_DRACONIAN lables must match, CAP_SYS_PTRACE has no effect
121 int smack_ptrace_rule = SMACK_PTRACE_DEFAULT;
124 * Certain IP addresses may be designated as single label hosts.
125 * Packets are sent there unlabeled, but only from tasks that
126 * can write to the specified label.
129 LIST_HEAD(smk_net4addr_list);
130 #if IS_ENABLED(CONFIG_IPV6)
131 LIST_HEAD(smk_net6addr_list);
132 #endif /* CONFIG_IPV6 */
135 * Rule lists are maintained for each label.
136 * This master list is just for reading /smack/load and /smack/load2.
138 struct smack_master_list {
139 struct list_head list;
140 struct smack_rule *smk_rule;
143 static LIST_HEAD(smack_rule_list);
145 struct smack_parsed_rule {
146 struct smack_known *smk_subject;
147 struct smack_known *smk_object;
148 int smk_access1;
149 int smk_access2;
152 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
155 * Values for parsing cipso rules
156 * SMK_DIGITLEN: Length of a digit field in a rule.
157 * SMK_CIPSOMIN: Minimum possible cipso rule length.
158 * SMK_CIPSOMAX: Maximum possible cipso rule length.
160 #define SMK_DIGITLEN 4
161 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
162 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
165 * Values for parsing MAC rules
166 * SMK_ACCESS: Maximum possible combination of access permissions
167 * SMK_ACCESSLEN: Maximum length for a rule access field
168 * SMK_LOADLEN: Smack rule length
170 #define SMK_OACCESS "rwxa"
171 #define SMK_ACCESS "rwxatl"
172 #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
173 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
174 #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
175 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
178 * Stricly for CIPSO level manipulation.
179 * Set the category bit number in a smack label sized buffer.
181 static inline void smack_catset_bit(unsigned int cat, char *catsetp)
183 if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
184 return;
186 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
190 * smk_netlabel_audit_set - fill a netlbl_audit struct
191 * @nap: structure to fill
193 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
195 struct smack_known *skp = smk_of_current();
197 nap->loginuid = audit_get_loginuid(current);
198 nap->sessionid = audit_get_sessionid(current);
199 nap->secid = skp->smk_secid;
203 * Value for parsing single label host rules
204 * "1.2.3.4 X"
206 #define SMK_NETLBLADDRMIN 9
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 * @global: if non-zero, indicates a global rule
215 * Looks through the current subject/object/access list for
216 * the subject/object pair and replaces the access that was
217 * there. If the pair isn't found add it with the specified
218 * access.
220 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
221 * during the allocation of the new pair to add.
223 static int smk_set_access(struct smack_parsed_rule *srp,
224 struct list_head *rule_list,
225 struct mutex *rule_lock, int global)
227 struct smack_rule *sp;
228 struct smack_master_list *smlp;
229 int found = 0;
230 int rc = 0;
232 mutex_lock(rule_lock);
235 * Because the object label is less likely to match
236 * than the subject label check it first
238 list_for_each_entry_rcu(sp, rule_list, list) {
239 if (sp->smk_object == srp->smk_object &&
240 sp->smk_subject == srp->smk_subject) {
241 found = 1;
242 sp->smk_access |= srp->smk_access1;
243 sp->smk_access &= ~srp->smk_access2;
244 break;
248 if (found == 0) {
249 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
250 if (sp == NULL) {
251 rc = -ENOMEM;
252 goto out;
255 sp->smk_subject = srp->smk_subject;
256 sp->smk_object = srp->smk_object;
257 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
259 list_add_rcu(&sp->list, rule_list);
261 * If this is a global as opposed to self and a new rule
262 * it needs to get added for reporting.
264 if (global) {
265 smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
266 if (smlp != NULL) {
267 smlp->smk_rule = sp;
268 list_add_rcu(&smlp->list, &smack_rule_list);
269 } else
270 rc = -ENOMEM;
274 out:
275 mutex_unlock(rule_lock);
276 return rc;
280 * smk_perm_from_str - parse smack accesses from a text string
281 * @string: a text string that contains a Smack accesses code
283 * Returns an integer with respective bits set for specified accesses.
285 static int smk_perm_from_str(const char *string)
287 int perm = 0;
288 const char *cp;
290 for (cp = string; ; cp++)
291 switch (*cp) {
292 case '-':
293 break;
294 case 'r':
295 case 'R':
296 perm |= MAY_READ;
297 break;
298 case 'w':
299 case 'W':
300 perm |= MAY_WRITE;
301 break;
302 case 'x':
303 case 'X':
304 perm |= MAY_EXEC;
305 break;
306 case 'a':
307 case 'A':
308 perm |= MAY_APPEND;
309 break;
310 case 't':
311 case 'T':
312 perm |= MAY_TRANSMUTE;
313 break;
314 case 'l':
315 case 'L':
316 perm |= MAY_LOCK;
317 break;
318 case 'b':
319 case 'B':
320 perm |= MAY_BRINGUP;
321 break;
322 default:
323 return perm;
328 * smk_fill_rule - Fill Smack rule from strings
329 * @subject: subject label string
330 * @object: object label string
331 * @access1: access string
332 * @access2: string with permissions to be removed
333 * @rule: Smack rule
334 * @import: if non-zero, import labels
335 * @len: label length limit
337 * Returns 0 on success, appropriate error code on failure.
339 static int smk_fill_rule(const char *subject, const char *object,
340 const char *access1, const char *access2,
341 struct smack_parsed_rule *rule, int import,
342 int len)
344 const char *cp;
345 struct smack_known *skp;
347 if (import) {
348 rule->smk_subject = smk_import_entry(subject, len);
349 if (IS_ERR(rule->smk_subject))
350 return PTR_ERR(rule->smk_subject);
352 rule->smk_object = smk_import_entry(object, len);
353 if (IS_ERR(rule->smk_object))
354 return PTR_ERR(rule->smk_object);
355 } else {
356 cp = smk_parse_smack(subject, len);
357 if (IS_ERR(cp))
358 return PTR_ERR(cp);
359 skp = smk_find_entry(cp);
360 kfree(cp);
361 if (skp == NULL)
362 return -ENOENT;
363 rule->smk_subject = skp;
365 cp = smk_parse_smack(object, len);
366 if (IS_ERR(cp))
367 return PTR_ERR(cp);
368 skp = smk_find_entry(cp);
369 kfree(cp);
370 if (skp == NULL)
371 return -ENOENT;
372 rule->smk_object = skp;
375 rule->smk_access1 = smk_perm_from_str(access1);
376 if (access2)
377 rule->smk_access2 = smk_perm_from_str(access2);
378 else
379 rule->smk_access2 = ~rule->smk_access1;
381 return 0;
385 * smk_parse_rule - parse Smack rule from load string
386 * @data: string to be parsed whose size is SMK_LOADLEN
387 * @rule: Smack rule
388 * @import: if non-zero, import labels
390 * Returns 0 on success, -1 on errors.
392 static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
393 int import)
395 int rc;
397 rc = smk_fill_rule(data, data + SMK_LABELLEN,
398 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
399 import, SMK_LABELLEN);
400 return rc;
404 * smk_parse_long_rule - parse Smack rule from rule string
405 * @data: string to be parsed, null terminated
406 * @rule: Will be filled with Smack parsed rule
407 * @import: if non-zero, import labels
408 * @tokens: numer of substrings expected in data
410 * Returns number of processed bytes on success, -ERRNO on failure.
412 static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
413 int import, int tokens)
415 ssize_t cnt = 0;
416 char *tok[4];
417 int rc;
418 int i;
421 * Parsing the rule in-place, filling all white-spaces with '\0'
423 for (i = 0; i < tokens; ++i) {
424 while (isspace(data[cnt]))
425 data[cnt++] = '\0';
427 if (data[cnt] == '\0')
428 /* Unexpected end of data */
429 return -EINVAL;
431 tok[i] = data + cnt;
433 while (data[cnt] && !isspace(data[cnt]))
434 ++cnt;
436 while (isspace(data[cnt]))
437 data[cnt++] = '\0';
439 while (i < 4)
440 tok[i++] = NULL;
442 rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
443 return rc == 0 ? cnt : rc;
446 #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
447 #define SMK_LONG_FMT 1 /* Variable long label format */
448 #define SMK_CHANGE_FMT 2 /* Rule modification format */
450 * smk_write_rules_list - write() for any /smack rule file
451 * @file: file pointer, not actually used
452 * @buf: where to get the data from
453 * @count: bytes sent
454 * @ppos: where to start - must be 0
455 * @rule_list: the list of rules to write to
456 * @rule_lock: lock for the rule list
457 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
459 * Get one smack access rule from above.
460 * The format for SMK_LONG_FMT is:
461 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
462 * The format for SMK_FIXED24_FMT is exactly:
463 * "subject object rwxat"
464 * The format for SMK_CHANGE_FMT is:
465 * "subject<whitespace>object<whitespace>
466 * acc_enable<whitespace>acc_disable[<whitespace>...]"
468 static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
469 size_t count, loff_t *ppos,
470 struct list_head *rule_list,
471 struct mutex *rule_lock, int format)
473 struct smack_parsed_rule rule;
474 char *data;
475 int rc;
476 int trunc = 0;
477 int tokens;
478 ssize_t cnt = 0;
481 * No partial writes.
482 * Enough data must be present.
484 if (*ppos != 0)
485 return -EINVAL;
487 if (format == SMK_FIXED24_FMT) {
489 * Minor hack for backward compatibility
491 if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
492 return -EINVAL;
493 } else {
494 if (count >= PAGE_SIZE) {
495 count = PAGE_SIZE - 1;
496 trunc = 1;
500 data = memdup_user_nul(buf, count);
501 if (IS_ERR(data))
502 return PTR_ERR(data);
505 * In case of parsing only part of user buf,
506 * avoid having partial rule at the data buffer
508 if (trunc) {
509 while (count > 0 && (data[count - 1] != '\n'))
510 --count;
511 if (count == 0) {
512 rc = -EINVAL;
513 goto out;
517 data[count] = '\0';
518 tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
519 while (cnt < count) {
520 if (format == SMK_FIXED24_FMT) {
521 rc = smk_parse_rule(data, &rule, 1);
522 if (rc < 0)
523 goto out;
524 cnt = count;
525 } else {
526 rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
527 if (rc < 0)
528 goto out;
529 if (rc == 0) {
530 rc = -EINVAL;
531 goto out;
533 cnt += rc;
536 if (rule_list == NULL)
537 rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
538 &rule.smk_subject->smk_rules_lock, 1);
539 else
540 rc = smk_set_access(&rule, rule_list, rule_lock, 0);
542 if (rc)
543 goto out;
546 rc = cnt;
547 out:
548 kfree(data);
549 return rc;
553 * Core logic for smackfs seq list operations.
556 static void *smk_seq_start(struct seq_file *s, loff_t *pos,
557 struct list_head *head)
559 struct list_head *list;
560 int i = *pos;
562 rcu_read_lock();
563 for (list = rcu_dereference(list_next_rcu(head));
564 list != head;
565 list = rcu_dereference(list_next_rcu(list))) {
566 if (i-- == 0)
567 return list;
570 return NULL;
573 static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
574 struct list_head *head)
576 struct list_head *list = v;
578 ++*pos;
579 list = rcu_dereference(list_next_rcu(list));
581 return (list == head) ? NULL : list;
584 static void smk_seq_stop(struct seq_file *s, void *v)
586 rcu_read_unlock();
589 static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
592 * Don't show any rules with label names too long for
593 * interface file (/smack/load or /smack/load2)
594 * because you should expect to be able to write
595 * anything you read back.
597 if (strlen(srp->smk_subject->smk_known) >= max ||
598 strlen(srp->smk_object->smk_known) >= max)
599 return;
601 if (srp->smk_access == 0)
602 return;
604 seq_printf(s, "%s %s",
605 srp->smk_subject->smk_known,
606 srp->smk_object->smk_known);
608 seq_putc(s, ' ');
610 if (srp->smk_access & MAY_READ)
611 seq_putc(s, 'r');
612 if (srp->smk_access & MAY_WRITE)
613 seq_putc(s, 'w');
614 if (srp->smk_access & MAY_EXEC)
615 seq_putc(s, 'x');
616 if (srp->smk_access & MAY_APPEND)
617 seq_putc(s, 'a');
618 if (srp->smk_access & MAY_TRANSMUTE)
619 seq_putc(s, 't');
620 if (srp->smk_access & MAY_LOCK)
621 seq_putc(s, 'l');
622 if (srp->smk_access & MAY_BRINGUP)
623 seq_putc(s, 'b');
625 seq_putc(s, '\n');
629 * Seq_file read operations for /smack/load
632 static void *load2_seq_start(struct seq_file *s, loff_t *pos)
634 return smk_seq_start(s, pos, &smack_rule_list);
637 static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
639 return smk_seq_next(s, v, pos, &smack_rule_list);
642 static int load_seq_show(struct seq_file *s, void *v)
644 struct list_head *list = v;
645 struct smack_master_list *smlp =
646 list_entry_rcu(list, struct smack_master_list, list);
648 smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
650 return 0;
653 static const struct seq_operations load_seq_ops = {
654 .start = load2_seq_start,
655 .next = load2_seq_next,
656 .show = load_seq_show,
657 .stop = smk_seq_stop,
661 * smk_open_load - open() for /smack/load
662 * @inode: inode structure representing file
663 * @file: "load" file pointer
665 * For reading, use load_seq_* seq_file reading operations.
667 static int smk_open_load(struct inode *inode, struct file *file)
669 return seq_open(file, &load_seq_ops);
673 * smk_write_load - write() for /smack/load
674 * @file: file pointer, not actually used
675 * @buf: where to get the data from
676 * @count: bytes sent
677 * @ppos: where to start - must be 0
680 static ssize_t smk_write_load(struct file *file, const char __user *buf,
681 size_t count, loff_t *ppos)
684 * Must have privilege.
685 * No partial writes.
686 * Enough data must be present.
688 if (!smack_privileged(CAP_MAC_ADMIN))
689 return -EPERM;
691 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
692 SMK_FIXED24_FMT);
695 static const struct file_operations smk_load_ops = {
696 .open = smk_open_load,
697 .read = seq_read,
698 .llseek = seq_lseek,
699 .write = smk_write_load,
700 .release = seq_release,
704 * smk_cipso_doi - initialize the CIPSO domain
706 static void smk_cipso_doi(void)
708 int rc;
709 struct cipso_v4_doi *doip;
710 struct netlbl_audit nai;
712 smk_netlabel_audit_set(&nai);
714 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
715 if (rc != 0)
716 printk(KERN_WARNING "%s:%d remove rc = %d\n",
717 __func__, __LINE__, rc);
719 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
720 if (doip == NULL)
721 panic("smack: Failed to initialize cipso DOI.\n");
722 doip->map.std = NULL;
723 doip->doi = smk_cipso_doi_value;
724 doip->type = CIPSO_V4_MAP_PASS;
725 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
726 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
727 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
729 rc = netlbl_cfg_cipsov4_add(doip, &nai);
730 if (rc != 0) {
731 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
732 __func__, __LINE__, rc);
733 kfree(doip);
734 return;
736 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
737 if (rc != 0) {
738 printk(KERN_WARNING "%s:%d map add rc = %d\n",
739 __func__, __LINE__, rc);
740 kfree(doip);
741 return;
746 * smk_unlbl_ambient - initialize the unlabeled domain
747 * @oldambient: previous domain string
749 static void smk_unlbl_ambient(char *oldambient)
751 int rc;
752 struct netlbl_audit nai;
754 smk_netlabel_audit_set(&nai);
756 if (oldambient != NULL) {
757 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
758 if (rc != 0)
759 printk(KERN_WARNING "%s:%d remove rc = %d\n",
760 __func__, __LINE__, rc);
762 if (smack_net_ambient == NULL)
763 smack_net_ambient = &smack_known_floor;
765 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
766 NULL, NULL, &nai);
767 if (rc != 0)
768 printk(KERN_WARNING "%s:%d add rc = %d\n",
769 __func__, __LINE__, rc);
773 * Seq_file read operations for /smack/cipso
776 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
778 return smk_seq_start(s, pos, &smack_known_list);
781 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
783 return smk_seq_next(s, v, pos, &smack_known_list);
787 * Print cipso labels in format:
788 * label level[/cat[,cat]]
790 static int cipso_seq_show(struct seq_file *s, void *v)
792 struct list_head *list = v;
793 struct smack_known *skp =
794 list_entry_rcu(list, struct smack_known, list);
795 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
796 char sep = '/';
797 int i;
800 * Don't show a label that could not have been set using
801 * /smack/cipso. This is in support of the notion that
802 * anything read from /smack/cipso ought to be writeable
803 * to /smack/cipso.
805 * /smack/cipso2 should be used instead.
807 if (strlen(skp->smk_known) >= SMK_LABELLEN)
808 return 0;
810 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
812 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
813 i = netlbl_catmap_walk(cmp, i + 1)) {
814 seq_printf(s, "%c%d", sep, i);
815 sep = ',';
818 seq_putc(s, '\n');
820 return 0;
823 static const struct seq_operations cipso_seq_ops = {
824 .start = cipso_seq_start,
825 .next = cipso_seq_next,
826 .show = cipso_seq_show,
827 .stop = smk_seq_stop,
831 * smk_open_cipso - open() for /smack/cipso
832 * @inode: inode structure representing file
833 * @file: "cipso" file pointer
835 * Connect our cipso_seq_* operations with /smack/cipso
836 * file_operations
838 static int smk_open_cipso(struct inode *inode, struct file *file)
840 return seq_open(file, &cipso_seq_ops);
844 * smk_set_cipso - do the work for write() for cipso and cipso2
845 * @file: file pointer, not actually used
846 * @buf: where to get the data from
847 * @count: bytes sent
848 * @ppos: where to start
849 * @format: /smack/cipso or /smack/cipso2
851 * Accepts only one cipso rule per write call.
852 * Returns number of bytes written or error code, as appropriate
854 static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
855 size_t count, loff_t *ppos, int format)
857 struct smack_known *skp;
858 struct netlbl_lsm_secattr ncats;
859 char mapcatset[SMK_CIPSOLEN];
860 int maplevel;
861 unsigned int cat;
862 int catlen;
863 ssize_t rc = -EINVAL;
864 char *data = NULL;
865 char *rule;
866 int ret;
867 int i;
870 * Must have privilege.
871 * No partial writes.
872 * Enough data must be present.
874 if (!smack_privileged(CAP_MAC_ADMIN))
875 return -EPERM;
876 if (*ppos != 0)
877 return -EINVAL;
878 if (format == SMK_FIXED24_FMT &&
879 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
880 return -EINVAL;
882 data = memdup_user_nul(buf, count);
883 if (IS_ERR(data))
884 return PTR_ERR(data);
886 rule = data;
888 * Only allow one writer at a time. Writes should be
889 * quite rare and small in any case.
891 mutex_lock(&smack_cipso_lock);
893 skp = smk_import_entry(rule, 0);
894 if (IS_ERR(skp)) {
895 rc = PTR_ERR(skp);
896 goto out;
899 if (format == SMK_FIXED24_FMT)
900 rule += SMK_LABELLEN;
901 else
902 rule += strlen(skp->smk_known) + 1;
904 if (rule > data + count) {
905 rc = -EOVERFLOW;
906 goto out;
909 ret = sscanf(rule, "%d", &maplevel);
910 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
911 goto out;
913 rule += SMK_DIGITLEN;
914 if (rule > data + count) {
915 rc = -EOVERFLOW;
916 goto out;
919 ret = sscanf(rule, "%d", &catlen);
920 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
921 goto out;
923 if (format == SMK_FIXED24_FMT &&
924 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
925 goto out;
927 memset(mapcatset, 0, sizeof(mapcatset));
929 for (i = 0; i < catlen; i++) {
930 rule += SMK_DIGITLEN;
931 ret = sscanf(rule, "%u", &cat);
932 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
933 goto out;
935 smack_catset_bit(cat, mapcatset);
938 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
939 if (rc >= 0) {
940 netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat);
941 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
942 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
943 rc = count;
946 out:
947 mutex_unlock(&smack_cipso_lock);
948 kfree(data);
949 return rc;
953 * smk_write_cipso - write() for /smack/cipso
954 * @file: file pointer, not actually used
955 * @buf: where to get the data from
956 * @count: bytes sent
957 * @ppos: where to start
959 * Accepts only one cipso rule per write call.
960 * Returns number of bytes written or error code, as appropriate
962 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
963 size_t count, loff_t *ppos)
965 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
968 static const struct file_operations smk_cipso_ops = {
969 .open = smk_open_cipso,
970 .read = seq_read,
971 .llseek = seq_lseek,
972 .write = smk_write_cipso,
973 .release = seq_release,
977 * Seq_file read operations for /smack/cipso2
981 * Print cipso labels in format:
982 * label level[/cat[,cat]]
984 static int cipso2_seq_show(struct seq_file *s, void *v)
986 struct list_head *list = v;
987 struct smack_known *skp =
988 list_entry_rcu(list, struct smack_known, list);
989 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
990 char sep = '/';
991 int i;
993 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
995 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
996 i = netlbl_catmap_walk(cmp, i + 1)) {
997 seq_printf(s, "%c%d", sep, i);
998 sep = ',';
1001 seq_putc(s, '\n');
1003 return 0;
1006 static const struct seq_operations cipso2_seq_ops = {
1007 .start = cipso_seq_start,
1008 .next = cipso_seq_next,
1009 .show = cipso2_seq_show,
1010 .stop = smk_seq_stop,
1014 * smk_open_cipso2 - open() for /smack/cipso2
1015 * @inode: inode structure representing file
1016 * @file: "cipso2" file pointer
1018 * Connect our cipso_seq_* operations with /smack/cipso2
1019 * file_operations
1021 static int smk_open_cipso2(struct inode *inode, struct file *file)
1023 return seq_open(file, &cipso2_seq_ops);
1027 * smk_write_cipso2 - write() for /smack/cipso2
1028 * @file: file pointer, not actually used
1029 * @buf: where to get the data from
1030 * @count: bytes sent
1031 * @ppos: where to start
1033 * Accepts only one cipso rule per write call.
1034 * Returns number of bytes written or error code, as appropriate
1036 static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1037 size_t count, loff_t *ppos)
1039 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1042 static const struct file_operations smk_cipso2_ops = {
1043 .open = smk_open_cipso2,
1044 .read = seq_read,
1045 .llseek = seq_lseek,
1046 .write = smk_write_cipso2,
1047 .release = seq_release,
1051 * Seq_file read operations for /smack/netlabel
1054 static void *net4addr_seq_start(struct seq_file *s, loff_t *pos)
1056 return smk_seq_start(s, pos, &smk_net4addr_list);
1059 static void *net4addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1061 return smk_seq_next(s, v, pos, &smk_net4addr_list);
1065 * Print host/label pairs
1067 static int net4addr_seq_show(struct seq_file *s, void *v)
1069 struct list_head *list = v;
1070 struct smk_net4addr *skp =
1071 list_entry_rcu(list, struct smk_net4addr, list);
1072 char *kp = SMACK_CIPSO_OPTION;
1074 if (skp->smk_label != NULL)
1075 kp = skp->smk_label->smk_known;
1076 seq_printf(s, "%pI4/%d %s\n", &skp->smk_host.s_addr,
1077 skp->smk_masks, kp);
1079 return 0;
1082 static const struct seq_operations net4addr_seq_ops = {
1083 .start = net4addr_seq_start,
1084 .next = net4addr_seq_next,
1085 .show = net4addr_seq_show,
1086 .stop = smk_seq_stop,
1090 * smk_open_net4addr - open() for /smack/netlabel
1091 * @inode: inode structure representing file
1092 * @file: "netlabel" file pointer
1094 * Connect our net4addr_seq_* operations with /smack/netlabel
1095 * file_operations
1097 static int smk_open_net4addr(struct inode *inode, struct file *file)
1099 return seq_open(file, &net4addr_seq_ops);
1103 * smk_net4addr_insert
1104 * @new : netlabel to insert
1106 * This helper insert netlabel in the smack_net4addrs list
1107 * sorted by netmask length (longest to smallest)
1108 * locked by &smk_net4addr_lock in smk_write_net4addr
1111 static void smk_net4addr_insert(struct smk_net4addr *new)
1113 struct smk_net4addr *m;
1114 struct smk_net4addr *m_next;
1116 if (list_empty(&smk_net4addr_list)) {
1117 list_add_rcu(&new->list, &smk_net4addr_list);
1118 return;
1121 m = list_entry_rcu(smk_net4addr_list.next,
1122 struct smk_net4addr, list);
1124 /* the comparison '>' is a bit hacky, but works */
1125 if (new->smk_masks > m->smk_masks) {
1126 list_add_rcu(&new->list, &smk_net4addr_list);
1127 return;
1130 list_for_each_entry_rcu(m, &smk_net4addr_list, list) {
1131 if (list_is_last(&m->list, &smk_net4addr_list)) {
1132 list_add_rcu(&new->list, &m->list);
1133 return;
1135 m_next = list_entry_rcu(m->list.next,
1136 struct smk_net4addr, list);
1137 if (new->smk_masks > m_next->smk_masks) {
1138 list_add_rcu(&new->list, &m->list);
1139 return;
1146 * smk_write_net4addr - write() for /smack/netlabel
1147 * @file: file pointer, not actually used
1148 * @buf: where to get the data from
1149 * @count: bytes sent
1150 * @ppos: where to start
1152 * Accepts only one net4addr per write call.
1153 * Returns number of bytes written or error code, as appropriate
1155 static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
1156 size_t count, loff_t *ppos)
1158 struct smk_net4addr *snp;
1159 struct sockaddr_in newname;
1160 char *smack;
1161 struct smack_known *skp = NULL;
1162 char *data;
1163 char *host = (char *)&newname.sin_addr.s_addr;
1164 int rc;
1165 struct netlbl_audit audit_info;
1166 struct in_addr mask;
1167 unsigned int m;
1168 unsigned int masks;
1169 int found;
1170 u32 mask_bits = (1<<31);
1171 __be32 nsa;
1172 u32 temp_mask;
1175 * Must have privilege.
1176 * No partial writes.
1177 * Enough data must be present.
1178 * "<addr/mask, as a.b.c.d/e><space><label>"
1179 * "<addr, as a.b.c.d><space><label>"
1181 if (!smack_privileged(CAP_MAC_ADMIN))
1182 return -EPERM;
1183 if (*ppos != 0)
1184 return -EINVAL;
1185 if (count < SMK_NETLBLADDRMIN)
1186 return -EINVAL;
1188 data = memdup_user_nul(buf, count);
1189 if (IS_ERR(data))
1190 return PTR_ERR(data);
1192 smack = kzalloc(count + 1, GFP_KERNEL);
1193 if (smack == NULL) {
1194 rc = -ENOMEM;
1195 goto free_data_out;
1198 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
1199 &host[0], &host[1], &host[2], &host[3], &masks, smack);
1200 if (rc != 6) {
1201 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1202 &host[0], &host[1], &host[2], &host[3], smack);
1203 if (rc != 5) {
1204 rc = -EINVAL;
1205 goto free_out;
1207 m = BEBITS;
1208 masks = 32;
1210 if (masks > BEBITS) {
1211 rc = -EINVAL;
1212 goto free_out;
1216 * If smack begins with '-', it is an option, don't import it
1218 if (smack[0] != '-') {
1219 skp = smk_import_entry(smack, 0);
1220 if (IS_ERR(skp)) {
1221 rc = PTR_ERR(skp);
1222 goto free_out;
1224 } else {
1226 * Only the -CIPSO option is supported for IPv4
1228 if (strcmp(smack, SMACK_CIPSO_OPTION) != 0) {
1229 rc = -EINVAL;
1230 goto free_out;
1234 for (m = masks, temp_mask = 0; m > 0; m--) {
1235 temp_mask |= mask_bits;
1236 mask_bits >>= 1;
1238 mask.s_addr = cpu_to_be32(temp_mask);
1240 newname.sin_addr.s_addr &= mask.s_addr;
1242 * Only allow one writer at a time. Writes should be
1243 * quite rare and small in any case.
1245 mutex_lock(&smk_net4addr_lock);
1247 nsa = newname.sin_addr.s_addr;
1248 /* try to find if the prefix is already in the list */
1249 found = 0;
1250 list_for_each_entry_rcu(snp, &smk_net4addr_list, list) {
1251 if (snp->smk_host.s_addr == nsa && snp->smk_masks == masks) {
1252 found = 1;
1253 break;
1256 smk_netlabel_audit_set(&audit_info);
1258 if (found == 0) {
1259 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1260 if (snp == NULL)
1261 rc = -ENOMEM;
1262 else {
1263 rc = 0;
1264 snp->smk_host.s_addr = newname.sin_addr.s_addr;
1265 snp->smk_mask.s_addr = mask.s_addr;
1266 snp->smk_label = skp;
1267 snp->smk_masks = masks;
1268 smk_net4addr_insert(snp);
1270 } else {
1272 * Delete the unlabeled entry, only if the previous label
1273 * wasn't the special CIPSO option
1275 if (snp->smk_label != NULL)
1276 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1277 &snp->smk_host, &snp->smk_mask,
1278 PF_INET, &audit_info);
1279 else
1280 rc = 0;
1281 snp->smk_label = skp;
1285 * Now tell netlabel about the single label nature of
1286 * this host so that incoming packets get labeled.
1287 * but only if we didn't get the special CIPSO option
1289 if (rc == 0 && skp != NULL)
1290 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1291 &snp->smk_host, &snp->smk_mask, PF_INET,
1292 snp->smk_label->smk_secid, &audit_info);
1294 if (rc == 0)
1295 rc = count;
1297 mutex_unlock(&smk_net4addr_lock);
1299 free_out:
1300 kfree(smack);
1301 free_data_out:
1302 kfree(data);
1304 return rc;
1307 static const struct file_operations smk_net4addr_ops = {
1308 .open = smk_open_net4addr,
1309 .read = seq_read,
1310 .llseek = seq_lseek,
1311 .write = smk_write_net4addr,
1312 .release = seq_release,
1315 #if IS_ENABLED(CONFIG_IPV6)
1317 * Seq_file read operations for /smack/netlabel6
1320 static void *net6addr_seq_start(struct seq_file *s, loff_t *pos)
1322 return smk_seq_start(s, pos, &smk_net6addr_list);
1325 static void *net6addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1327 return smk_seq_next(s, v, pos, &smk_net6addr_list);
1331 * Print host/label pairs
1333 static int net6addr_seq_show(struct seq_file *s, void *v)
1335 struct list_head *list = v;
1336 struct smk_net6addr *skp =
1337 list_entry(list, struct smk_net6addr, list);
1339 if (skp->smk_label != NULL)
1340 seq_printf(s, "%pI6/%d %s\n", &skp->smk_host, skp->smk_masks,
1341 skp->smk_label->smk_known);
1343 return 0;
1346 static const struct seq_operations net6addr_seq_ops = {
1347 .start = net6addr_seq_start,
1348 .next = net6addr_seq_next,
1349 .show = net6addr_seq_show,
1350 .stop = smk_seq_stop,
1354 * smk_open_net6addr - open() for /smack/netlabel
1355 * @inode: inode structure representing file
1356 * @file: "netlabel" file pointer
1358 * Connect our net6addr_seq_* operations with /smack/netlabel
1359 * file_operations
1361 static int smk_open_net6addr(struct inode *inode, struct file *file)
1363 return seq_open(file, &net6addr_seq_ops);
1367 * smk_net6addr_insert
1368 * @new : entry to insert
1370 * This inserts an entry in the smack_net6addrs list
1371 * sorted by netmask length (longest to smallest)
1372 * locked by &smk_net6addr_lock in smk_write_net6addr
1375 static void smk_net6addr_insert(struct smk_net6addr *new)
1377 struct smk_net6addr *m_next;
1378 struct smk_net6addr *m;
1380 if (list_empty(&smk_net6addr_list)) {
1381 list_add_rcu(&new->list, &smk_net6addr_list);
1382 return;
1385 m = list_entry_rcu(smk_net6addr_list.next,
1386 struct smk_net6addr, list);
1388 if (new->smk_masks > m->smk_masks) {
1389 list_add_rcu(&new->list, &smk_net6addr_list);
1390 return;
1393 list_for_each_entry_rcu(m, &smk_net6addr_list, list) {
1394 if (list_is_last(&m->list, &smk_net6addr_list)) {
1395 list_add_rcu(&new->list, &m->list);
1396 return;
1398 m_next = list_entry_rcu(m->list.next,
1399 struct smk_net6addr, list);
1400 if (new->smk_masks > m_next->smk_masks) {
1401 list_add_rcu(&new->list, &m->list);
1402 return;
1409 * smk_write_net6addr - write() for /smack/netlabel
1410 * @file: file pointer, not actually used
1411 * @buf: where to get the data from
1412 * @count: bytes sent
1413 * @ppos: where to start
1415 * Accepts only one net6addr per write call.
1416 * Returns number of bytes written or error code, as appropriate
1418 static ssize_t smk_write_net6addr(struct file *file, const char __user *buf,
1419 size_t count, loff_t *ppos)
1421 struct smk_net6addr *snp;
1422 struct in6_addr newname;
1423 struct in6_addr fullmask;
1424 struct smack_known *skp = NULL;
1425 char *smack;
1426 char *data;
1427 int rc = 0;
1428 int found = 0;
1429 int i;
1430 unsigned int scanned[8];
1431 unsigned int m;
1432 unsigned int mask = 128;
1435 * Must have privilege.
1436 * No partial writes.
1437 * Enough data must be present.
1438 * "<addr/mask, as a:b:c:d:e:f:g:h/e><space><label>"
1439 * "<addr, as a:b:c:d:e:f:g:h><space><label>"
1441 if (!smack_privileged(CAP_MAC_ADMIN))
1442 return -EPERM;
1443 if (*ppos != 0)
1444 return -EINVAL;
1445 if (count < SMK_NETLBLADDRMIN)
1446 return -EINVAL;
1448 data = memdup_user_nul(buf, count);
1449 if (IS_ERR(data))
1450 return PTR_ERR(data);
1452 smack = kzalloc(count + 1, GFP_KERNEL);
1453 if (smack == NULL) {
1454 rc = -ENOMEM;
1455 goto free_data_out;
1458 i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x/%u %s",
1459 &scanned[0], &scanned[1], &scanned[2], &scanned[3],
1460 &scanned[4], &scanned[5], &scanned[6], &scanned[7],
1461 &mask, smack);
1462 if (i != 10) {
1463 i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x %s",
1464 &scanned[0], &scanned[1], &scanned[2],
1465 &scanned[3], &scanned[4], &scanned[5],
1466 &scanned[6], &scanned[7], smack);
1467 if (i != 9) {
1468 rc = -EINVAL;
1469 goto free_out;
1472 if (mask > 128) {
1473 rc = -EINVAL;
1474 goto free_out;
1476 for (i = 0; i < 8; i++) {
1477 if (scanned[i] > 0xffff) {
1478 rc = -EINVAL;
1479 goto free_out;
1481 newname.s6_addr16[i] = htons(scanned[i]);
1485 * If smack begins with '-', it is an option, don't import it
1487 if (smack[0] != '-') {
1488 skp = smk_import_entry(smack, 0);
1489 if (IS_ERR(skp)) {
1490 rc = PTR_ERR(skp);
1491 goto free_out;
1493 } else {
1495 * Only -DELETE is supported for IPv6
1497 if (strcmp(smack, SMACK_DELETE_OPTION) != 0) {
1498 rc = -EINVAL;
1499 goto free_out;
1503 for (i = 0, m = mask; i < 8; i++) {
1504 if (m >= 16) {
1505 fullmask.s6_addr16[i] = 0xffff;
1506 m -= 16;
1507 } else if (m > 0) {
1508 fullmask.s6_addr16[i] = (1 << m) - 1;
1509 m = 0;
1510 } else
1511 fullmask.s6_addr16[i] = 0;
1512 newname.s6_addr16[i] &= fullmask.s6_addr16[i];
1516 * Only allow one writer at a time. Writes should be
1517 * quite rare and small in any case.
1519 mutex_lock(&smk_net6addr_lock);
1521 * Try to find the prefix in the list
1523 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
1524 if (mask != snp->smk_masks)
1525 continue;
1526 for (found = 1, i = 0; i < 8; i++) {
1527 if (newname.s6_addr16[i] !=
1528 snp->smk_host.s6_addr16[i]) {
1529 found = 0;
1530 break;
1533 if (found == 1)
1534 break;
1536 if (found == 0) {
1537 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1538 if (snp == NULL)
1539 rc = -ENOMEM;
1540 else {
1541 snp->smk_host = newname;
1542 snp->smk_mask = fullmask;
1543 snp->smk_masks = mask;
1544 snp->smk_label = skp;
1545 smk_net6addr_insert(snp);
1547 } else {
1548 snp->smk_label = skp;
1551 if (rc == 0)
1552 rc = count;
1554 mutex_unlock(&smk_net6addr_lock);
1556 free_out:
1557 kfree(smack);
1558 free_data_out:
1559 kfree(data);
1561 return rc;
1564 static const struct file_operations smk_net6addr_ops = {
1565 .open = smk_open_net6addr,
1566 .read = seq_read,
1567 .llseek = seq_lseek,
1568 .write = smk_write_net6addr,
1569 .release = seq_release,
1571 #endif /* CONFIG_IPV6 */
1574 * smk_read_doi - read() for /smack/doi
1575 * @filp: file pointer, not actually used
1576 * @buf: where to put the result
1577 * @count: maximum to send along
1578 * @ppos: where to start
1580 * Returns number of bytes read or error code, as appropriate
1582 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1583 size_t count, loff_t *ppos)
1585 char temp[80];
1586 ssize_t rc;
1588 if (*ppos != 0)
1589 return 0;
1591 sprintf(temp, "%d", smk_cipso_doi_value);
1592 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1594 return rc;
1598 * smk_write_doi - write() for /smack/doi
1599 * @file: file pointer, not actually used
1600 * @buf: where to get the data from
1601 * @count: bytes sent
1602 * @ppos: where to start
1604 * Returns number of bytes written or error code, as appropriate
1606 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1607 size_t count, loff_t *ppos)
1609 char temp[80];
1610 int i;
1612 if (!smack_privileged(CAP_MAC_ADMIN))
1613 return -EPERM;
1615 if (count >= sizeof(temp) || count == 0)
1616 return -EINVAL;
1618 if (copy_from_user(temp, buf, count) != 0)
1619 return -EFAULT;
1621 temp[count] = '\0';
1623 if (sscanf(temp, "%d", &i) != 1)
1624 return -EINVAL;
1626 smk_cipso_doi_value = i;
1628 smk_cipso_doi();
1630 return count;
1633 static const struct file_operations smk_doi_ops = {
1634 .read = smk_read_doi,
1635 .write = smk_write_doi,
1636 .llseek = default_llseek,
1640 * smk_read_direct - read() for /smack/direct
1641 * @filp: file pointer, not actually used
1642 * @buf: where to put the result
1643 * @count: maximum to send along
1644 * @ppos: where to start
1646 * Returns number of bytes read or error code, as appropriate
1648 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1649 size_t count, loff_t *ppos)
1651 char temp[80];
1652 ssize_t rc;
1654 if (*ppos != 0)
1655 return 0;
1657 sprintf(temp, "%d", smack_cipso_direct);
1658 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1660 return rc;
1664 * smk_write_direct - write() for /smack/direct
1665 * @file: file pointer, not actually used
1666 * @buf: where to get the data from
1667 * @count: bytes sent
1668 * @ppos: where to start
1670 * Returns number of bytes written or error code, as appropriate
1672 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1673 size_t count, loff_t *ppos)
1675 struct smack_known *skp;
1676 char temp[80];
1677 int i;
1679 if (!smack_privileged(CAP_MAC_ADMIN))
1680 return -EPERM;
1682 if (count >= sizeof(temp) || count == 0)
1683 return -EINVAL;
1685 if (copy_from_user(temp, buf, count) != 0)
1686 return -EFAULT;
1688 temp[count] = '\0';
1690 if (sscanf(temp, "%d", &i) != 1)
1691 return -EINVAL;
1694 * Don't do anything if the value hasn't actually changed.
1695 * If it is changing reset the level on entries that were
1696 * set up to be direct when they were created.
1698 if (smack_cipso_direct != i) {
1699 mutex_lock(&smack_known_lock);
1700 list_for_each_entry_rcu(skp, &smack_known_list, list)
1701 if (skp->smk_netlabel.attr.mls.lvl ==
1702 smack_cipso_direct)
1703 skp->smk_netlabel.attr.mls.lvl = i;
1704 smack_cipso_direct = i;
1705 mutex_unlock(&smack_known_lock);
1708 return count;
1711 static const struct file_operations smk_direct_ops = {
1712 .read = smk_read_direct,
1713 .write = smk_write_direct,
1714 .llseek = default_llseek,
1718 * smk_read_mapped - read() for /smack/mapped
1719 * @filp: file pointer, not actually used
1720 * @buf: where to put the result
1721 * @count: maximum to send along
1722 * @ppos: where to start
1724 * Returns number of bytes read or error code, as appropriate
1726 static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1727 size_t count, loff_t *ppos)
1729 char temp[80];
1730 ssize_t rc;
1732 if (*ppos != 0)
1733 return 0;
1735 sprintf(temp, "%d", smack_cipso_mapped);
1736 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1738 return rc;
1742 * smk_write_mapped - write() for /smack/mapped
1743 * @file: file pointer, not actually used
1744 * @buf: where to get the data from
1745 * @count: bytes sent
1746 * @ppos: where to start
1748 * Returns number of bytes written or error code, as appropriate
1750 static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1751 size_t count, loff_t *ppos)
1753 struct smack_known *skp;
1754 char temp[80];
1755 int i;
1757 if (!smack_privileged(CAP_MAC_ADMIN))
1758 return -EPERM;
1760 if (count >= sizeof(temp) || count == 0)
1761 return -EINVAL;
1763 if (copy_from_user(temp, buf, count) != 0)
1764 return -EFAULT;
1766 temp[count] = '\0';
1768 if (sscanf(temp, "%d", &i) != 1)
1769 return -EINVAL;
1772 * Don't do anything if the value hasn't actually changed.
1773 * If it is changing reset the level on entries that were
1774 * set up to be mapped when they were created.
1776 if (smack_cipso_mapped != i) {
1777 mutex_lock(&smack_known_lock);
1778 list_for_each_entry_rcu(skp, &smack_known_list, list)
1779 if (skp->smk_netlabel.attr.mls.lvl ==
1780 smack_cipso_mapped)
1781 skp->smk_netlabel.attr.mls.lvl = i;
1782 smack_cipso_mapped = i;
1783 mutex_unlock(&smack_known_lock);
1786 return count;
1789 static const struct file_operations smk_mapped_ops = {
1790 .read = smk_read_mapped,
1791 .write = smk_write_mapped,
1792 .llseek = default_llseek,
1796 * smk_read_ambient - read() for /smack/ambient
1797 * @filp: file pointer, not actually used
1798 * @buf: where to put the result
1799 * @cn: maximum to send along
1800 * @ppos: where to start
1802 * Returns number of bytes read or error code, as appropriate
1804 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1805 size_t cn, loff_t *ppos)
1807 ssize_t rc;
1808 int asize;
1810 if (*ppos != 0)
1811 return 0;
1813 * Being careful to avoid a problem in the case where
1814 * smack_net_ambient gets changed in midstream.
1816 mutex_lock(&smack_ambient_lock);
1818 asize = strlen(smack_net_ambient->smk_known) + 1;
1820 if (cn >= asize)
1821 rc = simple_read_from_buffer(buf, cn, ppos,
1822 smack_net_ambient->smk_known,
1823 asize);
1824 else
1825 rc = -EINVAL;
1827 mutex_unlock(&smack_ambient_lock);
1829 return rc;
1833 * smk_write_ambient - write() for /smack/ambient
1834 * @file: file pointer, not actually used
1835 * @buf: where to get the data from
1836 * @count: bytes sent
1837 * @ppos: where to start
1839 * Returns number of bytes written or error code, as appropriate
1841 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1842 size_t count, loff_t *ppos)
1844 struct smack_known *skp;
1845 char *oldambient;
1846 char *data;
1847 int rc = count;
1849 if (!smack_privileged(CAP_MAC_ADMIN))
1850 return -EPERM;
1852 data = memdup_user_nul(buf, count);
1853 if (IS_ERR(data))
1854 return PTR_ERR(data);
1856 skp = smk_import_entry(data, count);
1857 if (IS_ERR(skp)) {
1858 rc = PTR_ERR(skp);
1859 goto out;
1862 mutex_lock(&smack_ambient_lock);
1864 oldambient = smack_net_ambient->smk_known;
1865 smack_net_ambient = skp;
1866 smk_unlbl_ambient(oldambient);
1868 mutex_unlock(&smack_ambient_lock);
1870 out:
1871 kfree(data);
1872 return rc;
1875 static const struct file_operations smk_ambient_ops = {
1876 .read = smk_read_ambient,
1877 .write = smk_write_ambient,
1878 .llseek = default_llseek,
1882 * Seq_file operations for /smack/onlycap
1884 static void *onlycap_seq_start(struct seq_file *s, loff_t *pos)
1886 return smk_seq_start(s, pos, &smack_onlycap_list);
1889 static void *onlycap_seq_next(struct seq_file *s, void *v, loff_t *pos)
1891 return smk_seq_next(s, v, pos, &smack_onlycap_list);
1894 static int onlycap_seq_show(struct seq_file *s, void *v)
1896 struct list_head *list = v;
1897 struct smack_known_list_elem *sklep =
1898 list_entry_rcu(list, struct smack_known_list_elem, list);
1900 seq_puts(s, sklep->smk_label->smk_known);
1901 seq_putc(s, ' ');
1903 return 0;
1906 static const struct seq_operations onlycap_seq_ops = {
1907 .start = onlycap_seq_start,
1908 .next = onlycap_seq_next,
1909 .show = onlycap_seq_show,
1910 .stop = smk_seq_stop,
1913 static int smk_open_onlycap(struct inode *inode, struct file *file)
1915 return seq_open(file, &onlycap_seq_ops);
1919 * smk_list_swap_rcu - swap public list with a private one in RCU-safe way
1920 * The caller must hold appropriate mutex to prevent concurrent modifications
1921 * to the public list.
1922 * Private list is assumed to be not accessible to other threads yet.
1924 * @public: public list
1925 * @private: private list
1927 static void smk_list_swap_rcu(struct list_head *public,
1928 struct list_head *private)
1930 struct list_head *first, *last;
1932 if (list_empty(public)) {
1933 list_splice_init_rcu(private, public, synchronize_rcu);
1934 } else {
1935 /* Remember public list before replacing it */
1936 first = public->next;
1937 last = public->prev;
1939 /* Publish private list in place of public in RCU-safe way */
1940 private->prev->next = public;
1941 private->next->prev = public;
1942 rcu_assign_pointer(public->next, private->next);
1943 public->prev = private->prev;
1945 synchronize_rcu();
1947 /* When all readers are done with the old public list,
1948 * attach it in place of private */
1949 private->next = first;
1950 private->prev = last;
1951 first->prev = private;
1952 last->next = private;
1957 * smk_parse_label_list - parse list of Smack labels, separated by spaces
1959 * @data: the string to parse
1960 * @private: destination list
1962 * Returns zero on success or error code, as appropriate
1964 static int smk_parse_label_list(char *data, struct list_head *list)
1966 char *tok;
1967 struct smack_known *skp;
1968 struct smack_known_list_elem *sklep;
1970 while ((tok = strsep(&data, " ")) != NULL) {
1971 if (!*tok)
1972 continue;
1974 skp = smk_import_entry(tok, 0);
1975 if (IS_ERR(skp))
1976 return PTR_ERR(skp);
1978 sklep = kzalloc(sizeof(*sklep), GFP_KERNEL);
1979 if (sklep == NULL)
1980 return -ENOMEM;
1982 sklep->smk_label = skp;
1983 list_add(&sklep->list, list);
1986 return 0;
1990 * smk_destroy_label_list - destroy a list of smack_known_list_elem
1991 * @head: header pointer of the list to destroy
1993 void smk_destroy_label_list(struct list_head *list)
1995 struct smack_known_list_elem *sklep;
1996 struct smack_known_list_elem *sklep2;
1998 list_for_each_entry_safe(sklep, sklep2, list, list)
1999 kfree(sklep);
2001 INIT_LIST_HEAD(list);
2005 * smk_write_onlycap - write() for smackfs/onlycap
2006 * @file: file pointer, not actually used
2007 * @buf: where to get the data from
2008 * @count: bytes sent
2009 * @ppos: where to start
2011 * Returns number of bytes written or error code, as appropriate
2013 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
2014 size_t count, loff_t *ppos)
2016 char *data;
2017 LIST_HEAD(list_tmp);
2018 int rc;
2020 if (!smack_privileged(CAP_MAC_ADMIN))
2021 return -EPERM;
2023 data = memdup_user_nul(buf, count);
2024 if (IS_ERR(data))
2025 return PTR_ERR(data);
2027 rc = smk_parse_label_list(data, &list_tmp);
2028 kfree(data);
2031 * Clear the smack_onlycap on invalid label errors. This means
2032 * that we can pass a null string to unset the onlycap value.
2034 * Importing will also reject a label beginning with '-',
2035 * so "-usecapabilities" will also work.
2037 * But do so only on invalid label, not on system errors.
2038 * The invalid label must be first to count as clearing attempt.
2040 if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
2041 mutex_lock(&smack_onlycap_lock);
2042 smk_list_swap_rcu(&smack_onlycap_list, &list_tmp);
2043 mutex_unlock(&smack_onlycap_lock);
2044 rc = count;
2047 smk_destroy_label_list(&list_tmp);
2049 return rc;
2052 static const struct file_operations smk_onlycap_ops = {
2053 .open = smk_open_onlycap,
2054 .read = seq_read,
2055 .write = smk_write_onlycap,
2056 .llseek = seq_lseek,
2057 .release = seq_release,
2060 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
2062 * smk_read_unconfined - read() for smackfs/unconfined
2063 * @filp: file pointer, not actually used
2064 * @buf: where to put the result
2065 * @cn: maximum to send along
2066 * @ppos: where to start
2068 * Returns number of bytes read or error code, as appropriate
2070 static ssize_t smk_read_unconfined(struct file *filp, char __user *buf,
2071 size_t cn, loff_t *ppos)
2073 char *smack = "";
2074 ssize_t rc = -EINVAL;
2075 int asize;
2077 if (*ppos != 0)
2078 return 0;
2080 if (smack_unconfined != NULL)
2081 smack = smack_unconfined->smk_known;
2083 asize = strlen(smack) + 1;
2085 if (cn >= asize)
2086 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
2088 return rc;
2092 * smk_write_unconfined - write() for smackfs/unconfined
2093 * @file: file pointer, not actually used
2094 * @buf: where to get the data from
2095 * @count: bytes sent
2096 * @ppos: where to start
2098 * Returns number of bytes written or error code, as appropriate
2100 static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
2101 size_t count, loff_t *ppos)
2103 char *data;
2104 struct smack_known *skp;
2105 int rc = count;
2107 if (!smack_privileged(CAP_MAC_ADMIN))
2108 return -EPERM;
2110 data = memdup_user_nul(buf, count);
2111 if (IS_ERR(data))
2112 return PTR_ERR(data);
2115 * Clear the smack_unconfined on invalid label errors. This means
2116 * that we can pass a null string to unset the unconfined value.
2118 * Importing will also reject a label beginning with '-',
2119 * so "-confine" will also work.
2121 * But do so only on invalid label, not on system errors.
2123 skp = smk_import_entry(data, count);
2124 if (PTR_ERR(skp) == -EINVAL)
2125 skp = NULL;
2126 else if (IS_ERR(skp)) {
2127 rc = PTR_ERR(skp);
2128 goto freeout;
2131 smack_unconfined = skp;
2133 freeout:
2134 kfree(data);
2135 return rc;
2138 static const struct file_operations smk_unconfined_ops = {
2139 .read = smk_read_unconfined,
2140 .write = smk_write_unconfined,
2141 .llseek = default_llseek,
2143 #endif /* CONFIG_SECURITY_SMACK_BRINGUP */
2146 * smk_read_logging - read() for /smack/logging
2147 * @filp: file pointer, not actually used
2148 * @buf: where to put the result
2149 * @cn: maximum to send along
2150 * @ppos: where to start
2152 * Returns number of bytes read or error code, as appropriate
2154 static ssize_t smk_read_logging(struct file *filp, char __user *buf,
2155 size_t count, loff_t *ppos)
2157 char temp[32];
2158 ssize_t rc;
2160 if (*ppos != 0)
2161 return 0;
2163 sprintf(temp, "%d\n", log_policy);
2164 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2165 return rc;
2169 * smk_write_logging - write() for /smack/logging
2170 * @file: file pointer, not actually used
2171 * @buf: where to get the data from
2172 * @count: bytes sent
2173 * @ppos: where to start
2175 * Returns number of bytes written or error code, as appropriate
2177 static ssize_t smk_write_logging(struct file *file, const char __user *buf,
2178 size_t count, loff_t *ppos)
2180 char temp[32];
2181 int i;
2183 if (!smack_privileged(CAP_MAC_ADMIN))
2184 return -EPERM;
2186 if (count >= sizeof(temp) || count == 0)
2187 return -EINVAL;
2189 if (copy_from_user(temp, buf, count) != 0)
2190 return -EFAULT;
2192 temp[count] = '\0';
2194 if (sscanf(temp, "%d", &i) != 1)
2195 return -EINVAL;
2196 if (i < 0 || i > 3)
2197 return -EINVAL;
2198 log_policy = i;
2199 return count;
2204 static const struct file_operations smk_logging_ops = {
2205 .read = smk_read_logging,
2206 .write = smk_write_logging,
2207 .llseek = default_llseek,
2211 * Seq_file read operations for /smack/load-self
2214 static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
2216 struct task_smack *tsp = current_security();
2218 return smk_seq_start(s, pos, &tsp->smk_rules);
2221 static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
2223 struct task_smack *tsp = current_security();
2225 return smk_seq_next(s, v, pos, &tsp->smk_rules);
2228 static int load_self_seq_show(struct seq_file *s, void *v)
2230 struct list_head *list = v;
2231 struct smack_rule *srp =
2232 list_entry_rcu(list, struct smack_rule, list);
2234 smk_rule_show(s, srp, SMK_LABELLEN);
2236 return 0;
2239 static const struct seq_operations load_self_seq_ops = {
2240 .start = load_self_seq_start,
2241 .next = load_self_seq_next,
2242 .show = load_self_seq_show,
2243 .stop = smk_seq_stop,
2248 * smk_open_load_self - open() for /smack/load-self2
2249 * @inode: inode structure representing file
2250 * @file: "load" file pointer
2252 * For reading, use load_seq_* seq_file reading operations.
2254 static int smk_open_load_self(struct inode *inode, struct file *file)
2256 return seq_open(file, &load_self_seq_ops);
2260 * smk_write_load_self - write() for /smack/load-self
2261 * @file: file pointer, not actually used
2262 * @buf: where to get the data from
2263 * @count: bytes sent
2264 * @ppos: where to start - must be 0
2267 static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
2268 size_t count, loff_t *ppos)
2270 struct task_smack *tsp = current_security();
2272 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2273 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
2276 static const struct file_operations smk_load_self_ops = {
2277 .open = smk_open_load_self,
2278 .read = seq_read,
2279 .llseek = seq_lseek,
2280 .write = smk_write_load_self,
2281 .release = seq_release,
2285 * smk_user_access - handle access check transaction
2286 * @file: file pointer
2287 * @buf: data from user space
2288 * @count: bytes sent
2289 * @ppos: where to start - must be 0
2291 static ssize_t smk_user_access(struct file *file, const char __user *buf,
2292 size_t count, loff_t *ppos, int format)
2294 struct smack_parsed_rule rule;
2295 char *data;
2296 int res;
2298 data = simple_transaction_get(file, buf, count);
2299 if (IS_ERR(data))
2300 return PTR_ERR(data);
2302 if (format == SMK_FIXED24_FMT) {
2303 if (count < SMK_LOADLEN)
2304 return -EINVAL;
2305 res = smk_parse_rule(data, &rule, 0);
2306 } else {
2308 * simple_transaction_get() returns null-terminated data
2310 res = smk_parse_long_rule(data, &rule, 0, 3);
2313 if (res >= 0)
2314 res = smk_access(rule.smk_subject, rule.smk_object,
2315 rule.smk_access1, NULL);
2316 else if (res != -ENOENT)
2317 return res;
2320 * smk_access() can return a value > 0 in the "bringup" case.
2322 data[0] = res >= 0 ? '1' : '0';
2323 data[1] = '\0';
2325 simple_transaction_set(file, 2);
2327 if (format == SMK_FIXED24_FMT)
2328 return SMK_LOADLEN;
2329 return count;
2333 * smk_write_access - handle access check transaction
2334 * @file: file pointer
2335 * @buf: data from user space
2336 * @count: bytes sent
2337 * @ppos: where to start - must be 0
2339 static ssize_t smk_write_access(struct file *file, const char __user *buf,
2340 size_t count, loff_t *ppos)
2342 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
2345 static const struct file_operations smk_access_ops = {
2346 .write = smk_write_access,
2347 .read = simple_transaction_read,
2348 .release = simple_transaction_release,
2349 .llseek = generic_file_llseek,
2354 * Seq_file read operations for /smack/load2
2357 static int load2_seq_show(struct seq_file *s, void *v)
2359 struct list_head *list = v;
2360 struct smack_master_list *smlp =
2361 list_entry_rcu(list, struct smack_master_list, list);
2363 smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
2365 return 0;
2368 static const struct seq_operations load2_seq_ops = {
2369 .start = load2_seq_start,
2370 .next = load2_seq_next,
2371 .show = load2_seq_show,
2372 .stop = smk_seq_stop,
2376 * smk_open_load2 - open() for /smack/load2
2377 * @inode: inode structure representing file
2378 * @file: "load2" file pointer
2380 * For reading, use load2_seq_* seq_file reading operations.
2382 static int smk_open_load2(struct inode *inode, struct file *file)
2384 return seq_open(file, &load2_seq_ops);
2388 * smk_write_load2 - write() for /smack/load2
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
2395 static ssize_t smk_write_load2(struct file *file, const char __user *buf,
2396 size_t count, loff_t *ppos)
2399 * Must have privilege.
2401 if (!smack_privileged(CAP_MAC_ADMIN))
2402 return -EPERM;
2404 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2405 SMK_LONG_FMT);
2408 static const struct file_operations smk_load2_ops = {
2409 .open = smk_open_load2,
2410 .read = seq_read,
2411 .llseek = seq_lseek,
2412 .write = smk_write_load2,
2413 .release = seq_release,
2417 * Seq_file read operations for /smack/load-self2
2420 static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
2422 struct task_smack *tsp = current_security();
2424 return smk_seq_start(s, pos, &tsp->smk_rules);
2427 static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
2429 struct task_smack *tsp = current_security();
2431 return smk_seq_next(s, v, pos, &tsp->smk_rules);
2434 static int load_self2_seq_show(struct seq_file *s, void *v)
2436 struct list_head *list = v;
2437 struct smack_rule *srp =
2438 list_entry_rcu(list, struct smack_rule, list);
2440 smk_rule_show(s, srp, SMK_LONGLABEL);
2442 return 0;
2445 static const struct seq_operations load_self2_seq_ops = {
2446 .start = load_self2_seq_start,
2447 .next = load_self2_seq_next,
2448 .show = load_self2_seq_show,
2449 .stop = smk_seq_stop,
2453 * smk_open_load_self2 - open() for /smack/load-self2
2454 * @inode: inode structure representing file
2455 * @file: "load" file pointer
2457 * For reading, use load_seq_* seq_file reading operations.
2459 static int smk_open_load_self2(struct inode *inode, struct file *file)
2461 return seq_open(file, &load_self2_seq_ops);
2465 * smk_write_load_self2 - write() for /smack/load-self2
2466 * @file: file pointer, not actually used
2467 * @buf: where to get the data from
2468 * @count: bytes sent
2469 * @ppos: where to start - must be 0
2472 static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2473 size_t count, loff_t *ppos)
2475 struct task_smack *tsp = current_security();
2477 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2478 &tsp->smk_rules_lock, SMK_LONG_FMT);
2481 static const struct file_operations smk_load_self2_ops = {
2482 .open = smk_open_load_self2,
2483 .read = seq_read,
2484 .llseek = seq_lseek,
2485 .write = smk_write_load_self2,
2486 .release = seq_release,
2490 * smk_write_access2 - handle access check transaction
2491 * @file: file pointer
2492 * @buf: data from user space
2493 * @count: bytes sent
2494 * @ppos: where to start - must be 0
2496 static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2497 size_t count, loff_t *ppos)
2499 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2502 static const struct file_operations smk_access2_ops = {
2503 .write = smk_write_access2,
2504 .read = simple_transaction_read,
2505 .release = simple_transaction_release,
2506 .llseek = generic_file_llseek,
2510 * smk_write_revoke_subj - write() for /smack/revoke-subject
2511 * @file: file pointer
2512 * @buf: data from user space
2513 * @count: bytes sent
2514 * @ppos: where to start - must be 0
2516 static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2517 size_t count, loff_t *ppos)
2519 char *data;
2520 const char *cp;
2521 struct smack_known *skp;
2522 struct smack_rule *sp;
2523 struct list_head *rule_list;
2524 struct mutex *rule_lock;
2525 int rc = count;
2527 if (*ppos != 0)
2528 return -EINVAL;
2530 if (!smack_privileged(CAP_MAC_ADMIN))
2531 return -EPERM;
2533 if (count == 0 || count > SMK_LONGLABEL)
2534 return -EINVAL;
2536 data = memdup_user(buf, count);
2537 if (IS_ERR(data))
2538 return PTR_ERR(data);
2540 cp = smk_parse_smack(data, count);
2541 if (IS_ERR(cp)) {
2542 rc = PTR_ERR(cp);
2543 goto out_data;
2546 skp = smk_find_entry(cp);
2547 if (skp == NULL)
2548 goto out_cp;
2550 rule_list = &skp->smk_rules;
2551 rule_lock = &skp->smk_rules_lock;
2553 mutex_lock(rule_lock);
2555 list_for_each_entry_rcu(sp, rule_list, list)
2556 sp->smk_access = 0;
2558 mutex_unlock(rule_lock);
2560 out_cp:
2561 kfree(cp);
2562 out_data:
2563 kfree(data);
2565 return rc;
2568 static const struct file_operations smk_revoke_subj_ops = {
2569 .write = smk_write_revoke_subj,
2570 .read = simple_transaction_read,
2571 .release = simple_transaction_release,
2572 .llseek = generic_file_llseek,
2576 * smk_init_sysfs - initialize /sys/fs/smackfs
2579 static int smk_init_sysfs(void)
2581 return sysfs_create_mount_point(fs_kobj, "smackfs");
2585 * smk_write_change_rule - write() for /smack/change-rule
2586 * @file: file pointer
2587 * @buf: data from user space
2588 * @count: bytes sent
2589 * @ppos: where to start - must be 0
2591 static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2592 size_t count, loff_t *ppos)
2595 * Must have privilege.
2597 if (!smack_privileged(CAP_MAC_ADMIN))
2598 return -EPERM;
2600 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2601 SMK_CHANGE_FMT);
2604 static const struct file_operations smk_change_rule_ops = {
2605 .write = smk_write_change_rule,
2606 .read = simple_transaction_read,
2607 .release = simple_transaction_release,
2608 .llseek = generic_file_llseek,
2612 * smk_read_syslog - read() for smackfs/syslog
2613 * @filp: file pointer, not actually used
2614 * @buf: where to put the result
2615 * @cn: maximum to send along
2616 * @ppos: where to start
2618 * Returns number of bytes read or error code, as appropriate
2620 static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
2621 size_t cn, loff_t *ppos)
2623 struct smack_known *skp;
2624 ssize_t rc = -EINVAL;
2625 int asize;
2627 if (*ppos != 0)
2628 return 0;
2630 if (smack_syslog_label == NULL)
2631 skp = &smack_known_star;
2632 else
2633 skp = smack_syslog_label;
2635 asize = strlen(skp->smk_known) + 1;
2637 if (cn >= asize)
2638 rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
2639 asize);
2641 return rc;
2645 * smk_write_syslog - write() for smackfs/syslog
2646 * @file: file pointer, not actually used
2647 * @buf: where to get the data from
2648 * @count: bytes sent
2649 * @ppos: where to start
2651 * Returns number of bytes written or error code, as appropriate
2653 static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
2654 size_t count, loff_t *ppos)
2656 char *data;
2657 struct smack_known *skp;
2658 int rc = count;
2660 if (!smack_privileged(CAP_MAC_ADMIN))
2661 return -EPERM;
2663 data = memdup_user_nul(buf, count);
2664 if (IS_ERR(data))
2665 return PTR_ERR(data);
2667 skp = smk_import_entry(data, count);
2668 if (IS_ERR(skp))
2669 rc = PTR_ERR(skp);
2670 else
2671 smack_syslog_label = skp;
2673 kfree(data);
2674 return rc;
2677 static const struct file_operations smk_syslog_ops = {
2678 .read = smk_read_syslog,
2679 .write = smk_write_syslog,
2680 .llseek = default_llseek,
2684 * Seq_file read operations for /smack/relabel-self
2687 static void *relabel_self_seq_start(struct seq_file *s, loff_t *pos)
2689 struct task_smack *tsp = current_security();
2691 return smk_seq_start(s, pos, &tsp->smk_relabel);
2694 static void *relabel_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
2696 struct task_smack *tsp = current_security();
2698 return smk_seq_next(s, v, pos, &tsp->smk_relabel);
2701 static int relabel_self_seq_show(struct seq_file *s, void *v)
2703 struct list_head *list = v;
2704 struct smack_known_list_elem *sklep =
2705 list_entry(list, struct smack_known_list_elem, list);
2707 seq_puts(s, sklep->smk_label->smk_known);
2708 seq_putc(s, ' ');
2710 return 0;
2713 static const struct seq_operations relabel_self_seq_ops = {
2714 .start = relabel_self_seq_start,
2715 .next = relabel_self_seq_next,
2716 .show = relabel_self_seq_show,
2717 .stop = smk_seq_stop,
2721 * smk_open_relabel_self - open() for /smack/relabel-self
2722 * @inode: inode structure representing file
2723 * @file: "relabel-self" file pointer
2725 * Connect our relabel_self_seq_* operations with /smack/relabel-self
2726 * file_operations
2728 static int smk_open_relabel_self(struct inode *inode, struct file *file)
2730 return seq_open(file, &relabel_self_seq_ops);
2734 * smk_write_relabel_self - write() for /smack/relabel-self
2735 * @file: file pointer, not actually used
2736 * @buf: where to get the data from
2737 * @count: bytes sent
2738 * @ppos: where to start - must be 0
2741 static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
2742 size_t count, loff_t *ppos)
2744 struct task_smack *tsp = current_security();
2745 char *data;
2746 int rc;
2747 LIST_HEAD(list_tmp);
2750 * Must have privilege.
2752 if (!smack_privileged(CAP_MAC_ADMIN))
2753 return -EPERM;
2756 * Enough data must be present.
2758 if (*ppos != 0)
2759 return -EINVAL;
2761 data = memdup_user_nul(buf, count);
2762 if (IS_ERR(data))
2763 return PTR_ERR(data);
2765 rc = smk_parse_label_list(data, &list_tmp);
2766 kfree(data);
2768 if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
2769 smk_destroy_label_list(&tsp->smk_relabel);
2770 list_splice(&list_tmp, &tsp->smk_relabel);
2771 return count;
2774 smk_destroy_label_list(&list_tmp);
2775 return rc;
2778 static const struct file_operations smk_relabel_self_ops = {
2779 .open = smk_open_relabel_self,
2780 .read = seq_read,
2781 .llseek = seq_lseek,
2782 .write = smk_write_relabel_self,
2783 .release = seq_release,
2787 * smk_read_ptrace - read() for /smack/ptrace
2788 * @filp: file pointer, not actually used
2789 * @buf: where to put the result
2790 * @count: maximum to send along
2791 * @ppos: where to start
2793 * Returns number of bytes read or error code, as appropriate
2795 static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
2796 size_t count, loff_t *ppos)
2798 char temp[32];
2799 ssize_t rc;
2801 if (*ppos != 0)
2802 return 0;
2804 sprintf(temp, "%d\n", smack_ptrace_rule);
2805 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2806 return rc;
2810 * smk_write_ptrace - write() for /smack/ptrace
2811 * @file: file pointer
2812 * @buf: data from user space
2813 * @count: bytes sent
2814 * @ppos: where to start - must be 0
2816 static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
2817 size_t count, loff_t *ppos)
2819 char temp[32];
2820 int i;
2822 if (!smack_privileged(CAP_MAC_ADMIN))
2823 return -EPERM;
2825 if (*ppos != 0 || count >= sizeof(temp) || count == 0)
2826 return -EINVAL;
2828 if (copy_from_user(temp, buf, count) != 0)
2829 return -EFAULT;
2831 temp[count] = '\0';
2833 if (sscanf(temp, "%d", &i) != 1)
2834 return -EINVAL;
2835 if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
2836 return -EINVAL;
2837 smack_ptrace_rule = i;
2839 return count;
2842 static const struct file_operations smk_ptrace_ops = {
2843 .write = smk_write_ptrace,
2844 .read = smk_read_ptrace,
2845 .llseek = default_llseek,
2849 * smk_fill_super - fill the smackfs superblock
2850 * @sb: the empty superblock
2851 * @data: unused
2852 * @silent: unused
2854 * Fill in the well known entries for the smack filesystem
2856 * Returns 0 on success, an error code on failure
2858 static int smk_fill_super(struct super_block *sb, void *data, int silent)
2860 int rc;
2861 struct inode *root_inode;
2863 static struct tree_descr smack_files[] = {
2864 [SMK_LOAD] = {
2865 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2866 [SMK_CIPSO] = {
2867 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2868 [SMK_DOI] = {
2869 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2870 [SMK_DIRECT] = {
2871 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2872 [SMK_AMBIENT] = {
2873 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2874 [SMK_NET4ADDR] = {
2875 "netlabel", &smk_net4addr_ops, S_IRUGO|S_IWUSR},
2876 [SMK_ONLYCAP] = {
2877 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2878 [SMK_LOGGING] = {
2879 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2880 [SMK_LOAD_SELF] = {
2881 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
2882 [SMK_ACCESSES] = {
2883 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
2884 [SMK_MAPPED] = {
2885 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2886 [SMK_LOAD2] = {
2887 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2888 [SMK_LOAD_SELF2] = {
2889 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2890 [SMK_ACCESS2] = {
2891 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2892 [SMK_CIPSO2] = {
2893 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
2894 [SMK_REVOKE_SUBJ] = {
2895 "revoke-subject", &smk_revoke_subj_ops,
2896 S_IRUGO|S_IWUSR},
2897 [SMK_CHANGE_RULE] = {
2898 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
2899 [SMK_SYSLOG] = {
2900 "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
2901 [SMK_PTRACE] = {
2902 "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
2903 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
2904 [SMK_UNCONFINED] = {
2905 "unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR},
2906 #endif
2907 #if IS_ENABLED(CONFIG_IPV6)
2908 [SMK_NET6ADDR] = {
2909 "ipv6host", &smk_net6addr_ops, S_IRUGO|S_IWUSR},
2910 #endif /* CONFIG_IPV6 */
2911 [SMK_RELABEL_SELF] = {
2912 "relabel-self", &smk_relabel_self_ops,
2913 S_IRUGO|S_IWUGO},
2914 /* last one */
2915 {""}
2918 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2919 if (rc != 0) {
2920 printk(KERN_ERR "%s failed %d while creating inodes\n",
2921 __func__, rc);
2922 return rc;
2925 root_inode = d_inode(sb->s_root);
2927 return 0;
2931 * smk_mount - get the smackfs superblock
2932 * @fs_type: passed along without comment
2933 * @flags: passed along without comment
2934 * @dev_name: passed along without comment
2935 * @data: passed along without comment
2937 * Just passes everything along.
2939 * Returns what the lower level code does.
2941 static struct dentry *smk_mount(struct file_system_type *fs_type,
2942 int flags, const char *dev_name, void *data)
2944 return mount_single(fs_type, flags, data, smk_fill_super);
2947 static struct file_system_type smk_fs_type = {
2948 .name = "smackfs",
2949 .mount = smk_mount,
2950 .kill_sb = kill_litter_super,
2953 static struct vfsmount *smackfs_mount;
2955 static int __init smk_preset_netlabel(struct smack_known *skp)
2957 skp->smk_netlabel.domain = skp->smk_known;
2958 skp->smk_netlabel.flags =
2959 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2960 return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2961 &skp->smk_netlabel, strlen(skp->smk_known));
2965 * init_smk_fs - get the smackfs superblock
2967 * register the smackfs
2969 * Do not register smackfs if Smack wasn't enabled
2970 * on boot. We can not put this method normally under the
2971 * smack_init() code path since the security subsystem get
2972 * initialized before the vfs caches.
2974 * Returns true if we were not chosen on boot or if
2975 * we were chosen and filesystem registration succeeded.
2977 static int __init init_smk_fs(void)
2979 int err;
2980 int rc;
2982 if (smack_enabled == 0)
2983 return 0;
2985 err = smk_init_sysfs();
2986 if (err)
2987 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2989 err = register_filesystem(&smk_fs_type);
2990 if (!err) {
2991 smackfs_mount = kern_mount(&smk_fs_type);
2992 if (IS_ERR(smackfs_mount)) {
2993 printk(KERN_ERR "smackfs: could not mount!\n");
2994 err = PTR_ERR(smackfs_mount);
2995 smackfs_mount = NULL;
2999 smk_cipso_doi();
3000 smk_unlbl_ambient(NULL);
3002 rc = smk_preset_netlabel(&smack_known_floor);
3003 if (err == 0 && rc < 0)
3004 err = rc;
3005 rc = smk_preset_netlabel(&smack_known_hat);
3006 if (err == 0 && rc < 0)
3007 err = rc;
3008 rc = smk_preset_netlabel(&smack_known_huh);
3009 if (err == 0 && rc < 0)
3010 err = rc;
3011 rc = smk_preset_netlabel(&smack_known_invalid);
3012 if (err == 0 && rc < 0)
3013 err = rc;
3014 rc = smk_preset_netlabel(&smack_known_star);
3015 if (err == 0 && rc < 0)
3016 err = rc;
3017 rc = smk_preset_netlabel(&smack_known_web);
3018 if (err == 0 && rc < 0)
3019 err = rc;
3021 return err;
3024 __initcall(init_smk_fs);