CRED: Use RCU to access another task's creds and to release a task's own creds
[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 * Author:
7 * Casey Schaufler <casey@schaufler-ca.com>
8 *
9 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 */
15
16 #include <linux/xattr.h>
17 #include <linux/pagemap.h>
18 #include <linux/mount.h>
19 #include <linux/stat.h>
20 #include <linux/ext2_fs.h>
21 #include <linux/kd.h>
22 #include <asm/ioctls.h>
23 #include <linux/tcp.h>
24 #include <linux/udp.h>
25 #include <linux/mutex.h>
26 #include <linux/pipe_fs_i.h>
27 #include <net/netlabel.h>
28 #include <net/cipso_ipv4.h>
29 #include <linux/audit.h>
30
31 #include "smack.h"
32
33 #define task_security(task) (task_cred_xxx((task), security))
34
35 /*
36 * I hope these are the hokeyist lines of code in the module. Casey.
37 */
38 #define DEVPTS_SUPER_MAGIC 0x1cd1
39 #define SOCKFS_MAGIC 0x534F434B
40 #define TMPFS_MAGIC 0x01021994
41
42 /**
43 * smk_fetch - Fetch the smack label from a file.
44 * @ip: a pointer to the inode
45 * @dp: a pointer to the dentry
46 *
47 * Returns a pointer to the master list entry for the Smack label
48 * or NULL if there was no label to fetch.
49 */
50 static char *smk_fetch(struct inode *ip, struct dentry *dp)
51 {
52 int rc;
53 char in[SMK_LABELLEN];
54
55 if (ip->i_op->getxattr == NULL)
56 return NULL;
57
58 rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
59 if (rc < 0)
60 return NULL;
61
62 return smk_import(in, rc);
63 }
64
65 /**
66 * new_inode_smack - allocate an inode security blob
67 * @smack: a pointer to the Smack label to use in the blob
68 *
69 * Returns the new blob or NULL if there's no memory available
70 */
71 struct inode_smack *new_inode_smack(char *smack)
72 {
73 struct inode_smack *isp;
74
75 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
76 if (isp == NULL)
77 return NULL;
78
79 isp->smk_inode = smack;
80 isp->smk_flags = 0;
81 mutex_init(&isp->smk_lock);
82
83 return isp;
84 }
85
86 /*
87 * LSM hooks.
88 * We he, that is fun!
89 */
90
91 /**
92 * smack_ptrace_may_access - Smack approval on PTRACE_ATTACH
93 * @ctp: child task pointer
94 *
95 * Returns 0 if access is OK, an error code otherwise
96 *
97 * Do the capability checks, and require read and write.
98 */
99 static int smack_ptrace_may_access(struct task_struct *ctp, unsigned int mode)
100 {
101 int rc;
102
103 rc = cap_ptrace_may_access(ctp, mode);
104 if (rc != 0)
105 return rc;
106
107 rc = smk_access(current->cred->security, ctp->cred->security,
108 MAY_READWRITE);
109 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
110 return 0;
111 return rc;
112 }
113
114 /**
115 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
116 * @ptp: parent task pointer
117 *
118 * Returns 0 if access is OK, an error code otherwise
119 *
120 * Do the capability checks, and require read and write.
121 */
122 static int smack_ptrace_traceme(struct task_struct *ptp)
123 {
124 int rc;
125
126 rc = cap_ptrace_traceme(ptp);
127 if (rc != 0)
128 return rc;
129
130 rc = smk_access(ptp->cred->security, current->cred->security,
131 MAY_READWRITE);
132 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
133 return 0;
134 return rc;
135 }
136
137 /**
138 * smack_syslog - Smack approval on syslog
139 * @type: message type
140 *
141 * Require that the task has the floor label
142 *
143 * Returns 0 on success, error code otherwise.
144 */
145 static int smack_syslog(int type)
146 {
147 int rc;
148 char *sp = current_security();
149
150 rc = cap_syslog(type);
151 if (rc != 0)
152 return rc;
153
154 if (capable(CAP_MAC_OVERRIDE))
155 return 0;
156
157 if (sp != smack_known_floor.smk_known)
158 rc = -EACCES;
159
160 return rc;
161 }
162
163
164 /*
165 * Superblock Hooks.
166 */
167
168 /**
169 * smack_sb_alloc_security - allocate a superblock blob
170 * @sb: the superblock getting the blob
171 *
172 * Returns 0 on success or -ENOMEM on error.
173 */
174 static int smack_sb_alloc_security(struct super_block *sb)
175 {
176 struct superblock_smack *sbsp;
177
178 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
179
180 if (sbsp == NULL)
181 return -ENOMEM;
182
183 sbsp->smk_root = smack_known_floor.smk_known;
184 sbsp->smk_default = smack_known_floor.smk_known;
185 sbsp->smk_floor = smack_known_floor.smk_known;
186 sbsp->smk_hat = smack_known_hat.smk_known;
187 sbsp->smk_initialized = 0;
188 spin_lock_init(&sbsp->smk_sblock);
189
190 sb->s_security = sbsp;
191
192 return 0;
193 }
194
195 /**
196 * smack_sb_free_security - free a superblock blob
197 * @sb: the superblock getting the blob
198 *
199 */
200 static void smack_sb_free_security(struct super_block *sb)
201 {
202 kfree(sb->s_security);
203 sb->s_security = NULL;
204 }
205
206 /**
207 * smack_sb_copy_data - copy mount options data for processing
208 * @type: file system type
209 * @orig: where to start
210 * @smackopts
211 *
212 * Returns 0 on success or -ENOMEM on error.
213 *
214 * Copy the Smack specific mount options out of the mount
215 * options list.
216 */
217 static int smack_sb_copy_data(char *orig, char *smackopts)
218 {
219 char *cp, *commap, *otheropts, *dp;
220
221 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
222 if (otheropts == NULL)
223 return -ENOMEM;
224
225 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
226 if (strstr(cp, SMK_FSDEFAULT) == cp)
227 dp = smackopts;
228 else if (strstr(cp, SMK_FSFLOOR) == cp)
229 dp = smackopts;
230 else if (strstr(cp, SMK_FSHAT) == cp)
231 dp = smackopts;
232 else if (strstr(cp, SMK_FSROOT) == cp)
233 dp = smackopts;
234 else
235 dp = otheropts;
236
237 commap = strchr(cp, ',');
238 if (commap != NULL)
239 *commap = '\0';
240
241 if (*dp != '\0')
242 strcat(dp, ",");
243 strcat(dp, cp);
244 }
245
246 strcpy(orig, otheropts);
247 free_page((unsigned long)otheropts);
248
249 return 0;
250 }
251
252 /**
253 * smack_sb_kern_mount - Smack specific mount processing
254 * @sb: the file system superblock
255 * @data: the smack mount options
256 *
257 * Returns 0 on success, an error code on failure
258 */
259 static int smack_sb_kern_mount(struct super_block *sb, void *data)
260 {
261 struct dentry *root = sb->s_root;
262 struct inode *inode = root->d_inode;
263 struct superblock_smack *sp = sb->s_security;
264 struct inode_smack *isp;
265 char *op;
266 char *commap;
267 char *nsp;
268
269 spin_lock(&sp->smk_sblock);
270 if (sp->smk_initialized != 0) {
271 spin_unlock(&sp->smk_sblock);
272 return 0;
273 }
274 sp->smk_initialized = 1;
275 spin_unlock(&sp->smk_sblock);
276
277 for (op = data; op != NULL; op = commap) {
278 commap = strchr(op, ',');
279 if (commap != NULL)
280 *commap++ = '\0';
281
282 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
283 op += strlen(SMK_FSHAT);
284 nsp = smk_import(op, 0);
285 if (nsp != NULL)
286 sp->smk_hat = nsp;
287 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
288 op += strlen(SMK_FSFLOOR);
289 nsp = smk_import(op, 0);
290 if (nsp != NULL)
291 sp->smk_floor = nsp;
292 } else if (strncmp(op, SMK_FSDEFAULT,
293 strlen(SMK_FSDEFAULT)) == 0) {
294 op += strlen(SMK_FSDEFAULT);
295 nsp = smk_import(op, 0);
296 if (nsp != NULL)
297 sp->smk_default = nsp;
298 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
299 op += strlen(SMK_FSROOT);
300 nsp = smk_import(op, 0);
301 if (nsp != NULL)
302 sp->smk_root = nsp;
303 }
304 }
305
306 /*
307 * Initialize the root inode.
308 */
309 isp = inode->i_security;
310 if (isp == NULL)
311 inode->i_security = new_inode_smack(sp->smk_root);
312 else
313 isp->smk_inode = sp->smk_root;
314
315 return 0;
316 }
317
318 /**
319 * smack_sb_statfs - Smack check on statfs
320 * @dentry: identifies the file system in question
321 *
322 * Returns 0 if current can read the floor of the filesystem,
323 * and error code otherwise
324 */
325 static int smack_sb_statfs(struct dentry *dentry)
326 {
327 struct superblock_smack *sbp = dentry->d_sb->s_security;
328
329 return smk_curacc(sbp->smk_floor, MAY_READ);
330 }
331
332 /**
333 * smack_sb_mount - Smack check for mounting
334 * @dev_name: unused
335 * @nd: mount point
336 * @type: unused
337 * @flags: unused
338 * @data: unused
339 *
340 * Returns 0 if current can write the floor of the filesystem
341 * being mounted on, an error code otherwise.
342 */
343 static int smack_sb_mount(char *dev_name, struct path *path,
344 char *type, unsigned long flags, void *data)
345 {
346 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
347
348 return smk_curacc(sbp->smk_floor, MAY_WRITE);
349 }
350
351 /**
352 * smack_sb_umount - Smack check for unmounting
353 * @mnt: file system to unmount
354 * @flags: unused
355 *
356 * Returns 0 if current can write the floor of the filesystem
357 * being unmounted, an error code otherwise.
358 */
359 static int smack_sb_umount(struct vfsmount *mnt, int flags)
360 {
361 struct superblock_smack *sbp;
362
363 sbp = mnt->mnt_sb->s_security;
364
365 return smk_curacc(sbp->smk_floor, MAY_WRITE);
366 }
367
368 /*
369 * Inode hooks
370 */
371
372 /**
373 * smack_inode_alloc_security - allocate an inode blob
374 * @inode - the inode in need of a blob
375 *
376 * Returns 0 if it gets a blob, -ENOMEM otherwise
377 */
378 static int smack_inode_alloc_security(struct inode *inode)
379 {
380 inode->i_security = new_inode_smack(current_security());
381 if (inode->i_security == NULL)
382 return -ENOMEM;
383 return 0;
384 }
385
386 /**
387 * smack_inode_free_security - free an inode blob
388 * @inode - the inode with a blob
389 *
390 * Clears the blob pointer in inode
391 */
392 static void smack_inode_free_security(struct inode *inode)
393 {
394 kfree(inode->i_security);
395 inode->i_security = NULL;
396 }
397
398 /**
399 * smack_inode_init_security - copy out the smack from an inode
400 * @inode: the inode
401 * @dir: unused
402 * @name: where to put the attribute name
403 * @value: where to put the attribute value
404 * @len: where to put the length of the attribute
405 *
406 * Returns 0 if it all works out, -ENOMEM if there's no memory
407 */
408 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
409 char **name, void **value, size_t *len)
410 {
411 char *isp = smk_of_inode(inode);
412
413 if (name) {
414 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
415 if (*name == NULL)
416 return -ENOMEM;
417 }
418
419 if (value) {
420 *value = kstrdup(isp, GFP_KERNEL);
421 if (*value == NULL)
422 return -ENOMEM;
423 }
424
425 if (len)
426 *len = strlen(isp) + 1;
427
428 return 0;
429 }
430
431 /**
432 * smack_inode_link - Smack check on link
433 * @old_dentry: the existing object
434 * @dir: unused
435 * @new_dentry: the new object
436 *
437 * Returns 0 if access is permitted, an error code otherwise
438 */
439 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
440 struct dentry *new_dentry)
441 {
442 int rc;
443 char *isp;
444
445 isp = smk_of_inode(old_dentry->d_inode);
446 rc = smk_curacc(isp, MAY_WRITE);
447
448 if (rc == 0 && new_dentry->d_inode != NULL) {
449 isp = smk_of_inode(new_dentry->d_inode);
450 rc = smk_curacc(isp, MAY_WRITE);
451 }
452
453 return rc;
454 }
455
456 /**
457 * smack_inode_unlink - Smack check on inode deletion
458 * @dir: containing directory object
459 * @dentry: file to unlink
460 *
461 * Returns 0 if current can write the containing directory
462 * and the object, error code otherwise
463 */
464 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
465 {
466 struct inode *ip = dentry->d_inode;
467 int rc;
468
469 /*
470 * You need write access to the thing you're unlinking
471 */
472 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE);
473 if (rc == 0)
474 /*
475 * You also need write access to the containing directory
476 */
477 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
478
479 return rc;
480 }
481
482 /**
483 * smack_inode_rmdir - Smack check on directory deletion
484 * @dir: containing directory object
485 * @dentry: directory to unlink
486 *
487 * Returns 0 if current can write the containing directory
488 * and the directory, error code otherwise
489 */
490 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
491 {
492 int rc;
493
494 /*
495 * You need write access to the thing you're removing
496 */
497 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
498 if (rc == 0)
499 /*
500 * You also need write access to the containing directory
501 */
502 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
503
504 return rc;
505 }
506
507 /**
508 * smack_inode_rename - Smack check on rename
509 * @old_inode: the old directory
510 * @old_dentry: unused
511 * @new_inode: the new directory
512 * @new_dentry: unused
513 *
514 * Read and write access is required on both the old and
515 * new directories.
516 *
517 * Returns 0 if access is permitted, an error code otherwise
518 */
519 static int smack_inode_rename(struct inode *old_inode,
520 struct dentry *old_dentry,
521 struct inode *new_inode,
522 struct dentry *new_dentry)
523 {
524 int rc;
525 char *isp;
526
527 isp = smk_of_inode(old_dentry->d_inode);
528 rc = smk_curacc(isp, MAY_READWRITE);
529
530 if (rc == 0 && new_dentry->d_inode != NULL) {
531 isp = smk_of_inode(new_dentry->d_inode);
532 rc = smk_curacc(isp, MAY_READWRITE);
533 }
534
535 return rc;
536 }
537
538 /**
539 * smack_inode_permission - Smack version of permission()
540 * @inode: the inode in question
541 * @mask: the access requested
542 * @nd: unused
543 *
544 * This is the important Smack hook.
545 *
546 * Returns 0 if access is permitted, -EACCES otherwise
547 */
548 static int smack_inode_permission(struct inode *inode, int mask)
549 {
550 /*
551 * No permission to check. Existence test. Yup, it's there.
552 */
553 if (mask == 0)
554 return 0;
555
556 return smk_curacc(smk_of_inode(inode), mask);
557 }
558
559 /**
560 * smack_inode_setattr - Smack check for setting attributes
561 * @dentry: the object
562 * @iattr: for the force flag
563 *
564 * Returns 0 if access is permitted, an error code otherwise
565 */
566 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
567 {
568 /*
569 * Need to allow for clearing the setuid bit.
570 */
571 if (iattr->ia_valid & ATTR_FORCE)
572 return 0;
573
574 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
575 }
576
577 /**
578 * smack_inode_getattr - Smack check for getting attributes
579 * @mnt: unused
580 * @dentry: the object
581 *
582 * Returns 0 if access is permitted, an error code otherwise
583 */
584 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
585 {
586 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
587 }
588
589 /**
590 * smack_inode_setxattr - Smack check for setting xattrs
591 * @dentry: the object
592 * @name: name of the attribute
593 * @value: unused
594 * @size: unused
595 * @flags: unused
596 *
597 * This protects the Smack attribute explicitly.
598 *
599 * Returns 0 if access is permitted, an error code otherwise
600 */
601 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
602 const void *value, size_t size, int flags)
603 {
604 int rc = 0;
605
606 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
607 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
608 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
609 if (!capable(CAP_MAC_ADMIN))
610 rc = -EPERM;
611 } else
612 rc = cap_inode_setxattr(dentry, name, value, size, flags);
613
614 if (rc == 0)
615 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
616
617 return rc;
618 }
619
620 /**
621 * smack_inode_post_setxattr - Apply the Smack update approved above
622 * @dentry: object
623 * @name: attribute name
624 * @value: attribute value
625 * @size: attribute size
626 * @flags: unused
627 *
628 * Set the pointer in the inode blob to the entry found
629 * in the master label list.
630 */
631 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
632 const void *value, size_t size, int flags)
633 {
634 struct inode_smack *isp;
635 char *nsp;
636
637 /*
638 * Not SMACK
639 */
640 if (strcmp(name, XATTR_NAME_SMACK))
641 return;
642
643 if (size >= SMK_LABELLEN)
644 return;
645
646 isp = dentry->d_inode->i_security;
647
648 /*
649 * No locking is done here. This is a pointer
650 * assignment.
651 */
652 nsp = smk_import(value, size);
653 if (nsp != NULL)
654 isp->smk_inode = nsp;
655 else
656 isp->smk_inode = smack_known_invalid.smk_known;
657
658 return;
659 }
660
661 /*
662 * smack_inode_getxattr - Smack check on getxattr
663 * @dentry: the object
664 * @name: unused
665 *
666 * Returns 0 if access is permitted, an error code otherwise
667 */
668 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
669 {
670 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
671 }
672
673 /*
674 * smack_inode_removexattr - Smack check on removexattr
675 * @dentry: the object
676 * @name: name of the attribute
677 *
678 * Removing the Smack attribute requires CAP_MAC_ADMIN
679 *
680 * Returns 0 if access is permitted, an error code otherwise
681 */
682 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
683 {
684 int rc = 0;
685
686 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
687 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
688 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
689 if (!capable(CAP_MAC_ADMIN))
690 rc = -EPERM;
691 } else
692 rc = cap_inode_removexattr(dentry, name);
693
694 if (rc == 0)
695 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
696
697 return rc;
698 }
699
700 /**
701 * smack_inode_getsecurity - get smack xattrs
702 * @inode: the object
703 * @name: attribute name
704 * @buffer: where to put the result
705 * @size: size of the buffer
706 * @err: unused
707 *
708 * Returns the size of the attribute or an error code
709 */
710 static int smack_inode_getsecurity(const struct inode *inode,
711 const char *name, void **buffer,
712 bool alloc)
713 {
714 struct socket_smack *ssp;
715 struct socket *sock;
716 struct super_block *sbp;
717 struct inode *ip = (struct inode *)inode;
718 char *isp;
719 int ilen;
720 int rc = 0;
721
722 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
723 isp = smk_of_inode(inode);
724 ilen = strlen(isp) + 1;
725 *buffer = isp;
726 return ilen;
727 }
728
729 /*
730 * The rest of the Smack xattrs are only on sockets.
731 */
732 sbp = ip->i_sb;
733 if (sbp->s_magic != SOCKFS_MAGIC)
734 return -EOPNOTSUPP;
735
736 sock = SOCKET_I(ip);
737 if (sock == NULL || sock->sk == NULL)
738 return -EOPNOTSUPP;
739
740 ssp = sock->sk->sk_security;
741
742 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
743 isp = ssp->smk_in;
744 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
745 isp = ssp->smk_out;
746 else
747 return -EOPNOTSUPP;
748
749 ilen = strlen(isp) + 1;
750 if (rc == 0) {
751 *buffer = isp;
752 rc = ilen;
753 }
754
755 return rc;
756 }
757
758
759 /**
760 * smack_inode_listsecurity - list the Smack attributes
761 * @inode: the object
762 * @buffer: where they go
763 * @buffer_size: size of buffer
764 *
765 * Returns 0 on success, -EINVAL otherwise
766 */
767 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
768 size_t buffer_size)
769 {
770 int len = strlen(XATTR_NAME_SMACK);
771
772 if (buffer != NULL && len <= buffer_size) {
773 memcpy(buffer, XATTR_NAME_SMACK, len);
774 return len;
775 }
776 return -EINVAL;
777 }
778
779 /**
780 * smack_inode_getsecid - Extract inode's security id
781 * @inode: inode to extract the info from
782 * @secid: where result will be saved
783 */
784 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
785 {
786 struct inode_smack *isp = inode->i_security;
787
788 *secid = smack_to_secid(isp->smk_inode);
789 }
790
791 /*
792 * File Hooks
793 */
794
795 /**
796 * smack_file_permission - Smack check on file operations
797 * @file: unused
798 * @mask: unused
799 *
800 * Returns 0
801 *
802 * Should access checks be done on each read or write?
803 * UNICOS and SELinux say yes.
804 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
805 *
806 * I'll say no for now. Smack does not do the frequent
807 * label changing that SELinux does.
808 */
809 static int smack_file_permission(struct file *file, int mask)
810 {
811 return 0;
812 }
813
814 /**
815 * smack_file_alloc_security - assign a file security blob
816 * @file: the object
817 *
818 * The security blob for a file is a pointer to the master
819 * label list, so no allocation is done.
820 *
821 * Returns 0
822 */
823 static int smack_file_alloc_security(struct file *file)
824 {
825 file->f_security = current_security();
826 return 0;
827 }
828
829 /**
830 * smack_file_free_security - clear a file security blob
831 * @file: the object
832 *
833 * The security blob for a file is a pointer to the master
834 * label list, so no memory is freed.
835 */
836 static void smack_file_free_security(struct file *file)
837 {
838 file->f_security = NULL;
839 }
840
841 /**
842 * smack_file_ioctl - Smack check on ioctls
843 * @file: the object
844 * @cmd: what to do
845 * @arg: unused
846 *
847 * Relies heavily on the correct use of the ioctl command conventions.
848 *
849 * Returns 0 if allowed, error code otherwise
850 */
851 static int smack_file_ioctl(struct file *file, unsigned int cmd,
852 unsigned long arg)
853 {
854 int rc = 0;
855
856 if (_IOC_DIR(cmd) & _IOC_WRITE)
857 rc = smk_curacc(file->f_security, MAY_WRITE);
858
859 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
860 rc = smk_curacc(file->f_security, MAY_READ);
861
862 return rc;
863 }
864
865 /**
866 * smack_file_lock - Smack check on file locking
867 * @file: the object
868 * @cmd unused
869 *
870 * Returns 0 if current has write access, error code otherwise
871 */
872 static int smack_file_lock(struct file *file, unsigned int cmd)
873 {
874 return smk_curacc(file->f_security, MAY_WRITE);
875 }
876
877 /**
878 * smack_file_fcntl - Smack check on fcntl
879 * @file: the object
880 * @cmd: what action to check
881 * @arg: unused
882 *
883 * Returns 0 if current has access, error code otherwise
884 */
885 static int smack_file_fcntl(struct file *file, unsigned int cmd,
886 unsigned long arg)
887 {
888 int rc;
889
890 switch (cmd) {
891 case F_DUPFD:
892 case F_GETFD:
893 case F_GETFL:
894 case F_GETLK:
895 case F_GETOWN:
896 case F_GETSIG:
897 rc = smk_curacc(file->f_security, MAY_READ);
898 break;
899 case F_SETFD:
900 case F_SETFL:
901 case F_SETLK:
902 case F_SETLKW:
903 case F_SETOWN:
904 case F_SETSIG:
905 rc = smk_curacc(file->f_security, MAY_WRITE);
906 break;
907 default:
908 rc = smk_curacc(file->f_security, MAY_READWRITE);
909 }
910
911 return rc;
912 }
913
914 /**
915 * smack_file_set_fowner - set the file security blob value
916 * @file: object in question
917 *
918 * Returns 0
919 * Further research may be required on this one.
920 */
921 static int smack_file_set_fowner(struct file *file)
922 {
923 file->f_security = current_security();
924 return 0;
925 }
926
927 /**
928 * smack_file_send_sigiotask - Smack on sigio
929 * @tsk: The target task
930 * @fown: the object the signal come from
931 * @signum: unused
932 *
933 * Allow a privileged task to get signals even if it shouldn't
934 *
935 * Returns 0 if a subject with the object's smack could
936 * write to the task, an error code otherwise.
937 */
938 static int smack_file_send_sigiotask(struct task_struct *tsk,
939 struct fown_struct *fown, int signum)
940 {
941 struct file *file;
942 int rc;
943
944 /*
945 * struct fown_struct is never outside the context of a struct file
946 */
947 file = container_of(fown, struct file, f_owner);
948 rc = smk_access(file->f_security, tsk->cred->security, MAY_WRITE);
949 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
950 return 0;
951 return rc;
952 }
953
954 /**
955 * smack_file_receive - Smack file receive check
956 * @file: the object
957 *
958 * Returns 0 if current has access, error code otherwise
959 */
960 static int smack_file_receive(struct file *file)
961 {
962 int may = 0;
963
964 /*
965 * This code relies on bitmasks.
966 */
967 if (file->f_mode & FMODE_READ)
968 may = MAY_READ;
969 if (file->f_mode & FMODE_WRITE)
970 may |= MAY_WRITE;
971
972 return smk_curacc(file->f_security, may);
973 }
974
975 /*
976 * Task hooks
977 */
978
979 /**
980 * smack_cred_alloc_security - "allocate" a task cred blob
981 * @cred: the task creds in need of a blob
982 *
983 * Smack isn't using copies of blobs. Everyone
984 * points to an immutable list. No alloc required.
985 * No data copy required.
986 *
987 * Always returns 0
988 */
989 static int smack_cred_alloc_security(struct cred *cred)
990 {
991 cred->security = current_security();
992 return 0;
993 }
994
995 /**
996 * smack_cred_free - "free" task-level security credentials
997 * @cred: the credentials in question
998 *
999 * Smack isn't using copies of blobs. Everyone
1000 * points to an immutable list. The blobs never go away.
1001 * There is no leak here.
1002 */
1003 static void smack_cred_free(struct cred *cred)
1004 {
1005 cred->security = NULL;
1006 }
1007
1008 /**
1009 * smack_task_setpgid - Smack check on setting pgid
1010 * @p: the task object
1011 * @pgid: unused
1012 *
1013 * Return 0 if write access is permitted
1014 */
1015 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1016 {
1017 return smk_curacc(task_security(p), MAY_WRITE);
1018 }
1019
1020 /**
1021 * smack_task_getpgid - Smack access check for getpgid
1022 * @p: the object task
1023 *
1024 * Returns 0 if current can read the object task, error code otherwise
1025 */
1026 static int smack_task_getpgid(struct task_struct *p)
1027 {
1028 return smk_curacc(task_security(p), MAY_READ);
1029 }
1030
1031 /**
1032 * smack_task_getsid - Smack access check for getsid
1033 * @p: the object task
1034 *
1035 * Returns 0 if current can read the object task, error code otherwise
1036 */
1037 static int smack_task_getsid(struct task_struct *p)
1038 {
1039 return smk_curacc(task_security(p), MAY_READ);
1040 }
1041
1042 /**
1043 * smack_task_getsecid - get the secid of the task
1044 * @p: the object task
1045 * @secid: where to put the result
1046 *
1047 * Sets the secid to contain a u32 version of the smack label.
1048 */
1049 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1050 {
1051 *secid = smack_to_secid(task_security(p));
1052 }
1053
1054 /**
1055 * smack_task_setnice - Smack check on setting nice
1056 * @p: the task object
1057 * @nice: unused
1058 *
1059 * Return 0 if write access is permitted
1060 */
1061 static int smack_task_setnice(struct task_struct *p, int nice)
1062 {
1063 int rc;
1064
1065 rc = cap_task_setnice(p, nice);
1066 if (rc == 0)
1067 rc = smk_curacc(task_security(p), MAY_WRITE);
1068 return rc;
1069 }
1070
1071 /**
1072 * smack_task_setioprio - Smack check on setting ioprio
1073 * @p: the task object
1074 * @ioprio: unused
1075 *
1076 * Return 0 if write access is permitted
1077 */
1078 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1079 {
1080 int rc;
1081
1082 rc = cap_task_setioprio(p, ioprio);
1083 if (rc == 0)
1084 rc = smk_curacc(task_security(p), MAY_WRITE);
1085 return rc;
1086 }
1087
1088 /**
1089 * smack_task_getioprio - Smack check on reading ioprio
1090 * @p: the task object
1091 *
1092 * Return 0 if read access is permitted
1093 */
1094 static int smack_task_getioprio(struct task_struct *p)
1095 {
1096 return smk_curacc(task_security(p), MAY_READ);
1097 }
1098
1099 /**
1100 * smack_task_setscheduler - Smack check on setting scheduler
1101 * @p: the task object
1102 * @policy: unused
1103 * @lp: unused
1104 *
1105 * Return 0 if read access is permitted
1106 */
1107 static int smack_task_setscheduler(struct task_struct *p, int policy,
1108 struct sched_param *lp)
1109 {
1110 int rc;
1111
1112 rc = cap_task_setscheduler(p, policy, lp);
1113 if (rc == 0)
1114 rc = smk_curacc(task_security(p), MAY_WRITE);
1115 return rc;
1116 }
1117
1118 /**
1119 * smack_task_getscheduler - Smack check on reading scheduler
1120 * @p: the task object
1121 *
1122 * Return 0 if read access is permitted
1123 */
1124 static int smack_task_getscheduler(struct task_struct *p)
1125 {
1126 return smk_curacc(task_security(p), MAY_READ);
1127 }
1128
1129 /**
1130 * smack_task_movememory - Smack check on moving memory
1131 * @p: the task object
1132 *
1133 * Return 0 if write access is permitted
1134 */
1135 static int smack_task_movememory(struct task_struct *p)
1136 {
1137 return smk_curacc(task_security(p), MAY_WRITE);
1138 }
1139
1140 /**
1141 * smack_task_kill - Smack check on signal delivery
1142 * @p: the task object
1143 * @info: unused
1144 * @sig: unused
1145 * @secid: identifies the smack to use in lieu of current's
1146 *
1147 * Return 0 if write access is permitted
1148 *
1149 * The secid behavior is an artifact of an SELinux hack
1150 * in the USB code. Someday it may go away.
1151 */
1152 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1153 int sig, u32 secid)
1154 {
1155 /*
1156 * Sending a signal requires that the sender
1157 * can write the receiver.
1158 */
1159 if (secid == 0)
1160 return smk_curacc(task_security(p), MAY_WRITE);
1161 /*
1162 * If the secid isn't 0 we're dealing with some USB IO
1163 * specific behavior. This is not clean. For one thing
1164 * we can't take privilege into account.
1165 */
1166 return smk_access(smack_from_secid(secid), task_security(p), MAY_WRITE);
1167 }
1168
1169 /**
1170 * smack_task_wait - Smack access check for waiting
1171 * @p: task to wait for
1172 *
1173 * Returns 0 if current can wait for p, error code otherwise
1174 */
1175 static int smack_task_wait(struct task_struct *p)
1176 {
1177 int rc;
1178
1179 rc = smk_access(current_security(), task_security(p), MAY_WRITE);
1180 if (rc == 0)
1181 return 0;
1182
1183 /*
1184 * Allow the operation to succeed if either task
1185 * has privilege to perform operations that might
1186 * account for the smack labels having gotten to
1187 * be different in the first place.
1188 *
1189 * This breaks the strict subject/object access
1190 * control ideal, taking the object's privilege
1191 * state into account in the decision as well as
1192 * the smack value.
1193 */
1194 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1195 return 0;
1196
1197 return rc;
1198 }
1199
1200 /**
1201 * smack_task_to_inode - copy task smack into the inode blob
1202 * @p: task to copy from
1203 * inode: inode to copy to
1204 *
1205 * Sets the smack pointer in the inode security blob
1206 */
1207 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1208 {
1209 struct inode_smack *isp = inode->i_security;
1210 isp->smk_inode = task_security(p);
1211 }
1212
1213 /*
1214 * Socket hooks.
1215 */
1216
1217 /**
1218 * smack_sk_alloc_security - Allocate a socket blob
1219 * @sk: the socket
1220 * @family: unused
1221 * @priority: memory allocation priority
1222 *
1223 * Assign Smack pointers to current
1224 *
1225 * Returns 0 on success, -ENOMEM is there's no memory
1226 */
1227 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1228 {
1229 char *csp = current_security();
1230 struct socket_smack *ssp;
1231
1232 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1233 if (ssp == NULL)
1234 return -ENOMEM;
1235
1236 ssp->smk_in = csp;
1237 ssp->smk_out = csp;
1238 ssp->smk_packet[0] = '\0';
1239
1240 sk->sk_security = ssp;
1241
1242 return 0;
1243 }
1244
1245 /**
1246 * smack_sk_free_security - Free a socket blob
1247 * @sk: the socket
1248 *
1249 * Clears the blob pointer
1250 */
1251 static void smack_sk_free_security(struct sock *sk)
1252 {
1253 kfree(sk->sk_security);
1254 }
1255
1256 /**
1257 * smack_set_catset - convert a capset to netlabel mls categories
1258 * @catset: the Smack categories
1259 * @sap: where to put the netlabel categories
1260 *
1261 * Allocates and fills attr.mls.cat
1262 */
1263 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1264 {
1265 unsigned char *cp;
1266 unsigned char m;
1267 int cat;
1268 int rc;
1269 int byte;
1270
1271 if (!catset)
1272 return;
1273
1274 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1275 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1276 sap->attr.mls.cat->startbit = 0;
1277
1278 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1279 for (m = 0x80; m != 0; m >>= 1, cat++) {
1280 if ((m & *cp) == 0)
1281 continue;
1282 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1283 cat, GFP_ATOMIC);
1284 }
1285 }
1286
1287 /**
1288 * smack_to_secattr - fill a secattr from a smack value
1289 * @smack: the smack value
1290 * @nlsp: where the result goes
1291 *
1292 * Casey says that CIPSO is good enough for now.
1293 * It can be used to effect.
1294 * It can also be abused to effect when necessary.
1295 * Appologies to the TSIG group in general and GW in particular.
1296 */
1297 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1298 {
1299 struct smack_cipso cipso;
1300 int rc;
1301
1302 switch (smack_net_nltype) {
1303 case NETLBL_NLTYPE_CIPSOV4:
1304 nlsp->domain = smack;
1305 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1306
1307 rc = smack_to_cipso(smack, &cipso);
1308 if (rc == 0) {
1309 nlsp->attr.mls.lvl = cipso.smk_level;
1310 smack_set_catset(cipso.smk_catset, nlsp);
1311 } else {
1312 nlsp->attr.mls.lvl = smack_cipso_direct;
1313 smack_set_catset(smack, nlsp);
1314 }
1315 break;
1316 default:
1317 break;
1318 }
1319 }
1320
1321 /**
1322 * smack_netlabel - Set the secattr on a socket
1323 * @sk: the socket
1324 *
1325 * Convert the outbound smack value (smk_out) to a
1326 * secattr and attach it to the socket.
1327 *
1328 * Returns 0 on success or an error code
1329 */
1330 static int smack_netlabel(struct sock *sk)
1331 {
1332 struct socket_smack *ssp;
1333 struct netlbl_lsm_secattr secattr;
1334 int rc;
1335
1336 ssp = sk->sk_security;
1337 netlbl_secattr_init(&secattr);
1338 smack_to_secattr(ssp->smk_out, &secattr);
1339 rc = netlbl_sock_setattr(sk, &secattr);
1340 netlbl_secattr_destroy(&secattr);
1341
1342 return rc;
1343 }
1344
1345 /**
1346 * smack_inode_setsecurity - set smack xattrs
1347 * @inode: the object
1348 * @name: attribute name
1349 * @value: attribute value
1350 * @size: size of the attribute
1351 * @flags: unused
1352 *
1353 * Sets the named attribute in the appropriate blob
1354 *
1355 * Returns 0 on success, or an error code
1356 */
1357 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1358 const void *value, size_t size, int flags)
1359 {
1360 char *sp;
1361 struct inode_smack *nsp = inode->i_security;
1362 struct socket_smack *ssp;
1363 struct socket *sock;
1364 int rc = 0;
1365
1366 if (value == NULL || size > SMK_LABELLEN)
1367 return -EACCES;
1368
1369 sp = smk_import(value, size);
1370 if (sp == NULL)
1371 return -EINVAL;
1372
1373 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1374 nsp->smk_inode = sp;
1375 return 0;
1376 }
1377 /*
1378 * The rest of the Smack xattrs are only on sockets.
1379 */
1380 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1381 return -EOPNOTSUPP;
1382
1383 sock = SOCKET_I(inode);
1384 if (sock == NULL || sock->sk == NULL)
1385 return -EOPNOTSUPP;
1386
1387 ssp = sock->sk->sk_security;
1388
1389 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1390 ssp->smk_in = sp;
1391 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1392 ssp->smk_out = sp;
1393 rc = smack_netlabel(sock->sk);
1394 if (rc != 0)
1395 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1396 __func__, -rc);
1397 } else
1398 return -EOPNOTSUPP;
1399
1400 return 0;
1401 }
1402
1403 /**
1404 * smack_socket_post_create - finish socket setup
1405 * @sock: the socket
1406 * @family: protocol family
1407 * @type: unused
1408 * @protocol: unused
1409 * @kern: unused
1410 *
1411 * Sets the netlabel information on the socket
1412 *
1413 * Returns 0 on success, and error code otherwise
1414 */
1415 static int smack_socket_post_create(struct socket *sock, int family,
1416 int type, int protocol, int kern)
1417 {
1418 if (family != PF_INET || sock->sk == NULL)
1419 return 0;
1420 /*
1421 * Set the outbound netlbl.
1422 */
1423 return smack_netlabel(sock->sk);
1424 }
1425
1426 /**
1427 * smack_flags_to_may - convert S_ to MAY_ values
1428 * @flags: the S_ value
1429 *
1430 * Returns the equivalent MAY_ value
1431 */
1432 static int smack_flags_to_may(int flags)
1433 {
1434 int may = 0;
1435
1436 if (flags & S_IRUGO)
1437 may |= MAY_READ;
1438 if (flags & S_IWUGO)
1439 may |= MAY_WRITE;
1440 if (flags & S_IXUGO)
1441 may |= MAY_EXEC;
1442
1443 return may;
1444 }
1445
1446 /**
1447 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1448 * @msg: the object
1449 *
1450 * Returns 0
1451 */
1452 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1453 {
1454 msg->security = current_security();
1455 return 0;
1456 }
1457
1458 /**
1459 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1460 * @msg: the object
1461 *
1462 * Clears the blob pointer
1463 */
1464 static void smack_msg_msg_free_security(struct msg_msg *msg)
1465 {
1466 msg->security = NULL;
1467 }
1468
1469 /**
1470 * smack_of_shm - the smack pointer for the shm
1471 * @shp: the object
1472 *
1473 * Returns a pointer to the smack value
1474 */
1475 static char *smack_of_shm(struct shmid_kernel *shp)
1476 {
1477 return (char *)shp->shm_perm.security;
1478 }
1479
1480 /**
1481 * smack_shm_alloc_security - Set the security blob for shm
1482 * @shp: the object
1483 *
1484 * Returns 0
1485 */
1486 static int smack_shm_alloc_security(struct shmid_kernel *shp)
1487 {
1488 struct kern_ipc_perm *isp = &shp->shm_perm;
1489
1490 isp->security = current_security();
1491 return 0;
1492 }
1493
1494 /**
1495 * smack_shm_free_security - Clear the security blob for shm
1496 * @shp: the object
1497 *
1498 * Clears the blob pointer
1499 */
1500 static void smack_shm_free_security(struct shmid_kernel *shp)
1501 {
1502 struct kern_ipc_perm *isp = &shp->shm_perm;
1503
1504 isp->security = NULL;
1505 }
1506
1507 /**
1508 * smack_shm_associate - Smack access check for shm
1509 * @shp: the object
1510 * @shmflg: access requested
1511 *
1512 * Returns 0 if current has the requested access, error code otherwise
1513 */
1514 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1515 {
1516 char *ssp = smack_of_shm(shp);
1517 int may;
1518
1519 may = smack_flags_to_may(shmflg);
1520 return smk_curacc(ssp, may);
1521 }
1522
1523 /**
1524 * smack_shm_shmctl - Smack access check for shm
1525 * @shp: the object
1526 * @cmd: what it wants to do
1527 *
1528 * Returns 0 if current has the requested access, error code otherwise
1529 */
1530 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1531 {
1532 char *ssp;
1533 int may;
1534
1535 switch (cmd) {
1536 case IPC_STAT:
1537 case SHM_STAT:
1538 may = MAY_READ;
1539 break;
1540 case IPC_SET:
1541 case SHM_LOCK:
1542 case SHM_UNLOCK:
1543 case IPC_RMID:
1544 may = MAY_READWRITE;
1545 break;
1546 case IPC_INFO:
1547 case SHM_INFO:
1548 /*
1549 * System level information.
1550 */
1551 return 0;
1552 default:
1553 return -EINVAL;
1554 }
1555
1556 ssp = smack_of_shm(shp);
1557 return smk_curacc(ssp, may);
1558 }
1559
1560 /**
1561 * smack_shm_shmat - Smack access for shmat
1562 * @shp: the object
1563 * @shmaddr: unused
1564 * @shmflg: access requested
1565 *
1566 * Returns 0 if current has the requested access, error code otherwise
1567 */
1568 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1569 int shmflg)
1570 {
1571 char *ssp = smack_of_shm(shp);
1572 int may;
1573
1574 may = smack_flags_to_may(shmflg);
1575 return smk_curacc(ssp, may);
1576 }
1577
1578 /**
1579 * smack_of_sem - the smack pointer for the sem
1580 * @sma: the object
1581 *
1582 * Returns a pointer to the smack value
1583 */
1584 static char *smack_of_sem(struct sem_array *sma)
1585 {
1586 return (char *)sma->sem_perm.security;
1587 }
1588
1589 /**
1590 * smack_sem_alloc_security - Set the security blob for sem
1591 * @sma: the object
1592 *
1593 * Returns 0
1594 */
1595 static int smack_sem_alloc_security(struct sem_array *sma)
1596 {
1597 struct kern_ipc_perm *isp = &sma->sem_perm;
1598
1599 isp->security = current_security();
1600 return 0;
1601 }
1602
1603 /**
1604 * smack_sem_free_security - Clear the security blob for sem
1605 * @sma: the object
1606 *
1607 * Clears the blob pointer
1608 */
1609 static void smack_sem_free_security(struct sem_array *sma)
1610 {
1611 struct kern_ipc_perm *isp = &sma->sem_perm;
1612
1613 isp->security = NULL;
1614 }
1615
1616 /**
1617 * smack_sem_associate - Smack access check for sem
1618 * @sma: the object
1619 * @semflg: access requested
1620 *
1621 * Returns 0 if current has the requested access, error code otherwise
1622 */
1623 static int smack_sem_associate(struct sem_array *sma, int semflg)
1624 {
1625 char *ssp = smack_of_sem(sma);
1626 int may;
1627
1628 may = smack_flags_to_may(semflg);
1629 return smk_curacc(ssp, may);
1630 }
1631
1632 /**
1633 * smack_sem_shmctl - Smack access check for sem
1634 * @sma: the object
1635 * @cmd: what it wants to do
1636 *
1637 * Returns 0 if current has the requested access, error code otherwise
1638 */
1639 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1640 {
1641 char *ssp;
1642 int may;
1643
1644 switch (cmd) {
1645 case GETPID:
1646 case GETNCNT:
1647 case GETZCNT:
1648 case GETVAL:
1649 case GETALL:
1650 case IPC_STAT:
1651 case SEM_STAT:
1652 may = MAY_READ;
1653 break;
1654 case SETVAL:
1655 case SETALL:
1656 case IPC_RMID:
1657 case IPC_SET:
1658 may = MAY_READWRITE;
1659 break;
1660 case IPC_INFO:
1661 case SEM_INFO:
1662 /*
1663 * System level information
1664 */
1665 return 0;
1666 default:
1667 return -EINVAL;
1668 }
1669
1670 ssp = smack_of_sem(sma);
1671 return smk_curacc(ssp, may);
1672 }
1673
1674 /**
1675 * smack_sem_semop - Smack checks of semaphore operations
1676 * @sma: the object
1677 * @sops: unused
1678 * @nsops: unused
1679 * @alter: unused
1680 *
1681 * Treated as read and write in all cases.
1682 *
1683 * Returns 0 if access is allowed, error code otherwise
1684 */
1685 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
1686 unsigned nsops, int alter)
1687 {
1688 char *ssp = smack_of_sem(sma);
1689
1690 return smk_curacc(ssp, MAY_READWRITE);
1691 }
1692
1693 /**
1694 * smack_msg_alloc_security - Set the security blob for msg
1695 * @msq: the object
1696 *
1697 * Returns 0
1698 */
1699 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
1700 {
1701 struct kern_ipc_perm *kisp = &msq->q_perm;
1702
1703 kisp->security = current_security();
1704 return 0;
1705 }
1706
1707 /**
1708 * smack_msg_free_security - Clear the security blob for msg
1709 * @msq: the object
1710 *
1711 * Clears the blob pointer
1712 */
1713 static void smack_msg_queue_free_security(struct msg_queue *msq)
1714 {
1715 struct kern_ipc_perm *kisp = &msq->q_perm;
1716
1717 kisp->security = NULL;
1718 }
1719
1720 /**
1721 * smack_of_msq - the smack pointer for the msq
1722 * @msq: the object
1723 *
1724 * Returns a pointer to the smack value
1725 */
1726 static char *smack_of_msq(struct msg_queue *msq)
1727 {
1728 return (char *)msq->q_perm.security;
1729 }
1730
1731 /**
1732 * smack_msg_queue_associate - Smack access check for msg_queue
1733 * @msq: the object
1734 * @msqflg: access requested
1735 *
1736 * Returns 0 if current has the requested access, error code otherwise
1737 */
1738 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
1739 {
1740 char *msp = smack_of_msq(msq);
1741 int may;
1742
1743 may = smack_flags_to_may(msqflg);
1744 return smk_curacc(msp, may);
1745 }
1746
1747 /**
1748 * smack_msg_queue_msgctl - Smack access check for msg_queue
1749 * @msq: the object
1750 * @cmd: what it wants to do
1751 *
1752 * Returns 0 if current has the requested access, error code otherwise
1753 */
1754 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1755 {
1756 char *msp;
1757 int may;
1758
1759 switch (cmd) {
1760 case IPC_STAT:
1761 case MSG_STAT:
1762 may = MAY_READ;
1763 break;
1764 case IPC_SET:
1765 case IPC_RMID:
1766 may = MAY_READWRITE;
1767 break;
1768 case IPC_INFO:
1769 case MSG_INFO:
1770 /*
1771 * System level information
1772 */
1773 return 0;
1774 default:
1775 return -EINVAL;
1776 }
1777
1778 msp = smack_of_msq(msq);
1779 return smk_curacc(msp, may);
1780 }
1781
1782 /**
1783 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1784 * @msq: the object
1785 * @msg: unused
1786 * @msqflg: access requested
1787 *
1788 * Returns 0 if current has the requested access, error code otherwise
1789 */
1790 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
1791 int msqflg)
1792 {
1793 char *msp = smack_of_msq(msq);
1794 int rc;
1795
1796 rc = smack_flags_to_may(msqflg);
1797 return smk_curacc(msp, rc);
1798 }
1799
1800 /**
1801 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1802 * @msq: the object
1803 * @msg: unused
1804 * @target: unused
1805 * @type: unused
1806 * @mode: unused
1807 *
1808 * Returns 0 if current has read and write access, error code otherwise
1809 */
1810 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1811 struct task_struct *target, long type, int mode)
1812 {
1813 char *msp = smack_of_msq(msq);
1814
1815 return smk_curacc(msp, MAY_READWRITE);
1816 }
1817
1818 /**
1819 * smack_ipc_permission - Smack access for ipc_permission()
1820 * @ipp: the object permissions
1821 * @flag: access requested
1822 *
1823 * Returns 0 if current has read and write access, error code otherwise
1824 */
1825 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
1826 {
1827 char *isp = ipp->security;
1828 int may;
1829
1830 may = smack_flags_to_may(flag);
1831 return smk_curacc(isp, may);
1832 }
1833
1834 /**
1835 * smack_ipc_getsecid - Extract smack security id
1836 * @ipcp: the object permissions
1837 * @secid: where result will be saved
1838 */
1839 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
1840 {
1841 char *smack = ipp->security;
1842
1843 *secid = smack_to_secid(smack);
1844 }
1845
1846 /**
1847 * smack_d_instantiate - Make sure the blob is correct on an inode
1848 * @opt_dentry: unused
1849 * @inode: the object
1850 *
1851 * Set the inode's security blob if it hasn't been done already.
1852 */
1853 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
1854 {
1855 struct super_block *sbp;
1856 struct superblock_smack *sbsp;
1857 struct inode_smack *isp;
1858 char *csp = current_security();
1859 char *fetched;
1860 char *final;
1861 struct dentry *dp;
1862
1863 if (inode == NULL)
1864 return;
1865
1866 isp = inode->i_security;
1867
1868 mutex_lock(&isp->smk_lock);
1869 /*
1870 * If the inode is already instantiated
1871 * take the quick way out
1872 */
1873 if (isp->smk_flags & SMK_INODE_INSTANT)
1874 goto unlockandout;
1875
1876 sbp = inode->i_sb;
1877 sbsp = sbp->s_security;
1878 /*
1879 * We're going to use the superblock default label
1880 * if there's no label on the file.
1881 */
1882 final = sbsp->smk_default;
1883
1884 /*
1885 * If this is the root inode the superblock
1886 * may be in the process of initialization.
1887 * If that is the case use the root value out
1888 * of the superblock.
1889 */
1890 if (opt_dentry->d_parent == opt_dentry) {
1891 isp->smk_inode = sbsp->smk_root;
1892 isp->smk_flags |= SMK_INODE_INSTANT;
1893 goto unlockandout;
1894 }
1895
1896 /*
1897 * This is pretty hackish.
1898 * Casey says that we shouldn't have to do
1899 * file system specific code, but it does help
1900 * with keeping it simple.
1901 */
1902 switch (sbp->s_magic) {
1903 case SMACK_MAGIC:
1904 /*
1905 * Casey says that it's a little embarassing
1906 * that the smack file system doesn't do
1907 * extended attributes.
1908 */
1909 final = smack_known_star.smk_known;
1910 break;
1911 case PIPEFS_MAGIC:
1912 /*
1913 * Casey says pipes are easy (?)
1914 */
1915 final = smack_known_star.smk_known;
1916 break;
1917 case DEVPTS_SUPER_MAGIC:
1918 /*
1919 * devpts seems content with the label of the task.
1920 * Programs that change smack have to treat the
1921 * pty with respect.
1922 */
1923 final = csp;
1924 break;
1925 case SOCKFS_MAGIC:
1926 /*
1927 * Casey says sockets get the smack of the task.
1928 */
1929 final = csp;
1930 break;
1931 case PROC_SUPER_MAGIC:
1932 /*
1933 * Casey says procfs appears not to care.
1934 * The superblock default suffices.
1935 */
1936 break;
1937 case TMPFS_MAGIC:
1938 /*
1939 * Device labels should come from the filesystem,
1940 * but watch out, because they're volitile,
1941 * getting recreated on every reboot.
1942 */
1943 final = smack_known_star.smk_known;
1944 /*
1945 * No break.
1946 *
1947 * If a smack value has been set we want to use it,
1948 * but since tmpfs isn't giving us the opportunity
1949 * to set mount options simulate setting the
1950 * superblock default.
1951 */
1952 default:
1953 /*
1954 * This isn't an understood special case.
1955 * Get the value from the xattr.
1956 *
1957 * No xattr support means, alas, no SMACK label.
1958 * Use the aforeapplied default.
1959 * It would be curious if the label of the task
1960 * does not match that assigned.
1961 */
1962 if (inode->i_op->getxattr == NULL)
1963 break;
1964 /*
1965 * Get the dentry for xattr.
1966 */
1967 if (opt_dentry == NULL) {
1968 dp = d_find_alias(inode);
1969 if (dp == NULL)
1970 break;
1971 } else {
1972 dp = dget(opt_dentry);
1973 if (dp == NULL)
1974 break;
1975 }
1976
1977 fetched = smk_fetch(inode, dp);
1978 if (fetched != NULL)
1979 final = fetched;
1980
1981 dput(dp);
1982 break;
1983 }
1984
1985 if (final == NULL)
1986 isp->smk_inode = csp;
1987 else
1988 isp->smk_inode = final;
1989
1990 isp->smk_flags |= SMK_INODE_INSTANT;
1991
1992 unlockandout:
1993 mutex_unlock(&isp->smk_lock);
1994 return;
1995 }
1996
1997 /**
1998 * smack_getprocattr - Smack process attribute access
1999 * @p: the object task
2000 * @name: the name of the attribute in /proc/.../attr
2001 * @value: where to put the result
2002 *
2003 * Places a copy of the task Smack into value
2004 *
2005 * Returns the length of the smack label or an error code
2006 */
2007 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2008 {
2009 char *cp;
2010 int slen;
2011
2012 if (strcmp(name, "current") != 0)
2013 return -EINVAL;
2014
2015 cp = kstrdup(task_security(p), GFP_KERNEL);
2016 if (cp == NULL)
2017 return -ENOMEM;
2018
2019 slen = strlen(cp);
2020 *value = cp;
2021 return slen;
2022 }
2023
2024 /**
2025 * smack_setprocattr - Smack process attribute setting
2026 * @p: the object task
2027 * @name: the name of the attribute in /proc/.../attr
2028 * @value: the value to set
2029 * @size: the size of the value
2030 *
2031 * Sets the Smack value of the task. Only setting self
2032 * is permitted and only with privilege
2033 *
2034 * Returns the length of the smack label or an error code
2035 */
2036 static int smack_setprocattr(struct task_struct *p, char *name,
2037 void *value, size_t size)
2038 {
2039 char *newsmack;
2040
2041 /*
2042 * Changing another process' Smack value is too dangerous
2043 * and supports no sane use case.
2044 */
2045 if (p != current)
2046 return -EPERM;
2047
2048 if (!capable(CAP_MAC_ADMIN))
2049 return -EPERM;
2050
2051 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2052 return -EINVAL;
2053
2054 if (strcmp(name, "current") != 0)
2055 return -EINVAL;
2056
2057 newsmack = smk_import(value, size);
2058 if (newsmack == NULL)
2059 return -EINVAL;
2060
2061 p->cred->security = newsmack;
2062 return size;
2063 }
2064
2065 /**
2066 * smack_unix_stream_connect - Smack access on UDS
2067 * @sock: one socket
2068 * @other: the other socket
2069 * @newsk: unused
2070 *
2071 * Return 0 if a subject with the smack of sock could access
2072 * an object with the smack of other, otherwise an error code
2073 */
2074 static int smack_unix_stream_connect(struct socket *sock,
2075 struct socket *other, struct sock *newsk)
2076 {
2077 struct inode *sp = SOCK_INODE(sock);
2078 struct inode *op = SOCK_INODE(other);
2079
2080 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
2081 }
2082
2083 /**
2084 * smack_unix_may_send - Smack access on UDS
2085 * @sock: one socket
2086 * @other: the other socket
2087 *
2088 * Return 0 if a subject with the smack of sock could access
2089 * an object with the smack of other, otherwise an error code
2090 */
2091 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2092 {
2093 struct inode *sp = SOCK_INODE(sock);
2094 struct inode *op = SOCK_INODE(other);
2095
2096 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
2097 }
2098
2099 /**
2100 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat
2101 * pair to smack
2102 * @sap: netlabel secattr
2103 * @sip: where to put the result
2104 *
2105 * Copies a smack label into sip
2106 */
2107 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2108 {
2109 char smack[SMK_LABELLEN];
2110 int pcat;
2111
2112 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) == 0) {
2113 /*
2114 * If there are flags but no level netlabel isn't
2115 * behaving the way we expect it to.
2116 *
2117 * Without guidance regarding the smack value
2118 * for the packet fall back on the network
2119 * ambient value.
2120 */
2121 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2122 return;
2123 }
2124 /*
2125 * Get the categories, if any
2126 */
2127 memset(smack, '\0', SMK_LABELLEN);
2128 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2129 for (pcat = -1;;) {
2130 pcat = netlbl_secattr_catmap_walk(sap->attr.mls.cat,
2131 pcat + 1);
2132 if (pcat < 0)
2133 break;
2134 smack_catset_bit(pcat, smack);
2135 }
2136 /*
2137 * If it is CIPSO using smack direct mapping
2138 * we are already done. WeeHee.
2139 */
2140 if (sap->attr.mls.lvl == smack_cipso_direct) {
2141 memcpy(sip, smack, SMK_MAXLEN);
2142 return;
2143 }
2144 /*
2145 * Look it up in the supplied table if it is not a direct mapping.
2146 */
2147 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2148 return;
2149 }
2150
2151 /**
2152 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2153 * @sk: socket
2154 * @skb: packet
2155 *
2156 * Returns 0 if the packet should be delivered, an error code otherwise
2157 */
2158 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2159 {
2160 struct netlbl_lsm_secattr secattr;
2161 struct socket_smack *ssp = sk->sk_security;
2162 char smack[SMK_LABELLEN];
2163 int rc;
2164
2165 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2166 return 0;
2167
2168 /*
2169 * Translate what netlabel gave us.
2170 */
2171 memset(smack, '\0', SMK_LABELLEN);
2172 netlbl_secattr_init(&secattr);
2173 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2174 if (rc == 0)
2175 smack_from_secattr(&secattr, smack);
2176 else
2177 strncpy(smack, smack_net_ambient, SMK_MAXLEN);
2178 netlbl_secattr_destroy(&secattr);
2179 /*
2180 * Receiving a packet requires that the other end
2181 * be able to write here. Read access is not required.
2182 * This is the simplist possible security model
2183 * for networking.
2184 */
2185 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2186 if (rc != 0)
2187 netlbl_skbuff_err(skb, rc, 0);
2188 return rc;
2189 }
2190
2191 /**
2192 * smack_socket_getpeersec_stream - pull in packet label
2193 * @sock: the socket
2194 * @optval: user's destination
2195 * @optlen: size thereof
2196 * @len: max thereoe
2197 *
2198 * returns zero on success, an error code otherwise
2199 */
2200 static int smack_socket_getpeersec_stream(struct socket *sock,
2201 char __user *optval,
2202 int __user *optlen, unsigned len)
2203 {
2204 struct socket_smack *ssp;
2205 int slen;
2206 int rc = 0;
2207
2208 ssp = sock->sk->sk_security;
2209 slen = strlen(ssp->smk_packet) + 1;
2210
2211 if (slen > len)
2212 rc = -ERANGE;
2213 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2214 rc = -EFAULT;
2215
2216 if (put_user(slen, optlen) != 0)
2217 rc = -EFAULT;
2218
2219 return rc;
2220 }
2221
2222
2223 /**
2224 * smack_socket_getpeersec_dgram - pull in packet label
2225 * @sock: the socket
2226 * @skb: packet data
2227 * @secid: pointer to where to put the secid of the packet
2228 *
2229 * Sets the netlabel socket state on sk from parent
2230 */
2231 static int smack_socket_getpeersec_dgram(struct socket *sock,
2232 struct sk_buff *skb, u32 *secid)
2233
2234 {
2235 struct netlbl_lsm_secattr secattr;
2236 struct sock *sk;
2237 char smack[SMK_LABELLEN];
2238 int family = PF_INET;
2239 u32 s;
2240 int rc;
2241
2242 /*
2243 * Only works for families with packets.
2244 */
2245 if (sock != NULL) {
2246 sk = sock->sk;
2247 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2248 return 0;
2249 family = sk->sk_family;
2250 }
2251 /*
2252 * Translate what netlabel gave us.
2253 */
2254 memset(smack, '\0', SMK_LABELLEN);
2255 netlbl_secattr_init(&secattr);
2256 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2257 if (rc == 0)
2258 smack_from_secattr(&secattr, smack);
2259 netlbl_secattr_destroy(&secattr);
2260
2261 /*
2262 * Give up if we couldn't get anything
2263 */
2264 if (rc != 0)
2265 return rc;
2266
2267 s = smack_to_secid(smack);
2268 if (s == 0)
2269 return -EINVAL;
2270
2271 *secid = s;
2272 return 0;
2273 }
2274
2275 /**
2276 * smack_sock_graft - graft access state between two sockets
2277 * @sk: fresh sock
2278 * @parent: donor socket
2279 *
2280 * Sets the netlabel socket state on sk from parent
2281 */
2282 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2283 {
2284 struct socket_smack *ssp;
2285 int rc;
2286
2287 if (sk == NULL)
2288 return;
2289
2290 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2291 return;
2292
2293 ssp = sk->sk_security;
2294 ssp->smk_in = ssp->smk_out = current_security();
2295 ssp->smk_packet[0] = '\0';
2296
2297 rc = smack_netlabel(sk);
2298 if (rc != 0)
2299 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
2300 __func__, -rc);
2301 }
2302
2303 /**
2304 * smack_inet_conn_request - Smack access check on connect
2305 * @sk: socket involved
2306 * @skb: packet
2307 * @req: unused
2308 *
2309 * Returns 0 if a task with the packet label could write to
2310 * the socket, otherwise an error code
2311 */
2312 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2313 struct request_sock *req)
2314 {
2315 struct netlbl_lsm_secattr skb_secattr;
2316 struct socket_smack *ssp = sk->sk_security;
2317 char smack[SMK_LABELLEN];
2318 int rc;
2319
2320 if (skb == NULL)
2321 return -EACCES;
2322
2323 memset(smack, '\0', SMK_LABELLEN);
2324 netlbl_secattr_init(&skb_secattr);
2325 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr);
2326 if (rc == 0)
2327 smack_from_secattr(&skb_secattr, smack);
2328 else
2329 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2330 netlbl_secattr_destroy(&skb_secattr);
2331 /*
2332 * Receiving a packet requires that the other end
2333 * be able to write here. Read access is not required.
2334 *
2335 * If the request is successful save the peer's label
2336 * so that SO_PEERCRED can report it.
2337 */
2338 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2339 if (rc == 0)
2340 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2341
2342 return rc;
2343 }
2344
2345 /*
2346 * Key management security hooks
2347 *
2348 * Casey has not tested key support very heavily.
2349 * The permission check is most likely too restrictive.
2350 * If you care about keys please have a look.
2351 */
2352 #ifdef CONFIG_KEYS
2353
2354 /**
2355 * smack_key_alloc - Set the key security blob
2356 * @key: object
2357 * @tsk: the task associated with the key
2358 * @flags: unused
2359 *
2360 * No allocation required
2361 *
2362 * Returns 0
2363 */
2364 static int smack_key_alloc(struct key *key, struct task_struct *tsk,
2365 unsigned long flags)
2366 {
2367 key->security = tsk->cred->security;
2368 return 0;
2369 }
2370
2371 /**
2372 * smack_key_free - Clear the key security blob
2373 * @key: the object
2374 *
2375 * Clear the blob pointer
2376 */
2377 static void smack_key_free(struct key *key)
2378 {
2379 key->security = NULL;
2380 }
2381
2382 /*
2383 * smack_key_permission - Smack access on a key
2384 * @key_ref: gets to the object
2385 * @context: task involved
2386 * @perm: unused
2387 *
2388 * Return 0 if the task has read and write to the object,
2389 * an error code otherwise
2390 */
2391 static int smack_key_permission(key_ref_t key_ref,
2392 struct task_struct *context, key_perm_t perm)
2393 {
2394 struct key *keyp;
2395
2396 keyp = key_ref_to_ptr(key_ref);
2397 if (keyp == NULL)
2398 return -EINVAL;
2399 /*
2400 * If the key hasn't been initialized give it access so that
2401 * it may do so.
2402 */
2403 if (keyp->security == NULL)
2404 return 0;
2405 /*
2406 * This should not occur
2407 */
2408 if (context->cred->security == NULL)
2409 return -EACCES;
2410
2411 return smk_access(context->cred->security, keyp->security,
2412 MAY_READWRITE);
2413 }
2414 #endif /* CONFIG_KEYS */
2415
2416 /*
2417 * Smack Audit hooks
2418 *
2419 * Audit requires a unique representation of each Smack specific
2420 * rule. This unique representation is used to distinguish the
2421 * object to be audited from remaining kernel objects and also
2422 * works as a glue between the audit hooks.
2423 *
2424 * Since repository entries are added but never deleted, we'll use
2425 * the smack_known label address related to the given audit rule as
2426 * the needed unique representation. This also better fits the smack
2427 * model where nearly everything is a label.
2428 */
2429 #ifdef CONFIG_AUDIT
2430
2431 /**
2432 * smack_audit_rule_init - Initialize a smack audit rule
2433 * @field: audit rule fields given from user-space (audit.h)
2434 * @op: required testing operator (=, !=, >, <, ...)
2435 * @rulestr: smack label to be audited
2436 * @vrule: pointer to save our own audit rule representation
2437 *
2438 * Prepare to audit cases where (@field @op @rulestr) is true.
2439 * The label to be audited is created if necessay.
2440 */
2441 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2442 {
2443 char **rule = (char **)vrule;
2444 *rule = NULL;
2445
2446 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2447 return -EINVAL;
2448
2449 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2450 return -EINVAL;
2451
2452 *rule = smk_import(rulestr, 0);
2453
2454 return 0;
2455 }
2456
2457 /**
2458 * smack_audit_rule_known - Distinguish Smack audit rules
2459 * @krule: rule of interest, in Audit kernel representation format
2460 *
2461 * This is used to filter Smack rules from remaining Audit ones.
2462 * If it's proved that this rule belongs to us, the
2463 * audit_rule_match hook will be called to do the final judgement.
2464 */
2465 static int smack_audit_rule_known(struct audit_krule *krule)
2466 {
2467 struct audit_field *f;
2468 int i;
2469
2470 for (i = 0; i < krule->field_count; i++) {
2471 f = &krule->fields[i];
2472
2473 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2474 return 1;
2475 }
2476
2477 return 0;
2478 }
2479
2480 /**
2481 * smack_audit_rule_match - Audit given object ?
2482 * @secid: security id for identifying the object to test
2483 * @field: audit rule flags given from user-space
2484 * @op: required testing operator
2485 * @vrule: smack internal rule presentation
2486 * @actx: audit context associated with the check
2487 *
2488 * The core Audit hook. It's used to take the decision of
2489 * whether to audit or not to audit a given object.
2490 */
2491 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2492 struct audit_context *actx)
2493 {
2494 char *smack;
2495 char *rule = vrule;
2496
2497 if (!rule) {
2498 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2499 "Smack: missing rule\n");
2500 return -ENOENT;
2501 }
2502
2503 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2504 return 0;
2505
2506 smack = smack_from_secid(secid);
2507
2508 /*
2509 * No need to do string comparisons. If a match occurs,
2510 * both pointers will point to the same smack_known
2511 * label.
2512 */
2513 if (op == AUDIT_EQUAL)
2514 return (rule == smack);
2515 if (op == AUDIT_NOT_EQUAL)
2516 return (rule != smack);
2517
2518 return 0;
2519 }
2520
2521 /**
2522 * smack_audit_rule_free - free smack rule representation
2523 * @vrule: rule to be freed.
2524 *
2525 * No memory was allocated.
2526 */
2527 static void smack_audit_rule_free(void *vrule)
2528 {
2529 /* No-op */
2530 }
2531
2532 #endif /* CONFIG_AUDIT */
2533
2534 /*
2535 * smack_secid_to_secctx - return the smack label for a secid
2536 * @secid: incoming integer
2537 * @secdata: destination
2538 * @seclen: how long it is
2539 *
2540 * Exists for networking code.
2541 */
2542 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2543 {
2544 char *sp = smack_from_secid(secid);
2545
2546 *secdata = sp;
2547 *seclen = strlen(sp);
2548 return 0;
2549 }
2550
2551 /*
2552 * smack_secctx_to_secid - return the secid for a smack label
2553 * @secdata: smack label
2554 * @seclen: how long result is
2555 * @secid: outgoing integer
2556 *
2557 * Exists for audit and networking code.
2558 */
2559 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
2560 {
2561 *secid = smack_to_secid(secdata);
2562 return 0;
2563 }
2564
2565 /*
2566 * smack_release_secctx - don't do anything.
2567 * @key_ref: unused
2568 * @context: unused
2569 * @perm: unused
2570 *
2571 * Exists to make sure nothing gets done, and properly
2572 */
2573 static void smack_release_secctx(char *secdata, u32 seclen)
2574 {
2575 }
2576
2577 struct security_operations smack_ops = {
2578 .name = "smack",
2579
2580 .ptrace_may_access = smack_ptrace_may_access,
2581 .ptrace_traceme = smack_ptrace_traceme,
2582 .capget = cap_capget,
2583 .capset_check = cap_capset_check,
2584 .capset_set = cap_capset_set,
2585 .capable = cap_capable,
2586 .syslog = smack_syslog,
2587 .settime = cap_settime,
2588 .vm_enough_memory = cap_vm_enough_memory,
2589
2590 .bprm_apply_creds = cap_bprm_apply_creds,
2591 .bprm_set_security = cap_bprm_set_security,
2592 .bprm_secureexec = cap_bprm_secureexec,
2593
2594 .sb_alloc_security = smack_sb_alloc_security,
2595 .sb_free_security = smack_sb_free_security,
2596 .sb_copy_data = smack_sb_copy_data,
2597 .sb_kern_mount = smack_sb_kern_mount,
2598 .sb_statfs = smack_sb_statfs,
2599 .sb_mount = smack_sb_mount,
2600 .sb_umount = smack_sb_umount,
2601
2602 .inode_alloc_security = smack_inode_alloc_security,
2603 .inode_free_security = smack_inode_free_security,
2604 .inode_init_security = smack_inode_init_security,
2605 .inode_link = smack_inode_link,
2606 .inode_unlink = smack_inode_unlink,
2607 .inode_rmdir = smack_inode_rmdir,
2608 .inode_rename = smack_inode_rename,
2609 .inode_permission = smack_inode_permission,
2610 .inode_setattr = smack_inode_setattr,
2611 .inode_getattr = smack_inode_getattr,
2612 .inode_setxattr = smack_inode_setxattr,
2613 .inode_post_setxattr = smack_inode_post_setxattr,
2614 .inode_getxattr = smack_inode_getxattr,
2615 .inode_removexattr = smack_inode_removexattr,
2616 .inode_need_killpriv = cap_inode_need_killpriv,
2617 .inode_killpriv = cap_inode_killpriv,
2618 .inode_getsecurity = smack_inode_getsecurity,
2619 .inode_setsecurity = smack_inode_setsecurity,
2620 .inode_listsecurity = smack_inode_listsecurity,
2621 .inode_getsecid = smack_inode_getsecid,
2622
2623 .file_permission = smack_file_permission,
2624 .file_alloc_security = smack_file_alloc_security,
2625 .file_free_security = smack_file_free_security,
2626 .file_ioctl = smack_file_ioctl,
2627 .file_lock = smack_file_lock,
2628 .file_fcntl = smack_file_fcntl,
2629 .file_set_fowner = smack_file_set_fowner,
2630 .file_send_sigiotask = smack_file_send_sigiotask,
2631 .file_receive = smack_file_receive,
2632
2633 .cred_alloc_security = smack_cred_alloc_security,
2634 .cred_free = smack_cred_free,
2635 .task_post_setuid = cap_task_post_setuid,
2636 .task_setpgid = smack_task_setpgid,
2637 .task_getpgid = smack_task_getpgid,
2638 .task_getsid = smack_task_getsid,
2639 .task_getsecid = smack_task_getsecid,
2640 .task_setnice = smack_task_setnice,
2641 .task_setioprio = smack_task_setioprio,
2642 .task_getioprio = smack_task_getioprio,
2643 .task_setscheduler = smack_task_setscheduler,
2644 .task_getscheduler = smack_task_getscheduler,
2645 .task_movememory = smack_task_movememory,
2646 .task_kill = smack_task_kill,
2647 .task_wait = smack_task_wait,
2648 .task_reparent_to_init = cap_task_reparent_to_init,
2649 .task_to_inode = smack_task_to_inode,
2650 .task_prctl = cap_task_prctl,
2651
2652 .ipc_permission = smack_ipc_permission,
2653 .ipc_getsecid = smack_ipc_getsecid,
2654
2655 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
2656 .msg_msg_free_security = smack_msg_msg_free_security,
2657
2658 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
2659 .msg_queue_free_security = smack_msg_queue_free_security,
2660 .msg_queue_associate = smack_msg_queue_associate,
2661 .msg_queue_msgctl = smack_msg_queue_msgctl,
2662 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
2663 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
2664
2665 .shm_alloc_security = smack_shm_alloc_security,
2666 .shm_free_security = smack_shm_free_security,
2667 .shm_associate = smack_shm_associate,
2668 .shm_shmctl = smack_shm_shmctl,
2669 .shm_shmat = smack_shm_shmat,
2670
2671 .sem_alloc_security = smack_sem_alloc_security,
2672 .sem_free_security = smack_sem_free_security,
2673 .sem_associate = smack_sem_associate,
2674 .sem_semctl = smack_sem_semctl,
2675 .sem_semop = smack_sem_semop,
2676
2677 .netlink_send = cap_netlink_send,
2678 .netlink_recv = cap_netlink_recv,
2679
2680 .d_instantiate = smack_d_instantiate,
2681
2682 .getprocattr = smack_getprocattr,
2683 .setprocattr = smack_setprocattr,
2684
2685 .unix_stream_connect = smack_unix_stream_connect,
2686 .unix_may_send = smack_unix_may_send,
2687
2688 .socket_post_create = smack_socket_post_create,
2689 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
2690 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
2691 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
2692 .sk_alloc_security = smack_sk_alloc_security,
2693 .sk_free_security = smack_sk_free_security,
2694 .sock_graft = smack_sock_graft,
2695 .inet_conn_request = smack_inet_conn_request,
2696
2697 /* key management security hooks */
2698 #ifdef CONFIG_KEYS
2699 .key_alloc = smack_key_alloc,
2700 .key_free = smack_key_free,
2701 .key_permission = smack_key_permission,
2702 #endif /* CONFIG_KEYS */
2703
2704 /* Audit hooks */
2705 #ifdef CONFIG_AUDIT
2706 .audit_rule_init = smack_audit_rule_init,
2707 .audit_rule_known = smack_audit_rule_known,
2708 .audit_rule_match = smack_audit_rule_match,
2709 .audit_rule_free = smack_audit_rule_free,
2710 #endif /* CONFIG_AUDIT */
2711
2712 .secid_to_secctx = smack_secid_to_secctx,
2713 .secctx_to_secid = smack_secctx_to_secid,
2714 .release_secctx = smack_release_secctx,
2715 };
2716
2717 /**
2718 * smack_init - initialize the smack system
2719 *
2720 * Returns 0
2721 */
2722 static __init int smack_init(void)
2723 {
2724 if (!security_module_enable(&smack_ops))
2725 return 0;
2726
2727 printk(KERN_INFO "Smack: Initializing.\n");
2728
2729 /*
2730 * Set the security state for the initial task.
2731 */
2732 current->cred->security = &smack_known_floor.smk_known;
2733
2734 /*
2735 * Initialize locks
2736 */
2737 spin_lock_init(&smack_known_unset.smk_cipsolock);
2738 spin_lock_init(&smack_known_huh.smk_cipsolock);
2739 spin_lock_init(&smack_known_hat.smk_cipsolock);
2740 spin_lock_init(&smack_known_star.smk_cipsolock);
2741 spin_lock_init(&smack_known_floor.smk_cipsolock);
2742 spin_lock_init(&smack_known_invalid.smk_cipsolock);
2743
2744 /*
2745 * Register with LSM
2746 */
2747 if (register_security(&smack_ops))
2748 panic("smack: Unable to register with kernel.\n");
2749
2750 return 0;
2751 }
2752
2753 /*
2754 * Smack requires early initialization in order to label
2755 * all processes and objects when they are created.
2756 */
2757 security_initcall(smack_init);
This page took 0.111634 seconds and 5 git commands to generate.