xref: /openbmc/linux/security/smack/smack_lsm.c (revision 82ced6fd)
1 /*
2  *  Simplified MAC Kernel (smack) security module
3  *
4  *  This file contains the smack hook function implementations.
5  *
6  *  Author:
7  *	Casey Schaufler <casey@schaufler-ca.com>
8  *
9  *  Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
10  *  Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
11  *                Paul Moore <paul.moore@hp.com>
12  *
13  *	This program is free software; you can redistribute it and/or modify
14  *	it under the terms of the GNU General Public License version 2,
15  *      as published by the Free Software Foundation.
16  */
17 
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
22 #include <linux/ext2_fs.h>
23 #include <linux/kd.h>
24 #include <asm/ioctls.h>
25 #include <linux/ip.h>
26 #include <linux/tcp.h>
27 #include <linux/udp.h>
28 #include <linux/mutex.h>
29 #include <linux/pipe_fs_i.h>
30 #include <net/netlabel.h>
31 #include <net/cipso_ipv4.h>
32 #include <linux/audit.h>
33 
34 #include "smack.h"
35 
36 #define task_security(task)	(task_cred_xxx((task), security))
37 
38 /*
39  * I hope these are the hokeyist lines of code in the module. Casey.
40  */
41 #define DEVPTS_SUPER_MAGIC	0x1cd1
42 #define SOCKFS_MAGIC		0x534F434B
43 #define TMPFS_MAGIC		0x01021994
44 
45 /**
46  * smk_fetch - Fetch the smack label from a file.
47  * @ip: a pointer to the inode
48  * @dp: a pointer to the dentry
49  *
50  * Returns a pointer to the master list entry for the Smack label
51  * or NULL if there was no label to fetch.
52  */
53 static char *smk_fetch(struct inode *ip, struct dentry *dp)
54 {
55 	int rc;
56 	char in[SMK_LABELLEN];
57 
58 	if (ip->i_op->getxattr == NULL)
59 		return NULL;
60 
61 	rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
62 	if (rc < 0)
63 		return NULL;
64 
65 	return smk_import(in, rc);
66 }
67 
68 /**
69  * new_inode_smack - allocate an inode security blob
70  * @smack: a pointer to the Smack label to use in the blob
71  *
72  * Returns the new blob or NULL if there's no memory available
73  */
74 struct inode_smack *new_inode_smack(char *smack)
75 {
76 	struct inode_smack *isp;
77 
78 	isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
79 	if (isp == NULL)
80 		return NULL;
81 
82 	isp->smk_inode = smack;
83 	isp->smk_flags = 0;
84 	mutex_init(&isp->smk_lock);
85 
86 	return isp;
87 }
88 
89 /*
90  * LSM hooks.
91  * We he, that is fun!
92  */
93 
94 /**
95  * smack_ptrace_may_access - Smack approval on PTRACE_ATTACH
96  * @ctp: child task pointer
97  * @mode: ptrace attachment mode
98  *
99  * Returns 0 if access is OK, an error code otherwise
100  *
101  * Do the capability checks, and require read and write.
102  */
103 static int smack_ptrace_may_access(struct task_struct *ctp, unsigned int mode)
104 {
105 	int rc;
106 
107 	rc = cap_ptrace_may_access(ctp, mode);
108 	if (rc != 0)
109 		return rc;
110 
111 	rc = smk_access(current_security(), task_security(ctp), MAY_READWRITE);
112 	if (rc != 0 && capable(CAP_MAC_OVERRIDE))
113 		return 0;
114 	return rc;
115 }
116 
117 /**
118  * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
119  * @ptp: parent task pointer
120  *
121  * Returns 0 if access is OK, an error code otherwise
122  *
123  * Do the capability checks, and require read and write.
124  */
125 static int smack_ptrace_traceme(struct task_struct *ptp)
126 {
127 	int rc;
128 
129 	rc = cap_ptrace_traceme(ptp);
130 	if (rc != 0)
131 		return rc;
132 
133 	rc = smk_access(task_security(ptp), current_security(), MAY_READWRITE);
134 	if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
135 		return 0;
136 	return rc;
137 }
138 
139 /**
140  * smack_syslog - Smack approval on syslog
141  * @type: message type
142  *
143  * Require that the task has the floor label
144  *
145  * Returns 0 on success, error code otherwise.
146  */
147 static int smack_syslog(int type)
148 {
149 	int rc;
150 	char *sp = current_security();
151 
152 	rc = cap_syslog(type);
153 	if (rc != 0)
154 		return rc;
155 
156 	if (capable(CAP_MAC_OVERRIDE))
157 		return 0;
158 
159 	 if (sp != smack_known_floor.smk_known)
160 		rc = -EACCES;
161 
162 	return rc;
163 }
164 
165 
166 /*
167  * Superblock Hooks.
168  */
169 
170 /**
171  * smack_sb_alloc_security - allocate a superblock blob
172  * @sb: the superblock getting the blob
173  *
174  * Returns 0 on success or -ENOMEM on error.
175  */
176 static int smack_sb_alloc_security(struct super_block *sb)
177 {
178 	struct superblock_smack *sbsp;
179 
180 	sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
181 
182 	if (sbsp == NULL)
183 		return -ENOMEM;
184 
185 	sbsp->smk_root = smack_known_floor.smk_known;
186 	sbsp->smk_default = smack_known_floor.smk_known;
187 	sbsp->smk_floor = smack_known_floor.smk_known;
188 	sbsp->smk_hat = smack_known_hat.smk_known;
189 	sbsp->smk_initialized = 0;
190 	spin_lock_init(&sbsp->smk_sblock);
191 
192 	sb->s_security = sbsp;
193 
194 	return 0;
195 }
196 
197 /**
198  * smack_sb_free_security - free a superblock blob
199  * @sb: the superblock getting the blob
200  *
201  */
202 static void smack_sb_free_security(struct super_block *sb)
203 {
204 	kfree(sb->s_security);
205 	sb->s_security = NULL;
206 }
207 
208 /**
209  * smack_sb_copy_data - copy mount options data for processing
210  * @orig: where to start
211  * @smackopts: mount options string
212  *
213  * Returns 0 on success or -ENOMEM on error.
214  *
215  * Copy the Smack specific mount options out of the mount
216  * options list.
217  */
218 static int smack_sb_copy_data(char *orig, char *smackopts)
219 {
220 	char *cp, *commap, *otheropts, *dp;
221 
222 	otheropts = (char *)get_zeroed_page(GFP_KERNEL);
223 	if (otheropts == NULL)
224 		return -ENOMEM;
225 
226 	for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
227 		if (strstr(cp, SMK_FSDEFAULT) == cp)
228 			dp = smackopts;
229 		else if (strstr(cp, SMK_FSFLOOR) == cp)
230 			dp = smackopts;
231 		else if (strstr(cp, SMK_FSHAT) == cp)
232 			dp = smackopts;
233 		else if (strstr(cp, SMK_FSROOT) == cp)
234 			dp = smackopts;
235 		else
236 			dp = otheropts;
237 
238 		commap = strchr(cp, ',');
239 		if (commap != NULL)
240 			*commap = '\0';
241 
242 		if (*dp != '\0')
243 			strcat(dp, ",");
244 		strcat(dp, cp);
245 	}
246 
247 	strcpy(orig, otheropts);
248 	free_page((unsigned long)otheropts);
249 
250 	return 0;
251 }
252 
253 /**
254  * smack_sb_kern_mount - Smack specific mount processing
255  * @sb: the file system superblock
256  * @flags: the mount flags
257  * @data: the smack mount options
258  *
259  * Returns 0 on success, an error code on failure
260  */
261 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
262 {
263 	struct dentry *root = sb->s_root;
264 	struct inode *inode = root->d_inode;
265 	struct superblock_smack *sp = sb->s_security;
266 	struct inode_smack *isp;
267 	char *op;
268 	char *commap;
269 	char *nsp;
270 
271 	spin_lock(&sp->smk_sblock);
272 	if (sp->smk_initialized != 0) {
273 		spin_unlock(&sp->smk_sblock);
274 		return 0;
275 	}
276 	sp->smk_initialized = 1;
277 	spin_unlock(&sp->smk_sblock);
278 
279 	for (op = data; op != NULL; op = commap) {
280 		commap = strchr(op, ',');
281 		if (commap != NULL)
282 			*commap++ = '\0';
283 
284 		if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
285 			op += strlen(SMK_FSHAT);
286 			nsp = smk_import(op, 0);
287 			if (nsp != NULL)
288 				sp->smk_hat = nsp;
289 		} else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
290 			op += strlen(SMK_FSFLOOR);
291 			nsp = smk_import(op, 0);
292 			if (nsp != NULL)
293 				sp->smk_floor = nsp;
294 		} else if (strncmp(op, SMK_FSDEFAULT,
295 				   strlen(SMK_FSDEFAULT)) == 0) {
296 			op += strlen(SMK_FSDEFAULT);
297 			nsp = smk_import(op, 0);
298 			if (nsp != NULL)
299 				sp->smk_default = nsp;
300 		} else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
301 			op += strlen(SMK_FSROOT);
302 			nsp = smk_import(op, 0);
303 			if (nsp != NULL)
304 				sp->smk_root = nsp;
305 		}
306 	}
307 
308 	/*
309 	 * Initialize the root inode.
310 	 */
311 	isp = inode->i_security;
312 	if (isp == NULL)
313 		inode->i_security = new_inode_smack(sp->smk_root);
314 	else
315 		isp->smk_inode = sp->smk_root;
316 
317 	return 0;
318 }
319 
320 /**
321  * smack_sb_statfs - Smack check on statfs
322  * @dentry: identifies the file system in question
323  *
324  * Returns 0 if current can read the floor of the filesystem,
325  * and error code otherwise
326  */
327 static int smack_sb_statfs(struct dentry *dentry)
328 {
329 	struct superblock_smack *sbp = dentry->d_sb->s_security;
330 
331 	return smk_curacc(sbp->smk_floor, MAY_READ);
332 }
333 
334 /**
335  * smack_sb_mount - Smack check for mounting
336  * @dev_name: unused
337  * @path: mount point
338  * @type: unused
339  * @flags: unused
340  * @data: unused
341  *
342  * Returns 0 if current can write the floor of the filesystem
343  * being mounted on, an error code otherwise.
344  */
345 static int smack_sb_mount(char *dev_name, struct path *path,
346 			  char *type, unsigned long flags, void *data)
347 {
348 	struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
349 
350 	return smk_curacc(sbp->smk_floor, MAY_WRITE);
351 }
352 
353 /**
354  * smack_sb_umount - Smack check for unmounting
355  * @mnt: file system to unmount
356  * @flags: unused
357  *
358  * Returns 0 if current can write the floor of the filesystem
359  * being unmounted, an error code otherwise.
360  */
361 static int smack_sb_umount(struct vfsmount *mnt, int flags)
362 {
363 	struct superblock_smack *sbp;
364 
365 	sbp = mnt->mnt_sb->s_security;
366 
367 	return smk_curacc(sbp->smk_floor, MAY_WRITE);
368 }
369 
370 /*
371  * Inode hooks
372  */
373 
374 /**
375  * smack_inode_alloc_security - allocate an inode blob
376  * @inode: the inode in need of a blob
377  *
378  * Returns 0 if it gets a blob, -ENOMEM otherwise
379  */
380 static int smack_inode_alloc_security(struct inode *inode)
381 {
382 	inode->i_security = new_inode_smack(current_security());
383 	if (inode->i_security == NULL)
384 		return -ENOMEM;
385 	return 0;
386 }
387 
388 /**
389  * smack_inode_free_security - free an inode blob
390  * @inode: the inode with a blob
391  *
392  * Clears the blob pointer in inode
393  */
394 static void smack_inode_free_security(struct inode *inode)
395 {
396 	kfree(inode->i_security);
397 	inode->i_security = NULL;
398 }
399 
400 /**
401  * smack_inode_init_security - copy out the smack from an inode
402  * @inode: the inode
403  * @dir: unused
404  * @name: where to put the attribute name
405  * @value: where to put the attribute value
406  * @len: where to put the length of the attribute
407  *
408  * Returns 0 if it all works out, -ENOMEM if there's no memory
409  */
410 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
411 				     char **name, void **value, size_t *len)
412 {
413 	char *isp = smk_of_inode(inode);
414 
415 	if (name) {
416 		*name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
417 		if (*name == NULL)
418 			return -ENOMEM;
419 	}
420 
421 	if (value) {
422 		*value = kstrdup(isp, GFP_KERNEL);
423 		if (*value == NULL)
424 			return -ENOMEM;
425 	}
426 
427 	if (len)
428 		*len = strlen(isp) + 1;
429 
430 	return 0;
431 }
432 
433 /**
434  * smack_inode_link - Smack check on link
435  * @old_dentry: the existing object
436  * @dir: unused
437  * @new_dentry: the new object
438  *
439  * Returns 0 if access is permitted, an error code otherwise
440  */
441 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
442 			    struct dentry *new_dentry)
443 {
444 	int rc;
445 	char *isp;
446 
447 	isp = smk_of_inode(old_dentry->d_inode);
448 	rc = smk_curacc(isp, MAY_WRITE);
449 
450 	if (rc == 0 && new_dentry->d_inode != NULL) {
451 		isp = smk_of_inode(new_dentry->d_inode);
452 		rc = smk_curacc(isp, MAY_WRITE);
453 	}
454 
455 	return rc;
456 }
457 
458 /**
459  * smack_inode_unlink - Smack check on inode deletion
460  * @dir: containing directory object
461  * @dentry: file to unlink
462  *
463  * Returns 0 if current can write the containing directory
464  * and the object, error code otherwise
465  */
466 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
467 {
468 	struct inode *ip = dentry->d_inode;
469 	int rc;
470 
471 	/*
472 	 * You need write access to the thing you're unlinking
473 	 */
474 	rc = smk_curacc(smk_of_inode(ip), MAY_WRITE);
475 	if (rc == 0)
476 		/*
477 		 * You also need write access to the containing directory
478 		 */
479 		rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
480 
481 	return rc;
482 }
483 
484 /**
485  * smack_inode_rmdir - Smack check on directory deletion
486  * @dir: containing directory object
487  * @dentry: directory to unlink
488  *
489  * Returns 0 if current can write the containing directory
490  * and the directory, error code otherwise
491  */
492 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
493 {
494 	int rc;
495 
496 	/*
497 	 * You need write access to the thing you're removing
498 	 */
499 	rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
500 	if (rc == 0)
501 		/*
502 		 * You also need write access to the containing directory
503 		 */
504 		rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
505 
506 	return rc;
507 }
508 
509 /**
510  * smack_inode_rename - Smack check on rename
511  * @old_inode: the old directory
512  * @old_dentry: unused
513  * @new_inode: the new directory
514  * @new_dentry: unused
515  *
516  * Read and write access is required on both the old and
517  * new directories.
518  *
519  * Returns 0 if access is permitted, an error code otherwise
520  */
521 static int smack_inode_rename(struct inode *old_inode,
522 			      struct dentry *old_dentry,
523 			      struct inode *new_inode,
524 			      struct dentry *new_dentry)
525 {
526 	int rc;
527 	char *isp;
528 
529 	isp = smk_of_inode(old_dentry->d_inode);
530 	rc = smk_curacc(isp, MAY_READWRITE);
531 
532 	if (rc == 0 && new_dentry->d_inode != NULL) {
533 		isp = smk_of_inode(new_dentry->d_inode);
534 		rc = smk_curacc(isp, MAY_READWRITE);
535 	}
536 
537 	return rc;
538 }
539 
540 /**
541  * smack_inode_permission - Smack version of permission()
542  * @inode: the inode in question
543  * @mask: the access requested
544  *
545  * This is the important Smack hook.
546  *
547  * Returns 0 if access is permitted, -EACCES otherwise
548  */
549 static int smack_inode_permission(struct inode *inode, int mask)
550 {
551 	/*
552 	 * No permission to check. Existence test. Yup, it's there.
553 	 */
554 	if (mask == 0)
555 		return 0;
556 
557 	return smk_curacc(smk_of_inode(inode), mask);
558 }
559 
560 /**
561  * smack_inode_setattr - Smack check for setting attributes
562  * @dentry: the object
563  * @iattr: for the force flag
564  *
565  * Returns 0 if access is permitted, an error code otherwise
566  */
567 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
568 {
569 	/*
570 	 * Need to allow for clearing the setuid bit.
571 	 */
572 	if (iattr->ia_valid & ATTR_FORCE)
573 		return 0;
574 
575 	return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
576 }
577 
578 /**
579  * smack_inode_getattr - Smack check for getting attributes
580  * @mnt: unused
581  * @dentry: the object
582  *
583  * Returns 0 if access is permitted, an error code otherwise
584  */
585 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
586 {
587 	return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
588 }
589 
590 /**
591  * smack_inode_setxattr - Smack check for setting xattrs
592  * @dentry: the object
593  * @name: name of the attribute
594  * @value: unused
595  * @size: unused
596  * @flags: unused
597  *
598  * This protects the Smack attribute explicitly.
599  *
600  * Returns 0 if access is permitted, an error code otherwise
601  */
602 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
603 				const void *value, size_t size, int flags)
604 {
605 	int rc = 0;
606 
607 	if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
608 	    strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
609 	    strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
610 		if (!capable(CAP_MAC_ADMIN))
611 			rc = -EPERM;
612 		/*
613 		 * check label validity here so import wont fail on
614 		 * post_setxattr
615 		 */
616 		if (size == 0 || size >= SMK_LABELLEN ||
617 		    smk_import(value, size) == NULL)
618 			rc = -EINVAL;
619 	} else
620 		rc = cap_inode_setxattr(dentry, name, value, size, flags);
621 
622 	if (rc == 0)
623 		rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
624 
625 	return rc;
626 }
627 
628 /**
629  * smack_inode_post_setxattr - Apply the Smack update approved above
630  * @dentry: object
631  * @name: attribute name
632  * @value: attribute value
633  * @size: attribute size
634  * @flags: unused
635  *
636  * Set the pointer in the inode blob to the entry found
637  * in the master label list.
638  */
639 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
640 				      const void *value, size_t size, int flags)
641 {
642 	struct inode_smack *isp;
643 	char *nsp;
644 
645 	/*
646 	 * Not SMACK
647 	 */
648 	if (strcmp(name, XATTR_NAME_SMACK))
649 		return;
650 
651 	isp = dentry->d_inode->i_security;
652 
653 	/*
654 	 * No locking is done here. This is a pointer
655 	 * assignment.
656 	 */
657 	nsp = smk_import(value, size);
658 	if (nsp != NULL)
659 		isp->smk_inode = nsp;
660 	else
661 		isp->smk_inode = smack_known_invalid.smk_known;
662 
663 	return;
664 }
665 
666 /*
667  * smack_inode_getxattr - Smack check on getxattr
668  * @dentry: the object
669  * @name: unused
670  *
671  * Returns 0 if access is permitted, an error code otherwise
672  */
673 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
674 {
675 	return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
676 }
677 
678 /*
679  * smack_inode_removexattr - Smack check on removexattr
680  * @dentry: the object
681  * @name: name of the attribute
682  *
683  * Removing the Smack attribute requires CAP_MAC_ADMIN
684  *
685  * Returns 0 if access is permitted, an error code otherwise
686  */
687 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
688 {
689 	int rc = 0;
690 
691 	if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
692 	    strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
693 	    strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
694 		if (!capable(CAP_MAC_ADMIN))
695 			rc = -EPERM;
696 	} else
697 		rc = cap_inode_removexattr(dentry, name);
698 
699 	if (rc == 0)
700 		rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
701 
702 	return rc;
703 }
704 
705 /**
706  * smack_inode_getsecurity - get smack xattrs
707  * @inode: the object
708  * @name: attribute name
709  * @buffer: where to put the result
710  * @alloc: unused
711  *
712  * Returns the size of the attribute or an error code
713  */
714 static int smack_inode_getsecurity(const struct inode *inode,
715 				   const char *name, void **buffer,
716 				   bool alloc)
717 {
718 	struct socket_smack *ssp;
719 	struct socket *sock;
720 	struct super_block *sbp;
721 	struct inode *ip = (struct inode *)inode;
722 	char *isp;
723 	int ilen;
724 	int rc = 0;
725 
726 	if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
727 		isp = smk_of_inode(inode);
728 		ilen = strlen(isp) + 1;
729 		*buffer = isp;
730 		return ilen;
731 	}
732 
733 	/*
734 	 * The rest of the Smack xattrs are only on sockets.
735 	 */
736 	sbp = ip->i_sb;
737 	if (sbp->s_magic != SOCKFS_MAGIC)
738 		return -EOPNOTSUPP;
739 
740 	sock = SOCKET_I(ip);
741 	if (sock == NULL || sock->sk == NULL)
742 		return -EOPNOTSUPP;
743 
744 	ssp = sock->sk->sk_security;
745 
746 	if (strcmp(name, XATTR_SMACK_IPIN) == 0)
747 		isp = ssp->smk_in;
748 	else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
749 		isp = ssp->smk_out;
750 	else
751 		return -EOPNOTSUPP;
752 
753 	ilen = strlen(isp) + 1;
754 	if (rc == 0) {
755 		*buffer = isp;
756 		rc = ilen;
757 	}
758 
759 	return rc;
760 }
761 
762 
763 /**
764  * smack_inode_listsecurity - list the Smack attributes
765  * @inode: the object
766  * @buffer: where they go
767  * @buffer_size: size of buffer
768  *
769  * Returns 0 on success, -EINVAL otherwise
770  */
771 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
772 				    size_t buffer_size)
773 {
774 	int len = strlen(XATTR_NAME_SMACK);
775 
776 	if (buffer != NULL && len <= buffer_size) {
777 		memcpy(buffer, XATTR_NAME_SMACK, len);
778 		return len;
779 	}
780 	return -EINVAL;
781 }
782 
783 /**
784  * smack_inode_getsecid - Extract inode's security id
785  * @inode: inode to extract the info from
786  * @secid: where result will be saved
787  */
788 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
789 {
790 	struct inode_smack *isp = inode->i_security;
791 
792 	*secid = smack_to_secid(isp->smk_inode);
793 }
794 
795 /*
796  * File Hooks
797  */
798 
799 /**
800  * smack_file_permission - Smack check on file operations
801  * @file: unused
802  * @mask: unused
803  *
804  * Returns 0
805  *
806  * Should access checks be done on each read or write?
807  * UNICOS and SELinux say yes.
808  * Trusted Solaris, Trusted Irix, and just about everyone else says no.
809  *
810  * I'll say no for now. Smack does not do the frequent
811  * label changing that SELinux does.
812  */
813 static int smack_file_permission(struct file *file, int mask)
814 {
815 	return 0;
816 }
817 
818 /**
819  * smack_file_alloc_security - assign a file security blob
820  * @file: the object
821  *
822  * The security blob for a file is a pointer to the master
823  * label list, so no allocation is done.
824  *
825  * Returns 0
826  */
827 static int smack_file_alloc_security(struct file *file)
828 {
829 	file->f_security = current_security();
830 	return 0;
831 }
832 
833 /**
834  * smack_file_free_security - clear a file security blob
835  * @file: the object
836  *
837  * The security blob for a file is a pointer to the master
838  * label list, so no memory is freed.
839  */
840 static void smack_file_free_security(struct file *file)
841 {
842 	file->f_security = NULL;
843 }
844 
845 /**
846  * smack_file_ioctl - Smack check on ioctls
847  * @file: the object
848  * @cmd: what to do
849  * @arg: unused
850  *
851  * Relies heavily on the correct use of the ioctl command conventions.
852  *
853  * Returns 0 if allowed, error code otherwise
854  */
855 static int smack_file_ioctl(struct file *file, unsigned int cmd,
856 			    unsigned long arg)
857 {
858 	int rc = 0;
859 
860 	if (_IOC_DIR(cmd) & _IOC_WRITE)
861 		rc = smk_curacc(file->f_security, MAY_WRITE);
862 
863 	if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
864 		rc = smk_curacc(file->f_security, MAY_READ);
865 
866 	return rc;
867 }
868 
869 /**
870  * smack_file_lock - Smack check on file locking
871  * @file: the object
872  * @cmd: unused
873  *
874  * Returns 0 if current has write access, error code otherwise
875  */
876 static int smack_file_lock(struct file *file, unsigned int cmd)
877 {
878 	return smk_curacc(file->f_security, MAY_WRITE);
879 }
880 
881 /**
882  * smack_file_fcntl - Smack check on fcntl
883  * @file: the object
884  * @cmd: what action to check
885  * @arg: unused
886  *
887  * Returns 0 if current has access, error code otherwise
888  */
889 static int smack_file_fcntl(struct file *file, unsigned int cmd,
890 			    unsigned long arg)
891 {
892 	int rc;
893 
894 	switch (cmd) {
895 	case F_DUPFD:
896 	case F_GETFD:
897 	case F_GETFL:
898 	case F_GETLK:
899 	case F_GETOWN:
900 	case F_GETSIG:
901 		rc = smk_curacc(file->f_security, MAY_READ);
902 		break;
903 	case F_SETFD:
904 	case F_SETFL:
905 	case F_SETLK:
906 	case F_SETLKW:
907 	case F_SETOWN:
908 	case F_SETSIG:
909 		rc = smk_curacc(file->f_security, MAY_WRITE);
910 		break;
911 	default:
912 		rc = smk_curacc(file->f_security, MAY_READWRITE);
913 	}
914 
915 	return rc;
916 }
917 
918 /**
919  * smack_file_set_fowner - set the file security blob value
920  * @file: object in question
921  *
922  * Returns 0
923  * Further research may be required on this one.
924  */
925 static int smack_file_set_fowner(struct file *file)
926 {
927 	file->f_security = current_security();
928 	return 0;
929 }
930 
931 /**
932  * smack_file_send_sigiotask - Smack on sigio
933  * @tsk: The target task
934  * @fown: the object the signal come from
935  * @signum: unused
936  *
937  * Allow a privileged task to get signals even if it shouldn't
938  *
939  * Returns 0 if a subject with the object's smack could
940  * write to the task, an error code otherwise.
941  */
942 static int smack_file_send_sigiotask(struct task_struct *tsk,
943 				     struct fown_struct *fown, int signum)
944 {
945 	struct file *file;
946 	int rc;
947 
948 	/*
949 	 * struct fown_struct is never outside the context of a struct file
950 	 */
951 	file = container_of(fown, struct file, f_owner);
952 	rc = smk_access(file->f_security, tsk->cred->security, MAY_WRITE);
953 	if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
954 		return 0;
955 	return rc;
956 }
957 
958 /**
959  * smack_file_receive - Smack file receive check
960  * @file: the object
961  *
962  * Returns 0 if current has access, error code otherwise
963  */
964 static int smack_file_receive(struct file *file)
965 {
966 	int may = 0;
967 
968 	/*
969 	 * This code relies on bitmasks.
970 	 */
971 	if (file->f_mode & FMODE_READ)
972 		may = MAY_READ;
973 	if (file->f_mode & FMODE_WRITE)
974 		may |= MAY_WRITE;
975 
976 	return smk_curacc(file->f_security, may);
977 }
978 
979 /*
980  * Task hooks
981  */
982 
983 /**
984  * smack_cred_free - "free" task-level security credentials
985  * @cred: the credentials in question
986  *
987  * Smack isn't using copies of blobs. Everyone
988  * points to an immutable list. The blobs never go away.
989  * There is no leak here.
990  */
991 static void smack_cred_free(struct cred *cred)
992 {
993 	cred->security = NULL;
994 }
995 
996 /**
997  * smack_cred_prepare - prepare new set of credentials for modification
998  * @new: the new credentials
999  * @old: the original credentials
1000  * @gfp: the atomicity of any memory allocations
1001  *
1002  * Prepare a new set of credentials for modification.
1003  */
1004 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1005 			      gfp_t gfp)
1006 {
1007 	new->security = old->security;
1008 	return 0;
1009 }
1010 
1011 /**
1012  * smack_cred_commit - commit new credentials
1013  * @new: the new credentials
1014  * @old: the original credentials
1015  */
1016 static void smack_cred_commit(struct cred *new, const struct cred *old)
1017 {
1018 }
1019 
1020 /**
1021  * smack_kernel_act_as - Set the subjective context in a set of credentials
1022  * @new: points to the set of credentials to be modified.
1023  * @secid: specifies the security ID to be set
1024  *
1025  * Set the security data for a kernel service.
1026  */
1027 static int smack_kernel_act_as(struct cred *new, u32 secid)
1028 {
1029 	char *smack = smack_from_secid(secid);
1030 
1031 	if (smack == NULL)
1032 		return -EINVAL;
1033 
1034 	new->security = smack;
1035 	return 0;
1036 }
1037 
1038 /**
1039  * smack_kernel_create_files_as - Set the file creation label in a set of creds
1040  * @new: points to the set of credentials to be modified
1041  * @inode: points to the inode to use as a reference
1042  *
1043  * Set the file creation context in a set of credentials to the same
1044  * as the objective context of the specified inode
1045  */
1046 static int smack_kernel_create_files_as(struct cred *new,
1047 					struct inode *inode)
1048 {
1049 	struct inode_smack *isp = inode->i_security;
1050 
1051 	new->security = isp->smk_inode;
1052 	return 0;
1053 }
1054 
1055 /**
1056  * smack_task_setpgid - Smack check on setting pgid
1057  * @p: the task object
1058  * @pgid: unused
1059  *
1060  * Return 0 if write access is permitted
1061  */
1062 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1063 {
1064 	return smk_curacc(task_security(p), MAY_WRITE);
1065 }
1066 
1067 /**
1068  * smack_task_getpgid - Smack access check for getpgid
1069  * @p: the object task
1070  *
1071  * Returns 0 if current can read the object task, error code otherwise
1072  */
1073 static int smack_task_getpgid(struct task_struct *p)
1074 {
1075 	return smk_curacc(task_security(p), MAY_READ);
1076 }
1077 
1078 /**
1079  * smack_task_getsid - Smack access check for getsid
1080  * @p: the object task
1081  *
1082  * Returns 0 if current can read the object task, error code otherwise
1083  */
1084 static int smack_task_getsid(struct task_struct *p)
1085 {
1086 	return smk_curacc(task_security(p), MAY_READ);
1087 }
1088 
1089 /**
1090  * smack_task_getsecid - get the secid of the task
1091  * @p: the object task
1092  * @secid: where to put the result
1093  *
1094  * Sets the secid to contain a u32 version of the smack label.
1095  */
1096 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1097 {
1098 	*secid = smack_to_secid(task_security(p));
1099 }
1100 
1101 /**
1102  * smack_task_setnice - Smack check on setting nice
1103  * @p: the task object
1104  * @nice: unused
1105  *
1106  * Return 0 if write access is permitted
1107  */
1108 static int smack_task_setnice(struct task_struct *p, int nice)
1109 {
1110 	int rc;
1111 
1112 	rc = cap_task_setnice(p, nice);
1113 	if (rc == 0)
1114 		rc = smk_curacc(task_security(p), MAY_WRITE);
1115 	return rc;
1116 }
1117 
1118 /**
1119  * smack_task_setioprio - Smack check on setting ioprio
1120  * @p: the task object
1121  * @ioprio: unused
1122  *
1123  * Return 0 if write access is permitted
1124  */
1125 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1126 {
1127 	int rc;
1128 
1129 	rc = cap_task_setioprio(p, ioprio);
1130 	if (rc == 0)
1131 		rc = smk_curacc(task_security(p), MAY_WRITE);
1132 	return rc;
1133 }
1134 
1135 /**
1136  * smack_task_getioprio - Smack check on reading ioprio
1137  * @p: the task object
1138  *
1139  * Return 0 if read access is permitted
1140  */
1141 static int smack_task_getioprio(struct task_struct *p)
1142 {
1143 	return smk_curacc(task_security(p), MAY_READ);
1144 }
1145 
1146 /**
1147  * smack_task_setscheduler - Smack check on setting scheduler
1148  * @p: the task object
1149  * @policy: unused
1150  * @lp: unused
1151  *
1152  * Return 0 if read access is permitted
1153  */
1154 static int smack_task_setscheduler(struct task_struct *p, int policy,
1155 				   struct sched_param *lp)
1156 {
1157 	int rc;
1158 
1159 	rc = cap_task_setscheduler(p, policy, lp);
1160 	if (rc == 0)
1161 		rc = smk_curacc(task_security(p), MAY_WRITE);
1162 	return rc;
1163 }
1164 
1165 /**
1166  * smack_task_getscheduler - Smack check on reading scheduler
1167  * @p: the task object
1168  *
1169  * Return 0 if read access is permitted
1170  */
1171 static int smack_task_getscheduler(struct task_struct *p)
1172 {
1173 	return smk_curacc(task_security(p), MAY_READ);
1174 }
1175 
1176 /**
1177  * smack_task_movememory - Smack check on moving memory
1178  * @p: the task object
1179  *
1180  * Return 0 if write access is permitted
1181  */
1182 static int smack_task_movememory(struct task_struct *p)
1183 {
1184 	return smk_curacc(task_security(p), MAY_WRITE);
1185 }
1186 
1187 /**
1188  * smack_task_kill - Smack check on signal delivery
1189  * @p: the task object
1190  * @info: unused
1191  * @sig: unused
1192  * @secid: identifies the smack to use in lieu of current's
1193  *
1194  * Return 0 if write access is permitted
1195  *
1196  * The secid behavior is an artifact of an SELinux hack
1197  * in the USB code. Someday it may go away.
1198  */
1199 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1200 			   int sig, u32 secid)
1201 {
1202 	/*
1203 	 * Sending a signal requires that the sender
1204 	 * can write the receiver.
1205 	 */
1206 	if (secid == 0)
1207 		return smk_curacc(task_security(p), MAY_WRITE);
1208 	/*
1209 	 * If the secid isn't 0 we're dealing with some USB IO
1210 	 * specific behavior. This is not clean. For one thing
1211 	 * we can't take privilege into account.
1212 	 */
1213 	return smk_access(smack_from_secid(secid), task_security(p), MAY_WRITE);
1214 }
1215 
1216 /**
1217  * smack_task_wait - Smack access check for waiting
1218  * @p: task to wait for
1219  *
1220  * Returns 0 if current can wait for p, error code otherwise
1221  */
1222 static int smack_task_wait(struct task_struct *p)
1223 {
1224 	int rc;
1225 
1226 	rc = smk_access(current_security(), task_security(p), MAY_WRITE);
1227 	if (rc == 0)
1228 		return 0;
1229 
1230 	/*
1231 	 * Allow the operation to succeed if either task
1232 	 * has privilege to perform operations that might
1233 	 * account for the smack labels having gotten to
1234 	 * be different in the first place.
1235 	 *
1236 	 * This breaks the strict subject/object access
1237 	 * control ideal, taking the object's privilege
1238 	 * state into account in the decision as well as
1239 	 * the smack value.
1240 	 */
1241 	if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1242 		return 0;
1243 
1244 	return rc;
1245 }
1246 
1247 /**
1248  * smack_task_to_inode - copy task smack into the inode blob
1249  * @p: task to copy from
1250  * @inode: inode to copy to
1251  *
1252  * Sets the smack pointer in the inode security blob
1253  */
1254 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1255 {
1256 	struct inode_smack *isp = inode->i_security;
1257 	isp->smk_inode = task_security(p);
1258 }
1259 
1260 /*
1261  * Socket hooks.
1262  */
1263 
1264 /**
1265  * smack_sk_alloc_security - Allocate a socket blob
1266  * @sk: the socket
1267  * @family: unused
1268  * @gfp_flags: memory allocation flags
1269  *
1270  * Assign Smack pointers to current
1271  *
1272  * Returns 0 on success, -ENOMEM is there's no memory
1273  */
1274 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1275 {
1276 	char *csp = current_security();
1277 	struct socket_smack *ssp;
1278 
1279 	ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1280 	if (ssp == NULL)
1281 		return -ENOMEM;
1282 
1283 	ssp->smk_in = csp;
1284 	ssp->smk_out = csp;
1285 	ssp->smk_packet[0] = '\0';
1286 
1287 	sk->sk_security = ssp;
1288 
1289 	return 0;
1290 }
1291 
1292 /**
1293  * smack_sk_free_security - Free a socket blob
1294  * @sk: the socket
1295  *
1296  * Clears the blob pointer
1297  */
1298 static void smack_sk_free_security(struct sock *sk)
1299 {
1300 	kfree(sk->sk_security);
1301 }
1302 
1303 /**
1304 * smack_host_label - check host based restrictions
1305 * @sip: the object end
1306 *
1307 * looks for host based access restrictions
1308 *
1309 * This version will only be appropriate for really small sets of single label
1310 * hosts.  The caller is responsible for ensuring that the RCU read lock is
1311 * taken before calling this function.
1312 *
1313 * Returns the label of the far end or NULL if it's not special.
1314 */
1315 static char *smack_host_label(struct sockaddr_in *sip)
1316 {
1317 	struct smk_netlbladdr *snp;
1318 	struct in_addr *siap = &sip->sin_addr;
1319 
1320 	if (siap->s_addr == 0)
1321 		return NULL;
1322 
1323 	list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1324 		/*
1325 		* we break after finding the first match because
1326 		* the list is sorted from longest to shortest mask
1327 		* so we have found the most specific match
1328 		*/
1329 		if ((&snp->smk_host.sin_addr)->s_addr ==
1330 		    (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1331 			/* we have found the special CIPSO option */
1332 			if (snp->smk_label == smack_cipso_option)
1333 				return NULL;
1334 			return snp->smk_label;
1335 		}
1336 
1337 	return NULL;
1338 }
1339 
1340 /**
1341  * smack_set_catset - convert a capset to netlabel mls categories
1342  * @catset: the Smack categories
1343  * @sap: where to put the netlabel categories
1344  *
1345  * Allocates and fills attr.mls.cat
1346  */
1347 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1348 {
1349 	unsigned char *cp;
1350 	unsigned char m;
1351 	int cat;
1352 	int rc;
1353 	int byte;
1354 
1355 	if (!catset)
1356 		return;
1357 
1358 	sap->flags |= NETLBL_SECATTR_MLS_CAT;
1359 	sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1360 	sap->attr.mls.cat->startbit = 0;
1361 
1362 	for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1363 		for (m = 0x80; m != 0; m >>= 1, cat++) {
1364 			if ((m & *cp) == 0)
1365 				continue;
1366 			rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1367 							  cat, GFP_ATOMIC);
1368 		}
1369 }
1370 
1371 /**
1372  * smack_to_secattr - fill a secattr from a smack value
1373  * @smack: the smack value
1374  * @nlsp: where the result goes
1375  *
1376  * Casey says that CIPSO is good enough for now.
1377  * It can be used to effect.
1378  * It can also be abused to effect when necessary.
1379  * Appologies to the TSIG group in general and GW in particular.
1380  */
1381 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1382 {
1383 	struct smack_cipso cipso;
1384 	int rc;
1385 
1386 	nlsp->domain = smack;
1387 	nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1388 
1389 	rc = smack_to_cipso(smack, &cipso);
1390 	if (rc == 0) {
1391 		nlsp->attr.mls.lvl = cipso.smk_level;
1392 		smack_set_catset(cipso.smk_catset, nlsp);
1393 	} else {
1394 		nlsp->attr.mls.lvl = smack_cipso_direct;
1395 		smack_set_catset(smack, nlsp);
1396 	}
1397 }
1398 
1399 /**
1400  * smack_netlabel - Set the secattr on a socket
1401  * @sk: the socket
1402  * @labeled: socket label scheme
1403  *
1404  * Convert the outbound smack value (smk_out) to a
1405  * secattr and attach it to the socket.
1406  *
1407  * Returns 0 on success or an error code
1408  */
1409 static int smack_netlabel(struct sock *sk, int labeled)
1410 {
1411 	struct socket_smack *ssp = sk->sk_security;
1412 	struct netlbl_lsm_secattr secattr;
1413 	int rc = 0;
1414 
1415 	/*
1416 	 * Usually the netlabel code will handle changing the
1417 	 * packet labeling based on the label.
1418 	 * The case of a single label host is different, because
1419 	 * a single label host should never get a labeled packet
1420 	 * even though the label is usually associated with a packet
1421 	 * label.
1422 	 */
1423 	local_bh_disable();
1424 	bh_lock_sock_nested(sk);
1425 
1426 	if (ssp->smk_out == smack_net_ambient ||
1427 	    labeled == SMACK_UNLABELED_SOCKET)
1428 		netlbl_sock_delattr(sk);
1429 	else {
1430 		netlbl_secattr_init(&secattr);
1431 		smack_to_secattr(ssp->smk_out, &secattr);
1432 		rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
1433 		netlbl_secattr_destroy(&secattr);
1434 	}
1435 
1436 	bh_unlock_sock(sk);
1437 	local_bh_enable();
1438 
1439 	return rc;
1440 }
1441 
1442 /**
1443  * smack_netlbel_send - Set the secattr on a socket and perform access checks
1444  * @sk: the socket
1445  * @sap: the destination address
1446  *
1447  * Set the correct secattr for the given socket based on the destination
1448  * address and perform any outbound access checks needed.
1449  *
1450  * Returns 0 on success or an error code.
1451  *
1452  */
1453 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1454 {
1455 	int rc;
1456 	int sk_lbl;
1457 	char *hostsp;
1458 	struct socket_smack *ssp = sk->sk_security;
1459 
1460 	rcu_read_lock();
1461 	hostsp = smack_host_label(sap);
1462 	if (hostsp != NULL) {
1463 		sk_lbl = SMACK_UNLABELED_SOCKET;
1464 		rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE);
1465 	} else {
1466 		sk_lbl = SMACK_CIPSO_SOCKET;
1467 		rc = 0;
1468 	}
1469 	rcu_read_unlock();
1470 	if (rc != 0)
1471 		return rc;
1472 
1473 	return smack_netlabel(sk, sk_lbl);
1474 }
1475 
1476 /**
1477  * smack_inode_setsecurity - set smack xattrs
1478  * @inode: the object
1479  * @name: attribute name
1480  * @value: attribute value
1481  * @size: size of the attribute
1482  * @flags: unused
1483  *
1484  * Sets the named attribute in the appropriate blob
1485  *
1486  * Returns 0 on success, or an error code
1487  */
1488 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1489 				   const void *value, size_t size, int flags)
1490 {
1491 	char *sp;
1492 	struct inode_smack *nsp = inode->i_security;
1493 	struct socket_smack *ssp;
1494 	struct socket *sock;
1495 	int rc = 0;
1496 
1497 	if (value == NULL || size > SMK_LABELLEN || size == 0)
1498 		return -EACCES;
1499 
1500 	sp = smk_import(value, size);
1501 	if (sp == NULL)
1502 		return -EINVAL;
1503 
1504 	if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1505 		nsp->smk_inode = sp;
1506 		return 0;
1507 	}
1508 	/*
1509 	 * The rest of the Smack xattrs are only on sockets.
1510 	 */
1511 	if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1512 		return -EOPNOTSUPP;
1513 
1514 	sock = SOCKET_I(inode);
1515 	if (sock == NULL || sock->sk == NULL)
1516 		return -EOPNOTSUPP;
1517 
1518 	ssp = sock->sk->sk_security;
1519 
1520 	if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1521 		ssp->smk_in = sp;
1522 	else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1523 		ssp->smk_out = sp;
1524 		rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1525 		if (rc != 0)
1526 			printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1527 			       __func__, -rc);
1528 	} else
1529 		return -EOPNOTSUPP;
1530 
1531 	return 0;
1532 }
1533 
1534 /**
1535  * smack_socket_post_create - finish socket setup
1536  * @sock: the socket
1537  * @family: protocol family
1538  * @type: unused
1539  * @protocol: unused
1540  * @kern: unused
1541  *
1542  * Sets the netlabel information on the socket
1543  *
1544  * Returns 0 on success, and error code otherwise
1545  */
1546 static int smack_socket_post_create(struct socket *sock, int family,
1547 				    int type, int protocol, int kern)
1548 {
1549 	if (family != PF_INET || sock->sk == NULL)
1550 		return 0;
1551 	/*
1552 	 * Set the outbound netlbl.
1553 	 */
1554 	return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1555 }
1556 
1557 /**
1558  * smack_socket_connect - connect access check
1559  * @sock: the socket
1560  * @sap: the other end
1561  * @addrlen: size of sap
1562  *
1563  * Verifies that a connection may be possible
1564  *
1565  * Returns 0 on success, and error code otherwise
1566  */
1567 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1568 				int addrlen)
1569 {
1570 	if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1571 		return 0;
1572 	if (addrlen < sizeof(struct sockaddr_in))
1573 		return -EINVAL;
1574 
1575 	return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
1576 }
1577 
1578 /**
1579  * smack_flags_to_may - convert S_ to MAY_ values
1580  * @flags: the S_ value
1581  *
1582  * Returns the equivalent MAY_ value
1583  */
1584 static int smack_flags_to_may(int flags)
1585 {
1586 	int may = 0;
1587 
1588 	if (flags & S_IRUGO)
1589 		may |= MAY_READ;
1590 	if (flags & S_IWUGO)
1591 		may |= MAY_WRITE;
1592 	if (flags & S_IXUGO)
1593 		may |= MAY_EXEC;
1594 
1595 	return may;
1596 }
1597 
1598 /**
1599  * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1600  * @msg: the object
1601  *
1602  * Returns 0
1603  */
1604 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1605 {
1606 	msg->security = current_security();
1607 	return 0;
1608 }
1609 
1610 /**
1611  * smack_msg_msg_free_security - Clear the security blob for msg_msg
1612  * @msg: the object
1613  *
1614  * Clears the blob pointer
1615  */
1616 static void smack_msg_msg_free_security(struct msg_msg *msg)
1617 {
1618 	msg->security = NULL;
1619 }
1620 
1621 /**
1622  * smack_of_shm - the smack pointer for the shm
1623  * @shp: the object
1624  *
1625  * Returns a pointer to the smack value
1626  */
1627 static char *smack_of_shm(struct shmid_kernel *shp)
1628 {
1629 	return (char *)shp->shm_perm.security;
1630 }
1631 
1632 /**
1633  * smack_shm_alloc_security - Set the security blob for shm
1634  * @shp: the object
1635  *
1636  * Returns 0
1637  */
1638 static int smack_shm_alloc_security(struct shmid_kernel *shp)
1639 {
1640 	struct kern_ipc_perm *isp = &shp->shm_perm;
1641 
1642 	isp->security = current_security();
1643 	return 0;
1644 }
1645 
1646 /**
1647  * smack_shm_free_security - Clear the security blob for shm
1648  * @shp: the object
1649  *
1650  * Clears the blob pointer
1651  */
1652 static void smack_shm_free_security(struct shmid_kernel *shp)
1653 {
1654 	struct kern_ipc_perm *isp = &shp->shm_perm;
1655 
1656 	isp->security = NULL;
1657 }
1658 
1659 /**
1660  * smack_shm_associate - Smack access check for shm
1661  * @shp: the object
1662  * @shmflg: access requested
1663  *
1664  * Returns 0 if current has the requested access, error code otherwise
1665  */
1666 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1667 {
1668 	char *ssp = smack_of_shm(shp);
1669 	int may;
1670 
1671 	may = smack_flags_to_may(shmflg);
1672 	return smk_curacc(ssp, may);
1673 }
1674 
1675 /**
1676  * smack_shm_shmctl - Smack access check for shm
1677  * @shp: the object
1678  * @cmd: what it wants to do
1679  *
1680  * Returns 0 if current has the requested access, error code otherwise
1681  */
1682 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1683 {
1684 	char *ssp;
1685 	int may;
1686 
1687 	switch (cmd) {
1688 	case IPC_STAT:
1689 	case SHM_STAT:
1690 		may = MAY_READ;
1691 		break;
1692 	case IPC_SET:
1693 	case SHM_LOCK:
1694 	case SHM_UNLOCK:
1695 	case IPC_RMID:
1696 		may = MAY_READWRITE;
1697 		break;
1698 	case IPC_INFO:
1699 	case SHM_INFO:
1700 		/*
1701 		 * System level information.
1702 		 */
1703 		return 0;
1704 	default:
1705 		return -EINVAL;
1706 	}
1707 
1708 	ssp = smack_of_shm(shp);
1709 	return smk_curacc(ssp, may);
1710 }
1711 
1712 /**
1713  * smack_shm_shmat - Smack access for shmat
1714  * @shp: the object
1715  * @shmaddr: unused
1716  * @shmflg: access requested
1717  *
1718  * Returns 0 if current has the requested access, error code otherwise
1719  */
1720 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1721 			   int shmflg)
1722 {
1723 	char *ssp = smack_of_shm(shp);
1724 	int may;
1725 
1726 	may = smack_flags_to_may(shmflg);
1727 	return smk_curacc(ssp, may);
1728 }
1729 
1730 /**
1731  * smack_of_sem - the smack pointer for the sem
1732  * @sma: the object
1733  *
1734  * Returns a pointer to the smack value
1735  */
1736 static char *smack_of_sem(struct sem_array *sma)
1737 {
1738 	return (char *)sma->sem_perm.security;
1739 }
1740 
1741 /**
1742  * smack_sem_alloc_security - Set the security blob for sem
1743  * @sma: the object
1744  *
1745  * Returns 0
1746  */
1747 static int smack_sem_alloc_security(struct sem_array *sma)
1748 {
1749 	struct kern_ipc_perm *isp = &sma->sem_perm;
1750 
1751 	isp->security = current_security();
1752 	return 0;
1753 }
1754 
1755 /**
1756  * smack_sem_free_security - Clear the security blob for sem
1757  * @sma: the object
1758  *
1759  * Clears the blob pointer
1760  */
1761 static void smack_sem_free_security(struct sem_array *sma)
1762 {
1763 	struct kern_ipc_perm *isp = &sma->sem_perm;
1764 
1765 	isp->security = NULL;
1766 }
1767 
1768 /**
1769  * smack_sem_associate - Smack access check for sem
1770  * @sma: the object
1771  * @semflg: access requested
1772  *
1773  * Returns 0 if current has the requested access, error code otherwise
1774  */
1775 static int smack_sem_associate(struct sem_array *sma, int semflg)
1776 {
1777 	char *ssp = smack_of_sem(sma);
1778 	int may;
1779 
1780 	may = smack_flags_to_may(semflg);
1781 	return smk_curacc(ssp, may);
1782 }
1783 
1784 /**
1785  * smack_sem_shmctl - Smack access check for sem
1786  * @sma: the object
1787  * @cmd: what it wants to do
1788  *
1789  * Returns 0 if current has the requested access, error code otherwise
1790  */
1791 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1792 {
1793 	char *ssp;
1794 	int may;
1795 
1796 	switch (cmd) {
1797 	case GETPID:
1798 	case GETNCNT:
1799 	case GETZCNT:
1800 	case GETVAL:
1801 	case GETALL:
1802 	case IPC_STAT:
1803 	case SEM_STAT:
1804 		may = MAY_READ;
1805 		break;
1806 	case SETVAL:
1807 	case SETALL:
1808 	case IPC_RMID:
1809 	case IPC_SET:
1810 		may = MAY_READWRITE;
1811 		break;
1812 	case IPC_INFO:
1813 	case SEM_INFO:
1814 		/*
1815 		 * System level information
1816 		 */
1817 		return 0;
1818 	default:
1819 		return -EINVAL;
1820 	}
1821 
1822 	ssp = smack_of_sem(sma);
1823 	return smk_curacc(ssp, may);
1824 }
1825 
1826 /**
1827  * smack_sem_semop - Smack checks of semaphore operations
1828  * @sma: the object
1829  * @sops: unused
1830  * @nsops: unused
1831  * @alter: unused
1832  *
1833  * Treated as read and write in all cases.
1834  *
1835  * Returns 0 if access is allowed, error code otherwise
1836  */
1837 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
1838 			   unsigned nsops, int alter)
1839 {
1840 	char *ssp = smack_of_sem(sma);
1841 
1842 	return smk_curacc(ssp, MAY_READWRITE);
1843 }
1844 
1845 /**
1846  * smack_msg_alloc_security - Set the security blob for msg
1847  * @msq: the object
1848  *
1849  * Returns 0
1850  */
1851 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
1852 {
1853 	struct kern_ipc_perm *kisp = &msq->q_perm;
1854 
1855 	kisp->security = current_security();
1856 	return 0;
1857 }
1858 
1859 /**
1860  * smack_msg_free_security - Clear the security blob for msg
1861  * @msq: the object
1862  *
1863  * Clears the blob pointer
1864  */
1865 static void smack_msg_queue_free_security(struct msg_queue *msq)
1866 {
1867 	struct kern_ipc_perm *kisp = &msq->q_perm;
1868 
1869 	kisp->security = NULL;
1870 }
1871 
1872 /**
1873  * smack_of_msq - the smack pointer for the msq
1874  * @msq: the object
1875  *
1876  * Returns a pointer to the smack value
1877  */
1878 static char *smack_of_msq(struct msg_queue *msq)
1879 {
1880 	return (char *)msq->q_perm.security;
1881 }
1882 
1883 /**
1884  * smack_msg_queue_associate - Smack access check for msg_queue
1885  * @msq: the object
1886  * @msqflg: access requested
1887  *
1888  * Returns 0 if current has the requested access, error code otherwise
1889  */
1890 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
1891 {
1892 	char *msp = smack_of_msq(msq);
1893 	int may;
1894 
1895 	may = smack_flags_to_may(msqflg);
1896 	return smk_curacc(msp, may);
1897 }
1898 
1899 /**
1900  * smack_msg_queue_msgctl - Smack access check for msg_queue
1901  * @msq: the object
1902  * @cmd: what it wants to do
1903  *
1904  * Returns 0 if current has the requested access, error code otherwise
1905  */
1906 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1907 {
1908 	char *msp;
1909 	int may;
1910 
1911 	switch (cmd) {
1912 	case IPC_STAT:
1913 	case MSG_STAT:
1914 		may = MAY_READ;
1915 		break;
1916 	case IPC_SET:
1917 	case IPC_RMID:
1918 		may = MAY_READWRITE;
1919 		break;
1920 	case IPC_INFO:
1921 	case MSG_INFO:
1922 		/*
1923 		 * System level information
1924 		 */
1925 		return 0;
1926 	default:
1927 		return -EINVAL;
1928 	}
1929 
1930 	msp = smack_of_msq(msq);
1931 	return smk_curacc(msp, may);
1932 }
1933 
1934 /**
1935  * smack_msg_queue_msgsnd - Smack access check for msg_queue
1936  * @msq: the object
1937  * @msg: unused
1938  * @msqflg: access requested
1939  *
1940  * Returns 0 if current has the requested access, error code otherwise
1941  */
1942 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
1943 				  int msqflg)
1944 {
1945 	char *msp = smack_of_msq(msq);
1946 	int rc;
1947 
1948 	rc = smack_flags_to_may(msqflg);
1949 	return smk_curacc(msp, rc);
1950 }
1951 
1952 /**
1953  * smack_msg_queue_msgsnd - Smack access check for msg_queue
1954  * @msq: the object
1955  * @msg: unused
1956  * @target: unused
1957  * @type: unused
1958  * @mode: unused
1959  *
1960  * Returns 0 if current has read and write access, error code otherwise
1961  */
1962 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1963 			struct task_struct *target, long type, int mode)
1964 {
1965 	char *msp = smack_of_msq(msq);
1966 
1967 	return smk_curacc(msp, MAY_READWRITE);
1968 }
1969 
1970 /**
1971  * smack_ipc_permission - Smack access for ipc_permission()
1972  * @ipp: the object permissions
1973  * @flag: access requested
1974  *
1975  * Returns 0 if current has read and write access, error code otherwise
1976  */
1977 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
1978 {
1979 	char *isp = ipp->security;
1980 	int may;
1981 
1982 	may = smack_flags_to_may(flag);
1983 	return smk_curacc(isp, may);
1984 }
1985 
1986 /**
1987  * smack_ipc_getsecid - Extract smack security id
1988  * @ipp: the object permissions
1989  * @secid: where result will be saved
1990  */
1991 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
1992 {
1993 	char *smack = ipp->security;
1994 
1995 	*secid = smack_to_secid(smack);
1996 }
1997 
1998 /**
1999  * smack_d_instantiate - Make sure the blob is correct on an inode
2000  * @opt_dentry: unused
2001  * @inode: the object
2002  *
2003  * Set the inode's security blob if it hasn't been done already.
2004  */
2005 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2006 {
2007 	struct super_block *sbp;
2008 	struct superblock_smack *sbsp;
2009 	struct inode_smack *isp;
2010 	char *csp = current_security();
2011 	char *fetched;
2012 	char *final;
2013 	struct dentry *dp;
2014 
2015 	if (inode == NULL)
2016 		return;
2017 
2018 	isp = inode->i_security;
2019 
2020 	mutex_lock(&isp->smk_lock);
2021 	/*
2022 	 * If the inode is already instantiated
2023 	 * take the quick way out
2024 	 */
2025 	if (isp->smk_flags & SMK_INODE_INSTANT)
2026 		goto unlockandout;
2027 
2028 	sbp = inode->i_sb;
2029 	sbsp = sbp->s_security;
2030 	/*
2031 	 * We're going to use the superblock default label
2032 	 * if there's no label on the file.
2033 	 */
2034 	final = sbsp->smk_default;
2035 
2036 	/*
2037 	 * If this is the root inode the superblock
2038 	 * may be in the process of initialization.
2039 	 * If that is the case use the root value out
2040 	 * of the superblock.
2041 	 */
2042 	if (opt_dentry->d_parent == opt_dentry) {
2043 		isp->smk_inode = sbsp->smk_root;
2044 		isp->smk_flags |= SMK_INODE_INSTANT;
2045 		goto unlockandout;
2046 	}
2047 
2048 	/*
2049 	 * This is pretty hackish.
2050 	 * Casey says that we shouldn't have to do
2051 	 * file system specific code, but it does help
2052 	 * with keeping it simple.
2053 	 */
2054 	switch (sbp->s_magic) {
2055 	case SMACK_MAGIC:
2056 		/*
2057 		 * Casey says that it's a little embarassing
2058 		 * that the smack file system doesn't do
2059 		 * extended attributes.
2060 		 */
2061 		final = smack_known_star.smk_known;
2062 		break;
2063 	case PIPEFS_MAGIC:
2064 		/*
2065 		 * Casey says pipes are easy (?)
2066 		 */
2067 		final = smack_known_star.smk_known;
2068 		break;
2069 	case DEVPTS_SUPER_MAGIC:
2070 		/*
2071 		 * devpts seems content with the label of the task.
2072 		 * Programs that change smack have to treat the
2073 		 * pty with respect.
2074 		 */
2075 		final = csp;
2076 		break;
2077 	case SOCKFS_MAGIC:
2078 		/*
2079 		 * Casey says sockets get the smack of the task.
2080 		 */
2081 		final = csp;
2082 		break;
2083 	case PROC_SUPER_MAGIC:
2084 		/*
2085 		 * Casey says procfs appears not to care.
2086 		 * The superblock default suffices.
2087 		 */
2088 		break;
2089 	case TMPFS_MAGIC:
2090 		/*
2091 		 * Device labels should come from the filesystem,
2092 		 * but watch out, because they're volitile,
2093 		 * getting recreated on every reboot.
2094 		 */
2095 		final = smack_known_star.smk_known;
2096 		/*
2097 		 * No break.
2098 		 *
2099 		 * If a smack value has been set we want to use it,
2100 		 * but since tmpfs isn't giving us the opportunity
2101 		 * to set mount options simulate setting the
2102 		 * superblock default.
2103 		 */
2104 	default:
2105 		/*
2106 		 * This isn't an understood special case.
2107 		 * Get the value from the xattr.
2108 		 *
2109 		 * No xattr support means, alas, no SMACK label.
2110 		 * Use the aforeapplied default.
2111 		 * It would be curious if the label of the task
2112 		 * does not match that assigned.
2113 		 */
2114 		if (inode->i_op->getxattr == NULL)
2115 			break;
2116 		/*
2117 		 * Get the dentry for xattr.
2118 		 */
2119 		if (opt_dentry == NULL) {
2120 			dp = d_find_alias(inode);
2121 			if (dp == NULL)
2122 				break;
2123 		} else {
2124 			dp = dget(opt_dentry);
2125 			if (dp == NULL)
2126 				break;
2127 		}
2128 
2129 		fetched = smk_fetch(inode, dp);
2130 		if (fetched != NULL)
2131 			final = fetched;
2132 
2133 		dput(dp);
2134 		break;
2135 	}
2136 
2137 	if (final == NULL)
2138 		isp->smk_inode = csp;
2139 	else
2140 		isp->smk_inode = final;
2141 
2142 	isp->smk_flags |= SMK_INODE_INSTANT;
2143 
2144 unlockandout:
2145 	mutex_unlock(&isp->smk_lock);
2146 	return;
2147 }
2148 
2149 /**
2150  * smack_getprocattr - Smack process attribute access
2151  * @p: the object task
2152  * @name: the name of the attribute in /proc/.../attr
2153  * @value: where to put the result
2154  *
2155  * Places a copy of the task Smack into value
2156  *
2157  * Returns the length of the smack label or an error code
2158  */
2159 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2160 {
2161 	char *cp;
2162 	int slen;
2163 
2164 	if (strcmp(name, "current") != 0)
2165 		return -EINVAL;
2166 
2167 	cp = kstrdup(task_security(p), GFP_KERNEL);
2168 	if (cp == NULL)
2169 		return -ENOMEM;
2170 
2171 	slen = strlen(cp);
2172 	*value = cp;
2173 	return slen;
2174 }
2175 
2176 /**
2177  * smack_setprocattr - Smack process attribute setting
2178  * @p: the object task
2179  * @name: the name of the attribute in /proc/.../attr
2180  * @value: the value to set
2181  * @size: the size of the value
2182  *
2183  * Sets the Smack value of the task. Only setting self
2184  * is permitted and only with privilege
2185  *
2186  * Returns the length of the smack label or an error code
2187  */
2188 static int smack_setprocattr(struct task_struct *p, char *name,
2189 			     void *value, size_t size)
2190 {
2191 	struct cred *new;
2192 	char *newsmack;
2193 
2194 	/*
2195 	 * Changing another process' Smack value is too dangerous
2196 	 * and supports no sane use case.
2197 	 */
2198 	if (p != current)
2199 		return -EPERM;
2200 
2201 	if (!capable(CAP_MAC_ADMIN))
2202 		return -EPERM;
2203 
2204 	if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2205 		return -EINVAL;
2206 
2207 	if (strcmp(name, "current") != 0)
2208 		return -EINVAL;
2209 
2210 	newsmack = smk_import(value, size);
2211 	if (newsmack == NULL)
2212 		return -EINVAL;
2213 
2214 	/*
2215 	 * No process is ever allowed the web ("@") label.
2216 	 */
2217 	if (newsmack == smack_known_web.smk_known)
2218 		return -EPERM;
2219 
2220 	new = prepare_creds();
2221 	if (new == NULL)
2222 		return -ENOMEM;
2223 	new->security = newsmack;
2224 	commit_creds(new);
2225 	return size;
2226 }
2227 
2228 /**
2229  * smack_unix_stream_connect - Smack access on UDS
2230  * @sock: one socket
2231  * @other: the other socket
2232  * @newsk: unused
2233  *
2234  * Return 0 if a subject with the smack of sock could access
2235  * an object with the smack of other, otherwise an error code
2236  */
2237 static int smack_unix_stream_connect(struct socket *sock,
2238 				     struct socket *other, struct sock *newsk)
2239 {
2240 	struct inode *sp = SOCK_INODE(sock);
2241 	struct inode *op = SOCK_INODE(other);
2242 
2243 	return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
2244 }
2245 
2246 /**
2247  * smack_unix_may_send - Smack access on UDS
2248  * @sock: one socket
2249  * @other: the other socket
2250  *
2251  * Return 0 if a subject with the smack of sock could access
2252  * an object with the smack of other, otherwise an error code
2253  */
2254 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2255 {
2256 	struct inode *sp = SOCK_INODE(sock);
2257 	struct inode *op = SOCK_INODE(other);
2258 
2259 	return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
2260 }
2261 
2262 /**
2263  * smack_socket_sendmsg - Smack check based on destination host
2264  * @sock: the socket
2265  * @msg: the message
2266  * @size: the size of the message
2267  *
2268  * Return 0 if the current subject can write to the destination
2269  * host. This is only a question if the destination is a single
2270  * label host.
2271  */
2272 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2273 				int size)
2274 {
2275 	struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2276 
2277 	/*
2278 	 * Perfectly reasonable for this to be NULL
2279 	 */
2280 	if (sip == NULL || sip->sin_family != PF_INET)
2281 		return 0;
2282 
2283 	return smack_netlabel_send(sock->sk, sip);
2284 }
2285 
2286 
2287 /**
2288  * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2289  * @sap: netlabel secattr
2290  * @sip: where to put the result
2291  *
2292  * Copies a smack label into sip
2293  */
2294 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2295 {
2296 	char smack[SMK_LABELLEN];
2297 	char *sp;
2298 	int pcat;
2299 
2300 	if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2301 		/*
2302 		 * Looks like a CIPSO packet.
2303 		 * If there are flags but no level netlabel isn't
2304 		 * behaving the way we expect it to.
2305 		 *
2306 		 * Get the categories, if any
2307 		 * Without guidance regarding the smack value
2308 		 * for the packet fall back on the network
2309 		 * ambient value.
2310 		 */
2311 		memset(smack, '\0', SMK_LABELLEN);
2312 		if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2313 			for (pcat = -1;;) {
2314 				pcat = netlbl_secattr_catmap_walk(
2315 					sap->attr.mls.cat, pcat + 1);
2316 				if (pcat < 0)
2317 					break;
2318 				smack_catset_bit(pcat, smack);
2319 			}
2320 		/*
2321 		 * If it is CIPSO using smack direct mapping
2322 		 * we are already done. WeeHee.
2323 		 */
2324 		if (sap->attr.mls.lvl == smack_cipso_direct) {
2325 			memcpy(sip, smack, SMK_MAXLEN);
2326 			return;
2327 		}
2328 		/*
2329 		 * Look it up in the supplied table if it is not
2330 		 * a direct mapping.
2331 		 */
2332 		smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2333 		return;
2334 	}
2335 	if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2336 		/*
2337 		 * Looks like a fallback, which gives us a secid.
2338 		 */
2339 		sp = smack_from_secid(sap->attr.secid);
2340 		/*
2341 		 * This has got to be a bug because it is
2342 		 * impossible to specify a fallback without
2343 		 * specifying the label, which will ensure
2344 		 * it has a secid, and the only way to get a
2345 		 * secid is from a fallback.
2346 		 */
2347 		BUG_ON(sp == NULL);
2348 		strncpy(sip, sp, SMK_MAXLEN);
2349 		return;
2350 	}
2351 	/*
2352 	 * Without guidance regarding the smack value
2353 	 * for the packet fall back on the network
2354 	 * ambient value.
2355 	 */
2356 	strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2357 	return;
2358 }
2359 
2360 /**
2361  * smack_socket_sock_rcv_skb - Smack packet delivery access check
2362  * @sk: socket
2363  * @skb: packet
2364  *
2365  * Returns 0 if the packet should be delivered, an error code otherwise
2366  */
2367 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2368 {
2369 	struct netlbl_lsm_secattr secattr;
2370 	struct socket_smack *ssp = sk->sk_security;
2371 	char smack[SMK_LABELLEN];
2372 	char *csp;
2373 	int rc;
2374 
2375 	if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2376 		return 0;
2377 
2378 	/*
2379 	 * Translate what netlabel gave us.
2380 	 */
2381 	netlbl_secattr_init(&secattr);
2382 
2383 	rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2384 	if (rc == 0) {
2385 		smack_from_secattr(&secattr, smack);
2386 		csp = smack;
2387 	} else
2388 		csp = smack_net_ambient;
2389 
2390 	netlbl_secattr_destroy(&secattr);
2391 
2392 	/*
2393 	 * Receiving a packet requires that the other end
2394 	 * be able to write here. Read access is not required.
2395 	 * This is the simplist possible security model
2396 	 * for networking.
2397 	 */
2398 	rc = smk_access(csp, ssp->smk_in, MAY_WRITE);
2399 	if (rc != 0)
2400 		netlbl_skbuff_err(skb, rc, 0);
2401 	return rc;
2402 }
2403 
2404 /**
2405  * smack_socket_getpeersec_stream - pull in packet label
2406  * @sock: the socket
2407  * @optval: user's destination
2408  * @optlen: size thereof
2409  * @len: max thereof
2410  *
2411  * returns zero on success, an error code otherwise
2412  */
2413 static int smack_socket_getpeersec_stream(struct socket *sock,
2414 					  char __user *optval,
2415 					  int __user *optlen, unsigned len)
2416 {
2417 	struct socket_smack *ssp;
2418 	int slen;
2419 	int rc = 0;
2420 
2421 	ssp = sock->sk->sk_security;
2422 	slen = strlen(ssp->smk_packet) + 1;
2423 
2424 	if (slen > len)
2425 		rc = -ERANGE;
2426 	else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2427 		rc = -EFAULT;
2428 
2429 	if (put_user(slen, optlen) != 0)
2430 		rc = -EFAULT;
2431 
2432 	return rc;
2433 }
2434 
2435 
2436 /**
2437  * smack_socket_getpeersec_dgram - pull in packet label
2438  * @sock: the socket
2439  * @skb: packet data
2440  * @secid: pointer to where to put the secid of the packet
2441  *
2442  * Sets the netlabel socket state on sk from parent
2443  */
2444 static int smack_socket_getpeersec_dgram(struct socket *sock,
2445 					 struct sk_buff *skb, u32 *secid)
2446 
2447 {
2448 	struct netlbl_lsm_secattr secattr;
2449 	struct sock *sk;
2450 	char smack[SMK_LABELLEN];
2451 	int family = PF_INET;
2452 	u32 s;
2453 	int rc;
2454 
2455 	/*
2456 	 * Only works for families with packets.
2457 	 */
2458 	if (sock != NULL) {
2459 		sk = sock->sk;
2460 		if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2461 			return 0;
2462 		family = sk->sk_family;
2463 	}
2464 	/*
2465 	 * Translate what netlabel gave us.
2466 	 */
2467 	netlbl_secattr_init(&secattr);
2468 	rc = netlbl_skbuff_getattr(skb, family, &secattr);
2469 	if (rc == 0)
2470 		smack_from_secattr(&secattr, smack);
2471 	netlbl_secattr_destroy(&secattr);
2472 
2473 	/*
2474 	 * Give up if we couldn't get anything
2475 	 */
2476 	if (rc != 0)
2477 		return rc;
2478 
2479 	s = smack_to_secid(smack);
2480 	if (s == 0)
2481 		return -EINVAL;
2482 
2483 	*secid = s;
2484 	return 0;
2485 }
2486 
2487 /**
2488  * smack_sock_graft - Initialize a newly created socket with an existing sock
2489  * @sk: child sock
2490  * @parent: parent socket
2491  *
2492  * Set the smk_{in,out} state of an existing sock based on the process that
2493  * is creating the new socket.
2494  */
2495 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2496 {
2497 	struct socket_smack *ssp;
2498 
2499 	if (sk == NULL ||
2500 	    (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
2501 		return;
2502 
2503 	ssp = sk->sk_security;
2504 	ssp->smk_in = ssp->smk_out = current_security();
2505 	/* cssp->smk_packet is already set in smack_inet_csk_clone() */
2506 }
2507 
2508 /**
2509  * smack_inet_conn_request - Smack access check on connect
2510  * @sk: socket involved
2511  * @skb: packet
2512  * @req: unused
2513  *
2514  * Returns 0 if a task with the packet label could write to
2515  * the socket, otherwise an error code
2516  */
2517 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2518 				   struct request_sock *req)
2519 {
2520 	u16 family = sk->sk_family;
2521 	struct socket_smack *ssp = sk->sk_security;
2522 	struct netlbl_lsm_secattr secattr;
2523 	struct sockaddr_in addr;
2524 	struct iphdr *hdr;
2525 	char smack[SMK_LABELLEN];
2526 	int rc;
2527 
2528 	/* handle mapped IPv4 packets arriving via IPv6 sockets */
2529 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
2530 		family = PF_INET;
2531 
2532 	netlbl_secattr_init(&secattr);
2533 	rc = netlbl_skbuff_getattr(skb, family, &secattr);
2534 	if (rc == 0)
2535 		smack_from_secattr(&secattr, smack);
2536 	else
2537 		strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2538 	netlbl_secattr_destroy(&secattr);
2539 
2540 	/*
2541 	 * Receiving a packet requires that the other end be able to write
2542 	 * here. Read access is not required.
2543 	 */
2544 	rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2545 	if (rc != 0)
2546 		return rc;
2547 
2548 	/*
2549 	 * Save the peer's label in the request_sock so we can later setup
2550 	 * smk_packet in the child socket so that SO_PEERCRED can report it.
2551 	 */
2552 	req->peer_secid = smack_to_secid(smack);
2553 
2554 	/*
2555 	 * We need to decide if we want to label the incoming connection here
2556 	 * if we do we only need to label the request_sock and the stack will
2557 	 * propogate the wire-label to the sock when it is created.
2558 	 */
2559 	hdr = ip_hdr(skb);
2560 	addr.sin_addr.s_addr = hdr->saddr;
2561 	rcu_read_lock();
2562 	if (smack_host_label(&addr) == NULL) {
2563 		rcu_read_unlock();
2564 		netlbl_secattr_init(&secattr);
2565 		smack_to_secattr(smack, &secattr);
2566 		rc = netlbl_req_setattr(req, &secattr);
2567 		netlbl_secattr_destroy(&secattr);
2568 	} else {
2569 		rcu_read_unlock();
2570 		netlbl_req_delattr(req);
2571 	}
2572 
2573 	return rc;
2574 }
2575 
2576 /**
2577  * smack_inet_csk_clone - Copy the connection information to the new socket
2578  * @sk: the new socket
2579  * @req: the connection's request_sock
2580  *
2581  * Transfer the connection's peer label to the newly created socket.
2582  */
2583 static void smack_inet_csk_clone(struct sock *sk,
2584 				 const struct request_sock *req)
2585 {
2586 	struct socket_smack *ssp = sk->sk_security;
2587 	char *smack;
2588 
2589 	if (req->peer_secid != 0) {
2590 		smack = smack_from_secid(req->peer_secid);
2591 		strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2592 	} else
2593 		ssp->smk_packet[0] = '\0';
2594 }
2595 
2596 /*
2597  * Key management security hooks
2598  *
2599  * Casey has not tested key support very heavily.
2600  * The permission check is most likely too restrictive.
2601  * If you care about keys please have a look.
2602  */
2603 #ifdef CONFIG_KEYS
2604 
2605 /**
2606  * smack_key_alloc - Set the key security blob
2607  * @key: object
2608  * @cred: the credentials to use
2609  * @flags: unused
2610  *
2611  * No allocation required
2612  *
2613  * Returns 0
2614  */
2615 static int smack_key_alloc(struct key *key, const struct cred *cred,
2616 			   unsigned long flags)
2617 {
2618 	key->security = cred->security;
2619 	return 0;
2620 }
2621 
2622 /**
2623  * smack_key_free - Clear the key security blob
2624  * @key: the object
2625  *
2626  * Clear the blob pointer
2627  */
2628 static void smack_key_free(struct key *key)
2629 {
2630 	key->security = NULL;
2631 }
2632 
2633 /*
2634  * smack_key_permission - Smack access on a key
2635  * @key_ref: gets to the object
2636  * @cred: the credentials to use
2637  * @perm: unused
2638  *
2639  * Return 0 if the task has read and write to the object,
2640  * an error code otherwise
2641  */
2642 static int smack_key_permission(key_ref_t key_ref,
2643 				const struct cred *cred, key_perm_t perm)
2644 {
2645 	struct key *keyp;
2646 
2647 	keyp = key_ref_to_ptr(key_ref);
2648 	if (keyp == NULL)
2649 		return -EINVAL;
2650 	/*
2651 	 * If the key hasn't been initialized give it access so that
2652 	 * it may do so.
2653 	 */
2654 	if (keyp->security == NULL)
2655 		return 0;
2656 	/*
2657 	 * This should not occur
2658 	 */
2659 	if (cred->security == NULL)
2660 		return -EACCES;
2661 
2662 	return smk_access(cred->security, keyp->security, MAY_READWRITE);
2663 }
2664 #endif /* CONFIG_KEYS */
2665 
2666 /*
2667  * Smack Audit hooks
2668  *
2669  * Audit requires a unique representation of each Smack specific
2670  * rule. This unique representation is used to distinguish the
2671  * object to be audited from remaining kernel objects and also
2672  * works as a glue between the audit hooks.
2673  *
2674  * Since repository entries are added but never deleted, we'll use
2675  * the smack_known label address related to the given audit rule as
2676  * the needed unique representation. This also better fits the smack
2677  * model where nearly everything is a label.
2678  */
2679 #ifdef CONFIG_AUDIT
2680 
2681 /**
2682  * smack_audit_rule_init - Initialize a smack audit rule
2683  * @field: audit rule fields given from user-space (audit.h)
2684  * @op: required testing operator (=, !=, >, <, ...)
2685  * @rulestr: smack label to be audited
2686  * @vrule: pointer to save our own audit rule representation
2687  *
2688  * Prepare to audit cases where (@field @op @rulestr) is true.
2689  * The label to be audited is created if necessay.
2690  */
2691 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2692 {
2693 	char **rule = (char **)vrule;
2694 	*rule = NULL;
2695 
2696 	if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2697 		return -EINVAL;
2698 
2699 	if (op != Audit_equal && op != Audit_not_equal)
2700 		return -EINVAL;
2701 
2702 	*rule = smk_import(rulestr, 0);
2703 
2704 	return 0;
2705 }
2706 
2707 /**
2708  * smack_audit_rule_known - Distinguish Smack audit rules
2709  * @krule: rule of interest, in Audit kernel representation format
2710  *
2711  * This is used to filter Smack rules from remaining Audit ones.
2712  * If it's proved that this rule belongs to us, the
2713  * audit_rule_match hook will be called to do the final judgement.
2714  */
2715 static int smack_audit_rule_known(struct audit_krule *krule)
2716 {
2717 	struct audit_field *f;
2718 	int i;
2719 
2720 	for (i = 0; i < krule->field_count; i++) {
2721 		f = &krule->fields[i];
2722 
2723 		if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2724 			return 1;
2725 	}
2726 
2727 	return 0;
2728 }
2729 
2730 /**
2731  * smack_audit_rule_match - Audit given object ?
2732  * @secid: security id for identifying the object to test
2733  * @field: audit rule flags given from user-space
2734  * @op: required testing operator
2735  * @vrule: smack internal rule presentation
2736  * @actx: audit context associated with the check
2737  *
2738  * The core Audit hook. It's used to take the decision of
2739  * whether to audit or not to audit a given object.
2740  */
2741 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2742 				  struct audit_context *actx)
2743 {
2744 	char *smack;
2745 	char *rule = vrule;
2746 
2747 	if (!rule) {
2748 		audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2749 			  "Smack: missing rule\n");
2750 		return -ENOENT;
2751 	}
2752 
2753 	if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2754 		return 0;
2755 
2756 	smack = smack_from_secid(secid);
2757 
2758 	/*
2759 	 * No need to do string comparisons. If a match occurs,
2760 	 * both pointers will point to the same smack_known
2761 	 * label.
2762 	 */
2763 	if (op == Audit_equal)
2764 		return (rule == smack);
2765 	if (op == Audit_not_equal)
2766 		return (rule != smack);
2767 
2768 	return 0;
2769 }
2770 
2771 /**
2772  * smack_audit_rule_free - free smack rule representation
2773  * @vrule: rule to be freed.
2774  *
2775  * No memory was allocated.
2776  */
2777 static void smack_audit_rule_free(void *vrule)
2778 {
2779 	/* No-op */
2780 }
2781 
2782 #endif /* CONFIG_AUDIT */
2783 
2784 /**
2785  * smack_secid_to_secctx - return the smack label for a secid
2786  * @secid: incoming integer
2787  * @secdata: destination
2788  * @seclen: how long it is
2789  *
2790  * Exists for networking code.
2791  */
2792 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2793 {
2794 	char *sp = smack_from_secid(secid);
2795 
2796 	*secdata = sp;
2797 	*seclen = strlen(sp);
2798 	return 0;
2799 }
2800 
2801 /**
2802  * smack_secctx_to_secid - return the secid for a smack label
2803  * @secdata: smack label
2804  * @seclen: how long result is
2805  * @secid: outgoing integer
2806  *
2807  * Exists for audit and networking code.
2808  */
2809 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
2810 {
2811 	*secid = smack_to_secid(secdata);
2812 	return 0;
2813 }
2814 
2815 /**
2816  * smack_release_secctx - don't do anything.
2817  * @secdata: unused
2818  * @seclen: unused
2819  *
2820  * Exists to make sure nothing gets done, and properly
2821  */
2822 static void smack_release_secctx(char *secdata, u32 seclen)
2823 {
2824 }
2825 
2826 struct security_operations smack_ops = {
2827 	.name =				"smack",
2828 
2829 	.ptrace_may_access =		smack_ptrace_may_access,
2830 	.ptrace_traceme =		smack_ptrace_traceme,
2831 	.capget = 			cap_capget,
2832 	.capset = 			cap_capset,
2833 	.capable = 			cap_capable,
2834 	.syslog = 			smack_syslog,
2835 	.settime = 			cap_settime,
2836 	.vm_enough_memory = 		cap_vm_enough_memory,
2837 
2838 	.bprm_set_creds = 		cap_bprm_set_creds,
2839 	.bprm_secureexec = 		cap_bprm_secureexec,
2840 
2841 	.sb_alloc_security = 		smack_sb_alloc_security,
2842 	.sb_free_security = 		smack_sb_free_security,
2843 	.sb_copy_data = 		smack_sb_copy_data,
2844 	.sb_kern_mount = 		smack_sb_kern_mount,
2845 	.sb_statfs = 			smack_sb_statfs,
2846 	.sb_mount = 			smack_sb_mount,
2847 	.sb_umount = 			smack_sb_umount,
2848 
2849 	.inode_alloc_security = 	smack_inode_alloc_security,
2850 	.inode_free_security = 		smack_inode_free_security,
2851 	.inode_init_security = 		smack_inode_init_security,
2852 	.inode_link = 			smack_inode_link,
2853 	.inode_unlink = 		smack_inode_unlink,
2854 	.inode_rmdir = 			smack_inode_rmdir,
2855 	.inode_rename = 		smack_inode_rename,
2856 	.inode_permission = 		smack_inode_permission,
2857 	.inode_setattr = 		smack_inode_setattr,
2858 	.inode_getattr = 		smack_inode_getattr,
2859 	.inode_setxattr = 		smack_inode_setxattr,
2860 	.inode_post_setxattr = 		smack_inode_post_setxattr,
2861 	.inode_getxattr = 		smack_inode_getxattr,
2862 	.inode_removexattr = 		smack_inode_removexattr,
2863 	.inode_need_killpriv =		cap_inode_need_killpriv,
2864 	.inode_killpriv =		cap_inode_killpriv,
2865 	.inode_getsecurity = 		smack_inode_getsecurity,
2866 	.inode_setsecurity = 		smack_inode_setsecurity,
2867 	.inode_listsecurity = 		smack_inode_listsecurity,
2868 	.inode_getsecid =		smack_inode_getsecid,
2869 
2870 	.file_permission = 		smack_file_permission,
2871 	.file_alloc_security = 		smack_file_alloc_security,
2872 	.file_free_security = 		smack_file_free_security,
2873 	.file_ioctl = 			smack_file_ioctl,
2874 	.file_lock = 			smack_file_lock,
2875 	.file_fcntl = 			smack_file_fcntl,
2876 	.file_set_fowner = 		smack_file_set_fowner,
2877 	.file_send_sigiotask = 		smack_file_send_sigiotask,
2878 	.file_receive = 		smack_file_receive,
2879 
2880 	.cred_free =			smack_cred_free,
2881 	.cred_prepare =			smack_cred_prepare,
2882 	.cred_commit =			smack_cred_commit,
2883 	.kernel_act_as =		smack_kernel_act_as,
2884 	.kernel_create_files_as =	smack_kernel_create_files_as,
2885 	.task_fix_setuid =		cap_task_fix_setuid,
2886 	.task_setpgid = 		smack_task_setpgid,
2887 	.task_getpgid = 		smack_task_getpgid,
2888 	.task_getsid = 			smack_task_getsid,
2889 	.task_getsecid = 		smack_task_getsecid,
2890 	.task_setnice = 		smack_task_setnice,
2891 	.task_setioprio = 		smack_task_setioprio,
2892 	.task_getioprio = 		smack_task_getioprio,
2893 	.task_setscheduler = 		smack_task_setscheduler,
2894 	.task_getscheduler = 		smack_task_getscheduler,
2895 	.task_movememory = 		smack_task_movememory,
2896 	.task_kill = 			smack_task_kill,
2897 	.task_wait = 			smack_task_wait,
2898 	.task_to_inode = 		smack_task_to_inode,
2899 	.task_prctl =			cap_task_prctl,
2900 
2901 	.ipc_permission = 		smack_ipc_permission,
2902 	.ipc_getsecid =			smack_ipc_getsecid,
2903 
2904 	.msg_msg_alloc_security = 	smack_msg_msg_alloc_security,
2905 	.msg_msg_free_security = 	smack_msg_msg_free_security,
2906 
2907 	.msg_queue_alloc_security = 	smack_msg_queue_alloc_security,
2908 	.msg_queue_free_security = 	smack_msg_queue_free_security,
2909 	.msg_queue_associate = 		smack_msg_queue_associate,
2910 	.msg_queue_msgctl = 		smack_msg_queue_msgctl,
2911 	.msg_queue_msgsnd = 		smack_msg_queue_msgsnd,
2912 	.msg_queue_msgrcv = 		smack_msg_queue_msgrcv,
2913 
2914 	.shm_alloc_security = 		smack_shm_alloc_security,
2915 	.shm_free_security = 		smack_shm_free_security,
2916 	.shm_associate = 		smack_shm_associate,
2917 	.shm_shmctl = 			smack_shm_shmctl,
2918 	.shm_shmat = 			smack_shm_shmat,
2919 
2920 	.sem_alloc_security = 		smack_sem_alloc_security,
2921 	.sem_free_security = 		smack_sem_free_security,
2922 	.sem_associate = 		smack_sem_associate,
2923 	.sem_semctl = 			smack_sem_semctl,
2924 	.sem_semop = 			smack_sem_semop,
2925 
2926 	.netlink_send =			cap_netlink_send,
2927 	.netlink_recv = 		cap_netlink_recv,
2928 
2929 	.d_instantiate = 		smack_d_instantiate,
2930 
2931 	.getprocattr = 			smack_getprocattr,
2932 	.setprocattr = 			smack_setprocattr,
2933 
2934 	.unix_stream_connect = 		smack_unix_stream_connect,
2935 	.unix_may_send = 		smack_unix_may_send,
2936 
2937 	.socket_post_create = 		smack_socket_post_create,
2938 	.socket_connect =		smack_socket_connect,
2939 	.socket_sendmsg =		smack_socket_sendmsg,
2940 	.socket_sock_rcv_skb = 		smack_socket_sock_rcv_skb,
2941 	.socket_getpeersec_stream =	smack_socket_getpeersec_stream,
2942 	.socket_getpeersec_dgram =	smack_socket_getpeersec_dgram,
2943 	.sk_alloc_security = 		smack_sk_alloc_security,
2944 	.sk_free_security = 		smack_sk_free_security,
2945 	.sock_graft = 			smack_sock_graft,
2946 	.inet_conn_request = 		smack_inet_conn_request,
2947 	.inet_csk_clone =		smack_inet_csk_clone,
2948 
2949  /* key management security hooks */
2950 #ifdef CONFIG_KEYS
2951 	.key_alloc = 			smack_key_alloc,
2952 	.key_free = 			smack_key_free,
2953 	.key_permission = 		smack_key_permission,
2954 #endif /* CONFIG_KEYS */
2955 
2956  /* Audit hooks */
2957 #ifdef CONFIG_AUDIT
2958 	.audit_rule_init =		smack_audit_rule_init,
2959 	.audit_rule_known =		smack_audit_rule_known,
2960 	.audit_rule_match =		smack_audit_rule_match,
2961 	.audit_rule_free =		smack_audit_rule_free,
2962 #endif /* CONFIG_AUDIT */
2963 
2964 	.secid_to_secctx = 		smack_secid_to_secctx,
2965 	.secctx_to_secid = 		smack_secctx_to_secid,
2966 	.release_secctx = 		smack_release_secctx,
2967 };
2968 
2969 
2970 static __init void init_smack_know_list(void)
2971 {
2972 	list_add(&smack_known_huh.list, &smack_known_list);
2973 	list_add(&smack_known_hat.list, &smack_known_list);
2974 	list_add(&smack_known_star.list, &smack_known_list);
2975 	list_add(&smack_known_floor.list, &smack_known_list);
2976 	list_add(&smack_known_invalid.list, &smack_known_list);
2977 	list_add(&smack_known_web.list, &smack_known_list);
2978 }
2979 
2980 /**
2981  * smack_init - initialize the smack system
2982  *
2983  * Returns 0
2984  */
2985 static __init int smack_init(void)
2986 {
2987 	struct cred *cred;
2988 
2989 	if (!security_module_enable(&smack_ops))
2990 		return 0;
2991 
2992 	printk(KERN_INFO "Smack:  Initializing.\n");
2993 
2994 	/*
2995 	 * Set the security state for the initial task.
2996 	 */
2997 	cred = (struct cred *) current->cred;
2998 	cred->security = &smack_known_floor.smk_known;
2999 
3000 	/* initilize the smack_know_list */
3001 	init_smack_know_list();
3002 	/*
3003 	 * Initialize locks
3004 	 */
3005 	spin_lock_init(&smack_known_huh.smk_cipsolock);
3006 	spin_lock_init(&smack_known_hat.smk_cipsolock);
3007 	spin_lock_init(&smack_known_star.smk_cipsolock);
3008 	spin_lock_init(&smack_known_floor.smk_cipsolock);
3009 	spin_lock_init(&smack_known_invalid.smk_cipsolock);
3010 
3011 	/*
3012 	 * Register with LSM
3013 	 */
3014 	if (register_security(&smack_ops))
3015 		panic("smack: Unable to register with kernel.\n");
3016 
3017 	return 0;
3018 }
3019 
3020 /*
3021  * Smack requires early initialization in order to label
3022  * all processes and objects when they are created.
3023  */
3024 security_initcall(smack_init);
3025