From: Christian Brauner <brauner@kernel.org>
To: Jann Horn <jannh@google.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
linux-fsdevel@vger.kernel.org,
Arnaldo Carvalho de Melo <acme@kernel.org>,
linux-perf-users@vger.kernel.org, bpf@vger.kernel.org,
selinux@vger.kernel.org, Kees Cook <kees@kernel.org>,
linux-mm@kvack.org, linux-security-module@vger.kernel.org,
apparmor@lists.ubuntu.com, David Hildenbrand <david@kernel.org>,
"Christian Brauner (Amutable)" <brauner@kernel.org>,
stable@vger.kernel.org
Subject: [PATCH RFC 05/19] selinux: close inherited files that can't be replaced
Date: Tue, 24 Nov 2026 23:58:28 +0100 [thread overview]
Message-ID: <20261124-work-exec-update_lock-v1-5-e489035395b8@kernel.org> (raw)
In-Reply-To: <20261124-work-exec-update_lock-v1-0-e489035395b8@kernel.org>
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) <brauner@kernel.org>
---
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
next prev parent reply other threads:[~2026-11-24 22:59 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 22:58 [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 01/19] proc: copy namespace link after dropping exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 02/19] kcmp: look up epoll target without exec_update_lock held Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 03/19] perf: copy counter values after dropping ctx->mutex Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 04/19] perf: copy ioctl arguments without ctx->mutex held Christian Brauner
2026-11-24 22:58 ` Christian Brauner [this message]
2026-11-24 22:58 ` [PATCH RFC 06/19] exec: move unshare_sighand() before exec_mmap() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 07/19] selftests/exec: test exec with a task sleeping in a page fault Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 08/19] selftests/exec: test exec with a shared signal table Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 09/19] lsm: add security_bprm_revalidate_files() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 10/19] bpf: mark bprm_revalidate_files as sleepable Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 11/19] selinux: revalidate inherited files before taking exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 12/19] apparmor: " Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 13/19] exec: drop exec_update_lock after the credentials are committed Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 14/19] exec: switch time namespace after dropping exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 15/19] exec: don't take the mmap lock of the old mm Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 16/19] exit: don't take the mmap lock in exit_mm() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 17/19] proc: look up map_files vma without exec_update_lock held Christian Brauner
2026-11-24 23:09 ` sashiko-bot
2026-11-24 22:58 ` [PATCH RFC 18/19] vfork: wake up the parent once the child has left the mm Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 19/19] selftests/exec: test when a vfork() parent is woken up Christian Brauner
2026-11-24 23:15 ` [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124-work-exec-update_lock-v1-5-e489035395b8@kernel.org \
--to=brauner@kernel.org \
--cc=acme@kernel.org \
--cc=apparmor@lists.ubuntu.com \
--cc=bpf@vger.kernel.org \
--cc=david@kernel.org \
--cc=jannh@google.com \
--cc=kees@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=selinux@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox