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