9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] snprintf buffer overrun
@ 2016-11-27 14:58 Porlock
  2016-11-27 20:20 ` cinap_lenrek
  2016-11-27 20:31 ` cinap_lenrek
  0 siblings, 2 replies; 3+ messages in thread
From: Porlock @ 2016-11-27 14:58 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 541 bytes --]

Plan 9's implementation of the standard C functions snprintf and
vsnprintf have a buffer overrun bug.

If the buffer length equals the output length (without the terminating
null), then one too many characters is written to the buffer.

For example,
              snprintf(buf, 4, "ABCD");

will write 5 characters to buf.

Attached is a short program to illustrate this, which gives the
following output :-

% 8c printftest.c && 8l printftest.8
% ./8.out
  A   B   C   D   \0  *   *   *   *   *   *   *   *   *   *   *



[-- Attachment #2: printftest.c --]
[-- Type: text/x-csrc, Size: 341 bytes --]

#include <u.h>
#include <libc.h>
#include <stdio.h>

void main()
{
    char buf[16];
    int i;
    memset(buf, '*', sizeof(buf));
    snprintf(buf, 4, "ABCD");
    for (i = 0; i < sizeof(buf); ++i) {
        if (buf[i])
            print("  %c ", buf[i]);
        else
            print("  \\0");
    }
    print("\n");
}

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

end of thread, other threads:[~2016-11-27 20:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-27 14:58 [9fans] snprintf buffer overrun Porlock
2016-11-27 20:20 ` cinap_lenrek
2016-11-27 20:31 ` cinap_lenrek

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