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