From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3BB22737EB for ; Tue, 6 Jan 2026 10:06:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.196 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767694007; cv=none; b=KNLmXuD+JihU2VA5E8XyuG3ncdkUQNL451y/aj5VosGd4yRIlbs6IErU6wWf8jrVnXpOrxzTlUMCgyDPpzDgyhkz3qbHKmQ6CuKneag04Nr2Szl10nYIl3+i1WnxG4/mJchDMFPZBgkvR1VWAlORVnYK38vZEhpfLlOSYNaRiXU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767694007; c=relaxed/simple; bh=FPq+hVla/kgrU75FNwUO7PvWzZqU+gGKeGURYAVx7fQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=th0ua2qfqi3/SjMxPVWUkOQNQ9bt6q6WnkM4rcFnE/Bo0NECLOZ3wpDRWKo65qzZGJRfVdQWuH2J+glHTQvPhK7VLItJhfejyRRarEmXqWrnaZnkfODq4tSPum7FwHH2m95dzeJ29uDimXxXvEme+EB5anRvqCAylKn69EQojA0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=NSdoAaPO; arc=none smtp.client-ip=209.85.210.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NSdoAaPO" Received: by mail-pf1-f196.google.com with SMTP id d2e1a72fcca58-7acd9a03ba9so866691b3a.1 for ; Tue, 06 Jan 2026 02:06:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767694005; x=1768298805; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fyjBDmqYGYlsT9MUDjngqDgkA7cZrVa75NTOB9Fyfrs=; b=NSdoAaPOaRn6e3TlaBZVSSl5qPe2TcmMY8pcEolwFHAXbvxpw08GtpCxHdtj8wUhaw 6jv3NrDLJ9OkOiqRf3It32YPAvWXajau24quQ2r3MEHVtuU5j7b2usTMTZdrvaoCwACZ DZnKk+6E2M/XKsu/UAHHX4rfY49WAShbffNmt7dpp/EqqKFUyjLq1jpBxzNPbnbm1uxL dx/dapzKrRvEXsB2R7nPP/Dc2hSYF21+4AtHnZh7LyDlrkB2KXAnhqrS2dua1GFyBqR4 X2jh3CX86XHG6UUNElioRoFxjJ1+QBtcT3J49DYTCfSwdOZ72/jh3cD+NZ1e/pVofDdk OJyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767694005; x=1768298805; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fyjBDmqYGYlsT9MUDjngqDgkA7cZrVa75NTOB9Fyfrs=; b=keHxPz97g9F+kZ8zAoCEW3v38wdreS+oP8Qed855sWbG5XZ5hCXxHtcUANyXKFw6ly A1Xmaw+d5nh/yWCqbpPdvJVeh3SI7+wp7jtrR7oCLEwnv+5rEFc1qwmFMoz5SMGwHOY2 eyJ4S5OyVEj1rQBdlrUWN5OPp/mL4xbXlrylIzDDJyfnARUncvLwCrDqkMaaXvdNnOJq Pzj6FhsRr6d9nYn2JUwiCKQvgJGj+rATaKjWKCjBdIYlO0fPmT87UH49xki75Dd9ekD7 BxMUnNR+JksDwTrWX+Z0DMYl7yIMAE+XqjGJa3sYgb8s6PjjNABsWTt2jZT002OxircK gsQA== X-Forwarded-Encrypted: i=1; AJvYcCUfXc5HQ19jjG+rWLgb93URfpLpwi2E5XPusKl+nxkIMvAPEBs5zPIgKB22C/7NoCgl68z0jN8k6m0lgys=@vger.kernel.org X-Gm-Message-State: AOJu0YwxMzO+zVF1UTCrHRUhi+O9w4pfs3mGBpRSrzyckAaHKhrc/Ecf 1zFcDok/eB1jRYFOb5mYInZb6WZeynxH1VGdxT+06sJCh2tR8RKcPXGr X-Gm-Gg: AY/fxX6ckTGwH7i7v2iHscLgGvOiwjOfnPQCcSkhMlM2gBWWz5CuIZu+//nf4TRjo6+ mKR8WaoldDct0HdE5dSUSNI2RYuo/QgccxgNulsOIoEw3Bir9EmoASAcitzgqFp22cw2BzOwlam xIvMan3ZpNV5zlIelLZIpzYnYGoFlkFMHwW5iuC2zurps02eybuNTXvyJuNE0J1b/IDEuc1TF4Q bgzGlcTbFFgqEPehEbHzAEdHLZpA9whXx3grLzHpWjSDFN/HKg3YynscTiuT3OC1VgWifc+3RVb 1E50FBDR4E4Pkfw3t9Dz1O+sFzeGr4kCQvrYFAgiv64UaE2YhOpMdgRojFGx9hZdx3F78r6OS/V 9f1bzSp3ZVmAi0Dg2dyw9PcEgJRKeNOb/vHbSPBryXh0Z0xoU8Bx0omrr+b/8nwzs2sugYLl2/N HqNIBnkDdHalaYXDUctDkXZpEFKi0Jh/wGbixRfQ== X-Google-Smtp-Source: AGHT+IE+473eMseSkh1dO19LnDYvKoHrEbL00QxlzXWzm8npl1t7Ik1oQWALiBRR455Ym03PJyjpEg== X-Received: by 2002:a05:6a21:e097:b0:35e:1a80:464 with SMTP id adf61e73a8af0-389823c3fe3mr2030549637.46.1767694005126; Tue, 06 Jan 2026 02:06:45 -0800 (PST) Received: from lima-ubuntu.hz.ali.com ([47.246.98.217]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-34f5f7b1cc1sm1858667a91.2.2026.01.06.02.06.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jan 2026 02:06:44 -0800 (PST) From: Qing Wang To: oleg@redhat.com Cc: Liam.Howlett@oracle.com, akpm@linux-foundation.org, brauner@kernel.org, bsegall@google.com, david@kernel.org, dietmar.eggemann@arm.com, jack@suse.cz, joel.granados@kernel.org, juri.lelli@redhat.com, linux-kernel@vger.kernel.org, lorenzo.stoakes@oracle.com, mingo@redhat.com, mjguzik@gmail.com, peterz@infradead.org, rostedt@goodmis.org, rppt@kernel.org, syzbot+e0378d4f4fe57aa2bdd0@syzkaller.appspotmail.com, vbabka@suse.cz, vincent.guittot@linaro.org, wangqing7171@gmail.com Subject: Re: [PATCH] fork/pid: Fix use-after-free in __task_pid_nr_ns Date: Tue, 6 Jan 2026 18:06:35 +0800 Message-Id: <20260106100635.2757709-1-wangqing7171@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: 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=UTF-8 Content-Transfer-Encoding: 8bit On Tue, 06 Jan 2026 at 17:04, Oleg Nesterov wrote: > 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? The "<---...---clone" graph may have misled you. What I meant was that Task A is cloned from Task B. The modified bug timeline with explanation: 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 perf_event_init_task() 1. Task B create perf events by perf_event_open(). 2. Task B clone Task A, and Task A have perf events copied from Task B in this clone(). 3. Task A do one clone and fail to copy one(eg. copy_mm) in copy_process(), then goto cleanup free_signal_struct(). 4. Task B do close(event_fd), and access Task A's signal after free_signal_struct() and before perf_event_init_task() in Task A. > 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; > } I think it doesn't work, as I explained in my previous reply to Andrew: A newly created task should not be visible to other CPUs during creation: The perf subsystem copies the parent’s events to the child during copy_process(). Later, when the parent closes its own perf event, it may traverse child events and access child_ctx->task->signal. This means that a child process that has not yet been fully created can be referenced by other CPUs.