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