bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bash sockets: printf \x0a does TCP fragmentation


From: Bob Proulx
Subject: Re: bash sockets: printf \x0a does TCP fragmentation
Date: Fri, 21 Sep 2018 23:30:25 -0600
User-agent: Mutt/1.10.1 (2018-07-13)

dirk+bash@testssl.sh wrote:
> we discovered a strange phenomenon in the project testssl.sh:

You are doing something that is quite unusual.  You are using a shell
script direction on a TCP socket.  That isn't very common.  More
typically one would use a C program instead.  So it isn't surprising
that you are finding interactions that are not well known.

> printf -- "$data" >&5 2>/dev/null

Why is stderr discarded?  That is almost always bad because it
discards any errors that might occur.  You probably shouldn't do this.

What happens if $data contains % format strings?  What happens if the
format contains a sequence such as \c?  This looks problematic.  This
is not a safe programming proctice.

> does not do what it is intended.

"Intent" is in the eye of the beholder.

> "$data" is  a ClientHello like
> 
> '\x16\x03\x01\x2\x00\x01\x00\x1\xfc\x03\x03\x54\x51\x1e\x7a\xde\xad\xbe\xef\x31\x33\x07\x00\x00\x00\x00\x00\xcf\xbd\x39\x04\xcc\x16\x0a\...'
> 
> Each \x0a like the last one causes a new TCP fragment to begin which can be 
> easily
> spotted when using wireshark while running e.g.

As Chet said the libc stdio library is probably doing line oriented
buffering.  The newline is causing a flush at that time.

> One would assume that a bash socket connection cannot influence the TCP
> fragmentation but obviously it does.

One would be in error to assume this.

> If there's a workaround, please let me know. (tried to add "%b" with no
> effect). Otherwise I believe it's a bug.

You can re-block the output stream using other tools such as 'cat' or
'dd'.  Since you are concerned about block size then perhaps dd is the
better of the two.

  | cat

Or probably better:

  | dd status=none bs=1M

Or use whatever block size you wish.  The 'dd' program will read the
input into its buffer and then output that block of data all in one
write(2).  That seems to be what you are wanting.

Good luck! :-)

Bob

P.S. You can possibly use the 'stdbuf' command to control the output
buffering depending upon the program.

  info stdbuf



reply via email to

[Prev in Thread] Current Thread [Next in Thread]