Received: by 2002:a05:7412:798b:b0:fc:a2b0:25d7 with SMTP id fb11csp620055rdb; Thu, 22 Feb 2024 14:07:19 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXkwTn3gEsjAIPnhlvoyEgNFESgO1Pk9KDdueLJS5sD1YRVcgWDGTYvMKRZ5LRJzfZMvrIBg9pxfnM2UQ03nIK8mvjmpuCw/dbolTgs8w== X-Google-Smtp-Source: AGHT+IGglYrVt2x8F1YXc9sIfYTZoNwMjoEyGOA4/5tnWb/8wNYbByNBqRV3okFnmCleHjWoaRfr X-Received: by 2002:a05:620a:14ae:b0:783:ae0c:5e8f with SMTP id x14-20020a05620a14ae00b00783ae0c5e8fmr293921qkj.71.1708639639323; Thu, 22 Feb 2024 14:07:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708639639; cv=pass; d=google.com; s=arc-20160816; b=x17iQlmgZ539//EIGzuD3mijRQTLjHOmB7Evh2xZQsObTtesVUOddz6YzJXnZYxZQu dw8B5KhDowLqDYmwrVWlS9QW/PQ+T3L+LfXBxMa8Dn9psJwt5Pc/tatJaPGbheJfVFxZ /d9KCbmeY5+onE+04JqBLZL1u6CdUcH0eY+WilTdX91S1dJnqLrnvxxKmApk+CQPMDqP Z6PgH4dgz7e2HCamjnFl+0gHhiDNZlCLS2G5gvqVci3M09WVNBu3WA/6Dy27NIXsOaFn 6FfE7CYPY9QI9iZfcVP6s7xuyl8QxguNjVMuhyMeM776G6Jo0PCe/Fd5dF55HMfF8Lkm iPgQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=2xO2+giGNpQMfCaj0W7NZUwdaHnujksHyJgZqAdjT78=; fh=9vtbVmkJ/pfn6X5twcAgLhOEqThBKbKwYoJu8yMwtZU=; b=D6m/MHhnGl6xEAkIIqhVqlPDAP4cvVxA/V80ySetI+4uW+h2AOsMmuMKeT3S12mjWy njpYD7PX/oMouXvHPltr+MR9iR7eXk5BsmiVi88rM4abKreVVAV2uzdohL5Emejt1HdS 61tWSoHDX7kD+NY2Ld6tLcgGSkk60gmCyHEyYLWqY7kuJzWpIA9oL8DUa/K7X14JVi0j aocnbO51l558KId4Niv2iS68fLIS50n0iHElF5ppR1N00RlZPQ61pfCr0o8ee7VT8XSt 7ZJRSFyNFHBS3ZxEESlk3PLv6R/YEQT6YNtaeFG2wDmuQY/7BAWPzzc9CXl7NhsOCstS 7GXw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JQrkE2pU; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-77446-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77446-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id o4-20020a05620a22c400b0078553473225si13654881qki.419.2024.02.22.14.07.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:07:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77446-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JQrkE2pU; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-77446-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77446-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 08BAF1C21F85 for ; Thu, 22 Feb 2024 22:07:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 09DAC12D21A; Thu, 22 Feb 2024 22:07:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JQrkE2pU" Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C1A912D1EC; Thu, 22 Feb 2024 22:07:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708639630; cv=none; b=SjMqrw0qQTnsbc3nPK1vgGE20E/Y31ObNHUGPxrftlUHbiyTnyio+XomykfXPIvzJo1Avz1fwOZHI06zAilN0ysmYLo3qJdZIK+1UVWkQsZwDBIEmOHh1r0M5g8hBxoR5HL9CrrOJHZY6lwOvA4IqBQLLFMq3G2ID3OyNcdpUIE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708639630; c=relaxed/simple; bh=Jj7+pmu+kgGbQkhHoVgKs05d4OY3Rsuej9zneaZfX9c=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=SODfz4Hm6lZSiwemyU9LfG7M9eHnDls0b/w48Atp2wZmOzhEgxbyiVhDaQ4PPP55QovsZq67OWM32ZA7kdNpJqa3genGetDgjAMy2FzTzte7G1ttYRJ98B7T+MNLQtl4DQpOzfGWf3A/ne+LFVdCh4HsmTrafSkjS39Ify/kFRc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JQrkE2pU; arc=none smtp.client-ip=209.85.221.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-33adec41b55so144060f8f.0; Thu, 22 Feb 2024 14:07:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708639627; x=1709244427; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=2xO2+giGNpQMfCaj0W7NZUwdaHnujksHyJgZqAdjT78=; b=JQrkE2pUj11TZN/etK0eOmyLD3HQ7na/Cx2Fcz67t53zKpBSrn9nm+gjVK/xLqxOdU olcyjWHh3ER/iAPEFlJ4wvAdVfBG0owDPGbygoBZwoBZxgYxchqOcYc5Ce8s9ahQIBO3 WIj4N8hveaHObBeVB7hLXKNixHE7CuJZs/RgYJMXrHD+tluc4f1Vjoxte5id3MjUPgM7 wddkGSMiW/FkK1ws0Qi+6Jdym/YmX16kHkaPB9UxMbbB44SigGaBsOpt/X1X8bS3my/I QSc6q5r04xL/7UsVo4TjT4heuAuGG8bOVRAk3rktGYI3W4mG62wzI/RqhjNmrea6VtVk NXNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708639627; x=1709244427; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=2xO2+giGNpQMfCaj0W7NZUwdaHnujksHyJgZqAdjT78=; b=V3RWGGyk8E0bYmdfiAPscTKXMU6xEoRq/zD/w33oYm0X2AHGyWo27FO2K1R5Cvkuhm n1c4Uf0OY5vaGcgauohlYp/NNd53dMn7FZqYO6OnxX3GSdcGkGMGHlbOKiT4rs34kyWP VUmpT1Vxryken4ttc3kMOvx4vup/tm8lJEdNSqZ7ImqI4WMtI7OqYlmFQe0mio2iV0q0 w5u8AXjVkbAolhMU4KYENpPQ0mzLpgB9HIRROyeeZxofFyu2aH2ZQ9ZXK1vT8Mn9uXZY boILNRSZsu12tUwrwAonuXfvMgFO42ZmMLGCjfiP9qt6CMIiFy93IRiXzmXXiysboGUT BqMg== X-Forwarded-Encrypted: i=1; AJvYcCXIdDV/aVXefyrR7L0rsMqU7Xc4ib5qhkAdZ9mqpV0zMzPrfx1nAtW1TTJeJ6W9cUAmHvj1Iecjg12uKtRflTFLTurnrPJIc7744ZMHGaex5SQVO3GUVtPWBikcBzWN5BZyzmS7 X-Gm-Message-State: AOJu0YwJZ2fln49MAvhWNATLAfcz66l7NMzTilstYuPWraE5iC0zxiA2 CBbys0eb7K4VmdUEwKIGRUYr071rcguKtHz7KsiMQBrJQ8eRiQii X-Received: by 2002:adf:ec81:0:b0:33d:73e4:9c79 with SMTP id z1-20020adfec81000000b0033d73e49c79mr228529wrn.59.1708639626512; Thu, 22 Feb 2024 14:07:06 -0800 (PST) Received: from localhost (host86-164-109-77.range86-164.btcentralplus.com. [86.164.109.77]) by smtp.gmail.com with ESMTPSA id h14-20020adfa4ce000000b0033d56aa4f45sm317637wrb.112.2024.02.22.14.07.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:07:05 -0800 (PST) Date: Thu, 22 Feb 2024 22:04:51 +0000 From: Lorenzo Stoakes To: Vlastimil Babka Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Michal Hocko , "Liam R . Howlett" , stable@vger.kernel.org Subject: Re: [PATCH v2] mm, mmap: fix vma_merge() case 7 with vma_ops->close Message-ID: <8dc5c86e-f74d-4547-99c9-3bd4a0d92cde@lucifer.local> References: <20240222215930.14637-2-vbabka@suse.cz> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240222215930.14637-2-vbabka@suse.cz> On Thu, Feb 22, 2024 at 10:59:31PM +0100, Vlastimil Babka wrote: > When debugging issues with a workload using SysV shmem, Michal Hocko has > come up with a reproducer that shows how a series of mprotect() > operations can result in an elevated shm_nattch and thus leak of the > resource. > > The problem is caused by wrong assumptions in vma_merge() commit > 714965ca8252 ("mm/mmap: start distinguishing if vma can be removed in > mergeability test"). The shmem vmas have a vma_ops->close callback > that decrements shm_nattch, and we remove the vma without calling it. > > vma_merge() has thus historically avoided merging vma's with > vma_ops->close and commit 714965ca8252 was supposed to keep it that way. > It relaxed the checks for vma_ops->close in can_vma_merge_after() > assuming that it is never called on a vma that would be a candidate for > removal. However, the vma_merge() code does also use the result of this > check in the decision to remove a different vma in the merge case 7. > > A robust solution would be to refactor vma_merge() code in a way that > the vma_ops->close check is only done for vma's that are actually going > to be removed, and not as part of the preliminary checks. That would > both solve the existing bug, and also allow additional merges that the > checks currently prevent unnecessarily in some cases. > > However to fix the existing bug first with a minimized risk, and for > easier stable backports, this patch only adds a vma_ops->close check to > the buggy case 7 specifically. All other cases of vma removal are > covered by the can_vma_merge_before() check that includes the test for > vma_ops->close. > > The reproducer code, adapted from Michal Hocko's code: > > int main(int argc, char *argv[]) { > int segment_id; > size_t segment_size = 20 * PAGE_SIZE; > char * sh_mem; > struct shmid_ds shmid_ds; > > key_t key = 0x1234; > segment_id = shmget(key, segment_size, > IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); > sh_mem = (char *)shmat(segment_id, NULL, 0); > > mprotect(sh_mem + 2*PAGE_SIZE, PAGE_SIZE, PROT_NONE); > > mprotect(sh_mem + PAGE_SIZE, PAGE_SIZE, PROT_WRITE); > > mprotect(sh_mem + 2*PAGE_SIZE, PAGE_SIZE, PROT_WRITE); > > shmdt(sh_mem); > > shmctl(segment_id, IPC_STAT, &shmid_ds); > printf("nattch after shmdt(): %lu (expected: 0)\n", shmid_ds.shm_nattch); > > if (shmctl(segment_id, IPC_RMID, 0)) > printf("IPCRM failed %d\n", errno); > return (shmid_ds.shm_nattch) ? 1 : 0; > } > > Fixes: 714965ca8252 ("mm/mmap: start distinguishing if vma can be removed in mergeability test") > Signed-off-by: Vlastimil Babka > Reported-by: Michal Hocko > Cc: Liam R. Howlett > Cc: Lorenzo Stoakes > Cc: > --- > v2: deduplicate code, per Lorenzo > mm/mmap.c | 10 +++++++++- > 1 file changed, 9 insertions(+), 1 deletion(-) > > diff --git a/mm/mmap.c b/mm/mmap.c > index d89770eaab6b..3281287771c9 100644 > --- a/mm/mmap.c > +++ b/mm/mmap.c > @@ -954,13 +954,21 @@ static struct vm_area_struct > } else if (merge_prev) { /* case 2 */ > if (curr) { > vma_start_write(curr); > - err = dup_anon_vma(prev, curr, &anon_dup); > if (end == curr->vm_end) { /* case 7 */ > + /* > + * can_vma_merge_after() assumed we would not be > + * removing prev vma, so it skipped the check > + * for vm_ops->close, but we are removing curr > + */ > + if (curr->vm_ops && curr->vm_ops->close) > + err = -EINVAL; > remove = curr; > } else { /* case 5 */ > adjust = curr; > adj_start = (end - curr->vm_start); > } > + if (!err) > + err = dup_anon_vma(prev, curr, &anon_dup); > } > } else { /* merge_next */ > vma_start_write(next); > -- > 2.43.1 > Looks good to me, feel free to add: Reviewed-by: Lorenzo Stoakes