Commit 5b45a77d authored by Randall Huang's avatar Randall Huang Committed by Kelsey Skunberg

f2fs: fix to avoid accessing xattr across the boundary

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: default avatarRandall Huang <huangrandall@google.com>
[Jaegeuk Kim: Fix wrong ending boundary]
Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
Signed-off-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>

CVE-2019-9453
[back-ported from commit 2777e654]
[ben_r: adjusted patch, add def for VALID_XATTR_BLOCK_SIZE ]
Signed-off-by: default avatarBenjamin M Romer <benjamin.romer@canonical.com>
Acked-by: default avatarMarcelo Henrique Cerri <marcelo.cerri@canonical.com>
Acked-by: default avatarKamal Mostafa <kamal@canonical.com>
Signed-off-by: default avatarIan May <ian.may@canonical.com>
Signed-off-by: default avatarKelsey Skunberg <kelsey.skunberg@canonical.com>
parent ee65ed13
...@@ -248,12 +248,17 @@ static inline const struct xattr_handler *f2fs_xattr_handler(int index) ...@@ -248,12 +248,17 @@ static inline const struct xattr_handler *f2fs_xattr_handler(int index)
return handler; return handler;
} }
static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index, static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
void *last_base_addr, int index,
size_t len, const char *name) size_t len, const char *name)
{ {
struct f2fs_xattr_entry *entry; struct f2fs_xattr_entry *entry;
list_for_each_xattr(entry, base_addr) { list_for_each_xattr(entry, base_addr) {
if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
(void *)XATTR_NEXT_ENTRY(entry) > last_base_addr)
return NULL;
if (entry->e_name_index != index) if (entry->e_name_index != index)
continue; continue;
if (entry->e_name_len != len) if (entry->e_name_len != len)
...@@ -401,9 +406,10 @@ int f2fs_getxattr(struct inode *inode, int index, const char *name, ...@@ -401,9 +406,10 @@ int f2fs_getxattr(struct inode *inode, int index, const char *name,
void *buffer, size_t buffer_size, struct page *ipage) void *buffer, size_t buffer_size, struct page *ipage)
{ {
struct f2fs_xattr_entry *entry; struct f2fs_xattr_entry *entry;
void *base_addr; void *base_addr, *last_base_addr;
int error = 0; int error = 0;
size_t size, len; size_t size, len;
nid_t xnid = F2FS_I(inode)->i_xattr_nid;
if (name == NULL) if (name == NULL)
return -EINVAL; return -EINVAL;
...@@ -416,7 +422,9 @@ int f2fs_getxattr(struct inode *inode, int index, const char *name, ...@@ -416,7 +422,9 @@ int f2fs_getxattr(struct inode *inode, int index, const char *name,
if (!base_addr) if (!base_addr)
return -ENOMEM; return -ENOMEM;
entry = __find_xattr(base_addr, index, len, name); last_base_addr = (void *)base_addr + XATTR_SIZE(xnid, inode);
entry = __find_xattr(base_addr, last_base_addr, index, len, name);
if (IS_XATTR_LAST_ENTRY(entry)) { if (IS_XATTR_LAST_ENTRY(entry)) {
error = -ENODATA; error = -ENODATA;
goto cleanup; goto cleanup;
...@@ -483,7 +491,9 @@ static int __f2fs_setxattr(struct inode *inode, int index, ...@@ -483,7 +491,9 @@ static int __f2fs_setxattr(struct inode *inode, int index,
{ {
struct f2fs_inode_info *fi = F2FS_I(inode); struct f2fs_inode_info *fi = F2FS_I(inode);
struct f2fs_xattr_entry *here, *last; struct f2fs_xattr_entry *here, *last;
void *base_addr; void *base_addr, *last_base_addr;
nid_t xnid = F2FS_I(inode)->i_xattr_nid;
int found, newsize; int found, newsize;
size_t len; size_t len;
__u32 new_hsize; __u32 new_hsize;
...@@ -507,8 +517,14 @@ static int __f2fs_setxattr(struct inode *inode, int index, ...@@ -507,8 +517,14 @@ static int __f2fs_setxattr(struct inode *inode, int index,
if (!base_addr) if (!base_addr)
goto exit; goto exit;
last_base_addr = (void *)base_addr + XATTR_SIZE(xnid, inode);
/* find entry with wanted name. */ /* find entry with wanted name. */
here = __find_xattr(base_addr, index, len, name); here = __find_xattr(base_addr, last_base_addr, index, len, name);
if (!here) {
error = -EFAULT;
goto exit;
}
found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1; found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
......
...@@ -72,7 +72,9 @@ struct f2fs_xattr_entry { ...@@ -72,7 +72,9 @@ struct f2fs_xattr_entry {
for (entry = XATTR_FIRST_ENTRY(addr);\ for (entry = XATTR_FIRST_ENTRY(addr);\
!IS_XATTR_LAST_ENTRY(entry);\ !IS_XATTR_LAST_ENTRY(entry);\
entry = XATTR_NEXT_ENTRY(entry)) entry = XATTR_NEXT_ENTRY(entry))
#define VALID_XATTR_BLOCK_SIZE (PAGE_SIZE - sizeof(struct node_footer))
#define XATTR_SIZE(x,i) (((x) ? VALID_XATTR_BLOCK_SIZE : 0) + \
(inline_xattr_size(i)))
#define MIN_OFFSET(i) XATTR_ALIGN(inline_xattr_size(i) + PAGE_SIZE - \ #define MIN_OFFSET(i) XATTR_ALIGN(inline_xattr_size(i) + PAGE_SIZE - \
sizeof(struct node_footer) - sizeof(__u32)) sizeof(struct node_footer) - sizeof(__u32))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment