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