Write-Only Socket

M

Martin Kissner

Hello together,

I am trying to write a little tcp-server which creates a write only
socket an test it with a little client.
The code I have tried so far is below and it workes as expected in the
form I posted it.

The question is:
Why do I have to shutdown(0) $session _and_ $socket on the server?
I had expected that the client would not be able to write to his $socket
if only $session on the server was shutdown(0)

--- server code ---
#!/usr/bin/perl

use warnings;
use strict;

use IO::Socket;

my $socket = IO::Socket::INET->new(
Listen => 1,
LocalPort => 1025,
Proto => 'tcp',
Reuse => 1);

my $session = $socket->accept;

$socket->shutdown(0) or die; ### Question is here !
$session->shutdown(0) or die; ### Question is here !

while(1) {
print $session "I don't read!\n";
my $read = $session->getline or die "I can not read\n: $!\n";
print $read;
sleep 1;
}
--- --- ---

--- client code ---
#!/usr/bin/perl

use warnings;
use strict;

use IO::Socket;

my $socket = IO::Socket::INET->new(
PeerHost => 'localhost',
PeerPort => 1025,
Proto => 'tcp') or die "Keine Verbindung\n";

my $line;
while (my $line = $socket->getline) {
print $line;
print $socket "I try to write\n" or die "I can not write: $!\n";
}
--- --- ---
 
A

A. Sinan Unur

Hello together,

I am trying to write a little tcp-server which creates a write only
socket an test it with a little client.

I am very puzzled.

....
The question is:
Why do I have to shutdown(0) $session _and_ $socket on the server?

I must be missing something very obvious. The way I see it, a socket is
write only if you never read from it. There must be a reason you are doing
this.

Sinan
 
M

Martin Kissner

A. Sinan Unur wrote :
I am very puzzled.

...


I must be missing something very obvious. The way I see it, a socket is
write only if you never read from it. There must be a reason you are doing
this.

The reason is that I wanted to know if it can be done.

[OT]
The reason why I want to know is that I have noticed that the syslogd on
my computer (system: Mac OS X) opens an upd port whereas 'man syslogd'
says:

| Syslogd opens an Internet domain socket as specified in /etc/services.
| Normally syslogd will only use this socket to send messages
| outwards, but in `insecure'' mode it will also read messages from this
| socket.

I see no reason for opening this socket as long as there is no central
syslog server on the network, but the socket gets opened in any case so
on first sight this seems to me to be an unnecessary security hole.

If the socket is r/w the service might be potentially exploitable on the
application layer although the server is not supposed to read from the
socket.
If the socket is write only, I assume that data sent to the opened port
will not be passed to the server process but blocked on the transport
layer by the OS.
(It would be my next question whether this is true, but I am pretty sure
it is.)
[/OT]

After I had executes my little experiment, I found the behaviour which I
did't understand.
That was my reason for asking (after I had consulted the documentation
of course).

Best Regards
Martin
 
X

xhoster

Martin Kissner said:
Hello together,

I am trying to write a little tcp-server which creates a write only
socket an test it with a little client.
The code I have tried so far is below and it workes as expected in the
form I posted it.

The question is:
Why do I have to shutdown(0) $session _and_ $socket on the server?

As far as I can tell, you don't. I get the same behavior whether $socket
is shutdown or not.
I had expected that the client would not be able to write to his $socket
if only $session on the server was shutdown(0)

I don't see that happening regardless of what you shutdown. It looks to me
like the client is prohibited from writing to the socket only because
server exits. shutdown(0) does not seem to be enforced on the other
end of the socket.

Perl 5.8.0 on Linux.

Xho
 
B

Big and Blue

Martin said:
The reason is that I wanted to know if it can be done.

[OT]
The reason why I want to know is that I have noticed that the syslogd on
my computer (system: Mac OS X) opens an upd port whereas 'man syslogd'
says:

| Syslogd opens an Internet domain socket as specified in /etc/services.
| Normally syslogd will only use this socket to send messages
| outwards, but in `insecure'' mode it will also read messages from this
| socket.

I see no inconsistency in this at all. In insecure mode it will read
messages. So in secure mode it won't. That doesn't mean the socket is
open write-only, it just means that the daemon never attempts to read
anything from the socket (and being UDP they'll get discarded if an input
queue forms as a result).
If the socket is r/w the service might be potentially exploitable on the
application layer although the server is not supposed to read from the
socket.

If it isn't going to read() (or readfrom()) then I'm not sure how you
think you (or anyone else) could persuade it to do so.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top