sound: seq_midi_event: fix decoding of (N)RPN events
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / security / smack / smackfs.c
blob51f0efc50dab46a9200f98b6d22db0039a9a0653
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 <net/net_namespace.h>
24 #include <net/netlabel.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 "smack.h"
32 * smackfs pseudo filesystem.
35 enum smk_inos {
36 SMK_ROOT_INO = 2,
37 SMK_LOAD = 3, /* load policy */
38 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
39 SMK_DOI = 5, /* CIPSO DOI */
40 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
41 SMK_AMBIENT = 7, /* internet ambient label */
42 SMK_NETLBLADDR = 8, /* single label hosts */
43 SMK_ONLYCAP = 9, /* the only "capable" label */
47 * List locks
49 static DEFINE_MUTEX(smack_list_lock);
50 static DEFINE_MUTEX(smack_cipso_lock);
51 static DEFINE_MUTEX(smack_ambient_lock);
52 static DEFINE_MUTEX(smk_netlbladdr_lock);
55 * This is the "ambient" label for network traffic.
56 * If it isn't somehow marked, use this.
57 * It can be reset via smackfs/ambient
59 char *smack_net_ambient = smack_known_floor.smk_known;
62 * This is the level in a CIPSO header that indicates a
63 * smack label is contained directly in the category set.
64 * It can be reset via smackfs/direct
66 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
69 * Unless a process is running with this label even
70 * having CAP_MAC_OVERRIDE isn't enough to grant
71 * privilege to violate MAC policy. If no label is
72 * designated (the NULL case) capabilities apply to
73 * everyone. It is expected that the hat (^) label
74 * will be used if any label is used.
76 char *smack_onlycap;
79 * Certain IP addresses may be designated as single label hosts.
80 * Packets are sent there unlabeled, but only from tasks that
81 * can write to the specified label.
83 struct smk_netlbladdr *smack_netlbladdrs;
85 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
86 struct smk_list_entry *smack_list;
88 #define SEQ_READ_FINISHED 1
91 * Values for parsing cipso rules
92 * SMK_DIGITLEN: Length of a digit field in a rule.
93 * SMK_CIPSOMIN: Minimum possible cipso rule length.
94 * SMK_CIPSOMAX: Maximum possible cipso rule length.
96 #define SMK_DIGITLEN 4
97 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
98 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
101 * Values for parsing MAC rules
102 * SMK_ACCESS: Maximum possible combination of access permissions
103 * SMK_ACCESSLEN: Maximum length for a rule access field
104 * SMK_LOADLEN: Smack rule length
106 #define SMK_ACCESS "rwxa"
107 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
108 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
111 * smk_netlabel_audit_set - fill a netlbl_audit struct
112 * @nap: structure to fill
114 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
116 nap->loginuid = audit_get_loginuid(current);
117 nap->sessionid = audit_get_sessionid(current);
118 nap->secid = smack_to_secid(current_security());
122 * Values for parsing single label host rules
123 * "1.2.3.4 X"
124 * "192.168.138.129/32 abcdefghijklmnopqrstuvw"
126 #define SMK_NETLBLADDRMIN 9
127 #define SMK_NETLBLADDRMAX 42
130 * Seq_file read operations for /smack/load
133 static void *load_seq_start(struct seq_file *s, loff_t *pos)
135 if (*pos == SEQ_READ_FINISHED)
136 return NULL;
138 return smack_list;
141 static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
143 struct smk_list_entry *skp = ((struct smk_list_entry *) v)->smk_next;
145 if (skp == NULL)
146 *pos = SEQ_READ_FINISHED;
148 return skp;
151 static int load_seq_show(struct seq_file *s, void *v)
153 struct smk_list_entry *slp = (struct smk_list_entry *) v;
154 struct smack_rule *srp = &slp->smk_rule;
156 seq_printf(s, "%s %s", (char *)srp->smk_subject,
157 (char *)srp->smk_object);
159 seq_putc(s, ' ');
161 if (srp->smk_access & MAY_READ)
162 seq_putc(s, 'r');
163 if (srp->smk_access & MAY_WRITE)
164 seq_putc(s, 'w');
165 if (srp->smk_access & MAY_EXEC)
166 seq_putc(s, 'x');
167 if (srp->smk_access & MAY_APPEND)
168 seq_putc(s, 'a');
169 if (srp->smk_access == 0)
170 seq_putc(s, '-');
172 seq_putc(s, '\n');
174 return 0;
177 static void load_seq_stop(struct seq_file *s, void *v)
179 /* No-op */
182 static struct seq_operations load_seq_ops = {
183 .start = load_seq_start,
184 .next = load_seq_next,
185 .show = load_seq_show,
186 .stop = load_seq_stop,
190 * smk_open_load - open() for /smack/load
191 * @inode: inode structure representing file
192 * @file: "load" file pointer
194 * For reading, use load_seq_* seq_file reading operations.
196 static int smk_open_load(struct inode *inode, struct file *file)
198 return seq_open(file, &load_seq_ops);
202 * smk_set_access - add a rule to the rule list
203 * @srp: the new rule to add
205 * Looks through the current subject/object/access list for
206 * the subject/object pair and replaces the access that was
207 * there. If the pair isn't found add it with the specified
208 * access.
210 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
211 * during the allocation of the new pair to add.
213 static int smk_set_access(struct smack_rule *srp)
215 struct smk_list_entry *sp;
216 struct smk_list_entry *newp;
217 int ret = 0;
219 mutex_lock(&smack_list_lock);
221 for (sp = smack_list; sp != NULL; sp = sp->smk_next)
222 if (sp->smk_rule.smk_subject == srp->smk_subject &&
223 sp->smk_rule.smk_object == srp->smk_object) {
224 sp->smk_rule.smk_access = srp->smk_access;
225 break;
228 if (sp == NULL) {
229 newp = kzalloc(sizeof(struct smk_list_entry), GFP_KERNEL);
230 if (newp == NULL) {
231 ret = -ENOMEM;
232 goto out;
235 newp->smk_rule = *srp;
236 newp->smk_next = smack_list;
237 smack_list = newp;
240 out:
241 mutex_unlock(&smack_list_lock);
243 return ret;
247 * smk_write_load - write() for /smack/load
248 * @filp: file pointer, not actually used
249 * @buf: where to get the data from
250 * @count: bytes sent
251 * @ppos: where to start - must be 0
253 * Get one smack access rule from above.
254 * The format is exactly:
255 * char subject[SMK_LABELLEN]
256 * char object[SMK_LABELLEN]
257 * char access[SMK_ACCESSLEN]
259 * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
261 static ssize_t smk_write_load(struct file *file, const char __user *buf,
262 size_t count, loff_t *ppos)
264 struct smack_rule rule;
265 char *data;
266 int rc = -EINVAL;
269 * Must have privilege.
270 * No partial writes.
271 * Enough data must be present.
273 if (!capable(CAP_MAC_ADMIN))
274 return -EPERM;
275 if (*ppos != 0)
276 return -EINVAL;
277 if (count != SMK_LOADLEN)
278 return -EINVAL;
280 data = kzalloc(count, GFP_KERNEL);
281 if (data == NULL)
282 return -ENOMEM;
284 if (copy_from_user(data, buf, count) != 0) {
285 rc = -EFAULT;
286 goto out;
289 rule.smk_subject = smk_import(data, 0);
290 if (rule.smk_subject == NULL)
291 goto out;
293 rule.smk_object = smk_import(data + SMK_LABELLEN, 0);
294 if (rule.smk_object == NULL)
295 goto out;
297 rule.smk_access = 0;
299 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
300 case '-':
301 break;
302 case 'r':
303 case 'R':
304 rule.smk_access |= MAY_READ;
305 break;
306 default:
307 goto out;
310 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
311 case '-':
312 break;
313 case 'w':
314 case 'W':
315 rule.smk_access |= MAY_WRITE;
316 break;
317 default:
318 goto out;
321 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
322 case '-':
323 break;
324 case 'x':
325 case 'X':
326 rule.smk_access |= MAY_EXEC;
327 break;
328 default:
329 goto out;
332 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
333 case '-':
334 break;
335 case 'a':
336 case 'A':
337 rule.smk_access |= MAY_APPEND;
338 break;
339 default:
340 goto out;
343 rc = smk_set_access(&rule);
345 if (!rc)
346 rc = count;
348 out:
349 kfree(data);
350 return rc;
353 static const struct file_operations smk_load_ops = {
354 .open = smk_open_load,
355 .read = seq_read,
356 .llseek = seq_lseek,
357 .write = smk_write_load,
358 .release = seq_release,
362 * smk_cipso_doi - initialize the CIPSO domain
364 static void smk_cipso_doi(void)
366 int rc;
367 struct cipso_v4_doi *doip;
368 struct netlbl_audit nai;
370 smk_netlabel_audit_set(&nai);
372 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
373 if (rc != 0)
374 printk(KERN_WARNING "%s:%d remove rc = %d\n",
375 __func__, __LINE__, rc);
377 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
378 if (doip == NULL)
379 panic("smack: Failed to initialize cipso DOI.\n");
380 doip->map.std = NULL;
381 doip->doi = smk_cipso_doi_value;
382 doip->type = CIPSO_V4_MAP_PASS;
383 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
384 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
385 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
387 rc = netlbl_cfg_cipsov4_add(doip, &nai);
388 if (rc != 0) {
389 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
390 __func__, __LINE__, rc);
391 kfree(doip);
392 return;
394 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
395 if (rc != 0) {
396 printk(KERN_WARNING "%s:%d map add rc = %d\n",
397 __func__, __LINE__, rc);
398 kfree(doip);
399 return;
404 * smk_unlbl_ambient - initialize the unlabeled domain
406 static void smk_unlbl_ambient(char *oldambient)
408 int rc;
409 struct netlbl_audit nai;
411 smk_netlabel_audit_set(&nai);
413 if (oldambient != NULL) {
414 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
415 if (rc != 0)
416 printk(KERN_WARNING "%s:%d remove rc = %d\n",
417 __func__, __LINE__, rc);
420 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
421 NULL, NULL, &nai);
422 if (rc != 0)
423 printk(KERN_WARNING "%s:%d add rc = %d\n",
424 __func__, __LINE__, rc);
428 * Seq_file read operations for /smack/cipso
431 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
433 if (*pos == SEQ_READ_FINISHED)
434 return NULL;
436 return smack_known;
439 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
441 struct smack_known *skp = ((struct smack_known *) v)->smk_next;
444 * Omit labels with no associated cipso value
446 while (skp != NULL && !skp->smk_cipso)
447 skp = skp->smk_next;
449 if (skp == NULL)
450 *pos = SEQ_READ_FINISHED;
452 return skp;
456 * Print cipso labels in format:
457 * label level[/cat[,cat]]
459 static int cipso_seq_show(struct seq_file *s, void *v)
461 struct smack_known *skp = (struct smack_known *) v;
462 struct smack_cipso *scp = skp->smk_cipso;
463 char *cbp;
464 char sep = '/';
465 int cat = 1;
466 int i;
467 unsigned char m;
469 if (scp == NULL)
470 return 0;
472 seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
474 cbp = scp->smk_catset;
475 for (i = 0; i < SMK_LABELLEN; i++)
476 for (m = 0x80; m != 0; m >>= 1) {
477 if (m & cbp[i]) {
478 seq_printf(s, "%c%d", sep, cat);
479 sep = ',';
481 cat++;
484 seq_putc(s, '\n');
486 return 0;
489 static void cipso_seq_stop(struct seq_file *s, void *v)
491 /* No-op */
494 static struct seq_operations cipso_seq_ops = {
495 .start = cipso_seq_start,
496 .stop = cipso_seq_stop,
497 .next = cipso_seq_next,
498 .show = cipso_seq_show,
502 * smk_open_cipso - open() for /smack/cipso
503 * @inode: inode structure representing file
504 * @file: "cipso" file pointer
506 * Connect our cipso_seq_* operations with /smack/cipso
507 * file_operations
509 static int smk_open_cipso(struct inode *inode, struct file *file)
511 return seq_open(file, &cipso_seq_ops);
515 * smk_write_cipso - write() for /smack/cipso
516 * @filp: file pointer, not actually used
517 * @buf: where to get the data from
518 * @count: bytes sent
519 * @ppos: where to start
521 * Accepts only one cipso rule per write call.
522 * Returns number of bytes written or error code, as appropriate
524 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
525 size_t count, loff_t *ppos)
527 struct smack_known *skp;
528 struct smack_cipso *scp = NULL;
529 char mapcatset[SMK_LABELLEN];
530 int maplevel;
531 int cat;
532 int catlen;
533 ssize_t rc = -EINVAL;
534 char *data = NULL;
535 char *rule;
536 int ret;
537 int i;
540 * Must have privilege.
541 * No partial writes.
542 * Enough data must be present.
544 if (!capable(CAP_MAC_ADMIN))
545 return -EPERM;
546 if (*ppos != 0)
547 return -EINVAL;
548 if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
549 return -EINVAL;
551 data = kzalloc(count + 1, GFP_KERNEL);
552 if (data == NULL)
553 return -ENOMEM;
555 if (copy_from_user(data, buf, count) != 0) {
556 rc = -EFAULT;
557 goto unlockedout;
560 data[count] = '\0';
561 rule = data;
563 * Only allow one writer at a time. Writes should be
564 * quite rare and small in any case.
566 mutex_lock(&smack_cipso_lock);
568 skp = smk_import_entry(rule, 0);
569 if (skp == NULL)
570 goto out;
572 rule += SMK_LABELLEN;
573 ret = sscanf(rule, "%d", &maplevel);
574 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
575 goto out;
577 rule += SMK_DIGITLEN;
578 ret = sscanf(rule, "%d", &catlen);
579 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
580 goto out;
582 if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
583 goto out;
585 memset(mapcatset, 0, sizeof(mapcatset));
587 for (i = 0; i < catlen; i++) {
588 rule += SMK_DIGITLEN;
589 ret = sscanf(rule, "%d", &cat);
590 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
591 goto out;
593 smack_catset_bit(cat, mapcatset);
596 if (skp->smk_cipso == NULL) {
597 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
598 if (scp == NULL) {
599 rc = -ENOMEM;
600 goto out;
604 spin_lock_bh(&skp->smk_cipsolock);
606 if (scp == NULL)
607 scp = skp->smk_cipso;
608 else
609 skp->smk_cipso = scp;
611 scp->smk_level = maplevel;
612 memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
614 spin_unlock_bh(&skp->smk_cipsolock);
616 rc = count;
617 out:
618 mutex_unlock(&smack_cipso_lock);
619 unlockedout:
620 kfree(data);
621 return rc;
624 static const struct file_operations smk_cipso_ops = {
625 .open = smk_open_cipso,
626 .read = seq_read,
627 .llseek = seq_lseek,
628 .write = smk_write_cipso,
629 .release = seq_release,
633 * Seq_file read operations for /smack/netlabel
636 static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
638 if (*pos == SEQ_READ_FINISHED)
639 return NULL;
641 return smack_netlbladdrs;
644 static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
646 struct smk_netlbladdr *skp = ((struct smk_netlbladdr *) v)->smk_next;
648 if (skp == NULL)
649 *pos = SEQ_READ_FINISHED;
651 return skp;
653 #define BEBITS (sizeof(__be32) * 8)
656 * Print host/label pairs
658 static int netlbladdr_seq_show(struct seq_file *s, void *v)
660 struct smk_netlbladdr *skp = (struct smk_netlbladdr *) v;
661 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
662 int maskn;
663 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
665 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
667 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
668 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
670 return 0;
673 static void netlbladdr_seq_stop(struct seq_file *s, void *v)
675 /* No-op */
678 static struct seq_operations netlbladdr_seq_ops = {
679 .start = netlbladdr_seq_start,
680 .stop = netlbladdr_seq_stop,
681 .next = netlbladdr_seq_next,
682 .show = netlbladdr_seq_show,
686 * smk_open_netlbladdr - open() for /smack/netlabel
687 * @inode: inode structure representing file
688 * @file: "netlabel" file pointer
690 * Connect our netlbladdr_seq_* operations with /smack/netlabel
691 * file_operations
693 static int smk_open_netlbladdr(struct inode *inode, struct file *file)
695 return seq_open(file, &netlbladdr_seq_ops);
699 * smk_netlbladdr_insert
700 * @new : netlabel to insert
702 * This helper insert netlabel in the smack_netlbladdrs list
703 * sorted by netmask length (longest to smallest)
705 static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
707 struct smk_netlbladdr *m;
709 if (smack_netlbladdrs == NULL) {
710 smack_netlbladdrs = new;
711 return;
714 /* the comparison '>' is a bit hacky, but works */
715 if (new->smk_mask.s_addr > smack_netlbladdrs->smk_mask.s_addr) {
716 new->smk_next = smack_netlbladdrs;
717 smack_netlbladdrs = new;
718 return;
720 for (m = smack_netlbladdrs; m != NULL; m = m->smk_next) {
721 if (m->smk_next == NULL) {
722 m->smk_next = new;
723 return;
725 if (new->smk_mask.s_addr > m->smk_next->smk_mask.s_addr) {
726 new->smk_next = m->smk_next;
727 m->smk_next = new;
728 return;
735 * smk_write_netlbladdr - write() for /smack/netlabel
736 * @filp: file pointer, not actually used
737 * @buf: where to get the data from
738 * @count: bytes sent
739 * @ppos: where to start
741 * Accepts only one netlbladdr per write call.
742 * Returns number of bytes written or error code, as appropriate
744 static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
745 size_t count, loff_t *ppos)
747 struct smk_netlbladdr *skp;
748 struct sockaddr_in newname;
749 char smack[SMK_LABELLEN];
750 char *sp;
751 char data[SMK_NETLBLADDRMAX];
752 char *host = (char *)&newname.sin_addr.s_addr;
753 int rc;
754 struct netlbl_audit audit_info;
755 struct in_addr mask;
756 unsigned int m;
757 u32 mask_bits = (1<<31);
758 __be32 nsa;
759 u32 temp_mask;
762 * Must have privilege.
763 * No partial writes.
764 * Enough data must be present.
765 * "<addr/mask, as a.b.c.d/e><space><label>"
766 * "<addr, as a.b.c.d><space><label>"
768 if (!capable(CAP_MAC_ADMIN))
769 return -EPERM;
770 if (*ppos != 0)
771 return -EINVAL;
772 if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX)
773 return -EINVAL;
774 if (copy_from_user(data, buf, count) != 0)
775 return -EFAULT;
777 data[count] = '\0';
779 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
780 &host[0], &host[1], &host[2], &host[3], &m, smack);
781 if (rc != 6) {
782 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
783 &host[0], &host[1], &host[2], &host[3], smack);
784 if (rc != 5)
785 return -EINVAL;
786 m = BEBITS;
788 if (m > BEBITS)
789 return -EINVAL;
791 sp = smk_import(smack, 0);
792 if (sp == NULL)
793 return -EINVAL;
795 for (temp_mask = 0; m > 0; m--) {
796 temp_mask |= mask_bits;
797 mask_bits >>= 1;
799 mask.s_addr = cpu_to_be32(temp_mask);
801 newname.sin_addr.s_addr &= mask.s_addr;
803 * Only allow one writer at a time. Writes should be
804 * quite rare and small in any case.
806 mutex_lock(&smk_netlbladdr_lock);
808 nsa = newname.sin_addr.s_addr;
809 /* try to find if the prefix is already in the list */
810 for (skp = smack_netlbladdrs; skp != NULL; skp = skp->smk_next)
811 if (skp->smk_host.sin_addr.s_addr == nsa &&
812 skp->smk_mask.s_addr == mask.s_addr)
813 break;
815 smk_netlabel_audit_set(&audit_info);
817 if (skp == NULL) {
818 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
819 if (skp == NULL)
820 rc = -ENOMEM;
821 else {
822 rc = 0;
823 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
824 skp->smk_mask.s_addr = mask.s_addr;
825 skp->smk_label = sp;
826 smk_netlbladdr_insert(skp);
828 } else {
829 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
830 &skp->smk_host.sin_addr, &skp->smk_mask,
831 PF_INET, &audit_info);
832 skp->smk_label = sp;
836 * Now tell netlabel about the single label nature of
837 * this host so that incoming packets get labeled.
840 if (rc == 0)
841 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
842 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
843 smack_to_secid(skp->smk_label), &audit_info);
845 if (rc == 0)
846 rc = count;
848 mutex_unlock(&smk_netlbladdr_lock);
850 return rc;
853 static const struct file_operations smk_netlbladdr_ops = {
854 .open = smk_open_netlbladdr,
855 .read = seq_read,
856 .llseek = seq_lseek,
857 .write = smk_write_netlbladdr,
858 .release = seq_release,
862 * smk_read_doi - read() for /smack/doi
863 * @filp: file pointer, not actually used
864 * @buf: where to put the result
865 * @count: maximum to send along
866 * @ppos: where to start
868 * Returns number of bytes read or error code, as appropriate
870 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
871 size_t count, loff_t *ppos)
873 char temp[80];
874 ssize_t rc;
876 if (*ppos != 0)
877 return 0;
879 sprintf(temp, "%d", smk_cipso_doi_value);
880 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
882 return rc;
886 * smk_write_doi - write() for /smack/doi
887 * @filp: file pointer, not actually used
888 * @buf: where to get the data from
889 * @count: bytes sent
890 * @ppos: where to start
892 * Returns number of bytes written or error code, as appropriate
894 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
895 size_t count, loff_t *ppos)
897 char temp[80];
898 int i;
900 if (!capable(CAP_MAC_ADMIN))
901 return -EPERM;
903 if (count >= sizeof(temp) || count == 0)
904 return -EINVAL;
906 if (copy_from_user(temp, buf, count) != 0)
907 return -EFAULT;
909 temp[count] = '\0';
911 if (sscanf(temp, "%d", &i) != 1)
912 return -EINVAL;
914 smk_cipso_doi_value = i;
916 smk_cipso_doi();
918 return count;
921 static const struct file_operations smk_doi_ops = {
922 .read = smk_read_doi,
923 .write = smk_write_doi,
927 * smk_read_direct - read() for /smack/direct
928 * @filp: file pointer, not actually used
929 * @buf: where to put the result
930 * @count: maximum to send along
931 * @ppos: where to start
933 * Returns number of bytes read or error code, as appropriate
935 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
936 size_t count, loff_t *ppos)
938 char temp[80];
939 ssize_t rc;
941 if (*ppos != 0)
942 return 0;
944 sprintf(temp, "%d", smack_cipso_direct);
945 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
947 return rc;
951 * smk_write_direct - write() for /smack/direct
952 * @filp: file pointer, not actually used
953 * @buf: where to get the data from
954 * @count: bytes sent
955 * @ppos: where to start
957 * Returns number of bytes written or error code, as appropriate
959 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
960 size_t count, loff_t *ppos)
962 char temp[80];
963 int i;
965 if (!capable(CAP_MAC_ADMIN))
966 return -EPERM;
968 if (count >= sizeof(temp) || count == 0)
969 return -EINVAL;
971 if (copy_from_user(temp, buf, count) != 0)
972 return -EFAULT;
974 temp[count] = '\0';
976 if (sscanf(temp, "%d", &i) != 1)
977 return -EINVAL;
979 smack_cipso_direct = i;
981 return count;
984 static const struct file_operations smk_direct_ops = {
985 .read = smk_read_direct,
986 .write = smk_write_direct,
990 * smk_read_ambient - read() for /smack/ambient
991 * @filp: file pointer, not actually used
992 * @buf: where to put the result
993 * @cn: maximum to send along
994 * @ppos: where to start
996 * Returns number of bytes read or error code, as appropriate
998 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
999 size_t cn, loff_t *ppos)
1001 ssize_t rc;
1002 int asize;
1004 if (*ppos != 0)
1005 return 0;
1007 * Being careful to avoid a problem in the case where
1008 * smack_net_ambient gets changed in midstream.
1010 mutex_lock(&smack_ambient_lock);
1012 asize = strlen(smack_net_ambient) + 1;
1014 if (cn >= asize)
1015 rc = simple_read_from_buffer(buf, cn, ppos,
1016 smack_net_ambient, asize);
1017 else
1018 rc = -EINVAL;
1020 mutex_unlock(&smack_ambient_lock);
1022 return rc;
1026 * smk_write_ambient - write() for /smack/ambient
1027 * @filp: file pointer, not actually used
1028 * @buf: where to get the data from
1029 * @count: bytes sent
1030 * @ppos: where to start
1032 * Returns number of bytes written or error code, as appropriate
1034 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1035 size_t count, loff_t *ppos)
1037 char in[SMK_LABELLEN];
1038 char *oldambient;
1039 char *smack;
1041 if (!capable(CAP_MAC_ADMIN))
1042 return -EPERM;
1044 if (count >= SMK_LABELLEN)
1045 return -EINVAL;
1047 if (copy_from_user(in, buf, count) != 0)
1048 return -EFAULT;
1050 smack = smk_import(in, count);
1051 if (smack == NULL)
1052 return -EINVAL;
1054 mutex_lock(&smack_ambient_lock);
1056 oldambient = smack_net_ambient;
1057 smack_net_ambient = smack;
1058 smk_unlbl_ambient(oldambient);
1060 mutex_unlock(&smack_ambient_lock);
1062 return count;
1065 static const struct file_operations smk_ambient_ops = {
1066 .read = smk_read_ambient,
1067 .write = smk_write_ambient,
1071 * smk_read_onlycap - read() for /smack/onlycap
1072 * @filp: file pointer, not actually used
1073 * @buf: where to put the result
1074 * @cn: maximum to send along
1075 * @ppos: where to start
1077 * Returns number of bytes read or error code, as appropriate
1079 static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1080 size_t cn, loff_t *ppos)
1082 char *smack = "";
1083 ssize_t rc = -EINVAL;
1084 int asize;
1086 if (*ppos != 0)
1087 return 0;
1089 if (smack_onlycap != NULL)
1090 smack = smack_onlycap;
1092 asize = strlen(smack) + 1;
1094 if (cn >= asize)
1095 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1097 return rc;
1101 * smk_write_onlycap - write() for /smack/onlycap
1102 * @filp: file pointer, not actually used
1103 * @buf: where to get the data from
1104 * @count: bytes sent
1105 * @ppos: where to start
1107 * Returns number of bytes written or error code, as appropriate
1109 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1110 size_t count, loff_t *ppos)
1112 char in[SMK_LABELLEN];
1113 char *sp = current->cred->security;
1115 if (!capable(CAP_MAC_ADMIN))
1116 return -EPERM;
1119 * This can be done using smk_access() but is done
1120 * explicitly for clarity. The smk_access() implementation
1121 * would use smk_access(smack_onlycap, MAY_WRITE)
1123 if (smack_onlycap != NULL && smack_onlycap != sp)
1124 return -EPERM;
1126 if (count >= SMK_LABELLEN)
1127 return -EINVAL;
1129 if (copy_from_user(in, buf, count) != 0)
1130 return -EFAULT;
1133 * Should the null string be passed in unset the onlycap value.
1134 * This seems like something to be careful with as usually
1135 * smk_import only expects to return NULL for errors. It
1136 * is usually the case that a nullstring or "\n" would be
1137 * bad to pass to smk_import but in fact this is useful here.
1139 smack_onlycap = smk_import(in, count);
1141 return count;
1144 static const struct file_operations smk_onlycap_ops = {
1145 .read = smk_read_onlycap,
1146 .write = smk_write_onlycap,
1150 * smk_fill_super - fill the /smackfs superblock
1151 * @sb: the empty superblock
1152 * @data: unused
1153 * @silent: unused
1155 * Fill in the well known entries for /smack
1157 * Returns 0 on success, an error code on failure
1159 static int smk_fill_super(struct super_block *sb, void *data, int silent)
1161 int rc;
1162 struct inode *root_inode;
1164 static struct tree_descr smack_files[] = {
1165 [SMK_LOAD] =
1166 {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
1167 [SMK_CIPSO] =
1168 {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
1169 [SMK_DOI] =
1170 {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
1171 [SMK_DIRECT] =
1172 {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
1173 [SMK_AMBIENT] =
1174 {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
1175 [SMK_NETLBLADDR] =
1176 {"netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
1177 [SMK_ONLYCAP] =
1178 {"onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
1179 /* last one */ {""}
1182 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
1183 if (rc != 0) {
1184 printk(KERN_ERR "%s failed %d while creating inodes\n",
1185 __func__, rc);
1186 return rc;
1189 root_inode = sb->s_root->d_inode;
1190 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
1192 return 0;
1196 * smk_get_sb - get the smackfs superblock
1197 * @fs_type: passed along without comment
1198 * @flags: passed along without comment
1199 * @dev_name: passed along without comment
1200 * @data: passed along without comment
1201 * @mnt: passed along without comment
1203 * Just passes everything along.
1205 * Returns what the lower level code does.
1207 static int smk_get_sb(struct file_system_type *fs_type,
1208 int flags, const char *dev_name, void *data,
1209 struct vfsmount *mnt)
1211 return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
1214 static struct file_system_type smk_fs_type = {
1215 .name = "smackfs",
1216 .get_sb = smk_get_sb,
1217 .kill_sb = kill_litter_super,
1220 static struct vfsmount *smackfs_mount;
1223 * init_smk_fs - get the smackfs superblock
1225 * register the smackfs
1227 * Do not register smackfs if Smack wasn't enabled
1228 * on boot. We can not put this method normally under the
1229 * smack_init() code path since the security subsystem get
1230 * initialized before the vfs caches.
1232 * Returns true if we were not chosen on boot or if
1233 * we were chosen and filesystem registration succeeded.
1235 static int __init init_smk_fs(void)
1237 int err;
1239 if (!security_module_enable(&smack_ops))
1240 return 0;
1242 err = register_filesystem(&smk_fs_type);
1243 if (!err) {
1244 smackfs_mount = kern_mount(&smk_fs_type);
1245 if (IS_ERR(smackfs_mount)) {
1246 printk(KERN_ERR "smackfs: could not mount!\n");
1247 err = PTR_ERR(smackfs_mount);
1248 smackfs_mount = NULL;
1252 smk_cipso_doi();
1253 smk_unlbl_ambient(NULL);
1255 return err;
1258 __initcall(init_smk_fs);