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