^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/ext4/xattr_security.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Handler for storing security labels as extended attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "ext4_jbd2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "ext4.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) ext4_xattr_security_get(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct dentry *unused, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) const char *name, void *buffer, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return ext4_xattr_get(inode, EXT4_XATTR_INDEX_SECURITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) name, buffer, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) ext4_xattr_security_set(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct dentry *unused, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) const char *name, const void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return ext4_xattr_set(inode, EXT4_XATTR_INDEX_SECURITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) name, value, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ext4_initxattrs(struct inode *inode, const struct xattr *xattr_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) void *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) const struct xattr *xattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) handle_t *handle = fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) for (xattr = xattr_array; xattr->name != NULL; xattr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) err = ext4_xattr_set_handle(handle, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) EXT4_XATTR_INDEX_SECURITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) xattr->name, xattr->value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) xattr->value_len, XATTR_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ext4_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) const struct qstr *qstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return security_inode_init_security(inode, dir, qstr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) &ext4_initxattrs, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) const struct xattr_handler ext4_xattr_security_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .prefix = XATTR_SECURITY_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) .get = ext4_xattr_security_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .set = ext4_xattr_security_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) };