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