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

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