Ignore SIGPIPE signals when writing to GSS socket.
authorJennifer Richards <jennifer@painless-security.com>
Tue, 17 May 2016 17:30:58 +0000 (13:30 -0400)
committerJennifer Richards <jennifer@painless-security.com>
Tue, 17 May 2016 17:34:44 +0000 (13:34 -0400)
If the remote end of a GSS connection disconnects, attempts to write
to the now-broken pipe result in a SIGPIPE signal. This unceremoniously
exits. Instead, ignore those signals during the write() and let the
error handling code deal gracefully with the broken pipe.

gsscon/gsscon_common.c

index fe559de..c391698 100755 (executable)
@@ -52,6 +52,8 @@
  * or implied warranty.
  */
 
+#include <signal.h>
+
 #include <gsscon.h>
 
 /* --------------------------------------------------------------------------- */
@@ -131,7 +133,14 @@ static int WriteBuffer (int         inSocket,
     if (!err) {
         const char *ptr = inBuffer;
         do {
-            ssize_t count = write (inSocket, ptr, inBufferLength - bytesWritten);
+            ssize_t count;
+
+            /* disable the SIGPIPE signal while we write so that we can handle a
+             * broken pipe error gracefully */
+            signal(SIGPIPE, SIG_IGN); /* temporarily disable */
+            count = write (inSocket, ptr, inBufferLength - bytesWritten);
+            signal(SIGPIPE, SIG_DFL); /* reenable */
+
             if (count < 0) {
                 /* Try again on EINTR */
                 if (errno != EINTR) { err = errno; }
@@ -142,7 +151,7 @@ static int WriteBuffer (int         inSocket,
         } while (!err && (bytesWritten < inBufferLength));
     } 
     
-    if (err) { gsscon_print_error (err, "WritBuffer failed"); }
+    if (err) { gsscon_print_error (err, "WriteBuffer failed"); }
 
     return err;
 }