mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] module: Fix use-after-free bug in read_file_mod_stats()
@ 2023-04-28  5:59 Harshit Mogalapalli
  2023-05-17  6:33 ` Harshit Mogalapalli
  2023-05-22 21:21 ` Luis Chamberlain
  0 siblings, 2 replies; 3+ messages in thread
From: Harshit Mogalapalli @ 2023-04-28  5:59 UTC (permalink / raw)
  To: mcgrof, linux-modules
  Cc: linux-kernel, error27, kernel-janitors, Harshit Mogalapalli

Smatch warns:
	kernel/module/stats.c:394 read_file_mod_stats()
	warn: passing freed memory 'buf'

We are passing 'buf' to simple_read_from_buffer() after freeing it.

Fix this by changing the order of 'simple_read_from_buffer' and 'kfree'.

Fixes: df3e764d8e5c ("module: add debug stats to help identify memory pressure")
Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
---
Found with statis analysis, only compile tested.
---
 kernel/module/stats.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/kernel/module/stats.c b/kernel/module/stats.c
index ad7b6ada29f2..6ab2c94d6bc3 100644
--- a/kernel/module/stats.c
+++ b/kernel/module/stats.c
@@ -276,6 +276,7 @@ static ssize_t read_file_mod_stats(struct file *file, char __user *user_buf,
 	struct mod_fail_load *mod_fail;
 	unsigned int len, size, count_failed = 0;
 	char *buf;
+	int ret;
 	u32 live_mod_count, fkreads, fdecompress, fbecoming, floads;
 	unsigned long total_size, text_size, ikread_bytes, ibecoming_bytes,
 		idecompress_bytes, imod_bytes, total_virtual_lost;
@@ -390,8 +391,9 @@ static ssize_t read_file_mod_stats(struct file *file, char __user *user_buf,
 out_unlock:
 	mutex_unlock(&module_mutex);
 out:
+	ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
 	kfree(buf);
-        return simple_read_from_buffer(user_buf, count, ppos, buf, len);
+	return ret;
 }
 #undef MAX_PREAMBLE
 #undef MAX_FAILED_MOD_PRINT
-- 
2.40.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] module: Fix use-after-free bug in read_file_mod_stats()
  2023-04-28  5:59 [PATCH] module: Fix use-after-free bug in read_file_mod_stats() Harshit Mogalapalli
@ 2023-05-17  6:33 ` Harshit Mogalapalli
  2023-05-22 21:21 ` Luis Chamberlain
  1 sibling, 0 replies; 3+ messages in thread
From: Harshit Mogalapalli @ 2023-05-17  6:33 UTC (permalink / raw)
  To: mcgrof, linux-modules
  Cc: linux-kernel, error27, kernel-janitors, arnd, dan.carpenter

Hi,
On 28/04/23 11:29 am, Harshit Mogalapalli wrote:
> Smatch warns:
> 	kernel/module/stats.c:394 read_file_mod_stats()
> 	warn: passing freed memory 'buf'
> 
> We are passing 'buf' to simple_read_from_buffer() after freeing it.
> 
> Fix this by changing the order of 'simple_read_from_buffer' and 'kfree'.
> 

Friendly reminder to review this patch.

Thanks,
Harshit


> Fixes: df3e764d8e5c ("module: add debug stats to help identify memory pressure")
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
> ---
> Found with statis analysis, only compile tested.
> ---
>   kernel/module/stats.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/module/stats.c b/kernel/module/stats.c
> index ad7b6ada29f2..6ab2c94d6bc3 100644
> --- a/kernel/module/stats.c
> +++ b/kernel/module/stats.c
> @@ -276,6 +276,7 @@ static ssize_t read_file_mod_stats(struct file *file, char __user *user_buf,
>   	struct mod_fail_load *mod_fail;
>   	unsigned int len, size, count_failed = 0;
>   	char *buf;
> +	int ret;
>   	u32 live_mod_count, fkreads, fdecompress, fbecoming, floads;
>   	unsigned long total_size, text_size, ikread_bytes, ibecoming_bytes,
>   		idecompress_bytes, imod_bytes, total_virtual_lost;
> @@ -390,8 +391,9 @@ static ssize_t read_file_mod_stats(struct file *file, char __user *user_buf,
>   out_unlock:
>   	mutex_unlock(&module_mutex);
>   out:
> +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
>   	kfree(buf);
> -        return simple_read_from_buffer(user_buf, count, ppos, buf, len);
> +	return ret;
>   }
>   #undef MAX_PREAMBLE
>   #undef MAX_FAILED_MOD_PRINT

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] module: Fix use-after-free bug in read_file_mod_stats()
  2023-04-28  5:59 [PATCH] module: Fix use-after-free bug in read_file_mod_stats() Harshit Mogalapalli
  2023-05-17  6:33 ` Harshit Mogalapalli
@ 2023-05-22 21:21 ` Luis Chamberlain
  1 sibling, 0 replies; 3+ messages in thread
From: Luis Chamberlain @ 2023-05-22 21:21 UTC (permalink / raw)
  To: Harshit Mogalapalli; +Cc: linux-modules, linux-kernel, error27, kernel-janitors

On Thu, Apr 27, 2023 at 10:59:33PM -0700, Harshit Mogalapalli wrote:
> Smatch warns:
> 	kernel/module/stats.c:394 read_file_mod_stats()
> 	warn: passing freed memory 'buf'
> 
> We are passing 'buf' to simple_read_from_buffer() after freeing it.
> 
> Fix this by changing the order of 'simple_read_from_buffer' and 'kfree'.
> 
> Fixes: df3e764d8e5c ("module: add debug stats to help identify memory pressure")
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
> ---

Queued up, thanks!

  Luis

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-05-22 21:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-28  5:59 [PATCH] module: Fix use-after-free bug in read_file_mod_stats() Harshit Mogalapalli
2023-05-17  6:33 ` Harshit Mogalapalli
2023-05-22 21:21 ` Luis Chamberlain

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®