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