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