Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp2935328pxb; Mon, 18 Apr 2022 11:22:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxg+nTLg/IUlCVnxMDQVqYnJ7fmQilp9wBSftzuJY5MFvt3cZ85YiOzE5zUZyu9hJ+aucwB X-Received: by 2002:a17:90b:4c88:b0:1ce:88fc:29fe with SMTP id my8-20020a17090b4c8800b001ce88fc29femr14132007pjb.216.1650306163087; Mon, 18 Apr 2022 11:22:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650306163; cv=none; d=google.com; s=arc-20160816; b=iY3E6f4tv7jBBr5F70Rg+dWtcVVyD3z7KMYCMt2lyt8crqS3ao00TU6r+qc80epJoH 2EAtRHf0Fw51tlyo18/Ed+hhbDhqTEbWTeyTSZQrSJh4tI1ju3hvY5P008xmA30YSy3M HAeGPWw2MNZknpRBmXSeGr8XHttNsuDavqaQavNTLeRX4Zml4LOldlxeIwizRSmEmNhl kzj4glQaAu1S+Y4Lz6TSFS9WzK1+VXrBZ1v+PEb9yPrVffdlkO8xHWK6jXk/HwTRyMc2 FW5i2Ra0aaIYl7n0df5amSEi1j2bxlutMK5L29eDDokk1dcizNUKM/aDp+jmqNUO7Nbj fm6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=OSL5e14BRz/kXcuEeSwN1fGGasBaA6A2wO5mT5gHJYA=; b=Q24Z1g/Y+BKtsstcGlzuSd1AZK9k+vT92EQT8DdwuqbnWmAVzMuU4UDj152rJYmnlt A2TJqCi0cbdfSVvUU3qSkue30eftL8cV2r5LtCJ+uu5hQifP9rGGbnBFuDidSTJOfCUh YUmbq+uwMBsdT8GmPfR6lSkw8WEVhKjD6VIPdJg4Evh7dvRTycKlxCh19DVlMofZb64j X6kuaE9FaVzGTrnGqXEx5ik5uynG8r35SfuNbgE7FBR60/bTK1JQ8eGsZWjN9Ac7nkFJ fnzHNIIrfVDfzItSCjhmyUBXShoFmTZA8INQQJNtgx/aDo33ARRmJESaxDBDIGGsN9Dh yVyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aPyWLjc9; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y186-20020a638ac3000000b003a2e4a3b2d3si9448964pgd.163.2022.04.18.11.22.25; Mon, 18 Apr 2022 11:22:43 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b=aPyWLjc9; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245404AbiDROCz (ORCPT + 99 others); Mon, 18 Apr 2022 10:02:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244332AbiDRNn7 (ORCPT ); Mon, 18 Apr 2022 09:43:59 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EABE43A723; Mon, 18 Apr 2022 05:59:54 -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 079B9609FB; Mon, 18 Apr 2022 12:59:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1406FC385A1; Mon, 18 Apr 2022 12:59:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286793; bh=PEaXFzMkDHYkPb3G0Mpf3R+xyJLmQHdq0HhfbNpRfKY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aPyWLjc9yqyDWIetPPkuIu7kTjiOkzq3RWwUO5OGEHPfnFd6WnxBlHjkqsy2xtRi4 1d3jCC3qnlSY0GxNjZRdEBnTulzTAt0vbtrDJp7l6eWgGySHSCHT1iHaKrPx0Vv6aa POycbMjHfrLLojKJyGA221dC6YArDDvDTOOy31G0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Miaohe Lin , Michal Hocko , KOSAKI Motohiro , Mel Gorman , Andrew Morton , Linus Torvalds Subject: [PATCH 4.14 246/284] mm/mempolicy: fix mpol_new leak in shared_policy_replace Date: Mon, 18 Apr 2022 14:13:47 +0200 Message-Id: <20220418121219.122820428@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: Miaohe Lin commit 4ad099559b00ac01c3726e5c95dc3108ef47d03e upstream. If mpol_new is allocated but not used in restart loop, mpol_new will be freed via mpol_put before returning to the caller. But refcnt is not initialized yet, so mpol_put could not do the right things and might leak the unused mpol_new. This would happen if mempolicy was updated on the shared shmem file while the sp->lock has been dropped during the memory allocation. This issue could be triggered easily with the below code snippet if there are many processes doing the below work at the same time: shmid = shmget((key_t)5566, 1024 * PAGE_SIZE, 0666|IPC_CREAT); shm = shmat(shmid, 0, 0); loop many times { mbind(shm, 1024 * PAGE_SIZE, MPOL_LOCAL, mask, maxnode, 0); mbind(shm + 128 * PAGE_SIZE, 128 * PAGE_SIZE, MPOL_DEFAULT, mask, maxnode, 0); } Link: https://lkml.kernel.org/r/20220329111416.27954-1-linmiaohe@huawei.com Fixes: 42288fe366c4 ("mm: mempolicy: Convert shared_policy mutex to spinlock") Signed-off-by: Miaohe Lin Acked-by: Michal Hocko Cc: KOSAKI Motohiro Cc: Mel Gorman Cc: [3.8] Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/mempolicy.c | 1 + 1 file changed, 1 insertion(+) --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -2479,6 +2479,7 @@ alloc_new: mpol_new = kmem_cache_alloc(policy_cache, GFP_KERNEL); if (!mpol_new) goto err_out; + atomic_set(&mpol_new->refcnt, 1); goto restart; }