1141 lines · 26.8 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* * Pocessing of EA's * * Part of this file is based on code from the NTFS-3G. * * Copyright (c) 2014-2021 Jean-Pierre Andre * Copyright (c) 2025 LG Electronics Co., Ltd. */ #include <linux/fs.h>#include <linux/posix_acl.h>#include <linux/posix_acl_xattr.h>#include <linux/xattr.h> #include "layout.h"#include "attrib.h"#include "index.h"#include "dir.h"#include "ea.h" static int ntfs_write_ea(struct ntfs_inode *ni, __le32 type, char *value, s64 ea_off,		s64 ea_size, bool need_truncate){	struct inode *ea_vi;	int err = 0;	s64 written; 	ea_vi = ntfs_attr_iget(VFS_I(ni), type, AT_UNNAMED, 0);	if (IS_ERR(ea_vi))		return PTR_ERR(ea_vi); 	written = ntfs_inode_attr_pwrite(ea_vi, ea_off, ea_size, value, false);	if (written != ea_size)		err = -EIO;	else {		struct ntfs_inode *ea_ni = NTFS_I(ea_vi); 		if (need_truncate && ea_ni->data_size > ea_off + ea_size)			ntfs_attr_truncate(ea_ni, ea_off + ea_size);		mark_mft_record_dirty(ni);	} 	iput(ea_vi);	return err;} static int ntfs_ea_lookup(char *ea_buf, s64 ea_buf_size, const char *name,			  int name_len, s64 *ea_offset, s64 *ea_size){	const struct ea_attr *p_ea;	size_t actual_size;	loff_t offset, p_ea_size;	unsigned int next; 	offset = 0;	do {		if (ea_buf_size - offset < sizeof(struct ea_attr))			break; 		p_ea = (const struct ea_attr *)&ea_buf[offset];		next = le32_to_cpu(p_ea->next_entry_offset);		p_ea_size = next ? next : (ea_buf_size - offset); 		if (p_ea_size < sizeof(struct ea_attr) ||		    offset + p_ea_size > ea_buf_size)			break; 		if ((s64)p_ea->ea_name_length + 1 >		    p_ea_size - offsetof(struct ea_attr, ea_name))			break; 		actual_size = ALIGN(struct_size(p_ea, ea_name, 1 + p_ea->ea_name_length +					le16_to_cpu(p_ea->ea_value_length)), 4);		if (actual_size > p_ea_size)			break; 		if (p_ea->ea_name_length == name_len &&		    !memcmp(p_ea->ea_name, name, name_len)) {			*ea_offset = offset;			*ea_size = next ? next : actual_size; 			if (ea_buf_size < *ea_offset + *ea_size)				goto out; 			return 0;		}		offset += next;	} while (next > 0 && offset < ea_buf_size); out:	return -ENOENT;} /* * Return the existing EA * * The EA_INFORMATION is not examined and the consistency of the * existing EA is not checked. * * If successful, the full attribute is returned unchanged * and its size is returned. * If the designated buffer is too small, the needed size is * returned, and the buffer is left unchanged. * If there is an error, a negative value is returned and errno * is set according to the error. */static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len,		void *buffer, size_t size){	struct ntfs_inode *ni = NTFS_I(inode);	const struct ea_attr *p_ea;	char *ea_buf;	s64 ea_off, ea_size, all_ea_size, ea_info_size;	int err;	u32 ea_info_qlen;	u16 ea_value_len;	struct ea_information *p_ea_info; 	if (!NInoHasEA(ni))		return -ENODATA; 	p_ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0,			&ea_info_size);	if (IS_ERR(p_ea_info))		return PTR_ERR(p_ea_info);	if (ea_info_size != sizeof(struct ea_information)) {		kvfree(p_ea_info);		return -EIO;	} 	ea_info_qlen = le32_to_cpu(p_ea_info->ea_query_length);	kvfree(p_ea_info); 	ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &all_ea_size);	if (IS_ERR(ea_buf))		return PTR_ERR(ea_buf); 	if (ea_info_qlen > all_ea_size) {		err = -EIO;		goto free_ea_buf;	} 	err = ntfs_ea_lookup(ea_buf, ea_info_qlen, name, name_len, &ea_off,			&ea_size);	if (!err) {		p_ea = (struct ea_attr *)&ea_buf[ea_off];		ea_value_len = le16_to_cpu(p_ea->ea_value_length);		if (!buffer) {			kvfree(ea_buf);			return ea_value_len;		} 		if (ea_value_len > size) {			err = -ERANGE;			goto free_ea_buf;		} 		memcpy(buffer, &p_ea->ea_name[p_ea->ea_name_length + 1],				ea_value_len);		kvfree(ea_buf);		return ea_value_len;	} 	err = -ENODATA;free_ea_buf:	kvfree(ea_buf);	return err;} static inline int ea_packed_size(const struct ea_attr *p_ea){	/*	 * 4 bytes for header (flags and lengths) + name length + 1 +	 * value length.	 */	return 5 + p_ea->ea_name_length + le16_to_cpu(p_ea->ea_value_length);} /* * Set a new EA, and set EA_INFORMATION accordingly * * This is roughly the same as ZwSetEaFile() on Windows, however * the "offset to next" of the last EA should not be cleared. * * Consistency of the new EA is first checked. * * EA_INFORMATION is set first, and it is restored to its former * state if setting EA fails. */static int ntfs_set_ea(struct inode *inode, const char *name, size_t name_len,		const void *value, size_t val_size, int flags,		__le16 *packed_ea_size){	struct ntfs_inode *ni = NTFS_I(inode);	struct ea_information *p_ea_info = NULL;	int ea_packed, err = 0;	struct ea_attr *p_ea;	u32 ea_info_qsize = 0;	char *ea_buf = NULL;	char *new_ea_buf;	char *old_ea_buf = NULL;	struct ea_information old_ea_info;	size_t new_ea_size = ALIGN(struct_size(p_ea, ea_name, 1 + name_len + val_size), 4);	s64 ea_off, ea_info_size, all_ea_size, ea_size; 	if (name_len > 255)		return -ENAMETOOLONG; 	if (ntfs_attr_exist(ni, AT_EA_INFORMATION, AT_UNNAMED, 0)) {		p_ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0,						&ea_info_size);		if (IS_ERR(p_ea_info)) {			err = PTR_ERR(p_ea_info);			p_ea_info = NULL;			goto out;		}		if (ea_info_size != sizeof(struct ea_information)) {			err = -EIO;			goto out;		} 		ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &all_ea_size);		if (IS_ERR(ea_buf)) {			err = PTR_ERR(ea_buf);			ea_buf = NULL;			goto out;		}		if (!ea_buf) {			ea_info_qsize = 0;			kvfree(p_ea_info);			goto create_ea_info;		} 		ea_info_qsize = le32_to_cpu(p_ea_info->ea_query_length);	} else {create_ea_info:		p_ea_info = kzalloc_obj(struct ea_information, GFP_NOFS);		if (!p_ea_info)			return -ENOMEM; 		ea_info_qsize = 0;		err = ntfs_attr_add(ni, AT_EA_INFORMATION, AT_UNNAMED, 0,				(char *)p_ea_info, sizeof(struct ea_information));		if (err)			goto out; 		if (ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) {			err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0);			if (err)				goto out;		} 		goto alloc_new_ea;	} 	if (ea_info_qsize > all_ea_size) {		err = -EIO;		goto out;	} 	err = ntfs_ea_lookup(ea_buf, ea_info_qsize, name, name_len, &ea_off,			&ea_size);	if (ea_info_qsize && !err) {		if (flags & XATTR_CREATE) {			err = -EEXIST;			goto out;		}		if ((flags & XATTR_REPLACE) && !val_size) {			old_ea_info = *p_ea_info;			old_ea_buf = kvmemdup(ea_buf, all_ea_size, GFP_NOFS);			if (!old_ea_buf) {				err = -ENOMEM;				goto out;			}		} 		/* Check the final $EA size before removing the old entry. */		if (val_size &&		    ntfs_attr_size_bounds_check(ni->vol, AT_EA,					ea_info_qsize - ea_size + new_ea_size)) {			err = -EFBIG;			goto out;		} 		p_ea = (struct ea_attr *)(ea_buf + ea_off); 		if (val_size &&		    le16_to_cpu(p_ea->ea_value_length) == val_size &&		    !memcmp(p_ea->ea_name + p_ea->ea_name_length + 1, value,			    val_size))			goto out; 		le16_add_cpu(&p_ea_info->ea_length, 0 - ea_packed_size(p_ea)); 		if (p_ea->flags & NEED_EA)			le16_add_cpu(&p_ea_info->need_ea_count, -1); 		memmove((char *)p_ea, (char *)p_ea + ea_size, ea_info_qsize - (ea_off + ea_size));		ea_info_qsize -= ea_size;		p_ea_info->ea_query_length = cpu_to_le32(ea_info_qsize); 		if ((flags & XATTR_REPLACE) && !val_size && !ea_info_qsize) {			err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0);			if (err)				goto out; 			err = ntfs_attr_remove(ni, AT_EA_INFORMATION, AT_UNNAMED, 0);			if (err) {				/* Restore the original $EA if $EA_INFORMATION removal failed. */				ntfs_attr_add(ni, AT_EA, AT_UNNAMED, 0, old_ea_buf,					      all_ea_size);				ea_info_qsize = le32_to_cpu(old_ea_info.ea_query_length);			}			goto out;		} 		if ((flags & XATTR_REPLACE) && !val_size) {			err = ntfs_write_ea(ni, AT_EA, ea_buf, 0, ea_info_qsize,					true);			if (err) {				ntfs_write_ea(ni, AT_EA, old_ea_buf, 0,					      all_ea_size, false);				goto out;			} 			err = ntfs_write_ea(ni, AT_EA_INFORMATION, (char *)p_ea_info,					0, sizeof(struct ea_information), false);			if (err) {				ntfs_write_ea(ni, AT_EA, old_ea_buf, 0,					      all_ea_size, false);				ntfs_write_ea(ni, AT_EA_INFORMATION,					      (char *)&old_ea_info, 0,					      sizeof(old_ea_info), false);			}			goto out;		}	} else {		if (flags & XATTR_REPLACE) {			err = -ENODATA;			goto out;		} 		if (ntfs_attr_size_bounds_check(ni->vol, AT_EA,					ea_info_qsize + new_ea_size)) {			err = -EFBIG;			goto out;		}	}alloc_new_ea:	new_ea_buf = kvzalloc(ea_info_qsize + new_ea_size, GFP_NOFS);	if (!new_ea_buf) {		err = -ENOMEM;		goto out;	}	if (ea_info_qsize)		memcpy(new_ea_buf, ea_buf, ea_info_qsize);	kvfree(ea_buf);	ea_buf = new_ea_buf;	p_ea = (struct ea_attr *)(ea_buf + ea_info_qsize); 	/*	 * EA and REPARSE_POINT compatibility not checked any more,	 * required by Windows 10, but having both may lead to	 * problems with earlier versions.	 */	memcpy(p_ea->ea_name, name, name_len);	p_ea->ea_name_length = name_len;	p_ea->ea_name[name_len] = 0;	memcpy(p_ea->ea_name + name_len + 1, value, val_size);	p_ea->ea_value_length = cpu_to_le16(val_size);	p_ea->next_entry_offset = cpu_to_le32(new_ea_size); 	ea_packed = le16_to_cpu(p_ea_info->ea_length) + ea_packed_size(p_ea);	p_ea_info->ea_length = cpu_to_le16(ea_packed);	p_ea_info->ea_query_length = cpu_to_le32(ea_info_qsize + new_ea_size); 	if (ea_packed > 0xffff) {		err = -EFBIG;		goto out;	} 	/*	 * no EA or EA_INFORMATION : add them	 */	if (!ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) {		err = ntfs_attr_add(ni, AT_EA, AT_UNNAMED, 0, ea_buf,				ea_info_qsize + new_ea_size);		if (err)			goto out;	} else {		err = ntfs_write_ea(ni, AT_EA, ea_buf, 0,				ea_info_qsize + new_ea_size, true);		if (err)			goto out;	} 	err = ntfs_write_ea(ni, AT_EA_INFORMATION, (char *)p_ea_info, 0,			sizeof(struct ea_information), false);	if (err)		goto out; 	if (packed_ea_size)		*packed_ea_size = p_ea_info->ea_length;	mark_mft_record_dirty(ni);out:	if (!err) {		if (ea_info_qsize > 0)			NInoSetHasEA(ni);		else			NInoClearHasEA(ni);	} 	kvfree(ea_buf);	kvfree(old_ea_buf);	kvfree(p_ea_info); 	return err;} /* * Check for the presence of an EA "$LXDEV" (used by WSL) * and return its value as a device address */int ntfs_ea_get_wsl_inode(struct inode *inode, dev_t *rdevp, unsigned int flags,			  bool *has_lxmod){	int err;	__le32 v; 	*has_lxmod = false; 	if (!(flags & NTFS_VOL_UID)) {		/* Load uid to lxuid EA */		err = ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &v,				sizeof(v));		if (err == sizeof(v))			i_uid_write(inode, le32_to_cpu(v));	} 	if (!(flags & NTFS_VOL_GID)) {		/* Load gid to lxgid EA */		err = ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &v,				sizeof(v));		if (err == sizeof(v))			i_gid_write(inode, le32_to_cpu(v));	} 	/* Load mode to lxmod EA */	err = ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &v, sizeof(v));	if (err == sizeof(v)) {		inode->i_mode = le32_to_cpu(v);		*has_lxmod = true;	} else {		/* Everyone gets all permissions. */		inode->i_mode |= 0777;	} 	/* Load mode to lxdev EA */	err = ntfs_get_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &v, sizeof(v));	if (err == sizeof(v))		*rdevp = le32_to_cpu(v);	err = 0; 	return err;} int ntfs_ea_set_wsl_inode(struct inode *inode, dev_t rdev, __le16 *ea_size,		unsigned int flags){	__le32 v;	int err = 0; 	if (ea_size)		*ea_size = 0; 	if (flags & NTFS_EA_UID) {		/* Store uid to lxuid EA */		v = cpu_to_le32(i_uid_read(inode));		err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &v,				sizeof(v), 0, ea_size);		if (err)			return err;	} 	if (flags & NTFS_EA_GID) {		/* Store gid to lxgid EA */		v = cpu_to_le32(i_gid_read(inode));		err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &v,				sizeof(v), 0, ea_size);		if (err)			return err;	} 	if (flags & NTFS_EA_MODE) {		/* Store mode to lxmod EA */		v = cpu_to_le32(inode->i_mode);		err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &v,				sizeof(v), 0, ea_size);		if (err)

Lines 1–500 of 1141.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t