Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp2461956pxy; Tue, 3 Aug 2021 07:08:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwL7TZsgXwnN77/Irg1QBM7XSdTGS95ZxJrKwnLX9nNZ53KC1Fba9t5M87GqVJCZyaCN5Rr X-Received: by 2002:a05:6a00:a8a:b029:30c:a10b:3e3f with SMTP id b10-20020a056a000a8ab029030ca10b3e3fmr22410870pfl.40.1627999685464; Tue, 03 Aug 2021 07:08:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627999685; cv=none; d=google.com; s=arc-20160816; b=EBKy2on+B4rGRnpdQfyhE3SjHlXU5ROFZtV4qgh3q3/CHPKNc1tEBo1G6dKLw2OuCD iXNEC4t+Sc3JdAWbtHhcwmGNeT/4WuF9hSXp9tlixUa2Z4mAE3R4/1SpT0iSdZYADkav DH7CN0D9oWK2m0vGA++jhAvFsNmS+lC9osOZKXkXaL6DWi23VJBakc8usgjA5iVS/Rsh RNquPEbKq73wf7QOigDoejfxlTc/ZafclP/EpjGW0uThfodUBJdvYy+7PyBPbQq8HKrX YMc3bds6TC66joRYCnrm9sFOW34QywTh5RFssTBgo1J7kWm3sayuf7aIad3GDathJYDR a8dQ== 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=EfKoGN0ZcWZeQuS0xy6xpHJY7sh527isBCR2HNDZ5eY=; b=EmjvkqpEAPdBbjvO07k6z8eMB0yWarBSnNkcqcTdaLlE3iinMkCBhkdtwoOztadL/V dvrotu7fLmf/NuSHCGFxmVewmWBmoObdDn8QTq+RCw1tZdc1zSfbvdycG9FPv4jBCiRq Ac9/5zP6wl4mja4E+5t5ojlBiOpAE24p/PujKwOaaWhXoYLv07HnBOggr+3gYVpp7cvk sBle+hljuKNtH1YJlYbVinNrIuSPnRZ9crKXJIZEJs80svBlOd+S/G2C8MQMT6O9ngWY XGFOHxJ7KqAsqkrqrpVJ2tKIFE6vZLKigYDZ24aDQM5fU/yPCkP1cWpqUCv2pHa8DUEB J6YQ== 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 u6si12090496ilq.19.2021.08.03.07.07.51; Tue, 03 Aug 2021 07:08:05 -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 S236498AbhHCOHT (ORCPT + 99 others); Tue, 3 Aug 2021 10:07:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:46000 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236045AbhHCOHS (ORCPT ); Tue, 3 Aug 2021 10:07:18 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2466960F94; Tue, 3 Aug 2021 14:07:04 +0000 (UTC) Date: Tue, 3 Aug 2021 16:07:02 +0200 From: Christian Brauner To: CGEL 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: <20210803140702.f3rdnka3e2x6vj4r@wittgenstein> References: <20210728072629.530435-1-ran.xiaokai@zte.com.cn> <20210728115930.2lzs57h4hvnqipue@wittgenstein> <20210730082329.GA544980@www> <20210803100354.GA607722@www> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210803100354.GA607722@www> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 03, 2021 at 03:03:54AM -0700, CGEL wrote: > On Fri, Jul 30, 2021 at 01:23:31AM -0700, CGEL wrote: > > On Wed, Jul 28, 2021 at 01:59:30PM +0200, Christian Brauner wrote: > > > [Ccing a few people that did the PF_NPROC_EXCEEDED changes] > > > > > > > > > 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 > > > > Hi Christian, > > > > I think i didn't give enough information in the commit message. > > When switch to a capable but non-INIT_SUER and the RLIMIT_NPROC limit already exceeded, > > and calls these funcs: > > 1. set_xxuid()->exec() > > ---> fail > > 2. set_xxuid()->fork()->exec() > > ---> success > > Kernel should have the same behavior to uer space. > > Also i think non init_user CAN exceed the limit when with proper capability, > > so in the patch, set_user() clear PF_NPROC_EXCEEDED flag if capable() > > returns true. > > Hi, Christian > > Do you have any further comments on this patch? > is there anything i did not give enough infomation ? Yeah, this is fine and how I understood it too. I don't see anything obviously wrong with it and the weird detour workaround via fork() seems inconsistent. So if I don't here anyone come up with a good reason the current behavior makes sense I'll pick this up. Christian