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