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