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