Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix(bluetooth): address pairing review feedback

Nick Pisanidee9bea851cc
diff --git a/docs/CONTAINER.md b/docs/CONTAINER.md
index 74ab8a5..265db96 100644
--- a/docs/CONTAINER.md
+++ b/docs/CONTAINER.md
@@ -216,9 +216,9 @@ These environment switches are also usable outside Docker:
| `TETHER_NO_AUTOSTART` | `1` | Prevent CLI/client helpers from spawning `tetherd` |
| `TETHER_BLUEZ_SECURE_CONNECTIONS` | Boolean (`1`/`0`, `true`/`false`, `yes`/`no`, `on`/`off`) | Declare whether the host controller has Secure Connections enabled |
-Unset runtime switches preserve normal behavior. An unset capability declaration
-falls back to the local `btmgmt` probe, which may not be able to inspect the host
-controller from inside a container.
+Unset runtime switches preserve normal behavior. The local `btmgmt` result takes
+precedence when available; the capability declaration is used only when that probe
+cannot inspect the host controller from inside a container.
`XDG_DOWNLOAD_DIR` now takes precedence when nonempty and absolute; invalid or
empty overrides fall back to the existing GLib/HOME lookup.
diff --git a/src/core/include/tether/bluetooth/agent.hpp b/src/core/include/tether/bluetooth/agent.hpp
index cf6c7fa..8eb8f19 100644
--- a/src/core/include/tether/bluetooth/agent.hpp
+++ b/src/core/include/tether/bluetooth/agent.hpp
@@ -51,6 +51,11 @@ namespace tether::bluetooth {
void unregister_with_bluez();
void unexport_object();
+ // BlueZ may recreate an unpaired Device1 between attempts. Call this on
+ // the callback's GLib thread before retrying against the replacement.
+ void set_device_path(std::string device_path);
+ bool accepts_device_path(const std::string& device_path) const;
+
const std::string& path() const;
private:
diff --git a/src/core/include/tether/bluetooth/monitor.hpp b/src/core/include/tether/bluetooth/monitor.hpp
index ac6099f..ee6d84a 100644
--- a/src/core/include/tether/bluetooth/monitor.hpp
+++ b/src/core/include/tether/bluetooth/monitor.hpp
@@ -63,9 +63,9 @@ namespace tether::bluetooth {
extern BluezMonitor* g_bluez;
// BlueZ exposes Secure Connections only through the management interface.
- // Reads TETHER_BLUEZ_SECURE_CONNECTIONS when supplied by a container
- // orchestrator, otherwise runs btmgmt and returns no value if it fails or
- // exceeds the deadline. Public so the process boundary can be tested directly.
+ // Runs btmgmt first, then falls back to TETHER_BLUEZ_SECURE_CONNECTIONS when
+ // a container cannot use host management tools. Public so the process
+ // boundary can be tested directly.
std::optional<bool> probe_secure_connections(const std::string& adapter_id,
std::chrono::milliseconds timeout = std::chrono::seconds(1));
diff --git a/src/core/include/tether/bluetooth/pairing.hpp b/src/core/include/tether/bluetooth/pairing.hpp
index 63ea17d..d470c36 100644
--- a/src/core/include/tether/bluetooth/pairing.hpp
+++ b/src/core/include/tether/bluetooth/pairing.hpp
@@ -31,10 +31,6 @@ namespace tether::bluetooth {
// at all. Device1.Pair() involves no profile.
bool should_fall_back(AuthStrategy tried, bool paired, bool confirmation_failed);
- // Connect-first must select the BR/EDR profile path. Leaving BlueZ on
- // "last-used" can make it try LE first, which cannot induce Classic pairing.
- const char* preferred_bearer_for(AuthStrategy strategy);
-
// Whether a finished tether-dialog answered one way or another.
bool dialog_answered(int wait_status);
diff --git a/src/core/src/bluetooth/agent.cpp b/src/core/src/bluetooth/agent.cpp
index 094d798..458debc 100644
--- a/src/core/src/bluetooth/agent.cpp
+++ b/src/core/src/bluetooth/agent.cpp
@@ -100,6 +100,10 @@ namespace tether::bluetooth {
namespace {
+ bool accepts_device(const AgentState& state, const std::string& device_path) {
+ return device_path == state.device_path;
+ }
+
void reject(GDBusMethodInvocation* invocation, const char* why) {
g_dbus_method_invocation_return_dbus_error(invocation, ERROR_REJECTED, why);
}
@@ -131,7 +135,7 @@ namespace tether::bluetooth {
const gchar* device = nullptr;
g_variant_get_child(params, 0, "&o", &device);
const std::string device_path = device ? device : "";
- if (device_path != state->device_path) {
+ if (!accepts_device(*state, device_path)) {
debug::log(WARN, "bluetooth: agent refused {} for unexpected device {}", name, device_path);
reject(invocation, "Not the device being paired");
return;
@@ -198,6 +202,12 @@ namespace tether::bluetooth {
unexport_object();
}
+ void PairingAgent::set_device_path(std::string device_path) { state_->device_path = std::move(device_path); }
+
+ bool PairingAgent::accepts_device_path(const std::string& device_path) const {
+ return accepts_device(*state_, device_path);
+ }
+
const std::string& PairingAgent::path() const { return state_->path; }
bool PairingAgent::export_object() {
diff --git a/src/core/src/bluetooth/monitor.cpp b/src/core/src/bluetooth/monitor.cpp
index d00aed0..59b8172 100644
--- a/src/core/src/bluetooth/monitor.cpp
+++ b/src/core/src/bluetooth/monitor.cpp
@@ -190,9 +190,8 @@ namespace tether::bluetooth {
std::optional<bool> probe_secure_connections(const std::string& adapter_id, std::chrono::milliseconds timeout) {
if (adapter_id.empty())
return std::nullopt;
- if (const auto configured = environment_flag("TETHER_BLUEZ_SECURE_CONNECTIONS"))
- return configured;
+ const auto configured = [] { return environment_flag("TETHER_BLUEZ_SECURE_CONNECTIONS"); };
std::array<gchar*, 5> argv = {
const_cast<gchar*>("btmgmt"),
const_cast<gchar*>("--index"),
@@ -224,7 +223,7 @@ namespace tether::bluetooth {
adapter_id,
error ? error->message : "unknown error");
g_clear_error(&error);
- return std::nullopt;
+ return configured();
}
if (input_fd >= 0)
close(input_fd);
@@ -295,18 +294,18 @@ namespace tether::bluetooth {
debug::log(WARN, "bluetooth: btmgmt info for {} timed out after {}ms", adapter_id, elapsed.count());
close(output_fd);
g_spawn_close_pid(child);
- return std::nullopt;
+ return configured();
}
drain_output();
close(output_fd);
g_spawn_close_pid(child);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
- return std::nullopt;
+ return configured();
const size_t settings = output.find("current settings:");
if (settings == std::string::npos)
- return std::nullopt;
+ return configured();
const size_t line_end = output.find('\n', settings);
const std::string line = output.substr(settings, line_end - settings);
return line.find("secure-conn") != std::string::npos;
diff --git a/src/core/src/bluetooth/pairing.cpp b/src/core/src/bluetooth/pairing.cpp
index d56e126..cf4bc44 100644
--- a/src/core/src/bluetooth/pairing.cpp
+++ b/src/core/src/bluetooth/pairing.cpp
@@ -229,7 +229,7 @@ namespace tether::bluetooth {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(DISCOVERY_TIMEOUT_SECONDS);
bool found = false;
while (std::chrono::steady_clock::now() < deadline) {
- if (lookup_live(conn, address, out)) {
+ if (lookup(monitor, address, out)) {
found = true;
break;
}
@@ -579,10 +579,6 @@ namespace tether::bluetooth {
return tried == AuthStrategy::ConnectFirst && !paired && !confirmation_failed;
}
- const char* preferred_bearer_for(AuthStrategy strategy) {
- return strategy == AuthStrategy::ConnectFirst ? "bredr" : nullptr;
- }
-
PairResult pair_device(BluezMonitor& monitor,
const std::string& address,
AuthStrategy strategy,
@@ -692,16 +688,18 @@ namespace tether::bluetooth {
};
auto refresh_device = [&] {
- if (lookup_live(conn, result.device_address, device)) {
+ auto accept_device = [&] {
result.device_path = device.path;
+ monitor.invoke_sync([&] { agent.set_device_path(device.path); });
return true;
- }
+ };
+
+ if (lookup_live(conn, result.device_address, device))
+ return accept_device();
notify(progress, "rediscovering", result.device_address);
- if (discover(monitor, conn, result.device_address, device)) {
- result.device_path = device.path;
- return true;
- }
+ if (discover(monitor, conn, result.device_address, device))
+ return accept_device();
err = "Device1 disappeared and could not be rediscovered";
return false;
@@ -717,17 +715,6 @@ namespace tether::bluetooth {
return false;
}
- if (const char* bearer = preferred_bearer_for(how)) {
- std::string bearer_err;
- if (!set_property(conn,
- device.path,
- IFACE_DEVICE,
- "PreferredBearer",
- g_variant_new_string(bearer),
- &bearer_err))
- debug::log(WARN, "bluetooth: could not select {} before pairing: {}", bearer, bearer_err);
- }
-
if (how == AuthStrategy::ConnectFirst) {
notify(progress, "connecting", display_name);
initiated = call_device(conn, device.path, "Connect", PAIR_TIMEOUT_SECONDS * 1000, err);
diff --git a/test/test_bluetooth_monitor.cpp b/test/test_bluetooth_monitor.cpp
index c3f106d..6ee6e81 100644
--- a/test/test_bluetooth_monitor.cpp
+++ b/test/test_bluetooth_monitor.cpp
@@ -77,14 +77,29 @@ namespace {
} // namespace
-TEST(ContainerCapabilityHints, ReportsHostSecureConnectionsWithoutManagementTools) {
+TEST(ContainerCapabilityHints, ReportsHostSecureConnectionsWhenManagementProbeIsUnavailable) {
{
ScopedEnvironment secure("TETHER_BLUEZ_SECURE_CONNECTIONS", "true");
- EXPECT_EQ(probe_secure_connections("hci0", 1ms), true);
+ FakeBtmgmt fake("exit 1\n");
+ EXPECT_EQ(probe_secure_connections("hci0", 250ms), true);
}
{
ScopedEnvironment secure("TETHER_BLUEZ_SECURE_CONNECTIONS", "false");
- EXPECT_EQ(probe_secure_connections("hci0", 1ms), false);
+ FakeBtmgmt fake("exit 1\n");
+ EXPECT_EQ(probe_secure_connections("hci0", 250ms), false);
+ }
+}
+
+TEST(ContainerCapabilityHints, LocalManagementProbeTakesPrecedence) {
+ {
+ ScopedEnvironment secure("TETHER_BLUEZ_SECURE_CONNECTIONS", "true");
+ FakeBtmgmt fake("printf 'current settings: powered bondable\\n'\n");
+ EXPECT_EQ(probe_secure_connections("hci0", 250ms), false);
+ }
+ {
+ ScopedEnvironment secure("TETHER_BLUEZ_SECURE_CONNECTIONS", "false");
+ FakeBtmgmt fake("printf 'current settings: powered secure-conn bondable\\n'\n");
+ EXPECT_EQ(probe_secure_connections("hci0", 250ms), true);
}
}
diff --git a/test/test_bluetooth_pairing.cpp b/test/test_bluetooth_pairing.cpp
index 7cf8164..5887f36 100644
--- a/test/test_bluetooth_pairing.cpp
+++ b/test/test_bluetooth_pairing.cpp
@@ -59,6 +59,16 @@ TEST(AgentPolicy, CallsEnabledStillRefusesEverythingElse) {
EXPECT_FALSE(is_authorized_service("0000111e-0000-1000-8000-00805f9b34fbb", true));
}
+TEST(AgentPolicy, FollowsARecreatedDeviceObject) {
+ PairingAgent agent(nullptr, "/org/bluez/hci0/dev_old", {});
+ EXPECT_TRUE(agent.accepts_device_path("/org/bluez/hci0/dev_old"));
+ EXPECT_FALSE(agent.accepts_device_path("/org/bluez/hci0/dev_new"));
+
+ agent.set_device_path("/org/bluez/hci0/dev_new");
+ EXPECT_FALSE(agent.accepts_device_path("/org/bluez/hci0/dev_old"));
+ EXPECT_TRUE(agent.accepts_device_path("/org/bluez/hci0/dev_new"));
+}
+
// iOS renders the comparison with leading zeros. Dropping them shows the user a
// code that does not match their phone, which reads as a failed pairing.
TEST(Passkey, FormatsSixDigitsWithLeadingZeros) {
@@ -145,11 +155,6 @@ TEST(BluetoothConfig, SurvivesCorruptFile) {
// Connect-first induces authentication only as a side effect of a profile
// connect. A phone that refuses that profile never starts pairing at all, so the
// transaction is retried as an explicit Device1.Pair() -- see issue #49.
-TEST(PairingBearer, ConnectFirstPinsTheClassicTransport) {
- EXPECT_STREQ(preferred_bearer_for(AuthStrategy::ConnectFirst), "bredr");
- EXPECT_EQ(preferred_bearer_for(AuthStrategy::ExplicitPair), nullptr);
-}
-
TEST(FallbackPolicy, RetriesARefusedConnectFirst) {
EXPECT_TRUE(should_fall_back(AuthStrategy::ConnectFirst, /*paired=*/false, /*user_rejected=*/false));
}