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.
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/netlabel.h>
26 #include <net/cipso_ipv4.h>
27 #include <linux/seq_file.h>
28 #include <linux/ctype.h>
29 #include <linux/audit.h>
33 * smackfs pseudo filesystem.
38 SMK_LOAD
= 3, /* load policy */
39 SMK_CIPSO
= 4, /* load label -> CIPSO mapping */
40 SMK_DOI
= 5, /* CIPSO DOI */
41 SMK_DIRECT
= 6, /* CIPSO level indicating direct label */
42 SMK_AMBIENT
= 7, /* internet ambient label */
43 SMK_NETLBLADDR
= 8, /* single label hosts */
44 SMK_ONLYCAP
= 9, /* the only "capable" label */
45 SMK_LOGGING
= 10, /* logging */
46 SMK_LOAD_SELF
= 11, /* task specific rules */
47 SMK_ACCESSES
= 12, /* access policy */
53 static DEFINE_MUTEX(smack_list_lock
);
54 static DEFINE_MUTEX(smack_cipso_lock
);
55 static DEFINE_MUTEX(smack_ambient_lock
);
56 static DEFINE_MUTEX(smk_netlbladdr_lock
);
59 * This is the "ambient" label for network traffic.
60 * If it isn't somehow marked, use this.
61 * It can be reset via smackfs/ambient
63 char *smack_net_ambient
= smack_known_floor
.smk_known
;
66 * This is the level in a CIPSO header that indicates a
67 * smack label is contained directly in the category set.
68 * It can be reset via smackfs/direct
70 int smack_cipso_direct
= SMACK_CIPSO_DIRECT_DEFAULT
;
73 * Unless a process is running with this label even
74 * having CAP_MAC_OVERRIDE isn't enough to grant
75 * privilege to violate MAC policy. If no label is
76 * designated (the NULL case) capabilities apply to
77 * everyone. It is expected that the hat (^) label
78 * will be used if any label is used.
83 * Certain IP addresses may be designated as single label hosts.
84 * Packets are sent there unlabeled, but only from tasks that
85 * can write to the specified label.
88 LIST_HEAD(smk_netlbladdr_list
);
91 * Rule lists are maintained for each label.
92 * This master list is just for reading /smack/load.
94 struct smack_master_list
{
95 struct list_head list
;
96 struct smack_rule
*smk_rule
;
99 LIST_HEAD(smack_rule_list
);
101 static int smk_cipso_doi_value
= SMACK_CIPSO_DOI_DEFAULT
;
103 const char *smack_cipso_option
= SMACK_CIPSO_OPTION
;
106 * Values for parsing cipso rules
107 * SMK_DIGITLEN: Length of a digit field in a rule.
108 * SMK_CIPSOMIN: Minimum possible cipso rule length.
109 * SMK_CIPSOMAX: Maximum possible cipso rule length.
111 #define SMK_DIGITLEN 4
112 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
113 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
116 * Values for parsing MAC rules
117 * SMK_ACCESS: Maximum possible combination of access permissions
118 * SMK_ACCESSLEN: Maximum length for a rule access field
119 * SMK_LOADLEN: Smack rule length
121 #define SMK_OACCESS "rwxa"
122 #define SMK_ACCESS "rwxat"
123 #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
124 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
125 #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
126 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
129 * smk_netlabel_audit_set - fill a netlbl_audit struct
130 * @nap: structure to fill
132 static void smk_netlabel_audit_set(struct netlbl_audit
*nap
)
134 nap
->loginuid
= audit_get_loginuid(current
);
135 nap
->sessionid
= audit_get_sessionid(current
);
136 nap
->secid
= smack_to_secid(smk_of_current());
140 * Values for parsing single label host rules
142 * "192.168.138.129/32 abcdefghijklmnopqrstuvw"
144 #define SMK_NETLBLADDRMIN 9
145 #define SMK_NETLBLADDRMAX 42
148 * smk_set_access - add a rule to the rule list
149 * @srp: the new rule to add
150 * @rule_list: the list of rules
151 * @rule_lock: the rule list lock
153 * Looks through the current subject/object/access list for
154 * the subject/object pair and replaces the access that was
155 * there. If the pair isn't found add it with the specified
158 * Returns 1 if a rule was found to exist already, 0 if it is new
159 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
160 * during the allocation of the new pair to add.
162 static int smk_set_access(struct smack_rule
*srp
, struct list_head
*rule_list
,
163 struct mutex
*rule_lock
)
165 struct smack_rule
*sp
;
168 mutex_lock(rule_lock
);
171 * Because the object label is less likely to match
172 * than the subject label check it first
174 list_for_each_entry_rcu(sp
, rule_list
, list
) {
175 if (sp
->smk_object
== srp
->smk_object
&&
176 sp
->smk_subject
== srp
->smk_subject
) {
178 sp
->smk_access
= srp
->smk_access
;
183 list_add_rcu(&srp
->list
, rule_list
);
185 mutex_unlock(rule_lock
);
191 * smk_parse_rule - parse Smack rule from load string
192 * @data: string to be parsed whose size is SMK_LOADLEN
194 * @import: if non-zero, import labels
196 static int smk_parse_rule(const char *data
, struct smack_rule
*rule
, int import
)
198 char smack
[SMK_LABELLEN
];
199 struct smack_known
*skp
;
202 rule
->smk_subject
= smk_import(data
, 0);
203 if (rule
->smk_subject
== NULL
)
206 rule
->smk_object
= smk_import(data
+ SMK_LABELLEN
, 0);
207 if (rule
->smk_object
== NULL
)
210 smk_parse_smack(data
, 0, smack
);
211 skp
= smk_find_entry(smack
);
214 rule
->smk_subject
= skp
->smk_known
;
216 smk_parse_smack(data
+ SMK_LABELLEN
, 0, smack
);
217 skp
= smk_find_entry(smack
);
220 rule
->smk_object
= skp
->smk_known
;
223 rule
->smk_access
= 0;
225 switch (data
[SMK_LABELLEN
+ SMK_LABELLEN
]) {
230 rule
->smk_access
|= MAY_READ
;
236 switch (data
[SMK_LABELLEN
+ SMK_LABELLEN
+ 1]) {
241 rule
->smk_access
|= MAY_WRITE
;
247 switch (data
[SMK_LABELLEN
+ SMK_LABELLEN
+ 2]) {
252 rule
->smk_access
|= MAY_EXEC
;
258 switch (data
[SMK_LABELLEN
+ SMK_LABELLEN
+ 3]) {
263 rule
->smk_access
|= MAY_APPEND
;
269 switch (data
[SMK_LABELLEN
+ SMK_LABELLEN
+ 4]) {
274 rule
->smk_access
|= MAY_TRANSMUTE
;
284 * smk_write_load_list - write() for any /smack/load
285 * @file: file pointer, not actually used
286 * @buf: where to get the data from
288 * @ppos: where to start - must be 0
289 * @rule_list: the list of rules to write to
290 * @rule_lock: lock for the rule list
292 * Get one smack access rule from above.
293 * The format is exactly:
294 * char subject[SMK_LABELLEN]
295 * char object[SMK_LABELLEN]
296 * char access[SMK_ACCESSLEN]
298 * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
300 static ssize_t
smk_write_load_list(struct file
*file
, const char __user
*buf
,
301 size_t count
, loff_t
*ppos
,
302 struct list_head
*rule_list
,
303 struct mutex
*rule_lock
)
305 struct smack_master_list
*smlp
;
306 struct smack_known
*skp
;
307 struct smack_rule
*rule
;
314 * Enough data must be present.
319 * Minor hack for backward compatibility
321 if (count
< (SMK_OLOADLEN
) || count
> SMK_LOADLEN
)
324 data
= kzalloc(SMK_LOADLEN
, GFP_KERNEL
);
328 if (copy_from_user(data
, buf
, count
) != 0) {
334 * More on the minor hack for backward compatibility
336 if (count
== (SMK_OLOADLEN
))
337 data
[SMK_OLOADLEN
] = '-';
339 rule
= kzalloc(sizeof(*rule
), GFP_KERNEL
);
345 if (smk_parse_rule(data
, rule
, 1))
348 if (rule_list
== NULL
) {
350 skp
= smk_find_entry(rule
->smk_subject
);
351 rule_list
= &skp
->smk_rules
;
352 rule_lock
= &skp
->smk_rules_lock
;
357 * If this is "load" as opposed to "load-self" and a new rule
358 * it needs to get added for reporting.
359 * smk_set_access returns true if there was already a rule
360 * for the subject/object pair, and false if it was new.
362 if (load
&& !smk_set_access(rule
, rule_list
, rule_lock
)) {
363 smlp
= kzalloc(sizeof(*smlp
), GFP_KERNEL
);
365 smlp
->smk_rule
= rule
;
366 list_add_rcu(&smlp
->list
, &smack_rule_list
);
380 * Core logic for smackfs seq list operations.
383 static void *smk_seq_start(struct seq_file
*s
, loff_t
*pos
,
384 struct list_head
*head
)
386 struct list_head
*list
;
389 * This is 0 the first time through.
394 if (s
->private == NULL
)
398 if (list_empty(list
))
406 static void *smk_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
,
407 struct list_head
*head
)
409 struct list_head
*list
= v
;
411 if (list_is_last(list
, head
)) {
415 s
->private = list
->next
;
419 static void smk_seq_stop(struct seq_file
*s
, void *v
)
425 * Seq_file read operations for /smack/load
428 static void *load_seq_start(struct seq_file
*s
, loff_t
*pos
)
430 return smk_seq_start(s
, pos
, &smack_rule_list
);
433 static void *load_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
435 return smk_seq_next(s
, v
, pos
, &smack_rule_list
);
438 static int load_seq_show(struct seq_file
*s
, void *v
)
440 struct list_head
*list
= v
;
441 struct smack_master_list
*smlp
=
442 list_entry(list
, struct smack_master_list
, list
);
443 struct smack_rule
*srp
= smlp
->smk_rule
;
445 seq_printf(s
, "%s %s", (char *)srp
->smk_subject
,
446 (char *)srp
->smk_object
);
450 if (srp
->smk_access
& MAY_READ
)
452 if (srp
->smk_access
& MAY_WRITE
)
454 if (srp
->smk_access
& MAY_EXEC
)
456 if (srp
->smk_access
& MAY_APPEND
)
458 if (srp
->smk_access
& MAY_TRANSMUTE
)
460 if (srp
->smk_access
== 0)
468 static const struct seq_operations load_seq_ops
= {
469 .start
= load_seq_start
,
470 .next
= load_seq_next
,
471 .show
= load_seq_show
,
472 .stop
= smk_seq_stop
,
476 * smk_open_load - open() for /smack/load
477 * @inode: inode structure representing file
478 * @file: "load" file pointer
480 * For reading, use load_seq_* seq_file reading operations.
482 static int smk_open_load(struct inode
*inode
, struct file
*file
)
484 return seq_open(file
, &load_seq_ops
);
488 * smk_write_load - write() for /smack/load
489 * @file: file pointer, not actually used
490 * @buf: where to get the data from
492 * @ppos: where to start - must be 0
495 static ssize_t
smk_write_load(struct file
*file
, const char __user
*buf
,
496 size_t count
, loff_t
*ppos
)
500 * Must have privilege.
502 * Enough data must be present.
504 if (!capable(CAP_MAC_ADMIN
))
507 return smk_write_load_list(file
, buf
, count
, ppos
, NULL
, NULL
);
510 static const struct file_operations smk_load_ops
= {
511 .open
= smk_open_load
,
514 .write
= smk_write_load
,
515 .release
= seq_release
,
519 * smk_cipso_doi - initialize the CIPSO domain
521 static void smk_cipso_doi(void)
524 struct cipso_v4_doi
*doip
;
525 struct netlbl_audit nai
;
527 smk_netlabel_audit_set(&nai
);
529 rc
= netlbl_cfg_map_del(NULL
, PF_INET
, NULL
, NULL
, &nai
);
531 printk(KERN_WARNING
"%s:%d remove rc = %d\n",
532 __func__
, __LINE__
, rc
);
534 doip
= kmalloc(sizeof(struct cipso_v4_doi
), GFP_KERNEL
);
536 panic("smack: Failed to initialize cipso DOI.\n");
537 doip
->map
.std
= NULL
;
538 doip
->doi
= smk_cipso_doi_value
;
539 doip
->type
= CIPSO_V4_MAP_PASS
;
540 doip
->tags
[0] = CIPSO_V4_TAG_RBITMAP
;
541 for (rc
= 1; rc
< CIPSO_V4_TAG_MAXCNT
; rc
++)
542 doip
->tags
[rc
] = CIPSO_V4_TAG_INVALID
;
544 rc
= netlbl_cfg_cipsov4_add(doip
, &nai
);
546 printk(KERN_WARNING
"%s:%d cipso add rc = %d\n",
547 __func__
, __LINE__
, rc
);
551 rc
= netlbl_cfg_cipsov4_map_add(doip
->doi
, NULL
, NULL
, NULL
, &nai
);
553 printk(KERN_WARNING
"%s:%d map add rc = %d\n",
554 __func__
, __LINE__
, rc
);
561 * smk_unlbl_ambient - initialize the unlabeled domain
562 * @oldambient: previous domain string
564 static void smk_unlbl_ambient(char *oldambient
)
567 struct netlbl_audit nai
;
569 smk_netlabel_audit_set(&nai
);
571 if (oldambient
!= NULL
) {
572 rc
= netlbl_cfg_map_del(oldambient
, PF_INET
, NULL
, NULL
, &nai
);
574 printk(KERN_WARNING
"%s:%d remove rc = %d\n",
575 __func__
, __LINE__
, rc
);
578 rc
= netlbl_cfg_unlbl_map_add(smack_net_ambient
, PF_INET
,
581 printk(KERN_WARNING
"%s:%d add rc = %d\n",
582 __func__
, __LINE__
, rc
);
586 * Seq_file read operations for /smack/cipso
589 static void *cipso_seq_start(struct seq_file
*s
, loff_t
*pos
)
591 return smk_seq_start(s
, pos
, &smack_known_list
);
594 static void *cipso_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
596 return smk_seq_next(s
, v
, pos
, &smack_known_list
);
600 * Print cipso labels in format:
601 * label level[/cat[,cat]]
603 static int cipso_seq_show(struct seq_file
*s
, void *v
)
605 struct list_head
*list
= v
;
606 struct smack_known
*skp
=
607 list_entry(list
, struct smack_known
, list
);
608 struct smack_cipso
*scp
= skp
->smk_cipso
;
618 seq_printf(s
, "%s %3d", (char *)&skp
->smk_known
, scp
->smk_level
);
620 cbp
= scp
->smk_catset
;
621 for (i
= 0; i
< SMK_LABELLEN
; i
++)
622 for (m
= 0x80; m
!= 0; m
>>= 1) {
624 seq_printf(s
, "%c%d", sep
, cat
);
635 static const struct seq_operations cipso_seq_ops
= {
636 .start
= cipso_seq_start
,
637 .next
= cipso_seq_next
,
638 .show
= cipso_seq_show
,
639 .stop
= smk_seq_stop
,
643 * smk_open_cipso - open() for /smack/cipso
644 * @inode: inode structure representing file
645 * @file: "cipso" file pointer
647 * Connect our cipso_seq_* operations with /smack/cipso
650 static int smk_open_cipso(struct inode
*inode
, struct file
*file
)
652 return seq_open(file
, &cipso_seq_ops
);
656 * smk_write_cipso - write() for /smack/cipso
657 * @file: file pointer, not actually used
658 * @buf: where to get the data from
660 * @ppos: where to start
662 * Accepts only one cipso rule per write call.
663 * Returns number of bytes written or error code, as appropriate
665 static ssize_t
smk_write_cipso(struct file
*file
, const char __user
*buf
,
666 size_t count
, loff_t
*ppos
)
668 struct smack_known
*skp
;
669 struct smack_cipso
*scp
= NULL
;
670 char mapcatset
[SMK_LABELLEN
];
674 ssize_t rc
= -EINVAL
;
681 * Must have privilege.
683 * Enough data must be present.
685 if (!capable(CAP_MAC_ADMIN
))
689 if (count
< SMK_CIPSOMIN
|| count
> SMK_CIPSOMAX
)
692 data
= kzalloc(count
+ 1, GFP_KERNEL
);
696 if (copy_from_user(data
, buf
, count
) != 0) {
701 /* labels cannot begin with a '-' */
702 if (data
[0] == '-') {
709 * Only allow one writer at a time. Writes should be
710 * quite rare and small in any case.
712 mutex_lock(&smack_cipso_lock
);
714 skp
= smk_import_entry(rule
, 0);
718 rule
+= SMK_LABELLEN
;
719 ret
= sscanf(rule
, "%d", &maplevel
);
720 if (ret
!= 1 || maplevel
> SMACK_CIPSO_MAXLEVEL
)
723 rule
+= SMK_DIGITLEN
;
724 ret
= sscanf(rule
, "%d", &catlen
);
725 if (ret
!= 1 || catlen
> SMACK_CIPSO_MAXCATNUM
)
728 if (count
!= (SMK_CIPSOMIN
+ catlen
* SMK_DIGITLEN
))
731 memset(mapcatset
, 0, sizeof(mapcatset
));
733 for (i
= 0; i
< catlen
; i
++) {
734 rule
+= SMK_DIGITLEN
;
735 ret
= sscanf(rule
, "%d", &cat
);
736 if (ret
!= 1 || cat
> SMACK_CIPSO_MAXCATVAL
)
739 smack_catset_bit(cat
, mapcatset
);
742 if (skp
->smk_cipso
== NULL
) {
743 scp
= kzalloc(sizeof(struct smack_cipso
), GFP_KERNEL
);
750 spin_lock_bh(&skp
->smk_cipsolock
);
753 scp
= skp
->smk_cipso
;
755 skp
->smk_cipso
= scp
;
757 scp
->smk_level
= maplevel
;
758 memcpy(scp
->smk_catset
, mapcatset
, sizeof(mapcatset
));
760 spin_unlock_bh(&skp
->smk_cipsolock
);
764 mutex_unlock(&smack_cipso_lock
);
770 static const struct file_operations smk_cipso_ops
= {
771 .open
= smk_open_cipso
,
774 .write
= smk_write_cipso
,
775 .release
= seq_release
,
779 * Seq_file read operations for /smack/netlabel
782 static void *netlbladdr_seq_start(struct seq_file
*s
, loff_t
*pos
)
784 return smk_seq_start(s
, pos
, &smk_netlbladdr_list
);
787 static void *netlbladdr_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
789 return smk_seq_next(s
, v
, pos
, &smk_netlbladdr_list
);
791 #define BEBITS (sizeof(__be32) * 8)
794 * Print host/label pairs
796 static int netlbladdr_seq_show(struct seq_file
*s
, void *v
)
798 struct list_head
*list
= v
;
799 struct smk_netlbladdr
*skp
=
800 list_entry(list
, struct smk_netlbladdr
, list
);
801 unsigned char *hp
= (char *) &skp
->smk_host
.sin_addr
.s_addr
;
803 u32 temp_mask
= be32_to_cpu(skp
->smk_mask
.s_addr
);
805 for (maskn
= 0; temp_mask
; temp_mask
<<= 1, maskn
++);
807 seq_printf(s
, "%u.%u.%u.%u/%d %s\n",
808 hp
[0], hp
[1], hp
[2], hp
[3], maskn
, skp
->smk_label
);
813 static const struct seq_operations netlbladdr_seq_ops
= {
814 .start
= netlbladdr_seq_start
,
815 .next
= netlbladdr_seq_next
,
816 .show
= netlbladdr_seq_show
,
817 .stop
= smk_seq_stop
,
821 * smk_open_netlbladdr - open() for /smack/netlabel
822 * @inode: inode structure representing file
823 * @file: "netlabel" file pointer
825 * Connect our netlbladdr_seq_* operations with /smack/netlabel
828 static int smk_open_netlbladdr(struct inode
*inode
, struct file
*file
)
830 return seq_open(file
, &netlbladdr_seq_ops
);
834 * smk_netlbladdr_insert
835 * @new : netlabel to insert
837 * This helper insert netlabel in the smack_netlbladdrs list
838 * sorted by netmask length (longest to smallest)
839 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
842 static void smk_netlbladdr_insert(struct smk_netlbladdr
*new)
844 struct smk_netlbladdr
*m
, *m_next
;
846 if (list_empty(&smk_netlbladdr_list
)) {
847 list_add_rcu(&new->list
, &smk_netlbladdr_list
);
851 m
= list_entry_rcu(smk_netlbladdr_list
.next
,
852 struct smk_netlbladdr
, list
);
854 /* the comparison '>' is a bit hacky, but works */
855 if (new->smk_mask
.s_addr
> m
->smk_mask
.s_addr
) {
856 list_add_rcu(&new->list
, &smk_netlbladdr_list
);
860 list_for_each_entry_rcu(m
, &smk_netlbladdr_list
, list
) {
861 if (list_is_last(&m
->list
, &smk_netlbladdr_list
)) {
862 list_add_rcu(&new->list
, &m
->list
);
865 m_next
= list_entry_rcu(m
->list
.next
,
866 struct smk_netlbladdr
, list
);
867 if (new->smk_mask
.s_addr
> m_next
->smk_mask
.s_addr
) {
868 list_add_rcu(&new->list
, &m
->list
);
876 * smk_write_netlbladdr - write() for /smack/netlabel
877 * @file: file pointer, not actually used
878 * @buf: where to get the data from
880 * @ppos: where to start
882 * Accepts only one netlbladdr per write call.
883 * Returns number of bytes written or error code, as appropriate
885 static ssize_t
smk_write_netlbladdr(struct file
*file
, const char __user
*buf
,
886 size_t count
, loff_t
*ppos
)
888 struct smk_netlbladdr
*skp
;
889 struct sockaddr_in newname
;
890 char smack
[SMK_LABELLEN
];
892 char data
[SMK_NETLBLADDRMAX
+ 1];
893 char *host
= (char *)&newname
.sin_addr
.s_addr
;
895 struct netlbl_audit audit_info
;
899 u32 mask_bits
= (1<<31);
904 * Must have privilege.
906 * Enough data must be present.
907 * "<addr/mask, as a.b.c.d/e><space><label>"
908 * "<addr, as a.b.c.d><space><label>"
910 if (!capable(CAP_MAC_ADMIN
))
914 if (count
< SMK_NETLBLADDRMIN
|| count
> SMK_NETLBLADDRMAX
)
916 if (copy_from_user(data
, buf
, count
) != 0)
921 rc
= sscanf(data
, "%hhd.%hhd.%hhd.%hhd/%d %s",
922 &host
[0], &host
[1], &host
[2], &host
[3], &m
, smack
);
924 rc
= sscanf(data
, "%hhd.%hhd.%hhd.%hhd %s",
925 &host
[0], &host
[1], &host
[2], &host
[3], smack
);
933 /* if smack begins with '-', its an option, don't import it */
934 if (smack
[0] != '-') {
935 sp
= smk_import(smack
, 0);
939 /* check known options */
940 if (strcmp(smack
, smack_cipso_option
) == 0)
941 sp
= (char *)smack_cipso_option
;
946 for (temp_mask
= 0; m
> 0; m
--) {
947 temp_mask
|= mask_bits
;
950 mask
.s_addr
= cpu_to_be32(temp_mask
);
952 newname
.sin_addr
.s_addr
&= mask
.s_addr
;
954 * Only allow one writer at a time. Writes should be
955 * quite rare and small in any case.
957 mutex_lock(&smk_netlbladdr_lock
);
959 nsa
= newname
.sin_addr
.s_addr
;
960 /* try to find if the prefix is already in the list */
962 list_for_each_entry_rcu(skp
, &smk_netlbladdr_list
, list
) {
963 if (skp
->smk_host
.sin_addr
.s_addr
== nsa
&&
964 skp
->smk_mask
.s_addr
== mask
.s_addr
) {
969 smk_netlabel_audit_set(&audit_info
);
972 skp
= kzalloc(sizeof(*skp
), GFP_KERNEL
);
977 skp
->smk_host
.sin_addr
.s_addr
= newname
.sin_addr
.s_addr
;
978 skp
->smk_mask
.s_addr
= mask
.s_addr
;
980 smk_netlbladdr_insert(skp
);
983 /* we delete the unlabeled entry, only if the previous label
984 * wasn't the special CIPSO option */
985 if (skp
->smk_label
!= smack_cipso_option
)
986 rc
= netlbl_cfg_unlbl_static_del(&init_net
, NULL
,
987 &skp
->smk_host
.sin_addr
, &skp
->smk_mask
,
988 PF_INET
, &audit_info
);
995 * Now tell netlabel about the single label nature of
996 * this host so that incoming packets get labeled.
997 * but only if we didn't get the special CIPSO option
999 if (rc
== 0 && sp
!= smack_cipso_option
)
1000 rc
= netlbl_cfg_unlbl_static_add(&init_net
, NULL
,
1001 &skp
->smk_host
.sin_addr
, &skp
->smk_mask
, PF_INET
,
1002 smack_to_secid(skp
->smk_label
), &audit_info
);
1007 mutex_unlock(&smk_netlbladdr_lock
);
1012 static const struct file_operations smk_netlbladdr_ops
= {
1013 .open
= smk_open_netlbladdr
,
1015 .llseek
= seq_lseek
,
1016 .write
= smk_write_netlbladdr
,
1017 .release
= seq_release
,
1021 * smk_read_doi - read() for /smack/doi
1022 * @filp: file pointer, not actually used
1023 * @buf: where to put the result
1024 * @count: maximum to send along
1025 * @ppos: where to start
1027 * Returns number of bytes read or error code, as appropriate
1029 static ssize_t
smk_read_doi(struct file
*filp
, char __user
*buf
,
1030 size_t count
, loff_t
*ppos
)
1038 sprintf(temp
, "%d", smk_cipso_doi_value
);
1039 rc
= simple_read_from_buffer(buf
, count
, ppos
, temp
, strlen(temp
));
1045 * smk_write_doi - write() for /smack/doi
1046 * @file: file pointer, not actually used
1047 * @buf: where to get the data from
1048 * @count: bytes sent
1049 * @ppos: where to start
1051 * Returns number of bytes written or error code, as appropriate
1053 static ssize_t
smk_write_doi(struct file
*file
, const char __user
*buf
,
1054 size_t count
, loff_t
*ppos
)
1059 if (!capable(CAP_MAC_ADMIN
))
1062 if (count
>= sizeof(temp
) || count
== 0)
1065 if (copy_from_user(temp
, buf
, count
) != 0)
1070 if (sscanf(temp
, "%d", &i
) != 1)
1073 smk_cipso_doi_value
= i
;
1080 static const struct file_operations smk_doi_ops
= {
1081 .read
= smk_read_doi
,
1082 .write
= smk_write_doi
,
1083 .llseek
= default_llseek
,
1087 * smk_read_direct - read() for /smack/direct
1088 * @filp: file pointer, not actually used
1089 * @buf: where to put the result
1090 * @count: maximum to send along
1091 * @ppos: where to start
1093 * Returns number of bytes read or error code, as appropriate
1095 static ssize_t
smk_read_direct(struct file
*filp
, char __user
*buf
,
1096 size_t count
, loff_t
*ppos
)
1104 sprintf(temp
, "%d", smack_cipso_direct
);
1105 rc
= simple_read_from_buffer(buf
, count
, ppos
, temp
, strlen(temp
));
1111 * smk_write_direct - write() for /smack/direct
1112 * @file: file pointer, not actually used
1113 * @buf: where to get the data from
1114 * @count: bytes sent
1115 * @ppos: where to start
1117 * Returns number of bytes written or error code, as appropriate
1119 static ssize_t
smk_write_direct(struct file
*file
, const char __user
*buf
,
1120 size_t count
, loff_t
*ppos
)
1125 if (!capable(CAP_MAC_ADMIN
))
1128 if (count
>= sizeof(temp
) || count
== 0)
1131 if (copy_from_user(temp
, buf
, count
) != 0)
1136 if (sscanf(temp
, "%d", &i
) != 1)
1139 smack_cipso_direct
= i
;
1144 static const struct file_operations smk_direct_ops
= {
1145 .read
= smk_read_direct
,
1146 .write
= smk_write_direct
,
1147 .llseek
= default_llseek
,
1151 * smk_read_ambient - read() for /smack/ambient
1152 * @filp: file pointer, not actually used
1153 * @buf: where to put the result
1154 * @cn: maximum to send along
1155 * @ppos: where to start
1157 * Returns number of bytes read or error code, as appropriate
1159 static ssize_t
smk_read_ambient(struct file
*filp
, char __user
*buf
,
1160 size_t cn
, loff_t
*ppos
)
1168 * Being careful to avoid a problem in the case where
1169 * smack_net_ambient gets changed in midstream.
1171 mutex_lock(&smack_ambient_lock
);
1173 asize
= strlen(smack_net_ambient
) + 1;
1176 rc
= simple_read_from_buffer(buf
, cn
, ppos
,
1177 smack_net_ambient
, asize
);
1181 mutex_unlock(&smack_ambient_lock
);
1187 * smk_write_ambient - write() for /smack/ambient
1188 * @file: file pointer, not actually used
1189 * @buf: where to get the data from
1190 * @count: bytes sent
1191 * @ppos: where to start
1193 * Returns number of bytes written or error code, as appropriate
1195 static ssize_t
smk_write_ambient(struct file
*file
, const char __user
*buf
,
1196 size_t count
, loff_t
*ppos
)
1198 char in
[SMK_LABELLEN
];
1202 if (!capable(CAP_MAC_ADMIN
))
1205 if (count
>= SMK_LABELLEN
)
1208 if (copy_from_user(in
, buf
, count
) != 0)
1211 smack
= smk_import(in
, count
);
1215 mutex_lock(&smack_ambient_lock
);
1217 oldambient
= smack_net_ambient
;
1218 smack_net_ambient
= smack
;
1219 smk_unlbl_ambient(oldambient
);
1221 mutex_unlock(&smack_ambient_lock
);
1226 static const struct file_operations smk_ambient_ops
= {
1227 .read
= smk_read_ambient
,
1228 .write
= smk_write_ambient
,
1229 .llseek
= default_llseek
,
1233 * smk_read_onlycap - read() for /smack/onlycap
1234 * @filp: file pointer, not actually used
1235 * @buf: where to put the result
1236 * @cn: maximum to send along
1237 * @ppos: where to start
1239 * Returns number of bytes read or error code, as appropriate
1241 static ssize_t
smk_read_onlycap(struct file
*filp
, char __user
*buf
,
1242 size_t cn
, loff_t
*ppos
)
1245 ssize_t rc
= -EINVAL
;
1251 if (smack_onlycap
!= NULL
)
1252 smack
= smack_onlycap
;
1254 asize
= strlen(smack
) + 1;
1257 rc
= simple_read_from_buffer(buf
, cn
, ppos
, smack
, asize
);
1263 * smk_write_onlycap - write() for /smack/onlycap
1264 * @file: file pointer, not actually used
1265 * @buf: where to get the data from
1266 * @count: bytes sent
1267 * @ppos: where to start
1269 * Returns number of bytes written or error code, as appropriate
1271 static ssize_t
smk_write_onlycap(struct file
*file
, const char __user
*buf
,
1272 size_t count
, loff_t
*ppos
)
1274 char in
[SMK_LABELLEN
];
1275 char *sp
= smk_of_task(current
->cred
->security
);
1277 if (!capable(CAP_MAC_ADMIN
))
1281 * This can be done using smk_access() but is done
1282 * explicitly for clarity. The smk_access() implementation
1283 * would use smk_access(smack_onlycap, MAY_WRITE)
1285 if (smack_onlycap
!= NULL
&& smack_onlycap
!= sp
)
1288 if (count
>= SMK_LABELLEN
)
1291 if (copy_from_user(in
, buf
, count
) != 0)
1295 * Should the null string be passed in unset the onlycap value.
1296 * This seems like something to be careful with as usually
1297 * smk_import only expects to return NULL for errors. It
1298 * is usually the case that a nullstring or "\n" would be
1299 * bad to pass to smk_import but in fact this is useful here.
1301 smack_onlycap
= smk_import(in
, count
);
1306 static const struct file_operations smk_onlycap_ops
= {
1307 .read
= smk_read_onlycap
,
1308 .write
= smk_write_onlycap
,
1309 .llseek
= default_llseek
,
1313 * smk_read_logging - read() for /smack/logging
1314 * @filp: file pointer, not actually used
1315 * @buf: where to put the result
1316 * @cn: maximum to send along
1317 * @ppos: where to start
1319 * Returns number of bytes read or error code, as appropriate
1321 static ssize_t
smk_read_logging(struct file
*filp
, char __user
*buf
,
1322 size_t count
, loff_t
*ppos
)
1330 sprintf(temp
, "%d\n", log_policy
);
1331 rc
= simple_read_from_buffer(buf
, count
, ppos
, temp
, strlen(temp
));
1336 * smk_write_logging - write() for /smack/logging
1337 * @file: file pointer, not actually used
1338 * @buf: where to get the data from
1339 * @count: bytes sent
1340 * @ppos: where to start
1342 * Returns number of bytes written or error code, as appropriate
1344 static ssize_t
smk_write_logging(struct file
*file
, const char __user
*buf
,
1345 size_t count
, loff_t
*ppos
)
1350 if (!capable(CAP_MAC_ADMIN
))
1353 if (count
>= sizeof(temp
) || count
== 0)
1356 if (copy_from_user(temp
, buf
, count
) != 0)
1361 if (sscanf(temp
, "%d", &i
) != 1)
1371 static const struct file_operations smk_logging_ops
= {
1372 .read
= smk_read_logging
,
1373 .write
= smk_write_logging
,
1374 .llseek
= default_llseek
,
1378 * Seq_file read operations for /smack/load-self
1381 static void *load_self_seq_start(struct seq_file
*s
, loff_t
*pos
)
1383 struct task_smack
*tsp
= current_security();
1385 return smk_seq_start(s
, pos
, &tsp
->smk_rules
);
1388 static void *load_self_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
1390 struct task_smack
*tsp
= current_security();
1392 return smk_seq_next(s
, v
, pos
, &tsp
->smk_rules
);
1395 static int load_self_seq_show(struct seq_file
*s
, void *v
)
1397 struct list_head
*list
= v
;
1398 struct smack_rule
*srp
=
1399 list_entry(list
, struct smack_rule
, list
);
1401 seq_printf(s
, "%s %s", (char *)srp
->smk_subject
,
1402 (char *)srp
->smk_object
);
1406 if (srp
->smk_access
& MAY_READ
)
1408 if (srp
->smk_access
& MAY_WRITE
)
1410 if (srp
->smk_access
& MAY_EXEC
)
1412 if (srp
->smk_access
& MAY_APPEND
)
1414 if (srp
->smk_access
& MAY_TRANSMUTE
)
1416 if (srp
->smk_access
== 0)
1424 static const struct seq_operations load_self_seq_ops
= {
1425 .start
= load_self_seq_start
,
1426 .next
= load_self_seq_next
,
1427 .show
= load_self_seq_show
,
1428 .stop
= smk_seq_stop
,
1433 * smk_open_load_self - open() for /smack/load-self
1434 * @inode: inode structure representing file
1435 * @file: "load" file pointer
1437 * For reading, use load_seq_* seq_file reading operations.
1439 static int smk_open_load_self(struct inode
*inode
, struct file
*file
)
1441 return seq_open(file
, &load_self_seq_ops
);
1445 * smk_write_load_self - write() for /smack/load-self
1446 * @file: file pointer, not actually used
1447 * @buf: where to get the data from
1448 * @count: bytes sent
1449 * @ppos: where to start - must be 0
1452 static ssize_t
smk_write_load_self(struct file
*file
, const char __user
*buf
,
1453 size_t count
, loff_t
*ppos
)
1455 struct task_smack
*tsp
= current_security();
1457 return smk_write_load_list(file
, buf
, count
, ppos
, &tsp
->smk_rules
,
1458 &tsp
->smk_rules_lock
);
1461 static const struct file_operations smk_load_self_ops
= {
1462 .open
= smk_open_load_self
,
1464 .llseek
= seq_lseek
,
1465 .write
= smk_write_load_self
,
1466 .release
= seq_release
,
1470 * smk_write_access - handle access check transaction
1471 * @file: file pointer
1472 * @buf: data from user space
1473 * @count: bytes sent
1474 * @ppos: where to start - must be 0
1476 static ssize_t
smk_write_access(struct file
*file
, const char __user
*buf
,
1477 size_t count
, loff_t
*ppos
)
1479 struct smack_rule rule
;
1483 data
= simple_transaction_get(file
, buf
, count
);
1485 return PTR_ERR(data
);
1487 if (count
< SMK_LOADLEN
|| smk_parse_rule(data
, &rule
, 0))
1490 res
= smk_access(rule
.smk_subject
, rule
.smk_object
, rule
.smk_access
,
1492 data
[0] = res
== 0 ? '1' : '0';
1495 simple_transaction_set(file
, 2);
1499 static const struct file_operations smk_access_ops
= {
1500 .write
= smk_write_access
,
1501 .read
= simple_transaction_read
,
1502 .release
= simple_transaction_release
,
1503 .llseek
= generic_file_llseek
,
1507 * smk_fill_super - fill the /smackfs superblock
1508 * @sb: the empty superblock
1512 * Fill in the well known entries for /smack
1514 * Returns 0 on success, an error code on failure
1516 static int smk_fill_super(struct super_block
*sb
, void *data
, int silent
)
1519 struct inode
*root_inode
;
1521 static struct tree_descr smack_files
[] = {
1523 "load", &smk_load_ops
, S_IRUGO
|S_IWUSR
},
1525 "cipso", &smk_cipso_ops
, S_IRUGO
|S_IWUSR
},
1527 "doi", &smk_doi_ops
, S_IRUGO
|S_IWUSR
},
1529 "direct", &smk_direct_ops
, S_IRUGO
|S_IWUSR
},
1531 "ambient", &smk_ambient_ops
, S_IRUGO
|S_IWUSR
},
1532 [SMK_NETLBLADDR
] = {
1533 "netlabel", &smk_netlbladdr_ops
, S_IRUGO
|S_IWUSR
},
1535 "onlycap", &smk_onlycap_ops
, S_IRUGO
|S_IWUSR
},
1537 "logging", &smk_logging_ops
, S_IRUGO
|S_IWUSR
},
1539 "load-self", &smk_load_self_ops
, S_IRUGO
|S_IWUGO
},
1541 "access", &smk_access_ops
, S_IRUGO
|S_IWUGO
},
1546 rc
= simple_fill_super(sb
, SMACK_MAGIC
, smack_files
);
1548 printk(KERN_ERR
"%s failed %d while creating inodes\n",
1553 root_inode
= sb
->s_root
->d_inode
;
1554 root_inode
->i_security
= new_inode_smack(smack_known_floor
.smk_known
);
1560 * smk_mount - get the smackfs superblock
1561 * @fs_type: passed along without comment
1562 * @flags: passed along without comment
1563 * @dev_name: passed along without comment
1564 * @data: passed along without comment
1566 * Just passes everything along.
1568 * Returns what the lower level code does.
1570 static struct dentry
*smk_mount(struct file_system_type
*fs_type
,
1571 int flags
, const char *dev_name
, void *data
)
1573 return mount_single(fs_type
, flags
, data
, smk_fill_super
);
1576 static struct file_system_type smk_fs_type
= {
1579 .kill_sb
= kill_litter_super
,
1582 static struct vfsmount
*smackfs_mount
;
1585 * init_smk_fs - get the smackfs superblock
1587 * register the smackfs
1589 * Do not register smackfs if Smack wasn't enabled
1590 * on boot. We can not put this method normally under the
1591 * smack_init() code path since the security subsystem get
1592 * initialized before the vfs caches.
1594 * Returns true if we were not chosen on boot or if
1595 * we were chosen and filesystem registration succeeded.
1597 static int __init
init_smk_fs(void)
1601 if (!security_module_enable(&smack_ops
))
1604 err
= register_filesystem(&smk_fs_type
);
1606 smackfs_mount
= kern_mount(&smk_fs_type
);
1607 if (IS_ERR(smackfs_mount
)) {
1608 printk(KERN_ERR
"smackfs: could not mount!\n");
1609 err
= PTR_ERR(smackfs_mount
);
1610 smackfs_mount
= NULL
;
1615 smk_unlbl_ambient(NULL
);
1617 mutex_init(&smack_known_floor
.smk_rules_lock
);
1618 mutex_init(&smack_known_hat
.smk_rules_lock
);
1619 mutex_init(&smack_known_huh
.smk_rules_lock
);
1620 mutex_init(&smack_known_invalid
.smk_rules_lock
);
1621 mutex_init(&smack_known_star
.smk_rules_lock
);
1622 mutex_init(&smack_known_web
.smk_rules_lock
);
1624 INIT_LIST_HEAD(&smack_known_floor
.smk_rules
);
1625 INIT_LIST_HEAD(&smack_known_hat
.smk_rules
);
1626 INIT_LIST_HEAD(&smack_known_huh
.smk_rules
);
1627 INIT_LIST_HEAD(&smack_known_invalid
.smk_rules
);
1628 INIT_LIST_HEAD(&smack_known_star
.smk_rules
);
1629 INIT_LIST_HEAD(&smack_known_web
.smk_rules
);
1634 __initcall(init_smk_fs
);