Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp7769912rwb; Wed, 23 Nov 2022 10:28:33 -0800 (PST) X-Google-Smtp-Source: AA0mqf5wMKCdvO1IacQqScP6pWUxv3Y0/72WjbhEpVZ/twBL38rS6VznF5+QZ2T53+jPVjD/UVoW X-Received: by 2002:a17:902:db0b:b0:173:f3f:4a99 with SMTP id m11-20020a170902db0b00b001730f3f4a99mr13592064plx.79.1669228113061; Wed, 23 Nov 2022 10:28:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669228113; cv=none; d=google.com; s=arc-20160816; b=o++8VRaKA/5UEMIbNPMNj1fP7T4hyoRZIVx0u1aEBKRNE8M81VqdCsJadR3deiZAS0 8E7SBuCfCWu9Ny6BTZJlTkfSkD+HTa/o/xbqssKcpm/6rvY4kkfxtD3mOhOP09bvMS63 L2JDz5OOpP0Qn3/utZHwc6gyl6yzSrYB8OdUcTgPsuSt64k7+CJJ2dK8b5UdAu918hHG CVO0LHJ97xBboPUrTjawMkfs71ncL3DU7FhGpxzFbyPVnZ7zpm2/s6sMzaist4hPMzv6 L//oAIxj2wxpDCxaCzXkrc2tvEFz0Opbq+bmcePPONMegR9O08xR/d4zm/179D8H6EK6 4zHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=ZhRYpgjyXSj58WhO81SjAEi8hSt+BCz5HFDcwIe8pM8=; b=iJU38Ywe8Wpio3tW8Pfo/zHjGDfbq/JBj/v+BHoh+oVUrahT2EzLsPTVJly1FmnFXQ ouOVuORDzbCvfGC/nd1YhyjND13IrhfzUTNEKscB0U8C+OhRUtxgTNhZJaGcNjrGBno3 cj/Hn8kX472Q08dMtyfMNq5EI7cf/28ynYkfp2ttn+r+pGLaeOsDSRktALicNHn4MGG4 hCSbKZVlRfIPhbDRq9mm7N/Lvxcs4pUW33gzHwuBHj4UyqJcv6iaKbs1j2S2FzOqhqM1 m7T8xRbxW1yUyofMNvHk6Umn0JIHF0IbDqWJz5HMMi3mXFnT23+wocxd+c3+ilC7JovG clfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=eemCHDnP; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g3-20020a655803000000b0047697505449si16892833pgr.365.2022.11.23.10.28.20; Wed, 23 Nov 2022 10:28:33 -0800 (PST) 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=@google.com header.s=20210112 header.b=eemCHDnP; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239595AbiKWSOB (ORCPT + 88 others); Wed, 23 Nov 2022 13:14:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239406AbiKWSNa (ORCPT ); Wed, 23 Nov 2022 13:13:30 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95798248C3 for ; Wed, 23 Nov 2022 10:08:24 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id e12-20020a62aa0c000000b0056c12c0aadeso11480609pff.21 for ; Wed, 23 Nov 2022 10:08:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=ZhRYpgjyXSj58WhO81SjAEi8hSt+BCz5HFDcwIe8pM8=; b=eemCHDnPT999d32SWZB6wMX6suDtePfunnKNAKvzoIn3dNciBQ3Ljfi3yTquKoy4zi pcrK20E0xXlpAUqIBXCsUy2HESlz6t7owcjQppn946VULCvKTSBqzveGqfypNqgpNaj6 iXJJ06APrWP4InCRCTIf8kHE2/VDWVF+MFQq0ndYTrBiNiLs+FjRyPNEHDsG5r2ooIeI CsiIDq8CprGQJ677tUo/JkB1kA7RBtQGesqHsnudWvHEOOVStEsMkrcTe8XMdiBbDUUr Wvup2PTocsoLNg5b9xVg9/tSx6vcK13d6i/NYpDI5r7NH1ZyRGnIfOvOepA7ESo1xmo0 LQrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ZhRYpgjyXSj58WhO81SjAEi8hSt+BCz5HFDcwIe8pM8=; b=rrA/vhMaYFBsgDmnfTeBiSxjbchdan/avu1O32/yn5kDqV8nG94qpPcZKjKLwDUglg bAskeV0sn47kdLBvGjPDa3QWn5iBNLnVmkL2qOPtaxYtqGvSEem0T3MEcOz6fGYm3J1a j8TwfkNSHTqk2CakR5BUXmZNkgo8kgdGJo4IjdtoiR5X4echn7KMyBOZqcCY/LOuNgCs 5OjPBzto4lNmPGOJ95kk/+Z6YZOMe0GP5iN6DTT3QtvSu1eRvSnqGH1ol0KO66TKBFQy ew5Ao8MZ9aGyTP4VvvkSi3CM0VLPWIWgyIRCbNojOnGW5nE1duKBbUzbQdwHveRHrkgU 72sQ== X-Gm-Message-State: ANoB5pnnqrB7HoUKzKfcGgo4CAWHk88/Gn9s1TlUrcqxZFdDLqmzsfnP s4VN3ixnTgH+KlyueGuMcSA2L4aWsmklGw== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:aa7:83d3:0:b0:56e:8477:1add with SMTP id j19-20020aa783d3000000b0056e84771addmr19165796pfn.13.1669226904028; Wed, 23 Nov 2022 10:08:24 -0800 (PST) Date: Wed, 23 Nov 2022 18:08:09 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog Message-ID: <20221123180809.1501779-1-cmllamas@google.com> Subject: [PATCH 6.0] binder: validate alloc->mm in ->mmap() handler From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan , "Liam R. Howlett" Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, Jann Horn , stable@vger.kernel.org, Todd Kjos Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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 commit 3ce00bb7e91cf57d723905371507af57182c37ef upstream. Since commit 1da52815d5f1 ("binder: fix alloc->vma_vm_mm null-ptr dereference") binder caches a pointer to the current->mm during open(). This fixes a null-ptr dereference reported by syzkaller. Unfortunately, it also opens the door for a process to update its mm after the open(), (e.g. via execve) making the cached alloc->mm pointer invalid. Things get worse when the process continues to mmap() a vma. From this point forward, binder will attempt to find this vma using an obsolete alloc->mm reference. Such as in binder_update_page_range(), where the wrong vma is obtained via vma_lookup(), yet binder proceeds to happily insert new pages into it. To avoid this issue fail the ->mmap() callback if we detect a mismatch between the vma->vm_mm and the original alloc->mm pointer. This prevents alloc->vm_addr from getting set, so that any subsequent vma_lookup() calls fail as expected. Fixes: 1da52815d5f1 ("binder: fix alloc->vma_vm_mm null-ptr dereference") Reported-by: Jann Horn Cc: # 5.15+ Signed-off-by: Carlos Llamas Acked-by: Todd Kjos Link: https://lore.kernel.org/r/20221104231235.348958-1-cmllamas@google.com Signed-off-by: Greg Kroah-Hartman [cmllamas: renamed alloc->mm since missing e66b77e50522] Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 9b1778c00610..64999777e0bf 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -760,6 +760,12 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc, const char *failure_string; struct binder_buffer *buffer; + if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) { + ret = -EINVAL; + failure_string = "invalid vma->vm_mm"; + goto err_invalid_mm; + } + mutex_lock(&binder_alloc_mmap_lock); if (alloc->buffer_size) { ret = -EBUSY; @@ -806,6 +812,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc, alloc->buffer_size = 0; err_already_mapped: mutex_unlock(&binder_alloc_mmap_lock); +err_invalid_mm: binder_alloc_debug(BINDER_DEBUG_USER_ERROR, "%s: %d %lx-%lx %s failed %d\n", __func__, alloc->pid, vma->vm_start, vma->vm_end, -- 2.38.1.584.g0f3c55d4c2-goog