https://bugs.kde.org/show_bug.cgi?id=528748

            Bug ID: 528748
           Summary: Use-after-free when tearing down the EWS resource:
                    ~EwsSubscriptionManager touches an already destroyed
                    EwsSettings
    Classification: Frameworks and Libraries
           Product: Akonadi
      Version First GIT (master)
       Reported In:
          Platform: unspecified
                OS: Linux
            Status: REPORTED
          Severity: normal
          Priority: NOR
         Component: EWS Resource
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected]
  Target Milestone: ---

Environment
Akonadi / KDE PIM master (26.08.x), the akonadi_ews_resource agent, with
"Enable Server-Side Subscriptions" turned on (ServerSubscription=true; off by
default, ewsresource.kcfg). Observable on resource stop/restart, on agent exit
and on akonadi server shutdown while a server-side subscription is active.
Under ASAN it reports heap-use-after-free.

Steps to reproduce

Configure an EWS resource (Exchange URL, credentials).
Enable "Enable Server-Side Subscriptions" in the resource settings.
Wait until the resource has connected and created the subscription (a non-empty
EventSubscriptionId is stored in the settings).
Stop the resource: akonadictl stop or qdbus
org.freedesktop.Akonadi.Agent.akonadi_ews_resource_1 quit / restart the agent —
any exit during which doSetOnline(false) is not called.
Expected behaviour
On shutdown the subscription is cancelled on the server, the settings
(EventSubscriptionId/Watermark) are cleared and saved in the still-alive
EwsSettings object, and the process exits without memory errors.

Actual behaviour
EwsResource destroys its members in reverse declaration order:
QScopedPointer<EwsSettings> mSettings (ewsresource.h:181) deletes EwsSettings
before QScopedPointer<EwsSubscriptionManager> mSubManager (ewsresource.h:169).
~EwsResource was = default (ewsresource.cpp:131), so the manager was destroyed
only after the settings: ~EwsSubscriptionManager
(ewssubscriptionmanager.cpp:40-43) calls cancelSubscription()
(ewssubscriptionmanager.cpp:61-71), which reads
mSettings->eventSubscriptionId() (line 63), calls
setEventSubscriptionId()/setEventSubscriptionWatermark() (67-68) and
mSettings->save() (69) on the freed object. The manager holds a raw
EwsSettings* (ewssubscriptionmanager.h:86, from mSettings.data() in
ewsresource.cpp:219) — not a QPointer — so the pointer is never zeroed. Under
ASAN a heap-use-after-free; without it, memory corruption / a non-deterministic
crash on exit. Trigger condition: mSubManager was not reset earlier — only
doSetOnline(false) (ewsresource.cpp:1243-1249) and reloadConfig() (436-442) do
that, and akonadi's AgentBase::quit() does not call doSetOnline(false).

Proposed fix
Reset the manager in ~EwsResource: give the destructor a real body that calls
mSubManager.reset() with a comment that ~EwsSubscriptionManager dereferences
mSettings, so the manager is destroyed before any member, while the settings
are still alive; plus a comment at the mSubManager declaration noting the
dependency. Fix exists in a local branch, not pushed. Alternative (not
applied): declare mSettings above mSubManager — the order was left untouched,
since with the explicit reset it no longer matters.

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to