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 637DEC433F5 for ; Thu, 24 Feb 2022 23:18:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F37AC8D0005; Thu, 24 Feb 2022 18:18:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EC2758D0001; Thu, 24 Feb 2022 18:18:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DAEBA8D0005; Thu, 24 Feb 2022 18:18:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id C873C8D0001 for ; Thu, 24 Feb 2022 18:18:46 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 8E14A81772 for ; Thu, 24 Feb 2022 23:18:46 +0000 (UTC) X-FDA: 79179240252.14.65902ED Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf22.hostedemail.com (Postfix) with ESMTP id 70BEBC000E for ; Thu, 24 Feb 2022 23:18:44 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-2d6bca75aa2so13694927b3.18 for ; Thu, 24 Feb 2022 15:18:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=IALWH9LaKSweUR5bZa1FpDcJHobIK4+OdaXeGu0F6JQ=; b=MKEmgeV8keFzqWZcHOcTiLZlqPGywLiEIsJ+DgmNvryiXNxyu0iL7DTY+w67cd7N7k 3SrxnI0w3asp3PQPS8RyPFn1S2QzEbAZzICzfGiUBdQ0zOd3QM2m7anzgZkE/OFUNnm2 Trbc5I7BZ4JFq/zvqlsAvlUaGqtmbcl4I7dcF55Yz65c2ncpU98D6jPjRtkrbUvaiom8 JuGXg5p2w1QeO+72Z+SdmmfhveJg62J4j+7SLMbU3UbDjpPQ0Nwaeijm+obBAATLI3L2 DoDXNVK0HR3xnlI5Z5rO3zzynt191z2ym/fOmCgg9aiIOJQTJDWI/3TXCBPSAmlUDgBo FZOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=IALWH9LaKSweUR5bZa1FpDcJHobIK4+OdaXeGu0F6JQ=; b=hxF6FYqAjDp4ipO0p8hWogg2A92NhKBDqAFyJ/A4dot9R0iP82b3EJRV3UFf+twOlL t7T4m+5/aPcuWfojquK3ewgse6fUIgquYYc374G81PsXAQXv3NHVoPqgI2L7J2vRy0IC jtZsb5csijwKzqltRZ85Ky1AoqxGaJ8l38Bje0JBjdrn9IHrDIyk9TzYeNFqopldxUGe s2Xomz1wOvZoy9HYva7z4laRQnuDtYKYa4de2TCC6d+GcaKoavfoCsfFqRXgMMWwsdCY ZIjKRnSfQU3LDABLxlvs0bEq2C4Ky4yZzFIBvhy2AP6Hz3+Ee8X2nCEeYj70BEeaBZfS hPzw== X-Gm-Message-State: AOAM532CW4IBTcm+qU4SHZq7BqB9gxMvQR4inzOYiUZNdsj1ATlfo6u+ HYJjxXOg+cjsqNhmAiMVOSDoe8T7bEE= X-Google-Smtp-Source: ABdhPJylfTOhMDpaEnhM3agqq1RqsTGVtn1V6AZ3i7i5WpAOEcsxHrT1VmuNtYbRHQllOOnIxVs6NSfcC+k= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:1c5c:1806:c1bc:a422]) (user=surenb job=sendgmr) by 2002:a25:5f41:0:b0:624:754d:4c04 with SMTP id h1-20020a255f41000000b00624754d4c04mr4603744ybm.121.1645744723614; Thu, 24 Feb 2022 15:18:43 -0800 (PST) Date: Thu, 24 Feb 2022 15:18:34 -0800 In-Reply-To: <20220224231834.1481408-1-surenb@google.com> Message-Id: <20220224231834.1481408-3-surenb@google.com> Mime-Version: 1.0 References: <20220224231834.1481408-1-surenb@google.com> X-Mailer: git-send-email 2.35.1.574.g5d30c73bfb-goog Subject: [PATCH v6 3/3] mm: fix use-after-free when anon vma name is used after vma is freed From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: ccross@google.com, sumit.semwal@linaro.org, mhocko@suse.com, dave.hansen@intel.com, keescook@chromium.org, willy@infradead.org, kirill.shutemov@linux.intel.com, vbabka@suse.cz, hannes@cmpxchg.org, ebiederm@xmission.com, brauner@kernel.org, legion@kernel.org, ran.xiaokai@zte.com.cn, sashal@kernel.org, chris.hyser@oracle.com, dave@stgolabs.net, pcc@google.com, caoxiaofeng@yulong.com, david@redhat.com, gorcunov@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com, syzbot+aa7b3d4b35f9dc46a366@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 70BEBC000E X-Stat-Signature: irre9itawgrikd7waz1gu8871c4nw1mg Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=MKEmgeV8; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3UxIYYgYKCO4ikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3UxIYYgYKCO4ikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1645744724-315192 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: When adjacent vmas are being merged it can result in the vma that was originally passed to madvise_update_vma being destroyed. In the current implementation, the name parameter passed to madvise_update_vma points directly to vma->anon_name and it is used after the call to vma_merge. In the cases when vma_merge merges the original vma and destroys it, this might result in UAF. For that the original vma would have to hold the anon_vma_name with the last reference. The following vma would need to contain a different anon_vma_name object with the same string. Such scenario is shown below: madvise_vma_behavior(vma) madvise_update_vma(vma, ..., anon_name == vma->anon_name) vma_merge(vma) __vma_adjust(vma) <-- merges vma with adjacent one vm_area_free(vma) <-- frees the original vma replace_vma_anon_name(anon_name) <-- UAF of vma->anon_name Fix this by raising the name refcount and stabilizing it. Link: https://lkml.kernel.org/r/20220223153613.835563-3-surenb@google.com Fixes: 9a10064f5625 ("mm: add a field to store names for private anonymous memory") Signed-off-by: Suren Baghdasaryan Reported-by: syzbot+aa7b3d4b35f9dc46a366@syzkaller.appspotmail.com Acked-by: Michal Hocko --- changes in v6: - Updated description clarifying the last reference requirement, per Michal Hocko - Added Ack, per Michal Hocko mm/madvise.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/madvise.c b/mm/madvise.c index 1f2693dccf7b..38d0f515d548 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -131,6 +131,8 @@ static int replace_anon_vma_name(struct vm_area_struct *vma, /* * Update the vm_flags on region of a vma, splitting it or merging it as * necessary. Must be called with mmap_sem held for writing; + * Caller should ensure anon_name stability by raising its refcount even when + * anon_name belongs to a valid vma because this function might free that vma. */ static int madvise_update_vma(struct vm_area_struct *vma, struct vm_area_struct **prev, unsigned long start, @@ -945,6 +947,7 @@ static int madvise_vma_behavior(struct vm_area_struct *vma, unsigned long behavior) { int error; + struct anon_vma_name *anon_name; unsigned long new_flags = vma->vm_flags; switch (behavior) { @@ -1010,8 +1013,11 @@ static int madvise_vma_behavior(struct vm_area_struct *vma, break; } + anon_name = anon_vma_name(vma); + anon_vma_name_get(anon_name); error = madvise_update_vma(vma, prev, start, end, new_flags, - anon_vma_name(vma)); + anon_name); + anon_vma_name_put(anon_name); out: /* -- 2.35.1.574.g5d30c73bfb-goog