xref: /openbmc/linux/fs/reiserfs/xattr_security.c (revision b296821a)
1 #include "reiserfs.h"
2 #include <linux/errno.h>
3 #include <linux/fs.h>
4 #include <linux/pagemap.h>
5 #include <linux/xattr.h>
6 #include <linux/slab.h>
7 #include "xattr.h"
8 #include <linux/security.h>
9 #include <linux/uaccess.h>
10 
11 static int
12 security_get(const struct xattr_handler *handler, struct dentry *unused,
13 	     struct inode *inode, const char *name, void *buffer, size_t size)
14 {
15 	if (IS_PRIVATE(inode))
16 		return -EPERM;
17 
18 	return reiserfs_xattr_get(inode, xattr_full_name(handler, name),
19 				  buffer, size);
20 }
21 
22 static int
23 security_set(const struct xattr_handler *handler, struct dentry *dentry,
24 	     const char *name, const void *buffer, size_t size, int flags)
25 {
26 	if (IS_PRIVATE(d_inode(dentry)))
27 		return -EPERM;
28 
29 	return reiserfs_xattr_set(d_inode(dentry),
30 				  xattr_full_name(handler, name),
31 				  buffer, size, flags);
32 }
33 
34 static bool security_list(struct dentry *dentry)
35 {
36 	return !IS_PRIVATE(d_inode(dentry));
37 }
38 
39 /* Initializes the security context for a new inode and returns the number
40  * of blocks needed for the transaction. If successful, reiserfs_security
41  * must be released using reiserfs_security_free when the caller is done. */
42 int reiserfs_security_init(struct inode *dir, struct inode *inode,
43 			   const struct qstr *qstr,
44 			   struct reiserfs_security_handle *sec)
45 {
46 	int blocks = 0;
47 	int error;
48 
49 	sec->name = NULL;
50 
51 	/* Don't add selinux attributes on xattrs - they'll never get used */
52 	if (IS_PRIVATE(dir))
53 		return 0;
54 
55 	error = security_old_inode_init_security(inode, dir, qstr, &sec->name,
56 						 &sec->value, &sec->length);
57 	if (error) {
58 		if (error == -EOPNOTSUPP)
59 			error = 0;
60 
61 		sec->name = NULL;
62 		sec->value = NULL;
63 		sec->length = 0;
64 		return error;
65 	}
66 
67 	if (sec->length && reiserfs_xattrs_initialized(inode->i_sb)) {
68 		blocks = reiserfs_xattr_jcreate_nblocks(inode) +
69 			 reiserfs_xattr_nblocks(inode, sec->length);
70 		/* We don't want to count the directories twice if we have
71 		 * a default ACL. */
72 		REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
73 	}
74 	return blocks;
75 }
76 
77 int reiserfs_security_write(struct reiserfs_transaction_handle *th,
78 			    struct inode *inode,
79 			    struct reiserfs_security_handle *sec)
80 {
81 	int error;
82 	if (strlen(sec->name) < sizeof(XATTR_SECURITY_PREFIX))
83 		return -EINVAL;
84 
85 	error = reiserfs_xattr_set_handle(th, inode, sec->name, sec->value,
86 					  sec->length, XATTR_CREATE);
87 	if (error == -ENODATA || error == -EOPNOTSUPP)
88 		error = 0;
89 
90 	return error;
91 }
92 
93 void reiserfs_security_free(struct reiserfs_security_handle *sec)
94 {
95 	kfree(sec->name);
96 	kfree(sec->value);
97 	sec->name = NULL;
98 	sec->value = NULL;
99 }
100 
101 const struct xattr_handler reiserfs_xattr_security_handler = {
102 	.prefix = XATTR_SECURITY_PREFIX,
103 	.get = security_get,
104 	.set = security_set,
105 	.list = security_list,
106 };
107