blob: 8213f66f7b2dcb8472153a7b0799622bf913e72f [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,
Christian Braunere65ce2a2021-01-21 14:19:27 +010026 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -040027 struct dentry *unused, struct inode *inode,
28 const char *name, const void *value,
29 size_t size, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030{
Al Viro59301222016-05-27 10:19:30 -040031 return ext4_xattr_set(inode, EXT4_XATTR_INDEX_SECURITY,
Christoph Hellwig431547b2009-11-13 09:52:56 +000032 name, value, size, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070033}
34
Djalal Harouni176576d2012-01-04 22:32:12 -050035static int
36ext4_initxattrs(struct inode *inode, const struct xattr *xattr_array,
37 void *fs_info)
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040038{
39 const struct xattr *xattr;
40 handle_t *handle = fs_info;
41 int err = 0;
42
43 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
44 err = ext4_xattr_set_handle(handle, inode,
45 EXT4_XATTR_INDEX_SECURITY,
46 xattr->name, xattr->value,
Eryu Guan3f706c82018-05-10 11:52:14 -040047 xattr->value_len, XATTR_CREATE);
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040048 if (err < 0)
49 break;
50 }
51 return err;
52}
53
Dave Kleikampac27a0e2006-10-11 01:20:50 -070054int
Eric Paris2a7dba32011-02-01 11:05:39 -050055ext4_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
56 const struct qstr *qstr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070057{
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040058 return security_inode_init_security(inode, dir, qstr,
59 &ext4_initxattrs, handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070060}
61
Stephen Hemminger11e27522010-05-13 17:53:18 -070062const struct xattr_handler ext4_xattr_security_handler = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -070063 .prefix = XATTR_SECURITY_PREFIX,
Mingming Cao617ba132006-10-11 01:20:53 -070064 .get = ext4_xattr_security_get,
65 .set = ext4_xattr_security_set,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070066};