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