From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E638C606A32F for ; Thu, 3 Dec 2026 20:18:39 +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=ZOoXw6ym Received: by kanga.kvack.org (Postfix) id EEE216B05BE; Thu, 3 Dec 2026 15:18:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EC4B16B05BF; Thu, 3 Dec 2026 15:18:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DDA946B05C0; Thu, 3 Dec 2026 15:18:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 5CB796B05BE for ; Thu, 3 Dec 2026 15:18:38 -0500 (EST) Received: from smtpin05.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4FF15A6F45 for ; Thu, 3 Dec 2026 20:18:37 +0000 (UTC) X-FDA: 85503784674.05.15DE1CF Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf28.hostedemail.com (Postfix) with ESMTP id A7A78C000B for ; Thu, 3 Dec 2026 20:18:35 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=ZOoXw6ym; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf28.hostedemail.com: domain of sergeh@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sergeh@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796329115; b=MM0ObBGmBHi7dW3b1TMiZ16o/ILMCuxD7oNSOwFoxCW2Uc5rIzIKgCa1nhCxNjsyTBhWww 4GjDkBwd2dVTfAVkNZZ5mTKU/1eOFocuxgIhq4B6fvBqeKUhBuX4LN04nH/hU+2/dvNmjC mNV3P4Cl8qkHnmtiezvG/bvgqRNThho= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796329115; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QsupYiSy3gX6i2H2Lo6rXm+HrR8bE3Y1k8nkRiAzz34=; b=Df3XWHliDMqNbbr1HWUi3b/MQE8fO1MN0KMB7xa+t9qhm2J0VT+yXWsdtzUka83wqxweMj /vZJJ9LtQx7NhWe9uYlrVpi+T/X/7nLneSeoomPnRFfitD0r25O9ahdadicU/boO3zaH1l 5D6yhd+CkRoSqTTT7cUDHeuBbncFmmU= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=ZOoXw6ym; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf28.hostedemail.com: domain of sergeh@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sergeh@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id D686E60181; Thu, 3 Dec 2026 20:18:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D2931F000C2; Thu, 3 Dec 2026 20:18:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796329114; bh=QsupYiSy3gX6i2H2Lo6rXm+HrR8bE3Y1k8nkRiAzz34=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=ZOoXw6ymcKyV4WgigmUhqvXYl4cp86WDapeuy46FPUysEDTN1b1AJShY/oXSGF5bA SBVeRN3emBO1CIRJ8IQtGbmh2ZKrVpVtZtfmToAOob0AGtCRdj+zSUHguTrr5FM6mo uW1hr9EcX0NpudOixFBu+0taIZcaSOoY3pZcYv2aQlf1PE5zB8AowyHZOl3HSSm1is VSxHuIX1/wa0Ce2hVibidAVkufxKxLHk8MCK9ce9D/HcXv/qWfXZ+o4a6VogyuUvd4 Rp/0KKxY1V26riTTMckjxMhpqVfFLDlbT/wmJCs26rIX2kRsZOC44uL05Rv7p4Vo/s hF61w+wbRk07g== Date: Thu, 3 Dec 2026 14:18:26 -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 02/19] kcmp: look up epoll target without exec_update_lock held Message-ID: References: <20261124-work-exec-update_lock-v1-0-e489035395b8@kernel.org> <20261124-work-exec-update_lock-v1-2-e489035395b8@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261124-work-exec-update_lock-v1-2-e489035395b8@kernel.org> X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: A7A78C000B X-Rspam-User: X-Stat-Signature: sd3tgngb67qkduoggkpryosbf3n43uz9 X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796329115-955556 X-HE-Meta: U2FsdGVkX19PolMG6SiTyzsWqXdkuT4LnvsP2M2vBNBmIarNCvYWO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8WBDKypz96e3IlHGExVr6yno+Y/pZDz/QNqZuCkY+EWQs29XcUc/aLYibkBdZasj2rN34rqjeN2HYsNK7fKq+Ry75ChXS7KPVBq9XqRVQ2Ofk6528uAHFHdV5HwiTZ5QybxxX4JrPbvUKtfeXSZjSiHE5U5STWwwkRLa6D2Z0SkqLv45l6PnvYj7Je5jlA1mt4DsghAwCswxF81ECDFsO5RQ3q3nfl8w1em4GEZEHZpfrfWTc2ZfTFa0wLcpEF94shDHpJcSs5abLTY1nwuPhkUkk/WnnCWr4726kp3sKVXDNMvb0PW451cfLNEIlZTOaFqjf5EiGCyGYUHDtRvK7Gu3jqo9Ko7I+CCmF2L93GQ2DOl2yzhFiEBpDNeaqj1clRomtu2p+a8jeAUxpkEDP6s5vX8uP/IXRfJlwFFT/D6H+2uNy9lNN0h1s+qalt2dfYBZcWd4E2zGY= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Tue, Nov 24, 2026 at 11:58:25PM +0100, Christian Brauner wrote: > kcmp(KCMP_EPOLL_TFD) holds exec_update_lock and two things both of which > are susceptible to page fault induced, userspace controlled delays: > > (1) cpy struct kcmp_epoll_slot from userspace > > locks held by kcmp-reader/233: > #0: (&sig->exec_update_lock){++++}-{4:4}, at: __do_sys_kcmp+0x2ec/0xb10 > #1: (&sig->exec_update_lock/1){.+.+}-{4:4}, at: __do_sys_kcmp+0x210/0xb10 > > (2) takes the mutex of the epoll file to look up the target file. > > locks held by epoll-waiter/236: > #0: (&ep->mtx){+.+.}-{4:4}, at: ep_send_events+0x10a/0x9f0 > locks held by kcmp-caller/237: > #0: (&sig->exec_update_lock){++++}-{4:4}, at: __do_sys_kcmp+0x2ec/0xb10 > #1: (&sig->exec_update_lock/1){.+.+}-{4:4}, at: __do_sys_kcmp+0x210/0xb10 > #2: (&ep->mtx){+.+.}-{4:4}, at: get_epoll_tfile_raw_ptr+0xa5/0x190 > > In the meantime an exec of either task waits for exec_update_lock past > the point of no return. And everyone else who needs the exec_update_lock > of that task queues up behind the exec. So a read of /proc//stat > hangs as well: > > task:exec-task state:D pid:235 > Call Trace: > __schedule+0x1691/0x4b00 > schedule+0xe0/0x2e0 > schedule_preempt_disabled+0x11/0x20 > rwsem_down_write_slowpath+0x472/0x1820 > down_write_killable+0x1ba/0x240 > begin_new_exec+0xa4b/0x2df0 > load_elf_binary+0x340/0x4af0 > bprm_execve+0x772/0x1400 > do_execveat_common.isra.0+0x3d9/0x540 > __x64_sys_execve+0x8c/0xc0 > do_syscall_64+0x110/0x600 > entry_SYSCALL_64_after_hwframe+0x77/0x7f > > task:stat-reader state:D pid:238 > Call Trace: > __schedule+0x1691/0x4b00 > schedule+0xe0/0x2e0 > schedule_preempt_disabled+0x11/0x20 > rwsem_down_read_slowpath+0x5cc/0x10c0 > down_read_killable+0xf1/0x5f0 > do_task_stat+0x13b/0x2870 > proc_single_show+0x110/0x1d0 > seq_read_iter+0x2e4/0x1280 > seq_read+0x29b/0x550 > vfs_read+0x171/0xb20 > ksys_read+0x111/0x250 > do_syscall_64+0x110/0x600 > entry_SYSCALL_64_after_hwframe+0x77/0x7f > > All it takes is being allowed to kcmp() the two tasks. > > Stop that and copy the before the locks are taken. With the locks held "Stop that and copy the (what)" > only take a reference to the epoll file and look up the target after > they have been dropped. The epoll file is still taken from the task > after the access check and with exec_update_lock held. > > Note that the target is now looked up a moment later. So if the task > execs in between and keeps the epoll file the caller may get to compare > a target that only the new image registered. The caller still only gets > at the epoll file while it's allowed to look at the task and all it > learns is how two files compare. > > Fixes: 0791e3644e5e ("kcmp: add KCMP_EPOLL_TFD mode to compare epoll target files") > Cc: stable@vger.kernel.org > Signed-off-by: Christian Brauner (Amutable) Reviewed-by: Serge Hallyn (AMD) > --- > kernel/kcmp.c | 72 +++++++++++++++++++++++++++++++++++++++++------------------ > 1 file changed, 50 insertions(+), 22 deletions(-) > > diff --git a/kernel/kcmp.c b/kernel/kcmp.c > index 76476aeee067b..8102e5825efc5 100644 > --- a/kernel/kcmp.c > +++ b/kernel/kcmp.c > @@ -94,6 +94,34 @@ static int kcmp_lock(struct rw_semaphore *l1, struct rw_semaphore *l2) > return err; > } > > +static void kcmp_unlock_tasks(struct task_struct *task1, > + struct task_struct *task2) > +{ > + kcmp_unlock(&task1->signal->exec_update_lock, > + &task2->signal->exec_update_lock); > +} > + > +static int kcmp_lock_tasks(struct task_struct *task1, > + struct task_struct *task2) > +{ > + int ret; > + > + ret = kcmp_lock(&task1->signal->exec_update_lock, > + &task2->signal->exec_update_lock); > + if (ret) > + return ret; > + > + /* > + * One should have enough rights to inspect task details. > + */ > + if (ptrace_may_access(task1, PTRACE_MODE_READ_REALCREDS) && > + ptrace_may_access(task2, PTRACE_MODE_READ_REALCREDS)) > + return 0; > + > + kcmp_unlock_tasks(task1, task2); > + return -EPERM; > +} > + > #ifdef CONFIG_EPOLL > static int kcmp_epoll_target(struct task_struct *task1, > struct task_struct *task2, > @@ -102,18 +130,26 @@ static int kcmp_epoll_target(struct task_struct *task1, > { > struct file *filp, *filp_epoll, *filp_tgt; > struct kcmp_epoll_slot slot; > + int ret; > > + /* This may fault so do it before exec_update_lock is taken. */ > if (copy_from_user(&slot, uslot, sizeof(slot))) > return -EFAULT; > > - filp = get_file_raw_ptr(task1, idx1); > - if (!filp) > - return -EBADF; > + ret = kcmp_lock_tasks(task1, task2); > + if (ret) > + return ret; > > - filp_epoll = fget_task(task2, slot.efd); > + filp = get_file_raw_ptr(task1, idx1); > + filp_epoll = filp ? fget_task(task2, slot.efd) : NULL; > + kcmp_unlock_tasks(task1, task2); > if (!filp_epoll) > return -EBADF; > > + /* > + * This takes the epoll mutex which is held across page faults. So > + * do it after exec_update_lock has been dropped. > + */ > filp_tgt = get_epoll_tfile_raw_ptr(filp_epoll, slot.tfd, slot.toff); > fput(filp_epoll); > > @@ -153,19 +189,16 @@ SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type, > > rcu_read_unlock(); > > - /* > - * One should have enough rights to inspect task details. > - */ > - ret = kcmp_lock(&task1->signal->exec_update_lock, > - &task2->signal->exec_update_lock); > - if (ret) > - goto err; > - if (!ptrace_may_access(task1, PTRACE_MODE_READ_REALCREDS) || > - !ptrace_may_access(task2, PTRACE_MODE_READ_REALCREDS)) { > - ret = -EPERM; > - goto err_unlock; > + /* Takes and drops exec_update_lock itself. */ > + if (type == KCMP_EPOLL_TFD) { > + ret = kcmp_epoll_target(task1, task2, idx1, (void __user *)idx2); > + goto out; > } > > + ret = kcmp_lock_tasks(task1, task2); > + if (ret) > + goto out; > + > switch (type) { > case KCMP_FILE: { > struct file *filp1, *filp2; > @@ -203,18 +236,13 @@ SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type, > ret = -EOPNOTSUPP; > #endif > break; > - case KCMP_EPOLL_TFD: > - ret = kcmp_epoll_target(task1, task2, idx1, (void *)idx2); > - break; > default: > ret = -EINVAL; > break; > } > > -err_unlock: > - kcmp_unlock(&task1->signal->exec_update_lock, > - &task2->signal->exec_update_lock); > -err: > + kcmp_unlock_tasks(task1, task2); > +out: > put_task_struct(task1); > put_task_struct(task2); > > > -- > 2.53.0 >