blob: 197a9d8a15efe6822a2d55052005b4a23d767b31 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/xattr_security.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 * Handler for storing security labels as extended attributes.
5 */
6
Dave Kleikampac27a0e2006-10-11 01:20:50 -07007#include <linux/string.h>
8#include <linux/fs.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -07009#include <linux/security.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040011#include "ext4_jbd2.h"
12#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070013#include "xattr.h"
14
Dave Kleikampac27a0e2006-10-11 01:20:50 -070015static int
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020016ext4_xattr_security_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -040017 struct dentry *unused, struct inode *inode,
18 const char *name, void *buffer, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070019{
Al Virob2968212016-04-10 20:48:24 -040020 return ext4_xattr_get(inode, EXT4_XATTR_INDEX_SECURITY,
Christoph Hellwig431547b2009-11-13 09:52:56 +000021 name, buffer, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070022}
23
24static int
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020025ext4_xattr_security_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -040026 struct dentry *unused, struct inode *inode,
27 const char *name, const void *value,
28 size_t size, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029{
Al Viro59301222016-05-27 10:19:30 -040030 return ext4_xattr_set(inode, EXT4_XATTR_INDEX_SECURITY,
Christoph Hellwig431547b2009-11-13 09:52:56 +000031 name, value, size, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070032}
33
Djalal Harouni176576d2012-01-04 22:32:12 -050034static int
35ext4_initxattrs(struct inode *inode, const struct xattr *xattr_array,
36 void *fs_info)
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040037{
38 const struct xattr *xattr;
39 handle_t *handle = fs_info;
40 int err = 0;
41
42 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
43 err = ext4_xattr_set_handle(handle, inode,
44 EXT4_XATTR_INDEX_SECURITY,
45 xattr->name, xattr->value,
Eryu Guan3f706c82018-05-10 11:52:14 -040046 xattr->value_len, XATTR_CREATE);
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040047 if (err < 0)
48 break;
49 }
50 return err;
51}
52
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053int
Eric Paris2a7dba32011-02-01 11:05:39 -050054ext4_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
55 const struct qstr *qstr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070056{
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040057 return security_inode_init_security(inode, dir, qstr,
58 &ext4_initxattrs, handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070059}
60
Stephen Hemminger11e27522010-05-13 17:53:18 -070061const struct xattr_handler ext4_xattr_security_handler = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062 .prefix = XATTR_SECURITY_PREFIX,
Mingming Cao617ba132006-10-11 01:20:53 -070063 .get = ext4_xattr_security_get,
64 .set = ext4_xattr_security_set,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070065};