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