Package: netcat-openbsd
Version: 1.105
Severity: important
Tags: patch
When using netcat-openbsd -u (UDP) option on a closed port, netcat hangs
using 100% CPU. This bug has been reported a few years ago (#594614) and
hasn't been fixed like Aron thought believed.
The problem is that when the UDP port is closed the poll returns a
POLLERR event but netcat ignores it and gets into a busy wait loop. To
reproduce you just need to run:
$ nc -u localhost 12345
type something here
^C
And check that `nc' is on top of `top' (heh). You can see the POLLERR
events with `strace'. The patch couldn't be simpler and follows below.
diff --git a/netcat.c b/netcat.c
index c938d11..d7d0c4a 100644
--- a/netcat.c
+++ b/netcat.c
@@ -1055,7 +1055,7 @@ readwrite(int nfd)
return;
}
}
- else if (pfd[0].revents & POLLHUP) {
+ else if (pfd[0].revents & POLLHUP || pfd[0].revents & POLLERR) {
shutdown_rd:
shutdown(nfd, SHUT_RD);
pfd[0].fd = -1;
-- System Information:
Debian Release: 6.0.9
APT prefers oldstable-updates
APT policy: (500, 'oldstable-updates'), (500, 'oldstable'), (500, 'stable')
Architecture: amd64 (x86_64)
Kernel: Linux 3.13.0-29-generic (SMP w/4 CPU cores)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
diff --git a/netcat.c b/netcat.c
index c938d11..d7d0c4a 100644
--- a/netcat.c
+++ b/netcat.c
@@ -1055,7 +1055,7 @@ readwrite(int nfd)
return;
}
}
- else if (pfd[0].revents & POLLHUP) {
+ else if (pfd[0].revents & POLLHUP || pfd[0].revents & POLLERR) {
shutdown_rd:
shutdown(nfd, SHUT_RD);
pfd[0].fd = -1;