Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp5866440rwb; Wed, 21 Sep 2022 13:42:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6T5TwSeagG2FD8/NJHwbK4JBQwDTZdxyUCEHuw2r1qL7+Ite5vO8VdfjQjVURQo2h3dPYF X-Received: by 2002:aa7:8583:0:b0:53e:5344:2b7d with SMTP id w3-20020aa78583000000b0053e53442b7dmr30599635pfn.49.1663792939995; Wed, 21 Sep 2022 13:42:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663792939; cv=none; d=google.com; s=arc-20160816; b=ECaBQzWkHYlsPn5FLllfHTuFrcVArptEa2zXeG6SoPgQqiRUhjuXIsrxCxj4sJiDGB qxLwXDIm9HT1j8Xc+Ok8QwKjQCkA+dEMeErPX2BJ8/w6N2CshZ2o2Wr8qiip8Augw31e ewB2l2jxPE+N3QJrYkfhG7ICzmSSdIvPzBMyoocLSQv2t4X7uvB1PMLvxFYHjd0GGRs7 4Sh+DDOIrmH51NNA9ujqQkcKLF8zCHsIchLOODY79hMGeVgKEWMWN6C6M8NAPHBxZEhk Wf29+JdNqDE34NIw2ydNai86rKhPakeE8CK4feUOyBoAKWQx9Dd+NXt/W9HzbWOEf2zh u52Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=FJz/GmaV/T6w8MBoASwbnnxNDlayK4OQTIP3zNRDj3I=; b=aGWZtxVkVNGMXCUvb643Cmj8B4nuV5xLyAqkLD159CXnXoPRoECZtmIW77P1fi3fZB qKHVMildOCQWDCon1Y1amFo5hfWdi/V3TLgLK2+nB2gjqatbNRklOArsUy0JHFcY/CmC H5Av2TzZFJJbNjTVGUZwuFkQ6mwZ+lI1il0xRIbkqVvSUHWTu48PQQJVcxrLKjew/ood NFi4Hfdl3JvSnxQKZWPClMXAuZz6Hw48MJwp5KncbndozxjAXYEPgZ2YmuVXK45E2lUJ 233MaqzkftEIZap5lnUj50AIaTy9MumJC9h0/3M8jGKeh8Dr855AFdnjnBRoAV6VU5Dk SLfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=V9H7X499; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y136-20020a62ce8e000000b005453554828bsi379761pfg.204.2022.09.21.13.42.08; Wed, 21 Sep 2022 13:42:19 -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; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=V9H7X499; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229713AbiIUTHz (ORCPT + 99 others); Wed, 21 Sep 2022 15:07:55 -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 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, 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 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.