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