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