Skip to content

Commit ae3787d

Browse files
Randall Huanggregkh
authored andcommitted
f2fs: fix to avoid accessing xattr across the boundary
[ Upstream commit 2777e65 ] When we traverse xattr entries via __find_xattr(), if the raw filesystem content is faked or any hardware failure occurs, out-of-bound error can be detected by KASAN. Fix the issue by introducing boundary check. [ 38.402878] c7 1827 BUG: KASAN: slab-out-of-bounds in f2fs_getxattr+0x518/0x68c [ 38.402891] c7 1827 Read of size 4 at addr ffffffc0b6fb35dc by task [ 38.402935] c7 1827 Call trace: [ 38.402952] c7 1827 [<ffffff900809003c>] dump_backtrace+0x0/0x6bc [ 38.402966] c7 1827 [<ffffff9008090030>] show_stack+0x20/0x2c [ 38.402981] c7 1827 [<ffffff900871ab10>] dump_stack+0xfc/0x140 [ 38.402995] c7 1827 [<ffffff9008325c40>] print_address_description+0x80/0x2d8 [ 38.403009] c7 1827 [<ffffff900832629c>] kasan_report_error+0x198/0x1fc [ 38.403022] c7 1827 [<ffffff9008326104>] kasan_report_error+0x0/0x1fc [ 38.403037] c7 1827 [<ffffff9008325000>] __asan_load4+0x1b0/0x1b8 [ 38.403051] c7 1827 [<ffffff90085fcc44>] f2fs_getxattr+0x518/0x68c [ 38.403066] c7 1827 [<ffffff90085fc508>] f2fs_xattr_generic_get+0xb0/0xd0 [ 38.403080] c7 1827 [<ffffff9008395708>] __vfs_getxattr+0x1f4/0x1fc [ 38.403096] c7 1827 [<ffffff9008621bd0>] inode_doinit_with_dentry+0x360/0x938 [ 38.403109] c7 1827 [<ffffff900862d6cc>] selinux_d_instantiate+0x2c/0x38 [ 38.403123] c7 1827 [<ffffff900861b018>] security_d_instantiate+0x68/0x98 [ 38.403136] c7 1827 [<ffffff9008377db8>] d_splice_alias+0x58/0x348 [ 38.403149] c7 1827 [<ffffff900858d16c>] f2fs_lookup+0x608/0x774 [ 38.403163] c7 1827 [<ffffff900835eacc>] lookup_slow+0x1e0/0x2cc [ 38.403177] c7 1827 [<ffffff9008367fe0>] walk_component+0x160/0x520 [ 38.403190] c7 1827 [<ffffff9008369ef4>] path_lookupat+0x110/0x2b4 [ 38.403203] c7 1827 [<ffffff900835dd38>] filename_lookup+0x1d8/0x3a8 [ 38.403216] c7 1827 [<ffffff900835eeb0>] user_path_at_empty+0x54/0x68 [ 38.403229] c7 1827 [<ffffff9008395f44>] SyS_getxattr+0xb4/0x18c [ 38.403241] c7 1827 [<ffffff9008084200>] el0_svc_naked+0x34/0x38 Signed-off-by: Randall Huang <[email protected]> [Jaegeuk Kim: Fix wrong ending boundary] Reviewed-by: Chao Yu <[email protected]> Signed-off-by: Jaegeuk Kim <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 32f26da commit ae3787d

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

fs/f2fs/xattr.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,17 @@ static inline const struct xattr_handler *f2fs_xattr_handler(int index)
205205
return handler;
206206
}
207207

208-
static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
209-
size_t len, const char *name)
208+
static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
209+
void *last_base_addr, int index,
210+
size_t len, const char *name)
210211
{
211212
struct f2fs_xattr_entry *entry;
212213

213214
list_for_each_xattr(entry, base_addr) {
215+
if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
216+
(void *)XATTR_NEXT_ENTRY(entry) > last_base_addr)
217+
return NULL;
218+
214219
if (entry->e_name_index != index)
215220
continue;
216221
if (entry->e_name_len != len)
@@ -300,20 +305,22 @@ static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
300305
const char *name, struct f2fs_xattr_entry **xe,
301306
void **base_addr, int *base_size)
302307
{
303-
void *cur_addr, *txattr_addr, *last_addr = NULL;
308+
void *cur_addr, *txattr_addr, *last_txattr_addr;
309+
void *last_addr = NULL;
304310
nid_t xnid = F2FS_I(inode)->i_xattr_nid;
305-
unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
306311
unsigned int inline_size = inline_xattr_size(inode);
307312
int err = 0;
308313

309-
if (!size && !inline_size)
314+
if (!xnid && !inline_size)
310315
return -ENODATA;
311316

312-
*base_size = inline_size + size + XATTR_PADDING_SIZE;
317+
*base_size = XATTR_SIZE(xnid, inode) + XATTR_PADDING_SIZE;
313318
txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode), *base_size, GFP_NOFS);
314319
if (!txattr_addr)
315320
return -ENOMEM;
316321

322+
last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(xnid, inode);
323+
317324
/* read from inline xattr */
318325
if (inline_size) {
319326
err = read_inline_xattr(inode, ipage, txattr_addr);
@@ -340,7 +347,11 @@ static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
340347
else
341348
cur_addr = txattr_addr;
342349

343-
*xe = __find_xattr(cur_addr, index, len, name);
350+
*xe = __find_xattr(cur_addr, last_txattr_addr, index, len, name);
351+
if (!*xe) {
352+
err = -EFAULT;
353+
goto out;
354+
}
344355
check:
345356
if (IS_XATTR_LAST_ENTRY(*xe)) {
346357
err = -ENODATA;
@@ -584,7 +595,8 @@ static int __f2fs_setxattr(struct inode *inode, int index,
584595
struct page *ipage, int flags)
585596
{
586597
struct f2fs_xattr_entry *here, *last;
587-
void *base_addr;
598+
void *base_addr, *last_base_addr;
599+
nid_t xnid = F2FS_I(inode)->i_xattr_nid;
588600
int found, newsize;
589601
size_t len;
590602
__u32 new_hsize;
@@ -608,8 +620,14 @@ static int __f2fs_setxattr(struct inode *inode, int index,
608620
if (error)
609621
return error;
610622

623+
last_base_addr = (void *)base_addr + XATTR_SIZE(xnid, inode);
624+
611625
/* find entry with wanted name. */
612-
here = __find_xattr(base_addr, index, len, name);
626+
here = __find_xattr(base_addr, last_base_addr, index, len, name);
627+
if (!here) {
628+
error = -EFAULT;
629+
goto exit;
630+
}
613631

614632
found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
615633

fs/f2fs/xattr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ struct f2fs_xattr_entry {
7474
entry = XATTR_NEXT_ENTRY(entry))
7575
#define VALID_XATTR_BLOCK_SIZE (PAGE_SIZE - sizeof(struct node_footer))
7676
#define XATTR_PADDING_SIZE (sizeof(__u32))
77+
#define XATTR_SIZE(x,i) (((x) ? VALID_XATTR_BLOCK_SIZE : 0) + \
78+
(inline_xattr_size(i)))
7779
#define MIN_OFFSET(i) XATTR_ALIGN(inline_xattr_size(i) + \
7880
VALID_XATTR_BLOCK_SIZE)
7981

0 commit comments

Comments
 (0)