Skip to content

Commit

Permalink
module: Fix use-after-free bug in read_file_mod_stats()
Browse files Browse the repository at this point in the history
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: df3e764 ("module: add debug stats to help identify memory pressure")
Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
  • Loading branch information
harshimogalapalli authored and mcgrof committed May 22, 2023
1 parent 44c026a commit d36f6ef
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion kernel/module/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d36f6ef

Please sign in to comment.