Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp149493rwb; Wed, 21 Sep 2022 19:19:49 -0700 (PDT) X-Google-Smtp-Source: AMsMyM75+xOyyZoP3MkTFUdd7GZ5RCSOIA+0bKCHDDovxqE83S4yj3w50DXAuxqkNsHelq3r4sZd X-Received: by 2002:a17:90a:fe6:b0:203:7c29:6e0d with SMTP id 93-20020a17090a0fe600b002037c296e0dmr1169848pjz.181.1663813189046; Wed, 21 Sep 2022 19:19:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663813189; cv=none; d=google.com; s=arc-20160816; b=QshMg+HQNUUsyTm7uucoCYzv9B7QMnp8zVlsDuqBGox3xVJojIlo1+qhVeVYZ8xeo5 QoZEHVbk+5Y3GgYo1snaxO17Wk9KcOj+pTdP2o1wuGJ5fyqqe2iAQwwKMw6TV8kcos/x O9D6AR1QidDd9TIY8nTY/qsRk1wmUsOKnqKoU4LsrmFWyHgYLBWp93avee4Vq5lqiIrv TwmSXHJ70XKiwWSIqYEMZ/ClPVtlaBy6AE+kzKvMkGBWWPcyXO1Tsk+mx6CdHVKA6Gx8 e9G3DIl+iy49RwAuQfPs0kAovsA05XA7BT/GqDtwd7QJTSnWQZCmqyyZ89j+Qu3naNfx Cw2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject; bh=MS5vk6lic4FENdZwh2YSYz6XdOk2SBg8pD6ibJmZDl8=; b=V9Jth51Ysf/bEmHweRrjXHG+bnCKmdiBu3X19sJ0zI71dJ60e1KChqv8rENa4Ou6gW 0Q4hQg5kTbyZlfYrJ32fXI59ms/IlHsyM8Z5mE4IR/G4vzhobcCx+UaQh67dIfhspI6D 1P/h+GGpAIiclwI3LxZL6gU2R3UzXQVWPSvaERAH3liMs8nnJiUC4oLQpqko1hYF0y2a IBcaQJmgUNHqHcJFMLA736MESNiVkDUtAHooSRd0enzagURGV+t1PVXQHOQ7mPd62M+Y RzOxzAyB0MA3Qft2pDb9h7UoRo7Di3p2emzeHJs0XR55CVtTgzbeq1EEHdYBmeWjxeTF vj0A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f71-20020a636a4a000000b00434ac3b9915si4396526pgc.790.2022.09.21.19.19.37; Wed, 21 Sep 2022 19:19:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230161AbiIVB6o (ORCPT + 99 others); Wed, 21 Sep 2022 21:58:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229566AbiIVB6m (ORCPT ); Wed, 21 Sep 2022 21:58:42 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12C88A3D06 for ; Wed, 21 Sep 2022 18:58:41 -0700 (PDT) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.53]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4MXz0X74vszHqH1; Thu, 22 Sep 2022 09:56:28 +0800 (CST) Received: from dggpemm100009.china.huawei.com (7.185.36.113) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 22 Sep 2022 09:58:39 +0800 Received: from [10.174.179.24] (10.174.179.24) by dggpemm100009.china.huawei.com (7.185.36.113) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 22 Sep 2022 09:58:38 +0800 Subject: Re: [PATCH] mm: hugetlb: fix UAF in hugetlb_handle_userfault To: Andrew Morton References: <20220921083440.1267903-1-liushixin2@huawei.com> <20220921120748.79f3255fa0a06b182576f497@linux-foundation.org> CC: Liu Zixian , Mike Kravetz , Muchun Song , , , Kefeng Wang From: Liu Shixin Message-ID: Date: Thu, 22 Sep 2022 09:58:38 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: <20220921120748.79f3255fa0a06b182576f497@linux-foundation.org> Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.179.24] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm100009.china.huawei.com (7.185.36.113) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022/9/22 3:07, Andrew Morton wrote: > On Wed, 21 Sep 2022 16:34:40 +0800 Liu Shixin wrote: > >> The vma_lock and hugetlb_fault_mutex are dropped before handling >> userfault and reacquire them again after handle_userfault(), but >> reacquire the vma_lock could lead to UAF[1] due to the following >> race, >> >> hugetlb_fault >> hugetlb_no_page >> /*unlock vma_lock */ >> hugetlb_handle_userfault >> handle_userfault >> /* unlock mm->mmap_lock*/ >> vm_mmap_pgoff >> do_mmap >> mmap_region >> munmap_vma_range >> /* clean old vma */ >> /* lock vma_lock again <--- UAF */ >> /* unlock vma_lock */ >> >> Since the vma_lock will unlock immediately after hugetlb_handle_userfault(), >> let's drop the unneeded lock and unlock in hugetlb_handle_userfault() to fix >> the issue. >> >> @@ -5508,17 +5507,12 @@ static inline vm_fault_t hugetlb_handle_userfault(struct vm_area_struct *vma, >> >> /* >> * vma_lock and hugetlb_fault_mutex must be >> - * dropped before handling userfault. Reacquire >> - * after handling fault to make calling code simpler. >> + * dropped before handling userfault. >> */ >> hugetlb_vma_unlock_read(vma); >> hash = hugetlb_fault_mutex_hash(mapping, idx); >> mutex_unlock(&hugetlb_fault_mutex_table[hash]); >> - ret = handle_userfault(&vmf, reason); >> - mutex_lock(&hugetlb_fault_mutex_table[hash]); >> - hugetlb_vma_lock_read(vma); >> - >> - return ret; >> + return handle_userfault(&vmf, reason); >> } > Current code is rather different from this. So if the bug still exists > in current code, please verify this and redo the patch appropriately? > > And hang on to this version to help with the -stable backporting. > > Thanks. > . This patch conflicts with patch series "hugetlb: Use new vma lock for huge pmd sharing synchronization". So I reproduce the problem on next-20220920 and this patch is based on next-20220920 instead of mainline. This problem is existed since v4.11. I will send the stable version later.