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