mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-12-04 01:24:12 +08:00
7b2f6c3066
Use the actual length of volume coherency data when setting the
xattr to avoid the following KASAN report.
BUG: KASAN: slab-out-of-bounds in cachefiles_set_volume_xattr+0xa0/0x350 [cachefiles]
Write of size 4 at addr ffff888101e02af4 by task kworker/6:0/1347
CPU: 6 PID: 1347 Comm: kworker/6:0 Kdump: loaded Not tainted 5.18.0-rc1-nfs-fscache-netfs+ #13
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.14.0-4.fc34 04/01/2014
Workqueue: events fscache_create_volume_work [fscache]
Call Trace:
<TASK>
dump_stack_lvl+0x45/0x5a
print_report.cold+0x5e/0x5db
? __lock_text_start+0x8/0x8
? cachefiles_set_volume_xattr+0xa0/0x350 [cachefiles]
kasan_report+0xab/0x120
? cachefiles_set_volume_xattr+0xa0/0x350 [cachefiles]
kasan_check_range+0xf5/0x1d0
memcpy+0x39/0x60
cachefiles_set_volume_xattr+0xa0/0x350 [cachefiles]
cachefiles_acquire_volume+0x2be/0x500 [cachefiles]
? __cachefiles_free_volume+0x90/0x90 [cachefiles]
fscache_create_volume_work+0x68/0x160 [fscache]
process_one_work+0x3b7/0x6a0
worker_thread+0x2c4/0x650
? process_one_work+0x6a0/0x6a0
kthread+0x16c/0x1a0
? kthread_complete_and_exit+0x20/0x20
ret_from_fork+0x22/0x30
</TASK>
Allocated by task 1347:
kasan_save_stack+0x1e/0x40
__kasan_kmalloc+0x81/0xa0
cachefiles_set_volume_xattr+0x76/0x350 [cachefiles]
cachefiles_acquire_volume+0x2be/0x500 [cachefiles]
fscache_create_volume_work+0x68/0x160 [fscache]
process_one_work+0x3b7/0x6a0
worker_thread+0x2c4/0x650
kthread+0x16c/0x1a0
ret_from_fork+0x22/0x30
The buggy address belongs to the object at ffff888101e02af0
which belongs to the cache kmalloc-8 of size 8
The buggy address is located 4 bytes inside of
8-byte region [ffff888101e02af0, ffff888101e02af8)
The buggy address belongs to the physical page:
page:00000000a2292d70 refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x101e02
flags: 0x17ffffc0000200(slab|node=0|zone=2|lastcpupid=0x1fffff)
raw: 0017ffffc0000200 0000000000000000 dead000000000001 ffff888100042280
raw: 0000000000000000 0000000080660066 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
Memory state around the buggy address:
ffff888101e02980: fc 00 fc fc fc fc 00 fc fc fc fc 00 fc fc fc fc
ffff888101e02a00: 00 fc fc fc fc 00 fc fc fc fc 00 fc fc fc fc 00
>ffff888101e02a80: fc fc fc fc 00 fc fc fc fc 00 fc fc fc fc 04 fc
^
ffff888101e02b00: fc fc fc 00 fc fc fc fc 00 fc fc fc fc 00 fc fc
ffff888101e02b80: fc fc 00 fc fc fc fc 00 fc fc fc fc 00 fc fc fc
==================================================================
Fixes: 413a4a6b0b
"cachefiles: Fix volume coherency attribute"
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
Signed-off-by: David Howells <dhowells@redhat.com>
cc: linux-cachefs@redhat.com
Link: https://lore.kernel.org/r/20220405134649.6579-1-dwysocha@redhat.com/ # v1
Link: https://lore.kernel.org/r/20220405142810.8208-1-dwysocha@redhat.com/ # Incorrect v2
277 lines
7.7 KiB
C
277 lines
7.7 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/* CacheFiles extended attribute management
|
|
*
|
|
* Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/file.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/fsnotify.h>
|
|
#include <linux/quotaops.h>
|
|
#include <linux/xattr.h>
|
|
#include <linux/slab.h>
|
|
#include "internal.h"
|
|
|
|
#define CACHEFILES_COOKIE_TYPE_DATA 1
|
|
|
|
struct cachefiles_xattr {
|
|
__be64 object_size; /* Actual size of the object */
|
|
__be64 zero_point; /* Size after which server has no data not written by us */
|
|
__u8 type; /* Type of object */
|
|
__u8 content; /* Content presence (enum cachefiles_content) */
|
|
__u8 data[]; /* netfs coherency data */
|
|
} __packed;
|
|
|
|
static const char cachefiles_xattr_cache[] =
|
|
XATTR_USER_PREFIX "CacheFiles.cache";
|
|
|
|
struct cachefiles_vol_xattr {
|
|
__be32 reserved; /* Reserved, should be 0 */
|
|
__u8 data[]; /* netfs volume coherency data */
|
|
} __packed;
|
|
|
|
/*
|
|
* set the state xattr on a cache file
|
|
*/
|
|
int cachefiles_set_object_xattr(struct cachefiles_object *object)
|
|
{
|
|
struct cachefiles_xattr *buf;
|
|
struct dentry *dentry;
|
|
struct file *file = object->file;
|
|
unsigned int len = object->cookie->aux_len;
|
|
int ret;
|
|
|
|
if (!file)
|
|
return -ESTALE;
|
|
dentry = file->f_path.dentry;
|
|
|
|
_enter("%x,#%d", object->debug_id, len);
|
|
|
|
buf = kmalloc(sizeof(struct cachefiles_xattr) + len, GFP_KERNEL);
|
|
if (!buf)
|
|
return -ENOMEM;
|
|
|
|
buf->object_size = cpu_to_be64(object->cookie->object_size);
|
|
buf->zero_point = 0;
|
|
buf->type = CACHEFILES_COOKIE_TYPE_DATA;
|
|
buf->content = object->content_info;
|
|
if (test_bit(FSCACHE_COOKIE_LOCAL_WRITE, &object->cookie->flags))
|
|
buf->content = CACHEFILES_CONTENT_DIRTY;
|
|
if (len > 0)
|
|
memcpy(buf->data, fscache_get_aux(object->cookie), len);
|
|
|
|
ret = cachefiles_inject_write_error();
|
|
if (ret == 0)
|
|
ret = vfs_setxattr(&init_user_ns, dentry, cachefiles_xattr_cache,
|
|
buf, sizeof(struct cachefiles_xattr) + len, 0);
|
|
if (ret < 0) {
|
|
trace_cachefiles_vfs_error(object, file_inode(file), ret,
|
|
cachefiles_trace_setxattr_error);
|
|
trace_cachefiles_coherency(object, file_inode(file)->i_ino,
|
|
buf->content,
|
|
cachefiles_coherency_set_fail);
|
|
if (ret != -ENOMEM)
|
|
cachefiles_io_error_obj(
|
|
object,
|
|
"Failed to set xattr with error %d", ret);
|
|
} else {
|
|
trace_cachefiles_coherency(object, file_inode(file)->i_ino,
|
|
buf->content,
|
|
cachefiles_coherency_set_ok);
|
|
}
|
|
|
|
kfree(buf);
|
|
_leave(" = %d", ret);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* check the consistency between the backing cache and the FS-Cache cookie
|
|
*/
|
|
int cachefiles_check_auxdata(struct cachefiles_object *object, struct file *file)
|
|
{
|
|
struct cachefiles_xattr *buf;
|
|
struct dentry *dentry = file->f_path.dentry;
|
|
unsigned int len = object->cookie->aux_len, tlen;
|
|
const void *p = fscache_get_aux(object->cookie);
|
|
enum cachefiles_coherency_trace why;
|
|
ssize_t xlen;
|
|
int ret = -ESTALE;
|
|
|
|
tlen = sizeof(struct cachefiles_xattr) + len;
|
|
buf = kmalloc(tlen, GFP_KERNEL);
|
|
if (!buf)
|
|
return -ENOMEM;
|
|
|
|
xlen = cachefiles_inject_read_error();
|
|
if (xlen == 0)
|
|
xlen = vfs_getxattr(&init_user_ns, dentry, cachefiles_xattr_cache, buf, tlen);
|
|
if (xlen != tlen) {
|
|
if (xlen < 0)
|
|
trace_cachefiles_vfs_error(object, file_inode(file), xlen,
|
|
cachefiles_trace_getxattr_error);
|
|
if (xlen == -EIO)
|
|
cachefiles_io_error_obj(
|
|
object,
|
|
"Failed to read aux with error %zd", xlen);
|
|
why = cachefiles_coherency_check_xattr;
|
|
} else if (buf->type != CACHEFILES_COOKIE_TYPE_DATA) {
|
|
why = cachefiles_coherency_check_type;
|
|
} else if (memcmp(buf->data, p, len) != 0) {
|
|
why = cachefiles_coherency_check_aux;
|
|
} else if (be64_to_cpu(buf->object_size) != object->cookie->object_size) {
|
|
why = cachefiles_coherency_check_objsize;
|
|
} else if (buf->content == CACHEFILES_CONTENT_DIRTY) {
|
|
// TODO: Begin conflict resolution
|
|
pr_warn("Dirty object in cache\n");
|
|
why = cachefiles_coherency_check_dirty;
|
|
} else {
|
|
why = cachefiles_coherency_check_ok;
|
|
ret = 0;
|
|
}
|
|
|
|
trace_cachefiles_coherency(object, file_inode(file)->i_ino,
|
|
buf->content, why);
|
|
kfree(buf);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* remove the object's xattr to mark it stale
|
|
*/
|
|
int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
|
|
struct cachefiles_object *object,
|
|
struct dentry *dentry)
|
|
{
|
|
int ret;
|
|
|
|
ret = cachefiles_inject_remove_error();
|
|
if (ret == 0)
|
|
ret = vfs_removexattr(&init_user_ns, dentry, cachefiles_xattr_cache);
|
|
if (ret < 0) {
|
|
trace_cachefiles_vfs_error(object, d_inode(dentry), ret,
|
|
cachefiles_trace_remxattr_error);
|
|
if (ret == -ENOENT || ret == -ENODATA)
|
|
ret = 0;
|
|
else if (ret != -ENOMEM)
|
|
cachefiles_io_error(cache,
|
|
"Can't remove xattr from %lu"
|
|
" (error %d)",
|
|
d_backing_inode(dentry)->i_ino, -ret);
|
|
}
|
|
|
|
_leave(" = %d", ret);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Stick a marker on the cache object to indicate that it's dirty.
|
|
*/
|
|
void cachefiles_prepare_to_write(struct fscache_cookie *cookie)
|
|
{
|
|
const struct cred *saved_cred;
|
|
struct cachefiles_object *object = cookie->cache_priv;
|
|
struct cachefiles_cache *cache = object->volume->cache;
|
|
|
|
_enter("c=%08x", object->cookie->debug_id);
|
|
|
|
if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) {
|
|
cachefiles_begin_secure(cache, &saved_cred);
|
|
cachefiles_set_object_xattr(object);
|
|
cachefiles_end_secure(cache, saved_cred);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Set the state xattr on a volume directory.
|
|
*/
|
|
bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume)
|
|
{
|
|
struct cachefiles_vol_xattr *buf;
|
|
unsigned int len = volume->vcookie->coherency_len;
|
|
const void *p = volume->vcookie->coherency;
|
|
struct dentry *dentry = volume->dentry;
|
|
int ret;
|
|
|
|
_enter("%x,#%d", volume->vcookie->debug_id, len);
|
|
|
|
len += sizeof(*buf);
|
|
buf = kmalloc(len, GFP_KERNEL);
|
|
if (!buf)
|
|
return false;
|
|
buf->reserved = cpu_to_be32(0);
|
|
memcpy(buf->data, p, volume->vcookie->coherency_len);
|
|
|
|
ret = cachefiles_inject_write_error();
|
|
if (ret == 0)
|
|
ret = vfs_setxattr(&init_user_ns, dentry, cachefiles_xattr_cache,
|
|
buf, len, 0);
|
|
if (ret < 0) {
|
|
trace_cachefiles_vfs_error(NULL, d_inode(dentry), ret,
|
|
cachefiles_trace_setxattr_error);
|
|
trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino,
|
|
cachefiles_coherency_vol_set_fail);
|
|
if (ret != -ENOMEM)
|
|
cachefiles_io_error(
|
|
volume->cache, "Failed to set xattr with error %d", ret);
|
|
} else {
|
|
trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino,
|
|
cachefiles_coherency_vol_set_ok);
|
|
}
|
|
|
|
kfree(buf);
|
|
_leave(" = %d", ret);
|
|
return ret == 0;
|
|
}
|
|
|
|
/*
|
|
* Check the consistency between the backing cache and the volume cookie.
|
|
*/
|
|
int cachefiles_check_volume_xattr(struct cachefiles_volume *volume)
|
|
{
|
|
struct cachefiles_vol_xattr *buf;
|
|
struct dentry *dentry = volume->dentry;
|
|
unsigned int len = volume->vcookie->coherency_len;
|
|
const void *p = volume->vcookie->coherency;
|
|
enum cachefiles_coherency_trace why;
|
|
ssize_t xlen;
|
|
int ret = -ESTALE;
|
|
|
|
_enter("");
|
|
|
|
len += sizeof(*buf);
|
|
buf = kmalloc(len, GFP_KERNEL);
|
|
if (!buf)
|
|
return -ENOMEM;
|
|
|
|
xlen = cachefiles_inject_read_error();
|
|
if (xlen == 0)
|
|
xlen = vfs_getxattr(&init_user_ns, dentry, cachefiles_xattr_cache, buf, len);
|
|
if (xlen != len) {
|
|
if (xlen < 0) {
|
|
trace_cachefiles_vfs_error(NULL, d_inode(dentry), xlen,
|
|
cachefiles_trace_getxattr_error);
|
|
if (xlen == -EIO)
|
|
cachefiles_io_error(
|
|
volume->cache,
|
|
"Failed to read xattr with error %zd", xlen);
|
|
}
|
|
why = cachefiles_coherency_vol_check_xattr;
|
|
} else if (buf->reserved != cpu_to_be32(0)) {
|
|
why = cachefiles_coherency_vol_check_resv;
|
|
} else if (memcmp(buf->data, p, len - sizeof(*buf)) != 0) {
|
|
why = cachefiles_coherency_vol_check_cmp;
|
|
} else {
|
|
why = cachefiles_coherency_vol_check_ok;
|
|
ret = 0;
|
|
}
|
|
|
|
trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino, why);
|
|
kfree(buf);
|
|
_leave(" = %d", ret);
|
|
return ret;
|
|
}
|