From nobody Fri Dec 19 20:33:13 2025 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 668C2FA3740 for ; Mon, 24 Oct 2022 13:55:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236693AbiJXNzO (ORCPT ); Mon, 24 Oct 2022 09:55:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236591AbiJXNxj (ORCPT ); Mon, 24 Oct 2022 09:53:39 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 211EFBBE3A; Mon, 24 Oct 2022 05:43:25 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 3A1FD612E4; Mon, 24 Oct 2022 12:35:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 463ABC433D6; Mon, 24 Oct 2022 12:35:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666614933; bh=Rk1kpjjh/1GHPWcIQq0Axwe2OW0md26RjPeSe1XOlAY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JzQ6AZP7YupayTYPyW7ZJxvbVy707ueIeVKKLFTfqt4xHMmZSxBnvISntxdZYptuK ic+nS63FLhP8r5crDvwb6dH4h028QZITaM9+GC2i/rOeIza6CXPvEEIUhLj/NNZKvM Z1e1Ihx1r3sG4ZC/jj5gWwvFzw+mzaw7SzotzgsU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Carlos Llamas , Catalin Marinas , Andrii Nakryiko , Liam Howlett , "Christian Brauner (Microsoft)" , Michal Hocko , Suren Baghdasaryan , Andrew Morton Subject: [PATCH 5.15 058/530] mm/mmap: undo ->mmap() when arch_validate_flags() fails Date: Mon, 24 Oct 2022 13:26:42 +0200 Message-Id: <20221024113047.673447410@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113044.976326639@linuxfoundation.org> References: <20221024113044.976326639@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Carlos Llamas commit deb0f6562884b5b4beb883d73e66a7d3a1b96d99 upstream. Commit c462ac288f2c ("mm: Introduce arch_validate_flags()") added a late check in mmap_region() to let architectures validate vm_flags. The check needs to happen after calling ->mmap() as the flags can potentially be modified during this callback. If arch_validate_flags() check fails we unmap and free the vma. However, the error path fails to undo the ->mmap() call that previously succeeded and depending on the specific ->mmap() implementation this translates to reference increments, memory allocations and other operations what will not be cleaned up. There are several places (mainly device drivers) where this is an issue. However, one specific example is bpf_map_mmap() which keeps count of the mappings in map->writecnt. The count is incremented on ->mmap() and then decremented on vm_ops->close(). When arch_validate_flags() fails this count is off since bpf_map_mmap_close() is never called. One can reproduce this issue in arm64 devices with MTE support. Here the vm_flags are checked to only allow VM_MTE if VM_MTE_ALLOWED has been set previously. From userspace then is enough to pass the PROT_MTE flag to mmap() syscall to trigger the arch_validate_flags() failure. The following program reproduces this issue: #include #include #include #include #include int main(void) { union bpf_attr attr =3D { .map_type =3D BPF_MAP_TYPE_ARRAY, .key_size =3D sizeof(int), .value_size =3D sizeof(long long), .max_entries =3D 256, .map_flags =3D BPF_F_MMAPABLE, }; int fd; fd =3D syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); mmap(NULL, 4096, PROT_WRITE | PROT_MTE, MAP_SHARED, fd, 0); return 0; } By manually adding some log statements to the vm_ops callbacks we can confirm that when passing PROT_MTE to mmap() the map->writecnt is off upon ->release(): With PROT_MTE flag: root@debian:~# ./bpf-test [ 111.263874] bpf_map_write_active_inc: map=3D9 writecnt=3D1 [ 111.288763] bpf_map_release: map=3D9 writecnt=3D1 Without PROT_MTE flag: root@debian:~# ./bpf-test [ 157.816912] bpf_map_write_active_inc: map=3D10 writecnt=3D1 [ 157.830442] bpf_map_write_active_dec: map=3D10 writecnt=3D0 [ 157.832396] bpf_map_release: map=3D10 writecnt=3D0 This patch fixes the above issue by calling vm_ops->close() when the arch_validate_flags() check fails, after this we can proceed to unmap and free the vma on the error path. Link: https://lkml.kernel.org/r/20220930003844.1210987-1-cmllamas@google.com Fixes: c462ac288f2c ("mm: Introduce arch_validate_flags()") Signed-off-by: Carlos Llamas Reviewed-by: Catalin Marinas Acked-by: Andrii Nakryiko Reviewed-by: Liam Howlett Cc: Christian Brauner (Microsoft) Cc: Michal Hocko Cc: Suren Baghdasaryan Cc: [5.10+] Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- mm/mmap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1836,7 +1836,7 @@ unsigned long mmap_region(struct file *f if (!arch_validate_flags(vma->vm_flags)) { error =3D -EINVAL; if (file) - goto unmap_and_free_vma; + goto close_and_free_vma; else goto free_vma; } @@ -1876,6 +1876,9 @@ out: =20 return addr; =20 +close_and_free_vma: + if (vma->vm_ops && vma->vm_ops->close) + vma->vm_ops->close(vma); unmap_and_free_vma: fput(vma->vm_file); vma->vm_file =3D NULL;