From: Marco Pagani marpagan@redhat.com
mainline inclusion from mainline-v6.10-rc1 commit 4d4d2d4346857bf778fafaa97d6f76bb1663e3c9 category: bugfix bugzilla: https://gitee.com/src-openeuler/kernel/issues/IA7YM1 CVE: CVE-2024-37021
Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=...
--------------------------------
The current implementation of the fpga manager assumes that the low-level module registers a driver for the parent device and uses its owner pointer to take the module's refcount. This approach is problematic since it can lead to a null pointer dereference while attempting to get the manager if the parent device does not have a driver.
To address this problem, add a module owner pointer to the fpga_manager struct and use it to take the module's refcount. Modify the functions for registering the manager to take an additional owner module parameter and rename them to avoid conflicts. Use the old function names for helper macros that automatically set the module that registers the manager as the owner. This ensures compatibility with existing low-level control modules and reduces the chances of registering a manager without setting the owner.
Also, update the documentation to keep it consistent with the new interface for registering an fpga manager.
Other changes: opportunistically move put_device() from __fpga_mgr_get() to fpga_mgr_get() and of_fpga_mgr_get() to improve code clarity since the manager device is taken in these functions.
Fixes: 654ba4cc0f3e ("fpga manager: ensure lifetime with of_fpga_mgr_get") Suggested-by: Greg Kroah-Hartman gregkh@linuxfoundation.org Suggested-by: Xu Yilun yilun.xu@intel.com Signed-off-by: Marco Pagani marpagan@redhat.com Acked-by: Xu Yilun yilun.xu@intel.com Link: https://lore.kernel.org/r/20240305192926.84886-1-marpagan@redhat.com Signed-off-by: Xu Yilun yilun.xu@linux.intel.com
Conflicts: drivers/fpga/fpga-mgr.c include/linux/fpga/fpga-mgr.h Documentation/driver-api/fpga/fpga-mgr.rst [Liu Chuang: add owner module.] Signed-off-by: Liu Chuang liuchuang40@huawei.com --- drivers/fpga/fpga-mgr.c | 62 ++++++++++++++++++++--------------- include/linux/fpga/fpga-mgr.h | 14 +++++--- 2 files changed, 46 insertions(+), 30 deletions(-)
diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c index f38bab01432e..f65ea09af2fb 100644 --- a/drivers/fpga/fpga-mgr.c +++ b/drivers/fpga/fpga-mgr.c @@ -444,20 +444,15 @@ static struct attribute *fpga_mgr_attrs[] = { }; ATTRIBUTE_GROUPS(fpga_mgr);
-static struct fpga_manager *__fpga_mgr_get(struct device *dev) +static struct fpga_manager *__fpga_mgr_get(struct device *mgr_dev) { struct fpga_manager *mgr;
- mgr = to_fpga_manager(dev); - - if (!try_module_get(dev->parent->driver->owner)) - goto err_dev; + mgr = to_fpga_manager(mgr_dev); + if (!try_module_get(mgr->mops_owner)) + mgr = ERR_PTR(-ENODEV);
return mgr; - -err_dev: - put_device(dev); - return ERR_PTR(-ENODEV); }
static int fpga_mgr_dev_match(struct device *dev, const void *data) @@ -473,12 +468,18 @@ static int fpga_mgr_dev_match(struct device *dev, const void *data) */ struct fpga_manager *fpga_mgr_get(struct device *dev) { - struct device *mgr_dev = class_find_device(fpga_mgr_class, NULL, dev, - fpga_mgr_dev_match); + struct fpga_manager *mgr; + struct device *mgr_dev; + + mgr_dev = class_find_device(fpga_mgr_class, NULL, dev, fpga_mgr_dev_match); if (!mgr_dev) return ERR_PTR(-ENODEV);
- return __fpga_mgr_get(mgr_dev); + mgr = __fpga_mgr_get(mgr_dev); + if (IS_ERR(mgr)) + put_device(mgr_dev); + + return mgr; } EXPORT_SYMBOL_GPL(fpga_mgr_get);
@@ -491,13 +492,18 @@ EXPORT_SYMBOL_GPL(fpga_mgr_get); */ struct fpga_manager *of_fpga_mgr_get(struct device_node *node) { - struct device *dev; + struct fpga_manager *mgr; + struct device *mgr_dev;
- dev = class_find_device_by_of_node(fpga_mgr_class, node); - if (!dev) + mgr_dev = class_find_device_by_of_node(fpga_mgr_class, node); + if (!mgr_dev) return ERR_PTR(-ENODEV);
- return __fpga_mgr_get(dev); + mgr = __fpga_mgr_get(mgr_dev); + if (IS_ERR(mgr)) + put_device(mgr_dev); + + return mgr; } EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
@@ -507,7 +513,7 @@ EXPORT_SYMBOL_GPL(of_fpga_mgr_get); */ void fpga_mgr_put(struct fpga_manager *mgr) { - module_put(mgr->dev.parent->driver->owner); + module_put(mgr->mops_owner); put_device(&mgr->dev); } EXPORT_SYMBOL_GPL(fpga_mgr_put); @@ -546,20 +552,21 @@ void fpga_mgr_unlock(struct fpga_manager *mgr) EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
/** - * fpga_mgr_create - create and initialize a FPGA manager struct + * __fpga_mgr_create - create and initialize a FPGA manager struct * @dev: fpga manager device from pdev * @name: fpga manager name * @mops: pointer to structure of fpga manager ops * @priv: fpga manager private data + * @owner: owner module containing the ops * * The caller of this function is responsible for freeing the struct with * fpga_mgr_free(). Using devm_fpga_mgr_create() instead is recommended. * * Return: pointer to struct fpga_manager or NULL */ -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name, +struct fpga_manager *__fpga_mgr_create(struct device *dev, const char *name, const struct fpga_manager_ops *mops, - void *priv) + void *priv, struct module *owner) { struct fpga_manager *mgr; int id, ret; @@ -586,6 +593,8 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
mutex_init(&mgr->ref_mutex);
+ mgr->mops_owner = owner; + mgr->name = name; mgr->mops = mops; mgr->priv = priv; @@ -610,7 +619,7 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
return NULL; } -EXPORT_SYMBOL_GPL(fpga_mgr_create); +EXPORT_SYMBOL_GPL(__fpga_mgr_create);
/** * fpga_mgr_free - free a FPGA manager created with fpga_mgr_create() @@ -631,11 +640,12 @@ static void devm_fpga_mgr_release(struct device *dev, void *res) }
/** - * devm_fpga_mgr_create - create and initialize a managed FPGA manager struct + * __devm_fpga_mgr_create - create and initialize a managed FPGA manager struct * @dev: fpga manager device from pdev * @name: fpga manager name * @mops: pointer to structure of fpga manager ops * @priv: fpga manager private data + * @owner: owner module containing the ops * * This function is intended for use in a FPGA manager driver's probe function. * After the manager driver creates the manager struct with @@ -647,9 +657,9 @@ static void devm_fpga_mgr_release(struct device *dev, void *res) * * Return: pointer to struct fpga_manager or NULL */ -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name, +struct fpga_manager *__devm_fpga_mgr_create(struct device *dev, const char *name, const struct fpga_manager_ops *mops, - void *priv) + void *priv, struct module *owner) { struct fpga_manager **ptr, *mgr;
@@ -657,7 +667,7 @@ struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name, if (!ptr) return NULL;
- mgr = fpga_mgr_create(dev, name, mops, priv); + mgr = __fpga_mgr_create(dev, name, mops, priv, owner); if (!mgr) { devres_free(ptr); } else { @@ -667,7 +677,7 @@ struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
return mgr; } -EXPORT_SYMBOL_GPL(devm_fpga_mgr_create); +EXPORT_SYMBOL_GPL(__devm_fpga_mgr_create);
/** * fpga_mgr_register - register a FPGA manager diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h index e8ca62b2cb5b..7c97e1c359b0 100644 --- a/include/linux/fpga/fpga-mgr.h +++ b/include/linux/fpga/fpga-mgr.h @@ -162,6 +162,7 @@ struct fpga_compat_id { * @state: state of fpga manager * @compat_id: FPGA manager id for compatibility check. * @mops: pointer to struct of fpga manager ops + * @mops_owner: module containing the mops * @priv: low level driver private date */ struct fpga_manager { @@ -171,6 +172,7 @@ struct fpga_manager { enum fpga_mgr_states state; struct fpga_compat_id *compat_id; const struct fpga_manager_ops *mops; + struct module *mops_owner; void *priv; };
@@ -191,15 +193,19 @@ struct fpga_manager *fpga_mgr_get(struct device *dev);
void fpga_mgr_put(struct fpga_manager *mgr);
-struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name, +#define fpga_mgr_create(dev, name, mops, priv) \ + __fpga_mgr_create(dev, name, mops, priv, THIS_MODULE) +struct fpga_manager *__fpga_mgr_create(struct device *dev, const char *name, const struct fpga_manager_ops *mops, - void *priv); + void *priv, struct module *owner); void fpga_mgr_free(struct fpga_manager *mgr); int fpga_mgr_register(struct fpga_manager *mgr); void fpga_mgr_unregister(struct fpga_manager *mgr);
-struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name, +#define devm_fpga_mgr_create(dev, name, mops, priv) \ + __devm_fpga_mgr_create(dev, name, mops, priv, THIS_MODULE) +struct fpga_manager *__devm_fpga_mgr_create(struct device *dev, const char *name, const struct fpga_manager_ops *mops, - void *priv); + void *priv, struct module *owner);
#endif /*_LINUX_FPGA_MGR_H */
反馈: 您发送到kernel@openeuler.org的补丁/补丁集,已成功转换为PR! PR链接地址: https://gitee.com/openeuler/kernel/pulls/9836 邮件列表地址:https://mailweb.openeuler.org/hyperkitty/list/kernel@openeuler.org/message/J...
FeedBack: The patch(es) which you have sent to kernel@openeuler.org mailing list has been converted to a pull request successfully! Pull request link: https://gitee.com/openeuler/kernel/pulls/9836 Mailing list address: https://mailweb.openeuler.org/hyperkitty/list/kernel@openeuler.org/message/J...