18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * linux/fs/ext4/xattr_security.c
48c2ecf20Sopenharmony_ci * Handler for storing security labels as extended attributes.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/string.h>
88c2ecf20Sopenharmony_ci#include <linux/fs.h>
98c2ecf20Sopenharmony_ci#include <linux/security.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include "ext4_jbd2.h"
128c2ecf20Sopenharmony_ci#include "ext4.h"
138c2ecf20Sopenharmony_ci#include "xattr.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic int
168c2ecf20Sopenharmony_ciext4_xattr_security_get(const struct xattr_handler *handler,
178c2ecf20Sopenharmony_ci			struct dentry *unused, struct inode *inode,
188c2ecf20Sopenharmony_ci			const char *name, void *buffer, size_t size)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_SECURITY,
218c2ecf20Sopenharmony_ci			      name, buffer, size);
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic int
258c2ecf20Sopenharmony_ciext4_xattr_security_set(const struct xattr_handler *handler,
268c2ecf20Sopenharmony_ci			struct dentry *unused, struct inode *inode,
278c2ecf20Sopenharmony_ci			const char *name, const void *value,
288c2ecf20Sopenharmony_ci			size_t size, int flags)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	return ext4_xattr_set(inode, EXT4_XATTR_INDEX_SECURITY,
318c2ecf20Sopenharmony_ci			      name, value, size, flags);
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic int
358c2ecf20Sopenharmony_ciext4_initxattrs(struct inode *inode, const struct xattr *xattr_array,
368c2ecf20Sopenharmony_ci		void *fs_info)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	const struct xattr *xattr;
398c2ecf20Sopenharmony_ci	handle_t *handle = fs_info;
408c2ecf20Sopenharmony_ci	int err = 0;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
438c2ecf20Sopenharmony_ci		err = ext4_xattr_set_handle(handle, inode,
448c2ecf20Sopenharmony_ci					    EXT4_XATTR_INDEX_SECURITY,
458c2ecf20Sopenharmony_ci					    xattr->name, xattr->value,
468c2ecf20Sopenharmony_ci					    xattr->value_len, XATTR_CREATE);
478c2ecf20Sopenharmony_ci		if (err < 0)
488c2ecf20Sopenharmony_ci			break;
498c2ecf20Sopenharmony_ci	}
508c2ecf20Sopenharmony_ci	return err;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciint
548c2ecf20Sopenharmony_ciext4_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
558c2ecf20Sopenharmony_ci		   const struct qstr *qstr)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	return security_inode_init_security(inode, dir, qstr,
588c2ecf20Sopenharmony_ci					    &ext4_initxattrs, handle);
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ciconst struct xattr_handler ext4_xattr_security_handler = {
628c2ecf20Sopenharmony_ci	.prefix	= XATTR_SECURITY_PREFIX,
638c2ecf20Sopenharmony_ci	.get	= ext4_xattr_security_get,
648c2ecf20Sopenharmony_ci	.set	= ext4_xattr_security_set,
658c2ecf20Sopenharmony_ci};
66