Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp5170298pxv; Wed, 28 Jul 2021 05:01:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzJUFtF1i9cet83I4EOYW5iZRJ6oRrkIP3IRgoWLgNz5SanVP16LbXMqSW7qxt+XQRmFWHm X-Received: by 2002:a17:906:9bf0:: with SMTP id de48mr26882037ejc.260.1627473715472; Wed, 28 Jul 2021 05:01:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627473715; cv=none; d=google.com; s=arc-20160816; b=bass5YJga2bIXYkhRkahzh34+E405Es81KD19dMmFZjZOTovktukjlOGliccJVFlLV 4sqxDoKOyCyFds5NdvgJ79RCiYcPy/c2qUUvWnyqonLz4GUfTwns9/8e05PK0axO/yme ZGkaPq7oXdIq2ZCbQgyrf2ShM5E4BN2IH0yIUhlyeErBUMUdcReolj9zsQ2qXFs72sFL AymU5OjDfsMKp58W/Fi9o35rTewWiOlgmarHxrpDpjYDTQkJ5NN/PDt/VdzTCamWXoiz yq8SauoGaZEEkH1BL+WrOqhJyRf8zkLtN9w1IisGgd0Wq/n5QO/jpRg9aAH6iFJdvp3N lz7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=T27nRAChe14nzLtZeLJDcl12BMjSMmzLruDr6hxDyuk=; b=BTi74E6NkOC0+WzIFBLi99WszCXDMhQ/CmoO6ujP4EBEeAFbbEQna4NpgBYBaZ4vtL F/V5KeUoU+YbQY7zYFG0o/Itw7j0dl1MdN7T4izjbc2Fm/6PjknkBRn1m7JVkYghWbJL LyajJV+s8uxnSXGq137XAXRWtt0cahfF+NkHky00RwsIdiY83eI3UqNx+hsqHkDifiw4 1QaYIURGTqqEn2xwq1ZsSSRWV7gO+pVsC5e8Jf3YaSZrr9pufgdTh/I7mX4W4OEqCaD6 thwhQ5/ZJ9Erqv9riiiiMclmHKAt4qa/kZ8AhVduFtm3SicTv+t+HkKaosa3SnQAM9y9 eddg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b7si5594451edr.480.2021.07.28.05.01.25; Wed, 28 Jul 2021 05:01:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236209AbhG1L7j (ORCPT + 99 others); Wed, 28 Jul 2021 07:59:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:54756 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234753AbhG1L7h (ORCPT ); Wed, 28 Jul 2021 07:59:37 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 06E7F60F91; Wed, 28 Jul 2021 11:59:33 +0000 (UTC) Date: Wed, 28 Jul 2021 13:59:30 +0200 From: Christian Brauner To: cgel.zte@gmail.com Cc: peterz@infradead.org, tglx@linutronix.de, linux-kernel@vger.kernel.org, Ran Xiaokai , James Morris , Linus Torvalds , Kees Cook , NeilBrown Subject: Re: [PATCH] set_user: add capability check when rlimit(RLIMIT_NPROC) exceeds Message-ID: <20210728115930.2lzs57h4hvnqipue@wittgenstein> References: <20210728072629.530435-1-ran.xiaokai@zte.com.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210728072629.530435-1-ran.xiaokai@zte.com.cn> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [Ccing a few people that did the PF_NPROC_EXCEEDED changes] On Wed, Jul 28, 2021 at 12:26:29AM -0700, cgel.zte@gmail.com wrote: > From: Ran Xiaokai > > in copy_process(): non root users but with capability CAP_SYS_RESOURCE > or CAP_SYS_ADMIN will clean PF_NPROC_EXCEEDED flag even > rlimit(RLIMIT_NPROC) exceeds. Add the same capability check logic here. > > Signed-off-by: Ran Xiaokai > --- > kernel/sys.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/kernel/sys.c b/kernel/sys.c > index ef1a78f5d71c..72c7639e3c98 100644 > --- a/kernel/sys.c > +++ b/kernel/sys.c > @@ -480,7 +480,8 @@ 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 && > + !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) > current->flags |= PF_NPROC_EXCEEDED; > else > current->flags &= ~PF_NPROC_EXCEEDED; Hey Cgel, Hey Ran, The gist seems to me that this code wants to make sure that a program can't successfully exec if it has gone through a set*id() transition while exceeding its RLIMIT_NPROC. But I agree that the semantics here are a bit strange. Iicu, a capable but non-INIT_USER caller getting PF_NPROC_EXCEEDED set during a set*id() transition wouldn't be able to exec right away if they still exceed their RLIMIT_NPROC at the time of exec. So their exec would fail in fs/exec.c: if ((current->flags & PF_NPROC_EXCEEDED) && is_ucounts_overlimit(current_ucounts(), UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC))) { retval = -EAGAIN; goto out_ret; } However, if the caller were to fork() right after the set*id() transition but before the exec while still exceeding their RLIMIT_NPROC then they would get PF_NPROC_EXCEEDED cleared (while the child would inherit it): 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; } current->flags &= ~PF_NPROC_EXCEEDED; which means a subsequent exec by the capable caller would now succeed even though they could still exceed their RLIMIT_NPROC limit. So at first glance, it seems that set_user() should probably get the same check as it can be circumvented today unless I misunderstand the original motivation. Christian