Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp7839044rwb; Wed, 23 Nov 2022 11:25:55 -0800 (PST) X-Google-Smtp-Source: AA0mqf7lHyE9cCiXJWM9giYuT6+Ru7vITUow9syu+NfieHeMuPtufQRN3iRKyGFwCb4Li0yxzsDI X-Received: by 2002:a17:903:2144:b0:186:e5dc:4c55 with SMTP id s4-20020a170903214400b00186e5dc4c55mr10383255ple.148.1669231554874; Wed, 23 Nov 2022 11:25:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669231554; cv=none; d=google.com; s=arc-20160816; b=sGkNqWw2IARZ3nNfHVd2SrGYB/Wp+uFDRw3sxY6iKPZ1lMH5Ma78NHN8uk6a14JiB3 tXna/YySEKkMB+ML3iKwIBayXtZqJjk6lBbsdIsaLjCwTYGkCGoHUf0yzf2y5g0PisKq pTTFh8hWC6NsNPrf6Yc2a6dbZUlNa5QojQ7hO/yJBVSWM4uWpjR58Ul6+G8p3a5jaq5C 3j4WYXWqv7+vhXW6v6xy6ZxYzylS7mbWhuRSVrFefSpG+zDxLm4hSrliuMXUA84UN/9i c5BGzTfcTfBmhQVTvvJabh50paZJSPaFCzbH7wkxPeK2HkLqtqhK8OQclDRIYi/F0eiW 4o9w== 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=8AnrcGQxermayS9lzY4/PftYYJ1h7RYNfHX8xCVtV1c=; b=K0Xtji/S99pSUmWer8cIbyiABW+dxZ0BF5HuACHHPom1G/r+Uc/RpdAXfwelTeh04Q xFOdd9OINq9vk0u9CBo1J/2QDlnFOutgj1Jbw+USGLMpHKhu4ydAYYRHeSe7sV+uCses YxTO5jtSTekZwALETt2V1gMBXUiyxDAwG+z7iHXDVqeiJD933ajIKLCJWUazxphuu1V0 7rLTsfNi8ZyHcv5amwx2bXPEjxa6KnHhlDcoUnpjmMq64CS3eP7MS3ySHEnKySqTrCCE PZtA8PmgHjfUTcC4O1B4U4uqdIEhjzpEJl2VVQ54Qf8MellU4hx9md9s4xLfQ2XUHZu3 bQJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=mLkBARzh; 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 cw15-20020a056a00450f00b0056d919bf4a8si16265320pfb.270.2022.11.23.11.25.43; Wed, 23 Nov 2022 11:25:54 -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=mLkBARzh; 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 S239547AbiKWSOM (ORCPT + 88 others); Wed, 23 Nov 2022 13:14:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239532AbiKWSNc (ORCPT ); Wed, 23 Nov 2022 13:13:32 -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 B55AD65AE for ; Wed, 23 Nov 2022 10:09:25 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id bq9-20020a056a000e0900b00571802a2eaaso11522065pfb.22 for ; Wed, 23 Nov 2022 10:09:25 -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=8AnrcGQxermayS9lzY4/PftYYJ1h7RYNfHX8xCVtV1c=; b=mLkBARzh9DEoczzapN40SjDSvfSO9nAnK4l7SEQlkecJWP9ks6Bnl+jjSjXJFx96mP X/zxr5Gdo+FYdbk0nY5U+gtLuL1HoNADzQsH1vNYJ3aHZjb5OWnAkVWeYx1Nr4cx608+ 5rCF9s1+CsORn3Gva3cK6mJniR0xr8NdKtzQ/lt/wo61RW1zH4tnHKYqX+nl2bZMMJmO P19cZl/P1PQEyizWuij5WoOIv41uIflr/80PPs00WtxoMR/gbsEqqJSyrAeFhZGKAW5v 9FWFTV250Y2UnKZXM0FOoER96PJUv6pk6HezB4soFYS5xu5FUh9wZ5o49oM2hkb5wfk+ uqWQ== 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=8AnrcGQxermayS9lzY4/PftYYJ1h7RYNfHX8xCVtV1c=; b=3G9UHYR7Uk/VMxiK+je4JaxQbRRu7I1VnGXcdImofZi/yh35FGh5bQRDBoD7O1ra/I epu6FKd/BTNMoVF5n/cL9FiZDfpJn3AHT62rc1jIHnHWGIEBFVmHJ/q+hLY+M6Iu3ZiY 6hjtnnuQYRww0ZOa4BAgdZlGfm6m/xAF7D0M0hYRL5W8VoApIUxoWdLhwpN06Y4tJsa2 /zoOqkxMocRL3VPXnkBb+9MNoGgRJgvOdx9j3Q7JfHmxfksUkk+1I/w38YbVq1ohvC+n v5an95CvLD3ZbvE5sAlcrNGEL24kg7QJIomB96BrWes5Knkm15YVSdPoiVSTMalu+gmf qHeg== X-Gm-Message-State: ANoB5pmoKpFfyLpgUv87HByOUb23PZ+OIoQ8fAGArvf0kwsBe9UXsX2E UnfZtMQRp7RnRMU0UBTAjy2Pj7z2J2wqlA== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a17:90a:cf08:b0:218:6bd8:3114 with SMTP id h8-20020a17090acf0800b002186bd83114mr31561207pju.128.1669226965293; Wed, 23 Nov 2022 10:09:25 -0800 (PST) Date: Wed, 23 Nov 2022 18:09:21 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog Message-ID: <20221123180922.1502550-1-cmllamas@google.com> Subject: [PATCH 5.15] 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 , Hridya Valsaraju , Suren Baghdasaryan , "Liam R. Howlett" , Carlos Llamas 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 8ed450125c92..6acfb896b2e5 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -753,6 +753,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; @@ -799,6 +805,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