Subject: | Bug returning TCP data via Net::LibNIDS::tcp_stream::data() |
Date: | Thu, 17 Dec 2009 16:04:26 -0800 |
To: | "bug-Net-LibNIDS [...] rt.cpan.org" <bug-Net-LibNIDS [...] rt.cpan.org> |
From: | David Giller <dave [...] pdx.net> |
LibNIDS.xs has a bug when returning the data from a packet to the TCP callback sub. See the libnids API.txt or API.html section 5, which states:
"If nids_discard function is never called (like in above sample program), buffer hlf->data contains exactly hlf->count_new bytes."
Net::LibNIDS never calls nids_discard, and uses the default behavior, so the code in LibNIDS.xs is wrong here:
*** Net-LibNIDS-0.01.orig/LibNIDS.xs 2004-06-27 04:45:00.000000000 -0700
--- Net-LibNIDS-0.01.drg/LibNIDS.xs 2009-12-17 15:33:05.000000000 -0800
***************
*** 252,258 ****
data(obj)
SV* obj
CODE:
! RETVAL = newSVpv( obj2halfstream(obj)->data , obj2halfstream(obj)->count);
OUTPUT:
RETVAL
--- 253,259 ----
data(obj)
SV* obj
CODE:
! RETVAL = newSVpv( obj2halfstream(obj)->data , obj2halfstream(obj)->count_new);
OUTPUT:
RETVAL
The hlf->count value is the total number of bytes received in the stream, not the number of bytes in the buffer.
Alternately, and probably more correctly in general, the API document suggests that this would be the way to do it that would be correct even if Net::LibNIDS eventually exposes access to the nids_discard() function:
RETVAL = newSVpv( obj2halfstream(obj)->data , obj2halfstream(obj)->count - obj2halfstream(obj)->offset );
This is using libnids 1.20. This is using my own much simpler patch to solve the compilation problem that 'edeca' fixed with his patches for bug #34545, so these line numbers are relative to the unmodified Net::LibNIDS sources, but the code should not be at all hard to find.
Without this fix, LibNIDS.xs accesses uninitialized data and any TCP stream with more than one packet will be corrupted when decoded using $stream->client->data() or $stream->server->data().
Development is on Debian etch, with libnids and libnet from Debian repositories.
David Giller