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