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.129.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 06A3032E13A for ; Tue, 6 Jan 2026 12:51:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767703874; cv=none; b=oopRvRdK4f4aeJUesPUbAWXT47kPmN6vHZ6kceXbMiBw5+Vbndi8FiFwbwdfRqf0n4hQZgzqt4VzC60sT8Rt6bcMWOp2fqEh5B/jVt/pkKRO7guT3MGP6fiwvKwz3FtFbN5K8KZK4UaTWtkPK6AkCzkts77bOFDGsnnH6LZhQ9g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767703874; c=relaxed/simple; bh=nw3vXOAbiO7XShmWP2vnp2awUpyAAE7UfBp5Hm7NGpE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=l33Pps8Y3RXyfYfMygp32e7gbq7b9/ERPOBJSEKiWDM0rImKq8Y8HsKdApcI5dmnlSZFjrvhXhIlC3RDlu0JHp01p5OZ++Dqbw/M9WXZFOKC11j0dUZ/bZJ68uhNDwrhWdumelwj2oQecZbi9lXVmEYWl8z7Vlp0R/Ae8siup3U= 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=FrFDbcjv; arc=none smtp.client-ip=170.10.129.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="FrFDbcjv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767703872; 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=8qRcBXVrciXM23lDEyD0+rb3PaWXE7vB6ILb3/zh8cY=; b=FrFDbcjvSGEeCQfRFaL2Y1lE17b8m+SdR/jtNmCqzHhndywXSzqr3pCRKRCJ4rVTgr3dMY NN0+v5PQcVEl+XjdRhKEWZCu24842S4dsSOanTZE3Ac3wWdbHpVOPRYc+LgBEpIuo2aPj8 yCjk2Kqsr7yGTSz3gGcjqjwV8jjM3jY= Received: from mx-prod-mc-03.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-625-mHaNRhMzPWSTIIbN7-1jEA-1; Tue, 06 Jan 2026 07:51:07 -0500 X-MC-Unique: mHaNRhMzPWSTIIbN7-1jEA-1 X-Mimecast-MFC-AGG-ID: mHaNRhMzPWSTIIbN7-1jEA_1767703865 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 79D0C195605B; Tue, 6 Jan 2026 12:51:04 +0000 (UTC) Received: from fedora (unknown [10.44.33.62]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id 00304180049F; Tue, 6 Jan 2026 12:50:56 +0000 (UTC) Received: by fedora (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Tue, 6 Jan 2026 13:51:04 +0100 (CET) Date: Tue, 6 Jan 2026 13:50:55 +0100 From: Oleg Nesterov To: Qing Wang Cc: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, akpm@linux-foundation.org, david@kernel.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, vbabka@suse.cz, rppt@kernel.org, brauner@kernel.org, mjguzik@gmail.com, jack@suse.cz, joel.granados@kernel.org, linux-kernel@vger.kernel.org, syzbot+e0378d4f4fe57aa2bdd0@syzkaller.appspotmail.com Subject: Re: [PATCH] fork/pid: Fix use-after-free in __task_pid_nr_ns Message-ID: References: <20260105043627.1758935-1-wangqing7171@gmail.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: X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 On a second thought... sched_fork() is called before perf_event_init_task(). So perhaps sync_child_event() could also check task->__state != TASK_NEW before perf_event_read_event() ? Not sure, I know nothing about perf. Would be nice if perf experts can take a look. Oleg. On 01/06, Oleg Nesterov wrote: > > On 01/05, Qing Wang wrote: > > > > The race condition occurs between the failure path of copy_process() and > > getting the PIDTYPE_TGID via __task_pid_nr_ns(). > > > > Bug timeline: > > Task B > > perf_event_open() > > Task A <--------------------------- clone() > > copy_process() > > perf_event_init_task() > > ... > > one copy failed > > free_signal_struct() close(event_fd) > > perf_child_detach() > > __task_pid_nr_ns() > > access child task->signal > > Sorry, this description very confusing to me... Is it Task B who does > clone? Or another Task A does copy_process() ? Could you write a more > clear changelog? > > > bad_fork_cleanup_signal: > > - if (!(clone_flags & CLONE_THREAD)) > > - free_signal_struct(p->signal); > > + if (!(clone_flags & CLONE_THREAD)) { > > + free_sig = p->signal; > > + p->signal = NULL; > > + free_signal_struct(free_sig); > > + } > > bad_fork_cleanup_sighand: > > __cleanup_sighand(p->sighand); > > bad_fork_cleanup_fs: > > diff --git a/kernel/pid.c b/kernel/pid.c > > index a31771bc89c1..1a012e033552 100644 > > --- a/kernel/pid.c > > +++ b/kernel/pid.c > > @@ -329,9 +329,9 @@ EXPORT_SYMBOL_GPL(find_vpid); > > > > static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type) > > { > > - return (type == PIDTYPE_PID) ? > > - &task->thread_pid : > > - &task->signal->pids[type]; > > + if (type == PIDTYPE_PID) > > + return &task->thread_pid; > > + return task->signal ? &task->signal->pids[type] : NULL; > > } > > At first glance this is racy. Can't task->signal be freed right after > the check? > > And... Can't we make another fix? If copy_process() fails and does > free_signal_struct(), the child has not been added to rcu protected > lists and init_task_pid(child) was not called yet. > > So perhaps something like the patch below can work? > > Oleg. > --- > > --- x/kernel/events/core.c > +++ x/kernel/events/core.c > @@ -1422,16 +1422,17 @@ unclone_ctx(struct perf_event_context *c > static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p, > enum pid_type type) > { > - u32 nr; > + u32 nr = 0; > /* > * only top level events have the pid namespace they were created in > */ > if (event->parent) > event = event->parent; > > - nr = __task_pid_nr_ns(p, type, event->ns); > + if (pid_alive(p)) > + nr = __task_pid_nr_ns(p, type, event->ns); > /* avoid -1 if it is idle thread or runs in another ns */ > - if (!nr && !pid_alive(p)) > + if (!nr) > nr = -1; > return nr; > }