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