From nobody Sat Feb 7 17:48:38 2026 Received: from mail-pf1-f195.google.com (mail-pf1-f195.google.com [209.85.210.195]) (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 248862ED159 for ; Sun, 1 Feb 2026 11:34:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769945694; cv=none; b=HxQEEy5kz3gadmZSMNfUOfOAfSTO8jXYwAiV6k/O2aeFTG7Ns1Mf9frEGn8LPFRfsmAt7+af1h+Gd+bjAcYPBagDQanhHtojpaL6+rYvG5REqb0kX8TSOKVyHb4zPVqcqEXg5TSvoWMEumgsw+cDJdrMqgzjUZ4VvQZde2k72SM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769945694; c=relaxed/simple; bh=eZXo11M6bHHsqgdDRZ6nyiwm1HQFMosjdqmj0lsDnJ4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WDANDxEf1BulI51MJ8NjQtZXJ13ozWjCWDdZPw1nzcWYSLRaKm0tmL4UVK4fqBSztyTNeLJkntYWBsgqFqbisT//b04gQ1+JEwCn2QOBgV2yKz8URvEfQYqvCVLc8QL7p3g9T7CP4VpQLqZcNZQHkwOA7AaOGY2OWnEC+866K34= 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=jqRIX3/N; arc=none smtp.client-ip=209.85.210.195 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="jqRIX3/N" Received: by mail-pf1-f195.google.com with SMTP id d2e1a72fcca58-82310b74496so2033666b3a.3 for ; Sun, 01 Feb 2026 03:34:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769945691; x=1770550491; 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=c2mbN5TpWYvaDU6GwRvqVdLjXhGGLpqs5GSJMcsrbBg=; b=jqRIX3/NaQBKeHeNDcfQ7rBQr4+dASUbZb5ojbYqsFcPga8gyHHg7DMWDcW58HPzTD 5DHbEWAjTB7Q2FMKWFc3uS3hgGcbqANrVQEsfQ3jL8s1MqVyQSteqQ97ni4bzRuhOIyS QLR/ePTr4HA7hSnP28pCMlsCH+Dd/9gvXoRbuSUd8enRg4RW/yOSz9nZ+3wcm4GLqOCK xFuJKY8kB+K2FF0Q5R5GX/6p9sd6wf36dG4xKB+V+erDgEGibBM/fM1M/5dRnLiHuQLo n+A2+LT3tYLvnP55nN7QlBgLpDBjOi38eKtncPBriotksgxLXrtKLJknH0EtgJeIeoH/ nhkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769945691; x=1770550491; 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=c2mbN5TpWYvaDU6GwRvqVdLjXhGGLpqs5GSJMcsrbBg=; b=kzAz8Kg9pvi2nL2KMc2stECHiQIw9lXm/a4fB7NJyU5/cgGZ9vqGlCBfoKlP0Ti+gd iv2jfhb/b8CDPwacpYRTwg5AAm5kwaKeWlKsb/VqOMDrRW8GbI36vD3APOovymPi6wZw PRFPCmOEcTwJDcW7kDhaduArG/aqpEYGa4dBQfe3v89q6k80Kl1Tg+EVQyHAFGxQaAlF 95y74GcscjJWLlm++m5CdNb5YgHj6SXOLuLZaL4Bloamxl9xTCH+qLOFOJszlm+gP4dp S0FhJlhcJKfHOryP4vtcbvbIMDmIf1v8Acy1c2N5XRZSRMA49zx74PEbbBweC0pvPDJS 8udw== X-Forwarded-Encrypted: i=1; AJvYcCW4PQLu33heW9I9H0HwOcKBPolc2DY5z1Ei+dNVGx2WDWbzCELcFtxGEFpTLCwciMA5Gd8nZaU7DLC5co0=@vger.kernel.org X-Gm-Message-State: AOJu0YyVxcRDsCNNwSwTSJf/DQogOfMjDsPTXkNEuGBoj0Lt7IdNo9po 0+wlSKLAGxaLSJ+pghM8qg322y2uKLWUMe9RAOalJl5Lm6Nmu/3xQfhW X-Gm-Gg: AZuq6aJRkdcYfmnVG7pHdzgoFJc+5IW9PWaVs15cqd3MSISJ4P2ohVyh2dvzrDoIU6C pYBYPBCXgR1LouTc99duFQhKTU1vz01WaEzf1QiTh4dYjf0rVLvHQivW65IjIs5B7DEF+cVVgke EO+24PdFy6r7/U+fWwigmrTdnOZq3nvsliE+bnKYXIgP03SPOadaowVYym5pAR/sZ/EAFHI+20n N0WsSIve5WSQJZqQdCjedtHumzJXrM2RbGwbPyzQGo7JF/mgDsucptxJfB/TNIWdT65fi8ascD4 Ox5g7r1krbxtFWtn+7UF5MhW+in+eEbL7filQ1suLdOuAwDRI+e/RlE5XlIQS0G+RWywvjloyj3 XleBvVBRRWYWqpYMB8DesbPr5XzudNYwVEHz0rB7uh1tPnEyUx3Jqp6lv8CSed9WUuGNfIgv/Tc XxBJwyn92R3hFjf2MNgaJ9TeiaVrdDjkkAkB4= X-Received: by 2002:a05:6a20:43a1:b0:366:14af:9bbb with SMTP id adf61e73a8af0-392e019922amr8124082637.69.1769945691281; Sun, 01 Feb 2026 03:34:51 -0800 (PST) Received: from LAPTOP-1HUHJV8R.localdomain ([222.205.46.87]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a88b346de2sm115208875ad.0.2026.02.01.03.34.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Feb 2026 03:34:50 -0800 (PST) From: Haocheng Yu To: acme@kernel.org Cc: security@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH] perf/core: Fix refcount bug and potential UAF in perf_mmap Date: Sun, 1 Feb 2026 19:34:36 +0800 Message-ID: <20260201113446.4328-1-yuhaocheng035@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <2026020124-flashbulb-stumble-f24a@gregkh> References: <2026020124-flashbulb-stumble-f24a@gregkh> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The issue is caused by a race condition between mmap() and event teardown. In perf_mmap(), the ring_buffer (rb) is accessed via map_range() after the mmap_mutex is released. If another thread closes the event or detaches the buffer during this window, the reference count of rb can drop to zero, leading to a UAF or refcount saturation when map_range() or subsequent logic attempts to use it. Fix this by extending the scope of mmap_mutex to cover the entire setup process, including map_range(), ensuring the buffer remains valid until the mapping is complete. Signed-off-by: Haocheng Yu --- kernel/events/core.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 2c35acc2722b..7c93f7d057cb 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7167,28 +7167,28 @@ static int perf_mmap(struct file *file, struct vm_a= rea_struct *vma) ret =3D perf_mmap_aux(vma, event, nr_pages); if (ret) return ret; - } - - /* - * Since pinned accounting is per vm we cannot allow fork() to copy our - * vma. - */ - vm_flags_set(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP); - vma->vm_ops =3D &perf_mmap_vmops; =20 - mapped =3D get_mapped(event, event_mapped); - if (mapped) - mapped(event, vma->vm_mm); - - /* - * Try to map it into the page table. On fail, invoke - * perf_mmap_close() to undo the above, as the callsite expects - * full cleanup in this case and therefore does not invoke - * vmops::close(). - */ - ret =3D map_range(event->rb, vma); - if (ret) - perf_mmap_close(vma); + /* + * Since pinned accounting is per vm we cannot allow fork() to copy our + * vma. + */ + vm_flags_set(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP); + vma->vm_ops =3D &perf_mmap_vmops; + + mapped =3D get_mapped(event, event_mapped); + if (mapped) + mapped(event, vma->vm_mm); + + /* + * Try to map it into the page table. On fail, invoke + * perf_mmap_close() to undo the above, as the callsite expects + * full cleanup in this case and therefore does not invoke + * vmops::close(). + */ + ret =3D map_range(event->rb, vma); + if (ret) + perf_mmap_close(vma); + } =20 return ret; } --=20 2.51.0