blob: 4f48da5b08cbf66edf6870578977f650e6ca5b01 [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
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>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100029#include <linux/audit.h>
Casey Schauflere114e472008-02-04 22:29:50 -080030
31#include "smack.h"
32
David Howellsc69e8d92008-11-14 10:39:19 +110033#define task_security(task) (task_cred_xxx((task), security))
34
Casey Schauflere114e472008-02-04 22:29:50 -080035/*
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/**
David Howells5cd9c582008-08-14 11:37:28 +010092 * smack_ptrace_may_access - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -080093 * @ctp: child task pointer
Randy Dunlap251a2a92009-02-18 11:42:33 -080094 * @mode: ptrace attachment mode
Casey Schauflere114e472008-02-04 22:29:50 -080095 *
96 * Returns 0 if access is OK, an error code otherwise
97 *
98 * Do the capability checks, and require read and write.
99 */
David Howells5cd9c582008-08-14 11:37:28 +0100100static int smack_ptrace_may_access(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800101{
102 int rc;
103
David Howells5cd9c582008-08-14 11:37:28 +0100104 rc = cap_ptrace_may_access(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800105 if (rc != 0)
106 return rc;
107
David Howellsd84f4f92008-11-14 10:39:23 +1100108 rc = smk_access(current_security(), task_security(ctp), MAY_READWRITE);
David Howells5cd9c582008-08-14 11:37:28 +0100109 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800110 return 0;
David Howells5cd9c582008-08-14 11:37:28 +0100111 return rc;
112}
Casey Schauflere114e472008-02-04 22:29:50 -0800113
David Howells5cd9c582008-08-14 11:37:28 +0100114/**
115 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
116 * @ptp: parent task pointer
117 *
118 * Returns 0 if access is OK, an error code otherwise
119 *
120 * Do the capability checks, and require read and write.
121 */
122static int smack_ptrace_traceme(struct task_struct *ptp)
123{
124 int rc;
125
126 rc = cap_ptrace_traceme(ptp);
127 if (rc != 0)
128 return rc;
129
David Howellsd84f4f92008-11-14 10:39:23 +1100130 rc = smk_access(task_security(ptp), current_security(), MAY_READWRITE);
David Howells5cd9c582008-08-14 11:37:28 +0100131 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
132 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800133 return rc;
134}
135
136/**
137 * smack_syslog - Smack approval on syslog
138 * @type: message type
139 *
140 * Require that the task has the floor label
141 *
142 * Returns 0 on success, error code otherwise.
143 */
144static int smack_syslog(int type)
145{
146 int rc;
David Howells86a264a2008-11-14 10:39:18 +1100147 char *sp = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -0800148
149 rc = cap_syslog(type);
150 if (rc != 0)
151 return rc;
152
153 if (capable(CAP_MAC_OVERRIDE))
154 return 0;
155
156 if (sp != smack_known_floor.smk_known)
157 rc = -EACCES;
158
159 return rc;
160}
161
162
163/*
164 * Superblock Hooks.
165 */
166
167/**
168 * smack_sb_alloc_security - allocate a superblock blob
169 * @sb: the superblock getting the blob
170 *
171 * Returns 0 on success or -ENOMEM on error.
172 */
173static int smack_sb_alloc_security(struct super_block *sb)
174{
175 struct superblock_smack *sbsp;
176
177 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
178
179 if (sbsp == NULL)
180 return -ENOMEM;
181
182 sbsp->smk_root = smack_known_floor.smk_known;
183 sbsp->smk_default = smack_known_floor.smk_known;
184 sbsp->smk_floor = smack_known_floor.smk_known;
185 sbsp->smk_hat = smack_known_hat.smk_known;
186 sbsp->smk_initialized = 0;
187 spin_lock_init(&sbsp->smk_sblock);
188
189 sb->s_security = sbsp;
190
191 return 0;
192}
193
194/**
195 * smack_sb_free_security - free a superblock blob
196 * @sb: the superblock getting the blob
197 *
198 */
199static void smack_sb_free_security(struct super_block *sb)
200{
201 kfree(sb->s_security);
202 sb->s_security = NULL;
203}
204
205/**
206 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800207 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800208 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800209 *
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 */
Eric Parise0007522008-03-05 10:31:54 -0500215static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800216{
217 char *cp, *commap, *otheropts, *dp;
218
Casey Schauflere114e472008-02-04 22:29:50 -0800219 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
James Morris12204e22008-12-19 10:44:42 +1100253 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800254 * @data: the smack mount options
255 *
256 * Returns 0 on success, an error code on failure
257 */
James Morris12204e22008-12-19 10:44:42 +1100258static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800259{
260 struct dentry *root = sb->s_root;
261 struct inode *inode = root->d_inode;
262 struct superblock_smack *sp = sb->s_security;
263 struct inode_smack *isp;
264 char *op;
265 char *commap;
266 char *nsp;
267
268 spin_lock(&sp->smk_sblock);
269 if (sp->smk_initialized != 0) {
270 spin_unlock(&sp->smk_sblock);
271 return 0;
272 }
273 sp->smk_initialized = 1;
274 spin_unlock(&sp->smk_sblock);
275
276 for (op = data; op != NULL; op = commap) {
277 commap = strchr(op, ',');
278 if (commap != NULL)
279 *commap++ = '\0';
280
281 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
282 op += strlen(SMK_FSHAT);
283 nsp = smk_import(op, 0);
284 if (nsp != NULL)
285 sp->smk_hat = nsp;
286 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
287 op += strlen(SMK_FSFLOOR);
288 nsp = smk_import(op, 0);
289 if (nsp != NULL)
290 sp->smk_floor = nsp;
291 } else if (strncmp(op, SMK_FSDEFAULT,
292 strlen(SMK_FSDEFAULT)) == 0) {
293 op += strlen(SMK_FSDEFAULT);
294 nsp = smk_import(op, 0);
295 if (nsp != NULL)
296 sp->smk_default = nsp;
297 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
298 op += strlen(SMK_FSROOT);
299 nsp = smk_import(op, 0);
300 if (nsp != NULL)
301 sp->smk_root = nsp;
302 }
303 }
304
305 /*
306 * Initialize the root inode.
307 */
308 isp = inode->i_security;
309 if (isp == NULL)
310 inode->i_security = new_inode_smack(sp->smk_root);
311 else
312 isp->smk_inode = sp->smk_root;
313
314 return 0;
315}
316
317/**
318 * smack_sb_statfs - Smack check on statfs
319 * @dentry: identifies the file system in question
320 *
321 * Returns 0 if current can read the floor of the filesystem,
322 * and error code otherwise
323 */
324static int smack_sb_statfs(struct dentry *dentry)
325{
326 struct superblock_smack *sbp = dentry->d_sb->s_security;
327
328 return smk_curacc(sbp->smk_floor, MAY_READ);
329}
330
331/**
332 * smack_sb_mount - Smack check for mounting
333 * @dev_name: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -0800334 * @path: mount point
Casey Schauflere114e472008-02-04 22:29:50 -0800335 * @type: unused
336 * @flags: unused
337 * @data: unused
338 *
339 * Returns 0 if current can write the floor of the filesystem
340 * being mounted on, an error code otherwise.
341 */
Al Virob5266eb2008-03-22 17:48:24 -0400342static int smack_sb_mount(char *dev_name, struct path *path,
Casey Schauflere114e472008-02-04 22:29:50 -0800343 char *type, unsigned long flags, void *data)
344{
Al Virob5266eb2008-03-22 17:48:24 -0400345 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
Casey Schauflere114e472008-02-04 22:29:50 -0800346
347 return smk_curacc(sbp->smk_floor, MAY_WRITE);
348}
349
350/**
351 * smack_sb_umount - Smack check for unmounting
352 * @mnt: file system to unmount
353 * @flags: unused
354 *
355 * Returns 0 if current can write the floor of the filesystem
356 * being unmounted, an error code otherwise.
357 */
358static int smack_sb_umount(struct vfsmount *mnt, int flags)
359{
360 struct superblock_smack *sbp;
361
362 sbp = mnt->mnt_sb->s_security;
363
364 return smk_curacc(sbp->smk_floor, MAY_WRITE);
365}
366
367/*
368 * Inode hooks
369 */
370
371/**
372 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800373 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800374 *
375 * Returns 0 if it gets a blob, -ENOMEM otherwise
376 */
377static int smack_inode_alloc_security(struct inode *inode)
378{
David Howells86a264a2008-11-14 10:39:18 +1100379 inode->i_security = new_inode_smack(current_security());
Casey Schauflere114e472008-02-04 22:29:50 -0800380 if (inode->i_security == NULL)
381 return -ENOMEM;
382 return 0;
383}
384
385/**
386 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800387 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800388 *
389 * Clears the blob pointer in inode
390 */
391static void smack_inode_free_security(struct inode *inode)
392{
393 kfree(inode->i_security);
394 inode->i_security = NULL;
395}
396
397/**
398 * smack_inode_init_security - copy out the smack from an inode
399 * @inode: the inode
400 * @dir: unused
401 * @name: where to put the attribute name
402 * @value: where to put the attribute value
403 * @len: where to put the length of the attribute
404 *
405 * Returns 0 if it all works out, -ENOMEM if there's no memory
406 */
407static int smack_inode_init_security(struct inode *inode, struct inode *dir,
408 char **name, void **value, size_t *len)
409{
410 char *isp = smk_of_inode(inode);
411
412 if (name) {
413 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
414 if (*name == NULL)
415 return -ENOMEM;
416 }
417
418 if (value) {
419 *value = kstrdup(isp, GFP_KERNEL);
420 if (*value == NULL)
421 return -ENOMEM;
422 }
423
424 if (len)
425 *len = strlen(isp) + 1;
426
427 return 0;
428}
429
430/**
431 * smack_inode_link - Smack check on link
432 * @old_dentry: the existing object
433 * @dir: unused
434 * @new_dentry: the new object
435 *
436 * Returns 0 if access is permitted, an error code otherwise
437 */
438static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
439 struct dentry *new_dentry)
440{
441 int rc;
442 char *isp;
443
444 isp = smk_of_inode(old_dentry->d_inode);
445 rc = smk_curacc(isp, MAY_WRITE);
446
447 if (rc == 0 && new_dentry->d_inode != NULL) {
448 isp = smk_of_inode(new_dentry->d_inode);
449 rc = smk_curacc(isp, MAY_WRITE);
450 }
451
452 return rc;
453}
454
455/**
456 * smack_inode_unlink - Smack check on inode deletion
457 * @dir: containing directory object
458 * @dentry: file to unlink
459 *
460 * Returns 0 if current can write the containing directory
461 * and the object, error code otherwise
462 */
463static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
464{
465 struct inode *ip = dentry->d_inode;
466 int rc;
467
468 /*
469 * You need write access to the thing you're unlinking
470 */
471 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE);
472 if (rc == 0)
473 /*
474 * You also need write access to the containing directory
475 */
476 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
477
478 return rc;
479}
480
481/**
482 * smack_inode_rmdir - Smack check on directory deletion
483 * @dir: containing directory object
484 * @dentry: directory to unlink
485 *
486 * Returns 0 if current can write the containing directory
487 * and the directory, error code otherwise
488 */
489static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
490{
491 int rc;
492
493 /*
494 * You need write access to the thing you're removing
495 */
496 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
497 if (rc == 0)
498 /*
499 * You also need write access to the containing directory
500 */
501 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
502
503 return rc;
504}
505
506/**
507 * smack_inode_rename - Smack check on rename
508 * @old_inode: the old directory
509 * @old_dentry: unused
510 * @new_inode: the new directory
511 * @new_dentry: unused
512 *
513 * Read and write access is required on both the old and
514 * new directories.
515 *
516 * Returns 0 if access is permitted, an error code otherwise
517 */
518static int smack_inode_rename(struct inode *old_inode,
519 struct dentry *old_dentry,
520 struct inode *new_inode,
521 struct dentry *new_dentry)
522{
523 int rc;
524 char *isp;
525
526 isp = smk_of_inode(old_dentry->d_inode);
527 rc = smk_curacc(isp, MAY_READWRITE);
528
529 if (rc == 0 && new_dentry->d_inode != NULL) {
530 isp = smk_of_inode(new_dentry->d_inode);
531 rc = smk_curacc(isp, MAY_READWRITE);
532 }
533
534 return rc;
535}
536
537/**
538 * smack_inode_permission - Smack version of permission()
539 * @inode: the inode in question
540 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800541 *
542 * This is the important Smack hook.
543 *
544 * Returns 0 if access is permitted, -EACCES otherwise
545 */
Al Virob77b0642008-07-17 09:37:02 -0400546static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800547{
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 */
David Howells8f0cfa52008-04-29 00:59:41 -0700599static int smack_inode_setxattr(struct dentry *dentry, const char *name,
600 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800601{
Casey Schauflerbcdca222008-02-23 15:24:04 -0800602 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800603
Casey Schauflerbcdca222008-02-23 15:24:04 -0800604 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;
Casey Schauflere114e472008-02-04 22:29:50 -0800616}
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 */
David Howells8f0cfa52008-04-29 00:59:41 -0700629static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
630 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800631{
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 */
David Howells8f0cfa52008-04-29 00:59:41 -0700666static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800667{
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 */
David Howells8f0cfa52008-04-29 00:59:41 -0700680static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800681{
Casey Schauflerbcdca222008-02-23 15:24:04 -0800682 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800683
Casey Schauflerbcdca222008-02-23 15:24:04 -0800684 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;
Casey Schauflere114e472008-02-04 22:29:50 -0800696}
697
698/**
699 * smack_inode_getsecurity - get smack xattrs
700 * @inode: the object
701 * @name: attribute name
702 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800703 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800704 *
705 * Returns the size of the attribute or an error code
706 */
707static int smack_inode_getsecurity(const struct inode *inode,
708 const char *name, void **buffer,
709 bool alloc)
710{
711 struct socket_smack *ssp;
712 struct socket *sock;
713 struct super_block *sbp;
714 struct inode *ip = (struct inode *)inode;
715 char *isp;
716 int ilen;
717 int rc = 0;
718
719 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
720 isp = smk_of_inode(inode);
721 ilen = strlen(isp) + 1;
722 *buffer = isp;
723 return ilen;
724 }
725
726 /*
727 * The rest of the Smack xattrs are only on sockets.
728 */
729 sbp = ip->i_sb;
730 if (sbp->s_magic != SOCKFS_MAGIC)
731 return -EOPNOTSUPP;
732
733 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800734 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800735 return -EOPNOTSUPP;
736
737 ssp = sock->sk->sk_security;
738
739 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
740 isp = ssp->smk_in;
741 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
742 isp = ssp->smk_out;
743 else
744 return -EOPNOTSUPP;
745
746 ilen = strlen(isp) + 1;
747 if (rc == 0) {
748 *buffer = isp;
749 rc = ilen;
750 }
751
752 return rc;
753}
754
755
756/**
757 * smack_inode_listsecurity - list the Smack attributes
758 * @inode: the object
759 * @buffer: where they go
760 * @buffer_size: size of buffer
761 *
762 * Returns 0 on success, -EINVAL otherwise
763 */
764static int smack_inode_listsecurity(struct inode *inode, char *buffer,
765 size_t buffer_size)
766{
767 int len = strlen(XATTR_NAME_SMACK);
768
769 if (buffer != NULL && len <= buffer_size) {
770 memcpy(buffer, XATTR_NAME_SMACK, len);
771 return len;
772 }
773 return -EINVAL;
774}
775
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +1000776/**
777 * smack_inode_getsecid - Extract inode's security id
778 * @inode: inode to extract the info from
779 * @secid: where result will be saved
780 */
781static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
782{
783 struct inode_smack *isp = inode->i_security;
784
785 *secid = smack_to_secid(isp->smk_inode);
786}
787
Casey Schauflere114e472008-02-04 22:29:50 -0800788/*
789 * File Hooks
790 */
791
792/**
793 * smack_file_permission - Smack check on file operations
794 * @file: unused
795 * @mask: unused
796 *
797 * Returns 0
798 *
799 * Should access checks be done on each read or write?
800 * UNICOS and SELinux say yes.
801 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
802 *
803 * I'll say no for now. Smack does not do the frequent
804 * label changing that SELinux does.
805 */
806static int smack_file_permission(struct file *file, int mask)
807{
808 return 0;
809}
810
811/**
812 * smack_file_alloc_security - assign a file security blob
813 * @file: the object
814 *
815 * The security blob for a file is a pointer to the master
816 * label list, so no allocation is done.
817 *
818 * Returns 0
819 */
820static int smack_file_alloc_security(struct file *file)
821{
David Howells86a264a2008-11-14 10:39:18 +1100822 file->f_security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -0800823 return 0;
824}
825
826/**
827 * smack_file_free_security - clear a file security blob
828 * @file: the object
829 *
830 * The security blob for a file is a pointer to the master
831 * label list, so no memory is freed.
832 */
833static void smack_file_free_security(struct file *file)
834{
835 file->f_security = NULL;
836}
837
838/**
839 * smack_file_ioctl - Smack check on ioctls
840 * @file: the object
841 * @cmd: what to do
842 * @arg: unused
843 *
844 * Relies heavily on the correct use of the ioctl command conventions.
845 *
846 * Returns 0 if allowed, error code otherwise
847 */
848static int smack_file_ioctl(struct file *file, unsigned int cmd,
849 unsigned long arg)
850{
851 int rc = 0;
852
853 if (_IOC_DIR(cmd) & _IOC_WRITE)
854 rc = smk_curacc(file->f_security, MAY_WRITE);
855
856 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
857 rc = smk_curacc(file->f_security, MAY_READ);
858
859 return rc;
860}
861
862/**
863 * smack_file_lock - Smack check on file locking
864 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -0800865 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800866 *
867 * Returns 0 if current has write access, error code otherwise
868 */
869static int smack_file_lock(struct file *file, unsigned int cmd)
870{
871 return smk_curacc(file->f_security, MAY_WRITE);
872}
873
874/**
875 * smack_file_fcntl - Smack check on fcntl
876 * @file: the object
877 * @cmd: what action to check
878 * @arg: unused
879 *
880 * Returns 0 if current has access, error code otherwise
881 */
882static int smack_file_fcntl(struct file *file, unsigned int cmd,
883 unsigned long arg)
884{
885 int rc;
886
887 switch (cmd) {
888 case F_DUPFD:
889 case F_GETFD:
890 case F_GETFL:
891 case F_GETLK:
892 case F_GETOWN:
893 case F_GETSIG:
894 rc = smk_curacc(file->f_security, MAY_READ);
895 break;
896 case F_SETFD:
897 case F_SETFL:
898 case F_SETLK:
899 case F_SETLKW:
900 case F_SETOWN:
901 case F_SETSIG:
902 rc = smk_curacc(file->f_security, MAY_WRITE);
903 break;
904 default:
905 rc = smk_curacc(file->f_security, MAY_READWRITE);
906 }
907
908 return rc;
909}
910
911/**
912 * smack_file_set_fowner - set the file security blob value
913 * @file: object in question
914 *
915 * Returns 0
916 * Further research may be required on this one.
917 */
918static int smack_file_set_fowner(struct file *file)
919{
David Howells86a264a2008-11-14 10:39:18 +1100920 file->f_security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -0800921 return 0;
922}
923
924/**
925 * smack_file_send_sigiotask - Smack on sigio
926 * @tsk: The target task
927 * @fown: the object the signal come from
928 * @signum: unused
929 *
930 * Allow a privileged task to get signals even if it shouldn't
931 *
932 * Returns 0 if a subject with the object's smack could
933 * write to the task, an error code otherwise.
934 */
935static int smack_file_send_sigiotask(struct task_struct *tsk,
936 struct fown_struct *fown, int signum)
937{
938 struct file *file;
939 int rc;
940
941 /*
942 * struct fown_struct is never outside the context of a struct file
943 */
944 file = container_of(fown, struct file, f_owner);
David Howellsb6dff3e2008-11-14 10:39:16 +1100945 rc = smk_access(file->f_security, tsk->cred->security, MAY_WRITE);
David Howells5cd9c582008-08-14 11:37:28 +0100946 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800947 return 0;
948 return rc;
949}
950
951/**
952 * smack_file_receive - Smack file receive check
953 * @file: the object
954 *
955 * Returns 0 if current has access, error code otherwise
956 */
957static int smack_file_receive(struct file *file)
958{
959 int may = 0;
960
961 /*
962 * This code relies on bitmasks.
963 */
964 if (file->f_mode & FMODE_READ)
965 may = MAY_READ;
966 if (file->f_mode & FMODE_WRITE)
967 may |= MAY_WRITE;
968
969 return smk_curacc(file->f_security, may);
970}
971
972/*
973 * Task hooks
974 */
975
976/**
David Howellsf1752ee2008-11-14 10:39:17 +1100977 * smack_cred_free - "free" task-level security credentials
978 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -0800979 *
980 * Smack isn't using copies of blobs. Everyone
981 * points to an immutable list. The blobs never go away.
982 * There is no leak here.
983 */
David Howellsf1752ee2008-11-14 10:39:17 +1100984static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -0800985{
David Howellsf1752ee2008-11-14 10:39:17 +1100986 cred->security = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800987}
988
989/**
David Howellsd84f4f92008-11-14 10:39:23 +1100990 * smack_cred_prepare - prepare new set of credentials for modification
991 * @new: the new credentials
992 * @old: the original credentials
993 * @gfp: the atomicity of any memory allocations
994 *
995 * Prepare a new set of credentials for modification.
996 */
997static int smack_cred_prepare(struct cred *new, const struct cred *old,
998 gfp_t gfp)
999{
1000 new->security = old->security;
1001 return 0;
1002}
1003
Randy Dunlap251a2a92009-02-18 11:42:33 -08001004/**
1005 * smack_cred_commit - commit new credentials
David Howellsd84f4f92008-11-14 10:39:23 +11001006 * @new: the new credentials
1007 * @old: the original credentials
1008 */
1009static void smack_cred_commit(struct cred *new, const struct cred *old)
1010{
1011}
1012
1013/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001014 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001015 * @new: points to the set of credentials to be modified.
1016 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001017 *
1018 * Set the security data for a kernel service.
1019 */
1020static int smack_kernel_act_as(struct cred *new, u32 secid)
1021{
1022 char *smack = smack_from_secid(secid);
1023
1024 if (smack == NULL)
1025 return -EINVAL;
1026
1027 new->security = smack;
1028 return 0;
1029}
1030
1031/**
1032 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001033 * @new: points to the set of credentials to be modified
1034 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001035 *
1036 * Set the file creation context in a set of credentials to the same
1037 * as the objective context of the specified inode
1038 */
1039static int smack_kernel_create_files_as(struct cred *new,
1040 struct inode *inode)
1041{
1042 struct inode_smack *isp = inode->i_security;
1043
1044 new->security = isp->smk_inode;
1045 return 0;
1046}
1047
1048/**
Casey Schauflere114e472008-02-04 22:29:50 -08001049 * smack_task_setpgid - Smack check on setting pgid
1050 * @p: the task object
1051 * @pgid: unused
1052 *
1053 * Return 0 if write access is permitted
1054 */
1055static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1056{
David Howellsc69e8d92008-11-14 10:39:19 +11001057 return smk_curacc(task_security(p), MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001058}
1059
1060/**
1061 * smack_task_getpgid - Smack access check for getpgid
1062 * @p: the object task
1063 *
1064 * Returns 0 if current can read the object task, error code otherwise
1065 */
1066static int smack_task_getpgid(struct task_struct *p)
1067{
David Howellsc69e8d92008-11-14 10:39:19 +11001068 return smk_curacc(task_security(p), MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001069}
1070
1071/**
1072 * smack_task_getsid - Smack access check for getsid
1073 * @p: the object task
1074 *
1075 * Returns 0 if current can read the object task, error code otherwise
1076 */
1077static int smack_task_getsid(struct task_struct *p)
1078{
David Howellsc69e8d92008-11-14 10:39:19 +11001079 return smk_curacc(task_security(p), MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001080}
1081
1082/**
1083 * smack_task_getsecid - get the secid of the task
1084 * @p: the object task
1085 * @secid: where to put the result
1086 *
1087 * Sets the secid to contain a u32 version of the smack label.
1088 */
1089static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1090{
David Howellsc69e8d92008-11-14 10:39:19 +11001091 *secid = smack_to_secid(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001092}
1093
1094/**
1095 * smack_task_setnice - Smack check on setting nice
1096 * @p: the task object
1097 * @nice: unused
1098 *
1099 * Return 0 if write access is permitted
1100 */
1101static int smack_task_setnice(struct task_struct *p, int nice)
1102{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001103 int rc;
1104
1105 rc = cap_task_setnice(p, nice);
1106 if (rc == 0)
David Howellsc69e8d92008-11-14 10:39:19 +11001107 rc = smk_curacc(task_security(p), MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001108 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001109}
1110
1111/**
1112 * smack_task_setioprio - Smack check on setting ioprio
1113 * @p: the task object
1114 * @ioprio: unused
1115 *
1116 * Return 0 if write access is permitted
1117 */
1118static int smack_task_setioprio(struct task_struct *p, int ioprio)
1119{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001120 int rc;
1121
1122 rc = cap_task_setioprio(p, ioprio);
1123 if (rc == 0)
David Howellsc69e8d92008-11-14 10:39:19 +11001124 rc = smk_curacc(task_security(p), MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001125 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001126}
1127
1128/**
1129 * smack_task_getioprio - Smack check on reading ioprio
1130 * @p: the task object
1131 *
1132 * Return 0 if read access is permitted
1133 */
1134static int smack_task_getioprio(struct task_struct *p)
1135{
David Howellsc69e8d92008-11-14 10:39:19 +11001136 return smk_curacc(task_security(p), MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001137}
1138
1139/**
1140 * smack_task_setscheduler - Smack check on setting scheduler
1141 * @p: the task object
1142 * @policy: unused
1143 * @lp: unused
1144 *
1145 * Return 0 if read access is permitted
1146 */
1147static int smack_task_setscheduler(struct task_struct *p, int policy,
1148 struct sched_param *lp)
1149{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001150 int rc;
1151
1152 rc = cap_task_setscheduler(p, policy, lp);
1153 if (rc == 0)
David Howellsc69e8d92008-11-14 10:39:19 +11001154 rc = smk_curacc(task_security(p), MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001155 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001156}
1157
1158/**
1159 * smack_task_getscheduler - Smack check on reading scheduler
1160 * @p: the task object
1161 *
1162 * Return 0 if read access is permitted
1163 */
1164static int smack_task_getscheduler(struct task_struct *p)
1165{
David Howellsc69e8d92008-11-14 10:39:19 +11001166 return smk_curacc(task_security(p), MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001167}
1168
1169/**
1170 * smack_task_movememory - Smack check on moving memory
1171 * @p: the task object
1172 *
1173 * Return 0 if write access is permitted
1174 */
1175static int smack_task_movememory(struct task_struct *p)
1176{
David Howellsc69e8d92008-11-14 10:39:19 +11001177 return smk_curacc(task_security(p), MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001178}
1179
1180/**
1181 * smack_task_kill - Smack check on signal delivery
1182 * @p: the task object
1183 * @info: unused
1184 * @sig: unused
1185 * @secid: identifies the smack to use in lieu of current's
1186 *
1187 * Return 0 if write access is permitted
1188 *
1189 * The secid behavior is an artifact of an SELinux hack
1190 * in the USB code. Someday it may go away.
1191 */
1192static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1193 int sig, u32 secid)
1194{
1195 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001196 * Sending a signal requires that the sender
1197 * can write the receiver.
1198 */
1199 if (secid == 0)
David Howellsc69e8d92008-11-14 10:39:19 +11001200 return smk_curacc(task_security(p), MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001201 /*
1202 * If the secid isn't 0 we're dealing with some USB IO
1203 * specific behavior. This is not clean. For one thing
1204 * we can't take privilege into account.
1205 */
David Howellsc69e8d92008-11-14 10:39:19 +11001206 return smk_access(smack_from_secid(secid), task_security(p), MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001207}
1208
1209/**
1210 * smack_task_wait - Smack access check for waiting
1211 * @p: task to wait for
1212 *
1213 * Returns 0 if current can wait for p, error code otherwise
1214 */
1215static int smack_task_wait(struct task_struct *p)
1216{
1217 int rc;
1218
David Howellsc69e8d92008-11-14 10:39:19 +11001219 rc = smk_access(current_security(), task_security(p), MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001220 if (rc == 0)
1221 return 0;
1222
1223 /*
1224 * Allow the operation to succeed if either task
1225 * has privilege to perform operations that might
1226 * account for the smack labels having gotten to
1227 * be different in the first place.
1228 *
David Howells5cd9c582008-08-14 11:37:28 +01001229 * This breaks the strict subject/object access
Casey Schauflere114e472008-02-04 22:29:50 -08001230 * control ideal, taking the object's privilege
1231 * state into account in the decision as well as
1232 * the smack value.
1233 */
David Howells5cd9c582008-08-14 11:37:28 +01001234 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -08001235 return 0;
1236
1237 return rc;
1238}
1239
1240/**
1241 * smack_task_to_inode - copy task smack into the inode blob
1242 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001243 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001244 *
1245 * Sets the smack pointer in the inode security blob
1246 */
1247static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1248{
1249 struct inode_smack *isp = inode->i_security;
David Howellsc69e8d92008-11-14 10:39:19 +11001250 isp->smk_inode = task_security(p);
Casey Schauflere114e472008-02-04 22:29:50 -08001251}
1252
1253/*
1254 * Socket hooks.
1255 */
1256
1257/**
1258 * smack_sk_alloc_security - Allocate a socket blob
1259 * @sk: the socket
1260 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001261 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001262 *
1263 * Assign Smack pointers to current
1264 *
1265 * Returns 0 on success, -ENOMEM is there's no memory
1266 */
1267static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1268{
David Howells86a264a2008-11-14 10:39:18 +11001269 char *csp = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001270 struct socket_smack *ssp;
1271
1272 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1273 if (ssp == NULL)
1274 return -ENOMEM;
1275
1276 ssp->smk_in = csp;
1277 ssp->smk_out = csp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001278 ssp->smk_labeled = SMACK_CIPSO_SOCKET;
Casey Schauflere114e472008-02-04 22:29:50 -08001279 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 */
1292static 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 */
1304static 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
Harvey Harrisonc60264c2008-04-28 02:13:41 -07001312 if (!catset)
Casey Schauflere114e472008-02-04 22:29:50 -08001313 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 */
1338static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1339{
1340 struct smack_cipso cipso;
1341 int rc;
1342
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001343 nlsp->domain = smack;
1344 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
Casey Schauflere114e472008-02-04 22:29:50 -08001345
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001346 rc = smack_to_cipso(smack, &cipso);
1347 if (rc == 0) {
1348 nlsp->attr.mls.lvl = cipso.smk_level;
1349 smack_set_catset(cipso.smk_catset, nlsp);
1350 } else {
1351 nlsp->attr.mls.lvl = smack_cipso_direct;
1352 smack_set_catset(smack, nlsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001353 }
1354}
1355
1356/**
1357 * smack_netlabel - Set the secattr on a socket
1358 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001359 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001360 *
1361 * Convert the outbound smack value (smk_out) to a
1362 * secattr and attach it to the socket.
1363 *
1364 * Returns 0 on success or an error code
1365 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001366static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001367{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001368 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001369 struct netlbl_lsm_secattr secattr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001370 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001371
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001372 ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001373 /*
1374 * Usually the netlabel code will handle changing the
1375 * packet labeling based on the label.
1376 * The case of a single label host is different, because
1377 * a single label host should never get a labeled packet
1378 * even though the label is usually associated with a packet
1379 * label.
1380 */
1381 local_bh_disable();
1382 bh_lock_sock_nested(sk);
1383
1384 if (ssp->smk_out == smack_net_ambient ||
1385 labeled == SMACK_UNLABELED_SOCKET)
1386 netlbl_sock_delattr(sk);
1387 else {
1388 netlbl_secattr_init(&secattr);
1389 smack_to_secattr(ssp->smk_out, &secattr);
1390 rc = netlbl_sock_setattr(sk, &secattr);
1391 netlbl_secattr_destroy(&secattr);
1392 }
1393
1394 bh_unlock_sock(sk);
1395 local_bh_enable();
1396 /*
1397 * Remember the label scheme used so that it is not
1398 * necessary to do the netlabel setting if it has not
1399 * changed the next time through.
1400 *
1401 * The -EDESTADDRREQ case is an indication that there's
1402 * a single level host involved.
1403 */
1404 if (rc == 0)
1405 ssp->smk_labeled = labeled;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001406
Casey Schauflere114e472008-02-04 22:29:50 -08001407 return rc;
1408}
1409
1410/**
1411 * smack_inode_setsecurity - set smack xattrs
1412 * @inode: the object
1413 * @name: attribute name
1414 * @value: attribute value
1415 * @size: size of the attribute
1416 * @flags: unused
1417 *
1418 * Sets the named attribute in the appropriate blob
1419 *
1420 * Returns 0 on success, or an error code
1421 */
1422static int smack_inode_setsecurity(struct inode *inode, const char *name,
1423 const void *value, size_t size, int flags)
1424{
1425 char *sp;
1426 struct inode_smack *nsp = inode->i_security;
1427 struct socket_smack *ssp;
1428 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001429 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001430
1431 if (value == NULL || size > SMK_LABELLEN)
1432 return -EACCES;
1433
1434 sp = smk_import(value, size);
1435 if (sp == NULL)
1436 return -EINVAL;
1437
1438 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1439 nsp->smk_inode = sp;
1440 return 0;
1441 }
1442 /*
1443 * The rest of the Smack xattrs are only on sockets.
1444 */
1445 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1446 return -EOPNOTSUPP;
1447
1448 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001449 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001450 return -EOPNOTSUPP;
1451
1452 ssp = sock->sk->sk_security;
1453
1454 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1455 ssp->smk_in = sp;
1456 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1457 ssp->smk_out = sp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001458 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001459 if (rc != 0)
1460 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1461 __func__, -rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001462 } else
1463 return -EOPNOTSUPP;
1464
1465 return 0;
1466}
1467
1468/**
1469 * smack_socket_post_create - finish socket setup
1470 * @sock: the socket
1471 * @family: protocol family
1472 * @type: unused
1473 * @protocol: unused
1474 * @kern: unused
1475 *
1476 * Sets the netlabel information on the socket
1477 *
1478 * Returns 0 on success, and error code otherwise
1479 */
1480static int smack_socket_post_create(struct socket *sock, int family,
1481 int type, int protocol, int kern)
1482{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001483 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001484 return 0;
1485 /*
1486 * Set the outbound netlbl.
1487 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001488 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1489}
1490
1491
1492/**
1493 * smack_host_label - check host based restrictions
1494 * @sip: the object end
1495 *
1496 * looks for host based access restrictions
1497 *
1498 * This version will only be appropriate for really small
1499 * sets of single label hosts. Because of the masking
1500 * it cannot shortcut out on the first match. There are
1501 * numerious ways to address the problem, but none of them
1502 * have been applied here.
1503 *
1504 * Returns the label of the far end or NULL if it's not special.
1505 */
1506static char *smack_host_label(struct sockaddr_in *sip)
1507{
1508 struct smk_netlbladdr *snp;
1509 char *bestlabel = NULL;
1510 struct in_addr *siap = &sip->sin_addr;
1511 struct in_addr *liap;
1512 struct in_addr *miap;
1513 struct in_addr bestmask;
1514
1515 if (siap->s_addr == 0)
1516 return NULL;
1517
1518 bestmask.s_addr = 0;
1519
1520 for (snp = smack_netlbladdrs; snp != NULL; snp = snp->smk_next) {
1521 liap = &snp->smk_host.sin_addr;
1522 miap = &snp->smk_mask;
1523 /*
1524 * If the addresses match after applying the list entry mask
1525 * the entry matches the address. If it doesn't move along to
1526 * the next entry.
1527 */
1528 if ((liap->s_addr & miap->s_addr) !=
1529 (siap->s_addr & miap->s_addr))
1530 continue;
1531 /*
1532 * If the list entry mask identifies a single address
1533 * it can't get any more specific.
1534 */
1535 if (miap->s_addr == 0xffffffff)
1536 return snp->smk_label;
1537 /*
1538 * If the list entry mask is less specific than the best
1539 * already found this entry is uninteresting.
1540 */
1541 if ((miap->s_addr | bestmask.s_addr) == bestmask.s_addr)
1542 continue;
1543 /*
1544 * This is better than any entry found so far.
1545 */
1546 bestmask.s_addr = miap->s_addr;
1547 bestlabel = snp->smk_label;
1548 }
1549
1550 return bestlabel;
1551}
1552
1553/**
1554 * smack_socket_connect - connect access check
1555 * @sock: the socket
1556 * @sap: the other end
1557 * @addrlen: size of sap
1558 *
1559 * Verifies that a connection may be possible
1560 *
1561 * Returns 0 on success, and error code otherwise
1562 */
1563static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1564 int addrlen)
1565{
1566 struct socket_smack *ssp = sock->sk->sk_security;
1567 char *hostsp;
1568 int rc;
1569
1570 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1571 return 0;
1572
1573 if (addrlen < sizeof(struct sockaddr_in))
1574 return -EINVAL;
1575
1576 hostsp = smack_host_label((struct sockaddr_in *)sap);
1577 if (hostsp == NULL) {
1578 if (ssp->smk_labeled != SMACK_CIPSO_SOCKET)
1579 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1580 return 0;
1581 }
1582
1583 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE);
1584 if (rc != 0)
1585 return rc;
1586
1587 if (ssp->smk_labeled != SMACK_UNLABELED_SOCKET)
1588 return smack_netlabel(sock->sk, SMACK_UNLABELED_SOCKET);
1589 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001590}
1591
1592/**
1593 * smack_flags_to_may - convert S_ to MAY_ values
1594 * @flags: the S_ value
1595 *
1596 * Returns the equivalent MAY_ value
1597 */
1598static int smack_flags_to_may(int flags)
1599{
1600 int may = 0;
1601
1602 if (flags & S_IRUGO)
1603 may |= MAY_READ;
1604 if (flags & S_IWUGO)
1605 may |= MAY_WRITE;
1606 if (flags & S_IXUGO)
1607 may |= MAY_EXEC;
1608
1609 return may;
1610}
1611
1612/**
1613 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1614 * @msg: the object
1615 *
1616 * Returns 0
1617 */
1618static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1619{
David Howells86a264a2008-11-14 10:39:18 +11001620 msg->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001621 return 0;
1622}
1623
1624/**
1625 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1626 * @msg: the object
1627 *
1628 * Clears the blob pointer
1629 */
1630static void smack_msg_msg_free_security(struct msg_msg *msg)
1631{
1632 msg->security = NULL;
1633}
1634
1635/**
1636 * smack_of_shm - the smack pointer for the shm
1637 * @shp: the object
1638 *
1639 * Returns a pointer to the smack value
1640 */
1641static char *smack_of_shm(struct shmid_kernel *shp)
1642{
1643 return (char *)shp->shm_perm.security;
1644}
1645
1646/**
1647 * smack_shm_alloc_security - Set the security blob for shm
1648 * @shp: the object
1649 *
1650 * Returns 0
1651 */
1652static int smack_shm_alloc_security(struct shmid_kernel *shp)
1653{
1654 struct kern_ipc_perm *isp = &shp->shm_perm;
1655
David Howells86a264a2008-11-14 10:39:18 +11001656 isp->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001657 return 0;
1658}
1659
1660/**
1661 * smack_shm_free_security - Clear the security blob for shm
1662 * @shp: the object
1663 *
1664 * Clears the blob pointer
1665 */
1666static void smack_shm_free_security(struct shmid_kernel *shp)
1667{
1668 struct kern_ipc_perm *isp = &shp->shm_perm;
1669
1670 isp->security = NULL;
1671}
1672
1673/**
1674 * smack_shm_associate - Smack access check for shm
1675 * @shp: the object
1676 * @shmflg: access requested
1677 *
1678 * Returns 0 if current has the requested access, error code otherwise
1679 */
1680static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1681{
1682 char *ssp = smack_of_shm(shp);
1683 int may;
1684
1685 may = smack_flags_to_may(shmflg);
1686 return smk_curacc(ssp, may);
1687}
1688
1689/**
1690 * smack_shm_shmctl - Smack access check for shm
1691 * @shp: the object
1692 * @cmd: what it wants to do
1693 *
1694 * Returns 0 if current has the requested access, error code otherwise
1695 */
1696static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1697{
Ahmed S. Darwish1d252fb2008-03-19 17:00:51 -07001698 char *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001699 int may;
1700
1701 switch (cmd) {
1702 case IPC_STAT:
1703 case SHM_STAT:
1704 may = MAY_READ;
1705 break;
1706 case IPC_SET:
1707 case SHM_LOCK:
1708 case SHM_UNLOCK:
1709 case IPC_RMID:
1710 may = MAY_READWRITE;
1711 break;
1712 case IPC_INFO:
1713 case SHM_INFO:
1714 /*
1715 * System level information.
1716 */
1717 return 0;
1718 default:
1719 return -EINVAL;
1720 }
1721
Ahmed S. Darwish1d252fb2008-03-19 17:00:51 -07001722 ssp = smack_of_shm(shp);
Casey Schauflere114e472008-02-04 22:29:50 -08001723 return smk_curacc(ssp, may);
1724}
1725
1726/**
1727 * smack_shm_shmat - Smack access for shmat
1728 * @shp: the object
1729 * @shmaddr: unused
1730 * @shmflg: access requested
1731 *
1732 * Returns 0 if current has the requested access, error code otherwise
1733 */
1734static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1735 int shmflg)
1736{
1737 char *ssp = smack_of_shm(shp);
1738 int may;
1739
1740 may = smack_flags_to_may(shmflg);
1741 return smk_curacc(ssp, may);
1742}
1743
1744/**
1745 * smack_of_sem - the smack pointer for the sem
1746 * @sma: the object
1747 *
1748 * Returns a pointer to the smack value
1749 */
1750static char *smack_of_sem(struct sem_array *sma)
1751{
1752 return (char *)sma->sem_perm.security;
1753}
1754
1755/**
1756 * smack_sem_alloc_security - Set the security blob for sem
1757 * @sma: the object
1758 *
1759 * Returns 0
1760 */
1761static int smack_sem_alloc_security(struct sem_array *sma)
1762{
1763 struct kern_ipc_perm *isp = &sma->sem_perm;
1764
David Howells86a264a2008-11-14 10:39:18 +11001765 isp->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001766 return 0;
1767}
1768
1769/**
1770 * smack_sem_free_security - Clear the security blob for sem
1771 * @sma: the object
1772 *
1773 * Clears the blob pointer
1774 */
1775static void smack_sem_free_security(struct sem_array *sma)
1776{
1777 struct kern_ipc_perm *isp = &sma->sem_perm;
1778
1779 isp->security = NULL;
1780}
1781
1782/**
1783 * smack_sem_associate - Smack access check for sem
1784 * @sma: the object
1785 * @semflg: access requested
1786 *
1787 * Returns 0 if current has the requested access, error code otherwise
1788 */
1789static int smack_sem_associate(struct sem_array *sma, int semflg)
1790{
1791 char *ssp = smack_of_sem(sma);
1792 int may;
1793
1794 may = smack_flags_to_may(semflg);
1795 return smk_curacc(ssp, may);
1796}
1797
1798/**
1799 * smack_sem_shmctl - Smack access check for sem
1800 * @sma: the object
1801 * @cmd: what it wants to do
1802 *
1803 * Returns 0 if current has the requested access, error code otherwise
1804 */
1805static int smack_sem_semctl(struct sem_array *sma, int cmd)
1806{
Ahmed S. Darwish1d252fb2008-03-19 17:00:51 -07001807 char *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001808 int may;
1809
1810 switch (cmd) {
1811 case GETPID:
1812 case GETNCNT:
1813 case GETZCNT:
1814 case GETVAL:
1815 case GETALL:
1816 case IPC_STAT:
1817 case SEM_STAT:
1818 may = MAY_READ;
1819 break;
1820 case SETVAL:
1821 case SETALL:
1822 case IPC_RMID:
1823 case IPC_SET:
1824 may = MAY_READWRITE;
1825 break;
1826 case IPC_INFO:
1827 case SEM_INFO:
1828 /*
1829 * System level information
1830 */
1831 return 0;
1832 default:
1833 return -EINVAL;
1834 }
1835
Ahmed S. Darwish1d252fb2008-03-19 17:00:51 -07001836 ssp = smack_of_sem(sma);
Casey Schauflere114e472008-02-04 22:29:50 -08001837 return smk_curacc(ssp, may);
1838}
1839
1840/**
1841 * smack_sem_semop - Smack checks of semaphore operations
1842 * @sma: the object
1843 * @sops: unused
1844 * @nsops: unused
1845 * @alter: unused
1846 *
1847 * Treated as read and write in all cases.
1848 *
1849 * Returns 0 if access is allowed, error code otherwise
1850 */
1851static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
1852 unsigned nsops, int alter)
1853{
1854 char *ssp = smack_of_sem(sma);
1855
1856 return smk_curacc(ssp, MAY_READWRITE);
1857}
1858
1859/**
1860 * smack_msg_alloc_security - Set the security blob for msg
1861 * @msq: the object
1862 *
1863 * Returns 0
1864 */
1865static int smack_msg_queue_alloc_security(struct msg_queue *msq)
1866{
1867 struct kern_ipc_perm *kisp = &msq->q_perm;
1868
David Howells86a264a2008-11-14 10:39:18 +11001869 kisp->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001870 return 0;
1871}
1872
1873/**
1874 * smack_msg_free_security - Clear the security blob for msg
1875 * @msq: the object
1876 *
1877 * Clears the blob pointer
1878 */
1879static void smack_msg_queue_free_security(struct msg_queue *msq)
1880{
1881 struct kern_ipc_perm *kisp = &msq->q_perm;
1882
1883 kisp->security = NULL;
1884}
1885
1886/**
1887 * smack_of_msq - the smack pointer for the msq
1888 * @msq: the object
1889 *
1890 * Returns a pointer to the smack value
1891 */
1892static char *smack_of_msq(struct msg_queue *msq)
1893{
1894 return (char *)msq->q_perm.security;
1895}
1896
1897/**
1898 * smack_msg_queue_associate - Smack access check for msg_queue
1899 * @msq: the object
1900 * @msqflg: access requested
1901 *
1902 * Returns 0 if current has the requested access, error code otherwise
1903 */
1904static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
1905{
1906 char *msp = smack_of_msq(msq);
1907 int may;
1908
1909 may = smack_flags_to_may(msqflg);
1910 return smk_curacc(msp, may);
1911}
1912
1913/**
1914 * smack_msg_queue_msgctl - Smack access check for msg_queue
1915 * @msq: the object
1916 * @cmd: what it wants to do
1917 *
1918 * Returns 0 if current has the requested access, error code otherwise
1919 */
1920static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1921{
Ahmed S. Darwish1d252fb2008-03-19 17:00:51 -07001922 char *msp;
Casey Schauflere114e472008-02-04 22:29:50 -08001923 int may;
1924
1925 switch (cmd) {
1926 case IPC_STAT:
1927 case MSG_STAT:
1928 may = MAY_READ;
1929 break;
1930 case IPC_SET:
1931 case IPC_RMID:
1932 may = MAY_READWRITE;
1933 break;
1934 case IPC_INFO:
1935 case MSG_INFO:
1936 /*
1937 * System level information
1938 */
1939 return 0;
1940 default:
1941 return -EINVAL;
1942 }
1943
Ahmed S. Darwish1d252fb2008-03-19 17:00:51 -07001944 msp = smack_of_msq(msq);
Casey Schauflere114e472008-02-04 22:29:50 -08001945 return smk_curacc(msp, may);
1946}
1947
1948/**
1949 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1950 * @msq: the object
1951 * @msg: unused
1952 * @msqflg: access requested
1953 *
1954 * Returns 0 if current has the requested access, error code otherwise
1955 */
1956static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
1957 int msqflg)
1958{
1959 char *msp = smack_of_msq(msq);
1960 int rc;
1961
1962 rc = smack_flags_to_may(msqflg);
1963 return smk_curacc(msp, rc);
1964}
1965
1966/**
1967 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1968 * @msq: the object
1969 * @msg: unused
1970 * @target: unused
1971 * @type: unused
1972 * @mode: unused
1973 *
1974 * Returns 0 if current has read and write access, error code otherwise
1975 */
1976static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1977 struct task_struct *target, long type, int mode)
1978{
1979 char *msp = smack_of_msq(msq);
1980
1981 return smk_curacc(msp, MAY_READWRITE);
1982}
1983
1984/**
1985 * smack_ipc_permission - Smack access for ipc_permission()
1986 * @ipp: the object permissions
1987 * @flag: access requested
1988 *
1989 * Returns 0 if current has read and write access, error code otherwise
1990 */
1991static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
1992{
1993 char *isp = ipp->security;
1994 int may;
1995
1996 may = smack_flags_to_may(flag);
1997 return smk_curacc(isp, may);
1998}
1999
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002000/**
2001 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002002 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002003 * @secid: where result will be saved
2004 */
2005static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2006{
2007 char *smack = ipp->security;
2008
2009 *secid = smack_to_secid(smack);
2010}
2011
Casey Schauflere114e472008-02-04 22:29:50 -08002012/**
2013 * smack_d_instantiate - Make sure the blob is correct on an inode
2014 * @opt_dentry: unused
2015 * @inode: the object
2016 *
2017 * Set the inode's security blob if it hasn't been done already.
2018 */
2019static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2020{
2021 struct super_block *sbp;
2022 struct superblock_smack *sbsp;
2023 struct inode_smack *isp;
David Howells86a264a2008-11-14 10:39:18 +11002024 char *csp = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08002025 char *fetched;
2026 char *final;
2027 struct dentry *dp;
2028
2029 if (inode == NULL)
2030 return;
2031
2032 isp = inode->i_security;
2033
2034 mutex_lock(&isp->smk_lock);
2035 /*
2036 * If the inode is already instantiated
2037 * take the quick way out
2038 */
2039 if (isp->smk_flags & SMK_INODE_INSTANT)
2040 goto unlockandout;
2041
2042 sbp = inode->i_sb;
2043 sbsp = sbp->s_security;
2044 /*
2045 * We're going to use the superblock default label
2046 * if there's no label on the file.
2047 */
2048 final = sbsp->smk_default;
2049
2050 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002051 * If this is the root inode the superblock
2052 * may be in the process of initialization.
2053 * If that is the case use the root value out
2054 * of the superblock.
2055 */
2056 if (opt_dentry->d_parent == opt_dentry) {
2057 isp->smk_inode = sbsp->smk_root;
2058 isp->smk_flags |= SMK_INODE_INSTANT;
2059 goto unlockandout;
2060 }
2061
2062 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002063 * This is pretty hackish.
2064 * Casey says that we shouldn't have to do
2065 * file system specific code, but it does help
2066 * with keeping it simple.
2067 */
2068 switch (sbp->s_magic) {
2069 case SMACK_MAGIC:
2070 /*
2071 * Casey says that it's a little embarassing
2072 * that the smack file system doesn't do
2073 * extended attributes.
2074 */
2075 final = smack_known_star.smk_known;
2076 break;
2077 case PIPEFS_MAGIC:
2078 /*
2079 * Casey says pipes are easy (?)
2080 */
2081 final = smack_known_star.smk_known;
2082 break;
2083 case DEVPTS_SUPER_MAGIC:
2084 /*
2085 * devpts seems content with the label of the task.
2086 * Programs that change smack have to treat the
2087 * pty with respect.
2088 */
2089 final = csp;
2090 break;
2091 case SOCKFS_MAGIC:
2092 /*
2093 * Casey says sockets get the smack of the task.
2094 */
2095 final = csp;
2096 break;
2097 case PROC_SUPER_MAGIC:
2098 /*
2099 * Casey says procfs appears not to care.
2100 * The superblock default suffices.
2101 */
2102 break;
2103 case TMPFS_MAGIC:
2104 /*
2105 * Device labels should come from the filesystem,
2106 * but watch out, because they're volitile,
2107 * getting recreated on every reboot.
2108 */
2109 final = smack_known_star.smk_known;
2110 /*
2111 * No break.
2112 *
2113 * If a smack value has been set we want to use it,
2114 * but since tmpfs isn't giving us the opportunity
2115 * to set mount options simulate setting the
2116 * superblock default.
2117 */
2118 default:
2119 /*
2120 * This isn't an understood special case.
2121 * Get the value from the xattr.
2122 *
2123 * No xattr support means, alas, no SMACK label.
2124 * Use the aforeapplied default.
2125 * It would be curious if the label of the task
2126 * does not match that assigned.
2127 */
2128 if (inode->i_op->getxattr == NULL)
2129 break;
2130 /*
2131 * Get the dentry for xattr.
2132 */
2133 if (opt_dentry == NULL) {
2134 dp = d_find_alias(inode);
2135 if (dp == NULL)
2136 break;
2137 } else {
2138 dp = dget(opt_dentry);
2139 if (dp == NULL)
2140 break;
2141 }
2142
2143 fetched = smk_fetch(inode, dp);
2144 if (fetched != NULL)
2145 final = fetched;
2146
2147 dput(dp);
2148 break;
2149 }
2150
2151 if (final == NULL)
2152 isp->smk_inode = csp;
2153 else
2154 isp->smk_inode = final;
2155
2156 isp->smk_flags |= SMK_INODE_INSTANT;
2157
2158unlockandout:
2159 mutex_unlock(&isp->smk_lock);
2160 return;
2161}
2162
2163/**
2164 * smack_getprocattr - Smack process attribute access
2165 * @p: the object task
2166 * @name: the name of the attribute in /proc/.../attr
2167 * @value: where to put the result
2168 *
2169 * Places a copy of the task Smack into value
2170 *
2171 * Returns the length of the smack label or an error code
2172 */
2173static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2174{
2175 char *cp;
2176 int slen;
2177
2178 if (strcmp(name, "current") != 0)
2179 return -EINVAL;
2180
David Howellsc69e8d92008-11-14 10:39:19 +11002181 cp = kstrdup(task_security(p), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002182 if (cp == NULL)
2183 return -ENOMEM;
2184
2185 slen = strlen(cp);
2186 *value = cp;
2187 return slen;
2188}
2189
2190/**
2191 * smack_setprocattr - Smack process attribute setting
2192 * @p: the object task
2193 * @name: the name of the attribute in /proc/.../attr
2194 * @value: the value to set
2195 * @size: the size of the value
2196 *
2197 * Sets the Smack value of the task. Only setting self
2198 * is permitted and only with privilege
2199 *
2200 * Returns the length of the smack label or an error code
2201 */
2202static int smack_setprocattr(struct task_struct *p, char *name,
2203 void *value, size_t size)
2204{
David Howellsd84f4f92008-11-14 10:39:23 +11002205 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002206 char *newsmack;
2207
Casey Schauflere114e472008-02-04 22:29:50 -08002208 /*
2209 * Changing another process' Smack value is too dangerous
2210 * and supports no sane use case.
2211 */
2212 if (p != current)
2213 return -EPERM;
2214
David Howells5cd9c582008-08-14 11:37:28 +01002215 if (!capable(CAP_MAC_ADMIN))
2216 return -EPERM;
2217
Casey Schauflere114e472008-02-04 22:29:50 -08002218 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2219 return -EINVAL;
2220
2221 if (strcmp(name, "current") != 0)
2222 return -EINVAL;
2223
2224 newsmack = smk_import(value, size);
2225 if (newsmack == NULL)
2226 return -EINVAL;
2227
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002228 /*
2229 * No process is ever allowed the web ("@") label.
2230 */
2231 if (newsmack == smack_known_web.smk_known)
2232 return -EPERM;
2233
David Howellsd84f4f92008-11-14 10:39:23 +11002234 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002235 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002236 return -ENOMEM;
2237 new->security = newsmack;
2238 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002239 return size;
2240}
2241
2242/**
2243 * smack_unix_stream_connect - Smack access on UDS
2244 * @sock: one socket
2245 * @other: the other socket
2246 * @newsk: unused
2247 *
2248 * Return 0 if a subject with the smack of sock could access
2249 * an object with the smack of other, otherwise an error code
2250 */
2251static int smack_unix_stream_connect(struct socket *sock,
2252 struct socket *other, struct sock *newsk)
2253{
2254 struct inode *sp = SOCK_INODE(sock);
2255 struct inode *op = SOCK_INODE(other);
2256
2257 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
2258}
2259
2260/**
2261 * smack_unix_may_send - Smack access on UDS
2262 * @sock: one socket
2263 * @other: the other socket
2264 *
2265 * Return 0 if a subject with the smack of sock could access
2266 * an object with the smack of other, otherwise an error code
2267 */
2268static int smack_unix_may_send(struct socket *sock, struct socket *other)
2269{
2270 struct inode *sp = SOCK_INODE(sock);
2271 struct inode *op = SOCK_INODE(other);
2272
2273 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
2274}
2275
2276/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002277 * smack_socket_sendmsg - Smack check based on destination host
2278 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002279 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002280 * @size: the size of the message
2281 *
2282 * Return 0 if the current subject can write to the destination
2283 * host. This is only a question if the destination is a single
2284 * label host.
2285 */
2286static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2287 int size)
2288{
2289 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2290 struct socket_smack *ssp = sock->sk->sk_security;
2291 char *hostsp;
2292 int rc;
2293
2294 /*
2295 * Perfectly reasonable for this to be NULL
2296 */
2297 if (sip == NULL || sip->sin_family != PF_INET)
2298 return 0;
2299
2300 hostsp = smack_host_label(sip);
2301 if (hostsp == NULL) {
2302 if (ssp->smk_labeled != SMACK_CIPSO_SOCKET)
2303 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2304 return 0;
2305 }
2306
2307 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE);
2308 if (rc != 0)
2309 return rc;
2310
2311 if (ssp->smk_labeled != SMACK_UNLABELED_SOCKET)
2312 return smack_netlabel(sock->sk, SMACK_UNLABELED_SOCKET);
2313
2314 return 0;
2315
2316}
2317
2318
2319/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08002320 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08002321 * @sap: netlabel secattr
2322 * @sip: where to put the result
2323 *
2324 * Copies a smack label into sip
2325 */
2326static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2327{
2328 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002329 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002330 int pcat;
2331
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002332 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002333 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002334 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08002335 * If there are flags but no level netlabel isn't
2336 * behaving the way we expect it to.
2337 *
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002338 * Get the categories, if any
Casey Schauflere114e472008-02-04 22:29:50 -08002339 * Without guidance regarding the smack value
2340 * for the packet fall back on the network
2341 * ambient value.
2342 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002343 memset(smack, '\0', SMK_LABELLEN);
2344 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2345 for (pcat = -1;;) {
2346 pcat = netlbl_secattr_catmap_walk(
2347 sap->attr.mls.cat, pcat + 1);
2348 if (pcat < 0)
2349 break;
2350 smack_catset_bit(pcat, smack);
2351 }
2352 /*
2353 * If it is CIPSO using smack direct mapping
2354 * we are already done. WeeHee.
2355 */
2356 if (sap->attr.mls.lvl == smack_cipso_direct) {
2357 memcpy(sip, smack, SMK_MAXLEN);
2358 return;
Casey Schauflere114e472008-02-04 22:29:50 -08002359 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002360 /*
2361 * Look it up in the supplied table if it is not
2362 * a direct mapping.
2363 */
2364 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2365 return;
2366 }
2367 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2368 /*
2369 * Looks like a fallback, which gives us a secid.
2370 */
2371 sp = smack_from_secid(sap->attr.secid);
2372 /*
2373 * This has got to be a bug because it is
2374 * impossible to specify a fallback without
2375 * specifying the label, which will ensure
2376 * it has a secid, and the only way to get a
2377 * secid is from a fallback.
2378 */
2379 BUG_ON(sp == NULL);
2380 strncpy(sip, sp, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002381 return;
2382 }
2383 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002384 * Without guidance regarding the smack value
2385 * for the packet fall back on the network
2386 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08002387 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002388 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002389 return;
2390}
2391
2392/**
2393 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2394 * @sk: socket
2395 * @skb: packet
2396 *
2397 * Returns 0 if the packet should be delivered, an error code otherwise
2398 */
2399static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2400{
2401 struct netlbl_lsm_secattr secattr;
2402 struct socket_smack *ssp = sk->sk_security;
2403 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002404 char *csp;
Casey Schauflere114e472008-02-04 22:29:50 -08002405 int rc;
2406
2407 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2408 return 0;
2409
2410 /*
2411 * Translate what netlabel gave us.
2412 */
Casey Schauflere114e472008-02-04 22:29:50 -08002413 netlbl_secattr_init(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002414
Casey Schauflere114e472008-02-04 22:29:50 -08002415 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002416 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002417 smack_from_secattr(&secattr, smack);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002418 csp = smack;
2419 } else
2420 csp = smack_net_ambient;
2421
Casey Schauflere114e472008-02-04 22:29:50 -08002422 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002423
Casey Schauflere114e472008-02-04 22:29:50 -08002424 /*
2425 * Receiving a packet requires that the other end
2426 * be able to write here. Read access is not required.
2427 * This is the simplist possible security model
2428 * for networking.
2429 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002430 rc = smk_access(csp, ssp->smk_in, MAY_WRITE);
Paul Moorea8134292008-10-10 10:16:31 -04002431 if (rc != 0)
2432 netlbl_skbuff_err(skb, rc, 0);
2433 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002434}
2435
2436/**
2437 * smack_socket_getpeersec_stream - pull in packet label
2438 * @sock: the socket
2439 * @optval: user's destination
2440 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08002441 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08002442 *
2443 * returns zero on success, an error code otherwise
2444 */
2445static int smack_socket_getpeersec_stream(struct socket *sock,
2446 char __user *optval,
2447 int __user *optlen, unsigned len)
2448{
2449 struct socket_smack *ssp;
2450 int slen;
2451 int rc = 0;
2452
2453 ssp = sock->sk->sk_security;
2454 slen = strlen(ssp->smk_packet) + 1;
2455
2456 if (slen > len)
2457 rc = -ERANGE;
2458 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2459 rc = -EFAULT;
2460
2461 if (put_user(slen, optlen) != 0)
2462 rc = -EFAULT;
2463
2464 return rc;
2465}
2466
2467
2468/**
2469 * smack_socket_getpeersec_dgram - pull in packet label
2470 * @sock: the socket
2471 * @skb: packet data
2472 * @secid: pointer to where to put the secid of the packet
2473 *
2474 * Sets the netlabel socket state on sk from parent
2475 */
2476static int smack_socket_getpeersec_dgram(struct socket *sock,
2477 struct sk_buff *skb, u32 *secid)
2478
2479{
2480 struct netlbl_lsm_secattr secattr;
2481 struct sock *sk;
2482 char smack[SMK_LABELLEN];
2483 int family = PF_INET;
2484 u32 s;
2485 int rc;
2486
2487 /*
2488 * Only works for families with packets.
2489 */
2490 if (sock != NULL) {
2491 sk = sock->sk;
2492 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2493 return 0;
2494 family = sk->sk_family;
2495 }
2496 /*
2497 * Translate what netlabel gave us.
2498 */
Casey Schauflere114e472008-02-04 22:29:50 -08002499 netlbl_secattr_init(&secattr);
2500 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2501 if (rc == 0)
2502 smack_from_secattr(&secattr, smack);
2503 netlbl_secattr_destroy(&secattr);
2504
2505 /*
2506 * Give up if we couldn't get anything
2507 */
2508 if (rc != 0)
2509 return rc;
2510
2511 s = smack_to_secid(smack);
2512 if (s == 0)
2513 return -EINVAL;
2514
2515 *secid = s;
2516 return 0;
2517}
2518
2519/**
2520 * smack_sock_graft - graft access state between two sockets
2521 * @sk: fresh sock
2522 * @parent: donor socket
2523 *
2524 * Sets the netlabel socket state on sk from parent
2525 */
2526static void smack_sock_graft(struct sock *sk, struct socket *parent)
2527{
2528 struct socket_smack *ssp;
2529 int rc;
2530
2531 if (sk == NULL)
2532 return;
2533
2534 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2535 return;
2536
2537 ssp = sk->sk_security;
David Howells86a264a2008-11-14 10:39:18 +11002538 ssp->smk_in = ssp->smk_out = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08002539 ssp->smk_packet[0] = '\0';
2540
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002541 rc = smack_netlabel(sk, SMACK_CIPSO_SOCKET);
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002542 if (rc != 0)
2543 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
2544 __func__, -rc);
Casey Schauflere114e472008-02-04 22:29:50 -08002545}
2546
2547/**
2548 * smack_inet_conn_request - Smack access check on connect
2549 * @sk: socket involved
2550 * @skb: packet
2551 * @req: unused
2552 *
2553 * Returns 0 if a task with the packet label could write to
2554 * the socket, otherwise an error code
2555 */
2556static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2557 struct request_sock *req)
2558{
2559 struct netlbl_lsm_secattr skb_secattr;
2560 struct socket_smack *ssp = sk->sk_security;
2561 char smack[SMK_LABELLEN];
2562 int rc;
2563
2564 if (skb == NULL)
2565 return -EACCES;
2566
Casey Schauflere114e472008-02-04 22:29:50 -08002567 netlbl_secattr_init(&skb_secattr);
2568 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr);
2569 if (rc == 0)
2570 smack_from_secattr(&skb_secattr, smack);
2571 else
2572 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2573 netlbl_secattr_destroy(&skb_secattr);
2574 /*
2575 * Receiving a packet requires that the other end
2576 * be able to write here. Read access is not required.
2577 *
2578 * If the request is successful save the peer's label
2579 * so that SO_PEERCRED can report it.
2580 */
2581 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2582 if (rc == 0)
2583 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2584
2585 return rc;
2586}
2587
2588/*
2589 * Key management security hooks
2590 *
2591 * Casey has not tested key support very heavily.
2592 * The permission check is most likely too restrictive.
2593 * If you care about keys please have a look.
2594 */
2595#ifdef CONFIG_KEYS
2596
2597/**
2598 * smack_key_alloc - Set the key security blob
2599 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11002600 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08002601 * @flags: unused
2602 *
2603 * No allocation required
2604 *
2605 * Returns 0
2606 */
David Howellsd84f4f92008-11-14 10:39:23 +11002607static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08002608 unsigned long flags)
2609{
David Howellsd84f4f92008-11-14 10:39:23 +11002610 key->security = cred->security;
Casey Schauflere114e472008-02-04 22:29:50 -08002611 return 0;
2612}
2613
2614/**
2615 * smack_key_free - Clear the key security blob
2616 * @key: the object
2617 *
2618 * Clear the blob pointer
2619 */
2620static void smack_key_free(struct key *key)
2621{
2622 key->security = NULL;
2623}
2624
2625/*
2626 * smack_key_permission - Smack access on a key
2627 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11002628 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08002629 * @perm: unused
2630 *
2631 * Return 0 if the task has read and write to the object,
2632 * an error code otherwise
2633 */
2634static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11002635 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08002636{
2637 struct key *keyp;
2638
2639 keyp = key_ref_to_ptr(key_ref);
2640 if (keyp == NULL)
2641 return -EINVAL;
2642 /*
2643 * If the key hasn't been initialized give it access so that
2644 * it may do so.
2645 */
2646 if (keyp->security == NULL)
2647 return 0;
2648 /*
2649 * This should not occur
2650 */
David Howellsd84f4f92008-11-14 10:39:23 +11002651 if (cred->security == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002652 return -EACCES;
2653
David Howellsd84f4f92008-11-14 10:39:23 +11002654 return smk_access(cred->security, keyp->security, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002655}
2656#endif /* CONFIG_KEYS */
2657
2658/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002659 * Smack Audit hooks
2660 *
2661 * Audit requires a unique representation of each Smack specific
2662 * rule. This unique representation is used to distinguish the
2663 * object to be audited from remaining kernel objects and also
2664 * works as a glue between the audit hooks.
2665 *
2666 * Since repository entries are added but never deleted, we'll use
2667 * the smack_known label address related to the given audit rule as
2668 * the needed unique representation. This also better fits the smack
2669 * model where nearly everything is a label.
2670 */
2671#ifdef CONFIG_AUDIT
2672
2673/**
2674 * smack_audit_rule_init - Initialize a smack audit rule
2675 * @field: audit rule fields given from user-space (audit.h)
2676 * @op: required testing operator (=, !=, >, <, ...)
2677 * @rulestr: smack label to be audited
2678 * @vrule: pointer to save our own audit rule representation
2679 *
2680 * Prepare to audit cases where (@field @op @rulestr) is true.
2681 * The label to be audited is created if necessay.
2682 */
2683static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2684{
2685 char **rule = (char **)vrule;
2686 *rule = NULL;
2687
2688 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2689 return -EINVAL;
2690
Al Viro5af75d82008-12-16 05:59:26 -05002691 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002692 return -EINVAL;
2693
2694 *rule = smk_import(rulestr, 0);
2695
2696 return 0;
2697}
2698
2699/**
2700 * smack_audit_rule_known - Distinguish Smack audit rules
2701 * @krule: rule of interest, in Audit kernel representation format
2702 *
2703 * This is used to filter Smack rules from remaining Audit ones.
2704 * If it's proved that this rule belongs to us, the
2705 * audit_rule_match hook will be called to do the final judgement.
2706 */
2707static int smack_audit_rule_known(struct audit_krule *krule)
2708{
2709 struct audit_field *f;
2710 int i;
2711
2712 for (i = 0; i < krule->field_count; i++) {
2713 f = &krule->fields[i];
2714
2715 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2716 return 1;
2717 }
2718
2719 return 0;
2720}
2721
2722/**
2723 * smack_audit_rule_match - Audit given object ?
2724 * @secid: security id for identifying the object to test
2725 * @field: audit rule flags given from user-space
2726 * @op: required testing operator
2727 * @vrule: smack internal rule presentation
2728 * @actx: audit context associated with the check
2729 *
2730 * The core Audit hook. It's used to take the decision of
2731 * whether to audit or not to audit a given object.
2732 */
2733static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2734 struct audit_context *actx)
2735{
2736 char *smack;
2737 char *rule = vrule;
2738
2739 if (!rule) {
2740 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2741 "Smack: missing rule\n");
2742 return -ENOENT;
2743 }
2744
2745 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2746 return 0;
2747
2748 smack = smack_from_secid(secid);
2749
2750 /*
2751 * No need to do string comparisons. If a match occurs,
2752 * both pointers will point to the same smack_known
2753 * label.
2754 */
Al Viro5af75d82008-12-16 05:59:26 -05002755 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002756 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05002757 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002758 return (rule != smack);
2759
2760 return 0;
2761}
2762
2763/**
2764 * smack_audit_rule_free - free smack rule representation
2765 * @vrule: rule to be freed.
2766 *
2767 * No memory was allocated.
2768 */
2769static void smack_audit_rule_free(void *vrule)
2770{
2771 /* No-op */
2772}
2773
2774#endif /* CONFIG_AUDIT */
2775
Randy Dunlap251a2a92009-02-18 11:42:33 -08002776/**
Casey Schauflere114e472008-02-04 22:29:50 -08002777 * smack_secid_to_secctx - return the smack label for a secid
2778 * @secid: incoming integer
2779 * @secdata: destination
2780 * @seclen: how long it is
2781 *
2782 * Exists for networking code.
2783 */
2784static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2785{
2786 char *sp = smack_from_secid(secid);
2787
2788 *secdata = sp;
2789 *seclen = strlen(sp);
2790 return 0;
2791}
2792
Randy Dunlap251a2a92009-02-18 11:42:33 -08002793/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002794 * smack_secctx_to_secid - return the secid for a smack label
2795 * @secdata: smack label
2796 * @seclen: how long result is
2797 * @secid: outgoing integer
2798 *
2799 * Exists for audit and networking code.
2800 */
David Howellse52c17642008-04-29 20:52:51 +01002801static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002802{
2803 *secid = smack_to_secid(secdata);
2804 return 0;
2805}
2806
Randy Dunlap251a2a92009-02-18 11:42:33 -08002807/**
Casey Schauflere114e472008-02-04 22:29:50 -08002808 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08002809 * @secdata: unused
2810 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08002811 *
2812 * Exists to make sure nothing gets done, and properly
2813 */
2814static void smack_release_secctx(char *secdata, u32 seclen)
2815{
2816}
2817
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002818struct security_operations smack_ops = {
2819 .name = "smack",
2820
David Howells5cd9c582008-08-14 11:37:28 +01002821 .ptrace_may_access = smack_ptrace_may_access,
2822 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08002823 .capget = cap_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11002824 .capset = cap_capset,
Casey Schauflere114e472008-02-04 22:29:50 -08002825 .capable = cap_capable,
2826 .syslog = smack_syslog,
2827 .settime = cap_settime,
2828 .vm_enough_memory = cap_vm_enough_memory,
2829
David Howellsa6f76f22008-11-14 10:39:24 +11002830 .bprm_set_creds = cap_bprm_set_creds,
Casey Schauflere114e472008-02-04 22:29:50 -08002831 .bprm_secureexec = cap_bprm_secureexec,
2832
2833 .sb_alloc_security = smack_sb_alloc_security,
2834 .sb_free_security = smack_sb_free_security,
2835 .sb_copy_data = smack_sb_copy_data,
2836 .sb_kern_mount = smack_sb_kern_mount,
2837 .sb_statfs = smack_sb_statfs,
2838 .sb_mount = smack_sb_mount,
2839 .sb_umount = smack_sb_umount,
2840
2841 .inode_alloc_security = smack_inode_alloc_security,
2842 .inode_free_security = smack_inode_free_security,
2843 .inode_init_security = smack_inode_init_security,
2844 .inode_link = smack_inode_link,
2845 .inode_unlink = smack_inode_unlink,
2846 .inode_rmdir = smack_inode_rmdir,
2847 .inode_rename = smack_inode_rename,
2848 .inode_permission = smack_inode_permission,
2849 .inode_setattr = smack_inode_setattr,
2850 .inode_getattr = smack_inode_getattr,
2851 .inode_setxattr = smack_inode_setxattr,
2852 .inode_post_setxattr = smack_inode_post_setxattr,
2853 .inode_getxattr = smack_inode_getxattr,
2854 .inode_removexattr = smack_inode_removexattr,
Casey Schauflerbcdca222008-02-23 15:24:04 -08002855 .inode_need_killpriv = cap_inode_need_killpriv,
2856 .inode_killpriv = cap_inode_killpriv,
Casey Schauflere114e472008-02-04 22:29:50 -08002857 .inode_getsecurity = smack_inode_getsecurity,
2858 .inode_setsecurity = smack_inode_setsecurity,
2859 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002860 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08002861
2862 .file_permission = smack_file_permission,
2863 .file_alloc_security = smack_file_alloc_security,
2864 .file_free_security = smack_file_free_security,
2865 .file_ioctl = smack_file_ioctl,
2866 .file_lock = smack_file_lock,
2867 .file_fcntl = smack_file_fcntl,
2868 .file_set_fowner = smack_file_set_fowner,
2869 .file_send_sigiotask = smack_file_send_sigiotask,
2870 .file_receive = smack_file_receive,
2871
David Howellsf1752ee2008-11-14 10:39:17 +11002872 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11002873 .cred_prepare = smack_cred_prepare,
2874 .cred_commit = smack_cred_commit,
David Howells3a3b7ce2008-11-14 10:39:28 +11002875 .kernel_act_as = smack_kernel_act_as,
2876 .kernel_create_files_as = smack_kernel_create_files_as,
David Howellsd84f4f92008-11-14 10:39:23 +11002877 .task_fix_setuid = cap_task_fix_setuid,
Casey Schauflere114e472008-02-04 22:29:50 -08002878 .task_setpgid = smack_task_setpgid,
2879 .task_getpgid = smack_task_getpgid,
2880 .task_getsid = smack_task_getsid,
2881 .task_getsecid = smack_task_getsecid,
2882 .task_setnice = smack_task_setnice,
2883 .task_setioprio = smack_task_setioprio,
2884 .task_getioprio = smack_task_getioprio,
2885 .task_setscheduler = smack_task_setscheduler,
2886 .task_getscheduler = smack_task_getscheduler,
2887 .task_movememory = smack_task_movememory,
2888 .task_kill = smack_task_kill,
2889 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08002890 .task_to_inode = smack_task_to_inode,
Serge E. Hallyn1236cc32008-04-28 02:13:43 -07002891 .task_prctl = cap_task_prctl,
Casey Schauflere114e472008-02-04 22:29:50 -08002892
2893 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002894 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08002895
2896 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
2897 .msg_msg_free_security = smack_msg_msg_free_security,
2898
2899 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
2900 .msg_queue_free_security = smack_msg_queue_free_security,
2901 .msg_queue_associate = smack_msg_queue_associate,
2902 .msg_queue_msgctl = smack_msg_queue_msgctl,
2903 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
2904 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
2905
2906 .shm_alloc_security = smack_shm_alloc_security,
2907 .shm_free_security = smack_shm_free_security,
2908 .shm_associate = smack_shm_associate,
2909 .shm_shmctl = smack_shm_shmctl,
2910 .shm_shmat = smack_shm_shmat,
2911
2912 .sem_alloc_security = smack_sem_alloc_security,
2913 .sem_free_security = smack_sem_free_security,
2914 .sem_associate = smack_sem_associate,
2915 .sem_semctl = smack_sem_semctl,
2916 .sem_semop = smack_sem_semop,
2917
2918 .netlink_send = cap_netlink_send,
2919 .netlink_recv = cap_netlink_recv,
2920
2921 .d_instantiate = smack_d_instantiate,
2922
2923 .getprocattr = smack_getprocattr,
2924 .setprocattr = smack_setprocattr,
2925
2926 .unix_stream_connect = smack_unix_stream_connect,
2927 .unix_may_send = smack_unix_may_send,
2928
2929 .socket_post_create = smack_socket_post_create,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002930 .socket_connect = smack_socket_connect,
2931 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08002932 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
2933 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
2934 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
2935 .sk_alloc_security = smack_sk_alloc_security,
2936 .sk_free_security = smack_sk_free_security,
2937 .sock_graft = smack_sock_graft,
2938 .inet_conn_request = smack_inet_conn_request,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002939
Casey Schauflere114e472008-02-04 22:29:50 -08002940 /* key management security hooks */
2941#ifdef CONFIG_KEYS
2942 .key_alloc = smack_key_alloc,
2943 .key_free = smack_key_free,
2944 .key_permission = smack_key_permission,
2945#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002946
2947 /* Audit hooks */
2948#ifdef CONFIG_AUDIT
2949 .audit_rule_init = smack_audit_rule_init,
2950 .audit_rule_known = smack_audit_rule_known,
2951 .audit_rule_match = smack_audit_rule_match,
2952 .audit_rule_free = smack_audit_rule_free,
2953#endif /* CONFIG_AUDIT */
2954
Casey Schauflere114e472008-02-04 22:29:50 -08002955 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002956 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08002957 .release_secctx = smack_release_secctx,
2958};
2959
2960/**
2961 * smack_init - initialize the smack system
2962 *
2963 * Returns 0
2964 */
2965static __init int smack_init(void)
2966{
David Howellsd84f4f92008-11-14 10:39:23 +11002967 struct cred *cred;
2968
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002969 if (!security_module_enable(&smack_ops))
2970 return 0;
2971
Casey Schauflere114e472008-02-04 22:29:50 -08002972 printk(KERN_INFO "Smack: Initializing.\n");
2973
2974 /*
2975 * Set the security state for the initial task.
2976 */
David Howellsd84f4f92008-11-14 10:39:23 +11002977 cred = (struct cred *) current->cred;
2978 cred->security = &smack_known_floor.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002979
2980 /*
2981 * Initialize locks
2982 */
Casey Schauflere114e472008-02-04 22:29:50 -08002983 spin_lock_init(&smack_known_huh.smk_cipsolock);
2984 spin_lock_init(&smack_known_hat.smk_cipsolock);
2985 spin_lock_init(&smack_known_star.smk_cipsolock);
2986 spin_lock_init(&smack_known_floor.smk_cipsolock);
2987 spin_lock_init(&smack_known_invalid.smk_cipsolock);
2988
2989 /*
2990 * Register with LSM
2991 */
2992 if (register_security(&smack_ops))
2993 panic("smack: Unable to register with kernel.\n");
2994
2995 return 0;
2996}
2997
2998/*
2999 * Smack requires early initialization in order to label
3000 * all processes and objects when they are created.
3001 */
3002security_initcall(smack_init);