33b671a
--- contrib/mod_sftp_pam.c
33b671a
+++ contrib/mod_sftp_pam.c
33b671a
@@ -169,22 +169,13 @@ static int sftppam_converse(int nmsgs, P
33b671a
     return PAM_CONV_ERR;
33b671a
   }
33b671a
 
33b671a
-  if (sftp_kbdint_recv_response(sftppam_driver.driver_pool, &recvd_count,
33b671a
-      &recvd_responses) < 0) {
33b671a
+  if (sftp_kbdint_recv_response(sftppam_driver.driver_pool, list->nelts,
33b671a
+      &recvd_count, &recvd_responses) < 0) {
33b671a
     pr_trace_msg(trace_channel, 3,
33b671a
       "error receiving keyboard-interactive responses: %s", strerror(errno));
33b671a
     return PAM_CONV_ERR;
33b671a
   }
33b671a
 
33b671a
-  /* Make sure that the count of responses matches the challenge count. */
33b671a
-  if (recvd_count != list->nelts) {
33b671a
-    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_PAM_VERSION,
33b671a
-      "sent %d %s, but received %u %s", nmsgs,
33b671a
-      list->nelts != 1 ? "challenges" : "challenge", recvd_count,
33b671a
-      recvd_count != 1 ? "responses" : "response");
33b671a
-    return PAM_CONV_ERR;
33b671a
-  }
33b671a
-
33b671a
   res = calloc(nmsgs, sizeof(struct pam_response));
33b671a
   if (res == NULL) {
33b671a
     pr_log_pri(PR_LOG_CRIT, "Out of memory!");
33b671a
--- contrib/mod_sftp/kbdint.c
33b671a
+++ contrib/mod_sftp/kbdint.c
33b671a
@@ -31,6 +31,8 @@
33b671a
 #include "utf8.h"
33b671a
 #include "kbdint.h"
33b671a
 
33b671a
+#define SFTP_KBDINT_MAX_RESPONSES	500
33b671a
+
33b671a
 struct kbdint_driver {
33b671a
   struct kbdint_driver *next, *prev;
33b671a
 
33b671a
@@ -252,8 +254,8 @@ int sftp_kbdint_send_challenge(const cha
33b671a
   return res;
33b671a
 }
33b671a
 
33b671a
-int sftp_kbdint_recv_response(pool *p, unsigned int *count,
33b671a
-    const char ***responses) {
33b671a
+int sftp_kbdint_recv_response(pool *p, unsigned int expected_count,
33b671a
+    unsigned int *rcvd_count, const char ***responses) {
33b671a
   register unsigned int i;
33b671a
   char *buf;
33b671a
   cmd_rec *cmd;
33b671a
@@ -264,7 +266,7 @@ int sftp_kbdint_recv_response(pool *p, u
33b671a
   int res;
33b671a
 
33b671a
   if (p == NULL ||
33b671a
-      count == NULL ||
33b671a
+      rcvd_count == NULL ||
33b671a
       responses == NULL) {
33b671a
     errno = EINVAL;
33b671a
     return -1;
33b671a
@@ -299,6 +301,29 @@ int sftp_kbdint_recv_response(pool *p, u
33b671a
 
33b671a
   resp_count = sftp_msg_read_int(pkt->pool, &buf, &buflen);
33b671a
 
33b671a
+  /* Ensure that the number of responses sent by the client is the same
33b671a
+   * as the number of challenges sent, lest a malicious client attempt to
33b671a
+   * trick us into allocating too much memory (Bug#3973).
33b671a
+   */
33b671a
+  if (resp_count != expected_count) {
33b671a
+    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
33b671a
+      "sent %lu %s, but received %lu %s", (unsigned long) expected_count,
33b671a
+      expected_count != 1 ? "challenges" : "challenge",
33b671a
+      (unsigned long) resp_count, resp_count != 1 ? "responses" : "response");
33b671a
+    destroy_pool(pkt->pool);
33b671a
+    errno = EPERM;
33b671a
+    return -1;
33b671a
+  }
33b671a
+
33b671a
+  if (resp_count > SFTP_KBDINT_MAX_RESPONSES) {
33b671a
+    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
33b671a
+      "received too many responses (%lu > max %lu), rejecting",
33b671a
+      (unsigned long) resp_count, (unsigned long) SFTP_KBDINT_MAX_RESPONSES);
33b671a
+    destroy_pool(pkt->pool);
33b671a
+    errno = EPERM;
33b671a
+    return -1;
33b671a
+  }
33b671a
+
33b671a
   list = make_array(p, resp_count, sizeof(char *));
33b671a
   for (i = 0; i < resp_count; i++) {
33b671a
     char *resp;
33b671a
@@ -307,7 +332,7 @@ int sftp_kbdint_recv_response(pool *p, u
33b671a
     *((char **) push_array(list)) = pstrdup(p, sftp_utf8_decode_str(p, resp));
33b671a
   }
33b671a
 
33b671a
-  *count = (unsigned int) resp_count;
33b671a
+  *rcvd_count = (unsigned int) resp_count;
33b671a
   *responses = ((const char **) list->elts);
33b671a
   return 0;
33b671a
 }
33b671a
--- contrib/mod_sftp/mod_sftp.h.in
33b671a
+++ contrib/mod_sftp/mod_sftp.h.in
33b671a
@@ -160,7 +160,8 @@ int sftp_kbdint_register_driver(const ch
33b671a
 int sftp_kbdint_unregister_driver(const char *name);
33b671a
 int sftp_kbdint_send_challenge(const char *, const char *, unsigned int,
33b671a
   sftp_kbdint_challenge_t *);
33b671a
-int sftp_kbdint_recv_response(pool *, unsigned int *, const char ***);
33b671a
+int sftp_kbdint_recv_response(pool *, unsigned int, unsigned int *,
33b671a
+  const char ***);
33b671a
 
33b671a
 /* API for modules that which to register keystores, for the
33b671a
  * SFTPAuthorizedHostKeys and SFTPAuthorizedUserKeys directives.