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