27eccf
From ee8ea4f1c88a0393206769cd30a545dc3375f868 Mon Sep 17 00:00:00 2001
27eccf
From: =?UTF-8?q?Lubo=C5=A1=20Uhliarik?= <luhliari@redhat.com>
27eccf
Date: Wed, 2 Feb 2022 20:14:55 +0100
27eccf
Subject: [PATCH] Fix ALPACA security issue
27eccf
27eccf
---
27eccf
 src/mail/ngx_mail.h             |  3 +++
27eccf
 src/mail/ngx_mail_core_module.c | 10 ++++++++++
27eccf
 src/mail/ngx_mail_handler.c     | 15 ++++++++++++++-
27eccf
 3 files changed, 27 insertions(+), 1 deletion(-)
27eccf
27eccf
diff --git a/src/mail/ngx_mail.h b/src/mail/ngx_mail.h
27eccf
index b865a3b..76cae37 100644
27eccf
--- a/src/mail/ngx_mail.h
27eccf
+++ b/src/mail/ngx_mail.h
27eccf
@@ -115,6 +115,8 @@ typedef struct {
27eccf
     ngx_msec_t              timeout;
27eccf
     ngx_msec_t              resolver_timeout;
27eccf
 
27eccf
+    ngx_uint_t              max_errors;
27eccf
+
27eccf
     ngx_str_t               server_name;
27eccf
 
27eccf
     u_char                 *file_name;
27eccf
@@ -231,6 +233,7 @@ typedef struct {
27eccf
     ngx_uint_t              command;
27eccf
     ngx_array_t             args;
27eccf
 
27eccf
+    ngx_uint_t              errors;
27eccf
     ngx_uint_t              login_attempt;
27eccf
 
27eccf
     /* used to parse POP3/IMAP/SMTP command */
27eccf
diff --git a/src/mail/ngx_mail_core_module.c b/src/mail/ngx_mail_core_module.c
27eccf
index 4083124..115671c 100644
27eccf
--- a/src/mail/ngx_mail_core_module.c
27eccf
+++ b/src/mail/ngx_mail_core_module.c
27eccf
@@ -85,6 +85,13 @@ static ngx_command_t  ngx_mail_core_commands[] = {
27eccf
       offsetof(ngx_mail_core_srv_conf_t, resolver_timeout),
27eccf
       NULL },
27eccf
 
27eccf
+    { ngx_string("max_errors"),
27eccf
+      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
27eccf
+      ngx_conf_set_num_slot,
27eccf
+      NGX_MAIL_SRV_CONF_OFFSET,
27eccf
+      offsetof(ngx_mail_core_srv_conf_t, max_errors),
27eccf
+      NULL },
27eccf
+
27eccf
       ngx_null_command
27eccf
 };
27eccf
 
27eccf
@@ -163,6 +170,8 @@ ngx_mail_core_create_srv_conf(ngx_conf_t *cf)
27eccf
     cscf->timeout = NGX_CONF_UNSET_MSEC;
27eccf
     cscf->resolver_timeout = NGX_CONF_UNSET_MSEC;
27eccf
 
27eccf
+    cscf->max_errors = NGX_CONF_UNSET_UINT;
27eccf
+
27eccf
     cscf->resolver = NGX_CONF_UNSET_PTR;
27eccf
 
27eccf
     cscf->file_name = cf->conf_file->file.name.data;
27eccf
@@ -182,6 +191,7 @@ ngx_mail_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
27eccf
     ngx_conf_merge_msec_value(conf->resolver_timeout, prev->resolver_timeout,
27eccf
                               30000);
27eccf
 
27eccf
+    ngx_conf_merge_uint_value(conf->max_errors, prev->max_errors, 5);
27eccf
 
27eccf
     ngx_conf_merge_str_value(conf->server_name, prev->server_name, "");
27eccf
 
27eccf
diff --git a/src/mail/ngx_mail_handler.c b/src/mail/ngx_mail_handler.c
27eccf
index 0aaa0e7..71b8151 100644
27eccf
--- a/src/mail/ngx_mail_handler.c
27eccf
+++ b/src/mail/ngx_mail_handler.c
27eccf
@@ -871,7 +871,20 @@ ngx_mail_read_command(ngx_mail_session_t *s, ngx_connection_t *c)
27eccf
         return NGX_MAIL_PARSE_INVALID_COMMAND;
27eccf
     }
27eccf
 
27eccf
-    if (rc == NGX_IMAP_NEXT || rc == NGX_MAIL_PARSE_INVALID_COMMAND) {
27eccf
+    if (rc == NGX_MAIL_PARSE_INVALID_COMMAND) {
27eccf
+
27eccf
+        s->errors++;
27eccf
+
27eccf
+        if (s->errors >= cscf->max_errors) {
27eccf
+            ngx_log_error(NGX_LOG_INFO, c->log, 0,
27eccf
+                          "client sent too many invalid commands");
27eccf
+            s->quit = 1;
27eccf
+        }
27eccf
+
27eccf
+        return rc;
27eccf
+    }
27eccf
+
27eccf
+    if (rc == NGX_IMAP_NEXT) {
27eccf
         return rc;
27eccf
     }
27eccf
 
27eccf
-- 
27eccf
2.31.1
27eccf