Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3 | * linux/fs/ext4/xattr_security.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4 | * Handler for storing security labels as extended attributes. |
| 5 | */ |
| 6 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 7 | #include <linux/string.h> |
| 8 | #include <linux/fs.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 9 | #include <linux/security.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 11 | #include "ext4_jbd2.h" |
| 12 | #include "ext4.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 13 | #include "xattr.h" |
| 14 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 15 | static int |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 16 | ext4_xattr_security_get(const struct xattr_handler *handler, |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 17 | struct dentry *unused, struct inode *inode, |
| 18 | const char *name, void *buffer, size_t size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 19 | { |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 20 | return ext4_xattr_get(inode, EXT4_XATTR_INDEX_SECURITY, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 21 | name, buffer, size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | static int |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 25 | ext4_xattr_security_set(const struct xattr_handler *handler, |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 26 | struct user_namespace *mnt_userns, |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 27 | struct dentry *unused, struct inode *inode, |
| 28 | const char *name, const void *value, |
| 29 | size_t size, int flags) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 30 | { |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 31 | return ext4_xattr_set(inode, EXT4_XATTR_INDEX_SECURITY, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 32 | name, value, size, flags); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Djalal Harouni | 176576d | 2012-01-04 22:32:12 -0500 | [diff] [blame] | 35 | static int |
| 36 | ext4_initxattrs(struct inode *inode, const struct xattr *xattr_array, |
| 37 | void *fs_info) |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 38 | { |
| 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 Guan | 3f706c8 | 2018-05-10 11:52:14 -0400 | [diff] [blame] | 47 | xattr->value_len, XATTR_CREATE); |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 48 | if (err < 0) |
| 49 | break; |
| 50 | } |
| 51 | return err; |
| 52 | } |
| 53 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 54 | int |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 55 | ext4_init_security(handle_t *handle, struct inode *inode, struct inode *dir, |
| 56 | const struct qstr *qstr) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 57 | { |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 58 | return security_inode_init_security(inode, dir, qstr, |
| 59 | &ext4_initxattrs, handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Stephen Hemminger | 11e2752 | 2010-05-13 17:53:18 -0700 | [diff] [blame] | 62 | const struct xattr_handler ext4_xattr_security_handler = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 63 | .prefix = XATTR_SECURITY_PREFIX, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 64 | .get = ext4_xattr_security_get, |
| 65 | .set = ext4_xattr_security_set, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 66 | }; |