Skip to contentSkip to conversation

cpu: prevent buffer overflow in cstate name

PATCH New Tyler Hall 1 participant 1 message Apr 21, 2015

lore
Share & tools
lore

Tyler Hall

author
#0 lore raw

PATCH 1/1 cpu: prevent buffer overflow in cstate name

strcpy to a fixed-length field should have bounds checking. ARM platforms may have longer names for these states than the 16 byte buffer. Currently powertop crashes on the Marvell MVEBU platform with cpuidle enabled.

On this platform, the "linux_name" is
"MV CPU DEEP IDLECPU and L2 Fabric power down"

Use snprintf instead of strncpy so the buffer is always null-terminated.

Signed-off-by Tyler Hall <tylerwhall(a)gmail.com>
---
 src/cpu/abstract_cpu.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/cpu/abstract_cpu.cpp +2 -2
index a3a9ffa..18d3ec1 100644
@@ -204,8 +204,8 @@ void abstract_cpu::insert_cstate(const char *linux_name, const char *human_name,
 
 	cstates.push_back(state);
 
-	strcpy(state->linux_name, linux_name);
-	strcpy(state->human_name, human_name);
+	snprintf(state->linux_name, sizeof(state->linux_name), "%s", linux_name);
+	snprintf(state->human_name, sizeof(state->human_name), "%s", human_name);
 
 	state->line_level = -1;
 
Signature · 1 line
2.3.5