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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E0C1BC6FA8B for ; Wed, 21 Sep 2022 19:07:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229555AbiIUTH4 (ORCPT ); Wed, 21 Sep 2022 15:07:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbiIUTHy (ORCPT ); Wed, 21 Sep 2022 15:07:54 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CA29956B1 for ; Wed, 21 Sep 2022 12:07:53 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 1EAA8B8291B for ; Wed, 21 Sep 2022 19:07:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C35A7C433D6; Wed, 21 Sep 2022 19:07:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1663787270; bh=+dgwWgBjf4EjDuSy+NuN+4D3f+4fqKols2uXvgLzp6c=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=V9H7X499TAXk/psu574CoKuDuntHqiL08cIeLbaPTllmR3s8O2e9BD27lDC5dRDLp /Y4iAAewaV8hNRthPmI/09W+J7i5e4wKEwUObW8zaC4xZ6Btnf3u2Y2u858dUHqcUx XuSed0ogH8lPrMywzTT6KpAAhwTlxlXW/Zu1KvaQ= Date: Wed, 21 Sep 2022 12:07:48 -0700 From: Andrew Morton To: Liu Shixin Cc: Liu Zixian , Mike Kravetz , Muchun Song , , , Kefeng Wang Subject: Re: [PATCH] mm: hugetlb: fix UAF in hugetlb_handle_userfault Message-Id: <20220921120748.79f3255fa0a06b182576f497@linux-foundation.org> In-Reply-To: <20220921083440.1267903-1-liushixin2@huawei.com> References: <20220921083440.1267903-1-liushixin2@huawei.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.