linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* snprintf, overlapping destination and source
@ 2015-12-05 20:38 Rasmus Villemoes
  2015-12-05 20:42 ` Julia Lawall
  2015-12-07 22:04 ` Kees Cook
  0 siblings, 2 replies; 25+ messages in thread
From: Rasmus Villemoes @ 2015-12-05 20:38 UTC (permalink / raw)
  To: linux-kernel; +Cc: Kees Cook, Andrew Morton, Julia Lawall

I did a search for code doing

  s[n]printf(buf, "...", ..., buf, ...)

and found a few instances. They all do it with the format string
beginning with "%s" and buf being passed as the corresponding parameter
(obviously to append to the existing string). That works (AFAICT), both
with the current printf implementation and with the string()
modification which is now in -mm. It would obviously go horribly wrong
if anything, even non-specifiers, precede the "%s" in the format
string.

The question is, do we want to officially support this particular case of
overlapping src and dst? Or should we close our eyes and hope it will
continue to work [1] and that it won't cause a caffeine-deprived hacker
to accidentally think one could also prepend to a buffer by doing
sprintf(buf, "...%s", ..., buf)? I'm actually surprised gcc doesn't warn
about this.

[1] Not that I can immediately think of a sane way to implement snprintf
where it won't work, but you never know...

My coccinelle-fu isn't sufficient to find cases where one of the buf
instances is a more complicated expressions involving buf as a
subexpression, as in

  s[n]printf(buf, "...", ..., buf + 4, ...)

or

  s[n]printf(&buf[len], "...", ..., buf, ...)

which would presumably always be wrong. Julia?

Rasmus

The cases I've found are

./drivers/gpu/drm/amd/amdkfd/kfd_topology.c:613:53-54: s[n]printf, overlapping source and destination buffers
./drivers/gpu/drm/amd/amdkfd/kfd_topology.c:618:16-17: s[n]printf, overlapping source and destination buffers
./drivers/gpu/drm/amd/amdkfd/kfd_topology.c:488:58-59: s[n]printf, overlapping source and destination buffers
./drivers/input/joystick/analog.c:445:59-60: s[n]printf, overlapping source and destination buffers
./drivers/leds/led-class-flash.c:215:32-33: s[n]printf, overlapping source and destination buffers
./drivers/media/pci/zoran/videocodec.c:120:39-40: s[n]printf, overlapping source and destination buffers
./drivers/media/rc/ati_remote.c:875:47-48: s[n]printf, overlapping source and destination buffers
./drivers/net/wireless/ti/wlcore/boot.c:125:24-25: s[n]printf, overlapping source and destination buffers
./drivers/net/wireless/ti/wlcore/boot.c:128:37-38: s[n]printf, overlapping source and destination buffers
./drivers/usb/atm/usbatm.c:1341:46-47: s[n]printf, overlapping source and destination buffers

^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2016-03-14 19:18 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-05 20:38 snprintf, overlapping destination and source Rasmus Villemoes
2015-12-05 20:42 ` Julia Lawall
2015-12-07 22:04 ` Kees Cook
2015-12-16 13:14   ` Rasmus Villemoes
2016-03-08 20:40   ` [RFC 0/7] eliminate snprintf with overlapping src and dst Rasmus Villemoes
2016-03-08 20:40     ` [RFC 1/7] drm/amdkfd: avoid fragile and inefficient snprintf use Rasmus Villemoes
2016-03-14 14:33       ` Oded Gabbay
2016-03-14 19:18         ` Rasmus Villemoes
2016-03-08 20:40     ` [RFC 2/7] Input: joystick - avoid fragile " Rasmus Villemoes
2016-03-09  6:49       ` Andy Shevchenko
2016-03-08 20:40     ` [RFC 3/7] leds: avoid fragile sprintf use Rasmus Villemoes
2016-03-08 20:40     ` [RFC 4/7] drivers/media/pci/zoran: avoid fragile snprintf use Rasmus Villemoes
2016-03-08 20:40     ` [RFC 5/7] wlcore: " Rasmus Villemoes
2016-03-09 11:49       ` Kalle Valo
2016-03-08 20:40     ` [RFC 6/7] [media] ati_remote: " Rasmus Villemoes
2016-03-08 20:40     ` [RFC 7/7] USB: usbatm: avoid fragile and inefficient " Rasmus Villemoes
2016-03-08 21:01       ` Joe Perches
2016-03-10 23:56         ` Greg Kroah-Hartman
2016-03-09 13:08       ` Sergei Shtylyov
2016-03-08 23:07     ` [RFC 0/7] eliminate snprintf with overlapping src and dst Kees Cook
2016-03-08 23:13     ` Kees Cook
2016-03-09  6:51     ` Andy Shevchenko
2016-03-09 20:49     ` Andrew Morton
2016-03-09 22:19       ` Rasmus Villemoes
2016-03-10 13:59       ` One Thousand Gnomes

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).