<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.kde.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.6"
          urlbase="https://bugs.kde.org/"
          
          maintainer="sysadmin@kde.org"
>

    <bug>
          <bug_id>432353</bug_id>
          
          <creation_ts>2021-01-31 14:48:09 +0000</creation_ts>
          <short_desc>Untagged responses are processed before STARTTLS.</short_desc>
          <delta_ts>2022-01-31 21:11:10 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>2</classification_id>
          <classification>Applications</classification>
          <product>trojita</product>
          <component>IMAP</component>
          <version>0.7</version>
          <rep_platform>Other</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>NOR</priority>
          <bug_severity>critical</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Damian Poddebniak">93s4m32gd2ab8ax6</reporter>
          <assigned_to name="Trojita default assignee">trojita-bugs</assigned_to>
          <cc>aacid</cc>
    
    <cc>Kartz</cc>
          
          <cf_commitlink>https://invent.kde.org/pim/trojita/commit/27573cf04802fc2fdeb7f7beace4612b22ea1942</cf_commitlink>
          <cf_versionfixedin></cf_versionfixedin>
          <cf_sentryurl></cf_sentryurl>
          <votes>0</votes>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1999172</commentid>
    <comment_count>0</comment_count>
    <who name="Damian Poddebniak">93s4m32gd2ab8ax6</who>
    <bug_when>2021-01-31 14:48:09 +0000</bug_when>
    <thetext>Trojita accepts LIST, LSUB, STATUS, ... untagges responses before STARTTLS and incorporates them into local state.

I am not sure if this is already kind of a misbehavior even without STARTTLS, because the IMAP RFC does not really prohibit that. However, a meddler in the middle can use this to tamper with the state of Trojita.

This *could* also be escalated to a more severe issue. E.g. when an attacker injects a folder name with &quot;\r\n&lt;tag&gt; &lt;command&gt;&quot;, it could trick Trojita to execute attacker-controlled commands on the IMAP server after login.

The only thing preventing this is sanitization of folder names, but I am not sure if we should count on that...</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2050382</commentid>
    <comment_count>1</comment_count>
    <who name="Damian Poddebniak">93s4m32gd2ab8ax6</who>
    <bug_when>2021-08-02 13:25:55 +0000</bug_when>
    <thetext>Any update on this? To be clear: a network attacker can create new folders and tamper with local application state when STARTTLS is used.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2088201</commentid>
    <comment_count>2</comment_count>
    <who name="">Kartz</who>
    <bug_when>2021-12-26 12:34:50 +0000</bug_when>
    <thetext>Hi Damian,

I am going to work on it and to fix it. Pls give me some time.... :)

v/r
Andi</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2099618</commentid>
    <comment_count>3</comment_count>
    <who name="Jan Kundrát">jkt</who>
    <bug_when>2022-01-30 18:35:04 +0000</bug_when>
    <thetext>&gt; Trojita accepts LIST, LSUB, STATUS, ... untagges responses before STARTTLS
&gt; and incorporates them into local state.
&gt; 
&gt; I am not sure if this is already kind of a misbehavior even without
&gt; STARTTLS, because the IMAP RFC does not really prohibit that. However, a
&gt; meddler in the middle can use this to tamper with the state of Trojita.

Thanks for finding this, and sorry for not responding earlier. You&apos;re right, this can inject info about non-existing mailboxes (which will disappear after a reconnect), it can cause extra bandwidth consumption (due to syncing more than what is strictly needed to transfer), and it also has a Denial-of-Service potential (e.g., if the attacker pushes nonsense, the UID syncing code will ask the user to clear cache and refuse to proceed beforehand).

&gt; This *could* also be escalated to a more severe issue. E.g. when an attacker
&gt; injects a folder name with &quot;\r\n&lt;tag&gt; &lt;command&gt;&quot;, it could trick Trojita to
&gt; execute attacker-controlled commands on the IMAP server after login.
&gt; 
&gt; The only thing preventing this is sanitization of folder names, but I am not
&gt; sure if we should count on that...

This cannot be exploited like that. Since IMAP is a text-based protocol, there are rules on how to &quot;intervene&quot; user-controlled (or even attacker-controlled) strings with protocol commands. However, this is not specific to a possible side-channel injection due to STARTTLS. The real user can just as well create a mailbox which has a newline in its name, and the IMAP code must handle this properly. Let&apos;s not call this &quot;sanitization&quot;, please; it&apos;s a critical part of implementing a protocol. The STARTTLS vulnerability will only be relevant in this context if the attacker-controlled cache stored strings which are somehow escaped, and that is not the case.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2099625</commentid>
    <comment_count>4</comment_count>
    <who name="Bug Janitor Service">bug-janitor</who>
    <bug_when>2022-01-30 18:59:46 +0000</bug_when>
    <thetext>A possibly relevant merge request was started @ https://invent.kde.org/pim/trojita/-/merge_requests/4</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2099660</commentid>
    <comment_count>5</comment_count>
    <who name="Damian Poddebniak">93s4m32gd2ab8ax6</who>
    <bug_when>2022-01-30 20:46:58 +0000</bug_when>
    <thetext>Hey Jan,  thank you for working on this issue!

&gt; This cannot be exploited like that.

I know of at least one client where this is practically exploitable. However, I am not saying that it is possible in Trojita, though!

&gt; Since IMAP is a text-based protocol, there are rules on how to &quot;intervene&quot; user-controlled (or even attacker-controlled) strings with protocol commands. However, this is not specific to a possible side-channel injection due to STARTTLS. The real user can just as well create a mailbox which has a newline in its name, and the IMAP code must handle this properly. Let&apos;s not call this &quot;sanitization&quot;, please; it&apos;s a critical part of implementing a protocol.

I fully agree. Sanitization is not the correct term.

By the way, I know that you implemented the IMAP protocol very diligently in Trojita! ;-) Still, I also know IMAP very well and how complicated string handling is due to the many involved &quot;string types&quot; such as `tag`, `text`, `atom`, `astring`, `literal` ... (In fact, in my own IMAP implementation I was *so afraid* to forget to correctly encode some string in some place, that I wrapped all &quot;string types&quot; and use these wrappers throughout the whole library.¹)

&gt; The STARTTLS vulnerability will only be relevant in this context if the attacker-controlled cache stored strings which are somehow escaped, and that is not the case.

Not sure if I understand that. But it doesn&apos;t matter. If Trojita implements the IMAP protocol correctly and properly escapes folder names, it should not matter.

¹ https://github.com/duesee/imap-codec/blob/6bf1e5d0da45d576bd9ed4ddc0b3640da8e2ba80/src/types/mailbox.rs#L142</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2099663</commentid>
    <comment_count>6</comment_count>
    <who name="Jan Kundrát">jkt</who>
    <bug_when>2022-01-30 21:10:22 +0000</bug_when>
    <thetext>Yeah, in Trojita, the code is similar, see src/Imap/Parser/Command.h and src/Imap/Parser/Command.cpp and handling of Commands::PartOfCommand.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2099664</commentid>
    <comment_count>7</comment_count>
    <who name="Damian Poddebniak">93s4m32gd2ab8ax6</who>
    <bug_when>2022-01-30 21:21:43 +0000</bug_when>
    <thetext>Hehe, the code at

    https://github.com/KDE/trojita/blob/master/src/Imap/Parser/Command.cpp#L71

seems familiar :-)

`PartOfCommand` cannot contain a &quot;, right?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2099685</commentid>
    <comment_count>8</comment_count>
    <who name="Jan Kundrát">jkt</who>
    <bug_when>2022-01-30 23:27:44 +0000</bug_when>
    <thetext>Git commit 4953464742a65b401e5ea770a4bcb506c2f410d8 by Jan Kundrát.
Committed on 30/01/2022 at 23:24.
Pushed by jkt into branch &apos;bug-432353&apos;.

IMAP: ignore unsolicited LIST and STATUS before we&apos;re authenticated

These responses were combined with legit responses which arrive from the
real server later on, after having authenticated, and as a result, a
malicious active attacker on a network was able to inject extra fake
mailboxes to the GUI even when upgrading connection from plaintext to a
secure one via STARTTLS.

These two responses are the only ones which are handled by our pre-task
code directly in Model.cpp, which means that the state machine has to be
checked and messages which are received prior to authentication must be
rejected. Since this is really a pretty serious violation of the IMAP
state machine, let&apos;s not ignore them silently, and simply treat them as
any other unexpected response.
Change-Id: I9292fcb20215ebe4dbc7a103fc9403dfa97b258b
Reported-by: Damian Poddebniak
Co-authored-by: Espen Sandøy Hustad &lt;espen@ehustad.com&gt;

M  +6    -1    src/Imap/Model/Model.cpp
M  +108  -1    tests/Imap/test_Imap_Tasks_OpenConnection.cpp
M  +5    -0    tests/Imap/test_Imap_Tasks_OpenConnection.h

https://invent.kde.org/pim/trojita/commit/4953464742a65b401e5ea770a4bcb506c2f410d8</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2099686</commentid>
    <comment_count>9</comment_count>
    <who name="Jan Kundrát">jkt</who>
    <bug_when>2022-01-30 23:31:17 +0000</bug_when>
    <thetext>Looks like a hook changed this before this hit master. Let&apos;s wait a few days for a review.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2099889</commentid>
    <comment_count>10</comment_count>
    <who name="Bug Janitor Service">bug-janitor</who>
    <bug_when>2022-01-31 19:36:24 +0000</bug_when>
    <thetext>A possibly relevant merge request was started @ https://invent.kde.org/pim/trojita/-/merge_requests/6</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2099914</commentid>
    <comment_count>11</comment_count>
    <who name="Jan Kundrát">jkt</who>
    <bug_when>2022-01-31 21:11:10 +0000</bug_when>
    <thetext>Git commit 27573cf04802fc2fdeb7f7beace4612b22ea1942 by Jan Kundrát.
Committed on 31/01/2022 at 19:34.
Pushed by jkt into branch &apos;master&apos;.

IMAP: ignore unsolicited LIST and STATUS before we&apos;re authenticated

These responses were combined with legit responses which arrive from the
real server later on, after having authenticated, and as a result, a
malicious active attacker on a network was able to inject extra fake
mailboxes to the GUI even when upgrading connection from plaintext to a
secure one via STARTTLS.

These two responses are the only ones which are handled by our pre-task
code directly in Model.cpp, which means that the state machine has to be
checked and messages which are received prior to authentication must be
rejected. Since this is really a pretty serious violation of the IMAP
state machine, let&apos;s not ignore them silently, and simply treat them as
any other unexpected response.
Change-Id: I9292fcb20215ebe4dbc7a103fc9403dfa97b258b
Reported-by: Damian Poddebniak
Co-authored-by: Espen Sandøy Hustad &lt;espen@ehustad.com&gt;
Reviewed-by: Caspar Schutijser &lt;caspar@schutijser.com&gt;

M  +6    -1    src/Imap/Model/Model.cpp
M  +121  -1    tests/Imap/test_Imap_Tasks_OpenConnection.cpp
M  +9    -0    tests/Imap/test_Imap_Tasks_OpenConnection.h

https://invent.kde.org/pim/trojita/commit/27573cf04802fc2fdeb7f7beace4612b22ea1942</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>