File cups-2.2.7-CVE-2025-58436.patch of Package cups.41695

--- cups/http-private.h.original	2018-03-23 04:48:36.000000000 +0100
+++ cups/http-private.h	2025-10-07 12:12:36.673493878 +0200
@@ -171,6 +171,7 @@ extern "C" {
  * Constants...
  */
 
+#  define _HTTP_MAX_BUFFER	32768	/* Size of read buffer */
 #  define _HTTP_MAX_SBUFFER	65536	/* Size of (de)compression buffer */
 #  define _HTTP_RESOLVE_DEFAULT	0	/* Just resolve with default options */
 #  define _HTTP_RESOLVE_STDERR	1	/* Log resolve progress to stderr */
@@ -297,8 +298,8 @@ struct _http_s				/**** HTTP connection
   http_encoding_t	data_encoding;	/* Chunked or not */
   int			_data_remaining;/* Number of bytes left (deprecated) */
   int			used;		/* Number of bytes used in buffer */
-  char			buffer[HTTP_MAX_BUFFER];
-					/* Buffer for incoming data */
+  char			_buffer[HTTP_MAX_BUFFER];
+					/* Old read buffer (deprecated) */
   int			_auth_type;	/* Authentication in use (deprecated) */
   unsigned char		_md5_state[88];	/* MD5 state (deprecated) */
   char			nonce[HTTP_MAX_VALUE];
@@ -367,6 +368,8 @@ struct _http_s				/**** HTTP connection
   z_stream		stream;		/* (De)compression stream */
   Bytef			*sbuffer;	/* (De)compression buffer */
 #  endif /* HAVE_LIBZ */
+  char                 buffer[_HTTP_MAX_BUFFER];
+                                       /* Read buffer */
 };
 #  endif /* !_HTTP_NO_PRIVATE */
 
--- cups/http.c.patched	2025-10-07 12:03:50.322367416 +0200
+++ cups/http.c	2025-10-07 13:06:07.862243430 +0200
@@ -53,7 +53,7 @@ static http_t		*http_create(const char *
 static void		http_debug_hex(const char *prefix, const char *buffer,
 			               int bytes);
 #endif /* DEBUG */
-static ssize_t		http_read(http_t *http, char *buffer, size_t length);
+static ssize_t		http_read(http_t *http, char *buffer, size_t length, int timeout);
 static ssize_t		http_read_buffered(http_t *http, char *buffer, size_t length);
 static ssize_t		http_read_chunk(http_t *http, char *buffer, size_t length);
 static int		http_send(http_t *http, http_state_t request,
@@ -1210,7 +1210,7 @@ httpGets(char   *line,			/* I - Line to
         return (NULL);
       }
 
-      bytes = http_read(http, http->buffer + http->used, (size_t)(HTTP_MAX_BUFFER - http->used));
+      bytes = http_read(http, http->buffer + http->used, (size_t)(_HTTP_MAX_BUFFER - http->used), http->wait_value);
 
       DEBUG_printf(("4httpGets: read " CUPS_LLFMT " bytes.", CUPS_LLCAST bytes));
 
@@ -1725,24 +1725,13 @@ httpPeek(http_t *http,			/* I - HTTP con
 
     ssize_t	buflen;			/* Length of read for buffer */
 
-    if (!http->blocking)
-    {
-      while (!httpWait(http, http->wait_value))
-      {
-	if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data))
-	  continue;
-
-	return (0);
-      }
-    }
-
     if ((size_t)http->data_remaining > sizeof(http->buffer))
       buflen = sizeof(http->buffer);
     else
       buflen = (ssize_t)http->data_remaining;
 
     DEBUG_printf(("2httpPeek: Reading %d bytes into buffer.", (int)buflen));
-    bytes = http_read(http, http->buffer, (size_t)buflen);
+    bytes = http_read(http, http->buffer, (size_t)buflen, http->wait_value);
 
     DEBUG_printf(("2httpPeek: Read " CUPS_LLFMT " bytes into buffer.",
                   CUPS_LLCAST bytes));
@@ -1763,9 +1752,9 @@ httpPeek(http_t *http,			/* I - HTTP con
     int		zerr;			/* Decompressor error */
     z_stream	stream;			/* Copy of decompressor stream */
 
-    if (http->used > 0 && http->stream.avail_in < HTTP_MAX_BUFFER)
+    if (http->used > 0 && http->stream.avail_in < _HTTP_MAX_BUFFER)
     {
-      size_t buflen = buflen = HTTP_MAX_BUFFER - http->stream.avail_in;
+      size_t buflen = buflen = _HTTP_MAX_BUFFER - http->stream.avail_in;
 					/* Number of bytes to copy */
 
       if (http->stream.avail_in > 0 &&
@@ -2023,7 +2012,7 @@ httpRead2(http_t *http,			/* I - HTTP co
 
       if (bytes == 0)
       {
-        ssize_t buflen = HTTP_MAX_BUFFER - (ssize_t)http->stream.avail_in;
+        ssize_t buflen = _HTTP_MAX_BUFFER - (ssize_t)http->stream.avail_in;
 					/* Additional bytes for buffer */
 
         if (buflen > 0)
@@ -2796,7 +2785,7 @@ int					/* O - 1 to continue, 0 to stop
 _httpUpdate(http_t        *http,	/* I - HTTP connection */
             http_status_t *status)	/* O - Current HTTP status */
 {
-  char		line[32768],		/* Line from connection... */
+  char		line[_HTTP_MAX_BUFFER],	/* Line from connection... */
 		*value;			/* Pointer to value on line */
   http_field_t	field;			/* Field index */
   int		major, minor;		/* HTTP version numbers */
@@ -2804,12 +2793,46 @@ _httpUpdate(http_t        *http,	/* I -
 
   DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http, (void *)status, httpStateString(http->state)));
 
+  /* When doing non-blocking I/O, make sure we have a whole line... */
+  if (!http->blocking)
+  {
+    ssize_t	bytes;			/* Bytes "peeked" from connection */
+
+    /* See whether our read buffer is full... */
+    DEBUG_printf(("2_httpUpdate: used=%d", http->used));
+
+    if (http->used > 0 && !memchr(http->buffer, '\n', (size_t)http->used) && (size_t)http->used < sizeof(http->buffer))
+    {
+      /* No, try filling in more data... */
+      if ((bytes = http_read(http, http->buffer + http->used, sizeof(http->buffer) - (size_t)http->used, /*timeout*/0)) > 0)
+      {
+        DEBUG_printf(("2_httpUpdate: Read %d bytes.", (int)bytes));
+        http->used += (int)bytes;
+      }
+    }
+
+    /* Peek at the incoming data... */
+    if (!http->used || !memchr(http->buffer, '\n', (size_t)http->used))
+    {
+      /* Don't have a full line, tell the reader to try again when there is more data... */
+      DEBUG_puts("1_htttpUpdate: No newline in buffer yet.");
+      if ((size_t)http->used == sizeof(http->buffer))
+        *status = HTTP_STATUS_ERROR;
+      else
+        *status = HTTP_STATUS_CONTINUE;
+      return (0);
+    }
+
+    DEBUG_puts("2_httpUpdate: Found newline in buffer.");
+  }
+
  /*
   * Grab a single line from the connection...
   */
 
   if (!httpGets(line, sizeof(line), http))
   {
+    DEBUG_puts("1_httpUpdate: Error reading request line.");
     *status = HTTP_STATUS_ERROR;
     return (0);
   }
@@ -4069,7 +4092,8 @@ http_debug_hex(const char *prefix,	/* I
 static ssize_t				/* O - Number of bytes read or -1 on error */
 http_read(http_t *http,			/* I - HTTP connection */
           char   *buffer,		/* I - Buffer */
-          size_t length)		/* I - Maximum bytes to read */
+          size_t length,		/* I - Maximum bytes to read */
+          int    timeout)		/* I - Wait timeout */
 {
   ssize_t	bytes;			/* Bytes read */
 
@@ -4078,7 +4102,7 @@ http_read(http_t *http,			/* I - HTTP co
 
   if (!http->blocking || http->timeout_value > 0.0)
   {
-    while (!httpWait(http, http->wait_value))
+    while (!_httpWait(http, timeout, 1))
     {
       if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data))
 	continue;
@@ -4196,7 +4220,7 @@ http_read_buffered(http_t *http,	/* I -
     else
       bytes = (ssize_t)length;
 
-    DEBUG_printf(("2http_read: Grabbing %d bytes from input buffer.",
+    DEBUG_printf(("2http_read_buffered: Grabbing %d bytes from input buffer.",
                   (int)bytes));
 
     memcpy(buffer, http->buffer, (size_t)bytes);
@@ -4206,7 +4230,7 @@ http_read_buffered(http_t *http,	/* I -
       memmove(http->buffer, http->buffer + bytes, (size_t)http->used);
   }
   else
-    bytes = http_read(http, buffer, length);
+    bytes = http_read(http, buffer, length, http->wait_value);
 
   return (bytes);
 }
@@ -4551,15 +4575,15 @@ http_set_timeout(int    fd,		/* I - File
 static void
 http_set_wait(http_t *http)		/* I - HTTP connection */
 {
-  if (http->blocking)
-  {
-    http->wait_value = (int)(http->timeout_value * 1000);
+  http->wait_value = (int)(http->timeout_value * 1000);
 
-    if (http->wait_value <= 0)
+  if (http->wait_value <= 0)
+  {
+    if (http->blocking)
       http->wait_value = 60000;
+    else
+      http->wait_value = 1000;
   }
-  else
-    http->wait_value = 10000;
 }
 
 
--- cups/tls-gnutls.c.original	2018-03-23 04:48:36.000000000 +0100
+++ cups/tls-gnutls.c	2025-10-07 14:00:35.559976604 +0200
@@ -1488,10 +1488,9 @@ _httpTLSStart(http_t *http)		/* I - Conn
 
       if (!cupsMakeServerCredentials(tls_keypath, hostname[0] ? hostname : tls_common_name, 0, NULL, time(NULL) + 365 * 86400))
       {
-	DEBUG_puts("4_httpTLSStart: cupsMakeServerCredentials failed.");
+	DEBUG_printf(("4_httpTLSStart: cupsMakeServerCredentials failed: %s", GetLastError()));
 	http->error  = errno = EINVAL;
 	http->status = HTTP_STATUS_ERROR;
-	_cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create server credentials."), 1);
 
 	return (-1);
       }
--- scheduler/client.h.original	2018-03-23 04:48:36.000000000 +0100
+++ scheduler/client.h	2025-10-07 12:41:33.693609205 +0200
@@ -52,6 +52,9 @@ struct cupsd_client_s
   cups_lang_t		*language;	/* Language to use */
 #ifdef HAVE_SSL
   int			auto_ssl;	/* Automatic test for SSL/TLS */
+  time_t               tls_start;      /* Do TLS negotiation? */
+  int                  tls_upgrade;    /* Doing TLS upgrade via OPTIONS? */
+  http_encryption_t    encryption;     /* Type of TLS negotiation */
 #endif /* HAVE_SSL */
   http_addr_t		clientaddr;	/* Client's server address */
   char			clientname[256];/* Client's server name for connection */
--- scheduler/client.c.orig	2025-10-08 09:23:19.106909636 +0200
+++ scheduler/client.c	2025-10-08 10:18:51.122218396 +0200
@@ -40,6 +40,7 @@ static int            compare_clients(cupsd_client
 			                void *data);
 #ifdef HAVE_SSL
 static int		cupsd_start_tls(cupsd_client_t *con, http_encryption_t e);
+static int		check_start_tls(cupsd_client_t *con);
 #endif /* HAVE_SSL */
 static char		*get_file(cupsd_client_t *con, struct stat *filestats,
 			          char *filename, size_t len);
@@ -358,14 +359,20 @@ cupsdAcceptClient(cupsd_listener_t *lis)
   if (lis->encryption == HTTP_ENCRYPTION_ALWAYS)
   {
    /*
-    * https connection; go secure...
+    * HTTPS connection, force TLS negotiation...
     */
 
-    if (cupsd_start_tls(con, HTTP_ENCRYPTION_ALWAYS))
-      cupsdCloseClient(con);
+    con->tls_start     = time(NULL);
+    con->encryption = HTTP_ENCRYPTION_ALWAYS;
   }
   else
+  {
+   /*
+    * HTTP connection, but check for HTTPS negotiation on first data...
+    */
+
     con->auto_ssl = 1;
+  }
 #endif /* HAVE_SSL */
 }
 
@@ -596,17 +602,46 @@ cupsdReadClient(cupsd_client_t *con)	/*
 
     con->auto_ssl = 0;
 
-    if (recv(httpGetFd(con->http), buf, 1, MSG_PEEK) == 1 &&
-        (!buf[0] || !strchr("DGHOPT", buf[0])))
+    if (recv(httpGetFd(con->http), buf, 5, MSG_PEEK) == 5 && buf[0] == 0x16 && buf[1] == 3 && buf[2])
     {
      /*
-      * Encrypt this connection...
+      * Client hello record, encrypt this connection...
       */
 
-      cupsdLogClient(con, CUPSD_LOG_DEBUG2, "Saw first byte %02X, auto-negotiating SSL/TLS session.", buf[0] & 255);
+      cupsdLogClient(con, CUPSD_LOG_DEBUG2, "Saw client hello record, auto-negotiating TLS session.");
+      con->tls_start     = time(NULL);
+      con->encryption = HTTP_ENCRYPTION_ALWAYS;
+    }
+  }
+
+  if (con->tls_start)
+  {
+   /*
+    * Try negotiating TLS...
+    */
+
+    int tls_status = check_start_tls(con);
 
-      if (cupsd_start_tls(con, HTTP_ENCRYPTION_ALWAYS))
+    if (tls_status < 0)
+    {
+     /*
+      * TLS negotiation failed, close the connection.
+      */
+
+      cupsdCloseClient(con);
+      return;
+    }
+    else if (tls_status == 0)
+    {
+     /*
+      * Nothing to do yet...
+      */
+
+      if ((time(NULL) - con->tls_start) > 5)
+      {
+        // Timeout, close the connection...
         cupsdCloseClient(con);
+      }
 
       return;
     }
@@ -770,9 +805,7 @@ cupsdReadClient(cupsd_client_t *con)	/*
         * Parse incoming parameters until the status changes...
 	*/
 
-        while ((status = httpUpdate(con->http)) == HTTP_STATUS_CONTINUE)
-	  if (!httpGetReady(con->http))
-	    break;
+        status = httpUpdate(con->http);
 
 	if (status != HTTP_STATUS_OK && status != HTTP_STATUS_CONTINUE)
 	{
@@ -981,11 +994,10 @@ cupsdReadClient(cupsd_client_t *con)	/*
 	  return;
 	}
 
-        if (cupsd_start_tls(con, HTTP_ENCRYPTION_REQUIRED))
-        {
-	  cupsdCloseClient(con);
-	  return;
-	}
+        con->tls_start         = time(NULL);
+        con->tls_upgrade = 1;
+        con->encryption     = HTTP_ENCRYPTION_REQUIRED;
+        return;
 #else
 	if (!cupsdSendError(con, HTTP_STATUS_NOT_IMPLEMENTED, CUPSD_AUTH_NONE))
 	{
@@ -2871,6 +2883,69 @@ check_if_modified(
 }
 
 
+#ifdef HAVE_SSL
+/*
+ * 'check_start_tls()' - Start encryption on a connection.
+ */
+
+static int				/* O - 0 to continue, 1 on success, -1 on error */
+check_start_tls(cupsd_client_t *con)	/* I - Client connection */
+{
+  unsigned char	chello[4096];		/* Client hello record */
+  ssize_t	chello_bytes;		/* Bytes read/peeked */
+  int		chello_len;		/* Length of record */
+
+
+ /*
+  * See if we have a good and complete client hello record...
+  */
+
+  if ((chello_bytes = recv(httpGetFd(con->http), (char *)chello, sizeof(chello), MSG_PEEK)) < 5)
+    return (0);				/* Not enough bytes (yet) */
+
+  if (chello[0] != 0x016 || chello[1] != 3 || chello[2] == 0)
+    return (-1);			/* Not a TLS Client Hello record */
+
+  chello_len = (chello[3] << 8) | chello[4];
+
+  if ((chello_len + 5) > chello_bytes)
+    return (0);				/* Not enough bytes yet */
+
+ /*
+  * OK, we do, try negotiating...
+  */
+
+  con->tls_start = 0;
+
+  if (httpEncryption(con->http, con->encryption))
+  {
+    cupsdLogClient(con, CUPSD_LOG_ERROR, "Unable to encrypt connection: %s", cupsLastErrorString());
+    return (-1);
+  }
+
+  cupsdLogClient(con, CUPSD_LOG_DEBUG, "Connection now encrypted.");
+
+  if (con->tls_upgrade)
+  {
+    // Respond to the original OPTIONS command...
+    con->tls_upgrade = 0;
+
+    httpClearFields(con->http);
+    httpClearCookie(con->http);
+    httpSetField(con->http, HTTP_FIELD_CONTENT_LENGTH, "0");
+
+    if (!cupsdSendHeader(con, HTTP_STATUS_OK, NULL, CUPSD_AUTH_NONE))
+    {
+      cupsdCloseClient(con);
+      return (-1);
+    }
+  }
+
+  return (1);
+}
+#endif /* HAVE_SSL */
+
+
 /*
  * 'compare_clients()' - Compare two client connections.
  */
--- scheduler/select.c.orig	2018-03-23 04:48:36.000000000 +0100
+++ scheduler/select.c	2025-10-13 14:46:56.925913237 +0200
@@ -412,6 +412,9 @@ cupsdDoSelect(long timeout)		/* I - Time
 
   cupsd_in_select = 1;
 
+  // Prevent 100% CPU by releasing control before the kevent call...
+  usleep(1);
+
   if (timeout >= 0 && timeout < 86400)
   {
     ktimeout.tv_sec  = timeout;
@@ -457,6 +460,8 @@ cupsdDoSelect(long timeout)		/* I - Time
     int			i;		/* Looping var */
     struct epoll_event	*event;		/* Current event */
 
+    // Prevent 100% CPU by releasing control before the epoll_wait call...
+    usleep(1);
 
     if (timeout >= 0 && timeout < 86400)
       nfds = epoll_wait(cupsd_epoll_fd, cupsd_epoll_events, MaxFDs,
@@ -550,6 +555,9 @@ cupsdDoSelect(long timeout)		/* I - Time
     }
   }
 
+  // Prevent 100% CPU by releasing control before the poll call...
+  usleep(1);
+
   if (timeout >= 0 && timeout < 86400)
     nfds = poll(cupsd_pollfds, (nfds_t)count, timeout * 1000);
   else
@@ -603,6 +611,9 @@ cupsdDoSelect(long timeout)		/* I - Time
   cupsd_current_input  = cupsd_global_input;
   cupsd_current_output = cupsd_global_output;
 
+  // Prevent 100% CPU by releasing control before the select call...
+  usleep(1);
+
   if (timeout >= 0 && timeout < 86400)
   {
     stimeout.tv_sec  = timeout;
openSUSE Build Service is sponsored by