| From 2eb11e08ea15d783bc92687816a35d1ab2648755 Mon Sep 17 00:00:00 2001 |
| From: Sasha Levin <sashal@kernel.org> |
| Date: Mon, 30 Jun 2025 16:32:32 +0200 |
| Subject: module: Prevent silent truncation of module name in delete_module(2) |
| |
| From: Petr Pavlu <petr.pavlu@suse.com> |
| |
| [ Upstream commit a6323bd4e611567913e23df5b58f2d4e4da06789 ] |
| |
| Passing a module name longer than MODULE_NAME_LEN to the delete_module |
| syscall results in its silent truncation. This really isn't much of |
| a problem in practice, but it could theoretically lead to the removal of an |
| incorrect module. It is more sensible to return ENAMETOOLONG or ENOENT in |
| such a case. |
| |
| Update the syscall to return ENOENT, as documented in the delete_module(2) |
| man page to mean "No module by that name exists." This is appropriate |
| because a module with a name longer than MODULE_NAME_LEN cannot be loaded |
| in the first place. |
| |
| Signed-off-by: Petr Pavlu <petr.pavlu@suse.com> |
| Reviewed-by: Daniel Gomez <da.gomez@samsung.com> |
| Link: https://lore.kernel.org/r/20250630143535.267745-2-petr.pavlu@suse.com |
| Signed-off-by: Daniel Gomez <da.gomez@samsung.com> |
| Signed-off-by: Sasha Levin <sashal@kernel.org> |
| --- |
| kernel/module/main.c | 10 ++++++---- |
| 1 file changed, 6 insertions(+), 4 deletions(-) |
| |
| diff --git a/kernel/module/main.c b/kernel/module/main.c |
| index 554aba47ab68..3269f6c46814 100644 |
| --- a/kernel/module/main.c |
| +++ b/kernel/module/main.c |
| @@ -699,14 +699,16 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user, |
| struct module *mod; |
| char name[MODULE_NAME_LEN]; |
| char buf[MODULE_FLAGS_BUF_SIZE]; |
| - int ret, forced = 0; |
| + int ret, len, forced = 0; |
| |
| if (!capable(CAP_SYS_MODULE) || modules_disabled) |
| return -EPERM; |
| |
| - if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0) |
| - return -EFAULT; |
| - name[MODULE_NAME_LEN-1] = '\0'; |
| + len = strncpy_from_user(name, name_user, MODULE_NAME_LEN); |
| + if (len == 0 || len == MODULE_NAME_LEN) |
| + return -ENOENT; |
| + if (len < 0) |
| + return len; |
| |
| audit_log_kern_module(name); |
| |
| -- |
| 2.39.5 |
| |