From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796329152; b=vb+4qRNM0DRobAMwti4jSzFLQmcZOH2vUqu7kRu0yUxPjNBsKdWxYxmcSQ00Q0lZ1IFb uEh7q+2rT0q7TiW+dEQ8YYRo8l8EnpLYyM3YBj5GkFERVU5TkxM6jeFAb5cghj1YEoWMx fFONMjckqUId4yU1whjDpJCLayaPxztQX5hsp7ZiB+8b2jtwJ8H348IxR9helLysLL57E 3aZcTFfAmwWZd2e8907TFUIIBwdQgJBwRB6H6MHRcZDv41AVpAVJWslPUxTzffLCn4yro Hbo/ju8iID/zznxqHwcjm97FQTSUNVN/QmYRPTufpOlQKELZLN9xhxhXcK1esioeEig== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796329152; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=pcMmjHhvXqBmmeV9che86vieUv1h+RDU2bp/KSd3Uzs=; b=1atJDE9TR9QYRz5UdXS6OKimLNh9bD6pmNL+SZZQsUkXxMmL4GLNAcsVUkn78MhCjheq NAVg6NOBu9N7GETw9lCVEAme6KAFu8vxQMnOmqdJbj9jw+52ewbOReWHdlx9qHWxjGkFx 4AsT0/ac35t/H9iqCo4SR98uHOLOs04VIhek78+bXJgPNeM2wcGDEaAmNTPVBksUNVrWe zH1qC5NfRDiWtkwGhAR9SW9wNCz1HSdseResSSi3mfImOBQ9+uh6ccAEqmbO0z9WCZu8u LhQy3hCpsh01ufKDhdFa7uMmU7RTDYCDhvrafUUBOaRNzS7IpaUxWG3bGVE22Z7uuTg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=dEviRqKM; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2E70606A32F; Thu, 3 Dec 2026 20:19:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=dEviRqKM Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95EF01F0092B; Thu, 3 Dec 2026 20:19:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796329152; bh=pcMmjHhvXqBmmeV9che86vieUv1h+RDU2bp/KSd3Uzs=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=dEviRqKM/7flSGjOEXNM3+V1UpVAg6ljeWC0hPkAsQIVzwuN1chqHnX1VY7+tr7eb FdMpxRDqpcn2685ZhGvl8c7/yBKefDvuNgoAp+W0+KvuucKWOqgjRmE2rI0LPvGeCK y7X+3R4RgoFruNDL9kwLEFyyx9N9sRNzj7ci+pVY2mRgD+M2x6K+yIFpIMkFoVcNla Vc55T1527Hj5EbB8kgi40NyXzWyQ+dfRfCr55BCYXhTcuEXxDiKmYH59GB5Y/0gyjg BcBwAkJfIEvveiWHRyt8JSMt35Ki4Hw8h0fY1bVyzPz38mWF6T5kOyVOIxQKi1ngdt MeI5FGvqYvoRw== Date: Thu, 3 Dec 2026 14:19:08 -0600 From: "Serge Hallyn (AMD)" To: Christian Brauner Cc: Jann Horn , Linus Torvalds , linux-fsdevel@vger.kernel.org, Arnaldo Carvalho de Melo , linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, selinux@vger.kernel.org, Kees Cook , linux-mm@kvack.org, linux-security-module@vger.kernel.org, apparmor@lists.ubuntu.com, David Hildenbrand , stable@vger.kernel.org Subject: Re: [PATCH RFC 05/19] selinux: close inherited files that can't be replaced Message-ID: References: <20261124-work-exec-update_lock-v1-0-e489035395b8@kernel.org> <20261124-work-exec-update_lock-v1-5-e489035395b8@kernel.org> X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261124-work-exec-update_lock-v1-5-e489035395b8@kernel.org> On Tue, Nov 24, 2026 at 11:58:28PM +0100, Christian Brauner wrote: > When an exec transitions to a new SID flush_unauthorized_files() > replaces the inherited files the new SID isn't allowed to use with the > null device. It uses replace_fd() for that and ignores the return value. > > replace_fd() refuses a file descriptor at or above RLIMIT_NOFILE. And a > task can lower its own soft limit as it likes. So a task that sets > RLIMIT_NOFILE to the number of a file descriptor right before the exec > keeps that file open in the new image: > > fd = open(path, O_RDONLY); /* 3 */ > getrlimit(RLIMIT_NOFILE, &rl); > rl.rlim_cur = fd; > setrlimit(RLIMIT_NOFILE, &rl); > execve(...); > > The new SID still can't read from the file as that is checked again on > every use. But it holds on to the file and what isn't checked on use > like lseek() works. > > Before commit ee97cd872d08 ("switch flush_unauthorized_files() to > replace_fd()") the file was closed first and stayed closed if its file > descriptor couldn't be allocated again. So close the file if it can't be > replaced. > > Fixes: ee97cd872d08 ("switch flush_unauthorized_files() to replace_fd()") > Cc: stable@vger.kernel.org > Signed-off-by: Christian Brauner (Amutable) Reviewed-by: Serge Hallyn (AMD) > --- > security/selinux/hooks.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c > index 9950f89dfaa23..4ec9ec723cacc 100644 > --- a/security/selinux/hooks.c > +++ b/security/selinux/hooks.c > @@ -2521,7 +2521,9 @@ static inline void flush_unauthorized_files(const struct cred *cred, > devnull = NULL; > /* replace all the matching ones with this */ > do { > - replace_fd(n - 1, devnull, 0); > + /* replace_fd() fails for an fd at or above RLIMIT_NOFILE. */ > + if (replace_fd(n - 1, devnull, 0) < 0) > + close_fd(n - 1); > } while ((n = iterate_fd(files, n, match_file, cred)) != 0); > if (devnull) > fput(devnull); > > -- > 2.53.0 >