Skip to content

Commit 35acbad

Browse files
committed
fix: extend IPC security validation to macOS firewall
1 parent 5d774e6 commit 35acbad

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

service/server/killswitch.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "qjsonarray.h"
1313
#include "version.h"
1414

15-
#ifdef Q_OS_LINUX
15+
#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
1616
static bool isValidIpOrCidr(const QString &value) {
1717
static const QRegularExpression re(
1818
QStringLiteral(R"(^(\d{1,3}\.){3}\d{1,3}(/\d{1,2})?$)"));
@@ -414,28 +414,40 @@ bool KillSwitch::enableKillSwitch(const QJsonObject &configStr, int vpnAdapterIn
414414
MacOSFirewall::setAnchorEnabled(QStringLiteral("000.allowLoopback"), true);
415415
MacOSFirewall::setAnchorEnabled(QStringLiteral("100.blockAll"), blockAll);
416416
MacOSFirewall::setAnchorEnabled(QStringLiteral("110.allowNets"), allowNets);
417-
MacOSFirewall::setAnchorTable(QStringLiteral("110.allowNets"), allowNets, QStringLiteral("allownets"), allownets);
417+
MacOSFirewall::setAnchorTable(QStringLiteral("110.allowNets"), allowNets, QStringLiteral("allownets"), filterIpList(allownets));
418418

419419
MacOSFirewall::setAnchorEnabled(QStringLiteral("120.blockNets"), blockNets);
420-
MacOSFirewall::setAnchorTable(QStringLiteral("120.blockNets"), blockNets, QStringLiteral("blocknets"), blocknets);
420+
MacOSFirewall::setAnchorTable(QStringLiteral("120.blockNets"), blockNets, QStringLiteral("blocknets"), filterIpList(blocknets));
421421
MacOSFirewall::setAnchorEnabled(QStringLiteral("200.allowVPN"), true);
422422
MacOSFirewall::setAnchorEnabled(QStringLiteral("250.blockIPv6"), true);
423423
MacOSFirewall::setAnchorEnabled(QStringLiteral("290.allowDHCP"), true);
424424
MacOSFirewall::setAnchorEnabled(QStringLiteral("300.allowLAN"), true);
425425

426426
QStringList dnsServers;
427-
dnsServers.append(configStr.value(amnezia::configKey::dns1).toString());
427+
const QString dns1 = configStr.value(amnezia::configKey::dns1).toString();
428+
if (isValidIpOrCidr(dns1))
429+
dnsServers.append(dns1);
430+
else if (!dns1.isEmpty())
431+
qWarning() << "IPC: rejected invalid dns1:" << dns1;
428432

429433
// We don't use secondary DNS if primary DNS is AmneziaDNS
430-
if (!configStr.value(amnezia::configKey::dns1).toString().contains(amnezia::protocols::dns::amneziaDnsIp)) {
431-
dnsServers.append(configStr.value(amnezia::configKey::dns2).toString());
434+
if (!dns1.contains(amnezia::protocols::dns::amneziaDnsIp)) {
435+
const QString dns2 = configStr.value(amnezia::configKey::dns2).toString();
436+
if (isValidIpOrCidr(dns2))
437+
dnsServers.append(dns2);
438+
else if (!dns2.isEmpty())
439+
qWarning() << "IPC: rejected invalid dns2:" << dns2;
432440
}
433-
441+
434442
for (auto dns : configStr.value(amnezia::configKey::allowedDnsServers).toArray()) {
435443
if (!dns.isString()) {
436444
break;
437445
}
438-
dnsServers.append(dns.toString());
446+
const QString dnsStr = dns.toString();
447+
if (isValidIpOrCidr(dnsStr))
448+
dnsServers.append(dnsStr);
449+
else if (!dnsStr.isEmpty())
450+
qWarning() << "IPC: rejected invalid allowedDnsServer:" << dnsStr;
439451
}
440452

441453
MacOSFirewall::setAnchorEnabled(QStringLiteral("310.blockDNS"), true);

0 commit comments

Comments
 (0)