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 <net/netlabel.h>
24 #include <net/cipso_ipv4.h>
25 #include <linux/seq_file.h>
26 #include <linux/ctype.h>
27 #include <linux/audit.h>
31 * smackfs pseudo filesystem.
36 SMK_LOAD
= 3, /* load policy */
37 SMK_CIPSO
= 4, /* load label -> CIPSO mapping */
38 SMK_DOI
= 5, /* CIPSO DOI */
39 SMK_DIRECT
= 6, /* CIPSO level indicating direct label */
40 SMK_AMBIENT
= 7, /* internet ambient label */
41 SMK_NLTYPE
= 8, /* label scheme to use by default */
47 static DEFINE_MUTEX(smack_list_lock
);
48 static DEFINE_MUTEX(smack_cipso_lock
);
49 static DEFINE_MUTEX(smack_ambient_lock
);
52 * This is the "ambient" label for network traffic.
53 * If it isn't somehow marked, use this.
54 * It can be reset via smackfs/ambient
56 char *smack_net_ambient
= smack_known_floor
.smk_known
;
59 * This is the default packet marking scheme for network traffic.
60 * It can be reset via smackfs/nltype
62 int smack_net_nltype
= NETLBL_NLTYPE_CIPSOV4
;
65 * This is the level in a CIPSO header that indicates a
66 * smack label is contained directly in the category set.
67 * It can be reset via smackfs/direct
69 int smack_cipso_direct
= SMACK_CIPSO_DIRECT_DEFAULT
;
71 static int smk_cipso_doi_value
= SMACK_CIPSO_DOI_DEFAULT
;
72 struct smk_list_entry
*smack_list
;
74 #define SEQ_READ_FINISHED 1
77 * Values for parsing cipso rules
78 * SMK_DIGITLEN: Length of a digit field in a rule.
79 * SMK_CIPSOMIN: Minimum possible cipso rule length.
80 * SMK_CIPSOMAX: Maximum possible cipso rule length.
82 #define SMK_DIGITLEN 4
83 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
84 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
87 * Values for parsing MAC rules
88 * SMK_ACCESS: Maximum possible combination of access permissions
89 * SMK_ACCESSLEN: Maximum length for a rule access field
90 * SMK_LOADLEN: Smack rule length
92 #define SMK_ACCESS "rwxa"
93 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
94 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
98 * Seq_file read operations for /smack/load
101 static void *load_seq_start(struct seq_file
*s
, loff_t
*pos
)
103 if (*pos
== SEQ_READ_FINISHED
)
109 static void *load_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
111 struct smk_list_entry
*skp
= ((struct smk_list_entry
*) v
)->smk_next
;
114 *pos
= SEQ_READ_FINISHED
;
119 static int load_seq_show(struct seq_file
*s
, void *v
)
121 struct smk_list_entry
*slp
= (struct smk_list_entry
*) v
;
122 struct smack_rule
*srp
= &slp
->smk_rule
;
124 seq_printf(s
, "%s %s", (char *)srp
->smk_subject
,
125 (char *)srp
->smk_object
);
129 if (srp
->smk_access
& MAY_READ
)
131 if (srp
->smk_access
& MAY_WRITE
)
133 if (srp
->smk_access
& MAY_EXEC
)
135 if (srp
->smk_access
& MAY_APPEND
)
137 if (srp
->smk_access
== 0)
145 static void load_seq_stop(struct seq_file
*s
, void *v
)
150 static struct seq_operations load_seq_ops
= {
151 .start
= load_seq_start
,
152 .next
= load_seq_next
,
153 .show
= load_seq_show
,
154 .stop
= load_seq_stop
,
158 * smk_open_load - open() for /smack/load
159 * @inode: inode structure representing file
160 * @file: "load" file pointer
162 * For reading, use load_seq_* seq_file reading operations.
164 static int smk_open_load(struct inode
*inode
, struct file
*file
)
166 return seq_open(file
, &load_seq_ops
);
170 * smk_set_access - add a rule to the rule list
171 * @srp: the new rule to add
173 * Looks through the current subject/object/access list for
174 * the subject/object pair and replaces the access that was
175 * there. If the pair isn't found add it with the specified
178 static void smk_set_access(struct smack_rule
*srp
)
180 struct smk_list_entry
*sp
;
181 struct smk_list_entry
*newp
;
183 mutex_lock(&smack_list_lock
);
185 for (sp
= smack_list
; sp
!= NULL
; sp
= sp
->smk_next
)
186 if (sp
->smk_rule
.smk_subject
== srp
->smk_subject
&&
187 sp
->smk_rule
.smk_object
== srp
->smk_object
) {
188 sp
->smk_rule
.smk_access
= srp
->smk_access
;
193 newp
= kzalloc(sizeof(struct smk_list_entry
), GFP_KERNEL
);
194 newp
->smk_rule
= *srp
;
195 newp
->smk_next
= smack_list
;
199 mutex_unlock(&smack_list_lock
);
205 * smk_write_load - write() for /smack/load
206 * @filp: file pointer, not actually used
207 * @buf: where to get the data from
209 * @ppos: where to start - must be 0
211 * Get one smack access rule from above.
212 * The format is exactly:
213 * char subject[SMK_LABELLEN]
214 * char object[SMK_LABELLEN]
215 * char access[SMK_ACCESSLEN]
217 * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
219 static ssize_t
smk_write_load(struct file
*file
, const char __user
*buf
,
220 size_t count
, loff_t
*ppos
)
222 struct smack_rule rule
;
227 * Must have privilege.
229 * Enough data must be present.
231 if (!capable(CAP_MAC_ADMIN
))
235 if (count
!= SMK_LOADLEN
)
238 data
= kzalloc(count
, GFP_KERNEL
);
242 if (copy_from_user(data
, buf
, count
) != 0) {
247 rule
.smk_subject
= smk_import(data
, 0);
248 if (rule
.smk_subject
== NULL
)
251 rule
.smk_object
= smk_import(data
+ SMK_LABELLEN
, 0);
252 if (rule
.smk_object
== NULL
)
257 switch (data
[SMK_LABELLEN
+ SMK_LABELLEN
]) {
262 rule
.smk_access
|= MAY_READ
;
268 switch (data
[SMK_LABELLEN
+ SMK_LABELLEN
+ 1]) {
273 rule
.smk_access
|= MAY_WRITE
;
279 switch (data
[SMK_LABELLEN
+ SMK_LABELLEN
+ 2]) {
284 rule
.smk_access
|= MAY_EXEC
;
290 switch (data
[SMK_LABELLEN
+ SMK_LABELLEN
+ 3]) {
295 rule
.smk_access
|= MAY_READ
;
301 smk_set_access(&rule
);
309 static const struct file_operations smk_load_ops
= {
310 .open
= smk_open_load
,
313 .write
= smk_write_load
,
314 .release
= seq_release
,
318 * smk_cipso_doi - initialize the CIPSO domain
320 void smk_cipso_doi(void)
323 struct cipso_v4_doi
*doip
;
324 struct netlbl_audit audit_info
;
326 audit_info
.loginuid
= audit_get_loginuid(current
);
327 audit_info
.secid
= smack_to_secid(current
->security
);
329 rc
= netlbl_cfg_map_del(NULL
, &audit_info
);
331 printk(KERN_WARNING
"%s:%d remove rc = %d\n",
332 __func__
, __LINE__
, rc
);
334 doip
= kmalloc(sizeof(struct cipso_v4_doi
), GFP_KERNEL
);
336 panic("smack: Failed to initialize cipso DOI.\n");
337 doip
->map
.std
= NULL
;
338 doip
->doi
= smk_cipso_doi_value
;
339 doip
->type
= CIPSO_V4_MAP_PASS
;
340 doip
->tags
[0] = CIPSO_V4_TAG_RBITMAP
;
341 for (rc
= 1; rc
< CIPSO_V4_TAG_MAXCNT
; rc
++)
342 doip
->tags
[rc
] = CIPSO_V4_TAG_INVALID
;
344 rc
= netlbl_cfg_cipsov4_add_map(doip
, NULL
, &audit_info
);
346 printk(KERN_WARNING
"%s:%d add rc = %d\n",
347 __func__
, __LINE__
, rc
);
351 * smk_unlbl_ambient - initialize the unlabeled domain
353 void smk_unlbl_ambient(char *oldambient
)
356 struct netlbl_audit audit_info
;
358 audit_info
.loginuid
= audit_get_loginuid(current
);
359 audit_info
.secid
= smack_to_secid(current
->security
);
361 if (oldambient
!= NULL
) {
362 rc
= netlbl_cfg_map_del(oldambient
, &audit_info
);
364 printk(KERN_WARNING
"%s:%d remove rc = %d\n",
365 __func__
, __LINE__
, rc
);
368 rc
= netlbl_cfg_unlbl_add_map(smack_net_ambient
, &audit_info
);
370 printk(KERN_WARNING
"%s:%d add rc = %d\n",
371 __func__
, __LINE__
, rc
);
375 * Seq_file read operations for /smack/cipso
378 static void *cipso_seq_start(struct seq_file
*s
, loff_t
*pos
)
380 if (*pos
== SEQ_READ_FINISHED
)
386 static void *cipso_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
388 struct smack_known
*skp
= ((struct smack_known
*) v
)->smk_next
;
391 * Omit labels with no associated cipso value
393 while (skp
!= NULL
&& !skp
->smk_cipso
)
397 *pos
= SEQ_READ_FINISHED
;
403 * Print cipso labels in format:
404 * label level[/cat[,cat]]
406 static int cipso_seq_show(struct seq_file
*s
, void *v
)
408 struct smack_known
*skp
= (struct smack_known
*) v
;
409 struct smack_cipso
*scp
= skp
->smk_cipso
;
419 seq_printf(s
, "%s %3d", (char *)&skp
->smk_known
, scp
->smk_level
);
421 cbp
= scp
->smk_catset
;
422 for (i
= 0; i
< SMK_LABELLEN
; i
++)
423 for (m
= 0x80; m
!= 0; m
>>= 1) {
425 seq_printf(s
, "%c%d", sep
, cat
);
436 static void cipso_seq_stop(struct seq_file
*s
, void *v
)
441 static struct seq_operations cipso_seq_ops
= {
442 .start
= cipso_seq_start
,
443 .stop
= cipso_seq_stop
,
444 .next
= cipso_seq_next
,
445 .show
= cipso_seq_show
,
449 * smk_open_cipso - open() for /smack/cipso
450 * @inode: inode structure representing file
451 * @file: "cipso" file pointer
453 * Connect our cipso_seq_* operations with /smack/cipso
456 static int smk_open_cipso(struct inode
*inode
, struct file
*file
)
458 return seq_open(file
, &cipso_seq_ops
);
462 * smk_write_cipso - write() for /smack/cipso
463 * @filp: file pointer, not actually used
464 * @buf: where to get the data from
466 * @ppos: where to start
468 * Accepts only one cipso rule per write call.
469 * Returns number of bytes written or error code, as appropriate
471 static ssize_t
smk_write_cipso(struct file
*file
, const char __user
*buf
,
472 size_t count
, loff_t
*ppos
)
474 struct smack_known
*skp
;
475 struct smack_cipso
*scp
= NULL
;
476 char mapcatset
[SMK_LABELLEN
];
480 ssize_t rc
= -EINVAL
;
487 * Must have privilege.
489 * Enough data must be present.
491 if (!capable(CAP_MAC_ADMIN
))
495 if (count
< SMK_CIPSOMIN
|| count
> SMK_CIPSOMAX
)
498 data
= kzalloc(count
+ 1, GFP_KERNEL
);
502 if (copy_from_user(data
, buf
, count
) != 0) {
510 * Only allow one writer at a time. Writes should be
511 * quite rare and small in any case.
513 mutex_lock(&smack_cipso_lock
);
515 skp
= smk_import_entry(rule
, 0);
519 rule
+= SMK_LABELLEN
;;
520 ret
= sscanf(rule
, "%d", &maplevel
);
521 if (ret
!= 1 || maplevel
> SMACK_CIPSO_MAXLEVEL
)
524 rule
+= SMK_DIGITLEN
;
525 ret
= sscanf(rule
, "%d", &catlen
);
526 if (ret
!= 1 || catlen
> SMACK_CIPSO_MAXCATNUM
)
529 if (count
!= (SMK_CIPSOMIN
+ catlen
* SMK_DIGITLEN
))
532 memset(mapcatset
, 0, sizeof(mapcatset
));
534 for (i
= 0; i
< catlen
; i
++) {
535 rule
+= SMK_DIGITLEN
;
536 ret
= sscanf(rule
, "%d", &cat
);
537 if (ret
!= 1 || cat
> SMACK_CIPSO_MAXCATVAL
)
540 smack_catset_bit(cat
, mapcatset
);
543 if (skp
->smk_cipso
== NULL
) {
544 scp
= kzalloc(sizeof(struct smack_cipso
), GFP_KERNEL
);
551 spin_lock_bh(&skp
->smk_cipsolock
);
554 scp
= skp
->smk_cipso
;
556 skp
->smk_cipso
= scp
;
558 scp
->smk_level
= maplevel
;
559 memcpy(scp
->smk_catset
, mapcatset
, sizeof(mapcatset
));
561 spin_unlock_bh(&skp
->smk_cipsolock
);
565 mutex_unlock(&smack_cipso_lock
);
571 static const struct file_operations smk_cipso_ops
= {
572 .open
= smk_open_cipso
,
575 .write
= smk_write_cipso
,
576 .release
= seq_release
,
580 * smk_read_doi - read() for /smack/doi
581 * @filp: file pointer, not actually used
582 * @buf: where to put the result
583 * @count: maximum to send along
584 * @ppos: where to start
586 * Returns number of bytes read or error code, as appropriate
588 static ssize_t
smk_read_doi(struct file
*filp
, char __user
*buf
,
589 size_t count
, loff_t
*ppos
)
597 sprintf(temp
, "%d", smk_cipso_doi_value
);
598 rc
= simple_read_from_buffer(buf
, count
, ppos
, temp
, strlen(temp
));
604 * smk_write_doi - write() for /smack/doi
605 * @filp: file pointer, not actually used
606 * @buf: where to get the data from
608 * @ppos: where to start
610 * Returns number of bytes written or error code, as appropriate
612 static ssize_t
smk_write_doi(struct file
*file
, const char __user
*buf
,
613 size_t count
, loff_t
*ppos
)
618 if (!capable(CAP_MAC_ADMIN
))
621 if (count
>= sizeof(temp
) || count
== 0)
624 if (copy_from_user(temp
, buf
, count
) != 0)
629 if (sscanf(temp
, "%d", &i
) != 1)
632 smk_cipso_doi_value
= i
;
639 static const struct file_operations smk_doi_ops
= {
640 .read
= smk_read_doi
,
641 .write
= smk_write_doi
,
645 * smk_read_direct - read() for /smack/direct
646 * @filp: file pointer, not actually used
647 * @buf: where to put the result
648 * @count: maximum to send along
649 * @ppos: where to start
651 * Returns number of bytes read or error code, as appropriate
653 static ssize_t
smk_read_direct(struct file
*filp
, char __user
*buf
,
654 size_t count
, loff_t
*ppos
)
662 sprintf(temp
, "%d", smack_cipso_direct
);
663 rc
= simple_read_from_buffer(buf
, count
, ppos
, temp
, strlen(temp
));
669 * smk_write_direct - write() for /smack/direct
670 * @filp: file pointer, not actually used
671 * @buf: where to get the data from
673 * @ppos: where to start
675 * Returns number of bytes written or error code, as appropriate
677 static ssize_t
smk_write_direct(struct file
*file
, const char __user
*buf
,
678 size_t count
, loff_t
*ppos
)
683 if (!capable(CAP_MAC_ADMIN
))
686 if (count
>= sizeof(temp
) || count
== 0)
689 if (copy_from_user(temp
, buf
, count
) != 0)
694 if (sscanf(temp
, "%d", &i
) != 1)
697 smack_cipso_direct
= i
;
702 static const struct file_operations smk_direct_ops
= {
703 .read
= smk_read_direct
,
704 .write
= smk_write_direct
,
708 * smk_read_ambient - read() for /smack/ambient
709 * @filp: file pointer, not actually used
710 * @buf: where to put the result
711 * @cn: maximum to send along
712 * @ppos: where to start
714 * Returns number of bytes read or error code, as appropriate
716 static ssize_t
smk_read_ambient(struct file
*filp
, char __user
*buf
,
717 size_t cn
, loff_t
*ppos
)
725 * Being careful to avoid a problem in the case where
726 * smack_net_ambient gets changed in midstream.
728 mutex_lock(&smack_ambient_lock
);
730 asize
= strlen(smack_net_ambient
) + 1;
733 rc
= simple_read_from_buffer(buf
, cn
, ppos
,
734 smack_net_ambient
, asize
);
738 mutex_unlock(&smack_ambient_lock
);
744 * smk_write_ambient - write() for /smack/ambient
745 * @filp: file pointer, not actually used
746 * @buf: where to get the data from
748 * @ppos: where to start
750 * Returns number of bytes written or error code, as appropriate
752 static ssize_t
smk_write_ambient(struct file
*file
, const char __user
*buf
,
753 size_t count
, loff_t
*ppos
)
755 char in
[SMK_LABELLEN
];
759 if (!capable(CAP_MAC_ADMIN
))
762 if (count
>= SMK_LABELLEN
)
765 if (copy_from_user(in
, buf
, count
) != 0)
768 smack
= smk_import(in
, count
);
772 mutex_lock(&smack_ambient_lock
);
774 oldambient
= smack_net_ambient
;
775 smack_net_ambient
= smack
;
776 smk_unlbl_ambient(oldambient
);
778 mutex_unlock(&smack_ambient_lock
);
783 static const struct file_operations smk_ambient_ops
= {
784 .read
= smk_read_ambient
,
785 .write
= smk_write_ambient
,
788 struct option_names
{
794 static struct option_names netlbl_choices
[] = {
795 { NETLBL_NLTYPE_RIPSO
,
796 NETLBL_NLTYPE_RIPSO_NAME
, "ripso" },
797 { NETLBL_NLTYPE_CIPSOV4
,
798 NETLBL_NLTYPE_CIPSOV4_NAME
, "cipsov4" },
799 { NETLBL_NLTYPE_CIPSOV4
,
800 NETLBL_NLTYPE_CIPSOV4_NAME
, "cipso" },
801 { NETLBL_NLTYPE_CIPSOV6
,
802 NETLBL_NLTYPE_CIPSOV6_NAME
, "cipsov6" },
803 { NETLBL_NLTYPE_UNLABELED
,
804 NETLBL_NLTYPE_UNLABELED_NAME
, "unlabeled" },
808 * smk_read_nltype - read() for /smack/nltype
809 * @filp: file pointer, not actually used
810 * @buf: where to put the result
811 * @count: maximum to send along
812 * @ppos: where to start
814 * Returns number of bytes read or error code, as appropriate
816 static ssize_t
smk_read_nltype(struct file
*filp
, char __user
*buf
,
817 size_t count
, loff_t
*ppos
)
823 if (count
< SMK_LABELLEN
)
829 sprintf(bound
, "unknown");
831 for (i
= 0; i
< ARRAY_SIZE(netlbl_choices
); i
++)
832 if (smack_net_nltype
== netlbl_choices
[i
].o_number
) {
833 sprintf(bound
, "%s", netlbl_choices
[i
].o_name
);
837 rc
= simple_read_from_buffer(buf
, count
, ppos
, bound
, strlen(bound
));
843 * smk_write_nltype - write() for /smack/nltype
844 * @filp: file pointer, not actually used
845 * @buf: where to get the data from
847 * @ppos: where to start
849 * Returns number of bytes written or error code, as appropriate
851 static ssize_t
smk_write_nltype(struct file
*file
, const char __user
*buf
,
852 size_t count
, loff_t
*ppos
)
858 if (!capable(CAP_MAC_ADMIN
))
864 if (copy_from_user(bound
, buf
, count
) != 0)
868 cp
= strchr(bound
, ' ');
871 cp
= strchr(bound
, '\n');
875 for (i
= 0; i
< ARRAY_SIZE(netlbl_choices
); i
++)
876 if (strcmp(bound
, netlbl_choices
[i
].o_name
) == 0 ||
877 strcmp(bound
, netlbl_choices
[i
].o_alias
) == 0) {
878 smack_net_nltype
= netlbl_choices
[i
].o_number
;
882 * Not a valid choice.
887 static const struct file_operations smk_nltype_ops
= {
888 .read
= smk_read_nltype
,
889 .write
= smk_write_nltype
,
893 * smk_fill_super - fill the /smackfs superblock
894 * @sb: the empty superblock
898 * Fill in the well known entries for /smack
900 * Returns 0 on success, an error code on failure
902 static int smk_fill_super(struct super_block
*sb
, void *data
, int silent
)
905 struct inode
*root_inode
;
907 static struct tree_descr smack_files
[] = {
909 {"load", &smk_load_ops
, S_IRUGO
|S_IWUSR
},
911 {"cipso", &smk_cipso_ops
, S_IRUGO
|S_IWUSR
},
913 {"doi", &smk_doi_ops
, S_IRUGO
|S_IWUSR
},
915 {"direct", &smk_direct_ops
, S_IRUGO
|S_IWUSR
},
917 {"ambient", &smk_ambient_ops
, S_IRUGO
|S_IWUSR
},
919 {"nltype", &smk_nltype_ops
, S_IRUGO
|S_IWUSR
},
923 rc
= simple_fill_super(sb
, SMACK_MAGIC
, smack_files
);
925 printk(KERN_ERR
"%s failed %d while creating inodes\n",
930 root_inode
= sb
->s_root
->d_inode
;
931 root_inode
->i_security
= new_inode_smack(smack_known_floor
.smk_known
);
937 * smk_get_sb - get the smackfs superblock
938 * @fs_type: passed along without comment
939 * @flags: passed along without comment
940 * @dev_name: passed along without comment
941 * @data: passed along without comment
942 * @mnt: passed along without comment
944 * Just passes everything along.
946 * Returns what the lower level code does.
948 static int smk_get_sb(struct file_system_type
*fs_type
,
949 int flags
, const char *dev_name
, void *data
,
950 struct vfsmount
*mnt
)
952 return get_sb_single(fs_type
, flags
, data
, smk_fill_super
, mnt
);
955 static struct file_system_type smk_fs_type
= {
957 .get_sb
= smk_get_sb
,
958 .kill_sb
= kill_litter_super
,
961 static struct vfsmount
*smackfs_mount
;
964 * init_smk_fs - get the smackfs superblock
966 * register the smackfs
968 * Returns 0 unless the registration fails.
970 static int __init
init_smk_fs(void)
974 err
= register_filesystem(&smk_fs_type
);
976 smackfs_mount
= kern_mount(&smk_fs_type
);
977 if (IS_ERR(smackfs_mount
)) {
978 printk(KERN_ERR
"smackfs: could not mount!\n");
979 err
= PTR_ERR(smackfs_mount
);
980 smackfs_mount
= NULL
;
985 smk_unlbl_ambient(NULL
);
990 __initcall(init_smk_fs
);