From d56b91dbd0c1bf6e545abed11f5fa2e392d233d9 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Sat, 14 Feb 2026 14:08:45 +0100 Subject: [PATCH] gamecenter: harden KAuth helper power profile setting --- src/gamecenter/gamecenterkauthhelper.cpp | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/gamecenter/gamecenterkauthhelper.cpp b/src/gamecenter/gamecenterkauthhelper.cpp index 4403704..c73c675 100644 --- a/src/gamecenter/gamecenterkauthhelper.cpp +++ b/src/gamecenter/gamecenterkauthhelper.cpp @@ -4,7 +4,9 @@ #include #include +#include #include +#include #include #include #include @@ -28,6 +30,14 @@ public Q_SLOTS: ActionReply setpowerprofile(const QVariantMap &args); }; +static QVariant unwrapDbusVariant(QVariant v) +{ + if (v.canConvert()) { + v = v.value().variant(); + } + return v; +} + static QString unwrapStringArg(const QVariantMap &args, const QString &key) { QVariant v = args.value(key); @@ -53,6 +63,12 @@ ActionReply GameCenterHelper::setpowerprofile(const QVariantMap &args) return reply; } + if (bus.interface() && !bus.interface()->isServiceRegistered(kPowerProfilesService)) { + ActionReply reply = ActionReply::HelperErrorReply(); + reply.setErrorDescription(QStringLiteral("power-profiles-daemon service not available")); + return reply; + } + QDBusInterface props(kPowerProfilesService, kPowerProfilesPath, QStringLiteral("org.freedesktop.DBus.Properties"), bus); if (!props.isValid()) { ActionReply reply = ActionReply::HelperErrorReply(); @@ -60,6 +76,44 @@ ActionReply GameCenterHelper::setpowerprofile(const QVariantMap &args) return reply; } + { + const QDBusReply profilesReply = props.call(QStringLiteral("Get"), kPowerProfilesInterface, QStringLiteral("Profiles")); + if (profilesReply.isValid()) { + QVariant v = unwrapDbusVariant(profilesReply.value()); + QVariantList list; + if (v.canConvert()) { + list = qdbus_cast(v.value()); + } else if (v.canConvert()) { + list = v.toList(); + } + + if (!list.isEmpty()) { + QStringList available; + available.reserve(list.size()); + for (const QVariant &item : list) { + QVariant mV = unwrapDbusVariant(item); + QVariantMap m; + if (mV.canConvert()) { + m = qdbus_cast(mV.value()); + } else if (mV.canConvert()) { + m = mV.toMap(); + } + const QString name = m.value(QStringLiteral("Profile")).toString(); + if (!name.isEmpty()) { + available.push_back(name); + } + } + + if (!available.isEmpty() && !available.contains(profile)) { + ActionReply reply = ActionReply::HelperErrorReply(); + reply.setErrorDescription(QStringLiteral("unsupported profile '%1'").arg(profile)); + reply.addData(QStringLiteral("available"), available); + return reply; + } + } + } + } + QDBusMessage msg = QDBusMessage::createMethodCall(kPowerProfilesService, kPowerProfilesPath, QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("Set")); msg.setArguments({kPowerProfilesInterface, QStringLiteral("ActiveProfile"), QVariant::fromValue(QDBusVariant(profile))});