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