QMessageAuthenticationCode: fix result() non-re-entrancy

While QMessageAuthenticationCode is not copyable, result() is
nevertheless const, so a user could prepare a
QMessageAuthenticationCode object with setKey() and addData(), pass it
by const reference to two threads, which each just call result() on
it. This should be safe, but because result() performed lazy
evaluation without being internally synchronized, this would cause
data races.

Fix in the same was as b904de43a5 did
for QCryptographicHash. See there for a detailed discussion of the
solution.

Fixes: QTBUG-111347
Pick-to: 6.5 6.4 6.2 5.15
Change-Id: I1feb380973c480ad6268349a0a46ac471b9ca0f7
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: David Faure <david.faure@kdab.com>
bb10
Marc Mutz 2023-02-21 09:21:29 +01:00
parent 101e57d37c
commit 2449af142f
1 changed files with 8 additions and 0 deletions

View File

@ -1,8 +1,11 @@
// Copyright (C) 2023 The Qt Company Ltd.
// Copyright (C) 2013 Ruslan Nigmatullin <euroelessar@yandex.ru>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qmessageauthenticationcode.h"
#include "qvarlengtharray.h"
#include "qmutex.h"
#include "private/qlocking_p.h"
#include "qtcore-config_p.h"
@ -70,6 +73,7 @@ public:
QByteArray key;
QByteArray result;
QBasicMutex finalizeMutex;
QCryptographicHash messageHash;
QCryptographicHash::Algorithm method;
bool messageHashInited;
@ -77,7 +81,10 @@ public:
void initMessageHash();
void finalize();
// when not called from the static hash() function, this function needs to be
// called with finalizeMutex held:
void finalizeUnchecked();
// END functions that need to be called with finalizeMutex held
};
void QMessageAuthenticationCodePrivate::initMessageHash()
@ -217,6 +224,7 @@ QByteArray QMessageAuthenticationCode::result() const
void QMessageAuthenticationCodePrivate::finalize()
{
const auto lock = qt_scoped_lock(finalizeMutex);
if (!result.isEmpty())
return;
initMessageHash();