Blame SOURCES/rsyslog-8.2102.0-rhbz2046158-gnutls-broken-connection.patch

49a8b5
diff -up rsyslog-8.2102.0/runtime/nsd_gtls.c.orig rsyslog-8.2102.0/runtime/nsd_gtls.c
49a8b5
--- rsyslog-8.2102.0/runtime/nsd_gtls.c.orig	2022-04-11 09:26:17.826271989 +0200
49a8b5
+++ rsyslog-8.2102.0/runtime/nsd_gtls.c	2022-04-11 09:33:28.702012052 +0200
49a8b5
@@ -556,7 +556,9 @@ gtlsRecordRecv(nsd_gtls_t *pThis)
49a8b5
 	DEFiRet;
49a8b5
 
49a8b5
 	ISOBJ_TYPE_assert(pThis, nsd_gtls);
49a8b5
-	DBGPRINTF("gtlsRecordRecv: start\n");
49a8b5
+	DBGPRINTF("gtlsRecordRecv: start (Pending Data: %zd | Wanted Direction: %s)\n",
49a8b5
+		gnutls_record_check_pending(pThis->sess),
49a8b5
+		(gnutls_record_get_direction(pThis->sess) == gtlsDir_READ ? "READ" : "WRITE") );
49a8b5
 
49a8b5
 	lenRcvd = gnutls_record_recv(pThis->sess, pThis->pszRcvBuf, NSD_GTLS_MAX_RCVBUF);
49a8b5
 	if(lenRcvd >= 0) {
49a8b5
@@ -581,14 +583,30 @@ gtlsRecordRecv(nsd_gtls_t *pThis)
49a8b5
 					(NSD_GTLS_MAX_RCVBUF+lenRcvd));
49a8b5
 				pThis->lenRcvBuf = NSD_GTLS_MAX_RCVBUF+lenRcvd;
49a8b5
 			} else {
49a8b5
-				goto sslerr;
49a8b5
+				if (lenRcvd == GNUTLS_E_AGAIN || lenRcvd == GNUTLS_E_INTERRUPTED) {
49a8b5
+					goto sslerragain;	/* Go to ERR AGAIN handling */
49a8b5
+				} else {
49a8b5
+					/* Do all other error handling */
49a8b5
+					int gnuRet = lenRcvd;
49a8b5
+					ABORTgnutls;
49a8b5
+				}
49a8b5
 			}
49a8b5
 		}
49a8b5
 	} else if(lenRcvd == GNUTLS_E_AGAIN || lenRcvd == GNUTLS_E_INTERRUPTED) {
49a8b5
-sslerr:
49a8b5
-		pThis->rtryCall = gtlsRtry_recv;
49a8b5
-		dbgprintf("GnuTLS receive requires a retry (this most probably is OK and no error condition)\n");
49a8b5
-		ABORT_FINALIZE(RS_RET_RETRY);
49a8b5
+sslerragain:
49a8b5
+		/* Check if the underlaying file descriptor needs to read or write data!*/
49a8b5
+		if (gnutls_record_get_direction(pThis->sess) == gtlsDir_READ) {
49a8b5
+			pThis->rtryCall = gtlsRtry_recv;
49a8b5
+			dbgprintf("GnuTLS receive requires a retry, this most probably is OK and no error condition\n");
49a8b5
+			ABORT_FINALIZE(RS_RET_RETRY);
49a8b5
+		} else {
49a8b5
+			uchar *pErr = gtlsStrerror(lenRcvd);
49a8b5
+			LogError(0, RS_RET_GNUTLS_ERR, "GnuTLS receive error %zd has wrong read direction(wants write) "
49a8b5
+				"- this could be caused by a broken connection. GnuTLS reports: %s\n",
49a8b5
+				lenRcvd, pErr);
49a8b5
+			free(pErr);
49a8b5
+			ABORT_FINALIZE(RS_RET_GNUTLS_ERR);
49a8b5
+		}
49a8b5
 	} else {
49a8b5
 		int gnuRet = lenRcvd;
49a8b5
 		ABORTgnutls;
49a8b5
@@ -1978,6 +1996,7 @@ static rsRetVal
49a8b5
 Send(nsd_t *pNsd, uchar *pBuf, ssize_t *pLenBuf)
49a8b5
 {
49a8b5
 	int iSent;
49a8b5
+	int wantsWriteData = 0;
49a8b5
 	nsd_gtls_t *pThis = (nsd_gtls_t*) pNsd;
49a8b5
 	DEFiRet;
49a8b5
 	ISOBJ_TYPE_assert(pThis, nsd_gtls);
49a8b5
@@ -1998,10 +2017,12 @@ Send(nsd_t *pNsd, uchar *pBuf, ssize_t *
49a8b5
 			break;
49a8b5
 		}
49a8b5
 		if(iSent != GNUTLS_E_INTERRUPTED && iSent != GNUTLS_E_AGAIN) {
49a8b5
+			/* Check if the underlaying file descriptor needs to read or write data!*/
49a8b5
+			wantsWriteData = gnutls_record_get_direction(pThis->sess);
49a8b5
 			uchar *pErr = gtlsStrerror(iSent);
49a8b5
-			LogError(0, RS_RET_GNUTLS_ERR, "unexpected GnuTLS error %d - this "
49a8b5
-				"could be caused by a broken connection. GnuTLS reports: %s \n",
49a8b5
-				iSent, pErr);
49a8b5
+			LogError(0, RS_RET_GNUTLS_ERR, "unexpected GnuTLS error %d, wantsWriteData=%d - this "
49a8b5
+				"could be caused by a broken connection. GnuTLS reports: %s\n",
49a8b5
+				iSent, wantsWriteData, pErr);
49a8b5
 			free(pErr);
49a8b5
 			gnutls_perror(iSent);
49a8b5
 			ABORT_FINALIZE(RS_RET_GNUTLS_ERR);
49a8b5
diff -up rsyslog-8.2102.0/runtime/nsd_gtls.h.orig rsyslog-8.2102.0/runtime/nsd_gtls.h
49a8b5
--- rsyslog-8.2102.0/runtime/nsd_gtls.h.orig	2022-04-11 09:26:32.744262781 +0200
49a8b5
+++ rsyslog-8.2102.0/runtime/nsd_gtls.h	2022-04-11 09:34:29.909982895 +0200
49a8b5
@@ -33,6 +33,11 @@ typedef enum {
49a8b5
 	gtlsRtry_recv = 2
49a8b5
 } gtlsRtryCall_t;		/**< IDs of calls that needs to be retried */
49a8b5
 
49a8b5
+typedef enum {
49a8b5
+	gtlsDir_READ = 0,	/**< GNUTLS wants READ */
49a8b5
+	gtlsDir_WRITE = 1	/**< GNUTLS wants WRITE */
49a8b5
+} gtlsDirection_t;
49a8b5
+
49a8b5
 typedef nsd_if_t nsd_gtls_if_t; /* we just *implement* this interface */
49a8b5
 
49a8b5
 /* the nsd_gtls object */
49a8b5
diff -up rsyslog-8.2102.0/runtime/nsdsel_gtls.c.orig rsyslog-8.2102.0/runtime/nsdsel_gtls.c
49a8b5
--- rsyslog-8.2102.0/runtime/nsdsel_gtls.c.orig	2022-04-11 09:26:42.529256742 +0200
49a8b5
+++ rsyslog-8.2102.0/runtime/nsdsel_gtls.c	2022-04-11 09:38:27.425869737 +0200
49a8b5
@@ -81,6 +81,7 @@ Add(nsdsel_t *pNsdsel, nsd_t *pNsd, nsds
49a8b5
 
49a8b5
 	ISOBJ_TYPE_assert(pThis, nsdsel_gtls);
49a8b5
 	ISOBJ_TYPE_assert(pNsdGTLS, nsd_gtls);
49a8b5
+	DBGPRINTF("Add on nsd %p:\n", pNsdGTLS);
49a8b5
 	if(pNsdGTLS->iMode == 1) {
49a8b5
 		if(waitOp == NSDSEL_RD && gtlsHasRcvInBuffer(pNsdGTLS)) {
49a8b5
 			++pThis->iBufferRcvReady;
49a8b5
@@ -99,6 +100,8 @@ Add(nsdsel_t *pNsdsel, nsd_t *pNsd, nsds
49a8b5
 		}
49a8b5
 	}
49a8b5
 
49a8b5
+	dbgprintf("nsdsel_gtls: reached end on nsd %p, calling nsdsel_ptcp.Add with waitOp %d... \n", pNsdGTLS, waitOp);
49a8b5
+
49a8b5
 	/* if we reach this point, we need no special handling */
49a8b5
 	CHKiRet(nsdsel_ptcp.Add(pThis->pTcp, pNsdGTLS->pTcp, waitOp));
49a8b5
 
49a8b5
@@ -120,7 +123,8 @@ Select(nsdsel_t *pNsdsel, int *piNumRead
49a8b5
 	if(pThis->iBufferRcvReady > 0) {
49a8b5
 		/* we still have data ready! */
49a8b5
 		*piNumReady = pThis->iBufferRcvReady;
49a8b5
-		dbgprintf("nsdsel_gtls: doing dummy select, data present\n");
49a8b5
+		dbgprintf("nsdsel_gtls: doing dummy select for %p->iBufferRcvReady=%d, data present\n",
49a8b5
+			pThis, pThis->iBufferRcvReady);
49a8b5
 	} else {
49a8b5
 		iRet = nsdsel_ptcp.Select(pThis->pTcp, piNumReady);
49a8b5
 	}
49a8b5
@@ -138,7 +142,7 @@ doRetry(nsd_gtls_t *pNsd)
49a8b5
 	DEFiRet;
49a8b5
 	int gnuRet;
49a8b5
 
49a8b5
-	dbgprintf("GnuTLS requested retry of %d operation - executing\n", pNsd->rtryCall);
49a8b5
+	dbgprintf("doRetry: GnuTLS requested retry of %d operation - executing\n", pNsd->rtryCall);
49a8b5
 
49a8b5
 	/* We follow a common scheme here: first, we do the systen call and
49a8b5
 	 * then we check the result. So far, the result is checked after the
49a8b5
@@ -151,7 +155,7 @@ doRetry(nsd_gtls_t *pNsd)
49a8b5
 		case gtlsRtry_handshake:
49a8b5
 			gnuRet = gnutls_handshake(pNsd->sess);
49a8b5
 			if(gnuRet == GNUTLS_E_AGAIN || gnuRet == GNUTLS_E_INTERRUPTED) {
49a8b5
-				dbgprintf("GnuTLS handshake retry did not finish - "
49a8b5
+				dbgprintf("doRetry: GnuTLS handshake retry did not finish - "
49a8b5
 					"setting to retry (this is OK and can happen)\n");
49a8b5
 				FINALIZE;
49a8b5
 			} else if(gnuRet == 0) {
49a8b5
@@ -167,9 +171,20 @@ doRetry(nsd_gtls_t *pNsd)
49a8b5
 			}
49a8b5
 			break;
49a8b5
 		case gtlsRtry_recv:
49a8b5
-			dbgprintf("retrying gtls recv, nsd: %p\n", pNsd);
49a8b5
-			CHKiRet(gtlsRecordRecv(pNsd));
49a8b5
-			pNsd->rtryCall = gtlsRtry_None; /* we are done */
49a8b5
+			dbgprintf("doRetry: retrying gtls recv, nsd: %p\n", pNsd);
49a8b5
+			iRet = gtlsRecordRecv(pNsd);
49a8b5
+			if (iRet == RS_RET_RETRY) {
49a8b5
+				// Check if there is pending data
49a8b5
+				size_t stBytesLeft = gnutls_record_check_pending(pNsd->sess);
49a8b5
+				if (stBytesLeft > 0) {
49a8b5
+					// We are in retry and more data waiting, finalize it
49a8b5
+					goto finalize_it;
49a8b5
+				} else {
49a8b5
+					dbgprintf("doRetry: gtlsRecordRecv returned RETRY, but there is no pending"
49a8b5
+						"data on nsd: %p\n", pNsd);
49a8b5
+				}
49a8b5
+			}
49a8b5
+			pNsd->rtryCall = gtlsRtry_None; /* no more data, we are done */
49a8b5
 			gnuRet = 0;
49a8b5
 			break;
49a8b5
 		case gtlsRtry_None:
49a8b5
@@ -241,7 +256,7 @@ IsReady(nsdsel_t *pNsdsel, nsd_t *pNsd,
49a8b5
 		 * socket. -- rgerhards, 2010-11-20
49a8b5
 		 */
49a8b5
 		if(pThis->iBufferRcvReady) {
49a8b5
-			dbgprintf("nsd_gtls: dummy read, buffer not available for this FD\n");
49a8b5
+			dbgprintf("nsd_gtls: dummy read, %p->buffer not available for this FD\n", pThis);
49a8b5
 			*pbIsReady = 0;
49a8b5
 			FINALIZE;
49a8b5
 		}
49a8b5
diff -up rsyslog-8.2102.0/runtime/tcpsrv.c.orig rsyslog-8.2102.0/runtime/tcpsrv.c
49a8b5
--- rsyslog-8.2102.0/runtime/tcpsrv.c.orig	2022-04-11 09:27:00.376245726 +0200
49a8b5
+++ rsyslog-8.2102.0/runtime/tcpsrv.c	2022-04-11 09:41:57.885777708 +0200
49a8b5
@@ -609,14 +609,15 @@ doReceive(tcpsrv_t *pThis, tcps_sess_t *
49a8b5
 	int oserr = 0;
49a8b5
 
49a8b5
 	ISOBJ_TYPE_assert(pThis, tcpsrv);
49a8b5
-	DBGPRINTF("netstream %p with new data\n", (*ppSess)->pStrm);
49a8b5
+	prop.GetString((*ppSess)->fromHostIP, &pszPeer, &lenPeer);
49a8b5
+	DBGPRINTF("netstream %p with new data from remote peer %s\n", (*ppSess)->pStrm, pszPeer);
49a8b5
 	/* Receive message */
49a8b5
 	iRet = pThis->pRcvData(*ppSess, buf, sizeof(buf), &iRcvd, &oserr);
49a8b5
 	switch(iRet) {
49a8b5
 	case RS_RET_CLOSED:
49a8b5
 		if(pThis->bEmitMsgOnClose) {
49a8b5
 			errno = 0;
49a8b5
-			prop.GetString((*ppSess)->fromHostIP, &pszPeer, &lenPeer);
49a8b5
+			// prop.GetString((*ppSess)->fromHostIP, &pszPeer, &lenPeer);
49a8b5
 			LogError(0, RS_RET_PEER_CLOSED_CONN, "Netstream session %p closed by remote "
49a8b5
 				"peer %s.\n", (*ppSess)->pStrm, pszPeer);
49a8b5
 		}
49a8b5
@@ -632,13 +633,13 @@ doReceive(tcpsrv_t *pThis, tcps_sess_t *
49a8b5
 			/* in this case, something went awfully wrong.
49a8b5
 			 * We are instructed to terminate the session.
49a8b5
 			 */
49a8b5
-			prop.GetString((*ppSess)->fromHostIP, &pszPeer, &lenPeer);
49a8b5
+			// prop.GetString((*ppSess)->fromHostIP, &pszPeer, &lenPeer);
49a8b5
 			LogError(oserr, localRet, "Tearing down TCP Session from %s", pszPeer);
49a8b5
 			CHKiRet(closeSess(pThis, ppSess, pPoll));
49a8b5
 		}
49a8b5
 		break;
49a8b5
 	default:
49a8b5
-		prop.GetString((*ppSess)->fromHostIP, &pszPeer, &lenPeer);
49a8b5
+		// prop.GetString((*ppSess)->fromHostIP, &pszPeer, &lenPeer);
49a8b5
 		LogError(oserr, iRet, "netstream session %p from %s will be closed due to error",
49a8b5
 				(*ppSess)->pStrm, pszPeer);
49a8b5
 		CHKiRet(closeSess(pThis, ppSess, pPoll));
49a8b5
@@ -838,6 +839,7 @@ RunSelect(tcpsrv_t *pThis, nsd_epworkset
49a8b5
 		while(iTCPSess != -1) {
49a8b5
 			/* TODO: access to pNsd is NOT really CLEAN, use method... */
49a8b5
 			CHKiRet(nssel.Add(pSel, pThis->pSessions[iTCPSess]->pStrm, NSDSEL_RD));
49a8b5
+			DBGPRINTF("tcpsrv process session %d:\n", iTCPSess);
49a8b5
 			/* now get next... */
49a8b5
 			iTCPSess = TCPSessGetNxtSess(pThis, iTCPSess);
49a8b5
 		}