🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #79116

Re: Strange problem with nnrpd 2.4.4 and XOVER over SSL connection

#79116
From: Chris Caputo
Date: Tue, 10 Jun 2008 21:19
155 lines
6179 bytes
Tim Fardell <tim.fardell.all-your-clothes@virgin.net> writes:
>On Tue, 10 Jun 2008, Russ Allbery wrote:
>> Tim Fardell <tim.fardell.all-your-clothes@virgin.net> writes:
>> > With one specific group only, and no others, when the client requests
>> > XOVER for the full range of articles, instead of sending the overview
>> > data back to the client, there is a short pause (about 5 seconds) and
>> > nnrpd dies, closing the connection to the client.
>> 
>> This is fixed in STABLE.  The problem was that nnrpd was using alloca to
>> allocate memory for the SSL encryption buffer, which exploded if the data
>> to be encrypted was larger than the stack size limit.  It wasn't a good
>> place to use alloca and the current STABLE version switches to allocating
>> regular heap memory for this purpose.
>
>Thanks Russ. 
>
>Is there any way to work around this with 2.4.4? 
>
>I don't like the idea of running a development snapshot on a live server. 

The fix for this is attached below.

>Another, unrelated problem - I notice that nnrpd isn't always dieing when 
>the user disconnects, resulting in hundreds of 'orphan' nnrpd processes 
>hanging around. I had to do a 'killall nnrpd' today because users started 
>reporting that they couldn't log in to the server. Is this also fixed? 

If a user does not gracefully disconnect, nnrpd won't notice until it 
times out or tries to send data.  Activating keepalive packets on your TCP 
streams can help also detect a missing endpoint.  In my nnrpd.c main() 
function I add the following after the SO_REUSEADDR setsockopt().

---
        if (setsockopt(lfd, SOL_SOCKET, SO_KEEPALIVE,
                       (char *)&one, sizeof(one)) < 0) {
            syslog(L_FATAL, "can't setsockopt(SO_KEEPALIVE) (%m)");
            exit(1);
        }
---

Not sure of your OS, but under Linux I also add the following to 
sysctl.conf to speed up the detection of clients which have disappeared:

---
# KEEPALIVE tweaks - detect dead peers within 87 seconds...
# default: net.ipv4.tcp_keepalive_intvl = 75
net.ipv4.tcp_keepalive_intvl = 3
# default: net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_probes = 9
# default: net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_time = 60
# On a LAN '7' timesout in 57 seconds with SIGPIPE.  '15' was 16 minutes!
# default: net.ipv4.tcp_retries2 = 15
net.ipv4.tcp_retries2 = 7
---

Chris

---

Date: Wed, 21 May 2008 15:11:52 +0000 (UTC)
From: Chris Caputo <ccaputo@alt.net>
To: inn-workers@isc.org
Subject: PATCH: nnrpd/tls.c - fix stack overflow

Please consider the following patch for nnrpd/tls.c.  It should apply to 
inn-2.4.2 through 2.4.4.

When an article of a size greater than remaining stack are retrieved via 
SSL, a segmentation fault will occur due to the use of alloca().

The below patch uses heap based realloc() instead of stack based alloca(), 
with a static buffer growing as needed.

I used realloc() instead of malloc() for performance reasons since this 
function is called frequently.  The caveat is that the memory is never 
free()'ed, so if more correct code is desired you may want to adjust this.

Thanks,
Chris

diff -upr nnrpd-stock/tls.c nnrpd/tls.c
--- nnrpd-stock/tls.c	2004-12-22 04:21:19.000000000 +0000
+++ nnrpd/tls.c	2008-05-21 14:55:52.000000000 +0000
@@ -32,37 +32,6 @@
 #include <sys/stat.h>
 #include <sys/uio.h>
 
-/* taken from lib/parsedate.c */
-#ifndef WRITEV_USE_ALLOCA
-#ifdef alloca
-#define WRITEV_USE_ALLOCA
-#else /* alloca not defined */
-#ifdef __GNUC__
-#define WRITEV_USE_ALLOCA
-#define alloca __builtin_alloca
-#else /* not GNU C.  */
-#if (!defined (__STDC__) && defined (sparc)) || defined (__sparc__) || defined (__sparc) || defined (__sgi) || (defined (__sun) && defined (__i386))
-#define WRITEV_USE_ALLOCA
-#include <alloca.h>
-#else /* not sparc */
-#if (defined (_MSDOS) || defined (_MSDOS_)) && !defined (__TURBOC__)
-#else /* not MSDOS, or __TURBOC__ */
-#if defined(_AIX)
- #pragma alloca
-#define WRITEV_USE_ALLOCA
-#endif /* not _AIX */
-#endif /* not MSDOS, or __TURBOC__ */
-#endif /* not sparc */
-#endif /* not GNU C */
-#endif /* alloca not defined */
-#endif /* WRITEV_USE_ALLOCA not defined */
-#ifdef WRITEV_USE_ALLOCA
-#define WRITEV_ALLOC alloca
-#else
-#define WRITEV_ALLOC malloc
-#endif
-
-
 #endif
 
 /* outside the ifdef so `make depend` works even ifndef HAVE_SSL */
@@ -698,7 +667,8 @@ SSL_writev (ssl, vector, count)
      const struct iovec *vector;
      int count;
 {
-  char *buffer;
+  static char *buffer = NULL;
+  static size_t allocsize = 0;
   char *bp;
   size_t bytes, to_copy;
   int i;
@@ -706,8 +676,20 @@ SSL_writev (ssl, vector, count)
   bytes = 0;
   for (i = 0; i < count; ++i)
     bytes += vector[i].iov_len;
-  /* Allocate a temporary buffer to hold the data.  */
-  buffer = (char *) WRITEV_ALLOC (bytes);
+  /* Allocate a buffer to hold the data.  */
+  if (NULL == buffer)
+    {
+      buffer = (char *) xmalloc(bytes);
+      allocsize = bytes;
+    }
+  else
+    {
+      if (bytes > allocsize)
+        {
+          buffer = (char *) xrealloc (buffer, bytes);
+          allocsize = bytes;
+	}
+    }
   /* Copy the data into BUFFER.  */
   to_copy = bytes;
   bp = buffer;
Technical Details
Message-ID: <1ujglg.p6.19.1@news.alt.net>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.20141118.mbox.gz!number1.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news.alt.net!ccaputo
References: <Pine.LNX.4.44.0806101536250.5497-100000@menace.crumblenet.co.uk> <87abhte0r2.fsf@windlord.stanford.edu> <Pine.LNX.4.44.0806101813350.8321-100000@menace.crumblenet.co.uk>

Show Full Headers
Complete Article Headers
Message-ID: <1ujglg.p6.19.1@news.alt.net>
Subject: Re: Strange problem with nnrpd 2.4.4 and XOVER over SSL connection
Date: Tue, 10 Jun 2008 21:19:10 +0000
From: Chris Caputo <ccaputo@alt.net>
References: <Pine.LNX.4.44.0806101536250.5497-100000@menace.crumblenet.co.uk> <87abhte0r2.fsf@windlord.stanford.edu> <Pine.LNX.4.44.0806101813350.8321-100000@menace.crumblenet.co.uk>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.20141118.mbox.gz!number1.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news.alt.net!ccaputo
Newsgroups: news.software.nntp
Organization: Altopia Corp. - Usenet Access - www.altopia.com
Lines: 155
User-Agent: nn/6.7.3