Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp3849975pxb; Mon, 21 Feb 2022 07:00:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJzA7sH8cDDmDHEUotILzBGRSPfQoxV58py9a1bVsGGNMSwVjkZSMNuZs6iinbNZgtOTwsEk X-Received: by 2002:a17:90a:160f:b0:1b8:ab45:d287 with SMTP id n15-20020a17090a160f00b001b8ab45d287mr21679228pja.91.1645455603493; Mon, 21 Feb 2022 07:00:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645455603; cv=none; d=google.com; s=arc-20160816; b=jZPBBysN2BMuy9/LhNDxKieeH9vRa9k14QrxeRNnDyN4/yEpAYF1J6FC9DQBxpoE05 RrSZJOQIGs8p4o4cvMG3dxr6ejQgYZ9cjcwxOvqLIL0/AApwE3AZcivsPL0PiILFs0Tc +7GOgsqb8Q7Qf/dUi1Sfg82nEWqfhwHrY2iamV6qJYvbBLFl67vStb0usfsby3zciCHV kHTbogbmyvBSTB+nNjMfzC5uJEs9AIq7n0mJyIJE4X5YBp8S0s2PXtycHCmzrn5fsGp0 nOdPV64fyvmVgyrv6JbjYWePjIHd8wlVRSVjCJQ+WpwNPJLnXpbHdV3N/XJa7XxIQFn5 t/Gw== 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=w3y/DKRl9I+5rZQVMSnB3+Bv5ifgqg2dII5jyLfz6ZM=; b=x16xUG0iYZdMERFHLbQo2l+a6DxCmcDMQgZPT2svYyCZ6u9Eszxg6XyG+Bj1iy4su0 MeWV8CViyhvEPyZ0NaXIRgICWOqoxZuQzHy6vOY3SXsyWhl2GXwdMsn/Mjfz4ICwR8VY RG40NGbYtO7ImHOH4FqRHflEzfi4DEG8Iu0Hhz29lrT+Mm/4Con9mYezX/7GWPLMKKyY Jd8auNDLOBkWkCAsmfFDr/F+lqFB8FntH5H6I0WlAOdy5D69LZB5YHL1/2SrpySOQl0g ecJ4x7kNYUmwSh3Yg6Op/Nj8zNHpQ0KPQfAfOp4l3Ns6DaJ855p7iZvn37i3iZQVKFAi s4BQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rca7WwA9; 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 ds15-20020a17090b08cf00b001bc3694545dsi3076415pjb.135.2022.02.21.06.59.47; Mon, 21 Feb 2022 07:00:03 -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=@linuxfoundation.org header.s=korg header.b=rca7WwA9; 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 S1354233AbiBUKMP (ORCPT + 99 others); Mon, 21 Feb 2022 05:12:15 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:57162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352680AbiBUJ4U (ORCPT ); Mon, 21 Feb 2022 04:56:20 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3059F44742; Mon, 21 Feb 2022 01:24:37 -0800 (PST) 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 ams.source.kernel.org (Postfix) with ESMTPS id D9308B80EC8; Mon, 21 Feb 2022 09:24:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 20A86C340E9; Mon, 21 Feb 2022 09:24:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1645435474; bh=bsLHEmSsMJU6GzjBvsuYhbuE15Aw8KAYXvtwsnB0FoQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rca7WwA984DdNensbzMut2rzt0o9nUdLk1ewX2UpZW17jVnBJsTLOZnaRzUlZBsgj P4ywG+CVb0ih5NEQFwSEnQMCH6j6QR9bT0hHDuofCAst7dwkDZLO0yMQD/OtqgRcbu fLdQxl0CYuiEXxH3B22CHA1HEjUHI/17dnXafWV4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?Michal=20Koutn=C3=BD?= , "Eric W. Biederman" Subject: [PATCH 5.16 175/227] ucounts: Enforce RLIMIT_NPROC not RLIMIT_NPROC+1 Date: Mon, 21 Feb 2022 09:49:54 +0100 Message-Id: <20220221084940.630081292@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220221084934.836145070@linuxfoundation.org> References: <20220221084934.836145070@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.1 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: Eric W. Biederman commit 8f2f9c4d82f24f172ae439e5035fc1e0e4c229dd upstream. Michal Koutný wrote: > It was reported that v5.14 behaves differently when enforcing > RLIMIT_NPROC limit, namely, it allows one more task than previously. > This is consequence of the commit 21d1c5e386bc ("Reimplement > RLIMIT_NPROC on top of ucounts") that missed the sharpness of > equality in the forking path. This can be fixed either by fixing the test or by moving the increment to be before the test. Fix it my moving copy_creds which contains the increment before is_ucounts_overlimit. In the case of CLONE_NEWUSER the ucounts in the task_cred changes. The function is_ucounts_overlimit needs to use the final version of the ucounts for the new process. Which means moving the is_ucounts_overlimit test after copy_creds is necessary. Both the test in fork and the test in set_user were semantically changed when the code moved to ucounts. The change of the test in fork was bad because it was before the increment. The test in set_user was wrong and the change to ucounts fixed it. So this fix only restores the old behavior in one lcation not two. Link: https://lkml.kernel.org/r/20220204181144.24462-1-mkoutny@suse.com Link: https://lkml.kernel.org/r/20220216155832.680775-2-ebiederm@xmission.com Cc: stable@vger.kernel.org Reported-by: Michal Koutný Reviewed-by: Michal Koutný Fixes: 21d1c5e386bc ("Reimplement RLIMIT_NPROC on top of ucounts") Signed-off-by: "Eric W. Biederman" Signed-off-by: Greg Kroah-Hartman --- kernel/fork.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2052,18 +2052,18 @@ static __latent_entropy struct task_stru #ifdef CONFIG_PROVE_LOCKING DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); #endif + retval = copy_creds(p, clone_flags); + if (retval < 0) + goto bad_fork_free; + retval = -EAGAIN; if (is_ucounts_overlimit(task_ucounts(p), UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC))) { if (p->real_cred->user != INIT_USER && !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) - goto bad_fork_free; + goto bad_fork_cleanup_count; } current->flags &= ~PF_NPROC_EXCEEDED; - retval = copy_creds(p, clone_flags); - if (retval < 0) - goto bad_fork_free; - /* * If multiple threads are within copy_process(), then this check * triggers too late. This doesn't hurt, the check is only there