From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 059DD328972; Wed, 17 Sep 2025 13:03:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758114209; cv=none; b=cdVOgDTiwkZvFa+iPwWzeoBFem1vreM3ItawcK9+8OZD3N2l46JLcNhDOylppFl+a24wZFM2+rNbTRpmvIk0IGn2iIdzFa3xotyGICzXcGIByMEEYgOXT9Zs9UMxTeqBpC53dYaEKDTgnEfRq57GJUn9lASs6Rrj2mXeJADrTi4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758114209; c=relaxed/simple; bh=98/La3ZnYTsINZISz01f4ssAeWPpzoaJ6cnd01nCl44=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kxNaaGSLlmK5abbVDXsGIOMyK/sYEcK/THzwTiCCQ9dbCbAaYLVXcPDIbVX+FxKbKKGY1K8R9Ha2s70vCzZZK3cau4Osiws/NwnLsqVrY81aNOFmLoo1JGFDO1I1LF3qdRpJpnIJtXGmfhUfUSGYdIJQ8R3RoraKC2OJg5ENz3g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=detEG5sq; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="detEG5sq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74E45C4CEF0; Wed, 17 Sep 2025 13:03:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1758114208; bh=98/La3ZnYTsINZISz01f4ssAeWPpzoaJ6cnd01nCl44=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=detEG5sqA0Dfn2q7phKvnzRIyj4S+zutCz3WWVC/yQxvCPpqicNzwRhEDsehRGv/N v7BTz5JJviGaPYvtl0LNNqM9hLgZZg5rlCtucd8Iw7HiTr54ihMRBj+RZQfYjOT8dk JAetbBJxryfBW5Ws1r5LP74RsCDhl+AyFEc8s8rw= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Stanislav Fort , SeongJae Park , Andrew Morton Subject: [PATCH 6.1 34/78] mm/damon/sysfs: fix use-after-free in state_show() Date: Wed, 17 Sep 2025 14:34:55 +0200 Message-ID: <20250917123330.394136771@linuxfoundation.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250917123329.576087662@linuxfoundation.org> References: <20250917123329.576087662@linuxfoundation.org> User-Agent: quilt/0.68 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Stanislav Fort commit 3260a3f0828e06f5f13fac69fb1999a6d60d9cff upstream. state_show() reads kdamond->damon_ctx without holding damon_sysfs_lock. This allows a use-after-free race: CPU 0 CPU 1 ----- ----- state_show() damon_sysfs_turn_damon_on() ctx = kdamond->damon_ctx; mutex_lock(&damon_sysfs_lock); damon_destroy_ctx(kdamond->damon_ctx); kdamond->damon_ctx = NULL; mutex_unlock(&damon_sysfs_lock); damon_is_running(ctx); /* ctx is freed */ mutex_lock(&ctx->kdamond_lock); /* UAF */ (The race can also occur with damon_sysfs_kdamonds_rm_dirs() and damon_sysfs_kdamond_release(), which free or replace the context under damon_sysfs_lock.) Fix by taking damon_sysfs_lock before dereferencing the context, mirroring the locking used in pid_show(). The bug has existed since state_show() first accessed kdamond->damon_ctx. Link: https://lkml.kernel.org/r/20250905101046.2288-1-disclosure@aisle.com Fixes: a61ea561c871 ("mm/damon/sysfs: link DAMON for virtual address spaces monitoring") Signed-off-by: Stanislav Fort Reported-by: Stanislav Fort Reviewed-by: SeongJae Park Cc: Signed-off-by: Andrew Morton Signed-off-by: SeongJae Park Signed-off-by: Greg Kroah-Hartman --- mm/damon/sysfs.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -2093,14 +2093,18 @@ static ssize_t state_show(struct kobject { struct damon_sysfs_kdamond *kdamond = container_of(kobj, struct damon_sysfs_kdamond, kobj); - struct damon_ctx *ctx = kdamond->damon_ctx; - bool running; + struct damon_ctx *ctx; + bool running = false; - if (!ctx) - running = false; - else + if (!mutex_trylock(&damon_sysfs_lock)) + return -EBUSY; + + ctx = kdamond->damon_ctx; + if (ctx) running = damon_sysfs_ctx_running(ctx); + mutex_unlock(&damon_sysfs_lock); + return sysfs_emit(buf, "%s\n", running ? damon_sysfs_cmd_strs[DAMON_SYSFS_CMD_ON] : damon_sysfs_cmd_strs[DAMON_SYSFS_CMD_OFF]);