blob: 20be9a0e5870e5c8e2ee9f6bebca804e961446bf [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Al Virof466c6f2012-03-17 01:16:43 -04002#include "reiserfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/errno.h>
4#include <linux/fs.h>
5#include <linux/pagemap.h>
6#include <linux/xattr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Al Viroc45ac882012-03-17 00:59:06 -04008#include "xattr.h"
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04009#include <linux/security.h>
Fabian Frederick170939912014-08-08 14:21:12 -070010#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012static int
Al Virob2968212016-04-10 20:48:24 -040013security_get(const struct xattr_handler *handler, struct dentry *unused,
14 struct inode *inode, const char *name, void *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015{
Al Virob2968212016-04-10 20:48:24 -040016 if (IS_PRIVATE(inode))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070017 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Al Virob2968212016-04-10 20:48:24 -040019 return reiserfs_xattr_get(inode, xattr_full_name(handler, name),
Al Viro79a628d2016-04-10 18:50:48 -040020 buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021}
22
23static int
Al Viro59301222016-05-27 10:19:30 -040024security_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Al Viro59301222016-05-27 10:19:30 -040028 if (IS_PRIVATE(inode))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070029 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Al Viro59301222016-05-27 10:19:30 -040031 return reiserfs_xattr_set(inode,
Al Viro79a628d2016-04-10 18:50:48 -040032 xattr_full_name(handler, name),
33 buffer, size, flags);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070034}
35
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010036static bool security_list(struct dentry *dentry)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070037{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010038 return !IS_PRIVATE(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Jeff Mahoney57fe60d2009-03-30 14:02:41 -040041/* 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. */
44int reiserfs_security_init(struct inode *dir, struct inode *inode,
Eric Paris2a7dba32011-02-01 11:05:39 -050045 const struct qstr *qstr,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -040046 struct reiserfs_security_handle *sec)
47{
48 int blocks = 0;
Jeff Mahoneyb82bb722009-05-05 15:30:16 -040049 int error;
50
51 sec->name = NULL;
52
53 /* Don't add selinux attributes on xattrs - they'll never get used */
54 if (IS_PRIVATE(dir))
55 return 0;
56
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040057 error = security_old_inode_init_security(inode, dir, qstr, &sec->name,
58 &sec->value, &sec->length);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -040059 if (error) {
60 if (error == -EOPNOTSUPP)
61 error = 0;
62
63 sec->name = NULL;
64 sec->value = NULL;
65 sec->length = 0;
66 return error;
67 }
68
Jeff Mahoney6cb4aff2010-03-23 13:35:38 -070069 if (sec->length && reiserfs_xattrs_initialized(inode->i_sb)) {
Jeff Mahoney57fe60d2009-03-30 14:02:41 -040070 blocks = reiserfs_xattr_jcreate_nblocks(inode) +
71 reiserfs_xattr_nblocks(inode, sec->length);
72 /* We don't want to count the directories twice if we have
73 * a default ACL. */
74 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
75 }
76 return blocks;
77}
78
79int reiserfs_security_write(struct reiserfs_transaction_handle *th,
80 struct inode *inode,
81 struct reiserfs_security_handle *sec)
82{
83 int error;
84 if (strlen(sec->name) < sizeof(XATTR_SECURITY_PREFIX))
85 return -EINVAL;
86
87 error = reiserfs_xattr_set_handle(th, inode, sec->name, sec->value,
88 sec->length, XATTR_CREATE);
89 if (error == -ENODATA || error == -EOPNOTSUPP)
90 error = 0;
91
92 return error;
93}
94
95void reiserfs_security_free(struct reiserfs_security_handle *sec)
96{
97 kfree(sec->name);
98 kfree(sec->value);
99 sec->name = NULL;
100 sec->value = NULL;
101}
102
Stephen Hemminger94d09a92010-05-13 17:53:19 -0700103const struct xattr_handler reiserfs_xattr_security_handler = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .prefix = XATTR_SECURITY_PREFIX,
105 .get = security_get,
106 .set = security_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .list = security_list,
108};