CRED: Inaugurate COW credentials
[linux-block.git] / security / smack / smack_lsm.c
CommitLineData
e114e473
CS
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>
d20bdda6 29#include <linux/audit.h>
e114e473
CS
30
31#include "smack.h"
32
c69e8d9c
DH
33#define task_security(task) (task_cred_xxx((task), security))
34
e114e473
CS
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 */
50static 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 */
71struct 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/**
5cd9c58f 92 * smack_ptrace_may_access - Smack approval on PTRACE_ATTACH
e114e473
CS
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 */
5cd9c58f 99static int smack_ptrace_may_access(struct task_struct *ctp, unsigned int mode)
e114e473
CS
100{
101 int rc;
102
5cd9c58f 103 rc = cap_ptrace_may_access(ctp, mode);
e114e473
CS
104 if (rc != 0)
105 return rc;
106
d84f4f99 107 rc = smk_access(current_security(), task_security(ctp), MAY_READWRITE);
5cd9c58f 108 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
e114e473 109 return 0;
5cd9c58f
DH
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 */
121static 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;
e114e473 128
d84f4f99 129 rc = smk_access(task_security(ptp), current_security(), MAY_READWRITE);
5cd9c58f
DH
130 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
131 return 0;
e114e473
CS
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 */
143static int smack_syslog(int type)
144{
145 int rc;
86a264ab 146 char *sp = current_security();
e114e473
CS
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 */
172static 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 */
198static 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 */
e0007529 215static int smack_sb_copy_data(char *orig, char *smackopts)
e114e473
CS
216{
217 char *cp, *commap, *otheropts, *dp;
218
e114e473
CS
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 */
257static 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 */
323static 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 */
b5266eb4 341static int smack_sb_mount(char *dev_name, struct path *path,
e114e473
CS
342 char *type, unsigned long flags, void *data)
343{
b5266eb4 344 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
e114e473
CS
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 */
357static 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 */
376static int smack_inode_alloc_security(struct inode *inode)
377{
86a264ab 378 inode->i_security = new_inode_smack(current_security());
e114e473
CS
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 */
390static 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 */
406static 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 */
437static 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 */
462static 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 */
488static 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 */
517static 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 */
b77b0646 546static int smack_inode_permission(struct inode *inode, int mask)
e114e473
CS
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 */
564static 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 */
582static 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 */
8f0cfa52
DH
599static int smack_inode_setxattr(struct dentry *dentry, const char *name,
600 const void *value, size_t size, int flags)
e114e473 601{
bcdca225 602 int rc = 0;
e114e473 603
bcdca225
CS
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;
e114e473
CS
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 */
8f0cfa52
DH
629static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
630 const void *value, size_t size, int flags)
e114e473
CS
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 */
8f0cfa52 666static int smack_inode_getxattr(struct dentry *dentry, const char *name)
e114e473
CS
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 */
8f0cfa52 680static int smack_inode_removexattr(struct dentry *dentry, const char *name)
e114e473 681{
bcdca225 682 int rc = 0;
e114e473 683
bcdca225
CS
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;
e114e473
CS
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 */
708static 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);
2e1d146a 735 if (sock == NULL || sock->sk == NULL)
e114e473
CS
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 */
765static 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
d20bdda6
AD
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 */
782static 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
e114e473
CS
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 */
807static 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 */
821static int smack_file_alloc_security(struct file *file)
822{
86a264ab 823 file->f_security = current_security();
e114e473
CS
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 */
834static 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 */
849static 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 */
870static 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 */
883static 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 */
919static int smack_file_set_fowner(struct file *file)
920{
86a264ab 921 file->f_security = current_security();
e114e473
CS
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 */
936static 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);
b6dff3ec 946 rc = smk_access(file->f_security, tsk->cred->security, MAY_WRITE);
5cd9c58f 947 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
e114e473
CS
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 */
958static 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
e114e473 977/**
f1752eec
DH
978 * smack_cred_free - "free" task-level security credentials
979 * @cred: the credentials in question
e114e473
CS
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 */
f1752eec 985static void smack_cred_free(struct cred *cred)
e114e473 986{
f1752eec 987 cred->security = NULL;
e114e473
CS
988}
989
d84f4f99
DH
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 */
998static 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 */
1010static void smack_cred_commit(struct cred *new, const struct cred *old)
1011{
1012}
1013
e114e473
CS
1014/**
1015 * smack_task_setpgid - Smack check on setting pgid
1016 * @p: the task object
1017 * @pgid: unused
1018 *
1019 * Return 0 if write access is permitted
1020 */
1021static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1022{
c69e8d9c 1023 return smk_curacc(task_security(p), MAY_WRITE);
e114e473
CS
1024}
1025
1026/**
1027 * smack_task_getpgid - Smack access check for getpgid
1028 * @p: the object task
1029 *
1030 * Returns 0 if current can read the object task, error code otherwise
1031 */
1032static int smack_task_getpgid(struct task_struct *p)
1033{
c69e8d9c 1034 return smk_curacc(task_security(p), MAY_READ);
e114e473
CS
1035}
1036
1037/**
1038 * smack_task_getsid - Smack access check for getsid
1039 * @p: the object task
1040 *
1041 * Returns 0 if current can read the object task, error code otherwise
1042 */
1043static int smack_task_getsid(struct task_struct *p)
1044{
c69e8d9c 1045 return smk_curacc(task_security(p), MAY_READ);
e114e473
CS
1046}
1047
1048/**
1049 * smack_task_getsecid - get the secid of the task
1050 * @p: the object task
1051 * @secid: where to put the result
1052 *
1053 * Sets the secid to contain a u32 version of the smack label.
1054 */
1055static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1056{
c69e8d9c 1057 *secid = smack_to_secid(task_security(p));
e114e473
CS
1058}
1059
1060/**
1061 * smack_task_setnice - Smack check on setting nice
1062 * @p: the task object
1063 * @nice: unused
1064 *
1065 * Return 0 if write access is permitted
1066 */
1067static int smack_task_setnice(struct task_struct *p, int nice)
1068{
bcdca225
CS
1069 int rc;
1070
1071 rc = cap_task_setnice(p, nice);
1072 if (rc == 0)
c69e8d9c 1073 rc = smk_curacc(task_security(p), MAY_WRITE);
bcdca225 1074 return rc;
e114e473
CS
1075}
1076
1077/**
1078 * smack_task_setioprio - Smack check on setting ioprio
1079 * @p: the task object
1080 * @ioprio: unused
1081 *
1082 * Return 0 if write access is permitted
1083 */
1084static int smack_task_setioprio(struct task_struct *p, int ioprio)
1085{
bcdca225
CS
1086 int rc;
1087
1088 rc = cap_task_setioprio(p, ioprio);
1089 if (rc == 0)
c69e8d9c 1090 rc = smk_curacc(task_security(p), MAY_WRITE);
bcdca225 1091 return rc;
e114e473
CS
1092}
1093
1094/**
1095 * smack_task_getioprio - Smack check on reading ioprio
1096 * @p: the task object
1097 *
1098 * Return 0 if read access is permitted
1099 */
1100static int smack_task_getioprio(struct task_struct *p)
1101{
c69e8d9c 1102 return smk_curacc(task_security(p), MAY_READ);
e114e473
CS
1103}
1104
1105/**
1106 * smack_task_setscheduler - Smack check on setting scheduler
1107 * @p: the task object
1108 * @policy: unused
1109 * @lp: unused
1110 *
1111 * Return 0 if read access is permitted
1112 */
1113static int smack_task_setscheduler(struct task_struct *p, int policy,
1114 struct sched_param *lp)
1115{
bcdca225
CS
1116 int rc;
1117
1118 rc = cap_task_setscheduler(p, policy, lp);
1119 if (rc == 0)
c69e8d9c 1120 rc = smk_curacc(task_security(p), MAY_WRITE);
bcdca225 1121 return rc;
e114e473
CS
1122}
1123
1124/**
1125 * smack_task_getscheduler - Smack check on reading scheduler
1126 * @p: the task object
1127 *
1128 * Return 0 if read access is permitted
1129 */
1130static int smack_task_getscheduler(struct task_struct *p)
1131{
c69e8d9c 1132 return smk_curacc(task_security(p), MAY_READ);
e114e473
CS
1133}
1134
1135/**
1136 * smack_task_movememory - Smack check on moving memory
1137 * @p: the task object
1138 *
1139 * Return 0 if write access is permitted
1140 */
1141static int smack_task_movememory(struct task_struct *p)
1142{
c69e8d9c 1143 return smk_curacc(task_security(p), MAY_WRITE);
e114e473
CS
1144}
1145
1146/**
1147 * smack_task_kill - Smack check on signal delivery
1148 * @p: the task object
1149 * @info: unused
1150 * @sig: unused
1151 * @secid: identifies the smack to use in lieu of current's
1152 *
1153 * Return 0 if write access is permitted
1154 *
1155 * The secid behavior is an artifact of an SELinux hack
1156 * in the USB code. Someday it may go away.
1157 */
1158static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1159 int sig, u32 secid)
1160{
e114e473
CS
1161 /*
1162 * Sending a signal requires that the sender
1163 * can write the receiver.
1164 */
1165 if (secid == 0)
c69e8d9c 1166 return smk_curacc(task_security(p), MAY_WRITE);
e114e473
CS
1167 /*
1168 * If the secid isn't 0 we're dealing with some USB IO
1169 * specific behavior. This is not clean. For one thing
1170 * we can't take privilege into account.
1171 */
c69e8d9c 1172 return smk_access(smack_from_secid(secid), task_security(p), MAY_WRITE);
e114e473
CS
1173}
1174
1175/**
1176 * smack_task_wait - Smack access check for waiting
1177 * @p: task to wait for
1178 *
1179 * Returns 0 if current can wait for p, error code otherwise
1180 */
1181static int smack_task_wait(struct task_struct *p)
1182{
1183 int rc;
1184
c69e8d9c 1185 rc = smk_access(current_security(), task_security(p), MAY_WRITE);
e114e473
CS
1186 if (rc == 0)
1187 return 0;
1188
1189 /*
1190 * Allow the operation to succeed if either task
1191 * has privilege to perform operations that might
1192 * account for the smack labels having gotten to
1193 * be different in the first place.
1194 *
5cd9c58f 1195 * This breaks the strict subject/object access
e114e473
CS
1196 * control ideal, taking the object's privilege
1197 * state into account in the decision as well as
1198 * the smack value.
1199 */
5cd9c58f 1200 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
e114e473
CS
1201 return 0;
1202
1203 return rc;
1204}
1205
1206/**
1207 * smack_task_to_inode - copy task smack into the inode blob
1208 * @p: task to copy from
1209 * inode: inode to copy to
1210 *
1211 * Sets the smack pointer in the inode security blob
1212 */
1213static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1214{
1215 struct inode_smack *isp = inode->i_security;
c69e8d9c 1216 isp->smk_inode = task_security(p);
e114e473
CS
1217}
1218
1219/*
1220 * Socket hooks.
1221 */
1222
1223/**
1224 * smack_sk_alloc_security - Allocate a socket blob
1225 * @sk: the socket
1226 * @family: unused
1227 * @priority: memory allocation priority
1228 *
1229 * Assign Smack pointers to current
1230 *
1231 * Returns 0 on success, -ENOMEM is there's no memory
1232 */
1233static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1234{
86a264ab 1235 char *csp = current_security();
e114e473
CS
1236 struct socket_smack *ssp;
1237
1238 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1239 if (ssp == NULL)
1240 return -ENOMEM;
1241
1242 ssp->smk_in = csp;
1243 ssp->smk_out = csp;
1244 ssp->smk_packet[0] = '\0';
1245
1246 sk->sk_security = ssp;
1247
1248 return 0;
1249}
1250
1251/**
1252 * smack_sk_free_security - Free a socket blob
1253 * @sk: the socket
1254 *
1255 * Clears the blob pointer
1256 */
1257static void smack_sk_free_security(struct sock *sk)
1258{
1259 kfree(sk->sk_security);
1260}
1261
1262/**
1263 * smack_set_catset - convert a capset to netlabel mls categories
1264 * @catset: the Smack categories
1265 * @sap: where to put the netlabel categories
1266 *
1267 * Allocates and fills attr.mls.cat
1268 */
1269static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1270{
1271 unsigned char *cp;
1272 unsigned char m;
1273 int cat;
1274 int rc;
1275 int byte;
1276
c60264c4 1277 if (!catset)
e114e473
CS
1278 return;
1279
1280 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1281 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1282 sap->attr.mls.cat->startbit = 0;
1283
1284 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1285 for (m = 0x80; m != 0; m >>= 1, cat++) {
1286 if ((m & *cp) == 0)
1287 continue;
1288 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1289 cat, GFP_ATOMIC);
1290 }
1291}
1292
1293/**
1294 * smack_to_secattr - fill a secattr from a smack value
1295 * @smack: the smack value
1296 * @nlsp: where the result goes
1297 *
1298 * Casey says that CIPSO is good enough for now.
1299 * It can be used to effect.
1300 * It can also be abused to effect when necessary.
1301 * Appologies to the TSIG group in general and GW in particular.
1302 */
1303static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1304{
1305 struct smack_cipso cipso;
1306 int rc;
1307
1308 switch (smack_net_nltype) {
1309 case NETLBL_NLTYPE_CIPSOV4:
00447872 1310 nlsp->domain = smack;
4bc87e62 1311 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
e114e473
CS
1312
1313 rc = smack_to_cipso(smack, &cipso);
1314 if (rc == 0) {
1315 nlsp->attr.mls.lvl = cipso.smk_level;
1316 smack_set_catset(cipso.smk_catset, nlsp);
1317 } else {
1318 nlsp->attr.mls.lvl = smack_cipso_direct;
1319 smack_set_catset(smack, nlsp);
1320 }
1321 break;
1322 default:
1323 break;
1324 }
1325}
1326
1327/**
1328 * smack_netlabel - Set the secattr on a socket
1329 * @sk: the socket
1330 *
1331 * Convert the outbound smack value (smk_out) to a
1332 * secattr and attach it to the socket.
1333 *
1334 * Returns 0 on success or an error code
1335 */
1336static int smack_netlabel(struct sock *sk)
1337{
2e1d146a 1338 struct socket_smack *ssp;
e114e473 1339 struct netlbl_lsm_secattr secattr;
4bc87e62 1340 int rc;
e114e473 1341
2e1d146a 1342 ssp = sk->sk_security;
e114e473
CS
1343 netlbl_secattr_init(&secattr);
1344 smack_to_secattr(ssp->smk_out, &secattr);
4bc87e62 1345 rc = netlbl_sock_setattr(sk, &secattr);
e114e473 1346 netlbl_secattr_destroy(&secattr);
4bc87e62 1347
e114e473
CS
1348 return rc;
1349}
1350
1351/**
1352 * smack_inode_setsecurity - set smack xattrs
1353 * @inode: the object
1354 * @name: attribute name
1355 * @value: attribute value
1356 * @size: size of the attribute
1357 * @flags: unused
1358 *
1359 * Sets the named attribute in the appropriate blob
1360 *
1361 * Returns 0 on success, or an error code
1362 */
1363static int smack_inode_setsecurity(struct inode *inode, const char *name,
1364 const void *value, size_t size, int flags)
1365{
1366 char *sp;
1367 struct inode_smack *nsp = inode->i_security;
1368 struct socket_smack *ssp;
1369 struct socket *sock;
4bc87e62 1370 int rc = 0;
e114e473
CS
1371
1372 if (value == NULL || size > SMK_LABELLEN)
1373 return -EACCES;
1374
1375 sp = smk_import(value, size);
1376 if (sp == NULL)
1377 return -EINVAL;
1378
1379 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1380 nsp->smk_inode = sp;
1381 return 0;
1382 }
1383 /*
1384 * The rest of the Smack xattrs are only on sockets.
1385 */
1386 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1387 return -EOPNOTSUPP;
1388
1389 sock = SOCKET_I(inode);
2e1d146a 1390 if (sock == NULL || sock->sk == NULL)
e114e473
CS
1391 return -EOPNOTSUPP;
1392
1393 ssp = sock->sk->sk_security;
1394
1395 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1396 ssp->smk_in = sp;
1397 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1398 ssp->smk_out = sp;
4bc87e62
CS
1399 rc = smack_netlabel(sock->sk);
1400 if (rc != 0)
1401 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1402 __func__, -rc);
e114e473
CS
1403 } else
1404 return -EOPNOTSUPP;
1405
1406 return 0;
1407}
1408
1409/**
1410 * smack_socket_post_create - finish socket setup
1411 * @sock: the socket
1412 * @family: protocol family
1413 * @type: unused
1414 * @protocol: unused
1415 * @kern: unused
1416 *
1417 * Sets the netlabel information on the socket
1418 *
1419 * Returns 0 on success, and error code otherwise
1420 */
1421static int smack_socket_post_create(struct socket *sock, int family,
1422 int type, int protocol, int kern)
1423{
2e1d146a 1424 if (family != PF_INET || sock->sk == NULL)
e114e473
CS
1425 return 0;
1426 /*
1427 * Set the outbound netlbl.
1428 */
1429 return smack_netlabel(sock->sk);
1430}
1431
1432/**
1433 * smack_flags_to_may - convert S_ to MAY_ values
1434 * @flags: the S_ value
1435 *
1436 * Returns the equivalent MAY_ value
1437 */
1438static int smack_flags_to_may(int flags)
1439{
1440 int may = 0;
1441
1442 if (flags & S_IRUGO)
1443 may |= MAY_READ;
1444 if (flags & S_IWUGO)
1445 may |= MAY_WRITE;
1446 if (flags & S_IXUGO)
1447 may |= MAY_EXEC;
1448
1449 return may;
1450}
1451
1452/**
1453 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1454 * @msg: the object
1455 *
1456 * Returns 0
1457 */
1458static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1459{
86a264ab 1460 msg->security = current_security();
e114e473
CS
1461 return 0;
1462}
1463
1464/**
1465 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1466 * @msg: the object
1467 *
1468 * Clears the blob pointer
1469 */
1470static void smack_msg_msg_free_security(struct msg_msg *msg)
1471{
1472 msg->security = NULL;
1473}
1474
1475/**
1476 * smack_of_shm - the smack pointer for the shm
1477 * @shp: the object
1478 *
1479 * Returns a pointer to the smack value
1480 */
1481static char *smack_of_shm(struct shmid_kernel *shp)
1482{
1483 return (char *)shp->shm_perm.security;
1484}
1485
1486/**
1487 * smack_shm_alloc_security - Set the security blob for shm
1488 * @shp: the object
1489 *
1490 * Returns 0
1491 */
1492static int smack_shm_alloc_security(struct shmid_kernel *shp)
1493{
1494 struct kern_ipc_perm *isp = &shp->shm_perm;
1495
86a264ab 1496 isp->security = current_security();
e114e473
CS
1497 return 0;
1498}
1499
1500/**
1501 * smack_shm_free_security - Clear the security blob for shm
1502 * @shp: the object
1503 *
1504 * Clears the blob pointer
1505 */
1506static void smack_shm_free_security(struct shmid_kernel *shp)
1507{
1508 struct kern_ipc_perm *isp = &shp->shm_perm;
1509
1510 isp->security = NULL;
1511}
1512
1513/**
1514 * smack_shm_associate - Smack access check for shm
1515 * @shp: the object
1516 * @shmflg: access requested
1517 *
1518 * Returns 0 if current has the requested access, error code otherwise
1519 */
1520static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1521{
1522 char *ssp = smack_of_shm(shp);
1523 int may;
1524
1525 may = smack_flags_to_may(shmflg);
1526 return smk_curacc(ssp, may);
1527}
1528
1529/**
1530 * smack_shm_shmctl - Smack access check for shm
1531 * @shp: the object
1532 * @cmd: what it wants to do
1533 *
1534 * Returns 0 if current has the requested access, error code otherwise
1535 */
1536static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1537{
1d252fb8 1538 char *ssp;
e114e473
CS
1539 int may;
1540
1541 switch (cmd) {
1542 case IPC_STAT:
1543 case SHM_STAT:
1544 may = MAY_READ;
1545 break;
1546 case IPC_SET:
1547 case SHM_LOCK:
1548 case SHM_UNLOCK:
1549 case IPC_RMID:
1550 may = MAY_READWRITE;
1551 break;
1552 case IPC_INFO:
1553 case SHM_INFO:
1554 /*
1555 * System level information.
1556 */
1557 return 0;
1558 default:
1559 return -EINVAL;
1560 }
1561
1d252fb8 1562 ssp = smack_of_shm(shp);
e114e473
CS
1563 return smk_curacc(ssp, may);
1564}
1565
1566/**
1567 * smack_shm_shmat - Smack access for shmat
1568 * @shp: the object
1569 * @shmaddr: unused
1570 * @shmflg: access requested
1571 *
1572 * Returns 0 if current has the requested access, error code otherwise
1573 */
1574static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1575 int shmflg)
1576{
1577 char *ssp = smack_of_shm(shp);
1578 int may;
1579
1580 may = smack_flags_to_may(shmflg);
1581 return smk_curacc(ssp, may);
1582}
1583
1584/**
1585 * smack_of_sem - the smack pointer for the sem
1586 * @sma: the object
1587 *
1588 * Returns a pointer to the smack value
1589 */
1590static char *smack_of_sem(struct sem_array *sma)
1591{
1592 return (char *)sma->sem_perm.security;
1593}
1594
1595/**
1596 * smack_sem_alloc_security - Set the security blob for sem
1597 * @sma: the object
1598 *
1599 * Returns 0
1600 */
1601static int smack_sem_alloc_security(struct sem_array *sma)
1602{
1603 struct kern_ipc_perm *isp = &sma->sem_perm;
1604
86a264ab 1605 isp->security = current_security();
e114e473
CS
1606 return 0;
1607}
1608
1609/**
1610 * smack_sem_free_security - Clear the security blob for sem
1611 * @sma: the object
1612 *
1613 * Clears the blob pointer
1614 */
1615static void smack_sem_free_security(struct sem_array *sma)
1616{
1617 struct kern_ipc_perm *isp = &sma->sem_perm;
1618
1619 isp->security = NULL;
1620}
1621
1622/**
1623 * smack_sem_associate - Smack access check for sem
1624 * @sma: the object
1625 * @semflg: access requested
1626 *
1627 * Returns 0 if current has the requested access, error code otherwise
1628 */
1629static int smack_sem_associate(struct sem_array *sma, int semflg)
1630{
1631 char *ssp = smack_of_sem(sma);
1632 int may;
1633
1634 may = smack_flags_to_may(semflg);
1635 return smk_curacc(ssp, may);
1636}
1637
1638/**
1639 * smack_sem_shmctl - Smack access check for sem
1640 * @sma: the object
1641 * @cmd: what it wants to do
1642 *
1643 * Returns 0 if current has the requested access, error code otherwise
1644 */
1645static int smack_sem_semctl(struct sem_array *sma, int cmd)
1646{
1d252fb8 1647 char *ssp;
e114e473
CS
1648 int may;
1649
1650 switch (cmd) {
1651 case GETPID:
1652 case GETNCNT:
1653 case GETZCNT:
1654 case GETVAL:
1655 case GETALL:
1656 case IPC_STAT:
1657 case SEM_STAT:
1658 may = MAY_READ;
1659 break;
1660 case SETVAL:
1661 case SETALL:
1662 case IPC_RMID:
1663 case IPC_SET:
1664 may = MAY_READWRITE;
1665 break;
1666 case IPC_INFO:
1667 case SEM_INFO:
1668 /*
1669 * System level information
1670 */
1671 return 0;
1672 default:
1673 return -EINVAL;
1674 }
1675
1d252fb8 1676 ssp = smack_of_sem(sma);
e114e473
CS
1677 return smk_curacc(ssp, may);
1678}
1679
1680/**
1681 * smack_sem_semop - Smack checks of semaphore operations
1682 * @sma: the object
1683 * @sops: unused
1684 * @nsops: unused
1685 * @alter: unused
1686 *
1687 * Treated as read and write in all cases.
1688 *
1689 * Returns 0 if access is allowed, error code otherwise
1690 */
1691static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
1692 unsigned nsops, int alter)
1693{
1694 char *ssp = smack_of_sem(sma);
1695
1696 return smk_curacc(ssp, MAY_READWRITE);
1697}
1698
1699/**
1700 * smack_msg_alloc_security - Set the security blob for msg
1701 * @msq: the object
1702 *
1703 * Returns 0
1704 */
1705static int smack_msg_queue_alloc_security(struct msg_queue *msq)
1706{
1707 struct kern_ipc_perm *kisp = &msq->q_perm;
1708
86a264ab 1709 kisp->security = current_security();
e114e473
CS
1710 return 0;
1711}
1712
1713/**
1714 * smack_msg_free_security - Clear the security blob for msg
1715 * @msq: the object
1716 *
1717 * Clears the blob pointer
1718 */
1719static void smack_msg_queue_free_security(struct msg_queue *msq)
1720{
1721 struct kern_ipc_perm *kisp = &msq->q_perm;
1722
1723 kisp->security = NULL;
1724}
1725
1726/**
1727 * smack_of_msq - the smack pointer for the msq
1728 * @msq: the object
1729 *
1730 * Returns a pointer to the smack value
1731 */
1732static char *smack_of_msq(struct msg_queue *msq)
1733{
1734 return (char *)msq->q_perm.security;
1735}
1736
1737/**
1738 * smack_msg_queue_associate - Smack access check for msg_queue
1739 * @msq: the object
1740 * @msqflg: access requested
1741 *
1742 * Returns 0 if current has the requested access, error code otherwise
1743 */
1744static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
1745{
1746 char *msp = smack_of_msq(msq);
1747 int may;
1748
1749 may = smack_flags_to_may(msqflg);
1750 return smk_curacc(msp, may);
1751}
1752
1753/**
1754 * smack_msg_queue_msgctl - Smack access check for msg_queue
1755 * @msq: the object
1756 * @cmd: what it wants to do
1757 *
1758 * Returns 0 if current has the requested access, error code otherwise
1759 */
1760static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1761{
1d252fb8 1762 char *msp;
e114e473
CS
1763 int may;
1764
1765 switch (cmd) {
1766 case IPC_STAT:
1767 case MSG_STAT:
1768 may = MAY_READ;
1769 break;
1770 case IPC_SET:
1771 case IPC_RMID:
1772 may = MAY_READWRITE;
1773 break;
1774 case IPC_INFO:
1775 case MSG_INFO:
1776 /*
1777 * System level information
1778 */
1779 return 0;
1780 default:
1781 return -EINVAL;
1782 }
1783
1d252fb8 1784 msp = smack_of_msq(msq);
e114e473
CS
1785 return smk_curacc(msp, may);
1786}
1787
1788/**
1789 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1790 * @msq: the object
1791 * @msg: unused
1792 * @msqflg: access requested
1793 *
1794 * Returns 0 if current has the requested access, error code otherwise
1795 */
1796static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
1797 int msqflg)
1798{
1799 char *msp = smack_of_msq(msq);
1800 int rc;
1801
1802 rc = smack_flags_to_may(msqflg);
1803 return smk_curacc(msp, rc);
1804}
1805
1806/**
1807 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1808 * @msq: the object
1809 * @msg: unused
1810 * @target: unused
1811 * @type: unused
1812 * @mode: unused
1813 *
1814 * Returns 0 if current has read and write access, error code otherwise
1815 */
1816static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1817 struct task_struct *target, long type, int mode)
1818{
1819 char *msp = smack_of_msq(msq);
1820
1821 return smk_curacc(msp, MAY_READWRITE);
1822}
1823
1824/**
1825 * smack_ipc_permission - Smack access for ipc_permission()
1826 * @ipp: the object permissions
1827 * @flag: access requested
1828 *
1829 * Returns 0 if current has read and write access, error code otherwise
1830 */
1831static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
1832{
1833 char *isp = ipp->security;
1834 int may;
1835
1836 may = smack_flags_to_may(flag);
1837 return smk_curacc(isp, may);
1838}
1839
d20bdda6
AD
1840/**
1841 * smack_ipc_getsecid - Extract smack security id
1842 * @ipcp: the object permissions
1843 * @secid: where result will be saved
1844 */
1845static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
1846{
1847 char *smack = ipp->security;
1848
1849 *secid = smack_to_secid(smack);
1850}
1851
e114e473
CS
1852/**
1853 * smack_d_instantiate - Make sure the blob is correct on an inode
1854 * @opt_dentry: unused
1855 * @inode: the object
1856 *
1857 * Set the inode's security blob if it hasn't been done already.
1858 */
1859static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
1860{
1861 struct super_block *sbp;
1862 struct superblock_smack *sbsp;
1863 struct inode_smack *isp;
86a264ab 1864 char *csp = current_security();
e114e473
CS
1865 char *fetched;
1866 char *final;
1867 struct dentry *dp;
1868
1869 if (inode == NULL)
1870 return;
1871
1872 isp = inode->i_security;
1873
1874 mutex_lock(&isp->smk_lock);
1875 /*
1876 * If the inode is already instantiated
1877 * take the quick way out
1878 */
1879 if (isp->smk_flags & SMK_INODE_INSTANT)
1880 goto unlockandout;
1881
1882 sbp = inode->i_sb;
1883 sbsp = sbp->s_security;
1884 /*
1885 * We're going to use the superblock default label
1886 * if there's no label on the file.
1887 */
1888 final = sbsp->smk_default;
1889
e97dcb0e
CS
1890 /*
1891 * If this is the root inode the superblock
1892 * may be in the process of initialization.
1893 * If that is the case use the root value out
1894 * of the superblock.
1895 */
1896 if (opt_dentry->d_parent == opt_dentry) {
1897 isp->smk_inode = sbsp->smk_root;
1898 isp->smk_flags |= SMK_INODE_INSTANT;
1899 goto unlockandout;
1900 }
1901
e114e473
CS
1902 /*
1903 * This is pretty hackish.
1904 * Casey says that we shouldn't have to do
1905 * file system specific code, but it does help
1906 * with keeping it simple.
1907 */
1908 switch (sbp->s_magic) {
1909 case SMACK_MAGIC:
1910 /*
1911 * Casey says that it's a little embarassing
1912 * that the smack file system doesn't do
1913 * extended attributes.
1914 */
1915 final = smack_known_star.smk_known;
1916 break;
1917 case PIPEFS_MAGIC:
1918 /*
1919 * Casey says pipes are easy (?)
1920 */
1921 final = smack_known_star.smk_known;
1922 break;
1923 case DEVPTS_SUPER_MAGIC:
1924 /*
1925 * devpts seems content with the label of the task.
1926 * Programs that change smack have to treat the
1927 * pty with respect.
1928 */
1929 final = csp;
1930 break;
1931 case SOCKFS_MAGIC:
1932 /*
1933 * Casey says sockets get the smack of the task.
1934 */
1935 final = csp;
1936 break;
1937 case PROC_SUPER_MAGIC:
1938 /*
1939 * Casey says procfs appears not to care.
1940 * The superblock default suffices.
1941 */
1942 break;
1943 case TMPFS_MAGIC:
1944 /*
1945 * Device labels should come from the filesystem,
1946 * but watch out, because they're volitile,
1947 * getting recreated on every reboot.
1948 */
1949 final = smack_known_star.smk_known;
1950 /*
1951 * No break.
1952 *
1953 * If a smack value has been set we want to use it,
1954 * but since tmpfs isn't giving us the opportunity
1955 * to set mount options simulate setting the
1956 * superblock default.
1957 */
1958 default:
1959 /*
1960 * This isn't an understood special case.
1961 * Get the value from the xattr.
1962 *
1963 * No xattr support means, alas, no SMACK label.
1964 * Use the aforeapplied default.
1965 * It would be curious if the label of the task
1966 * does not match that assigned.
1967 */
1968 if (inode->i_op->getxattr == NULL)
1969 break;
1970 /*
1971 * Get the dentry for xattr.
1972 */
1973 if (opt_dentry == NULL) {
1974 dp = d_find_alias(inode);
1975 if (dp == NULL)
1976 break;
1977 } else {
1978 dp = dget(opt_dentry);
1979 if (dp == NULL)
1980 break;
1981 }
1982
1983 fetched = smk_fetch(inode, dp);
1984 if (fetched != NULL)
1985 final = fetched;
1986
1987 dput(dp);
1988 break;
1989 }
1990
1991 if (final == NULL)
1992 isp->smk_inode = csp;
1993 else
1994 isp->smk_inode = final;
1995
1996 isp->smk_flags |= SMK_INODE_INSTANT;
1997
1998unlockandout:
1999 mutex_unlock(&isp->smk_lock);
2000 return;
2001}
2002
2003/**
2004 * smack_getprocattr - Smack process attribute access
2005 * @p: the object task
2006 * @name: the name of the attribute in /proc/.../attr
2007 * @value: where to put the result
2008 *
2009 * Places a copy of the task Smack into value
2010 *
2011 * Returns the length of the smack label or an error code
2012 */
2013static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2014{
2015 char *cp;
2016 int slen;
2017
2018 if (strcmp(name, "current") != 0)
2019 return -EINVAL;
2020
c69e8d9c 2021 cp = kstrdup(task_security(p), GFP_KERNEL);
e114e473
CS
2022 if (cp == NULL)
2023 return -ENOMEM;
2024
2025 slen = strlen(cp);
2026 *value = cp;
2027 return slen;
2028}
2029
2030/**
2031 * smack_setprocattr - Smack process attribute setting
2032 * @p: the object task
2033 * @name: the name of the attribute in /proc/.../attr
2034 * @value: the value to set
2035 * @size: the size of the value
2036 *
2037 * Sets the Smack value of the task. Only setting self
2038 * is permitted and only with privilege
2039 *
2040 * Returns the length of the smack label or an error code
2041 */
2042static int smack_setprocattr(struct task_struct *p, char *name,
2043 void *value, size_t size)
2044{
d84f4f99 2045 struct cred *new;
e114e473
CS
2046 char *newsmack;
2047
e114e473
CS
2048 /*
2049 * Changing another process' Smack value is too dangerous
2050 * and supports no sane use case.
2051 */
2052 if (p != current)
2053 return -EPERM;
2054
5cd9c58f
DH
2055 if (!capable(CAP_MAC_ADMIN))
2056 return -EPERM;
2057
e114e473
CS
2058 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2059 return -EINVAL;
2060
2061 if (strcmp(name, "current") != 0)
2062 return -EINVAL;
2063
2064 newsmack = smk_import(value, size);
2065 if (newsmack == NULL)
2066 return -EINVAL;
2067
d84f4f99
DH
2068 new = prepare_creds();
2069 if (!new)
2070 return -ENOMEM;
2071 new->security = newsmack;
2072 commit_creds(new);
e114e473
CS
2073 return size;
2074}
2075
2076/**
2077 * smack_unix_stream_connect - Smack access on UDS
2078 * @sock: one socket
2079 * @other: the other socket
2080 * @newsk: unused
2081 *
2082 * Return 0 if a subject with the smack of sock could access
2083 * an object with the smack of other, otherwise an error code
2084 */
2085static int smack_unix_stream_connect(struct socket *sock,
2086 struct socket *other, struct sock *newsk)
2087{
2088 struct inode *sp = SOCK_INODE(sock);
2089 struct inode *op = SOCK_INODE(other);
2090
2091 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
2092}
2093
2094/**
2095 * smack_unix_may_send - Smack access on UDS
2096 * @sock: one socket
2097 * @other: the other socket
2098 *
2099 * Return 0 if a subject with the smack of sock could access
2100 * an object with the smack of other, otherwise an error code
2101 */
2102static int smack_unix_may_send(struct socket *sock, struct socket *other)
2103{
2104 struct inode *sp = SOCK_INODE(sock);
2105 struct inode *op = SOCK_INODE(other);
2106
2107 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
2108}
2109
2110/**
2111 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat
2112 * pair to smack
2113 * @sap: netlabel secattr
2114 * @sip: where to put the result
2115 *
2116 * Copies a smack label into sip
2117 */
2118static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2119{
2120 char smack[SMK_LABELLEN];
2121 int pcat;
2122
2123 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) == 0) {
2124 /*
2125 * If there are flags but no level netlabel isn't
2126 * behaving the way we expect it to.
2127 *
2128 * Without guidance regarding the smack value
2129 * for the packet fall back on the network
2130 * ambient value.
2131 */
2132 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2133 return;
2134 }
2135 /*
2136 * Get the categories, if any
2137 */
2138 memset(smack, '\0', SMK_LABELLEN);
2139 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2140 for (pcat = -1;;) {
2141 pcat = netlbl_secattr_catmap_walk(sap->attr.mls.cat,
2142 pcat + 1);
2143 if (pcat < 0)
2144 break;
2145 smack_catset_bit(pcat, smack);
2146 }
2147 /*
2148 * If it is CIPSO using smack direct mapping
2149 * we are already done. WeeHee.
2150 */
2151 if (sap->attr.mls.lvl == smack_cipso_direct) {
2152 memcpy(sip, smack, SMK_MAXLEN);
2153 return;
2154 }
2155 /*
2156 * Look it up in the supplied table if it is not a direct mapping.
2157 */
2158 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2159 return;
2160}
2161
2162/**
2163 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2164 * @sk: socket
2165 * @skb: packet
2166 *
2167 * Returns 0 if the packet should be delivered, an error code otherwise
2168 */
2169static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2170{
2171 struct netlbl_lsm_secattr secattr;
2172 struct socket_smack *ssp = sk->sk_security;
2173 char smack[SMK_LABELLEN];
2174 int rc;
2175
2176 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2177 return 0;
2178
2179 /*
2180 * Translate what netlabel gave us.
2181 */
2182 memset(smack, '\0', SMK_LABELLEN);
2183 netlbl_secattr_init(&secattr);
2184 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2185 if (rc == 0)
2186 smack_from_secattr(&secattr, smack);
2187 else
2188 strncpy(smack, smack_net_ambient, SMK_MAXLEN);
2189 netlbl_secattr_destroy(&secattr);
2190 /*
2191 * Receiving a packet requires that the other end
2192 * be able to write here. Read access is not required.
2193 * This is the simplist possible security model
2194 * for networking.
2195 */
a8134296
PM
2196 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2197 if (rc != 0)
2198 netlbl_skbuff_err(skb, rc, 0);
2199 return rc;
e114e473
CS
2200}
2201
2202/**
2203 * smack_socket_getpeersec_stream - pull in packet label
2204 * @sock: the socket
2205 * @optval: user's destination
2206 * @optlen: size thereof
2207 * @len: max thereoe
2208 *
2209 * returns zero on success, an error code otherwise
2210 */
2211static int smack_socket_getpeersec_stream(struct socket *sock,
2212 char __user *optval,
2213 int __user *optlen, unsigned len)
2214{
2215 struct socket_smack *ssp;
2216 int slen;
2217 int rc = 0;
2218
2219 ssp = sock->sk->sk_security;
2220 slen = strlen(ssp->smk_packet) + 1;
2221
2222 if (slen > len)
2223 rc = -ERANGE;
2224 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2225 rc = -EFAULT;
2226
2227 if (put_user(slen, optlen) != 0)
2228 rc = -EFAULT;
2229
2230 return rc;
2231}
2232
2233
2234/**
2235 * smack_socket_getpeersec_dgram - pull in packet label
2236 * @sock: the socket
2237 * @skb: packet data
2238 * @secid: pointer to where to put the secid of the packet
2239 *
2240 * Sets the netlabel socket state on sk from parent
2241 */
2242static int smack_socket_getpeersec_dgram(struct socket *sock,
2243 struct sk_buff *skb, u32 *secid)
2244
2245{
2246 struct netlbl_lsm_secattr secattr;
2247 struct sock *sk;
2248 char smack[SMK_LABELLEN];
2249 int family = PF_INET;
2250 u32 s;
2251 int rc;
2252
2253 /*
2254 * Only works for families with packets.
2255 */
2256 if (sock != NULL) {
2257 sk = sock->sk;
2258 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2259 return 0;
2260 family = sk->sk_family;
2261 }
2262 /*
2263 * Translate what netlabel gave us.
2264 */
2265 memset(smack, '\0', SMK_LABELLEN);
2266 netlbl_secattr_init(&secattr);
2267 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2268 if (rc == 0)
2269 smack_from_secattr(&secattr, smack);
2270 netlbl_secattr_destroy(&secattr);
2271
2272 /*
2273 * Give up if we couldn't get anything
2274 */
2275 if (rc != 0)
2276 return rc;
2277
2278 s = smack_to_secid(smack);
2279 if (s == 0)
2280 return -EINVAL;
2281
2282 *secid = s;
2283 return 0;
2284}
2285
2286/**
2287 * smack_sock_graft - graft access state between two sockets
2288 * @sk: fresh sock
2289 * @parent: donor socket
2290 *
2291 * Sets the netlabel socket state on sk from parent
2292 */
2293static void smack_sock_graft(struct sock *sk, struct socket *parent)
2294{
2295 struct socket_smack *ssp;
2296 int rc;
2297
2298 if (sk == NULL)
2299 return;
2300
2301 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2302 return;
2303
2304 ssp = sk->sk_security;
86a264ab 2305 ssp->smk_in = ssp->smk_out = current_security();
e114e473
CS
2306 ssp->smk_packet[0] = '\0';
2307
2308 rc = smack_netlabel(sk);
4bc87e62
CS
2309 if (rc != 0)
2310 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
2311 __func__, -rc);
e114e473
CS
2312}
2313
2314/**
2315 * smack_inet_conn_request - Smack access check on connect
2316 * @sk: socket involved
2317 * @skb: packet
2318 * @req: unused
2319 *
2320 * Returns 0 if a task with the packet label could write to
2321 * the socket, otherwise an error code
2322 */
2323static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2324 struct request_sock *req)
2325{
2326 struct netlbl_lsm_secattr skb_secattr;
2327 struct socket_smack *ssp = sk->sk_security;
2328 char smack[SMK_LABELLEN];
2329 int rc;
2330
2331 if (skb == NULL)
2332 return -EACCES;
2333
2334 memset(smack, '\0', SMK_LABELLEN);
2335 netlbl_secattr_init(&skb_secattr);
2336 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr);
2337 if (rc == 0)
2338 smack_from_secattr(&skb_secattr, smack);
2339 else
2340 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2341 netlbl_secattr_destroy(&skb_secattr);
2342 /*
2343 * Receiving a packet requires that the other end
2344 * be able to write here. Read access is not required.
2345 *
2346 * If the request is successful save the peer's label
2347 * so that SO_PEERCRED can report it.
2348 */
2349 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2350 if (rc == 0)
2351 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2352
2353 return rc;
2354}
2355
2356/*
2357 * Key management security hooks
2358 *
2359 * Casey has not tested key support very heavily.
2360 * The permission check is most likely too restrictive.
2361 * If you care about keys please have a look.
2362 */
2363#ifdef CONFIG_KEYS
2364
2365/**
2366 * smack_key_alloc - Set the key security blob
2367 * @key: object
d84f4f99 2368 * @cred: the credentials to use
e114e473
CS
2369 * @flags: unused
2370 *
2371 * No allocation required
2372 *
2373 * Returns 0
2374 */
d84f4f99 2375static int smack_key_alloc(struct key *key, const struct cred *cred,
e114e473
CS
2376 unsigned long flags)
2377{
d84f4f99 2378 key->security = cred->security;
e114e473
CS
2379 return 0;
2380}
2381
2382/**
2383 * smack_key_free - Clear the key security blob
2384 * @key: the object
2385 *
2386 * Clear the blob pointer
2387 */
2388static void smack_key_free(struct key *key)
2389{
2390 key->security = NULL;
2391}
2392
2393/*
2394 * smack_key_permission - Smack access on a key
2395 * @key_ref: gets to the object
d84f4f99 2396 * @cred: the credentials to use
e114e473
CS
2397 * @perm: unused
2398 *
2399 * Return 0 if the task has read and write to the object,
2400 * an error code otherwise
2401 */
2402static int smack_key_permission(key_ref_t key_ref,
d84f4f99 2403 const struct cred *cred, key_perm_t perm)
e114e473
CS
2404{
2405 struct key *keyp;
2406
2407 keyp = key_ref_to_ptr(key_ref);
2408 if (keyp == NULL)
2409 return -EINVAL;
2410 /*
2411 * If the key hasn't been initialized give it access so that
2412 * it may do so.
2413 */
2414 if (keyp->security == NULL)
2415 return 0;
2416 /*
2417 * This should not occur
2418 */
d84f4f99 2419 if (cred->security == NULL)
e114e473
CS
2420 return -EACCES;
2421
d84f4f99 2422 return smk_access(cred->security, keyp->security, MAY_READWRITE);
e114e473
CS
2423}
2424#endif /* CONFIG_KEYS */
2425
d20bdda6
AD
2426/*
2427 * Smack Audit hooks
2428 *
2429 * Audit requires a unique representation of each Smack specific
2430 * rule. This unique representation is used to distinguish the
2431 * object to be audited from remaining kernel objects and also
2432 * works as a glue between the audit hooks.
2433 *
2434 * Since repository entries are added but never deleted, we'll use
2435 * the smack_known label address related to the given audit rule as
2436 * the needed unique representation. This also better fits the smack
2437 * model where nearly everything is a label.
2438 */
2439#ifdef CONFIG_AUDIT
2440
2441/**
2442 * smack_audit_rule_init - Initialize a smack audit rule
2443 * @field: audit rule fields given from user-space (audit.h)
2444 * @op: required testing operator (=, !=, >, <, ...)
2445 * @rulestr: smack label to be audited
2446 * @vrule: pointer to save our own audit rule representation
2447 *
2448 * Prepare to audit cases where (@field @op @rulestr) is true.
2449 * The label to be audited is created if necessay.
2450 */
2451static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2452{
2453 char **rule = (char **)vrule;
2454 *rule = NULL;
2455
2456 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2457 return -EINVAL;
2458
2459 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2460 return -EINVAL;
2461
2462 *rule = smk_import(rulestr, 0);
2463
2464 return 0;
2465}
2466
2467/**
2468 * smack_audit_rule_known - Distinguish Smack audit rules
2469 * @krule: rule of interest, in Audit kernel representation format
2470 *
2471 * This is used to filter Smack rules from remaining Audit ones.
2472 * If it's proved that this rule belongs to us, the
2473 * audit_rule_match hook will be called to do the final judgement.
2474 */
2475static int smack_audit_rule_known(struct audit_krule *krule)
2476{
2477 struct audit_field *f;
2478 int i;
2479
2480 for (i = 0; i < krule->field_count; i++) {
2481 f = &krule->fields[i];
2482
2483 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2484 return 1;
2485 }
2486
2487 return 0;
2488}
2489
2490/**
2491 * smack_audit_rule_match - Audit given object ?
2492 * @secid: security id for identifying the object to test
2493 * @field: audit rule flags given from user-space
2494 * @op: required testing operator
2495 * @vrule: smack internal rule presentation
2496 * @actx: audit context associated with the check
2497 *
2498 * The core Audit hook. It's used to take the decision of
2499 * whether to audit or not to audit a given object.
2500 */
2501static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2502 struct audit_context *actx)
2503{
2504 char *smack;
2505 char *rule = vrule;
2506
2507 if (!rule) {
2508 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2509 "Smack: missing rule\n");
2510 return -ENOENT;
2511 }
2512
2513 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2514 return 0;
2515
2516 smack = smack_from_secid(secid);
2517
2518 /*
2519 * No need to do string comparisons. If a match occurs,
2520 * both pointers will point to the same smack_known
2521 * label.
2522 */
2523 if (op == AUDIT_EQUAL)
2524 return (rule == smack);
2525 if (op == AUDIT_NOT_EQUAL)
2526 return (rule != smack);
2527
2528 return 0;
2529}
2530
2531/**
2532 * smack_audit_rule_free - free smack rule representation
2533 * @vrule: rule to be freed.
2534 *
2535 * No memory was allocated.
2536 */
2537static void smack_audit_rule_free(void *vrule)
2538{
2539 /* No-op */
2540}
2541
2542#endif /* CONFIG_AUDIT */
2543
e114e473
CS
2544/*
2545 * smack_secid_to_secctx - return the smack label for a secid
2546 * @secid: incoming integer
2547 * @secdata: destination
2548 * @seclen: how long it is
2549 *
2550 * Exists for networking code.
2551 */
2552static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2553{
2554 char *sp = smack_from_secid(secid);
2555
2556 *secdata = sp;
2557 *seclen = strlen(sp);
2558 return 0;
2559}
2560
4bc87e62
CS
2561/*
2562 * smack_secctx_to_secid - return the secid for a smack label
2563 * @secdata: smack label
2564 * @seclen: how long result is
2565 * @secid: outgoing integer
2566 *
2567 * Exists for audit and networking code.
2568 */
e52c1764 2569static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4bc87e62
CS
2570{
2571 *secid = smack_to_secid(secdata);
2572 return 0;
2573}
2574
e114e473
CS
2575/*
2576 * smack_release_secctx - don't do anything.
2577 * @key_ref: unused
2578 * @context: unused
2579 * @perm: unused
2580 *
2581 * Exists to make sure nothing gets done, and properly
2582 */
2583static void smack_release_secctx(char *secdata, u32 seclen)
2584{
2585}
2586
076c54c5
AD
2587struct security_operations smack_ops = {
2588 .name = "smack",
2589
5cd9c58f
DH
2590 .ptrace_may_access = smack_ptrace_may_access,
2591 .ptrace_traceme = smack_ptrace_traceme,
e114e473 2592 .capget = cap_capget,
d84f4f99 2593 .capset = cap_capset,
e114e473
CS
2594 .capable = cap_capable,
2595 .syslog = smack_syslog,
2596 .settime = cap_settime,
2597 .vm_enough_memory = cap_vm_enough_memory,
2598
2599 .bprm_apply_creds = cap_bprm_apply_creds,
2600 .bprm_set_security = cap_bprm_set_security,
2601 .bprm_secureexec = cap_bprm_secureexec,
2602
2603 .sb_alloc_security = smack_sb_alloc_security,
2604 .sb_free_security = smack_sb_free_security,
2605 .sb_copy_data = smack_sb_copy_data,
2606 .sb_kern_mount = smack_sb_kern_mount,
2607 .sb_statfs = smack_sb_statfs,
2608 .sb_mount = smack_sb_mount,
2609 .sb_umount = smack_sb_umount,
2610
2611 .inode_alloc_security = smack_inode_alloc_security,
2612 .inode_free_security = smack_inode_free_security,
2613 .inode_init_security = smack_inode_init_security,
2614 .inode_link = smack_inode_link,
2615 .inode_unlink = smack_inode_unlink,
2616 .inode_rmdir = smack_inode_rmdir,
2617 .inode_rename = smack_inode_rename,
2618 .inode_permission = smack_inode_permission,
2619 .inode_setattr = smack_inode_setattr,
2620 .inode_getattr = smack_inode_getattr,
2621 .inode_setxattr = smack_inode_setxattr,
2622 .inode_post_setxattr = smack_inode_post_setxattr,
2623 .inode_getxattr = smack_inode_getxattr,
2624 .inode_removexattr = smack_inode_removexattr,
bcdca225
CS
2625 .inode_need_killpriv = cap_inode_need_killpriv,
2626 .inode_killpriv = cap_inode_killpriv,
e114e473
CS
2627 .inode_getsecurity = smack_inode_getsecurity,
2628 .inode_setsecurity = smack_inode_setsecurity,
2629 .inode_listsecurity = smack_inode_listsecurity,
d20bdda6 2630 .inode_getsecid = smack_inode_getsecid,
e114e473
CS
2631
2632 .file_permission = smack_file_permission,
2633 .file_alloc_security = smack_file_alloc_security,
2634 .file_free_security = smack_file_free_security,
2635 .file_ioctl = smack_file_ioctl,
2636 .file_lock = smack_file_lock,
2637 .file_fcntl = smack_file_fcntl,
2638 .file_set_fowner = smack_file_set_fowner,
2639 .file_send_sigiotask = smack_file_send_sigiotask,
2640 .file_receive = smack_file_receive,
2641
f1752eec 2642 .cred_free = smack_cred_free,
d84f4f99
DH
2643 .cred_prepare = smack_cred_prepare,
2644 .cred_commit = smack_cred_commit,
2645 .task_fix_setuid = cap_task_fix_setuid,
e114e473
CS
2646 .task_setpgid = smack_task_setpgid,
2647 .task_getpgid = smack_task_getpgid,
2648 .task_getsid = smack_task_getsid,
2649 .task_getsecid = smack_task_getsecid,
2650 .task_setnice = smack_task_setnice,
2651 .task_setioprio = smack_task_setioprio,
2652 .task_getioprio = smack_task_getioprio,
2653 .task_setscheduler = smack_task_setscheduler,
2654 .task_getscheduler = smack_task_getscheduler,
2655 .task_movememory = smack_task_movememory,
2656 .task_kill = smack_task_kill,
2657 .task_wait = smack_task_wait,
e114e473 2658 .task_to_inode = smack_task_to_inode,
1236cc3c 2659 .task_prctl = cap_task_prctl,
e114e473
CS
2660
2661 .ipc_permission = smack_ipc_permission,
d20bdda6 2662 .ipc_getsecid = smack_ipc_getsecid,
e114e473
CS
2663
2664 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
2665 .msg_msg_free_security = smack_msg_msg_free_security,
2666
2667 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
2668 .msg_queue_free_security = smack_msg_queue_free_security,
2669 .msg_queue_associate = smack_msg_queue_associate,
2670 .msg_queue_msgctl = smack_msg_queue_msgctl,
2671 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
2672 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
2673
2674 .shm_alloc_security = smack_shm_alloc_security,
2675 .shm_free_security = smack_shm_free_security,
2676 .shm_associate = smack_shm_associate,
2677 .shm_shmctl = smack_shm_shmctl,
2678 .shm_shmat = smack_shm_shmat,
2679
2680 .sem_alloc_security = smack_sem_alloc_security,
2681 .sem_free_security = smack_sem_free_security,
2682 .sem_associate = smack_sem_associate,
2683 .sem_semctl = smack_sem_semctl,
2684 .sem_semop = smack_sem_semop,
2685
2686 .netlink_send = cap_netlink_send,
2687 .netlink_recv = cap_netlink_recv,
2688
2689 .d_instantiate = smack_d_instantiate,
2690
2691 .getprocattr = smack_getprocattr,
2692 .setprocattr = smack_setprocattr,
2693
2694 .unix_stream_connect = smack_unix_stream_connect,
2695 .unix_may_send = smack_unix_may_send,
2696
2697 .socket_post_create = smack_socket_post_create,
2698 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
2699 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
2700 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
2701 .sk_alloc_security = smack_sk_alloc_security,
2702 .sk_free_security = smack_sk_free_security,
2703 .sock_graft = smack_sock_graft,
2704 .inet_conn_request = smack_inet_conn_request,
d20bdda6 2705
e114e473
CS
2706 /* key management security hooks */
2707#ifdef CONFIG_KEYS
2708 .key_alloc = smack_key_alloc,
2709 .key_free = smack_key_free,
2710 .key_permission = smack_key_permission,
2711#endif /* CONFIG_KEYS */
d20bdda6
AD
2712
2713 /* Audit hooks */
2714#ifdef CONFIG_AUDIT
2715 .audit_rule_init = smack_audit_rule_init,
2716 .audit_rule_known = smack_audit_rule_known,
2717 .audit_rule_match = smack_audit_rule_match,
2718 .audit_rule_free = smack_audit_rule_free,
2719#endif /* CONFIG_AUDIT */
2720
e114e473 2721 .secid_to_secctx = smack_secid_to_secctx,
4bc87e62 2722 .secctx_to_secid = smack_secctx_to_secid,
e114e473
CS
2723 .release_secctx = smack_release_secctx,
2724};
2725
2726/**
2727 * smack_init - initialize the smack system
2728 *
2729 * Returns 0
2730 */
2731static __init int smack_init(void)
2732{
d84f4f99
DH
2733 struct cred *cred;
2734
076c54c5
AD
2735 if (!security_module_enable(&smack_ops))
2736 return 0;
2737
e114e473
CS
2738 printk(KERN_INFO "Smack: Initializing.\n");
2739
2740 /*
2741 * Set the security state for the initial task.
2742 */
d84f4f99
DH
2743 cred = (struct cred *) current->cred;
2744 cred->security = &smack_known_floor.smk_known;
e114e473
CS
2745
2746 /*
2747 * Initialize locks
2748 */
2749 spin_lock_init(&smack_known_unset.smk_cipsolock);
2750 spin_lock_init(&smack_known_huh.smk_cipsolock);
2751 spin_lock_init(&smack_known_hat.smk_cipsolock);
2752 spin_lock_init(&smack_known_star.smk_cipsolock);
2753 spin_lock_init(&smack_known_floor.smk_cipsolock);
2754 spin_lock_init(&smack_known_invalid.smk_cipsolock);
2755
2756 /*
2757 * Register with LSM
2758 */
2759 if (register_security(&smack_ops))
2760 panic("smack: Unable to register with kernel.\n");
2761
2762 return 0;
2763}
2764
2765/*
2766 * Smack requires early initialization in order to label
2767 * all processes and objects when they are created.
2768 */
2769security_initcall(smack_init);