From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28B8F2309B2 for ; Sun, 14 Sep 2025 11:11:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757848273; cv=none; b=oqu5LY08L7RtYHKOttPbR+z+HrVGrFY80CF9zkWwNhA2bG2c1lA422Mzl/plUwKEIngLoThdQj5DB4Ua4ndfKserftpP4pZEZ71/2/YH8908dAhMbxtPcgP4aYEmRCkOtPPFW8cO7gNlPJ9S1QBdpE2xwrNCNuOI9LdwMxRvewY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757848273; c=relaxed/simple; bh=q81X5Dxa4VVZv5ELLlS92QyAoM6ANYshOT+UNGy+/Yc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=UgsU/8m+/A7eRgB5VgLHstP0cOAQQ0hdlmMB+WRvJySmr/sGQs0hOSWA5E2Wao/+YhsTMjoewhJs6eEfY697LySfzu0vuDdgnVn4HH+XW+b6rEYgKQjJ20wwlTB40xChLKPjBxkqF8780tC2kCi1GPlpD06cE2tkAnFRGYIJH6I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=FpiJ6H1f; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="FpiJ6H1f" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1757848271; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=sCo/Ei8LxXPxIbDKjNeJPLj1Gqs5dzyj1pmCpVuUDxM=; b=FpiJ6H1fEZ/fAJfIofocSUpDfHBoOMr4W4oKOFUuaaOFvZKv6QpWSY8vI7giqcPa3GRpys Qtozt8ISNB416VtPE1ddzmyR0AcDPhxfaV0VATt7OJkWPv6Y4hLQ3Pb80DSi8AEJV3ecN5 cydL4cJbKZFitduL1TRos6UpWr9j8f8= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-594-mMNB9MVtMkW4KZSY8Gg2xg-1; Sun, 14 Sep 2025 07:11:06 -0400 X-MC-Unique: mMNB9MVtMkW4KZSY8Gg2xg-1 X-Mimecast-MFC-AGG-ID: mMNB9MVtMkW4KZSY8Gg2xg_1757848265 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0616B1956086; Sun, 14 Sep 2025 11:11:05 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.224.10]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id CE9221954126; Sun, 14 Sep 2025 11:11:02 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Sun, 14 Sep 2025 13:09:40 +0200 (CEST) Date: Sun, 14 Sep 2025 13:09:37 +0200 From: Oleg Nesterov To: Andrew Morton Cc: Christian Brauner , Jiri Slaby , Mateusz Guzik , linux-kernel@vger.kernel.org Subject: [PATCH 2/2] fix the racy usage of task_lock(tsk->group_leader) in sys_prlimit64() paths Message-ID: <20250914110937.GA18778@redhat.com> References: <20250914110908.GA18769@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250914110908.GA18769@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 The usage of task_lock(tsk->group_leader) in sys_prlimit64()->do_prlimit() path is very broken. sys_prlimit64() does get_task_struct(tsk) but this only protects task_struct itself. If tsk != current and tsk is not a leader, this process can exit/exec and task_lock(tsk->group_leader) may use the already freed task_struct. Another problem is that sys_prlimit64() can race with mt-exec which changes ->group_leader. In this case do_prlimit() may take the wrong lock, or (worse) ->group_leader may change between task_lock() and task_unlock(). Change sys_prlimit64() to take tasklist_lock when necessary. This is not nice, but I don't see a better fix for -stable. Cc: stable@vger.kernel.org Fixes: c022a0acad53 ("rlimits: implement prlimit64 syscall") Signed-off-by: Oleg Nesterov --- kernel/sys.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/kernel/sys.c b/kernel/sys.c index 1e28b40053ce..36d66ff41611 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -1734,6 +1734,7 @@ SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource, struct rlimit old, new; struct task_struct *tsk; unsigned int checkflags = 0; + bool need_tasklist; int ret; if (old_rlim) @@ -1760,8 +1761,25 @@ SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource, get_task_struct(tsk); rcu_read_unlock(); - ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL, - old_rlim ? &old : NULL); + need_tasklist = !same_thread_group(tsk, current); + if (need_tasklist) { + /* + * Ensure we can't race with group exit or de_thread(), + * so tsk->group_leader can't be freed or changed until + * read_unlock(tasklist_lock) below. + */ + read_lock(&tasklist_lock); + if (!pid_alive(tsk)) + ret = -ESRCH; + } + + if (!ret) { + ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL, + old_rlim ? &old : NULL); + } + + if (need_tasklist) + read_unlock(&tasklist_lock); if (!ret && old_rlim) { rlim_to_rlim64(&old, &old64); -- 2.25.1.362.g51ebf55