xattr_security.c 2.7 KB
Newer Older
1
#include "reiserfs.h"
L
Linus Torvalds 已提交
2 3 4 5
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <linux/xattr.h>
6
#include <linux/slab.h>
7
#include "xattr.h"
8
#include <linux/security.h>
9
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
10 11

static int
12 13
security_get(const struct xattr_handler *handler, struct dentry *unused,
	     struct inode *inode, const char *name, void *buffer, size_t size)
L
Linus Torvalds 已提交
14
{
15
	if (IS_PRIVATE(inode))
16
		return -EPERM;
L
Linus Torvalds 已提交
17

18
	return reiserfs_xattr_get(inode, xattr_full_name(handler, name),
19
				  buffer, size);
L
Linus Torvalds 已提交
20 21 22
}

static int
23 24 25
security_set(const struct xattr_handler *handler, struct dentry *unused,
	     struct inode *inode, const char *name, const void *buffer,
	     size_t size, int flags)
L
Linus Torvalds 已提交
26
{
27
	if (IS_PRIVATE(inode))
28
		return -EPERM;
L
Linus Torvalds 已提交
29

30
	return reiserfs_xattr_set(inode,
31 32
				  xattr_full_name(handler, name),
				  buffer, size, flags);
L
Linus Torvalds 已提交
33 34
}

35
static bool security_list(struct dentry *dentry)
L
Linus Torvalds 已提交
36
{
37
	return !IS_PRIVATE(d_inode(dentry));
L
Linus Torvalds 已提交
38 39
}

40 41 42 43
/* Initializes the security context for a new inode and returns the number
 * of blocks needed for the transaction. If successful, reiserfs_security
 * must be released using reiserfs_security_free when the caller is done. */
int reiserfs_security_init(struct inode *dir, struct inode *inode,
44
			   const struct qstr *qstr,
45 46 47
			   struct reiserfs_security_handle *sec)
{
	int blocks = 0;
48 49 50 51 52 53 54 55
	int error;

	sec->name = NULL;

	/* Don't add selinux attributes on xattrs - they'll never get used */
	if (IS_PRIVATE(dir))
		return 0;

56 57
	error = security_old_inode_init_security(inode, dir, qstr, &sec->name,
						 &sec->value, &sec->length);
58 59 60 61 62 63 64 65 66 67
	if (error) {
		if (error == -EOPNOTSUPP)
			error = 0;

		sec->name = NULL;
		sec->value = NULL;
		sec->length = 0;
		return error;
	}

68
	if (sec->length && reiserfs_xattrs_initialized(inode->i_sb)) {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		blocks = reiserfs_xattr_jcreate_nblocks(inode) +
			 reiserfs_xattr_nblocks(inode, sec->length);
		/* We don't want to count the directories twice if we have
		 * a default ACL. */
		REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
	}
	return blocks;
}

int reiserfs_security_write(struct reiserfs_transaction_handle *th,
			    struct inode *inode,
			    struct reiserfs_security_handle *sec)
{
	int error;
	if (strlen(sec->name) < sizeof(XATTR_SECURITY_PREFIX))
		return -EINVAL;

	error = reiserfs_xattr_set_handle(th, inode, sec->name, sec->value,
					  sec->length, XATTR_CREATE);
	if (error == -ENODATA || error == -EOPNOTSUPP)
		error = 0;

	return error;
}

void reiserfs_security_free(struct reiserfs_security_handle *sec)
{
	kfree(sec->name);
	kfree(sec->value);
	sec->name = NULL;
	sec->value = NULL;
}

102
const struct xattr_handler reiserfs_xattr_security_handler = {
L
Linus Torvalds 已提交
103 104 105 106 107
	.prefix = XATTR_SECURITY_PREFIX,
	.get = security_get,
	.set = security_set,
	.list = security_list,
};