Mailing List Archive

Revision 3591 t/029-charbuf failing with buffer overflow
Guten Morgen allerseits!

Revision 3591 is failing one of it's tests:

t/029-charbuf....................ok 1/34*** buffer overflow detected ***:
/usr/bin/perl terminated
======= Backtrace: =========
/lib/libc.so.6(__chk_fail+0x41)[0x78f3a1]
/lib/libc.so.6[0x78ebb8]
/lib/libc.so.6(_IO_default_xsputn+0xcd)[0x7101bd]
/lib/libc.so.6(_IO_vfprintf+0x912)[0x6e8fb2]
/lib/libc.so.6(__vsprintf_chk+0xad)[0x78ec6d]
/lib/libc.so.6(__sprintf_chk+0x30)[0x78eba0]
/home/henry/projects/ks/trunk/perl/blib/arch/auto/KinoSearch/KinoSearch.so(kino_TestCB_vcatf_tests+0x416)[0x2591a6]
/home/henry/projects/ks/trunk/perl/blib/arch/auto/KinoSearch/KinoSearch.so(XS_KinoSearch__Test__Util__TestCharBuf__vcatf_tests+0x94)[0x212914]
/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/libperl.so(Perl_pp_entersub+0x40d)[0x99647d]
/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/libperl.so(Perl_runops_standard+0x1f)[0x98f8df]
/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/libperl.so(perl_run+0x2ee)[0x9350de]
/usr/bin/perl(main+0x13e)[0x80491ee]
/lib/libc.so.6(__libc_start_main+0xdc)[0x6c2dec]
/usr/bin/perl[0x8049021]



_______________________________________________
KinoSearch mailing list
KinoSearch@rectangular.com
http://www.rectangular.com/mailman/listinfo/kinosearch
Re: Revision 3591 t/029-charbuf failing with buffer overflow [ In reply to ]
On Jul 8, 2008, at 6:16 PM, Henry wrote:

> Guten Morgen allerseits!

Hi, sorry about the delay in responding. There were problems at the
facility where the rectangular.com box is colocated, and after it went
down last Wednesday it took a while to get it back in service.

> Revision 3591 is failing one of it's tests:
>
> t/029-charbuf....................ok 1/34*** buffer overflow detected
> ***:

This overflow happened to occur in a sloppily coded test case rather
than the CB_VCatF method itself. The problem was that I added more
characters to the test case but forgot to change the buffer size where
the "wanted" string was being sprintf'd, and we wound up with a buf of
20 chars when we needed 21 to hold the full string including the
terminating NULL:

@@ -135,7 +147,7 @@
vcatf_f(VArray *tests)
{
CharBuf *wanted;
- char buf[20];
+ char buf[64];
float num = 1.3f;
CharBuf *got = get_cb("foo ");
sprintf(buf, "foo bar %f baz", num);


Curiously, Valgrind didn't report an error on my system, though in
theory it probably should have. In any case, r3592 should resolve the
issue.

Nevertheless, it's worth reviewing one aspect of CB_VCatF anyway.
CB_VCatF is meant to be a crude and slow but safe formatted string
concatenator -- a poor relation of vsnprintf or the perlapi function
sv_vcatpvfn. Its primary use is error messages. CB_VCatF does one
fancy thing that printf and friends don't: "%o" takes a Boilerplater
object and concatentates the output of the object's To_String()
method. However, at present CB_VCatF handles no formatting modifiers
at all: "%f" only, not "%0.2f" and such. That makes it relatively
easy to calculate how much space CB_VCatF needs in comparison to
something hard like a full implementation of snprintf. So, the
question is... Are there any systems on which a sprintf'd float with
no modifiers ever requires more than the buffer size used in
kino_CB_vcatf(), 64 bytes?

Since your system failed when mine didn't, try this C app and tell me
what you get:

#include <stdio.h>
#include <float.h>
int main () {
char buf[128];
unsigned len = sprintf(buf, "%f", FLT_MAX);
printf("%s\n", buf);
printf("Bytes: %u\n", len);
return 0;
}

This is what I see:

340282346638528859811704183484516925440.000000
Bytes: 46

For reference, here's a relevant chunk taken from Perl_sv_vcatpvfn, in
sv.c

/* Times 4: a decimal digit takes more than 3 binary digits.
* NV_DIG: mantissa takes than many decimal digits.
* Plus 32: Playing safe. */
char ebuf[IV_DIG * 4 + NV_DIG + 32];
/* large enough for "%#.#f" --chip */
/* what about long double NVs? --jhi */

We don't need quite as much as Perl does, though, because CB_VCatF
doesn't accept modifiers. No doubles / long doubles, either.

Marvin Humphrey
Rectangular Research
http://www.rectangular.com/


_______________________________________________
KinoSearch mailing list
KinoSearch@rectangular.com
http://www.rectangular.com/mailman/listinfo/kinosearch
Re: Revision 3591 t/029-charbuf failing with buffer overflow [ In reply to ]
On Mon, July 14, 2008 12:03 am, Marvin Humphrey wrote:
> On Jul 8, 2008, at 6:16 PM, Henry wrote:
>> Guten Morgen allerseits!
> Hi, sorry about the delay in responding. There were problems at the
> facility where the rectangular.com box is colocated, and after it went
> down last Wednesday it took a while to get it back in service.

No worries.

> Since your system failed when mine didn't, try this C app and tell me
> what you get:
> This is what I see:
>
> 340282346638528859811704183484516925440.000000
> Bytes: 46

I get the same (gcc version 4.1.2 20070626 (Red Hat 4.1.2-14)):

340282346638528859811704183484516925440.000000
Bytes: 46

Revision 3592 cleared that test failure, thanks!

Regards
Henry


_______________________________________________
KinoSearch mailing list
KinoSearch@rectangular.com
http://www.rectangular.com/mailman/listinfo/kinosearch