From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Subject: [PATCH] Fix CVE-2026-40020 Upstream commits: acl: Add acl_id_is_valid() imap-acl: Fail if ACL identifier is invalid acl: Assert-crash if ACL identifier is invalid before writing it Taken from the Debian bookworm security update (dovecot 1:2.3.19.1+dfsg1-2.1+deb12u7), which carries these fixes for 2.3, and applied to 2.3.21.1. --- a/src/plugins/acl/acl-api.c +++ b/src/plugins/acl/acl-api.c @@ -3,6 +3,7 @@ #include "lib.h" #include "array.h" #include "str.h" +#include "unichar.h" #include "strescape.h" #include "hash.h" #include "mail-user.h" @@ -11,6 +12,8 @@ #include "acl-cache.h" #include "acl-api-private.h" +#include + struct acl_letter_map { char letter; const char *name; @@ -269,6 +272,19 @@ int acl_backend_nonowner_lookups_rebuild return backend->v.nonowner_lookups_rebuild(backend); } +bool acl_id_is_valid(const char *id) +{ + size_t len = strlen(id); + + if (len > ACL_ID_MAX_LEN) + return FALSE; + for (size_t i = 0; i < len; i++) { + if (i_iscntrl(id[i])) + return FALSE; + } + return uni_utf8_data_is_valid((const unsigned char *)id, len); +} + void acl_rights_write_id(string_t *dest, const struct acl_rights *right) { switch (right->id_type) { --- a/src/plugins/acl/acl-api.h +++ b/src/plugins/acl/acl-api.h @@ -35,6 +35,8 @@ struct acl_object; #define MAILBOX_ATTRIBUTE_PREFIX_ACL \ MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT"acl/" +#define ACL_ID_MAX_LEN 1024 + /* ACL identifiers in override order */ enum acl_id_type { /* Anyone's rights, including anonymous's. @@ -164,4 +166,8 @@ int acl_object_list_deinit(struct acl_ob /* Returns the canonical ID for the right. */ const char *acl_rights_get_id(const struct acl_rights *right); +/* Returns TRUE if the ACL identifier string is valid: no longer than + ACL_ID_MAX_LEN bytes, no control characters and valid UTF-8. */ +bool acl_id_is_valid(const char *id); + #endif --- a/src/plugins/acl/acl-backend-vfile-update.c +++ b/src/plugins/acl/acl-backend-vfile-update.c @@ -116,6 +116,7 @@ vfile_write_right(string_t *dest, const if (neg) str_append_c(dest,'-'); acl_rights_write_id(dest, right); + i_assert(acl_id_is_valid(str_c(dest))); if (strchr(str_c(dest), ' ') != NULL) T_BEGIN { /* need to escape it */ const char *escaped = t_strdup(str_escape(str_c(dest))); --- a/src/plugins/imap-acl/imap-acl-plugin.c +++ b/src/plugins/imap-acl/imap-acl-plugin.c @@ -802,6 +802,11 @@ imap_acl_identifier_parse(struct client_ { struct mail_user *user = cmd->client->user; + if (!acl_id_is_valid(id)) { + *client_error_r = "Invalid identifier"; + return -1; + } + if (str_begins(id, IMAP_ACL_GLOBAL_PREFIX)) { *client_error_r = t_strdup_printf("Global ACLs can't be modified: %s", id);