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