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