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