2929 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
3030 * Portions Copyright (c) 1994, Regents of the University of California
3131 *
32- * $Id: pqcomm.c,v 1.140 2002/09/04 20 :31:19 momjian Exp $
32+ * $Id: pqcomm.c,v 1.141 2002/09/04 23 :31:34 tgl Exp $
3333 *
3434 *-------------------------------------------------------------------------
3535 */
@@ -555,24 +555,32 @@ pq_getbytes(char *s, size_t len)
555555 * The return value is placed in an expansible StringInfo.
556556 * Note that space allocation comes from the current memory context!
557557 *
558+ * If maxlen is not zero, it is an upper limit on the length of the
559+ * string we are willing to accept. We abort the connection (by
560+ * returning EOF) if client tries to send more than that. Note that
561+ * since we test maxlen in the outer per-bufferload loop, the limit
562+ * is fuzzy: we might accept up to PQ_BUFFER_SIZE more bytes than
563+ * specified. This is fine for the intended purpose, which is just
564+ * to prevent DoS attacks from not-yet-authenticated clients.
565+ *
558566 * NOTE: this routine does not do any character set conversion,
559567 * even though it is presumably useful only for text, because
560568 * no code in this module should depend on the encoding.
561- * See pq_getstr in pqformat.c for that.
569+ * See pq_getstr_bounded in pqformat.c for that.
562570 *
563571 * returns 0 if OK, EOF if trouble
564572 * --------------------------------
565573 */
566574int
567- pq_getstring (StringInfo s )
575+ pq_getstring (StringInfo s , int maxlen )
568576{
569577 int i ;
570578
571579 /* Reset string to empty */
572580 s -> len = 0 ;
573581 s -> data [0 ] = '\0' ;
574582
575- /* Read until we get the terminating '\0' */
583+ /* Read until we get the terminating '\0' or overrun maxlen */
576584 for (;;)
577585 {
578586 while (PqRecvPointer >= PqRecvLength )
@@ -594,10 +602,13 @@ pq_getstring(StringInfo s)
594602 }
595603
596604 /* If we're here we haven't got the \0 in the buffer yet. */
597-
598605 appendBinaryStringInfo (s , PqRecvBuffer + PqRecvPointer ,
599606 PqRecvLength - PqRecvPointer );
600607 PqRecvPointer = PqRecvLength ;
608+
609+ /* If maxlen is specified, check for overlength input. */
610+ if (maxlen > 0 && s -> len > maxlen )
611+ return EOF ;
601612 }
602613}
603614
0 commit comments