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