Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4440981pxb; Mon, 21 Feb 2022 21:42:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJzWh8/2NHSAVeEF1Lo8kqPVwP9y/z3vXLl2N04kQXFfn14b0tQxD2eDeCuE91AwUEo33pMY X-Received: by 2002:a17:902:c443:b0:14d:a756:b1f2 with SMTP id m3-20020a170902c44300b0014da756b1f2mr21427710plm.94.1645508566989; Mon, 21 Feb 2022 21:42:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645508566; cv=none; d=google.com; s=arc-20160816; b=0VTZCC6gSs6paT+7o+DkOpV/e2pZR15FwfRqQP/lbl8Zw6nxk7UChD1Lx/XTgKQEvf wMZPD8FPDPN1Jvqc38C3dvcPTbT/pHel97J36hGpSPFqhtGTAHi3094H+HHpqIFaUxp/ 0eD1Mq17m/tPiFeYiVogfK7nYLi6w/9T2moEntEf4Fzbx6wZ3o/vIChFC28qwAnBDRse V6W9VCnyDo3J5Ge3uPQ0Hfi9qgnqzQVoatJWKPK+EYU+yNQqVV3shlpc5sYy6KUh2iRi 1PX4oC/VMcj5ewSex6+a5TLUj1eAJK2FsdFLXqqqf9HOYO4QsN2Y1JRvq025dHpg44+e mCXg== 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=owAuPlLPxUpdxXFn72T0y1A1djpcH5e6GJWmEZLTXOE=; b=BeQZSgFmrAkIe7Qc8q6M4C/9pkPgIyi798KtWE4rd+8cDJSG7tiM44xO9jdrbKZMfo svMpj821baY2uspnPV0JbuZMKOjOZNwaDmiy7GdubcLs/MshXKe+MScPNvMz9yQg1XhZ 6C0eO8L0zmC6hVdrmu+2CFGNHjosvS5VTCB414fPdYHbipD10QhF3X4jl7XKiuEFtdJs f+EmomHSL2vx1Q2HiNE0MviGZMpStLyujbZedTgofQNKvTqlZu7biMn2/5Yg8EvprWrA 96Uz9X25/u5zTohW571EQC62SKjiOWzPV4+SHnetYB/Ha9cU9JdKzwgCnZ3QPBDO/iuK QpRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Zn03hV+9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id ha2-20020a17090af3c200b001bc7193ab4csi809201pjb.38.2022.02.21.21.42.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Feb 2022 21:42:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Zn03hV+9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D601436B7A; Mon, 21 Feb 2022 21:08:36 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351228AbiBUJn0 (ORCPT + 99 others); Mon, 21 Feb 2022 04:43:26 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:48728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351347AbiBUJhA (ORCPT ); Mon, 21 Feb 2022 04:37:00 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D96EC2E088; Mon, 21 Feb 2022 01:15:41 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 88D3E60ED4; Mon, 21 Feb 2022 09:15:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D7ADC340E9; Mon, 21 Feb 2022 09:15:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1645434939; bh=7/FBbwrvHWUltZ4Jxxrkk2lhlNEV2NohBiXF+5Bj8II=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zn03hV+92fPA/KDU27CMmNBNyzd1ncBfrkNfCnLRHACZ5gWsosX+hOSX1Ds+hzBb0 7fN4hK4dcOgo45fw4IJ/rwksyu+8Q0Fo6UGoBTivbi+g8BzQkrYIm+/t7dP+Pc/+v8 DA1H6mWGFDVn4huBc8b/5ooLExPsqGDiugsKBBy8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Eric W. Biederman" Subject: [PATCH 5.15 183/196] ucounts: Move RLIMIT_NPROC handling after set_user Date: Mon, 21 Feb 2022 09:50:15 +0100 Message-Id: <20220221084937.065172247@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220221084930.872957717@linuxfoundation.org> References: <20220221084930.872957717@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=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 c923a8e7edb010da67424077cbf1a6f1396ebd2e upstream. During set*id() which cred->ucounts to charge the the current process to is not known until after set_cred_ucounts. So move the RLIMIT_NPROC checking into a new helper flag_nproc_exceeded and call flag_nproc_exceeded after set_cred_ucounts. This is very much an arbitrary subset of the places where we currently change the RLIMIT_NPROC accounting, designed to preserve the existing logic. Fixing the existing logic will be the subject of another series of changes. Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20220216155832.680775-4-ebiederm@xmission.com Fixes: 21d1c5e386bc ("Reimplement RLIMIT_NPROC on top of ucounts") Signed-off-by: "Eric W. Biederman" Signed-off-by: Greg Kroah-Hartman --- kernel/sys.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) --- a/kernel/sys.c +++ b/kernel/sys.c @@ -472,6 +472,16 @@ static int set_user(struct cred *new) if (!new_user) return -EAGAIN; + free_uid(new->user); + new->user = new_user; + return 0; +} + +static void flag_nproc_exceeded(struct cred *new) +{ + if (new->ucounts == current_ucounts()) + return; + /* * We don't fail in case of NPROC limit excess here because too many * poorly written programs don't check set*uid() return code, assuming @@ -480,14 +490,10 @@ static int set_user(struct cred *new) * failure to the execve() stage. */ if (is_ucounts_overlimit(new->ucounts, UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC)) && - new_user != INIT_USER) + new->user != INIT_USER) current->flags |= PF_NPROC_EXCEEDED; else current->flags &= ~PF_NPROC_EXCEEDED; - - free_uid(new->user); - new->user = new_user; - return 0; } /* @@ -562,6 +568,7 @@ long __sys_setreuid(uid_t ruid, uid_t eu if (retval < 0) goto error; + flag_nproc_exceeded(new); return commit_creds(new); error: @@ -624,6 +631,7 @@ long __sys_setuid(uid_t uid) if (retval < 0) goto error; + flag_nproc_exceeded(new); return commit_creds(new); error: @@ -703,6 +711,7 @@ long __sys_setresuid(uid_t ruid, uid_t e if (retval < 0) goto error; + flag_nproc_exceeded(new); return commit_creds(new); error: