From patchwork Sat Sep 14 19:41:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Vishal Moola (Oracle)" X-Patchwork-Id: 13804547 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 8EAF3C021AF for ; Sat, 14 Sep 2024 19:43:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E85EE6B0083; Sat, 14 Sep 2024 15:43:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E2BC76B0088; Sat, 14 Sep 2024 15:43:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF34B6B0089; Sat, 14 Sep 2024 15:43:51 -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 B0FF76B0083 for ; Sat, 14 Sep 2024 15:43:51 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 33266160975 for ; Sat, 14 Sep 2024 19:43:51 +0000 (UTC) X-FDA: 82564369062.18.C4E54AD Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf17.hostedemail.com (Postfix) with ESMTP id 5941940008 for ; Sat, 14 Sep 2024 19:43:49 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=M8pPC0ZK; spf=pass (imf17.hostedemail.com: domain of vishal.moola@gmail.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=vishal.moola@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726342975; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=qEFv/WIVVDnR9INyh6CO+aEW/z2pecEd+lx/1UNH+Es=; b=LzAtupWDoquhsnIt41vTGB5fhRoviRFc3YypzpVgcWwcZoHdlhVIuqIgVKaks3qptxEZ4l jq90GWjjG1eFxf61Y1jIoH9MtNG2peB1JvCOkYX2Ec+Sv2sQ88fdJS75tsHgL8GTZnad5J EME8XLmdKTGavfe2cUS3pOFUo6cM53s= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=M8pPC0ZK; spf=pass (imf17.hostedemail.com: domain of vishal.moola@gmail.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=vishal.moola@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726342975; a=rsa-sha256; cv=none; b=Bq4YCUrX93Vm8EtuyDjoxw3a20SwkXtCKrkLOgcIi/1hRJ3tHxG72r40GOTrl5Q6LaTUY1 BQvPNS7BoJtjfiZ5HPiHO8sct8NlRXeg+H1uEl5nywSLjU2UQh49e31e/3jF/QhFC9qnTw M4BSS5gDoE/CtzpuaPTTlQGlUWjSXds= Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-718e285544fso1616586b3a.1 for ; Sat, 14 Sep 2024 12:43:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726343028; x=1726947828; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qEFv/WIVVDnR9INyh6CO+aEW/z2pecEd+lx/1UNH+Es=; b=M8pPC0ZKg+Ao0lFls8tl01w+qPPD7urtduWzQDQllZQ5KLnbio6IznQdWtIaNLou6T fowE1ep+krexoD7xaGw/P1gdUnizTOdrB/sZZi4eMMYzT+Q5I+PmMBzHIfX7o6B6ebFI RxWXUj6LchAqWJBfX2ClBNPJWePxPOknKPZb8T5tnjquIiJuDfYmxnduPKpKvJq5P1jZ wtPiuYXYoS9VEEk4al/MfP/G8TEp1fGXVODVLPFzCk3vw4iIDTmb0aa4blFgSs15dD4K A7++gMrc13bR0o4A1dR4dsr5HL7TsUNiPZ7FvOdrhl4TVJKndMNH7wGmZ7NIY4Hs+LaU qj9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726343028; x=1726947828; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qEFv/WIVVDnR9INyh6CO+aEW/z2pecEd+lx/1UNH+Es=; b=U21s+3a2W1FJV+1ntX16rlmAtumy5B/wZWMQAXPBnlXh4YwHSZsFeXWFO7WwbNbuW3 XilvJCfhFPuyXg7WWJvxIk5o8kEnXwX7ISgIOIfCUJNBNGkYQDQgIn911yQ4l2+wFJr+ zyx99uuVkFuJ4MC93kMWU46dxR2CZhcvDKHoZV/Cas7KB1h1JrER9/+YL45ZWpLu+FqD uDv5yGEU84vuqnZSUTGixKGohWCnMGUII71cieB9rcm6X7ecoLzRobf3vYmC+0wmNUdd kmC/IXamFoxIiva6I2AN0WB/UyNyBkXbhELh7DTuYBEnhnwRKA9nk7GKlMqRjSdoDBCr HuIg== X-Forwarded-Encrypted: i=1; AJvYcCXEbIRD+QArs4Fs5RYkiS+y5Q+qARv2y59+o0NyL42Pc8lNF2/r4IChrOePeA17ZNUPP4LBb4gKDg==@kvack.org X-Gm-Message-State: AOJu0YwwkI0EEYD+ayNqqPgFxmMoJpnfq5z1jMo5kU2q11nbzOgeuBjB 4JnNrJ+fxp6fFKa5X5p8FjsiRGd3Iaa7NzhbNkMk5qqjwYr+m++H X-Google-Smtp-Source: AGHT+IHrDIsY/QTLODnUtzDg70cOoM1IO63V2p1YyrSgtGMrwYZoljG+kzn5MdMlfDTCg2BnwTHqUw== X-Received: by 2002:a05:6a00:1701:b0:717:8b4e:98b6 with SMTP id d2e1a72fcca58-71936adf4a5mr11079419b3a.21.1726343028008; Sat, 14 Sep 2024 12:43:48 -0700 (PDT) Received: from localhost.localdomain (c-67-164-59-41.hsd1.ca.comcast.net. [67.164.59.41]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-71944a9cab5sm1294742b3a.5.2024.09.14.12.43.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Sep 2024 12:43:47 -0700 (PDT) From: "Vishal Moola (Oracle)" To: Muchun Song , syzbot , Andrew Morton , LKML , Linux Memory Management List , syzkaller-bugs@googlegroups.com Cc: "Vishal Moola (Oracle)" , stable@vger.kernel.org Subject: [PATCH 2/2] mm/hugetlb.c: Fix UAF of vma in hugetlb fault pathway Date: Sat, 14 Sep 2024 12:41:19 -0700 Message-ID: <20240914194243.245-2-vishal.moola@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240914194243.245-1-vishal.moola@gmail.com> References: <7B2E5B76-8FC6-49EA-B0B3-2452ED6ABC5D@linux.dev> <20240914194243.245-1-vishal.moola@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 5941940008 X-Stat-Signature: kwn118db9xt1gzhkoob4twesgqwrt341 X-HE-Tag: 1726343029-479584 X-HE-Meta: U2FsdGVkX1/rJYuz6EUVA/HBx+dW1TOaPtIw3a1Q6V8adH4qTXYurJCD63VeD6SWEmbcpB0ruc3Nlm9s+TdspQ5DCq8romE89kDiOF5jXQ0lptTfNZ+DzKcR4E5P4Hu8/4YSixx24rIURQsUWgtwNrjJqAv0wbrwbA1CdpwAK25b4amzH3FGHmBXBgeqetcMVBroVPhqpxW8b8oT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jt1Y3J7iAOIqambs4bxqT5e3vC4jLDIzdPLqD2RTl6cgRtqWagKWCs6lAWC9D3XbfrscItH917IIFCOfg+tUy89oy2lTNSaen5Ly7jkSUDA6v5bmQcA0tnIOj4dQTnt0/T5TozqRtRvD+w8UnG8roTa1gYThNR3xUVTImtFDZrtvnkJeets7teVnEGqpUYu3dlLiucDAXpf5FXA2gkYzJRykrM4cb5mKUpmSchsx6Bah4KQqqr4cWqNlSOWAp+G/npfAPLtClj9cYXQLR2gFVyCBz0KqpZWQlT7Dtzp/hGJF/3l6Z+IQZLEDk+o9MV7AF74DqeXtBagXEoPlUZoeQecA== 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: List-Subscribe: List-Unsubscribe: Syzbot reports a UAF in hugetlb_fault(). This happens because vmf_anon_prepare() could drop the per-VMA lock and allow the current VMA to be freed before hugetlb_vma_unlock_read() is called. We can fix this by using a modified version of vmf_anon_prepare() that doesn't release the VMA lock on failure, and then release it ourselves after hugetlb_vma_unlock_read(). Reported-by: syzbot+2dab93857ee95f2eeb08@syzkaller.appspotmail.com Closes: https://lore.kernel.org/linux-mm/00000000000067c20b06219fbc26@google.com/ Fixes: 9acad7ba3e25 ("hugetlb: use vmf_anon_prepare() instead of anon_vma_prepare()") Signed-off-by: Vishal Moola (Oracle) Cc: --- mm/hugetlb.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 5c77defad295..190fa05635f4 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5915,7 +5915,7 @@ static vm_fault_t hugetlb_wp(struct folio *pagecache_folio, * When the original hugepage is shared one, it does not have * anon_vma prepared. */ - ret = vmf_anon_prepare(vmf); + ret = __vmf_anon_prepare(vmf); if (unlikely(ret)) goto out_release_all; @@ -6114,7 +6114,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, } if (!(vma->vm_flags & VM_MAYSHARE)) { - ret = vmf_anon_prepare(vmf); + ret = __vmf_anon_prepare(vmf); if (unlikely(ret)) goto out; } @@ -6245,6 +6245,14 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, folio_unlock(folio); out: hugetlb_vma_unlock_read(vma); + + /* + * We must check to release the per-VMA lock. __vmf_anon_prepare() is + * the only way ret can be set to VM_FAULT_RETRY. + */ + if (unlikely(ret & VM_FAULT_RETRY)) + vma_end_read(vma); + mutex_unlock(&hugetlb_fault_mutex_table[hash]); return ret; @@ -6466,6 +6474,14 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, } out_mutex: hugetlb_vma_unlock_read(vma); + + /* + * We must check to release the per-VMA lock. __vmf_anon_prepare() in + * hugetlb_wp() is the only way ret can be set to VM_FAULT_RETRY. + */ + if (unlikely(ret & VM_FAULT_RETRY)) + vma_end_read(vma); + mutex_unlock(&hugetlb_fault_mutex_table[hash]); /* * Generally it's safe to hold refcount during waiting page lock. But