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