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