Skip to content

Commit 4fde9b7

Browse files
mm: kmem: fix a NULL pointer dereference in obj_stock_flush_required()
jira VULN-155868 cve CVE-2023-53401 commit-author Roman Gushchin <roman.gushchin@linux.dev> commit 3b8abb3 KCSAN found an issue in obj_stock_flush_required(): stock->cached_objcg can be reset between the check and dereference: ================================================================== BUG: KCSAN: data-race in drain_all_stock / drain_obj_stock write to 0xffff888237c2a2f8 of 8 bytes by task 19625 on cpu 0: drain_obj_stock+0x408/0x4e0 mm/memcontrol.c:3306 refill_obj_stock+0x9c/0x1e0 mm/memcontrol.c:3340 obj_cgroup_uncharge+0xe/0x10 mm/memcontrol.c:3408 memcg_slab_free_hook mm/slab.h:587 [inline] __cache_free mm/slab.c:3373 [inline] __do_kmem_cache_free mm/slab.c:3577 [inline] kmem_cache_free+0x105/0x280 mm/slab.c:3602 __d_free fs/dcache.c:298 [inline] dentry_free fs/dcache.c:375 [inline] __dentry_kill+0x422/0x4a0 fs/dcache.c:621 dentry_kill+0x8d/0x1e0 dput+0x118/0x1f0 fs/dcache.c:913 __fput+0x3bf/0x570 fs/file_table.c:329 ____fput+0x15/0x20 fs/file_table.c:349 task_work_run+0x123/0x160 kernel/task_work.c:179 resume_user_mode_work include/linux/resume_user_mode.h:49 [inline] exit_to_user_mode_loop+0xcf/0xe0 kernel/entry/common.c:171 exit_to_user_mode_prepare+0x6a/0xa0 kernel/entry/common.c:203 __syscall_exit_to_user_mode_work kernel/entry/common.c:285 [inline] syscall_exit_to_user_mode+0x26/0x140 kernel/entry/common.c:296 do_syscall_64+0x4d/0xc0 arch/x86/entry/common.c:86 entry_SYSCALL_64_after_hwframe+0x63/0xcd read to 0xffff888237c2a2f8 of 8 bytes by task 19632 on cpu 1: obj_stock_flush_required mm/memcontrol.c:3319 [inline] drain_all_stock+0x174/0x2a0 mm/memcontrol.c:2361 try_charge_memcg+0x6d0/0xd10 mm/memcontrol.c:2703 try_charge mm/memcontrol.c:2837 [inline] mem_cgroup_charge_skmem+0x51/0x140 mm/memcontrol.c:7290 sock_reserve_memory+0xb1/0x390 net/core/sock.c:1025 sk_setsockopt+0x800/0x1e70 net/core/sock.c:1525 udp_lib_setsockopt+0x99/0x6c0 net/ipv4/udp.c:2692 udp_setsockopt+0x73/0xa0 net/ipv4/udp.c:2817 sock_common_setsockopt+0x61/0x70 net/core/sock.c:3668 __sys_setsockopt+0x1c3/0x230 net/socket.c:2271 __do_sys_setsockopt net/socket.c:2282 [inline] __se_sys_setsockopt net/socket.c:2279 [inline] __x64_sys_setsockopt+0x66/0x80 net/socket.c:2279 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x41/0xc0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd value changed: 0xffff8881382d52c0 -> 0xffff888138893740 Reported by Kernel Concurrency Sanitizer on: CPU: 1 PID: 19632 Comm: syz-executor.0 Not tainted 6.3.0-rc2-syzkaller-00387-g534293368afa #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 03/02/2023 Fix it by using READ_ONCE()/WRITE_ONCE() for all accesses to stock->cached_objcg. Link: https://lkml.kernel.org/r/20230502160839.361544-1-roman.gushchin@linux.dev Fixes: bf4f059 ("mm: memcg/slab: obj_cgroup API") Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev> Reported-by: syzbot+774c29891415ab0fd29d@syzkaller.appspotmail.com Reported-by: Dmitry Vyukov <dvyukov@google.com> Link: https://lore.kernel.org/linux-mm/CACT4Y+ZfucZhM60YPphWiCLJr6+SGFhT+jjm8k1P-a_8Kkxsjg@mail.gmail.com/T/#t Reviewed-by: Yosry Ahmed <yosryahmed@google.com> Acked-by: Shakeel Butt <shakeelb@google.com> Reviewed-by: Dmitry Vyukov <dvyukov@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> (cherry picked from commit 3b8abb3) Signed-off-by: Shreeya Patel <spatel@ciq.com>
1 parent 779928a commit 4fde9b7

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

mm/memcontrol.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,12 +3255,12 @@ void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
32553255
* accumulating over a page of vmstat data or when pgdat or idx
32563256
* changes.
32573257
*/
3258-
if (stock->cached_objcg != objcg) {
3258+
if (READ_ONCE(stock->cached_objcg) != objcg) {
32593259
old = drain_obj_stock(stock);
32603260
obj_cgroup_get(objcg);
32613261
stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
32623262
? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
3263-
stock->cached_objcg = objcg;
3263+
WRITE_ONCE(stock->cached_objcg, objcg);
32643264
stock->cached_pgdat = pgdat;
32653265
} else if (stock->cached_pgdat != pgdat) {
32663266
/* Flush the existing cached vmstat data */
@@ -3314,7 +3314,7 @@ static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes)
33143314
local_lock_irqsave(&memcg_stock.stock_lock, flags);
33153315

33163316
stock = this_cpu_ptr(&memcg_stock);
3317-
if (objcg == stock->cached_objcg && stock->nr_bytes >= nr_bytes) {
3317+
if (objcg == READ_ONCE(stock->cached_objcg) && stock->nr_bytes >= nr_bytes) {
33183318
stock->nr_bytes -= nr_bytes;
33193319
ret = true;
33203320
}
@@ -3326,7 +3326,7 @@ static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes)
33263326

33273327
static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock)
33283328
{
3329-
struct obj_cgroup *old = stock->cached_objcg;
3329+
struct obj_cgroup *old = READ_ONCE(stock->cached_objcg);
33303330

33313331
if (!old)
33323332
return NULL;
@@ -3379,7 +3379,7 @@ static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock)
33793379
stock->cached_pgdat = NULL;
33803380
}
33813381

3382-
stock->cached_objcg = NULL;
3382+
WRITE_ONCE(stock->cached_objcg, NULL);
33833383
/*
33843384
* The `old' objects needs to be released by the caller via
33853385
* obj_cgroup_put() outside of memcg_stock_pcp::stock_lock.
@@ -3390,10 +3390,11 @@ static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock)
33903390
static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
33913391
struct mem_cgroup *root_memcg)
33923392
{
3393+
struct obj_cgroup *objcg = READ_ONCE(stock->cached_objcg);
33933394
struct mem_cgroup *memcg;
33943395

3395-
if (stock->cached_objcg) {
3396-
memcg = obj_cgroup_memcg(stock->cached_objcg);
3396+
if (objcg) {
3397+
memcg = obj_cgroup_memcg(objcg);
33973398
if (memcg && mem_cgroup_is_descendant(memcg, root_memcg))
33983399
return true;
33993400
}
@@ -3412,10 +3413,10 @@ static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes,
34123413
local_lock_irqsave(&memcg_stock.stock_lock, flags);
34133414

34143415
stock = this_cpu_ptr(&memcg_stock);
3415-
if (stock->cached_objcg != objcg) { /* reset if necessary */
3416+
if (READ_ONCE(stock->cached_objcg) != objcg) { /* reset if necessary */
34163417
old = drain_obj_stock(stock);
34173418
obj_cgroup_get(objcg);
3418-
stock->cached_objcg = objcg;
3419+
WRITE_ONCE(stock->cached_objcg, objcg);
34193420
stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
34203421
? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
34213422
allow_uncharge = true; /* Allow uncharge when objcg changes */

0 commit comments

Comments
 (0)