[PATCH v3] cpupower/debug/i386: check mmap return value in main

longlong yan posted 1 patch 5 days, 1 hour ago
tools/power/cpupower/debug/i386/dump_psb.c | 5 +++++
1 file changed, 5 insertions(+)
[PATCH v3] cpupower/debug/i386: check mmap return value in main
Posted by longlong yan 5 days, 1 hour ago
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>

Changes in v2:
- Remove trailing whitespace and blank row

Changes in v3:
- Format Adjustment
---
 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..fcbb73b48acb 100644
--- a/tools/power/cpupower/debug/i386/dump_psb.c
+++ b/tools/power/cpupower/debug/i386/dump_psb.c
@@ -182,6 +182,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) {
 			decode_psb(p, numpst);
-- 
2.43.0
Re: [PATCH v3] cpupower/debug/i386: check mmap return value in main
Posted by Shuah Khan 4 days, 3 hours ago
On 7/19/26 19:34, 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>
> 
> Changes in v2:
> - Remove trailing whitespace and blank row
> 
> Changes in v3:
> - Format Adjustment
> ---

This is where the change information needs to be placed.

It is still not correct. Please refer to submitting patches
documentation.

>   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..fcbb73b48acb 100644
> --- a/tools/power/cpupower/debug/i386/dump_psb.c
> +++ b/tools/power/cpupower/debug/i386/dump_psb.c
> @@ -182,6 +182,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) {
>   			decode_psb(p, numpst);

thanks,
-- Shuah