9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Porlock <porlock@mailinator.com>
To: 9fans@9fans.net
Subject: [9fans] snprintf buffer overrun
Date: Sun, 27 Nov 2016 14:58:02 +0000	[thread overview]
Message-ID: <583AF47A.4010400@mailinator.com> (raw)

[-- 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");
}

             reply	other threads:[~2016-11-27 14:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-27 14:58 Porlock [this message]
2016-11-27 20:20 ` cinap_lenrek
2016-11-27 20:31 ` cinap_lenrek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=583AF47A.4010400@mailinator.com \
    --to=porlock@mailinator.com \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).