arch: arm64: kernel: sprintf(), 'str' needs additional 1 byte for failure processing
Subsystems (2)
PATCH 1/1 arch: arm64: kernel: sprintf(), 'str' needs additional 1 byte for failure processing
When failure occurs at the last looping cycle (when 'i == 0'), it will print "bad PC value" instead of "(%08x) ", which needs additional 1 byte.
If not add 1 byte, the str will not be NUL terminated, and the next printk() will cause issue.
--- arch/arm64/kernel/traps.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/arm64/kernel/traps.c +1 -1
@@ -100,7 +100,7 @@ static void dump_instr(const char *lvl, struct pt_regs *regs) {unsigned long addr =instruction_pointer (regs);mm_segment_t fs; -char str[sizeof ("00000000 " ) *5 +2 +1 ], *p = str; +char str[sizeof ("00000000 " ) *5 +2 +1 +1 ], *p = str;int i;/*
Signature · 1 line
1.7.7.6
Quoted 8 lines · Chen Gang
When failure occurs at the last looping cycle (when 'i == 0'), it will print "bad PC value" instead of "(%08x) ", which needs additional 1 byte.
If not add 1 byte, the str will not be NUL terminated, and the next
printk()will cause issue.
Oh, I type incorrect contents. It should be "If not add 1 byte, it will memory overflow"
I will send patch v2.
Quoted 19 lines
Signed-off-by Chen Gang <[email protected]>--- arch/arm64/kernel/traps.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)arch/arm64/kernel/traps.c +1
-1@@ -100,7 +100,7 @@ static void dump_instr(const char *lvl, struct pt_regs *regs) {unsigned long addr =instruction_pointer (regs);mm_segment_t fs; -char str[sizeof ("00000000 " ) *5 +2 +1 ], *p = str; +char str[sizeof ("00000000 " ) *5 +2 +1 +1 ], *p = str;int i;/*
Signature · 3 lines
Chen Gang Asianux Corporation
PATCH v2 1/1 arch: arm64: kernel: sprintf(), 'str' needs additional 1 byte for failure processing
In reply to Chen GangWhen failure occurs at the last looping cycle (when 'i == 0'), it will print "bad PC value" instead of "(%08x) ", which needs additional 1 byte.
If not add 1 byte, the 'str' may be memory overflow.
--- arch/arm64/kernel/traps.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/arm64/kernel/traps.c +1 -1
@@ -100,7 +100,7 @@ static void dump_instr(const char *lvl, struct pt_regs *regs) {unsigned long addr =instruction_pointer (regs);mm_segment_t fs; -char str[sizeof ("00000000 " ) *5 +2 +1 ], *p = str; +char str[sizeof ("00000000 " ) *5 +2 +1 +1 ], *p = str;int i;/*
Signature · 1 line
1.7.7.6
Hello,
Quoted 18 lines · Chen Gang
When failure occurs at the last looping cycle (when 'i == 0'), it will print "bad PC value" instead of "(%08x) ", which needs additional 1 byte.
If not add 1 byte, the 'str' may be memory overflow.
Signed-off-by Chen Gang <[email protected]>--- arch/arm64/kernel/traps.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)arch/arm64/kernel/traps.c +0
-0
arch/arm64/kernel/traps.c static void dump_instr(const char *lvl, struct pt_regs *regs) · line 104
Quoted 6 lines
arch/arm64/kernel/traps.c +1
-1@@ -100,7 +100,7 @@ static void dump_instr(const char *lvl, struct pt_regs *regs) {unsigned long addr =instruction_pointer (regs);mm_segment_t fs; -char str[sizeof ("00000000 " ) *5 +2 +1 ], *p = str; +char str[sizeof ("00000000 " ) *5 +2 +1 +1 ], *p = str;int i;
Whilst this code is difficult to follow, I don't think it's incorrect. The size of the str array is:
sizeof("00000000 ") = 8 ('0's) + 1 (space) + 1 (NUL) = 10 bytes
*5 = 50 bytes
+2 (for the two brackets when i == 0) = 52 bytes
+1 (this is for the "bad PC value") = 53 bytesIn the worst case (when the PC is bad) we print:
"%08x " x4 = (8 + 1 + 1) *4 = 40 bytes "bad PC value" = 12 + 1 = 13 bytes
so again, 53 bytes.
... or have I missed a character somewhere?
Will
Quoted 30 lines · Will Deacon
Hello,
Quoted 13 lines · Chen Gang
Quoted 13 lines
When failure occurs at the last looping cycle (when 'i == 0'), it will print "bad PC value" instead of "(%08x) ", which needs additional 1 byte.
If not add 1 byte, the 'str' may be memory overflow.
Signed-off-by Chen Gang <[email protected]>--- arch/arm64/kernel/traps.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)Quoted 11 lines
arch/arm64/kernel/traps.c static void dump_instr(const char *lvl, struct pt_regs *regs) · line 104
Quoted 11 lines
arch/arm64/kernel/traps.c +1
-1@@ -100,7 +100,7 @@ static void dump_instr(const char *lvl, struct pt_regs *regs) {unsigned long addr =instruction_pointer (regs);mm_segment_t fs; -char str[sizeof ("00000000 " ) *5 +2 +1 ], *p = str; +char str[sizeof ("00000000 " ) *5 +2 +1 +1 ], *p = str;int i;Whilst this code is difficult to follow, I don't think it's incorrect. The size of the str array is:
Quoted 1 line
sizeof("00000000 ") = 8 ('0's) + 1 (space) + 1 (NUL) = 10 bytesOh, it is my fault, I miss the "1 (NUL)".
Quoted 7 lines
*5 = 50 bytes +2 (for the two brackets when i == 0) = 52 bytes +1 (this is for the "bad PC value") = 53 bytesIn the worst case (when the PC is bad) we print:
"%08x " x4 = (8 + 1 + 1) *4 = 40 bytes
Actually, it is 36 bytes, for sprintf() return the length excluding NUL.
(although, we still need notice the last NUL)
Quoted 4 lines
"bad PC value" = 12 + 1 = 13 bytesso again, 53 bytes.
So again, 49 bytes. ;-)
Quoted 2 lines
.. or have I missed a character somewhere?
NO, I missed characters, but you 'find' more characters. :-)
All together, this patch is incorrect, original implementation will not cause issue.
Thanks
Signature · 3 lines
Chen Gang Asianux Corporation