Skip to contentSkip to conversation

New: sometime kernel crash when kzfree is called in fs/f2fs/xattr.c

Bug 202747 [email protected] 1 participant 3 messages Mar 4, 2019 – Mar 16, 2019 F2FS file system

lore
Share & tools
lore
#0 lore raw

https://bugzilla.kernel.org/show_bug.cgi?id=202747

Bug ID: 202747
Summary: sometime kernel crash when kzfree is called in
fs/f2fs/xattr.c
Product: File System
Version: 2.5
Kernel Version: f2fs-dev
Hardware: All
OS: Linux
Tree: Mainline
Status: NEW
Severity: high
Priority: P1
Component: f2fs
Assignee: [email protected]
Reporter: [email protected]
Regression: No

in file fs/f2fs/xattr.c

kzfree() is called somewhere

example:

static int read_all_xattrs(struct inode *inode, struct page *ipage,
void **base_addr)

{
        ......
        txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
                        inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
        .....
        kzfree(txattr_addr);
        return err;
}

address is alloced by f2fs_kzalloc(), step into, it may use kmalloc() or kvmalloc(),
accordingly the address should be freed by kfree() or kvfree(), but kzfree() aways use kfree(), then the kernel crashed when the address is alloced by kvmalloc().

I have changed kzfree to kvfree, kernel not crash any more, I not understand why set the memory to zero before free the address.

so I use the patch, kernel not crash also。

it diff diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index 85d9508..c4b3d7b 100644

fs/f2fs/f2fs.h +0 -0
@@ -2798,6 +2798,18 @@ static inline void *f2fs_kmalloc(struct f2fs_sb_info

*sbi,

        return kvmalloc(size, flags);
}

+static inline void *f2fs_kzfree(const void *p) {
+ size_t ks;
+ void *mem = (void *)p;
+
+ if (unlikely(ZERO_OR_NULL_PTR(mem)))
+ return;

+       ks = ksize(mem);
+       memset(mem, 0, ks);
+       kvfree(mem);
+}
+
static inline void *kvzalloc(size_t size, gfp_t flags) {
        void *ret;
fs/f2fs/xattr.c +0 -0
@@ -386,7 +386,7 @@ check:
        *base_addr = txattr_addr;
        return 0;

out:
- kzfree(txattr_addr);

+       f2fs_kzfree(txattr_addr);
        return err;
}

@@ -429,7 +429,7 @@ static int read_all_xattrs(struct inode *inode, struct page *ipage,

        *base_addr = txattr_addr;
        return 0;
fail:
-       kzfree(txattr_addr);
+       f2fs_kzfree(txattr_addr);
        return err;
}

@@ -556,7 +556,7 @@ int f2fs_getxattr(struct inode *inode, int index, const char *name,

        }
        error = size;
out:
-       kzfree(base_addr);
+       f2fs_kzfree(base_addr);
        return error;
Signature · 7 lines
You are receiving this mail because:
You are watching the assignee of the bug.

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw

[Bug 202747] sometime kernel crash when kzfree is called in fs/f2fs/xattr.c

https://bugzilla.kernel.org/show_bug.cgi?id=202747

Chao Yu ([email protected]) changed:

What |Removed |Added
----------------------------------------------------------------------------

             Status|NEW                         |ASSIGNED
                 CC|                            |[email protected]

--- Comment #1 from Chao Yu ([email protected]) ---
I didn't see many filesystems are using kzfree(), instead, there are many callers come from crypto module, I guess they use the buffer to store ciphertext or crypto key temporarily, so, before freeing those buffer it will be better to clean the data in buffer to avoid confidential data leak.

For f2fs, I think there is no such demand, and we just missed to change kzfree to kvfree in below commit:

5222595d093e ("f2fs: use kvmalloc, if kmalloc is failed")

Signature · 2 lines
You are receiving this mail because:
You are watching the assignee of the bug.
#2 lore raw

[Bug 202747] sometime kernel crash when kzfree is called in fs/f2fs/xattr.c

In reply to [email protected]

https://bugzilla.kernel.org/show_bug.cgi?id=202747

Chao Yu ([email protected]) changed:

What |Removed |Added
----------------------------------------------------------------------------

             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |CODE_FIX

--- Comment #2 from Chao Yu ([email protected]) --- The fixing patch has been merged, close this issue.

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2a6a7e722e7a78d774ce02b847c5b183a3ff2672

Signature · 2 lines
You are receiving this mail because:
You are watching the assignee of the bug.