From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f196.google.com (mail-yw1-f196.google.com [209.85.128.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 812BC38B7BC for ; Sat, 16 May 2026 22:34:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.196 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778970888; cv=none; b=Pz7Ax05rXvyIKW4HPlTSsSzJaH12hjaIWimjbg/56/ZEz80lku8Czy2RhOYMNryiKL038ZAdQmdNE7VDMhwi5nqzMYJHz39Gm8WuDPd+ASzQNIk+OtOKxCxhETYxKtrIaKAGV+5a9oksVoSaA8s1ENxKIi9hkhmvupC1yvXl6lk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778970888; c=relaxed/simple; bh=RGTQjDI4J9zN6ogXHBydYjAWRRH6xgj1FRwnKHtWy40=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iz96V40w7zPz99vc33HWnwBMsKedaa+1dpqwCLq3bgROhwf5+nQYbo8cOeP2hrJD8DQv+3JZpfUsgzb2n/jFSYQH26vSmVMNCI/bZvwlbXZ0jU35eZUqsMM3pvRFjkuAsco/YXg+3OfSfQjcDDoHDxEi2YvhsCMCwBjHJrlzKm0= 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=KGKi44VY; arc=none smtp.client-ip=209.85.128.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="KGKi44VY" Received: by mail-yw1-f196.google.com with SMTP id 00721157ae682-7bb0d18c7f9so4385677b3.0 for ; Sat, 16 May 2026 15:34:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778970885; x=1779575685; 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=14OZdlsCd/minckeoEg/l7pBnGdlCv1AuWKncPE3uPM=; b=KGKi44VYG/mBh+KbAnHtgO2UffNtOjBUoFwXnnFj9Nz9kBHI2k6H1rLFL76IXtQuRN WlGhH7JHJQHGAFucG1GQ2UhC4tWmlEIeksG0xIVSfDgKOjT/RP+xgTFgUCf1kInWNmNM aPzQYnTuQVArvS89g/ylhjQN7DPoDLcsJ+a/5woe3+nQF7kzoVAQz6ahj5dt2ND+5PtR 7CwI7vjY0UaCFaITPDV2VhVpH7V/pVuNHgFLJJpb6Ppyxs8V5rxCFmIRdPwYE+7J6FJ6 jEV9ADuuFRDuWlDiPsXtygnbz4LGyEI4e83GWqQYacJVl9rfF/F2P80OSvgAizjdOxE8 VydA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778970885; x=1779575685; 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=14OZdlsCd/minckeoEg/l7pBnGdlCv1AuWKncPE3uPM=; b=kH8xkeXwzdxGkQThS9YqhZG255rYcUzF83Iv+x6653OJ7TmowbMHFYrk+zJixcCfMg lKpjkhukxawHF4WrUddDsDGKgf+hhF0znqn6DKZ2liUc7JVfS6huF/cRyqi9qQUvvsQa ITNf3NdsiF/Gwmok/SN1aJfu7g3l4Pjiwy25Z4PhPOnnGwJP0gYc15233QFlqvN/wF12 27IuWE+LGoPiA1oscOE7CtJ4E3uEMQZzGXDQFCLNRA3Nh3eIweslk/wILmPg1yTeVs2p gDkKAzm3XB7rhGHhsRRuiiZRAe+mt8xyy9j6us/0UOBuxuwLB8JgngD1ZodlitBEUUuz HZ1Q== X-Forwarded-Encrypted: i=1; AFNElJ8a8hSIJLHlqRvckmxRx0pqbnN1l6zvW4lme3eSma68S2dkYbTBGbrpORLPtw87l2gH/l3d/9rf+HnbyLw=@vger.kernel.org X-Gm-Message-State: AOJu0YyUT0sZwViFZz5LGSJjXP9OAhN/2OrGWRI9Ozt+C0hPrgcA97ZM ExR+J27NAjZUztddBmZVsnDUrtIAsWEjVbeWA+nZ2iY3AKmosqWxKPk= X-Gm-Gg: Acq92OGXHDTPiWd+0AiZhUGd1+kTAhGtWOM6lsGcpuyTUfIUbPJBQDSV0y8Oleff9OH lHr0v8H5t4A0/eErXvDnrVDxg+M3bkgihqhktkNab/T4rGNbzsrtqRnwyFOjbq074wZduFtuaTN 8za5ZmPoAnUawYQHfuvQOrKa5OyQP9HWPmFn5nDy4eUoCQc87ZpOEXLgO1nVFf9c1fFla9lQ9Ch bh5QdtfEDmEsDN/4I53WJIbFwhg4jgemcnOuu63zyUZO/axIXFX+HXI5DJ42O+AZlVa/1coanPD 7XxBpIfHZ6rTUZasLM4G+KyrkCLPI0z+S7oyiDCtgkBlzXbJeFy5o8CFzQMQh5KcBAsJxMlmouN 41tPAmg3HtFgnWnRr2sCmqYAFN3nCOz5N9MSMEiwVbCyQaU/wSzsiIDp3vULfEkk4MbRu8a7PSP GesF+GMBsUfu2at+xe1x4nUXuherzpHx/2uS0SpB0uEYkIDwbXYwRu6PtBiwSMk17e+6TR/kxs9 Q== X-Received: by 2002:a05:690c:6b12:b0:7bd:d4f4:261b with SMTP id 00721157ae682-7c95b33d4b2mr103710917b3.29.1778970885288; Sat, 16 May 2026 15:34:45 -0700 (PDT) Received: from localhost (23-116-43-216.lightspeed.sntcca.sbcglobal.net. [23.116.43.216]) by smtp.gmail.com with ESMTPSA id 00721157ae682-7cc9c2e49e9sm668787b3.33.2026.05.16.15.34.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 May 2026 15:34:44 -0700 (PDT) From: Ravi Jonnalagadda To: sj@kernel.org, damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Cc: akpm@linux-foundation.org, corbet@lwn.net, bijan311@gmail.com, ajayjoshi@micron.com, honggyu.kim@sk.com, yunjeong.mun@sk.com, ravis.opensrc@gmail.com, bharata@amd.com Subject: [RFC PATCH 1/7] mm/damon/core: refcount ops owner module to prevent rmmod UAF Date: Sat, 16 May 2026 15:34:26 -0700 Message-ID: <20260516223439.4033-2-ravis.opensrc@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260516223439.4033-1-ravis.opensrc@gmail.com> References: <20260516223439.4033-1-ravis.opensrc@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit damon_select_ops() copies the registered damon_operations struct into ctx->ops by value. After damon_unregister_ops() is called from a backend module's exit path, the registry slot is cleared but any surviving ctx still holds function pointers that resolve into the unloaded module's text. Restarting kdamond on such a ctx, or invoking any ops callback, jumps into freed code. Add a struct module *owner field to damon_operations. In damon_select_ops(), take a reference to ops->owner via try_module_get() after locating the registry entry; on failure return -EBUSY without binding the ctx. If the ctx already had an ops bound (re-select case), drop the previous owner's reference before installing the new one to keep the refcount balanced. In damon_destroy_ctx(), release the reference via module_put(ctx->ops.owner). In damon_commit_ctx(), the live ops field is overwritten by a value copy from src. Balance the refcount when the owner changes: take a ref on the new owner (return -EBUSY on failure) and put the ref on the old owner before the assignment. Built-in ops sets (vaddr, paddr) leave owner = NULL; try_module_get(NULL) returns true and module_put(NULL) is a no-op. Loadable backends set owner = THIS_MODULE in their registration. Also add damon_unregister_ops() so loadable backends have a clean exit path. Signed-off-by: Ravi Jonnalagadda --- include/linux/damon.h | 4 ++++ mm/damon/core.c | 46 ++++++++++++++++++++++++++++++++++--- mm/damon/tests/core-kunit.h | 2 +- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index df7910a39b407..8e6e1cd89e551 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -682,6 +682,8 @@ enum damon_ops_id { * struct damon_operations - Monitoring operations for given use cases. * * @id: Identifier of this operations set. + * @owner: Module that provides this operations set, or NULL + * for built-in ops. * @init: Initialize operations-related data structures. * @update: Update operations-related data structures. * @prepare_access_checks: Prepare next access check of target regions. @@ -728,6 +730,7 @@ enum damon_ops_id { */ struct damon_operations { enum damon_ops_id id; + struct module *owner; void (*init)(struct damon_ctx *context); void (*update)(struct damon_ctx *context); void (*prepare_access_checks)(struct damon_ctx *context); @@ -1206,6 +1209,7 @@ int damon_commit_ctx(struct damon_ctx *old_ctx, struct damon_ctx *new_ctx); int damon_nr_running_ctxs(void); bool damon_is_registered_ops(enum damon_ops_id id); int damon_register_ops(struct damon_operations *ops); +int damon_unregister_ops(enum damon_ops_id id); int damon_select_ops(struct damon_ctx *ctx, enum damon_ops_id id); static inline bool damon_target_has_pid(const struct damon_ctx *ctx) diff --git a/mm/damon/core.c b/mm/damon/core.c index e4b9adc0a64dd..b605d36b29b1a 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -93,6 +94,31 @@ int damon_register_ops(struct damon_operations *ops) mutex_unlock(&damon_ops_lock); return err; } +EXPORT_SYMBOL_GPL(damon_register_ops); + +/** + * damon_unregister_ops() - Unregister a monitoring operations set. + * @id: ID of the operations set to unregister. + * + * Return: 0 on success, negative error code otherwise. + */ +int damon_unregister_ops(enum damon_ops_id id) +{ + if (id >= NR_DAMON_OPS) + return -EINVAL; + + /* + * Callers (typically the owning module exit path) hold a + * module ref via try_module_get() in damon_select_ops(); the + * unregister cannot race with active ctxs because module_exit + * runs only at owner refcount 0. + */ + mutex_lock(&damon_ops_lock); + memset(&damon_registered_ops[id], 0, sizeof(damon_registered_ops[id])); + mutex_unlock(&damon_ops_lock); + return 0; +} +EXPORT_SYMBOL_GPL(damon_unregister_ops); /** * damon_select_ops() - Select a monitoring operations to use with the context. @@ -112,10 +138,18 @@ int damon_select_ops(struct damon_ctx *ctx, enum damon_ops_id id) return -EINVAL; mutex_lock(&damon_ops_lock); - if (!__damon_is_registered_ops(id)) + if (!__damon_is_registered_ops(id)) { err = -EINVAL; - else - ctx->ops = damon_registered_ops[id]; + goto out; + } + if (!try_module_get(damon_registered_ops[id].owner)) { + err = -EBUSY; + goto out; + } + /* Drop previous owner ref if this ctx had ops selected before. */ + module_put(ctx->ops.owner); + ctx->ops = damon_registered_ops[id]; +out: mutex_unlock(&damon_ops_lock); return err; } @@ -835,6 +869,7 @@ void damon_destroy_ctx(struct damon_ctx *ctx) damon_for_each_sample_filter_safe(f, next_f, &ctx->sample_control) damon_destroy_sample_filter(f, &ctx->sample_control); + module_put(ctx->ops.owner); kfree(ctx); } @@ -1749,6 +1784,11 @@ int damon_commit_ctx(struct damon_ctx *dst, struct damon_ctx *src) return err; } dst->pause = src->pause; + if (src->ops.owner != dst->ops.owner) { + if (!try_module_get(src->ops.owner)) + return -EBUSY; + module_put(dst->ops.owner); + } dst->ops = src->ops; err = damon_commit_probes(dst, src); if (err) diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h index 0369c717b93db..300659b115602 100644 --- a/mm/damon/tests/core-kunit.h +++ b/mm/damon/tests/core-kunit.h @@ -342,7 +342,7 @@ static void damon_test_split_regions_of(struct kunit *test) static void damon_test_ops_registration(struct kunit *test) { struct damon_ctx *c = damon_new_ctx(); - struct damon_operations ops = {.id = DAMON_OPS_VADDR}, bak; + struct damon_operations ops = {.id = DAMON_OPS_VADDR}, bak = {}; bool need_cleanup = false; if (!c) -- 2.43.0