Linux Power Management development
 help / color / mirror / Atom feed
* [PATCH] cpupower/debug/i386: check mmap return value in main
@ 2026-06-29  3:11 longlong yan
  2026-07-16 23:26 ` Shuah
  0 siblings, 1 reply; 2+ messages in thread
From: longlong yan @ 2026-06-29  3:11 UTC (permalink / raw)
  To: trenn, shuah, jwyatt, jkacur
  Cc: skhan, sageofredondo, linux-pm, linux-kernel, longlong yan

The return value of mmap() is not checked. On failure mmap() returns
MAP_FAILED, not NULL. If mmap() fails, the subsequent for-loop
dereferences the invalid pointer (MAP_FAILED), causing a segfault.

Add a check for MAP_FAILED after mmap(), consistent with the existing
error handling style for open() in the same function.

Fixes: 7fe2f6399a84 ("cpupowerutils - cpufrequtils extended with quite some features")
Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
---
 tools/power/cpupower/debug/i386/dump_psb.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/power/cpupower/debug/i386/dump_psb.c b/tools/power/cpupower/debug/i386/dump_psb.c
index 6fb81b42ea61..9da519200433 100644
--- a/tools/power/cpupower/debug/i386/dump_psb.c
+++ b/tools/power/cpupower/debug/i386/dump_psb.c
@@ -181,6 +181,11 @@ main(int argc, char *argv[])
 
 	mem = mmap(mem, 0x100000 - 0xc0000, PROT_READ, MAP_SHARED, fd, 0xc0000);
 	close(fd);
+	
+	if (mem == MAP_FAILED) {
+		printf ("Couldn't mmap /dev/mem\n");
+		exit(1);
+	}
 
 	for (p = mem; p - mem < LEN; p+=16) {
 		if (memcmp(p, "AMDK7PNOW!", 10) == 0) {
-- 
2.43.0


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

* Re: [PATCH] cpupower/debug/i386: check mmap return value in main
  2026-06-29  3:11 [PATCH] cpupower/debug/i386: check mmap return value in main longlong yan
@ 2026-07-16 23:26 ` Shuah
  0 siblings, 0 replies; 2+ messages in thread
From: Shuah @ 2026-07-16 23:26 UTC (permalink / raw)
  To: longlong yan, trenn, jwyatt, jkacur
  Cc: skhan, sageofredondo, linux-pm, linux-kernel, Shuah Khan

On 6/28/26 21:11, longlong yan wrote:
> The return value of mmap() is not checked. On failure mmap() returns
> MAP_FAILED, not NULL. If mmap() fails, the subsequent for-loop
> dereferences the invalid pointer (MAP_FAILED), causing a segfault.
> 
> Add a check for MAP_FAILED after mmap(), consistent with the existing
> error handling style for open() in the same function.
> 
> Fixes: 7fe2f6399a84 ("cpupowerutils - cpufrequtils extended with quite some features")
> Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
> ---
>   tools/power/cpupower/debug/i386/dump_psb.c | 5 +++++
>   1 file changed, 5 insertions(+)
> 
> diff --git a/tools/power/cpupower/debug/i386/dump_psb.c b/tools/power/cpupower/debug/i386/dump_psb.c
> index 6fb81b42ea61..9da519200433 100644
> --- a/tools/power/cpupower/debug/i386/dump_psb.c
> +++ b/tools/power/cpupower/debug/i386/dump_psb.c
> @@ -181,6 +181,11 @@ main(int argc, char *argv[])
>   
>   	mem = mmap(mem, 0x100000 - 0xc0000, PROT_READ, MAP_SHARED, fd, 0xc0000);
>   	close(fd);
> +	
> +	if (mem == MAP_FAILED) {
> +		printf ("Couldn't mmap /dev/mem\n");
> +		exit(1);
> +	}
>   
>   	for (p = mem; p - mem < LEN; p+=16) {
>   		if (memcmp(p, "AMDK7PNOW!", 10) == 0) {

Thank you for the patch. Sorry for a late response.

Please send v2 after fixing checkpatch errors and warnings.

thanks,
-- Shuah

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

end of thread, other threads:[~2026-07-16 23:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-29  3:11 [PATCH] cpupower/debug/i386: check mmap return value in main longlong yan
2026-07-16 23:26 ` Shuah

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