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 5D60E309EE4 for ; Tue, 6 Jan 2026 09:04:35 +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=1767690277; cv=none; b=RIyl1qpc96jytk3F1dWaC/DvSClX9WPU+rrqjiG+iDtVF495pZTjvxqd/Lkd4Avee+zzeVPhxBOUQaK33vFHg7Evr1EsM6LB8UfTQlS4gXymBKvhehkSsUgo+6k/R/BSG9BFi5Jnm/pxNyw+dfUpycZ/kipP3mhShfBmeyGkFXU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767690277; c=relaxed/simple; bh=faeI243Jh/1glnGslg1d+2v7prbJknCwctlCWzRZhRQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=QY9Ktr0I0D0a42MMZcs0yZOA/vizrDytv1uMJPAPOJhPj4lI1+wk+3q5pKbMj1W7GBk89zHH08e+O9/X4x+aAvGq3MPQdvqR3+3PWEOGnDdSFU7CMByZrSCHAZ2tguwBGiLaE0PZhxglOR/fjzD9BbMyproIo/6qMNHZ5U1NRHo= 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=WQDn+Ln7; 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="WQDn+Ln7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767690274; 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=pwwdGuAHj+rNjZtGV12cU6YJhYGzsh88d2VQS/J1DV8=; b=WQDn+Ln7AEIaFqZ3Xm8epAumEf091nX9xLL94g0uzZ5Ywm9yU6CGPF8znTEtf6STiAS6qZ VKpVM9gpO5t/R+az9AYqF29/2HpFjfujc3BTKnVL1B6WTJvPda1XDQz90N6eHn76EiQzlY /IrOJ725O3hPiyVrTdmAEEWBzTUpNcU= Received: from mx-prod-mc-01.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-248-YHBp4VeFNw6s3WZSxD64Dw-1; Tue, 06 Jan 2026 04:04:29 -0500 X-MC-Unique: YHBp4VeFNw6s3WZSxD64Dw-1 X-Mimecast-MFC-AGG-ID: YHBp4VeFNw6s3WZSxD64Dw_1767690267 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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D1712195DE48; Tue, 6 Jan 2026 09:04:26 +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 576C0180045B; Tue, 6 Jan 2026 09:04:19 +0000 (UTC) Received: by fedora (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Tue, 6 Jan 2026 10:04:26 +0100 (CET) Date: Tue, 6 Jan 2026 10:04:17 +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: <20260105043627.1758935-1-wangqing7171@gmail.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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; }