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 static 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
.sessionid
= audit_get_sessionid(current
);
328 audit_info
.secid
= smack_to_secid(current
->security
);
330 rc
= netlbl_cfg_map_del(NULL
, &audit_info
);
332 printk(KERN_WARNING
"%s:%d remove rc = %d\n",
333 __func__
, __LINE__
, rc
);
335 doip
= kmalloc(sizeof(struct cipso_v4_doi
), GFP_KERNEL
);
337 panic("smack: Failed to initialize cipso DOI.\n");
338 doip
->map
.std
= NULL
;
339 doip
->doi
= smk_cipso_doi_value
;
340 doip
->type
= CIPSO_V4_MAP_PASS
;
341 doip
->tags
[0] = CIPSO_V4_TAG_RBITMAP
;
342 for (rc
= 1; rc
< CIPSO_V4_TAG_MAXCNT
; rc
++)
343 doip
->tags
[rc
] = CIPSO_V4_TAG_INVALID
;
345 rc
= netlbl_cfg_cipsov4_add_map(doip
, NULL
, &audit_info
);
347 printk(KERN_WARNING
"%s:%d add rc = %d\n",
348 __func__
, __LINE__
, rc
);
352 * smk_unlbl_ambient - initialize the unlabeled domain
354 static void smk_unlbl_ambient(char *oldambient
)
357 struct netlbl_audit audit_info
;
359 audit_info
.loginuid
= audit_get_loginuid(current
);
360 audit_info
.sessionid
= audit_get_sessionid(current
);
361 audit_info
.secid
= smack_to_secid(current
->security
);
363 if (oldambient
!= NULL
) {
364 rc
= netlbl_cfg_map_del(oldambient
, &audit_info
);
366 printk(KERN_WARNING
"%s:%d remove rc = %d\n",
367 __func__
, __LINE__
, rc
);
370 rc
= netlbl_cfg_unlbl_add_map(smack_net_ambient
, &audit_info
);
372 printk(KERN_WARNING
"%s:%d add rc = %d\n",
373 __func__
, __LINE__
, rc
);
377 * Seq_file read operations for /smack/cipso
380 static void *cipso_seq_start(struct seq_file
*s
, loff_t
*pos
)
382 if (*pos
== SEQ_READ_FINISHED
)
388 static void *cipso_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
390 struct smack_known
*skp
= ((struct smack_known
*) v
)->smk_next
;
393 * Omit labels with no associated cipso value
395 while (skp
!= NULL
&& !skp
->smk_cipso
)
399 *pos
= SEQ_READ_FINISHED
;
405 * Print cipso labels in format:
406 * label level[/cat[,cat]]
408 static int cipso_seq_show(struct seq_file
*s
, void *v
)
410 struct smack_known
*skp
= (struct smack_known
*) v
;
411 struct smack_cipso
*scp
= skp
->smk_cipso
;
421 seq_printf(s
, "%s %3d", (char *)&skp
->smk_known
, scp
->smk_level
);
423 cbp
= scp
->smk_catset
;
424 for (i
= 0; i
< SMK_LABELLEN
; i
++)
425 for (m
= 0x80; m
!= 0; m
>>= 1) {
427 seq_printf(s
, "%c%d", sep
, cat
);
438 static void cipso_seq_stop(struct seq_file
*s
, void *v
)
443 static struct seq_operations cipso_seq_ops
= {
444 .start
= cipso_seq_start
,
445 .stop
= cipso_seq_stop
,
446 .next
= cipso_seq_next
,
447 .show
= cipso_seq_show
,
451 * smk_open_cipso - open() for /smack/cipso
452 * @inode: inode structure representing file
453 * @file: "cipso" file pointer
455 * Connect our cipso_seq_* operations with /smack/cipso
458 static int smk_open_cipso(struct inode
*inode
, struct file
*file
)
460 return seq_open(file
, &cipso_seq_ops
);
464 * smk_write_cipso - write() for /smack/cipso
465 * @filp: file pointer, not actually used
466 * @buf: where to get the data from
468 * @ppos: where to start
470 * Accepts only one cipso rule per write call.
471 * Returns number of bytes written or error code, as appropriate
473 static ssize_t
smk_write_cipso(struct file
*file
, const char __user
*buf
,
474 size_t count
, loff_t
*ppos
)
476 struct smack_known
*skp
;
477 struct smack_cipso
*scp
= NULL
;
478 char mapcatset
[SMK_LABELLEN
];
482 ssize_t rc
= -EINVAL
;
489 * Must have privilege.
491 * Enough data must be present.
493 if (!capable(CAP_MAC_ADMIN
))
497 if (count
< SMK_CIPSOMIN
|| count
> SMK_CIPSOMAX
)
500 data
= kzalloc(count
+ 1, GFP_KERNEL
);
504 if (copy_from_user(data
, buf
, count
) != 0) {
512 * Only allow one writer at a time. Writes should be
513 * quite rare and small in any case.
515 mutex_lock(&smack_cipso_lock
);
517 skp
= smk_import_entry(rule
, 0);
521 rule
+= SMK_LABELLEN
;;
522 ret
= sscanf(rule
, "%d", &maplevel
);
523 if (ret
!= 1 || maplevel
> SMACK_CIPSO_MAXLEVEL
)
526 rule
+= SMK_DIGITLEN
;
527 ret
= sscanf(rule
, "%d", &catlen
);
528 if (ret
!= 1 || catlen
> SMACK_CIPSO_MAXCATNUM
)
531 if (count
!= (SMK_CIPSOMIN
+ catlen
* SMK_DIGITLEN
))
534 memset(mapcatset
, 0, sizeof(mapcatset
));
536 for (i
= 0; i
< catlen
; i
++) {
537 rule
+= SMK_DIGITLEN
;
538 ret
= sscanf(rule
, "%d", &cat
);
539 if (ret
!= 1 || cat
> SMACK_CIPSO_MAXCATVAL
)
542 smack_catset_bit(cat
, mapcatset
);
545 if (skp
->smk_cipso
== NULL
) {
546 scp
= kzalloc(sizeof(struct smack_cipso
), GFP_KERNEL
);
553 spin_lock_bh(&skp
->smk_cipsolock
);
556 scp
= skp
->smk_cipso
;
558 skp
->smk_cipso
= scp
;
560 scp
->smk_level
= maplevel
;
561 memcpy(scp
->smk_catset
, mapcatset
, sizeof(mapcatset
));
563 spin_unlock_bh(&skp
->smk_cipsolock
);
567 mutex_unlock(&smack_cipso_lock
);
573 static const struct file_operations smk_cipso_ops
= {
574 .open
= smk_open_cipso
,
577 .write
= smk_write_cipso
,
578 .release
= seq_release
,
582 * smk_read_doi - read() for /smack/doi
583 * @filp: file pointer, not actually used
584 * @buf: where to put the result
585 * @count: maximum to send along
586 * @ppos: where to start
588 * Returns number of bytes read or error code, as appropriate
590 static ssize_t
smk_read_doi(struct file
*filp
, char __user
*buf
,
591 size_t count
, loff_t
*ppos
)
599 sprintf(temp
, "%d", smk_cipso_doi_value
);
600 rc
= simple_read_from_buffer(buf
, count
, ppos
, temp
, strlen(temp
));
606 * smk_write_doi - write() for /smack/doi
607 * @filp: file pointer, not actually used
608 * @buf: where to get the data from
610 * @ppos: where to start
612 * Returns number of bytes written or error code, as appropriate
614 static ssize_t
smk_write_doi(struct file
*file
, const char __user
*buf
,
615 size_t count
, loff_t
*ppos
)
620 if (!capable(CAP_MAC_ADMIN
))
623 if (count
>= sizeof(temp
) || count
== 0)
626 if (copy_from_user(temp
, buf
, count
) != 0)
631 if (sscanf(temp
, "%d", &i
) != 1)
634 smk_cipso_doi_value
= i
;
641 static const struct file_operations smk_doi_ops
= {
642 .read
= smk_read_doi
,
643 .write
= smk_write_doi
,
647 * smk_read_direct - read() for /smack/direct
648 * @filp: file pointer, not actually used
649 * @buf: where to put the result
650 * @count: maximum to send along
651 * @ppos: where to start
653 * Returns number of bytes read or error code, as appropriate
655 static ssize_t
smk_read_direct(struct file
*filp
, char __user
*buf
,
656 size_t count
, loff_t
*ppos
)
664 sprintf(temp
, "%d", smack_cipso_direct
);
665 rc
= simple_read_from_buffer(buf
, count
, ppos
, temp
, strlen(temp
));
671 * smk_write_direct - write() for /smack/direct
672 * @filp: file pointer, not actually used
673 * @buf: where to get the data from
675 * @ppos: where to start
677 * Returns number of bytes written or error code, as appropriate
679 static ssize_t
smk_write_direct(struct file
*file
, const char __user
*buf
,
680 size_t count
, loff_t
*ppos
)
685 if (!capable(CAP_MAC_ADMIN
))
688 if (count
>= sizeof(temp
) || count
== 0)
691 if (copy_from_user(temp
, buf
, count
) != 0)
696 if (sscanf(temp
, "%d", &i
) != 1)
699 smack_cipso_direct
= i
;
704 static const struct file_operations smk_direct_ops
= {
705 .read
= smk_read_direct
,
706 .write
= smk_write_direct
,
710 * smk_read_ambient - read() for /smack/ambient
711 * @filp: file pointer, not actually used
712 * @buf: where to put the result
713 * @cn: maximum to send along
714 * @ppos: where to start
716 * Returns number of bytes read or error code, as appropriate
718 static ssize_t
smk_read_ambient(struct file
*filp
, char __user
*buf
,
719 size_t cn
, loff_t
*ppos
)
727 * Being careful to avoid a problem in the case where
728 * smack_net_ambient gets changed in midstream.
730 mutex_lock(&smack_ambient_lock
);
732 asize
= strlen(smack_net_ambient
) + 1;
735 rc
= simple_read_from_buffer(buf
, cn
, ppos
,
736 smack_net_ambient
, asize
);
740 mutex_unlock(&smack_ambient_lock
);
746 * smk_write_ambient - write() for /smack/ambient
747 * @filp: file pointer, not actually used
748 * @buf: where to get the data from
750 * @ppos: where to start
752 * Returns number of bytes written or error code, as appropriate
754 static ssize_t
smk_write_ambient(struct file
*file
, const char __user
*buf
,
755 size_t count
, loff_t
*ppos
)
757 char in
[SMK_LABELLEN
];
761 if (!capable(CAP_MAC_ADMIN
))
764 if (count
>= SMK_LABELLEN
)
767 if (copy_from_user(in
, buf
, count
) != 0)
770 smack
= smk_import(in
, count
);
774 mutex_lock(&smack_ambient_lock
);
776 oldambient
= smack_net_ambient
;
777 smack_net_ambient
= smack
;
778 smk_unlbl_ambient(oldambient
);
780 mutex_unlock(&smack_ambient_lock
);
785 static const struct file_operations smk_ambient_ops
= {
786 .read
= smk_read_ambient
,
787 .write
= smk_write_ambient
,
790 struct option_names
{
796 static struct option_names netlbl_choices
[] = {
797 { NETLBL_NLTYPE_RIPSO
,
798 NETLBL_NLTYPE_RIPSO_NAME
, "ripso" },
799 { NETLBL_NLTYPE_CIPSOV4
,
800 NETLBL_NLTYPE_CIPSOV4_NAME
, "cipsov4" },
801 { NETLBL_NLTYPE_CIPSOV4
,
802 NETLBL_NLTYPE_CIPSOV4_NAME
, "cipso" },
803 { NETLBL_NLTYPE_CIPSOV6
,
804 NETLBL_NLTYPE_CIPSOV6_NAME
, "cipsov6" },
805 { NETLBL_NLTYPE_UNLABELED
,
806 NETLBL_NLTYPE_UNLABELED_NAME
, "unlabeled" },
810 * smk_read_nltype - read() for /smack/nltype
811 * @filp: file pointer, not actually used
812 * @buf: where to put the result
813 * @count: maximum to send along
814 * @ppos: where to start
816 * Returns number of bytes read or error code, as appropriate
818 static ssize_t
smk_read_nltype(struct file
*filp
, char __user
*buf
,
819 size_t count
, loff_t
*ppos
)
825 if (count
< SMK_LABELLEN
)
831 sprintf(bound
, "unknown");
833 for (i
= 0; i
< ARRAY_SIZE(netlbl_choices
); i
++)
834 if (smack_net_nltype
== netlbl_choices
[i
].o_number
) {
835 sprintf(bound
, "%s", netlbl_choices
[i
].o_name
);
839 rc
= simple_read_from_buffer(buf
, count
, ppos
, bound
, strlen(bound
));
845 * smk_write_nltype - write() for /smack/nltype
846 * @filp: file pointer, not actually used
847 * @buf: where to get the data from
849 * @ppos: where to start
851 * Returns number of bytes written or error code, as appropriate
853 static ssize_t
smk_write_nltype(struct file
*file
, const char __user
*buf
,
854 size_t count
, loff_t
*ppos
)
860 if (!capable(CAP_MAC_ADMIN
))
866 if (copy_from_user(bound
, buf
, count
) != 0)
870 cp
= strchr(bound
, ' ');
873 cp
= strchr(bound
, '\n');
877 for (i
= 0; i
< ARRAY_SIZE(netlbl_choices
); i
++)
878 if (strcmp(bound
, netlbl_choices
[i
].o_name
) == 0 ||
879 strcmp(bound
, netlbl_choices
[i
].o_alias
) == 0) {
880 smack_net_nltype
= netlbl_choices
[i
].o_number
;
884 * Not a valid choice.
889 static const struct file_operations smk_nltype_ops
= {
890 .read
= smk_read_nltype
,
891 .write
= smk_write_nltype
,
895 * smk_fill_super - fill the /smackfs superblock
896 * @sb: the empty superblock
900 * Fill in the well known entries for /smack
902 * Returns 0 on success, an error code on failure
904 static int smk_fill_super(struct super_block
*sb
, void *data
, int silent
)
907 struct inode
*root_inode
;
909 static struct tree_descr smack_files
[] = {
911 {"load", &smk_load_ops
, S_IRUGO
|S_IWUSR
},
913 {"cipso", &smk_cipso_ops
, S_IRUGO
|S_IWUSR
},
915 {"doi", &smk_doi_ops
, S_IRUGO
|S_IWUSR
},
917 {"direct", &smk_direct_ops
, S_IRUGO
|S_IWUSR
},
919 {"ambient", &smk_ambient_ops
, S_IRUGO
|S_IWUSR
},
921 {"nltype", &smk_nltype_ops
, S_IRUGO
|S_IWUSR
},
925 rc
= simple_fill_super(sb
, SMACK_MAGIC
, smack_files
);
927 printk(KERN_ERR
"%s failed %d while creating inodes\n",
932 root_inode
= sb
->s_root
->d_inode
;
933 root_inode
->i_security
= new_inode_smack(smack_known_floor
.smk_known
);
939 * smk_get_sb - get the smackfs superblock
940 * @fs_type: passed along without comment
941 * @flags: passed along without comment
942 * @dev_name: passed along without comment
943 * @data: passed along without comment
944 * @mnt: passed along without comment
946 * Just passes everything along.
948 * Returns what the lower level code does.
950 static int smk_get_sb(struct file_system_type
*fs_type
,
951 int flags
, const char *dev_name
, void *data
,
952 struct vfsmount
*mnt
)
954 return get_sb_single(fs_type
, flags
, data
, smk_fill_super
, mnt
);
957 static struct file_system_type smk_fs_type
= {
959 .get_sb
= smk_get_sb
,
960 .kill_sb
= kill_litter_super
,
963 static struct vfsmount
*smackfs_mount
;
966 * init_smk_fs - get the smackfs superblock
968 * register the smackfs
970 * Do not register smackfs if Smack wasn't enabled
971 * on boot. We can not put this method normally under the
972 * smack_init() code path since the security subsystem get
973 * initialized before the vfs caches.
975 * Returns true if we were not chosen on boot or if
976 * we were chosen and filesystem registration succeeded.
978 static int __init
init_smk_fs(void)
982 if (!security_module_enable(&smack_ops
))
985 err
= register_filesystem(&smk_fs_type
);
987 smackfs_mount
= kern_mount(&smk_fs_type
);
988 if (IS_ERR(smackfs_mount
)) {
989 printk(KERN_ERR
"smackfs: could not mount!\n");
990 err
= PTR_ERR(smackfs_mount
);
991 smackfs_mount
= NULL
;
996 smk_unlbl_ambient(NULL
);
1001 __initcall(init_smk_fs
);