1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/xattr.h>
19#include <linux/pagemap.h>
20#include <linux/mount.h>
21#include <linux/stat.h>
22#include <linux/kd.h>
23#include <asm/ioctls.h>
24#include <linux/ip.h>
25#include <linux/tcp.h>
26#include <linux/udp.h>
27#include <linux/dccp.h>
28#include <linux/icmpv6.h>
29#include <linux/slab.h>
30#include <linux/mutex.h>
31#include <net/cipso_ipv4.h>
32#include <net/ip.h>
33#include <net/ipv6.h>
34#include <linux/audit.h>
35#include <linux/magic.h>
36#include <linux/dcache.h>
37#include <linux/personality.h>
38#include <linux/msg.h>
39#include <linux/shm.h>
40#include <linux/binfmts.h>
41#include <linux/parser.h>
42#include <linux/fs_context.h>
43#include <linux/fs_parser.h>
44#include <linux/watch_queue.h>
45#include "smack.h"
46
47#define TRANS_TRUE "TRUE"
48#define TRANS_TRUE_SIZE 4
49
50#define SMK_CONNECTING 0
51#define SMK_RECEIVING 1
52#define SMK_SENDING 2
53
54#ifdef SMACK_IPV6_PORT_LABELING
55static DEFINE_MUTEX(smack_ipv6_lock);
56static LIST_HEAD(smk_ipv6_port_list);
57#endif
58struct kmem_cache *smack_rule_cache;
59int smack_enabled __initdata;
60
61#define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
62static struct {
63 const char *name;
64 int len;
65 int opt;
66} smk_mount_opts[] = {
67 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
68 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
69};
70#undef A
71
72static int match_opt_prefix(char *s, int l, char **arg)
73{
74 int i;
75
76 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
77 size_t len = smk_mount_opts[i].len;
78 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
79 continue;
80 if (len == l || s[len] != '=')
81 continue;
82 *arg = s + len + 1;
83 return smk_mount_opts[i].opt;
84 }
85 return Opt_error;
86}
87
88#ifdef CONFIG_SECURITY_SMACK_BRINGUP
89static char *smk_bu_mess[] = {
90 "Bringup Error",
91 "Bringup",
92 "Unconfined Subject",
93 "Unconfined Object",
94};
95
96static void smk_bu_mode(int mode, char *s)
97{
98 int i = 0;
99
100 if (mode & MAY_READ)
101 s[i++] = 'r';
102 if (mode & MAY_WRITE)
103 s[i++] = 'w';
104 if (mode & MAY_EXEC)
105 s[i++] = 'x';
106 if (mode & MAY_APPEND)
107 s[i++] = 'a';
108 if (mode & MAY_TRANSMUTE)
109 s[i++] = 't';
110 if (mode & MAY_LOCK)
111 s[i++] = 'l';
112 if (i == 0)
113 s[i++] = '-';
114 s[i] = '\0';
115}
116#endif
117
118#ifdef CONFIG_SECURITY_SMACK_BRINGUP
119static int smk_bu_note(char *note, struct smack_known *sskp,
120 struct smack_known *oskp, int mode, int rc)
121{
122 char acc[SMK_NUM_ACCESS_TYPE + 1];
123
124 if (rc <= 0)
125 return rc;
126 if (rc > SMACK_UNCONFINED_OBJECT)
127 rc = 0;
128
129 smk_bu_mode(mode, acc);
130 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
131 sskp->smk_known, oskp->smk_known, acc, note);
132 return 0;
133}
134#else
135#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
136#endif
137
138#ifdef CONFIG_SECURITY_SMACK_BRINGUP
139static int smk_bu_current(char *note, struct smack_known *oskp,
140 int mode, int rc)
141{
142 struct task_smack *tsp = smack_cred(current_cred());
143 char acc[SMK_NUM_ACCESS_TYPE + 1];
144
145 if (rc <= 0)
146 return rc;
147 if (rc > SMACK_UNCONFINED_OBJECT)
148 rc = 0;
149
150 smk_bu_mode(mode, acc);
151 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
152 tsp->smk_task->smk_known, oskp->smk_known,
153 acc, current->comm, note);
154 return 0;
155}
156#else
157#define smk_bu_current(note, oskp, mode, RC) (RC)
158#endif
159
160#ifdef CONFIG_SECURITY_SMACK_BRINGUP
161static int smk_bu_task(struct task_struct *otp, int mode, int rc)
162{
163 struct task_smack *tsp = smack_cred(current_cred());
164 struct smack_known *smk_task = smk_of_task_struct_obj(otp);
165 char acc[SMK_NUM_ACCESS_TYPE + 1];
166
167 if (rc <= 0)
168 return rc;
169 if (rc > SMACK_UNCONFINED_OBJECT)
170 rc = 0;
171
172 smk_bu_mode(mode, acc);
173 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
174 tsp->smk_task->smk_known, smk_task->smk_known, acc,
175 current->comm, otp->comm);
176 return 0;
177}
178#else
179#define smk_bu_task(otp, mode, RC) (RC)
180#endif
181
182#ifdef CONFIG_SECURITY_SMACK_BRINGUP
183static int smk_bu_inode(struct inode *inode, int mode, int rc)
184{
185 struct task_smack *tsp = smack_cred(current_cred());
186 struct inode_smack *isp = smack_inode(inode);
187 char acc[SMK_NUM_ACCESS_TYPE + 1];
188
189 if (isp->smk_flags & SMK_INODE_IMPURE)
190 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
191 inode->i_sb->s_id, inode->i_ino, current->comm);
192
193 if (rc <= 0)
194 return rc;
195 if (rc > SMACK_UNCONFINED_OBJECT)
196 rc = 0;
197 if (rc == SMACK_UNCONFINED_SUBJECT &&
198 (mode & (MAY_WRITE | MAY_APPEND)))
199 isp->smk_flags |= SMK_INODE_IMPURE;
200
201 smk_bu_mode(mode, acc);
202
203 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
204 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
205 inode->i_sb->s_id, inode->i_ino, current->comm);
206 return 0;
207}
208#else
209#define smk_bu_inode(inode, mode, RC) (RC)
210#endif
211
212#ifdef CONFIG_SECURITY_SMACK_BRINGUP
213static int smk_bu_file(struct file *file, int mode, int rc)
214{
215 struct task_smack *tsp = smack_cred(current_cred());
216 struct smack_known *sskp = tsp->smk_task;
217 struct inode *inode = file_inode(file);
218 struct inode_smack *isp = smack_inode(inode);
219 char acc[SMK_NUM_ACCESS_TYPE + 1];
220
221 if (isp->smk_flags & SMK_INODE_IMPURE)
222 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
223 inode->i_sb->s_id, inode->i_ino, current->comm);
224
225 if (rc <= 0)
226 return rc;
227 if (rc > SMACK_UNCONFINED_OBJECT)
228 rc = 0;
229
230 smk_bu_mode(mode, acc);
231 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
232 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
233 inode->i_sb->s_id, inode->i_ino, file,
234 current->comm);
235 return 0;
236}
237#else
238#define smk_bu_file(file, mode, RC) (RC)
239#endif
240
241#ifdef CONFIG_SECURITY_SMACK_BRINGUP
242static int smk_bu_credfile(const struct cred *cred, struct file *file,
243 int mode, int rc)
244{
245 struct task_smack *tsp = smack_cred(cred);
246 struct smack_known *sskp = tsp->smk_task;
247 struct inode *inode = file_inode(file);
248 struct inode_smack *isp = smack_inode(inode);
249 char acc[SMK_NUM_ACCESS_TYPE + 1];
250
251 if (isp->smk_flags & SMK_INODE_IMPURE)
252 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
253 inode->i_sb->s_id, inode->i_ino, current->comm);
254
255 if (rc <= 0)
256 return rc;
257 if (rc > SMACK_UNCONFINED_OBJECT)
258 rc = 0;
259
260 smk_bu_mode(mode, acc);
261 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
262 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
263 inode->i_sb->s_id, inode->i_ino, file,
264 current->comm);
265 return 0;
266}
267#else
268#define smk_bu_credfile(cred, file, mode, RC) (RC)
269#endif
270
271
272
273
274
275
276
277
278
279
280static struct smack_known *smk_fetch(const char *name, struct inode *ip,
281 struct dentry *dp)
282{
283 int rc;
284 char *buffer;
285 struct smack_known *skp = NULL;
286
287 if (!(ip->i_opflags & IOP_XATTR))
288 return ERR_PTR(-EOPNOTSUPP);
289
290 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
291 if (buffer == NULL)
292 return ERR_PTR(-ENOMEM);
293
294 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
295 if (rc < 0)
296 skp = ERR_PTR(rc);
297 else if (rc == 0)
298 skp = NULL;
299 else
300 skp = smk_import_entry(buffer, rc);
301
302 kfree(buffer);
303
304 return skp;
305}
306
307
308
309
310
311
312
313static void init_inode_smack(struct inode *inode, struct smack_known *skp)
314{
315 struct inode_smack *isp = smack_inode(inode);
316
317 isp->smk_inode = skp;
318 isp->smk_flags = 0;
319}
320
321
322
323
324
325
326
327
328static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
329 struct smack_known *forked)
330{
331 tsp->smk_task = task;
332 tsp->smk_forked = forked;
333 INIT_LIST_HEAD(&tsp->smk_rules);
334 INIT_LIST_HEAD(&tsp->smk_relabel);
335 mutex_init(&tsp->smk_rules_lock);
336}
337
338
339
340
341
342
343
344
345
346static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
347 gfp_t gfp)
348{
349 struct smack_rule *nrp;
350 struct smack_rule *orp;
351 int rc = 0;
352
353 list_for_each_entry_rcu(orp, ohead, list) {
354 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
355 if (nrp == NULL) {
356 rc = -ENOMEM;
357 break;
358 }
359 *nrp = *orp;
360 list_add_rcu(&nrp->list, nhead);
361 }
362 return rc;
363}
364
365
366
367
368
369
370
371
372
373static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
374 gfp_t gfp)
375{
376 struct smack_known_list_elem *nklep;
377 struct smack_known_list_elem *oklep;
378
379 list_for_each_entry(oklep, ohead, list) {
380 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
381 if (nklep == NULL) {
382 smk_destroy_label_list(nhead);
383 return -ENOMEM;
384 }
385 nklep->smk_label = oklep->smk_label;
386 list_add(&nklep->list, nhead);
387 }
388
389 return 0;
390}
391
392
393
394
395
396
397
398static inline unsigned int smk_ptrace_mode(unsigned int mode)
399{
400 if (mode & PTRACE_MODE_ATTACH)
401 return MAY_READWRITE;
402 if (mode & PTRACE_MODE_READ)
403 return MAY_READ;
404
405 return 0;
406}
407
408
409
410
411
412
413
414
415
416
417static int smk_ptrace_rule_check(struct task_struct *tracer,
418 struct smack_known *tracee_known,
419 unsigned int mode, const char *func)
420{
421 int rc;
422 struct smk_audit_info ad, *saip = NULL;
423 struct task_smack *tsp;
424 struct smack_known *tracer_known;
425 const struct cred *tracercred;
426
427 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
428 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
429 smk_ad_setfield_u_tsk(&ad, tracer);
430 saip = &ad;
431 }
432
433 rcu_read_lock();
434 tracercred = __task_cred(tracer);
435 tsp = smack_cred(tracercred);
436 tracer_known = smk_of_task(tsp);
437
438 if ((mode & PTRACE_MODE_ATTACH) &&
439 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
440 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
441 if (tracer_known->smk_known == tracee_known->smk_known)
442 rc = 0;
443 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
444 rc = -EACCES;
445 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
446 rc = 0;
447 else
448 rc = -EACCES;
449
450 if (saip)
451 smack_log(tracer_known->smk_known,
452 tracee_known->smk_known,
453 0, rc, saip);
454
455 rcu_read_unlock();
456 return rc;
457 }
458
459
460 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
461
462 rcu_read_unlock();
463 return rc;
464}
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
481{
482 struct smack_known *skp;
483
484 skp = smk_of_task_struct_obj(ctp);
485
486 return smk_ptrace_rule_check(current, skp, mode, __func__);
487}
488
489
490
491
492
493
494
495
496
497static int smack_ptrace_traceme(struct task_struct *ptp)
498{
499 int rc;
500 struct smack_known *skp;
501
502 skp = smk_of_task(smack_cred(current_cred()));
503
504 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
505 return rc;
506}
507
508
509
510
511
512
513
514static int smack_syslog(int typefrom_file)
515{
516 int rc = 0;
517 struct smack_known *skp = smk_of_current();
518
519 if (smack_privileged(CAP_MAC_OVERRIDE))
520 return 0;
521
522 if (smack_syslog_label != NULL && smack_syslog_label != skp)
523 rc = -EACCES;
524
525 return rc;
526}
527
528
529
530
531
532
533
534
535
536
537
538static int smack_sb_alloc_security(struct super_block *sb)
539{
540 struct superblock_smack *sbsp = smack_superblock(sb);
541
542 sbsp->smk_root = &smack_known_floor;
543 sbsp->smk_default = &smack_known_floor;
544 sbsp->smk_floor = &smack_known_floor;
545 sbsp->smk_hat = &smack_known_hat;
546
547
548
549
550 return 0;
551}
552
553struct smack_mnt_opts {
554 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
555};
556
557static void smack_free_mnt_opts(void *mnt_opts)
558{
559 struct smack_mnt_opts *opts = mnt_opts;
560 kfree(opts->fsdefault);
561 kfree(opts->fsfloor);
562 kfree(opts->fshat);
563 kfree(opts->fsroot);
564 kfree(opts->fstransmute);
565 kfree(opts);
566}
567
568static int smack_add_opt(int token, const char *s, void **mnt_opts)
569{
570 struct smack_mnt_opts *opts = *mnt_opts;
571
572 if (!opts) {
573 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
574 if (!opts)
575 return -ENOMEM;
576 *mnt_opts = opts;
577 }
578 if (!s)
579 return -ENOMEM;
580
581 switch (token) {
582 case Opt_fsdefault:
583 if (opts->fsdefault)
584 goto out_opt_err;
585 opts->fsdefault = s;
586 break;
587 case Opt_fsfloor:
588 if (opts->fsfloor)
589 goto out_opt_err;
590 opts->fsfloor = s;
591 break;
592 case Opt_fshat:
593 if (opts->fshat)
594 goto out_opt_err;
595 opts->fshat = s;
596 break;
597 case Opt_fsroot:
598 if (opts->fsroot)
599 goto out_opt_err;
600 opts->fsroot = s;
601 break;
602 case Opt_fstransmute:
603 if (opts->fstransmute)
604 goto out_opt_err;
605 opts->fstransmute = s;
606 break;
607 }
608 return 0;
609
610out_opt_err:
611 pr_warn("Smack: duplicate mount options\n");
612 return -EINVAL;
613}
614
615
616
617
618
619
620
621
622static int smack_fs_context_dup(struct fs_context *fc,
623 struct fs_context *src_fc)
624{
625 struct smack_mnt_opts *dst, *src = src_fc->security;
626
627 if (!src)
628 return 0;
629
630 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
631 if (!fc->security)
632 return -ENOMEM;
633 dst = fc->security;
634
635 if (src->fsdefault) {
636 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
637 if (!dst->fsdefault)
638 return -ENOMEM;
639 }
640 if (src->fsfloor) {
641 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
642 if (!dst->fsfloor)
643 return -ENOMEM;
644 }
645 if (src->fshat) {
646 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
647 if (!dst->fshat)
648 return -ENOMEM;
649 }
650 if (src->fsroot) {
651 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
652 if (!dst->fsroot)
653 return -ENOMEM;
654 }
655 if (src->fstransmute) {
656 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
657 if (!dst->fstransmute)
658 return -ENOMEM;
659 }
660 return 0;
661}
662
663static const struct fs_parameter_spec smack_fs_parameters[] = {
664 fsparam_string("smackfsdef", Opt_fsdefault),
665 fsparam_string("smackfsdefault", Opt_fsdefault),
666 fsparam_string("smackfsfloor", Opt_fsfloor),
667 fsparam_string("smackfshat", Opt_fshat),
668 fsparam_string("smackfsroot", Opt_fsroot),
669 fsparam_string("smackfstransmute", Opt_fstransmute),
670 {}
671};
672
673
674
675
676
677
678
679
680
681static int smack_fs_context_parse_param(struct fs_context *fc,
682 struct fs_parameter *param)
683{
684 struct fs_parse_result result;
685 int opt, rc;
686
687 opt = fs_parse(fc, smack_fs_parameters, param, &result);
688 if (opt < 0)
689 return opt;
690
691 rc = smack_add_opt(opt, param->string, &fc->security);
692 if (!rc)
693 param->string = NULL;
694 return rc;
695}
696
697static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
698{
699 char *from = options, *to = options;
700 bool first = true;
701
702 while (1) {
703 char *next = strchr(from, ',');
704 int token, len, rc;
705 char *arg = NULL;
706
707 if (next)
708 len = next - from;
709 else
710 len = strlen(from);
711
712 token = match_opt_prefix(from, len, &arg);
713 if (token != Opt_error) {
714 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
715 rc = smack_add_opt(token, arg, mnt_opts);
716 if (unlikely(rc)) {
717 kfree(arg);
718 if (*mnt_opts)
719 smack_free_mnt_opts(*mnt_opts);
720 *mnt_opts = NULL;
721 return rc;
722 }
723 } else {
724 if (!first) {
725 from--;
726 len++;
727 }
728 if (to != from)
729 memmove(to, from, len);
730 to += len;
731 first = false;
732 }
733 if (!from[len])
734 break;
735 from += len + 1;
736 }
737 *to = '\0';
738 return 0;
739}
740
741
742
743
744
745
746
747
748
749
750
751
752
753static int smack_set_mnt_opts(struct super_block *sb,
754 void *mnt_opts,
755 unsigned long kern_flags,
756 unsigned long *set_kern_flags)
757{
758 struct dentry *root = sb->s_root;
759 struct inode *inode = d_backing_inode(root);
760 struct superblock_smack *sp = smack_superblock(sb);
761 struct inode_smack *isp;
762 struct smack_known *skp;
763 struct smack_mnt_opts *opts = mnt_opts;
764 bool transmute = false;
765
766 if (sp->smk_flags & SMK_SB_INITIALIZED)
767 return 0;
768
769 if (inode->i_security == NULL) {
770 int rc = lsm_inode_alloc(inode);
771
772 if (rc)
773 return rc;
774 }
775
776 if (!smack_privileged(CAP_MAC_ADMIN)) {
777
778
779
780 if (opts)
781 return -EPERM;
782
783
784
785 skp = smk_of_current();
786 sp->smk_root = skp;
787 sp->smk_default = skp;
788
789
790
791
792
793 if (sb->s_user_ns != &init_user_ns &&
794 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
795 sb->s_magic != RAMFS_MAGIC) {
796 transmute = true;
797 sp->smk_flags |= SMK_SB_UNTRUSTED;
798 }
799 }
800
801 sp->smk_flags |= SMK_SB_INITIALIZED;
802
803 if (opts) {
804 if (opts->fsdefault) {
805 skp = smk_import_entry(opts->fsdefault, 0);
806 if (IS_ERR(skp))
807 return PTR_ERR(skp);
808 sp->smk_default = skp;
809 }
810 if (opts->fsfloor) {
811 skp = smk_import_entry(opts->fsfloor, 0);
812 if (IS_ERR(skp))
813 return PTR_ERR(skp);
814 sp->smk_floor = skp;
815 }
816 if (opts->fshat) {
817 skp = smk_import_entry(opts->fshat, 0);
818 if (IS_ERR(skp))
819 return PTR_ERR(skp);
820 sp->smk_hat = skp;
821 }
822 if (opts->fsroot) {
823 skp = smk_import_entry(opts->fsroot, 0);
824 if (IS_ERR(skp))
825 return PTR_ERR(skp);
826 sp->smk_root = skp;
827 }
828 if (opts->fstransmute) {
829 skp = smk_import_entry(opts->fstransmute, 0);
830 if (IS_ERR(skp))
831 return PTR_ERR(skp);
832 sp->smk_root = skp;
833 transmute = true;
834 }
835 }
836
837
838
839
840 init_inode_smack(inode, sp->smk_root);
841
842 if (transmute) {
843 isp = smack_inode(inode);
844 isp->smk_flags |= SMK_INODE_TRANSMUTE;
845 }
846
847 return 0;
848}
849
850
851
852
853
854
855
856
857static int smack_sb_statfs(struct dentry *dentry)
858{
859 struct superblock_smack *sbp = smack_superblock(dentry->d_sb);
860 int rc;
861 struct smk_audit_info ad;
862
863 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
864 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
865
866 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
867 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
868 return rc;
869}
870
871
872
873
874
875
876
877
878
879
880
881static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
882{
883 struct inode *inode = file_inode(bprm->file);
884 struct task_smack *bsp = smack_cred(bprm->cred);
885 struct inode_smack *isp;
886 struct superblock_smack *sbsp;
887 int rc;
888
889 isp = smack_inode(inode);
890 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
891 return 0;
892
893 sbsp = smack_superblock(inode->i_sb);
894 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
895 isp->smk_task != sbsp->smk_root)
896 return 0;
897
898 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
899 struct task_struct *tracer;
900 rc = 0;
901
902 rcu_read_lock();
903 tracer = ptrace_parent(current);
904 if (likely(tracer != NULL))
905 rc = smk_ptrace_rule_check(tracer,
906 isp->smk_task,
907 PTRACE_MODE_ATTACH,
908 __func__);
909 rcu_read_unlock();
910
911 if (rc != 0)
912 return rc;
913 }
914 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
915 return -EPERM;
916
917 bsp->smk_task = isp->smk_task;
918 bprm->per_clear |= PER_CLEAR_ON_SETID;
919
920
921 if (bsp->smk_task != bsp->smk_forked)
922 bprm->secureexec = 1;
923
924 return 0;
925}
926
927
928
929
930
931
932
933
934
935
936
937static int smack_inode_alloc_security(struct inode *inode)
938{
939 struct smack_known *skp = smk_of_current();
940
941 init_inode_smack(inode, skp);
942 return 0;
943}
944
945
946
947
948
949
950
951
952
953
954
955
956static int smack_inode_init_security(struct inode *inode, struct inode *dir,
957 const struct qstr *qstr, const char **name,
958 void **value, size_t *len)
959{
960 struct inode_smack *issp = smack_inode(inode);
961 struct smack_known *skp = smk_of_current();
962 struct smack_known *isp = smk_of_inode(inode);
963 struct smack_known *dsp = smk_of_inode(dir);
964 int may;
965
966 if (name)
967 *name = XATTR_SMACK_SUFFIX;
968
969 if (value && len) {
970 rcu_read_lock();
971 may = smk_access_entry(skp->smk_known, dsp->smk_known,
972 &skp->smk_rules);
973 rcu_read_unlock();
974
975
976
977
978
979
980
981 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
982 smk_inode_transmutable(dir)) {
983 isp = dsp;
984 issp->smk_flags |= SMK_INODE_CHANGED;
985 }
986
987 *value = kstrdup(isp->smk_known, GFP_NOFS);
988 if (*value == NULL)
989 return -ENOMEM;
990
991 *len = strlen(isp->smk_known);
992 }
993
994 return 0;
995}
996
997
998
999
1000
1001
1002
1003
1004
1005static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1006 struct dentry *new_dentry)
1007{
1008 struct smack_known *isp;
1009 struct smk_audit_info ad;
1010 int rc;
1011
1012 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1013 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1014
1015 isp = smk_of_inode(d_backing_inode(old_dentry));
1016 rc = smk_curacc(isp, MAY_WRITE, &ad);
1017 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1018
1019 if (rc == 0 && d_is_positive(new_dentry)) {
1020 isp = smk_of_inode(d_backing_inode(new_dentry));
1021 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1022 rc = smk_curacc(isp, MAY_WRITE, &ad);
1023 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1024 }
1025
1026 return rc;
1027}
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1038{
1039 struct inode *ip = d_backing_inode(dentry);
1040 struct smk_audit_info ad;
1041 int rc;
1042
1043 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1044 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1045
1046
1047
1048
1049 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
1050 rc = smk_bu_inode(ip, MAY_WRITE, rc);
1051 if (rc == 0) {
1052
1053
1054
1055 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1056 smk_ad_setfield_u_fs_inode(&ad, dir);
1057 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1058 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1059 }
1060 return rc;
1061}
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1072{
1073 struct smk_audit_info ad;
1074 int rc;
1075
1076 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1077 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1078
1079
1080
1081
1082 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1083 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1084 if (rc == 0) {
1085
1086
1087
1088 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1089 smk_ad_setfield_u_fs_inode(&ad, dir);
1090 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
1091 rc = smk_bu_inode(dir, MAY_WRITE, rc);
1092 }
1093
1094 return rc;
1095}
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109static int smack_inode_rename(struct inode *old_inode,
1110 struct dentry *old_dentry,
1111 struct inode *new_inode,
1112 struct dentry *new_dentry)
1113{
1114 int rc;
1115 struct smack_known *isp;
1116 struct smk_audit_info ad;
1117
1118 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1119 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1120
1121 isp = smk_of_inode(d_backing_inode(old_dentry));
1122 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1123 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1124
1125 if (rc == 0 && d_is_positive(new_dentry)) {
1126 isp = smk_of_inode(d_backing_inode(new_dentry));
1127 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1128 rc = smk_curacc(isp, MAY_READWRITE, &ad);
1129 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1130 }
1131 return rc;
1132}
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143static int smack_inode_permission(struct inode *inode, int mask)
1144{
1145 struct superblock_smack *sbsp = smack_superblock(inode->i_sb);
1146 struct smk_audit_info ad;
1147 int no_block = mask & MAY_NOT_BLOCK;
1148 int rc;
1149
1150 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1151
1152
1153
1154 if (mask == 0)
1155 return 0;
1156
1157 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1158 if (smk_of_inode(inode) != sbsp->smk_root)
1159 return -EACCES;
1160 }
1161
1162
1163 if (no_block)
1164 return -ECHILD;
1165 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
1166 smk_ad_setfield_u_fs_inode(&ad, inode);
1167 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1168 rc = smk_bu_inode(inode, mask, rc);
1169 return rc;
1170}
1171
1172
1173
1174
1175
1176
1177
1178
1179static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1180{
1181 struct smk_audit_info ad;
1182 int rc;
1183
1184
1185
1186
1187 if (iattr->ia_valid & ATTR_FORCE)
1188 return 0;
1189 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1190 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1191
1192 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1193 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1194 return rc;
1195}
1196
1197
1198
1199
1200
1201
1202
1203static int smack_inode_getattr(const struct path *path)
1204{
1205 struct smk_audit_info ad;
1206 struct inode *inode = d_backing_inode(path->dentry);
1207 int rc;
1208
1209 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1210 smk_ad_setfield_u_fs_path(&ad, *path);
1211 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1212 rc = smk_bu_inode(inode, MAY_READ, rc);
1213 return rc;
1214}
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229static int smack_inode_setxattr(struct user_namespace *mnt_userns,
1230 struct dentry *dentry, const char *name,
1231 const void *value, size_t size, int flags)
1232{
1233 struct smk_audit_info ad;
1234 struct smack_known *skp;
1235 int check_priv = 0;
1236 int check_import = 0;
1237 int check_star = 0;
1238 int rc = 0;
1239
1240
1241
1242
1243 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1244 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1245 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1246 check_priv = 1;
1247 check_import = 1;
1248 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1249 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1250 check_priv = 1;
1251 check_import = 1;
1252 check_star = 1;
1253 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1254 check_priv = 1;
1255 if (size != TRANS_TRUE_SIZE ||
1256 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1257 rc = -EINVAL;
1258 } else
1259 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1260
1261 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1262 rc = -EPERM;
1263
1264 if (rc == 0 && check_import) {
1265 skp = size ? smk_import_entry(value, size) : NULL;
1266 if (IS_ERR(skp))
1267 rc = PTR_ERR(skp);
1268 else if (skp == NULL || (check_star &&
1269 (skp == &smack_known_star || skp == &smack_known_web)))
1270 rc = -EINVAL;
1271 }
1272
1273 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1274 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1275
1276 if (rc == 0) {
1277 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1278 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1279 }
1280
1281 return rc;
1282}
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1296 const void *value, size_t size, int flags)
1297{
1298 struct smack_known *skp;
1299 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
1300
1301 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1302 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1303 return;
1304 }
1305
1306 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1307 skp = smk_import_entry(value, size);
1308 if (!IS_ERR(skp))
1309 isp->smk_inode = skp;
1310 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1311 skp = smk_import_entry(value, size);
1312 if (!IS_ERR(skp))
1313 isp->smk_task = skp;
1314 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1315 skp = smk_import_entry(value, size);
1316 if (!IS_ERR(skp))
1317 isp->smk_mmap = skp;
1318 }
1319
1320 return;
1321}
1322
1323
1324
1325
1326
1327
1328
1329
1330static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1331{
1332 struct smk_audit_info ad;
1333 int rc;
1334
1335 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1336 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1337
1338 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1339 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
1340 return rc;
1341}
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353static int smack_inode_removexattr(struct user_namespace *mnt_userns,
1354 struct dentry *dentry, const char *name)
1355{
1356 struct inode_smack *isp;
1357 struct smk_audit_info ad;
1358 int rc = 0;
1359
1360 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1361 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1362 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1363 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1364 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
1365 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1366 if (!smack_privileged(CAP_MAC_ADMIN))
1367 rc = -EPERM;
1368 } else
1369 rc = cap_inode_removexattr(mnt_userns, dentry, name);
1370
1371 if (rc != 0)
1372 return rc;
1373
1374 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
1375 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1376
1377 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1378 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1379 if (rc != 0)
1380 return rc;
1381
1382 isp = smack_inode(d_backing_inode(dentry));
1383
1384
1385
1386
1387
1388 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1389 struct super_block *sbp = dentry->d_sb;
1390 struct superblock_smack *sbsp = smack_superblock(sbp);
1391
1392 isp->smk_inode = sbsp->smk_default;
1393 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1394 isp->smk_task = NULL;
1395 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1396 isp->smk_mmap = NULL;
1397 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1398 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1399
1400 return 0;
1401}
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413static int smack_inode_getsecurity(struct user_namespace *mnt_userns,
1414 struct inode *inode, const char *name,
1415 void **buffer, bool alloc)
1416{
1417 struct socket_smack *ssp;
1418 struct socket *sock;
1419 struct super_block *sbp;
1420 struct inode *ip = (struct inode *)inode;
1421 struct smack_known *isp;
1422
1423 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1424 isp = smk_of_inode(inode);
1425 else {
1426
1427
1428
1429 sbp = ip->i_sb;
1430 if (sbp->s_magic != SOCKFS_MAGIC)
1431 return -EOPNOTSUPP;
1432
1433 sock = SOCKET_I(ip);
1434 if (sock == NULL || sock->sk == NULL)
1435 return -EOPNOTSUPP;
1436
1437 ssp = sock->sk->sk_security;
1438
1439 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1440 isp = ssp->smk_in;
1441 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1442 isp = ssp->smk_out;
1443 else
1444 return -EOPNOTSUPP;
1445 }
1446
1447 if (alloc) {
1448 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1449 if (*buffer == NULL)
1450 return -ENOMEM;
1451 }
1452
1453 return strlen(isp->smk_known);
1454}
1455
1456
1457
1458
1459
1460
1461
1462
1463static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1464 size_t buffer_size)
1465{
1466 int len = sizeof(XATTR_NAME_SMACK);
1467
1468 if (buffer != NULL && len <= buffer_size)
1469 memcpy(buffer, XATTR_NAME_SMACK, len);
1470
1471 return len;
1472}
1473
1474
1475
1476
1477
1478
1479static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1480{
1481 struct smack_known *skp = smk_of_inode(inode);
1482
1483 *secid = skp->smk_secid;
1484}
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513static int smack_file_alloc_security(struct file *file)
1514{
1515 struct smack_known **blob = smack_file(file);
1516
1517 *blob = smk_of_current();
1518 return 0;
1519}
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531static int smack_file_ioctl(struct file *file, unsigned int cmd,
1532 unsigned long arg)
1533{
1534 int rc = 0;
1535 struct smk_audit_info ad;
1536 struct inode *inode = file_inode(file);
1537
1538 if (unlikely(IS_PRIVATE(inode)))
1539 return 0;
1540
1541 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1542 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1543
1544 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1545 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1546 rc = smk_bu_file(file, MAY_WRITE, rc);
1547 }
1548
1549 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1550 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1551 rc = smk_bu_file(file, MAY_READ, rc);
1552 }
1553
1554 return rc;
1555}
1556
1557
1558
1559
1560
1561
1562
1563
1564static int smack_file_lock(struct file *file, unsigned int cmd)
1565{
1566 struct smk_audit_info ad;
1567 int rc;
1568 struct inode *inode = file_inode(file);
1569
1570 if (unlikely(IS_PRIVATE(inode)))
1571 return 0;
1572
1573 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1574 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1575 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1576 rc = smk_bu_file(file, MAY_LOCK, rc);
1577 return rc;
1578}
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592static int smack_file_fcntl(struct file *file, unsigned int cmd,
1593 unsigned long arg)
1594{
1595 struct smk_audit_info ad;
1596 int rc = 0;
1597 struct inode *inode = file_inode(file);
1598
1599 if (unlikely(IS_PRIVATE(inode)))
1600 return 0;
1601
1602 switch (cmd) {
1603 case F_GETLK:
1604 break;
1605 case F_SETLK:
1606 case F_SETLKW:
1607 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1608 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1609 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1610 rc = smk_bu_file(file, MAY_LOCK, rc);
1611 break;
1612 case F_SETOWN:
1613 case F_SETSIG:
1614 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1615 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1616 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1617 rc = smk_bu_file(file, MAY_WRITE, rc);
1618 break;
1619 default:
1620 break;
1621 }
1622
1623 return rc;
1624}
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637static int smack_mmap_file(struct file *file,
1638 unsigned long reqprot, unsigned long prot,
1639 unsigned long flags)
1640{
1641 struct smack_known *skp;
1642 struct smack_known *mkp;
1643 struct smack_rule *srp;
1644 struct task_smack *tsp;
1645 struct smack_known *okp;
1646 struct inode_smack *isp;
1647 struct superblock_smack *sbsp;
1648 int may;
1649 int mmay;
1650 int tmay;
1651 int rc;
1652
1653 if (file == NULL)
1654 return 0;
1655
1656 if (unlikely(IS_PRIVATE(file_inode(file))))
1657 return 0;
1658
1659 isp = smack_inode(file_inode(file));
1660 if (isp->smk_mmap == NULL)
1661 return 0;
1662 sbsp = smack_superblock(file_inode(file)->i_sb);
1663 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1664 isp->smk_mmap != sbsp->smk_root)
1665 return -EACCES;
1666 mkp = isp->smk_mmap;
1667
1668 tsp = smack_cred(current_cred());
1669 skp = smk_of_current();
1670 rc = 0;
1671
1672 rcu_read_lock();
1673
1674
1675
1676
1677
1678 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1679 okp = srp->smk_object;
1680
1681
1682
1683 if (mkp->smk_known == okp->smk_known)
1684 continue;
1685
1686
1687
1688
1689 may = smk_access_entry(srp->smk_subject->smk_known,
1690 okp->smk_known,
1691 &tsp->smk_rules);
1692 if (may == -ENOENT)
1693 may = srp->smk_access;
1694 else
1695 may &= srp->smk_access;
1696
1697
1698
1699
1700 if (may == 0)
1701 continue;
1702
1703
1704
1705
1706
1707
1708 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1709 &mkp->smk_rules);
1710 if (mmay == -ENOENT) {
1711 rc = -EACCES;
1712 break;
1713 }
1714
1715
1716
1717
1718 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1719 &tsp->smk_rules);
1720 if (tmay != -ENOENT)
1721 mmay &= tmay;
1722
1723
1724
1725
1726
1727
1728 if ((may | mmay) != mmay) {
1729 rc = -EACCES;
1730 break;
1731 }
1732 }
1733
1734 rcu_read_unlock();
1735
1736 return rc;
1737}
1738
1739
1740
1741
1742
1743
1744static void smack_file_set_fowner(struct file *file)
1745{
1746 struct smack_known **blob = smack_file(file);
1747
1748 *blob = smk_of_current();
1749}
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762static int smack_file_send_sigiotask(struct task_struct *tsk,
1763 struct fown_struct *fown, int signum)
1764{
1765 struct smack_known **blob;
1766 struct smack_known *skp;
1767 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1768 const struct cred *tcred;
1769 struct file *file;
1770 int rc;
1771 struct smk_audit_info ad;
1772
1773
1774
1775
1776 file = container_of(fown, struct file, f_owner);
1777
1778
1779 blob = smack_file(file);
1780 skp = *blob;
1781 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1782 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1783
1784 rcu_read_lock();
1785 tcred = __task_cred(tsk);
1786 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1787 rc = 0;
1788 rcu_read_unlock();
1789
1790 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1791 smk_ad_setfield_u_tsk(&ad, tsk);
1792 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1793 return rc;
1794}
1795
1796
1797
1798
1799
1800
1801
1802static int smack_file_receive(struct file *file)
1803{
1804 int rc;
1805 int may = 0;
1806 struct smk_audit_info ad;
1807 struct inode *inode = file_inode(file);
1808 struct socket *sock;
1809 struct task_smack *tsp;
1810 struct socket_smack *ssp;
1811
1812 if (unlikely(IS_PRIVATE(inode)))
1813 return 0;
1814
1815 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1816 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1817
1818 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1819 sock = SOCKET_I(inode);
1820 ssp = sock->sk->sk_security;
1821 tsp = smack_cred(current_cred());
1822
1823
1824
1825
1826
1827
1828 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1829 rc = smk_bu_file(file, may, rc);
1830 if (rc < 0)
1831 return rc;
1832 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1833 rc = smk_bu_file(file, may, rc);
1834 return rc;
1835 }
1836
1837
1838
1839 if (file->f_mode & FMODE_READ)
1840 may = MAY_READ;
1841 if (file->f_mode & FMODE_WRITE)
1842 may |= MAY_WRITE;
1843
1844 rc = smk_curacc(smk_of_inode(inode), may, &ad);
1845 rc = smk_bu_file(file, may, rc);
1846 return rc;
1847}
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860static int smack_file_open(struct file *file)
1861{
1862 struct task_smack *tsp = smack_cred(file->f_cred);
1863 struct inode *inode = file_inode(file);
1864 struct smk_audit_info ad;
1865 int rc;
1866
1867 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1868 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1869 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1870 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1871
1872 return rc;
1873}
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1889{
1890 init_task_smack(smack_cred(cred), NULL, NULL);
1891 return 0;
1892}
1893
1894
1895
1896
1897
1898
1899
1900static void smack_cred_free(struct cred *cred)
1901{
1902 struct task_smack *tsp = smack_cred(cred);
1903 struct smack_rule *rp;
1904 struct list_head *l;
1905 struct list_head *n;
1906
1907 smk_destroy_label_list(&tsp->smk_relabel);
1908
1909 list_for_each_safe(l, n, &tsp->smk_rules) {
1910 rp = list_entry(l, struct smack_rule, list);
1911 list_del(&rp->list);
1912 kmem_cache_free(smack_rule_cache, rp);
1913 }
1914}
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924static int smack_cred_prepare(struct cred *new, const struct cred *old,
1925 gfp_t gfp)
1926{
1927 struct task_smack *old_tsp = smack_cred(old);
1928 struct task_smack *new_tsp = smack_cred(new);
1929 int rc;
1930
1931 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1932
1933 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1934 if (rc != 0)
1935 return rc;
1936
1937 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1938 gfp);
1939 return rc;
1940}
1941
1942
1943
1944
1945
1946
1947
1948
1949static void smack_cred_transfer(struct cred *new, const struct cred *old)
1950{
1951 struct task_smack *old_tsp = smack_cred(old);
1952 struct task_smack *new_tsp = smack_cred(new);
1953
1954 new_tsp->smk_task = old_tsp->smk_task;
1955 new_tsp->smk_forked = old_tsp->smk_task;
1956 mutex_init(&new_tsp->smk_rules_lock);
1957 INIT_LIST_HEAD(&new_tsp->smk_rules);
1958
1959
1960}
1961
1962
1963
1964
1965
1966
1967
1968
1969static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1970{
1971 struct smack_known *skp;
1972
1973 rcu_read_lock();
1974 skp = smk_of_task(smack_cred(cred));
1975 *secid = skp->smk_secid;
1976 rcu_read_unlock();
1977}
1978
1979
1980
1981
1982
1983
1984
1985
1986static int smack_kernel_act_as(struct cred *new, u32 secid)
1987{
1988 struct task_smack *new_tsp = smack_cred(new);
1989
1990 new_tsp->smk_task = smack_from_secid(secid);
1991 return 0;
1992}
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002static int smack_kernel_create_files_as(struct cred *new,
2003 struct inode *inode)
2004{
2005 struct inode_smack *isp = smack_inode(inode);
2006 struct task_smack *tsp = smack_cred(new);
2007
2008 tsp->smk_forked = isp->smk_inode;
2009 tsp->smk_task = tsp->smk_forked;
2010 return 0;
2011}
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021static int smk_curacc_on_task(struct task_struct *p, int access,
2022 const char *caller)
2023{
2024 struct smk_audit_info ad;
2025 struct smack_known *skp = smk_of_task_struct_obj(p);
2026 int rc;
2027
2028 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2029 smk_ad_setfield_u_tsk(&ad, p);
2030 rc = smk_curacc(skp, access, &ad);
2031 rc = smk_bu_task(p, access, rc);
2032 return rc;
2033}
2034
2035
2036
2037
2038
2039
2040
2041
2042static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2043{
2044 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2045}
2046
2047
2048
2049
2050
2051
2052
2053static int smack_task_getpgid(struct task_struct *p)
2054{
2055 return smk_curacc_on_task(p, MAY_READ, __func__);
2056}
2057
2058
2059
2060
2061
2062
2063
2064static int smack_task_getsid(struct task_struct *p)
2065{
2066 return smk_curacc_on_task(p, MAY_READ, __func__);
2067}
2068
2069
2070
2071
2072
2073
2074
2075static void smack_current_getsecid_subj(u32 *secid)
2076{
2077 struct smack_known *skp = smk_of_current();
2078
2079 *secid = skp->smk_secid;
2080}
2081
2082
2083
2084
2085
2086
2087
2088
2089static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2090{
2091 struct smack_known *skp = smk_of_task_struct_obj(p);
2092
2093 *secid = skp->smk_secid;
2094}
2095
2096
2097
2098
2099
2100
2101
2102
2103static int smack_task_setnice(struct task_struct *p, int nice)
2104{
2105 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2106}
2107
2108
2109
2110
2111
2112
2113
2114
2115static int smack_task_setioprio(struct task_struct *p, int ioprio)
2116{
2117 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2118}
2119
2120
2121
2122
2123
2124
2125
2126static int smack_task_getioprio(struct task_struct *p)
2127{
2128 return smk_curacc_on_task(p, MAY_READ, __func__);
2129}
2130
2131
2132
2133
2134
2135
2136
2137static int smack_task_setscheduler(struct task_struct *p)
2138{
2139 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2140}
2141
2142
2143
2144
2145
2146
2147
2148static int smack_task_getscheduler(struct task_struct *p)
2149{
2150 return smk_curacc_on_task(p, MAY_READ, __func__);
2151}
2152
2153
2154
2155
2156
2157
2158
2159static int smack_task_movememory(struct task_struct *p)
2160{
2161 return smk_curacc_on_task(p, MAY_WRITE, __func__);
2162}
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2175 int sig, const struct cred *cred)
2176{
2177 struct smk_audit_info ad;
2178 struct smack_known *skp;
2179 struct smack_known *tkp = smk_of_task_struct_obj(p);
2180 int rc;
2181
2182 if (!sig)
2183 return 0;
2184
2185 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2186 smk_ad_setfield_u_tsk(&ad, p);
2187
2188
2189
2190
2191 if (cred == NULL) {
2192 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2193 rc = smk_bu_task(p, MAY_DELIVER, rc);
2194 return rc;
2195 }
2196
2197
2198
2199
2200
2201 skp = smk_of_task(smack_cred(cred));
2202 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2203 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2204 return rc;
2205}
2206
2207
2208
2209
2210
2211
2212
2213
2214static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2215{
2216 struct inode_smack *isp = smack_inode(inode);
2217 struct smack_known *skp = smk_of_task_struct_obj(p);
2218
2219 isp->smk_inode = skp;
2220 isp->smk_flags |= SMK_INODE_INSTANT;
2221}
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2238{
2239 struct smack_known *skp = smk_of_current();
2240 struct socket_smack *ssp;
2241
2242 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2243 if (ssp == NULL)
2244 return -ENOMEM;
2245
2246
2247
2248
2249 if (unlikely(current->flags & PF_KTHREAD)) {
2250 ssp->smk_in = &smack_known_web;
2251 ssp->smk_out = &smack_known_web;
2252 } else {
2253 ssp->smk_in = skp;
2254 ssp->smk_out = skp;
2255 }
2256 ssp->smk_packet = NULL;
2257
2258 sk->sk_security = ssp;
2259
2260 return 0;
2261}
2262
2263
2264
2265
2266
2267
2268
2269static void smack_sk_free_security(struct sock *sk)
2270{
2271#ifdef SMACK_IPV6_PORT_LABELING
2272 struct smk_port_label *spp;
2273
2274 if (sk->sk_family == PF_INET6) {
2275 rcu_read_lock();
2276 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2277 if (spp->smk_sock != sk)
2278 continue;
2279 spp->smk_can_reuse = 1;
2280 break;
2281 }
2282 rcu_read_unlock();
2283 }
2284#endif
2285 kfree(sk->sk_security);
2286}
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2301{
2302 struct smk_net4addr *snp;
2303 struct in_addr *siap = &sip->sin_addr;
2304
2305 if (siap->s_addr == 0)
2306 return NULL;
2307
2308 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2309
2310
2311
2312
2313
2314 if (snp->smk_host.s_addr ==
2315 (siap->s_addr & snp->smk_mask.s_addr))
2316 return snp->smk_label;
2317
2318 return NULL;
2319}
2320
2321
2322
2323
2324
2325
2326
2327static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2328{
2329 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2330 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2331
2332 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2333 ntohs(be16p[7]) == 1)
2334 return true;
2335 return false;
2336}
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2351{
2352 struct smk_net6addr *snp;
2353 struct in6_addr *sap = &sip->sin6_addr;
2354 int i;
2355 int found = 0;
2356
2357
2358
2359
2360 if (smk_ipv6_localhost(sip))
2361 return NULL;
2362
2363 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2364
2365
2366
2367
2368 if (snp->smk_label == NULL)
2369 continue;
2370
2371
2372
2373
2374
2375 for (found = 1, i = 0; i < 8; i++) {
2376 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2377 snp->smk_host.s6_addr16[i]) {
2378 found = 0;
2379 break;
2380 }
2381 }
2382 if (found)
2383 return snp->smk_label;
2384 }
2385
2386 return NULL;
2387}
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397static int smack_netlbl_add(struct sock *sk)
2398{
2399 struct socket_smack *ssp = sk->sk_security;
2400 struct smack_known *skp = ssp->smk_out;
2401 int rc;
2402
2403 local_bh_disable();
2404 bh_lock_sock_nested(sk);
2405
2406 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2407 switch (rc) {
2408 case 0:
2409 ssp->smk_state = SMK_NETLBL_LABELED;
2410 break;
2411 case -EDESTADDRREQ:
2412 ssp->smk_state = SMK_NETLBL_REQSKB;
2413 rc = 0;
2414 break;
2415 }
2416
2417 bh_unlock_sock(sk);
2418 local_bh_enable();
2419
2420 return rc;
2421}
2422
2423
2424
2425
2426
2427
2428
2429static void smack_netlbl_delete(struct sock *sk)
2430{
2431 struct socket_smack *ssp = sk->sk_security;
2432
2433
2434
2435
2436 if (ssp->smk_state != SMK_NETLBL_LABELED)
2437 return;
2438
2439 local_bh_disable();
2440 bh_lock_sock_nested(sk);
2441 netlbl_sock_delattr(sk);
2442 bh_unlock_sock(sk);
2443 local_bh_enable();
2444 ssp->smk_state = SMK_NETLBL_UNLABELED;
2445}
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
2459{
2460 struct smack_known *skp;
2461 int rc = 0;
2462 struct smack_known *hkp;
2463 struct socket_smack *ssp = sk->sk_security;
2464 struct smk_audit_info ad;
2465
2466 rcu_read_lock();
2467 hkp = smack_ipv4host_label(sap);
2468 if (hkp != NULL) {
2469#ifdef CONFIG_AUDIT
2470 struct lsm_network_audit net;
2471
2472 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2473 ad.a.u.net->family = sap->sin_family;
2474 ad.a.u.net->dport = sap->sin_port;
2475 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2476#endif
2477 skp = ssp->smk_out;
2478 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2479 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2480
2481
2482
2483 if (!rc)
2484 smack_netlbl_delete(sk);
2485 }
2486 rcu_read_unlock();
2487
2488 return rc;
2489}
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500static int smk_ipv6_check(struct smack_known *subject,
2501 struct smack_known *object,
2502 struct sockaddr_in6 *address, int act)
2503{
2504#ifdef CONFIG_AUDIT
2505 struct lsm_network_audit net;
2506#endif
2507 struct smk_audit_info ad;
2508 int rc;
2509
2510#ifdef CONFIG_AUDIT
2511 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2512 ad.a.u.net->family = PF_INET6;
2513 ad.a.u.net->dport = address->sin6_port;
2514 if (act == SMK_RECEIVING)
2515 ad.a.u.net->v6info.saddr = address->sin6_addr;
2516 else
2517 ad.a.u.net->v6info.daddr = address->sin6_addr;
2518#endif
2519 rc = smk_access(subject, object, MAY_WRITE, &ad);
2520 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2521 return rc;
2522}
2523
2524#ifdef SMACK_IPV6_PORT_LABELING
2525
2526
2527
2528
2529
2530
2531
2532static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2533{
2534 struct sock *sk = sock->sk;
2535 struct sockaddr_in6 *addr6;
2536 struct socket_smack *ssp = sock->sk->sk_security;
2537 struct smk_port_label *spp;
2538 unsigned short port = 0;
2539
2540 if (address == NULL) {
2541
2542
2543
2544
2545
2546 rcu_read_lock();
2547 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2548 if (sk != spp->smk_sock)
2549 continue;
2550 spp->smk_in = ssp->smk_in;
2551 spp->smk_out = ssp->smk_out;
2552 rcu_read_unlock();
2553 return;
2554 }
2555
2556
2557
2558
2559 rcu_read_unlock();
2560 return;
2561 }
2562
2563 addr6 = (struct sockaddr_in6 *)address;
2564 port = ntohs(addr6->sin6_port);
2565
2566
2567
2568 if (port == 0)
2569 return;
2570
2571
2572
2573
2574
2575 rcu_read_lock();
2576 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2577 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2578 continue;
2579 if (spp->smk_can_reuse != 1) {
2580 rcu_read_unlock();
2581 return;
2582 }
2583 spp->smk_port = port;
2584 spp->smk_sock = sk;
2585 spp->smk_in = ssp->smk_in;
2586 spp->smk_out = ssp->smk_out;
2587 spp->smk_can_reuse = 0;
2588 rcu_read_unlock();
2589 return;
2590 }
2591 rcu_read_unlock();
2592
2593
2594
2595 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2596 if (spp == NULL)
2597 return;
2598
2599 spp->smk_port = port;
2600 spp->smk_sock = sk;
2601 spp->smk_in = ssp->smk_in;
2602 spp->smk_out = ssp->smk_out;
2603 spp->smk_sock_type = sock->type;
2604 spp->smk_can_reuse = 0;
2605
2606 mutex_lock(&smack_ipv6_lock);
2607 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2608 mutex_unlock(&smack_ipv6_lock);
2609 return;
2610}
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2621 int act)
2622{
2623 struct smk_port_label *spp;
2624 struct socket_smack *ssp = sk->sk_security;
2625 struct smack_known *skp = NULL;
2626 unsigned short port;
2627 struct smack_known *object;
2628
2629 if (act == SMK_RECEIVING) {
2630 skp = smack_ipv6host_label(address);
2631 object = ssp->smk_in;
2632 } else {
2633 skp = ssp->smk_out;
2634 object = smack_ipv6host_label(address);
2635 }
2636
2637
2638
2639
2640 if (skp != NULL && object != NULL)
2641 return smk_ipv6_check(skp, object, address, act);
2642 if (skp == NULL)
2643 skp = smack_net_ambient;
2644 if (object == NULL)
2645 object = smack_net_ambient;
2646
2647
2648
2649
2650 if (!smk_ipv6_localhost(address))
2651 return smk_ipv6_check(skp, object, address, act);
2652
2653
2654
2655
2656 if (act == SMK_RECEIVING)
2657 return 0;
2658
2659 port = ntohs(address->sin6_port);
2660 rcu_read_lock();
2661 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2662 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2663 continue;
2664 object = spp->smk_in;
2665 if (act == SMK_CONNECTING)
2666 ssp->smk_packet = spp->smk_out;
2667 break;
2668 }
2669 rcu_read_unlock();
2670
2671 return smk_ipv6_check(skp, object, address, act);
2672}
2673#endif
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687static int smack_inode_setsecurity(struct inode *inode, const char *name,
2688 const void *value, size_t size, int flags)
2689{
2690 struct smack_known *skp;
2691 struct inode_smack *nsp = smack_inode(inode);
2692 struct socket_smack *ssp;
2693 struct socket *sock;
2694 int rc = 0;
2695
2696 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2697 return -EINVAL;
2698
2699 skp = smk_import_entry(value, size);
2700 if (IS_ERR(skp))
2701 return PTR_ERR(skp);
2702
2703 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2704 nsp->smk_inode = skp;
2705 nsp->smk_flags |= SMK_INODE_INSTANT;
2706 return 0;
2707 }
2708
2709
2710
2711 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2712 return -EOPNOTSUPP;
2713
2714 sock = SOCKET_I(inode);
2715 if (sock == NULL || sock->sk == NULL)
2716 return -EOPNOTSUPP;
2717
2718 ssp = sock->sk->sk_security;
2719
2720 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2721 ssp->smk_in = skp;
2722 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2723 ssp->smk_out = skp;
2724 if (sock->sk->sk_family == PF_INET) {
2725 rc = smack_netlbl_add(sock->sk);
2726 if (rc != 0)
2727 printk(KERN_WARNING
2728 "Smack: \"%s\" netlbl error %d.\n",
2729 __func__, -rc);
2730 }
2731 } else
2732 return -EOPNOTSUPP;
2733
2734#ifdef SMACK_IPV6_PORT_LABELING
2735 if (sock->sk->sk_family == PF_INET6)
2736 smk_ipv6_port_label(sock, NULL);
2737#endif
2738
2739 return 0;
2740}
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754static int smack_socket_post_create(struct socket *sock, int family,
2755 int type, int protocol, int kern)
2756{
2757 struct socket_smack *ssp;
2758
2759 if (sock->sk == NULL)
2760 return 0;
2761
2762
2763
2764
2765 if (unlikely(current->flags & PF_KTHREAD)) {
2766 ssp = sock->sk->sk_security;
2767 ssp->smk_in = &smack_known_web;
2768 ssp->smk_out = &smack_known_web;
2769 }
2770
2771 if (family != PF_INET)
2772 return 0;
2773
2774
2775
2776 return smack_netlbl_add(sock->sk);
2777}
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788static int smack_socket_socketpair(struct socket *socka,
2789 struct socket *sockb)
2790{
2791 struct socket_smack *asp = socka->sk->sk_security;
2792 struct socket_smack *bsp = sockb->sk->sk_security;
2793
2794 asp->smk_packet = bsp->smk_out;
2795 bsp->smk_packet = asp->smk_out;
2796
2797 return 0;
2798}
2799
2800#ifdef SMACK_IPV6_PORT_LABELING
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2812 int addrlen)
2813{
2814 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2815 if (addrlen < SIN6_LEN_RFC2133 ||
2816 address->sa_family != AF_INET6)
2817 return -EINVAL;
2818 smk_ipv6_port_label(sock, address);
2819 }
2820 return 0;
2821}
2822#endif
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2835 int addrlen)
2836{
2837 int rc = 0;
2838
2839 if (sock->sk == NULL)
2840 return 0;
2841 if (sock->sk->sk_family != PF_INET &&
2842 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2843 return 0;
2844 if (addrlen < offsetofend(struct sockaddr, sa_family))
2845 return 0;
2846 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2847 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2848 struct smack_known *rsp = NULL;
2849
2850 if (addrlen < SIN6_LEN_RFC2133)
2851 return 0;
2852 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2853 rsp = smack_ipv6host_label(sip);
2854 if (rsp != NULL) {
2855 struct socket_smack *ssp = sock->sk->sk_security;
2856
2857 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2858 SMK_CONNECTING);
2859 }
2860#ifdef SMACK_IPV6_PORT_LABELING
2861 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2862#endif
2863
2864 return rc;
2865 }
2866 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2867 return 0;
2868 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
2869 return rc;
2870}
2871
2872
2873
2874
2875
2876
2877
2878static int smack_flags_to_may(int flags)
2879{
2880 int may = 0;
2881
2882 if (flags & S_IRUGO)
2883 may |= MAY_READ;
2884 if (flags & S_IWUGO)
2885 may |= MAY_WRITE;
2886 if (flags & S_IXUGO)
2887 may |= MAY_EXEC;
2888
2889 return may;
2890}
2891
2892
2893
2894
2895
2896
2897
2898static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2899{
2900 struct smack_known **blob = smack_msg_msg(msg);
2901
2902 *blob = smk_of_current();
2903 return 0;
2904}
2905
2906
2907
2908
2909
2910
2911
2912static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2913{
2914 struct smack_known **blob = smack_ipc(isp);
2915
2916 return *blob;
2917}
2918
2919
2920
2921
2922
2923
2924
2925static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2926{
2927 struct smack_known **blob = smack_ipc(isp);
2928
2929 *blob = smk_of_current();
2930 return 0;
2931}
2932
2933
2934
2935
2936
2937
2938
2939
2940static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2941{
2942 struct smack_known *ssp = smack_of_ipc(isp);
2943 struct smk_audit_info ad;
2944 int rc;
2945
2946#ifdef CONFIG_AUDIT
2947 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2948 ad.a.u.ipc_id = isp->id;
2949#endif
2950 rc = smk_curacc(ssp, access, &ad);
2951 rc = smk_bu_current("shm", ssp, access, rc);
2952 return rc;
2953}
2954
2955
2956
2957
2958
2959
2960
2961
2962static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2963{
2964 int may;
2965
2966 may = smack_flags_to_may(shmflg);
2967 return smk_curacc_shm(isp, may);
2968}
2969
2970
2971
2972
2973
2974
2975
2976
2977static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2978{
2979 int may;
2980
2981 switch (cmd) {
2982 case IPC_STAT:
2983 case SHM_STAT:
2984 case SHM_STAT_ANY:
2985 may = MAY_READ;
2986 break;
2987 case IPC_SET:
2988 case SHM_LOCK:
2989 case SHM_UNLOCK:
2990 case IPC_RMID:
2991 may = MAY_READWRITE;
2992 break;
2993 case IPC_INFO:
2994 case SHM_INFO:
2995
2996
2997
2998 return 0;
2999 default:
3000 return -EINVAL;
3001 }
3002 return smk_curacc_shm(isp, may);
3003}
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
3014 int shmflg)
3015{
3016 int may;
3017
3018 may = smack_flags_to_may(shmflg);
3019 return smk_curacc_shm(isp, may);
3020}
3021
3022
3023
3024
3025
3026
3027
3028
3029static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3030{
3031 struct smack_known *ssp = smack_of_ipc(isp);
3032 struct smk_audit_info ad;
3033 int rc;
3034
3035#ifdef CONFIG_AUDIT
3036 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3037 ad.a.u.ipc_id = isp->id;
3038#endif
3039 rc = smk_curacc(ssp, access, &ad);
3040 rc = smk_bu_current("sem", ssp, access, rc);
3041 return rc;
3042}
3043
3044
3045
3046
3047
3048
3049
3050
3051static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3052{
3053 int may;
3054
3055 may = smack_flags_to_may(semflg);
3056 return smk_curacc_sem(isp, may);
3057}
3058
3059
3060
3061
3062
3063
3064
3065
3066static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3067{
3068 int may;
3069
3070 switch (cmd) {
3071 case GETPID:
3072 case GETNCNT:
3073 case GETZCNT:
3074 case GETVAL:
3075 case GETALL:
3076 case IPC_STAT:
3077 case SEM_STAT:
3078 case SEM_STAT_ANY:
3079 may = MAY_READ;
3080 break;
3081 case SETVAL:
3082 case SETALL:
3083 case IPC_RMID:
3084 case IPC_SET:
3085 may = MAY_READWRITE;
3086 break;
3087 case IPC_INFO:
3088 case SEM_INFO:
3089
3090
3091
3092 return 0;
3093 default:
3094 return -EINVAL;
3095 }
3096
3097 return smk_curacc_sem(isp, may);
3098}
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3112 unsigned nsops, int alter)
3113{
3114 return smk_curacc_sem(isp, MAY_READWRITE);
3115}
3116
3117
3118
3119
3120
3121
3122
3123
3124static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3125{
3126 struct smack_known *msp = smack_of_ipc(isp);
3127 struct smk_audit_info ad;
3128 int rc;
3129
3130#ifdef CONFIG_AUDIT
3131 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3132 ad.a.u.ipc_id = isp->id;
3133#endif
3134 rc = smk_curacc(msp, access, &ad);
3135 rc = smk_bu_current("msq", msp, access, rc);
3136 return rc;
3137}
3138
3139
3140
3141
3142
3143
3144
3145
3146static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3147{
3148 int may;
3149
3150 may = smack_flags_to_may(msqflg);
3151 return smk_curacc_msq(isp, may);
3152}
3153
3154
3155
3156
3157
3158
3159
3160
3161static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3162{
3163 int may;
3164
3165 switch (cmd) {
3166 case IPC_STAT:
3167 case MSG_STAT:
3168 case MSG_STAT_ANY:
3169 may = MAY_READ;
3170 break;
3171 case IPC_SET:
3172 case IPC_RMID:
3173 may = MAY_READWRITE;
3174 break;
3175 case IPC_INFO:
3176 case MSG_INFO:
3177
3178
3179
3180 return 0;
3181 default:
3182 return -EINVAL;
3183 }
3184
3185 return smk_curacc_msq(isp, may);
3186}
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3197 int msqflg)
3198{
3199 int may;
3200
3201 may = smack_flags_to_may(msqflg);
3202 return smk_curacc_msq(isp, may);
3203}
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp,
3216 struct msg_msg *msg,
3217 struct task_struct *target, long type,
3218 int mode)
3219{
3220 return smk_curacc_msq(isp, MAY_READWRITE);
3221}
3222
3223
3224
3225
3226
3227
3228
3229
3230static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3231{
3232 struct smack_known **blob = smack_ipc(ipp);
3233 struct smack_known *iskp = *blob;
3234 int may = smack_flags_to_may(flag);
3235 struct smk_audit_info ad;
3236 int rc;
3237
3238#ifdef CONFIG_AUDIT
3239 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3240 ad.a.u.ipc_id = ipp->id;
3241#endif
3242 rc = smk_curacc(iskp, may, &ad);
3243 rc = smk_bu_current("svipc", iskp, may, rc);
3244 return rc;
3245}
3246
3247
3248
3249
3250
3251
3252static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3253{
3254 struct smack_known **blob = smack_ipc(ipp);
3255 struct smack_known *iskp = *blob;
3256
3257 *secid = iskp->smk_secid;
3258}
3259
3260
3261
3262
3263
3264
3265
3266
3267static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3268{
3269 struct super_block *sbp;
3270 struct superblock_smack *sbsp;
3271 struct inode_smack *isp;
3272 struct smack_known *skp;
3273 struct smack_known *ckp = smk_of_current();
3274 struct smack_known *final;
3275 char trattr[TRANS_TRUE_SIZE];
3276 int transflag = 0;
3277 int rc;
3278 struct dentry *dp;
3279
3280 if (inode == NULL)
3281 return;
3282
3283 isp = smack_inode(inode);
3284
3285
3286
3287
3288
3289 if (isp->smk_flags & SMK_INODE_INSTANT)
3290 return;
3291
3292 sbp = inode->i_sb;
3293 sbsp = smack_superblock(sbp);
3294
3295
3296
3297
3298 final = sbsp->smk_default;
3299
3300
3301
3302
3303
3304
3305
3306 if (opt_dentry->d_parent == opt_dentry) {
3307 switch (sbp->s_magic) {
3308 case CGROUP_SUPER_MAGIC:
3309 case CGROUP2_SUPER_MAGIC:
3310
3311
3312
3313
3314
3315 sbsp->smk_root = &smack_known_star;
3316 sbsp->smk_default = &smack_known_star;
3317 isp->smk_inode = sbsp->smk_root;
3318 break;
3319 case TMPFS_MAGIC:
3320
3321
3322
3323
3324 isp->smk_inode = smk_of_current();
3325 break;
3326 case PIPEFS_MAGIC:
3327 isp->smk_inode = smk_of_current();
3328 break;
3329 case SOCKFS_MAGIC:
3330
3331
3332
3333
3334 isp->smk_inode = &smack_known_star;
3335 break;
3336 default:
3337 isp->smk_inode = sbsp->smk_root;
3338 break;
3339 }
3340 isp->smk_flags |= SMK_INODE_INSTANT;
3341 return;
3342 }
3343
3344
3345
3346
3347
3348
3349
3350 switch (sbp->s_magic) {
3351 case SMACK_MAGIC:
3352 case CGROUP_SUPER_MAGIC:
3353 case CGROUP2_SUPER_MAGIC:
3354
3355
3356
3357
3358
3359
3360
3361 final = &smack_known_star;
3362 break;
3363 case DEVPTS_SUPER_MAGIC:
3364
3365
3366
3367
3368
3369 final = ckp;
3370 break;
3371 case PROC_SUPER_MAGIC:
3372
3373
3374
3375
3376 break;
3377 case TMPFS_MAGIC:
3378
3379
3380
3381
3382
3383 final = &smack_known_star;
3384
3385
3386
3387
3388
3389
3390 fallthrough;
3391 default:
3392
3393
3394
3395
3396
3397
3398
3399
3400 if (S_ISSOCK(inode->i_mode)) {
3401 final = &smack_known_star;
3402 break;
3403 }
3404
3405
3406
3407
3408
3409
3410 if (!(inode->i_opflags & IOP_XATTR))
3411 break;
3412
3413
3414
3415 dp = dget(opt_dentry);
3416 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3417 if (!IS_ERR_OR_NULL(skp))
3418 final = skp;
3419
3420
3421
3422
3423 if (S_ISDIR(inode->i_mode)) {
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433 if (isp->smk_flags & SMK_INODE_CHANGED) {
3434 isp->smk_flags &= ~SMK_INODE_CHANGED;
3435 rc = __vfs_setxattr(&init_user_ns, dp, inode,
3436 XATTR_NAME_SMACKTRANSMUTE,
3437 TRANS_TRUE, TRANS_TRUE_SIZE,
3438 0);
3439 } else {
3440 rc = __vfs_getxattr(dp, inode,
3441 XATTR_NAME_SMACKTRANSMUTE, trattr,
3442 TRANS_TRUE_SIZE);
3443 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3444 TRANS_TRUE_SIZE) != 0)
3445 rc = -EINVAL;
3446 }
3447 if (rc >= 0)
3448 transflag = SMK_INODE_TRANSMUTE;
3449 }
3450
3451
3452
3453 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3454 if (IS_ERR(skp) || skp == &smack_known_star ||
3455 skp == &smack_known_web)
3456 skp = NULL;
3457 isp->smk_task = skp;
3458
3459 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3460 if (IS_ERR(skp) || skp == &smack_known_star ||
3461 skp == &smack_known_web)
3462 skp = NULL;
3463 isp->smk_mmap = skp;
3464
3465 dput(dp);
3466 break;
3467 }
3468
3469 if (final == NULL)
3470 isp->smk_inode = ckp;
3471 else
3472 isp->smk_inode = final;
3473
3474 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3475
3476 return;
3477}
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3490{
3491 struct smack_known *skp = smk_of_task_struct_obj(p);
3492 char *cp;
3493 int slen;
3494
3495 if (strcmp(name, "current") != 0)
3496 return -EINVAL;
3497
3498 cp = kstrdup(skp->smk_known, GFP_KERNEL);
3499 if (cp == NULL)
3500 return -ENOMEM;
3501
3502 slen = strlen(cp);
3503 *value = cp;
3504 return slen;
3505}
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518static int smack_setprocattr(const char *name, void *value, size_t size)
3519{
3520 struct task_smack *tsp = smack_cred(current_cred());
3521 struct cred *new;
3522 struct smack_known *skp;
3523 struct smack_known_list_elem *sklep;
3524 int rc;
3525
3526 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3527 return -EPERM;
3528
3529 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3530 return -EINVAL;
3531
3532 if (strcmp(name, "current") != 0)
3533 return -EINVAL;
3534
3535 skp = smk_import_entry(value, size);
3536 if (IS_ERR(skp))
3537 return PTR_ERR(skp);
3538
3539
3540
3541
3542
3543 if (skp == &smack_known_web || skp == &smack_known_star)
3544 return -EINVAL;
3545
3546 if (!smack_privileged(CAP_MAC_ADMIN)) {
3547 rc = -EPERM;
3548 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3549 if (sklep->smk_label == skp) {
3550 rc = 0;
3551 break;
3552 }
3553 if (rc)
3554 return rc;
3555 }
3556
3557 new = prepare_creds();
3558 if (new == NULL)
3559 return -ENOMEM;
3560
3561 tsp = smack_cred(new);
3562 tsp->smk_task = skp;
3563
3564
3565
3566 smk_destroy_label_list(&tsp->smk_relabel);
3567
3568 commit_creds(new);
3569 return size;
3570}
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581static int smack_unix_stream_connect(struct sock *sock,
3582 struct sock *other, struct sock *newsk)
3583{
3584 struct smack_known *skp;
3585 struct smack_known *okp;
3586 struct socket_smack *ssp = sock->sk_security;
3587 struct socket_smack *osp = other->sk_security;
3588 struct socket_smack *nsp = newsk->sk_security;
3589 struct smk_audit_info ad;
3590 int rc = 0;
3591#ifdef CONFIG_AUDIT
3592 struct lsm_network_audit net;
3593#endif
3594
3595 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3596 skp = ssp->smk_out;
3597 okp = osp->smk_in;
3598#ifdef CONFIG_AUDIT
3599 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3600 smk_ad_setfield_u_net_sk(&ad, other);
3601#endif
3602 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3603 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3604 if (rc == 0) {
3605 okp = osp->smk_out;
3606 skp = ssp->smk_in;
3607 rc = smk_access(okp, skp, MAY_WRITE, &ad);
3608 rc = smk_bu_note("UDS connect", okp, skp,
3609 MAY_WRITE, rc);
3610 }
3611 }
3612
3613
3614
3615
3616 if (rc == 0) {
3617 nsp->smk_packet = ssp->smk_out;
3618 ssp->smk_packet = osp->smk_out;
3619 }
3620
3621 return rc;
3622}
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632static int smack_unix_may_send(struct socket *sock, struct socket *other)
3633{
3634 struct socket_smack *ssp = sock->sk->sk_security;
3635 struct socket_smack *osp = other->sk->sk_security;
3636 struct smk_audit_info ad;
3637 int rc;
3638
3639#ifdef CONFIG_AUDIT
3640 struct lsm_network_audit net;
3641
3642 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3643 smk_ad_setfield_u_net_sk(&ad, other->sk);
3644#endif
3645
3646 if (smack_privileged(CAP_MAC_OVERRIDE))
3647 return 0;
3648
3649 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3650 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3651 return rc;
3652}
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3665 int size)
3666{
3667 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3668#if IS_ENABLED(CONFIG_IPV6)
3669 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3670#endif
3671#ifdef SMACK_IPV6_SECMARK_LABELING
3672 struct socket_smack *ssp = sock->sk->sk_security;
3673 struct smack_known *rsp;
3674#endif
3675 int rc = 0;
3676
3677
3678
3679
3680 if (sip == NULL)
3681 return 0;
3682
3683 switch (sock->sk->sk_family) {
3684 case AF_INET:
3685 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3686 sip->sin_family != AF_INET)
3687 return -EINVAL;
3688 rc = smk_ipv4_check(sock->sk, sip);
3689 break;
3690#if IS_ENABLED(CONFIG_IPV6)
3691 case AF_INET6:
3692 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3693 sap->sin6_family != AF_INET6)
3694 return -EINVAL;
3695#ifdef SMACK_IPV6_SECMARK_LABELING
3696 rsp = smack_ipv6host_label(sap);
3697 if (rsp != NULL)
3698 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3699 SMK_CONNECTING);
3700#endif
3701#ifdef SMACK_IPV6_PORT_LABELING
3702 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3703#endif
3704#endif
3705 break;
3706 }
3707 return rc;
3708}
3709
3710
3711
3712
3713
3714
3715
3716
3717static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3718 struct socket_smack *ssp)
3719{
3720 struct smack_known *skp;
3721 int found = 0;
3722 int acat;
3723 int kcat;
3724
3725
3726
3727
3728 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3729 return (struct smack_known *)sap->cache->data;
3730
3731 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3732
3733
3734
3735 return smack_from_secid(sap->attr.secid);
3736
3737 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748 rcu_read_lock();
3749 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3750 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3751 continue;
3752
3753
3754
3755 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3756 if ((skp->smk_netlabel.flags &
3757 NETLBL_SECATTR_MLS_CAT) == 0)
3758 found = 1;
3759 break;
3760 }
3761 for (acat = -1, kcat = -1; acat == kcat; ) {
3762 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3763 acat + 1);
3764 kcat = netlbl_catmap_walk(
3765 skp->smk_netlabel.attr.mls.cat,
3766 kcat + 1);
3767 if (acat < 0 || kcat < 0)
3768 break;
3769 }
3770 if (acat == kcat) {
3771 found = 1;
3772 break;
3773 }
3774 }
3775 rcu_read_unlock();
3776
3777 if (found)
3778 return skp;
3779
3780 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3781 return &smack_known_web;
3782 return &smack_known_star;
3783 }
3784
3785
3786
3787
3788
3789 return smack_net_ambient;
3790}
3791
3792#if IS_ENABLED(CONFIG_IPV6)
3793static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3794{
3795 u8 nexthdr;
3796 int offset;
3797 int proto = -EINVAL;
3798 struct ipv6hdr _ipv6h;
3799 struct ipv6hdr *ip6;
3800 __be16 frag_off;
3801 struct tcphdr _tcph, *th;
3802 struct udphdr _udph, *uh;
3803 struct dccp_hdr _dccph, *dh;
3804
3805 sip->sin6_port = 0;
3806
3807 offset = skb_network_offset(skb);
3808 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3809 if (ip6 == NULL)
3810 return -EINVAL;
3811 sip->sin6_addr = ip6->saddr;
3812
3813 nexthdr = ip6->nexthdr;
3814 offset += sizeof(_ipv6h);
3815 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3816 if (offset < 0)
3817 return -EINVAL;
3818
3819 proto = nexthdr;
3820 switch (proto) {
3821 case IPPROTO_TCP:
3822 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3823 if (th != NULL)
3824 sip->sin6_port = th->source;
3825 break;
3826 case IPPROTO_UDP:
3827 case IPPROTO_UDPLITE:
3828 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3829 if (uh != NULL)
3830 sip->sin6_port = uh->source;
3831 break;
3832 case IPPROTO_DCCP:
3833 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3834 if (dh != NULL)
3835 sip->sin6_port = dh->dccph_sport;
3836 break;
3837 }
3838 return proto;
3839}
3840#endif
3841
3842
3843
3844
3845
3846
3847
3848#ifdef CONFIG_NETWORK_SECMARK
3849static struct smack_known *smack_from_skb(struct sk_buff *skb)
3850{
3851 if (skb == NULL || skb->secmark == 0)
3852 return NULL;
3853
3854 return smack_from_secid(skb->secmark);
3855}
3856#else
3857static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
3858{
3859 return NULL;
3860}
3861#endif
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
3875 struct sk_buff *skb)
3876{
3877 struct netlbl_lsm_secattr secattr;
3878 struct socket_smack *ssp = NULL;
3879 struct smack_known *skp = NULL;
3880
3881 netlbl_secattr_init(&secattr);
3882
3883 if (sk)
3884 ssp = sk->sk_security;
3885
3886 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
3887 skp = smack_from_secattr(&secattr, ssp);
3888 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
3889 netlbl_cache_add(skb, family, &skp->smk_netlabel);
3890 }
3891
3892 netlbl_secattr_destroy(&secattr);
3893
3894 return skp;
3895}
3896
3897
3898
3899
3900
3901
3902
3903
3904static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3905{
3906 struct socket_smack *ssp = sk->sk_security;
3907 struct smack_known *skp = NULL;
3908 int rc = 0;
3909 struct smk_audit_info ad;
3910 u16 family = sk->sk_family;
3911#ifdef CONFIG_AUDIT
3912 struct lsm_network_audit net;
3913#endif
3914#if IS_ENABLED(CONFIG_IPV6)
3915 struct sockaddr_in6 sadd;
3916 int proto;
3917
3918 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3919 family = PF_INET;
3920#endif
3921
3922 switch (family) {
3923 case PF_INET:
3924
3925
3926
3927
3928
3929 skp = smack_from_skb(skb);
3930 if (skp == NULL) {
3931 skp = smack_from_netlbl(sk, family, skb);
3932 if (skp == NULL)
3933 skp = smack_net_ambient;
3934 }
3935
3936#ifdef CONFIG_AUDIT
3937 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3938 ad.a.u.net->family = family;
3939 ad.a.u.net->netif = skb->skb_iif;
3940 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3941#endif
3942
3943
3944
3945
3946
3947
3948 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3949 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3950 MAY_WRITE, rc);
3951 if (rc != 0)
3952 netlbl_skbuff_err(skb, family, rc, 0);
3953 break;
3954#if IS_ENABLED(CONFIG_IPV6)
3955 case PF_INET6:
3956 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3957 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3958 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3959 break;
3960#ifdef SMACK_IPV6_SECMARK_LABELING
3961 skp = smack_from_skb(skb);
3962 if (skp == NULL) {
3963 if (smk_ipv6_localhost(&sadd))
3964 break;
3965 skp = smack_ipv6host_label(&sadd);
3966 if (skp == NULL)
3967 skp = smack_net_ambient;
3968 }
3969#ifdef CONFIG_AUDIT
3970 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3971 ad.a.u.net->family = family;
3972 ad.a.u.net->netif = skb->skb_iif;
3973 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3974#endif
3975 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3976 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3977 MAY_WRITE, rc);
3978#endif
3979#ifdef SMACK_IPV6_PORT_LABELING
3980 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3981#endif
3982 if (rc != 0)
3983 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3984 ICMPV6_ADM_PROHIBITED, 0);
3985 break;
3986#endif
3987 }
3988
3989 return rc;
3990}
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001static int smack_socket_getpeersec_stream(struct socket *sock,
4002 char __user *optval,
4003 int __user *optlen, unsigned len)
4004{
4005 struct socket_smack *ssp;
4006 char *rcp = "";
4007 int slen = 1;
4008 int rc = 0;
4009
4010 ssp = sock->sk->sk_security;
4011 if (ssp->smk_packet != NULL) {
4012 rcp = ssp->smk_packet->smk_known;
4013 slen = strlen(rcp) + 1;
4014 }
4015
4016 if (slen > len)
4017 rc = -ERANGE;
4018 else if (copy_to_user(optval, rcp, slen) != 0)
4019 rc = -EFAULT;
4020
4021 if (put_user(slen, optlen) != 0)
4022 rc = -EFAULT;
4023
4024 return rc;
4025}
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036static int smack_socket_getpeersec_dgram(struct socket *sock,
4037 struct sk_buff *skb, u32 *secid)
4038
4039{
4040 struct socket_smack *ssp = NULL;
4041 struct smack_known *skp;
4042 struct sock *sk = NULL;
4043 int family = PF_UNSPEC;
4044 u32 s = 0;
4045
4046 if (skb != NULL) {
4047 if (skb->protocol == htons(ETH_P_IP))
4048 family = PF_INET;
4049#if IS_ENABLED(CONFIG_IPV6)
4050 else if (skb->protocol == htons(ETH_P_IPV6))
4051 family = PF_INET6;
4052#endif
4053 }
4054 if (family == PF_UNSPEC && sock != NULL)
4055 family = sock->sk->sk_family;
4056
4057 switch (family) {
4058 case PF_UNIX:
4059 ssp = sock->sk->sk_security;
4060 s = ssp->smk_out->smk_secid;
4061 break;
4062 case PF_INET:
4063 skp = smack_from_skb(skb);
4064 if (skp) {
4065 s = skp->smk_secid;
4066 break;
4067 }
4068
4069
4070
4071 if (sock != NULL)
4072 sk = sock->sk;
4073 skp = smack_from_netlbl(sk, family, skb);
4074 if (skp != NULL)
4075 s = skp->smk_secid;
4076 break;
4077 case PF_INET6:
4078#ifdef SMACK_IPV6_SECMARK_LABELING
4079 skp = smack_from_skb(skb);
4080 if (skp)
4081 s = skp->smk_secid;
4082#endif
4083 break;
4084 }
4085 *secid = s;
4086 if (s == 0)
4087 return -EINVAL;
4088 return 0;
4089}
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099static void smack_sock_graft(struct sock *sk, struct socket *parent)
4100{
4101 struct socket_smack *ssp;
4102 struct smack_known *skp = smk_of_current();
4103
4104 if (sk == NULL ||
4105 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4106 return;
4107
4108 ssp = sk->sk_security;
4109 ssp->smk_in = skp;
4110 ssp->smk_out = skp;
4111
4112}
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
4124 struct request_sock *req)
4125{
4126 u16 family = sk->sk_family;
4127 struct smack_known *skp;
4128 struct socket_smack *ssp = sk->sk_security;
4129 struct sockaddr_in addr;
4130 struct iphdr *hdr;
4131 struct smack_known *hskp;
4132 int rc;
4133 struct smk_audit_info ad;
4134#ifdef CONFIG_AUDIT
4135 struct lsm_network_audit net;
4136#endif
4137
4138#if IS_ENABLED(CONFIG_IPV6)
4139 if (family == PF_INET6) {
4140
4141
4142
4143
4144
4145 if (skb->protocol == htons(ETH_P_IP))
4146 family = PF_INET;
4147 else
4148 return 0;
4149 }
4150#endif
4151
4152
4153
4154
4155
4156
4157 skp = smack_from_skb(skb);
4158 if (skp == NULL) {
4159 skp = smack_from_netlbl(sk, family, skb);
4160 if (skp == NULL)
4161 skp = &smack_known_huh;
4162 }
4163
4164#ifdef CONFIG_AUDIT
4165 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4166 ad.a.u.net->family = family;
4167 ad.a.u.net->netif = skb->skb_iif;
4168 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4169#endif
4170
4171
4172
4173
4174 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4175 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4176 if (rc != 0)
4177 return rc;
4178
4179
4180
4181
4182
4183 req->peer_secid = skp->smk_secid;
4184
4185
4186
4187
4188
4189
4190 hdr = ip_hdr(skb);
4191 addr.sin_addr.s_addr = hdr->saddr;
4192 rcu_read_lock();
4193 hskp = smack_ipv4host_label(&addr);
4194 rcu_read_unlock();
4195
4196 if (hskp == NULL)
4197 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4198 else
4199 netlbl_req_delattr(req);
4200
4201 return rc;
4202}
4203
4204
4205
4206
4207
4208
4209
4210
4211static void smack_inet_csk_clone(struct sock *sk,
4212 const struct request_sock *req)
4213{
4214 struct socket_smack *ssp = sk->sk_security;
4215 struct smack_known *skp;
4216
4217 if (req->peer_secid != 0) {
4218 skp = smack_from_secid(req->peer_secid);
4219 ssp->smk_packet = skp;
4220 } else
4221 ssp->smk_packet = NULL;
4222}
4223
4224
4225
4226
4227
4228
4229
4230
4231#ifdef CONFIG_KEYS
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243static int smack_key_alloc(struct key *key, const struct cred *cred,
4244 unsigned long flags)
4245{
4246 struct smack_known *skp = smk_of_task(smack_cred(cred));
4247
4248 key->security = skp;
4249 return 0;
4250}
4251
4252
4253
4254
4255
4256
4257
4258static void smack_key_free(struct key *key)
4259{
4260 key->security = NULL;
4261}
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272static int smack_key_permission(key_ref_t key_ref,
4273 const struct cred *cred,
4274 enum key_need_perm need_perm)
4275{
4276 struct key *keyp;
4277 struct smk_audit_info ad;
4278 struct smack_known *tkp = smk_of_task(smack_cred(cred));
4279 int request = 0;
4280 int rc;
4281
4282
4283
4284
4285 switch (need_perm) {
4286 case KEY_NEED_READ:
4287 case KEY_NEED_SEARCH:
4288 case KEY_NEED_VIEW:
4289 request |= MAY_READ;
4290 break;
4291 case KEY_NEED_WRITE:
4292 case KEY_NEED_LINK:
4293 case KEY_NEED_SETATTR:
4294 request |= MAY_WRITE;
4295 break;
4296 case KEY_NEED_UNSPECIFIED:
4297 case KEY_NEED_UNLINK:
4298 case KEY_SYSADMIN_OVERRIDE:
4299 case KEY_AUTHTOKEN_OVERRIDE:
4300 case KEY_DEFER_PERM_CHECK:
4301 return 0;
4302 default:
4303 return -EINVAL;
4304 }
4305
4306 keyp = key_ref_to_ptr(key_ref);
4307 if (keyp == NULL)
4308 return -EINVAL;
4309
4310
4311
4312
4313 if (keyp->security == NULL)
4314 return 0;
4315
4316
4317
4318 if (tkp == NULL)
4319 return -EACCES;
4320
4321 if (smack_privileged(CAP_MAC_OVERRIDE))
4322 return 0;
4323
4324#ifdef CONFIG_AUDIT
4325 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4326 ad.a.u.key_struct.key = keyp->serial;
4327 ad.a.u.key_struct.key_desc = keyp->description;
4328#endif
4329 rc = smk_access(tkp, keyp->security, request, &ad);
4330 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4331 return rc;
4332}
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343static int smack_key_getsecurity(struct key *key, char **_buffer)
4344{
4345 struct smack_known *skp = key->security;
4346 size_t length;
4347 char *copy;
4348
4349 if (key->security == NULL) {
4350 *_buffer = NULL;
4351 return 0;
4352 }
4353
4354 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4355 if (copy == NULL)
4356 return -ENOMEM;
4357 length = strlen(copy) + 1;
4358
4359 *_buffer = copy;
4360 return length;
4361}
4362
4363
4364#ifdef CONFIG_KEY_NOTIFICATIONS
4365
4366
4367
4368
4369
4370
4371
4372static int smack_watch_key(struct key *key)
4373{
4374 struct smk_audit_info ad;
4375 struct smack_known *tkp = smk_of_current();
4376 int rc;
4377
4378 if (key == NULL)
4379 return -EINVAL;
4380
4381
4382
4383
4384 if (key->security == NULL)
4385 return 0;
4386
4387
4388
4389 if (tkp == NULL)
4390 return -EACCES;
4391
4392 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4393 return 0;
4394
4395#ifdef CONFIG_AUDIT
4396 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4397 ad.a.u.key_struct.key = key->serial;
4398 ad.a.u.key_struct.key_desc = key->description;
4399#endif
4400 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4401 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4402 return rc;
4403}
4404#endif
4405#endif
4406
4407#ifdef CONFIG_WATCH_QUEUE
4408
4409
4410
4411
4412
4413
4414static int smack_post_notification(const struct cred *w_cred,
4415 const struct cred *cred,
4416 struct watch_notification *n)
4417{
4418 struct smk_audit_info ad;
4419 struct smack_known *subj, *obj;
4420 int rc;
4421
4422
4423 if (n->type == WATCH_TYPE_META)
4424 return 0;
4425
4426 if (!cred)
4427 return 0;
4428 subj = smk_of_task(smack_cred(cred));
4429 obj = smk_of_task(smack_cred(w_cred));
4430
4431 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4432 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4433 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4434 return rc;
4435}
4436#endif
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451#ifdef CONFIG_AUDIT
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4464{
4465 struct smack_known *skp;
4466 char **rule = (char **)vrule;
4467 *rule = NULL;
4468
4469 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4470 return -EINVAL;
4471
4472 if (op != Audit_equal && op != Audit_not_equal)
4473 return -EINVAL;
4474
4475 skp = smk_import_entry(rulestr, 0);
4476 if (IS_ERR(skp))
4477 return PTR_ERR(skp);
4478
4479 *rule = skp->smk_known;
4480
4481 return 0;
4482}
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492static int smack_audit_rule_known(struct audit_krule *krule)
4493{
4494 struct audit_field *f;
4495 int i;
4496
4497 for (i = 0; i < krule->field_count; i++) {
4498 f = &krule->fields[i];
4499
4500 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4501 return 1;
4502 }
4503
4504 return 0;
4505}
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
4518{
4519 struct smack_known *skp;
4520 char *rule = vrule;
4521
4522 if (unlikely(!rule)) {
4523 WARN_ONCE(1, "Smack: missing rule\n");
4524 return -ENOENT;
4525 }
4526
4527 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4528 return 0;
4529
4530 skp = smack_from_secid(secid);
4531
4532
4533
4534
4535
4536
4537 if (op == Audit_equal)
4538 return (rule == skp->smk_known);
4539 if (op == Audit_not_equal)
4540 return (rule != skp->smk_known);
4541
4542 return 0;
4543}
4544
4545
4546
4547
4548
4549
4550#endif
4551
4552
4553
4554
4555
4556static int smack_ismaclabel(const char *name)
4557{
4558 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4559}
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4571{
4572 struct smack_known *skp = smack_from_secid(secid);
4573
4574 if (secdata)
4575 *secdata = skp->smk_known;
4576 *seclen = strlen(skp->smk_known);
4577 return 0;
4578}
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4589{
4590 struct smack_known *skp = smk_find_entry(secdata);
4591
4592 if (skp)
4593 *secid = skp->smk_secid;
4594 else
4595 *secid = 0;
4596 return 0;
4597}
4598
4599
4600
4601
4602
4603
4604
4605static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4606{
4607 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4608 ctxlen, 0);
4609}
4610
4611static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4612{
4613 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SMACK,
4614 ctx, ctxlen, 0);
4615}
4616
4617static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4618{
4619 struct smack_known *skp = smk_of_inode(inode);
4620
4621 *ctx = skp->smk_known;
4622 *ctxlen = strlen(skp->smk_known);
4623 return 0;
4624}
4625
4626static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4627{
4628
4629 struct task_smack *tsp;
4630 struct smack_known *skp;
4631 struct inode_smack *isp;
4632 struct cred *new_creds = *new;
4633
4634 if (new_creds == NULL) {
4635 new_creds = prepare_creds();
4636 if (new_creds == NULL)
4637 return -ENOMEM;
4638 }
4639
4640 tsp = smack_cred(new_creds);
4641
4642
4643
4644
4645 isp = smack_inode(d_inode(dentry));
4646 skp = isp->smk_inode;
4647 tsp->smk_task = skp;
4648 *new = new_creds;
4649 return 0;
4650}
4651
4652static int smack_inode_copy_up_xattr(const char *name)
4653{
4654
4655
4656
4657 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4658 return 1;
4659
4660 return -EOPNOTSUPP;
4661}
4662
4663static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4664 struct qstr *name,
4665 const struct cred *old,
4666 struct cred *new)
4667{
4668 struct task_smack *otsp = smack_cred(old);
4669 struct task_smack *ntsp = smack_cred(new);
4670 struct inode_smack *isp;
4671 int may;
4672
4673
4674
4675
4676
4677 ntsp->smk_task = otsp->smk_task;
4678
4679
4680
4681
4682 isp = smack_inode(d_inode(dentry->d_parent));
4683
4684 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4685 rcu_read_lock();
4686 may = smk_access_entry(otsp->smk_task->smk_known,
4687 isp->smk_inode->smk_known,
4688 &otsp->smk_task->smk_rules);
4689 rcu_read_unlock();
4690
4691
4692
4693
4694
4695
4696 if (may > 0 && (may & MAY_TRANSMUTE))
4697 ntsp->smk_task = isp->smk_inode;
4698 }
4699 return 0;
4700}
4701
4702#ifdef CONFIG_IO_URING
4703
4704
4705
4706
4707
4708
4709
4710static int smack_uring_override_creds(const struct cred *new)
4711{
4712 struct task_smack *tsp = smack_cred(current_cred());
4713 struct task_smack *nsp = smack_cred(new);
4714
4715
4716
4717
4718
4719 if (tsp->smk_task == nsp->smk_task)
4720 return 0;
4721
4722 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4723 return 0;
4724
4725 return -EPERM;
4726}
4727
4728
4729
4730
4731
4732
4733
4734static int smack_uring_sqpoll(void)
4735{
4736 if (smack_privileged_cred(CAP_MAC_ADMIN, current_cred()))
4737 return 0;
4738
4739 return -EPERM;
4740}
4741
4742#endif
4743
4744struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4745 .lbs_cred = sizeof(struct task_smack),
4746 .lbs_file = sizeof(struct smack_known *),
4747 .lbs_inode = sizeof(struct inode_smack),
4748 .lbs_ipc = sizeof(struct smack_known *),
4749 .lbs_msg_msg = sizeof(struct smack_known *),
4750 .lbs_superblock = sizeof(struct superblock_smack),
4751};
4752
4753static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4754 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4755 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4756 LSM_HOOK_INIT(syslog, smack_syslog),
4757
4758 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4759 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4760
4761 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4762 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4763 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4764 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4765 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4766
4767 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
4768
4769 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4770 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4771 LSM_HOOK_INIT(inode_link, smack_inode_link),
4772 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4773 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4774 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4775 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4776 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4777 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4778 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4779 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4780 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4781 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4782 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4783 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4784 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4785 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4786
4787 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4788 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4789 LSM_HOOK_INIT(file_lock, smack_file_lock),
4790 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4791 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4792 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4793 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4794 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4795 LSM_HOOK_INIT(file_receive, smack_file_receive),
4796
4797 LSM_HOOK_INIT(file_open, smack_file_open),
4798
4799 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4800 LSM_HOOK_INIT(cred_free, smack_cred_free),
4801 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4802 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4803 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4804 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4805 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4806 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4807 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4808 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4809 LSM_HOOK_INIT(current_getsecid_subj, smack_current_getsecid_subj),
4810 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj),
4811 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4812 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4813 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4814 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4815 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4816 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4817 LSM_HOOK_INIT(task_kill, smack_task_kill),
4818 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4819
4820 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4821 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4822
4823 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4824
4825 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4826 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4827 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4828 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4829 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4830
4831 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4832 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4833 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4834 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4835
4836 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4837 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4838 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4839 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4840
4841 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4842
4843 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4844 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4845
4846 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4847 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4848
4849 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4850 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4851#ifdef SMACK_IPV6_PORT_LABELING
4852 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4853#endif
4854 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4855 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4856 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4857 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4858 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4859 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4860 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4861 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4862 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4863 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4864
4865
4866#ifdef CONFIG_KEYS
4867 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4868 LSM_HOOK_INIT(key_free, smack_key_free),
4869 LSM_HOOK_INIT(key_permission, smack_key_permission),
4870 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4871#ifdef CONFIG_KEY_NOTIFICATIONS
4872 LSM_HOOK_INIT(watch_key, smack_watch_key),
4873#endif
4874#endif
4875
4876#ifdef CONFIG_WATCH_QUEUE
4877 LSM_HOOK_INIT(post_notification, smack_post_notification),
4878#endif
4879
4880
4881#ifdef CONFIG_AUDIT
4882 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4883 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4884 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4885#endif
4886
4887 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4888 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4889 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4890 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4891 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4892 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4893 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4894 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4895 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4896#ifdef CONFIG_IO_URING
4897 LSM_HOOK_INIT(uring_override_creds, smack_uring_override_creds),
4898 LSM_HOOK_INIT(uring_sqpoll, smack_uring_sqpoll),
4899#endif
4900};
4901
4902
4903static __init void init_smack_known_list(void)
4904{
4905
4906
4907
4908 mutex_init(&smack_known_huh.smk_rules_lock);
4909 mutex_init(&smack_known_hat.smk_rules_lock);
4910 mutex_init(&smack_known_floor.smk_rules_lock);
4911 mutex_init(&smack_known_star.smk_rules_lock);
4912 mutex_init(&smack_known_web.smk_rules_lock);
4913
4914
4915
4916 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4917 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4918 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4919 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4920 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4921
4922
4923
4924 smk_insert_entry(&smack_known_huh);
4925 smk_insert_entry(&smack_known_hat);
4926 smk_insert_entry(&smack_known_star);
4927 smk_insert_entry(&smack_known_floor);
4928 smk_insert_entry(&smack_known_web);
4929}
4930
4931
4932
4933
4934
4935
4936static __init int smack_init(void)
4937{
4938 struct cred *cred = (struct cred *) current->cred;
4939 struct task_smack *tsp;
4940
4941 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4942 if (!smack_rule_cache)
4943 return -ENOMEM;
4944
4945
4946
4947
4948 tsp = smack_cred(cred);
4949 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4950
4951
4952
4953
4954 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4955 smack_enabled = 1;
4956
4957 pr_info("Smack: Initializing.\n");
4958#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4959 pr_info("Smack: Netfilter enabled.\n");
4960#endif
4961#ifdef SMACK_IPV6_PORT_LABELING
4962 pr_info("Smack: IPv6 port labeling enabled.\n");
4963#endif
4964#ifdef SMACK_IPV6_SECMARK_LABELING
4965 pr_info("Smack: IPv6 Netfilter enabled.\n");
4966#endif
4967
4968
4969 init_smack_known_list();
4970
4971 return 0;
4972}
4973
4974
4975
4976
4977
4978DEFINE_LSM(smack) = {
4979 .name = "smack",
4980 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4981 .blobs = &smack_blob_sizes,
4982 .init = smack_init,
4983};
4984