18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * linux/fs/hfsplus/xattr_trusted.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Vyacheslav Dubeyko <slava@dubeyko.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Handler for storing security labels as extended attributes.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/security.h>
118c2ecf20Sopenharmony_ci#include <linux/nls.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "hfsplus_fs.h"
148c2ecf20Sopenharmony_ci#include "xattr.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic int hfsplus_security_getxattr(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 hfsplus_getxattr(inode, name, buffer, size,
218c2ecf20Sopenharmony_ci				XATTR_SECURITY_PREFIX,
228c2ecf20Sopenharmony_ci				XATTR_SECURITY_PREFIX_LEN);
238c2ecf20Sopenharmony_ci}
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic int hfsplus_security_setxattr(const struct xattr_handler *handler,
268c2ecf20Sopenharmony_ci				     struct dentry *unused, struct inode *inode,
278c2ecf20Sopenharmony_ci				     const char *name, const void *buffer,
288c2ecf20Sopenharmony_ci				     size_t size, int flags)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	return hfsplus_setxattr(inode, name, buffer, size, flags,
318c2ecf20Sopenharmony_ci				XATTR_SECURITY_PREFIX,
328c2ecf20Sopenharmony_ci				XATTR_SECURITY_PREFIX_LEN);
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic int hfsplus_initxattrs(struct inode *inode,
368c2ecf20Sopenharmony_ci				const struct xattr *xattr_array,
378c2ecf20Sopenharmony_ci				void *fs_info)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	const struct xattr *xattr;
408c2ecf20Sopenharmony_ci	char *xattr_name;
418c2ecf20Sopenharmony_ci	int err = 0;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
448c2ecf20Sopenharmony_ci		GFP_KERNEL);
458c2ecf20Sopenharmony_ci	if (!xattr_name)
468c2ecf20Sopenharmony_ci		return -ENOMEM;
478c2ecf20Sopenharmony_ci	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci		if (!strcmp(xattr->name, ""))
508c2ecf20Sopenharmony_ci			continue;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci		strcpy(xattr_name, XATTR_SECURITY_PREFIX);
538c2ecf20Sopenharmony_ci		strcpy(xattr_name +
548c2ecf20Sopenharmony_ci			XATTR_SECURITY_PREFIX_LEN, xattr->name);
558c2ecf20Sopenharmony_ci		memset(xattr_name +
568c2ecf20Sopenharmony_ci			XATTR_SECURITY_PREFIX_LEN + strlen(xattr->name), 0, 1);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci		err = __hfsplus_setxattr(inode, xattr_name,
598c2ecf20Sopenharmony_ci					xattr->value, xattr->value_len, 0);
608c2ecf20Sopenharmony_ci		if (err)
618c2ecf20Sopenharmony_ci			break;
628c2ecf20Sopenharmony_ci	}
638c2ecf20Sopenharmony_ci	kfree(xattr_name);
648c2ecf20Sopenharmony_ci	return err;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ciint hfsplus_init_security(struct inode *inode, struct inode *dir,
688c2ecf20Sopenharmony_ci				const struct qstr *qstr)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	return security_inode_init_security(inode, dir, qstr,
718c2ecf20Sopenharmony_ci					&hfsplus_initxattrs, NULL);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ciconst struct xattr_handler hfsplus_xattr_security_handler = {
758c2ecf20Sopenharmony_ci	.prefix	= XATTR_SECURITY_PREFIX,
768c2ecf20Sopenharmony_ci	.get	= hfsplus_security_getxattr,
778c2ecf20Sopenharmony_ci	.set	= hfsplus_security_setxattr,
788c2ecf20Sopenharmony_ci};
79