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