From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00B83C43334 for ; Fri, 22 Jul 2022 01:57:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 787746B0072; Thu, 21 Jul 2022 21:57:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 736298E0002; Thu, 21 Jul 2022 21:57:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FDD58E0001; Thu, 21 Jul 2022 21:57:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 506A96B0072 for ; Thu, 21 Jul 2022 21:57:49 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 261CE1C63F8 for ; Fri, 22 Jul 2022 01:57:49 +0000 (UTC) X-FDA: 79713074658.28.2F47086 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf11.hostedemail.com (Postfix) with ESMTP id CF57E4000C for ; Fri, 22 Jul 2022 01:57:47 +0000 (UTC) Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.56]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4LpsxM2yV8zGpDB; Fri, 22 Jul 2022 09:56:39 +0800 (CST) Received: from dggpemm500001.china.huawei.com (7.185.36.107) by dggpemm500024.china.huawei.com (7.185.36.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 22 Jul 2022 09:57:43 +0800 Received: from [10.174.177.243] (10.174.177.243) by dggpemm500001.china.huawei.com (7.185.36.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 22 Jul 2022 09:57:43 +0800 Content-Type: multipart/alternative; boundary="------------dnTanfFGP1M54x7sprFjILAl" Message-ID: Date: Fri, 22 Jul 2022 09:57:42 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: Race in mm/ksm.c Content-Language: en-US To: , , , CC: Gabriel Ryan References: From: Kefeng Wang In-Reply-To: X-Originating-IP: [10.174.177.243] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemm500001.china.huawei.com (7.185.36.107) X-CFilter-Loop: Reflected ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658455068; a=rsa-sha256; cv=none; b=1OFauFQPV8NNeBSDQFqMAamKzDAaNImrJ5pU3+p7JEjWmCrCd3LjeejMOX9Ve0NCcoxnxk RaRRHBVagJ1QAo7UCkF2V3Sf4JA2TKCFWR7LDjGlDTp5YQ+tV/+j7A8nCPP992rsNZiuTP NjD6GR0qfdAL/68wH0+XiDA8d0dDwz8= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf11.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658455068; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mg3QuMa9ECBXPFzbjbF+JLGXJvJ8zZIXM74GcSd9GGs=; b=uXrGm1bt6p65KHooGSkCKTk4Sho1wrYw+d4UcCvp/1gasSYzANnb4rSyIr5O4a84ziQufo XY00HLUjEIq/0cFkRKYwCtCO3IF3MYWdwd3pQrfzCxUqpa/Uz5FkpORwlvDCHSqH2caezf Ev/0+hyhVQ9wSkBGuWJTG6Sy+DSBmaE= Authentication-Results: imf11.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf11.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: CF57E4000C X-Stat-Signature: wh5disjkzxj331qdrrzwo7hqg4wnb89y X-HE-Tag: 1658455067-834181 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: --------------dnTanfFGP1M54x7sprFjILAl Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit On 2022/7/21 23:58, Abhishek Shah wrote: > Dear Kernel Maintainers, > > We found a race in mm/ksm.c. During the execution of the function > /__ksm_run/ which uses variable /ksm_run/ to decide the list insertion > point, the variable /ksm_run/ can be concurrently modified in the > function /run_store/, which we thought could be undesirable since “KSM > pages in newly forked mms can be missed” (See comment here: > https://elixir.bootlin.com/linux/v5.18-rc5/source/mm/ksm.c#L2498 > ). > We would also like your thoughts on the security impact given it is a > TOCTOU bug. > > We provide more details below including the trace and reproducing > test cases. Hello, could the following changes to avoid the data-race issue? diff --git a/mm/ksm.c b/mm/ksm.c index 54f78c9eecae..f072753cbb3a 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2497,6 +2497,7 @@ int __ksm_enter(struct mm_struct *mm)  {         struct mm_slot *mm_slot;         int needs_wakeup; +       bool ksm_run_merge;         mm_slot = alloc_mm_slot();         if (!mm_slot) @@ -2505,6 +2506,10 @@ int __ksm_enter(struct mm_struct *mm)         /* Check ksm_run too?  Would need tighter locking */         needs_wakeup = list_empty(&ksm_mm_head.mm_list); +       mutex_lock(&ksm_thread_mutex); +       ksm_run_unmerge = !!(ksm_run & KSM_RUN_UNMERGE); +       mutex_unlock(&ksm_thread_mutex); +         spin_lock(&ksm_mmlist_lock);         insert_to_mm_slots_hash(mm, mm_slot);         /* @@ -2517,7 +2522,7 @@ int __ksm_enter(struct mm_struct *mm)          * scanning cursor, otherwise KSM pages in newly forked mms will be          * missed: then we might as well insert at the end of the list.          */ -       if (ksm_run & KSM_RUN_UNMERGE) +       if (ksm_run_unmerge)                 list_add_tail(&mm_slot->mm_list, &ksm_mm_head.mm_list);         else                 list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list); > > > *Trace* > BUG: KCSAN: data-race in __ksm_enter / run_store > write to 0xffffffff881edae0 of 8 bytes by task 6542 on cpu 0: >  run_store+0x19a/0x2d0 mm/ksm.c:2897 >  kobj_attr_store+0x44/0x60 lib/kobject.c:824 >  sysfs_kf_write+0x16f/0x1a0 fs/sysfs/file.c:136 >  kernfs_fop_write_iter+0x2ae/0x370 fs/kernfs/file.c:291 >  call_write_iter include/linux/fs.h:2050 [inline] >  new_sync_write fs/read_write.c:504 [inline] >  vfs_write+0x779/0x900 fs/read_write.c:591 >  ksys_write+0xde/0x190 fs/read_write.c:644 >  __do_sys_write fs/read_write.c:656 [inline] >  __se_sys_write fs/read_write.c:653 [inline] >  __x64_sys_write+0x43/0x50 fs/read_write.c:653 >  do_syscall_x64 arch/x86/entry/common.c:50 [inline] >  do_syscall_64+0x3d/0x90 arch/x86/entry/common.c:80 >  entry_SYSCALL_64_after_hwframe+0x44/0xae > > read to 0xffffffff881edae0 of 8 bytes by task 6541 on cpu 1: >  __ksm_enter+0x114/0x260 mm/ksm.c:2501 >  ksm_madvise+0x291/0x350 mm/ksm.c:2451 >  madvise_vma_behavior mm/madvise.c:1039 [inline] >  madvise_walk_vmas mm/madvise.c:1221 [inline] >  do_madvise+0x656/0xeb0 mm/madvise.c:1399 >  __do_sys_madvise mm/madvise.c:1412 [inline] >  __se_sys_madvise mm/madvise.c:1410 [inline] >  __x64_sys_madvise+0x64/0x70 mm/madvise.c:1410 >  do_syscall_x64 arch/x86/entry/common.c:50 [inline] >  do_syscall_64+0x3d/0x90 arch/x86/entry/common.c:80 >  entry_SYSCALL_64_after_hwframe+0x44/0xae > > Reported by Kernel Concurrency Sanitizer on: > CPU: 1 PID: 6541 Comm: syz-executor2-n Not tainted 5.18.0-rc5+ #107 > Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 > 04/01/2014 > --------------------- > *Inputs * > Input CPU 0: > r0 = openat$sysctl(0xffffff9c, > &(0x7f0000000100)='/sys/kernel/mm/ksm/run\x00', 0x1, 0x0) > write$sysctl(r0, &(0x7f0000000000)='2\x00', 0x2) > > Input CPU 1: > madvise(&(0x7f0000ffc000/0x4000)=nil, 0x4000, 0xc) > mlock2(&(0x7f0000ffe000/0x2000)=nil, 0x2000, 0x0) > madvise(&(0x7f0000ffd000/0x3000)=nil, 0x3000, 0x12) > clone(0x0, 0x0, 0x0, 0x0, 0x0) --------------dnTanfFGP1M54x7sprFjILAl Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: 8bit


On 2022/7/21 23:58, Abhishek Shah wrote:
Dear Kernel Maintainers,

We found a race in mm/ksm.c. During the execution of the function __ksm_run which uses variable ksm_run to decide the list insertion point, the variable ksm_run can be concurrently modified in the function run_store, which we thought could be undesirable since “KSM pages in newly forked mms can be missed” (See comment here: https://elixir.bootlin.com/linux/v5.18-rc5/source/mm/ksm.c#L2498). We would also like your thoughts on the security impact given it is a TOCTOU bug.  

We provide more details below including the trace and reproducing test cases.


Hello, could the following changes to avoid the data-race issue?

diff --git a/mm/ksm.c b/mm/ksm.c
index 54f78c9eecae..f072753cbb3a 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2497,6 +2497,7 @@ int __ksm_enter(struct mm_struct *mm)
 {
        struct mm_slot *mm_slot;
        int needs_wakeup;
+       bool ksm_run_merge;
 
        mm_slot = alloc_mm_slot();
        if (!mm_slot)
@@ -2505,6 +2506,10 @@ int __ksm_enter(struct mm_struct *mm)
        /* Check ksm_run too?  Would need tighter locking */
        needs_wakeup = list_empty(&ksm_mm_head.mm_list);
 
+       mutex_lock(&ksm_thread_mutex);
+       ksm_run_unmerge = !!(ksm_run & KSM_RUN_UNMERGE);
+       mutex_unlock(&ksm_thread_mutex);
+
        spin_lock(&ksm_mmlist_lock);
        insert_to_mm_slots_hash(mm, mm_slot);
        /*
@@ -2517,7 +2522,7 @@ int __ksm_enter(struct mm_struct *mm)
         * scanning cursor, otherwise KSM pages in newly forked mms will be
         * missed: then we might as well insert at the end of the list.
         */
-       if (ksm_run & KSM_RUN_UNMERGE)
+       if (ksm_run_unmerge)
                list_add_tail(&mm_slot->mm_list, &ksm_mm_head.mm_list);
        else
                list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list);




Trace
BUG: KCSAN: data-race in __ksm_enter / run_store
write to 0xffffffff881edae0 of 8 bytes by task 6542 on cpu 0:
 run_store+0x19a/0x2d0 mm/ksm.c:2897
 kobj_attr_store+0x44/0x60 lib/kobject.c:824
 sysfs_kf_write+0x16f/0x1a0 fs/sysfs/file.c:136
 kernfs_fop_write_iter+0x2ae/0x370 fs/kernfs/file.c:291
 call_write_iter include/linux/fs.h:2050 [inline]
 new_sync_write fs/read_write.c:504 [inline]
 vfs_write+0x779/0x900 fs/read_write.c:591
 ksys_write+0xde/0x190 fs/read_write.c:644
 __do_sys_write fs/read_write.c:656 [inline]
 __se_sys_write fs/read_write.c:653 [inline]
 __x64_sys_write+0x43/0x50 fs/read_write.c:653
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3d/0x90 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x44/0xae

read to 0xffffffff881edae0 of 8 bytes by task 6541 on cpu 1:
 __ksm_enter+0x114/0x260 mm/ksm.c:2501
 ksm_madvise+0x291/0x350 mm/ksm.c:2451
 madvise_vma_behavior mm/madvise.c:1039 [inline]
 madvise_walk_vmas mm/madvise.c:1221 [inline]
 do_madvise+0x656/0xeb0 mm/madvise.c:1399
 __do_sys_madvise mm/madvise.c:1412 [inline]
 __se_sys_madvise mm/madvise.c:1410 [inline]
 __x64_sys_madvise+0x64/0x70 mm/madvise.c:1410
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3d/0x90 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x44/0xae

Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 6541 Comm: syz-executor2-n Not tainted 5.18.0-rc5+ #107
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
---------------------
Inputs 
Input CPU 0:
r0 = openat$sysctl(0xffffff9c, &(0x7f0000000100)='/sys/kernel/mm/ksm/run\x00', 0x1, 0x0)
write$sysctl(r0, &(0x7f0000000000)='2\x00', 0x2)

Input CPU 1:
madvise(&(0x7f0000ffc000/0x4000)=nil, 0x4000, 0xc)
mlock2(&(0x7f0000ffe000/0x2000)=nil, 0x2000, 0x0)
madvise(&(0x7f0000ffd000/0x3000)=nil, 0x3000, 0x12)
clone(0x0, 0x0, 0x0, 0x0, 0x0)
--------------dnTanfFGP1M54x7sprFjILAl--