* [PATCH] [next] ARM: Replace snprintf() with the safer scnprintf() variant
@ 2024-10-04 2:59 Paulo Miguel Almeida
2024-10-04 9:09 ` Russell King (Oracle)
0 siblings, 1 reply; 3+ messages in thread
From: Paulo Miguel Almeida @ 2024-10-04 2:59 UTC (permalink / raw)
To: linux, linux-arm-kernel
Cc: paulo.miguel.almeida.rodenas, linux-hardening, linux-kernel
There is a general misunderstanding amongst engineers that {v}snprintf()
returns the length of the data *actually* encoded into the destination
array. However, as per the C99 standard {v}snprintf() really returns
the length of the data that *would have been* written if there were
enough space for it. This misunderstanding has led to buffer-overruns
in the past. It's generally considered safer to use the {v}scnprintf()
variants in their place (or even sprintf() in simple cases).
Link: https://lwn.net/Articles/69419/
Link: https://github.com/KSPP/linux/issues/105
Signed-off-by: Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com>
---
arch/arm/kernel/process.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/arm/kernel/process.c b/arch/arm/kernel/process.c
index e16ed102960c..9d768a93fb1c 100644
--- a/arch/arm/kernel/process.c
+++ b/arch/arm/kernel/process.c
@@ -186,8 +186,8 @@ void __show_regs(struct pt_regs *regs)
unsigned int transbase;
asm("mrc p15, 0, %0, c2, c0\n\t"
: "=r" (transbase));
- snprintf(buf, sizeof(buf), " Table: %08x DAC: %08x",
- transbase, domain);
+ scnprintf(buf, sizeof(buf), " Table: %08x DAC: %08x",
+ transbase, domain);
}
#endif
asm("mrc p15, 0, %0, c1, c0\n" : "=r" (ctrl));
--
2.46.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] [next] ARM: Replace snprintf() with the safer scnprintf() variant
2024-10-04 2:59 [PATCH] [next] ARM: Replace snprintf() with the safer scnprintf() variant Paulo Miguel Almeida
@ 2024-10-04 9:09 ` Russell King (Oracle)
2024-10-04 18:46 ` Paulo Miguel Almeida
0 siblings, 1 reply; 3+ messages in thread
From: Russell King (Oracle) @ 2024-10-04 9:09 UTC (permalink / raw)
To: Paulo Miguel Almeida; +Cc: linux-arm-kernel, linux-hardening, linux-kernel
On Fri, Oct 04, 2024 at 03:59:30PM +1300, Paulo Miguel Almeida wrote:
> There is a general misunderstanding amongst engineers that {v}snprintf()
> returns the length of the data *actually* encoded into the destination
> array. However, as per the C99 standard {v}snprintf() really returns
> the length of the data that *would have been* written if there were
> enough space for it. This misunderstanding has led to buffer-overruns
> in the past. It's generally considered safer to use the {v}scnprintf()
> variants in their place (or even sprintf() in simple cases).
So, basically, it's unsafe to use the result of (v)snprintf(). So why
do we need to change locations that do not use the result?
This patch is mere noise. Sorry, I won't be applying it.
--
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] [next] ARM: Replace snprintf() with the safer scnprintf() variant
2024-10-04 9:09 ` Russell King (Oracle)
@ 2024-10-04 18:46 ` Paulo Miguel Almeida
0 siblings, 0 replies; 3+ messages in thread
From: Paulo Miguel Almeida @ 2024-10-04 18:46 UTC (permalink / raw)
To: Russell King (Oracle); +Cc: linux-arm-kernel, linux-hardening, linux-kernel
On Fri, Oct 04, 2024 at 10:09:50AM +0100, Russell King (Oracle) wrote:
> On Fri, Oct 04, 2024 at 03:59:30PM +1300, Paulo Miguel Almeida wrote:
> > There is a general misunderstanding amongst engineers that {v}snprintf()
> > returns the length of the data *actually* encoded into the destination
> > array. However, as per the C99 standard {v}snprintf() really returns
> > the length of the data that *would have been* written if there were
> > enough space for it. This misunderstanding has led to buffer-overruns
> > in the past. It's generally considered safer to use the {v}scnprintf()
> > variants in their place (or even sprintf() in simple cases).
>
> So, basically, it's unsafe to use the result of (v)snprintf(). So why
> do we need to change locations that do not use the result?
>
> This patch is mere noise. Sorry, I won't be applying it.
>
Thanks for taking the time to review this patch.
My take on this is that it boils down to nipping it in the bud proactively,
so if the result starts being used, no one has to remember to change from
scprint() to scnprint(), which can be easy to miss.
There have been other instances where the result wasn't being used, yet the
patch was still accepted [1] — should that help sway your opinion. :)
I understand that each maintainer has different approaches to what they deem
trivial patches, but I hope you reconsider accepting this one. That said,
if there is no utilization of snprint(), we could eventually deprecate or
remove the function altogether.
[1] https://lore.kernel.org/lkml/20231213164246.1021885-11-lee@kernel.org/
Paulo A.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-10-04 18:47 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-04 2:59 [PATCH] [next] ARM: Replace snprintf() with the safer scnprintf() variant Paulo Miguel Almeida
2024-10-04 9:09 ` Russell King (Oracle)
2024-10-04 18:46 ` Paulo Miguel Almeida
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).