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 79FD1C5478C for ; Thu, 22 Feb 2024 18:54:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F20E36B0075; Thu, 22 Feb 2024 13:54:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ED0986B007B; Thu, 22 Feb 2024 13:54:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D711B6B007D; Thu, 22 Feb 2024 13:54:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C4E726B0075 for ; Thu, 22 Feb 2024 13:54:17 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6F5FF40CB2 for ; Thu, 22 Feb 2024 18:54:17 +0000 (UTC) X-FDA: 81820340154.25.B09C5AE Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) by imf10.hostedemail.com (Postfix) with ESMTP id 95C3DC0012 for ; Thu, 22 Feb 2024 18:54:15 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CDgCTCyp; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.49 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708628055; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=uUveRwg7p1Ajr3BNGaCAfvh6euWerQ4mOYsRYfUynZc=; b=fso5KaSdl64T08VFCd0IMlK5pedwC2zo9oNtugl0aI3jI0AbCr55P7w/OcehPy0RKcYCwx 17BGRgZbsjKoLKXKhhGYTJbMkKP9A+FE66n25sviEStcnpWiy8XaTs3DqfVGTNsxxkU8o6 rpgluYVI1RylZMA5XPclvXY/YybxLFQ= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CDgCTCyp; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.49 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708628055; a=rsa-sha256; cv=none; b=kWwamOTB6FD/DjsfQ63gfChfEdo+WmZFDaOcCaN1ClSsA7IfQCPoSLX0YIgER/wx/aZbw6 op67emIJjlaye/apoHiFEKvUWUXQQHtE/mAx9Hul6hvT4svI/NXCFO/X/59nlSs3lpELSd GMmQPSrlbwYnxwwfTpkk+qY58jFMUpQ= Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-33d28468666so46708f8f.0 for ; Thu, 22 Feb 2024 10:54:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708628054; x=1709232854; darn=kvack.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=uUveRwg7p1Ajr3BNGaCAfvh6euWerQ4mOYsRYfUynZc=; b=CDgCTCypIOKHjeUHdDtGjp4usn0929oSz5CQ4HzRQQhANvBUznvLREdBYqjXxtZEW2 /qyYMKDuglvH5FcpG/eCMmOzMK4rMHxqoiwxw+jgK3FErL7bUOdZaSIi2SxL4rHd6NPa Q0/TW18bf/7RiktIdjMZniQo4YvQmCce7/Xy4nYN+h7KUS/slb8orj0fYS6iop9t8/TM HeLLKrCyNzpAp1mvlqtsAOX0lloCq3MK/FUGQinE8IstNwOiy2Y0FGiS5xvXZEfRQtIz YRaQ3bKRVWZzDyCYzZewUD9mlFxj5S/XnsQN9InffOPxm1l/I3lGi6R6h6QW9CfkLLTp 6sfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708628054; x=1709232854; 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=uUveRwg7p1Ajr3BNGaCAfvh6euWerQ4mOYsRYfUynZc=; b=eVrXa3yqeSEjTE0BpRB7dZNTHuc3wIDqFxI2DKZKlvguj/w95x10g4AqQ33Dt7aXLk M0TyTfOvKQKoE+3ovfdsi+Bd7OxP9OhiqMExTtRlKkiWZ8qK/u+8dMPnJ0ErwLNkUkUm TxwQjE1stHX6mSZd49Qtcuq77p93XcKRFuTHaUpXq+i8hNkOG5NmjSam5o62euLzUfOn +A5sOHxhzBogu1yET9/DkDbSzQtWzPP533qla8es9sBpjYROx1bYQFBvoaMppEhBP9ln 6ctYeKp0JF+pNL51M2LTZKgykJw+7toTMJZ1aFuCoOnzF5hgK7DbF2jEHNcJcOE/Uy+w mODQ== X-Forwarded-Encrypted: i=1; AJvYcCWfgT6U1kfBwe0QyfkW/qHQR34Nvc8XmdqCtn5OXAEUbVfBYLGBcJ5CA5w6Im6Ao48UPqGfQqPZy16++i9aac35bHc= X-Gm-Message-State: AOJu0YzGJOKoZdD4CFZY1D9JAxrLxVAcvQx4CjGoWBy+n11aV1M1cwc5 qgwAyT9umX0VX51iIMkLv8zA4UR0S65L+QP8GxHE+PANg7oRDGnC X-Google-Smtp-Source: AGHT+IE9WmNFgdG/1L1kexwdly0Wru10Q7ftPEjrThRsR3vLWcN21sNvlNn+IdKJATom/w3SZKD9Xg== X-Received: by 2002:a05:6000:809:b0:33d:1656:2204 with SMTP id bt9-20020a056000080900b0033d16562204mr19886534wrb.53.1708628053705; Thu, 22 Feb 2024 10:54:13 -0800 (PST) Received: from localhost (host86-164-109-77.range86-164.btcentralplus.com. [86.164.109.77]) by smtp.gmail.com with ESMTPSA id b1-20020a05600003c100b0033d81d9c44esm5995447wrg.70.2024.02.22.10.54.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 10:54:12 -0800 (PST) Date: Thu, 22 Feb 2024 18:51:58 +0000 From: Lorenzo Stoakes To: Vlastimil Babka Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Liam R. Howlett" , Michal Hocko , stable@vger.kernel.org Subject: Re: [PATCH] mm, mmap: fix vma_merge() case 7 with vma_ops->close Message-ID: References: <20240222165549.32753-2-vbabka@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240222165549.32753-2-vbabka@suse.cz> X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 95C3DC0012 X-Stat-Signature: uuq4383b8568nkapea7bwhykq3wohp1s X-Rspam-User: X-HE-Tag: 1708628055-915328 X-HE-Meta: U2FsdGVkX19RzPMtI7fmw6K9/V+ibUbwm5thAGrlQ9gSJ+xwAXJ9ahU2naxo9FynLF0eMQj6SFivNdLgPG2U7n5vq4CdncBrFGeqzPOlj61Jbdjv2je4FNkabbcH9dfTA1XVzbljE9sXerxCTKu78rgzK9Hix/Myl1mJ9t1vTSgxQoLlJoOHmTEo+IFAuWlXFNMVl/VxcpYreRICsCmFi1l0jc7vb0ZG4j9+5FrEQE58dqu7x4pnYEzaFoICS5hvKxBuQ68jVB4sf16dew/Xd+5r2YSixsEMyNHotnaJFuYXEeTRSz9CdjhpoN+R5xfbzkj8piFaz4CH9ys09Fr3oogE9cdUgT9ay4JV2rvB1zb6CiZq7BqwYrvs0gRMC9WF1Ovrn7Z21wLllQiwNLdVwWp/sJ/IHUIh/GdmDl29LA+AGt+7ZxZz6IAOFyzQg82DFqaNKM23sMBN4umGUzUXyONXDoOQBBERaUKqC3wn7vKUunnBw5IzxiyHrmg2lKU3aocQIUbPiDnmt50VAcEv2RuWfmlK8Ll9AWHwTRZlG99H+7WLh6Noe606nEtDchcdn8/8bRAMUqMmjVIRqyB88HKiUwemnBPVJsbOxltiwd1mvNW6Tl8HSl0gx770+9NNZyGVk3cqST9HrWBkhFFkOF5CJNUSVBSXF+lvlgXj79tzn7V41nOdX2+wLLQheY955B7L6pbCKSiFJuPWNnAbvnQOZqcvgZ01CN2f19cIm6MoR57OBJxDhrk+2ho7sn97YR/cYxQroMBR5O5h4hXcBKg5YwbYH1e0U+9dF9w+gw830pulDEUxVEsRv1ojGdnjdXWWk1e8VLWFhj5weiuqRONBGCq5V2SmtCUnAde9+MU8mE8dBYxeJKJK6anzO4Ub8vKHBfpmwgZVArRB8+fv8m0BavOlKSdKXJU+54WdwrE7lsCLYZWloEA0Jg5TO5cCRWBH8UrBkCMvxB/UJR8 FS020xZS BFEgEbt4CJGsNHTUCpI38ZB+nitPv12OXoNaonfE9YoBqr3s2DpGgntQZgAqWAndODnibEl4ueOE79gc= 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: List-Subscribe: List-Unsubscribe: On Thu, Feb 22, 2024 at 05:55:50PM +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. Let's do that pretty soon :) this is a bit of an ugly fix but understandable to do it in this form to make it easier to backport (+ perhaps generate some CVEs? :) > > 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. I concur, all the other cases require merge_next which would have invoked can_vma_merge_before() that calls is_mergeable_vma() with may_remove_vma set to true hence performs the close check. > > 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") > Reported-by: Michal Hocko > Cc: > Signed-off-by: Vlastimil Babka > --- > mm/mmap.c | 11 ++++++++++- > 1 file changed, 10 insertions(+), 1 deletion(-) > > diff --git a/mm/mmap.c b/mm/mmap.c > index d89770eaab6b..a4238373ee9b 100644 > --- a/mm/mmap.c > +++ b/mm/mmap.c > @@ -954,10 +954,19 @@ 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; > + else > + err = dup_anon_vma(prev, curr, &anon_dup); > remove = curr; > } else { /* case 5 */ > + err = dup_anon_vma(prev, curr, &anon_dup); This (ironically) duplicates code, could we pull this out of the if/else and put it afterwards like: if (!err) err = dup_anon_vma(prev, curr, &anon_dup); > adjust = curr; > adj_start = (end - curr->vm_start); > } > -- > 2.43.1 >