xref: /openbmc/linux/fs/jffs2/security.c (revision d9a82a04)
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright © 2006  NEC Corporation
5  *
6  * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/fs.h>
15 #include <linux/time.h>
16 #include <linux/pagemap.h>
17 #include <linux/highmem.h>
18 #include <linux/crc32.h>
19 #include <linux/jffs2.h>
20 #include <linux/xattr.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/security.h>
23 #include "nodelist.h"
24 
25 /* ---- Initial Security Label(s) Attachment callback --- */
26 static int jffs2_initxattrs(struct inode *inode,
27 			    const struct xattr *xattr_array, void *fs_info)
28 {
29 	const struct xattr *xattr;
30 	int err = 0;
31 
32 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
33 		err = do_jffs2_setxattr(inode, JFFS2_XPREFIX_SECURITY,
34 					xattr->name, xattr->value,
35 					xattr->value_len, 0);
36 		if (err < 0)
37 			break;
38 	}
39 	return err;
40 }
41 
42 /* ---- Initial Security Label(s) Attachment ----------- */
43 int jffs2_init_security(struct inode *inode, struct inode *dir,
44 			const struct qstr *qstr)
45 {
46 	return security_inode_init_security(inode, dir, qstr,
47 					    &jffs2_initxattrs, NULL);
48 }
49 
50 /* ---- XATTR Handler for "security.*" ----------------- */
51 static int jffs2_security_getxattr(const struct xattr_handler *handler,
52 				   struct dentry *dentry, const char *name,
53 				   void *buffer, size_t size)
54 {
55 	if (!strcmp(name, ""))
56 		return -EINVAL;
57 
58 	return do_jffs2_getxattr(d_inode(dentry), JFFS2_XPREFIX_SECURITY,
59 				 name, buffer, size);
60 }
61 
62 static int jffs2_security_setxattr(const struct xattr_handler *handler,
63 				   struct dentry *dentry, const char *name,
64 				   const void *buffer, size_t size, int flags)
65 {
66 	if (!strcmp(name, ""))
67 		return -EINVAL;
68 
69 	return do_jffs2_setxattr(d_inode(dentry), JFFS2_XPREFIX_SECURITY,
70 				 name, buffer, size, flags);
71 }
72 
73 static size_t jffs2_security_listxattr(const struct xattr_handler *handler,
74 				       struct dentry *dentry, char *list,
75 				       size_t list_size, const char *name,
76 				       size_t name_len)
77 {
78 	size_t retlen = XATTR_SECURITY_PREFIX_LEN + name_len + 1;
79 
80 	if (list && retlen <= list_size) {
81 		strcpy(list, XATTR_SECURITY_PREFIX);
82 		strcpy(list + XATTR_SECURITY_PREFIX_LEN, name);
83 	}
84 
85 	return retlen;
86 }
87 
88 const struct xattr_handler jffs2_security_xattr_handler = {
89 	.prefix = XATTR_SECURITY_PREFIX,
90 	.list = jffs2_security_listxattr,
91 	.set = jffs2_security_setxattr,
92 	.get = jffs2_security_getxattr
93 };
94