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