Skip to contentSkip to conversation

tools: fwumdata: Fix use-after-free in parse_config()

PATCH In review Kory Maincent 2 participants 2 messages Apr 7 – Apr 28

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: fwumdata: Fix use-after-free in parse_config()

In parse_config(), devname is dynamically allocated by sscanf(). When sscanf() fails to fill enough fields (rc < 3), devname is freed and the loop continues to the next line. However, if the next call to sscanf() fails to match (rc == 0), devname is not written and still holds the stale freed pointer. The subsequent free(devname) then operates on already-freed memory.

Fix this by resetting devname to NULL before each sscanf() call, so that a non-matching call leaves a NULL pointer and the subsequent free() becomes a harmless no-op.

---
 tools/fwumdata_src/fwumdata.c | 1 +
 1 file changed, 1 insertion(+)
tools/fwumdata_src/fwumdata.c +1 -0
index c5b0f56842d..44195ce2bf2 100644
@@ -84,6 +84,7 @@ static int parse_config(const char *fname)
 		if (line[0] == '#' || line[0] == '\n')
 			continue;
 
+		devname = NULL;
 		rc = sscanf(line, "%ms %lli %lx %lx",
 			    &devname,
 			    &devices[i].devoff,
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Kory

Quoted 8 lines · Kory Maincent

In parse_config(), devname is dynamically allocated by sscanf(). When sscanf() fails to fill enough fields (rc < 3), devname is freed and the loop continues to the next line. However, if the next call to sscanf() fails to match (rc == 0), devname is not written and still holds the stale freed pointer. The subsequent free(devname) then operates on already-freed memory.

Quoted 8 lines

Fix this by resetting devname to NULL before each sscanf() call, so that a non-matching call leaves a NULL pointer and the subsequent free() becomes a harmless no-op.

---

Quoted 18 lines
 tools/fwumdata_src/fwumdata.c | 1 +
 1 file changed, 1 insertion(+)
tools/fwumdata_src/fwumdata.c +1 -1
index c5b0f56842d..44195ce2bf2 100644
@@ -84,6 +84,7 @@ static int parse_config(const char *fname)
                if (line[0] == '#' || line[0] == '\n')
                        continue;
 
+               devname = NULL;
                rc = sscanf(line, "%ms %lli %lx %lx",
                            &devname,
                            &devices[i].devoff,
--

2.43.0