Skip to content

Commit

Permalink
Darwin: Keep MTRCommissionableBrowser around until OnBleScanStopped (#…
Browse files Browse the repository at this point in the history
…33674)

* Darwin: Keep MTRCommissionableBrowser around until OnBleScanStopped

Because the BLE platform implementation uses a separate dispatch queue,
StopBleScan() does not synchrnously stop any further use of the current
BleScannerDelegate. Keep the MTRCommissionableBrowser that contains the
delegate alive until OnBleScanStopped to avoid a UAF.

* restyle

* Retain owner in Stop() instead of Start()

* More review comments
  • Loading branch information
ksperling-apple authored and pull[bot] committed Jun 13, 2024
1 parent 692c94b commit 4084036
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/darwin/Framework/CHIP/MTRCommissionableBrowser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ @implementation MTRCommissionableBrowserResult
#endif // CONFIG_NETWORK_LAYER_BLE
{
public:
#if CONFIG_NETWORK_LAYER_BLE
id mBleScannerDelegateOwner;
#endif // CONFIG_NETWORK_LAYER_BLE

CHIP_ERROR Start(id<MTRCommissionableBrowserDelegate> delegate, MTRDeviceController * controller, dispatch_queue_t queue)
{
assertChipStackLockedByCurrentThread();
Expand Down Expand Up @@ -90,7 +94,7 @@ CHIP_ERROR Start(id<MTRCommissionableBrowserDelegate> delegate, MTRDeviceControl
chip::Inet::InterfaceId::Null(), this);
}

CHIP_ERROR Stop()
CHIP_ERROR Stop(id owner)
{
assertChipStackLockedByCurrentThread();

Expand All @@ -109,7 +113,10 @@ CHIP_ERROR Stop()
mDiscoveredResults = nil;

#if CONFIG_NETWORK_LAYER_BLE
ReturnErrorOnFailure(PlatformMgrImpl().StopBleScan());
mBleScannerDelegateOwner = owner; // retain the owner until OnBleScanStopped is called
PlatformMgrImpl().StopBleScan(); // doesn't actually fail, and if it did we'd want to carry on regardless
#else
(void) owner;
#endif // CONFIG_NETWORK_LAYER_BLE

return ChipDnssdStopBrowse(this);
Expand Down Expand Up @@ -281,6 +288,7 @@ void OnBrowseStop(CHIP_ERROR error) override
void OnBleScanAdd(BLE_CONNECTION_OBJECT connObj, const ChipBLEDeviceIdentificationInfo & info) override
{
assertChipStackLockedByCurrentThread();
VerifyOrReturn(mDelegate != nil);

auto result = [[MTRCommissionableBrowserResult alloc] init];
result.instanceName = [NSString stringWithUTF8String:kBleKey];
Expand All @@ -303,6 +311,7 @@ void OnBleScanAdd(BLE_CONNECTION_OBJECT connObj, const ChipBLEDeviceIdentificati
void OnBleScanRemove(BLE_CONNECTION_OBJECT connObj) override
{
assertChipStackLockedByCurrentThread();
VerifyOrReturn(mDelegate != nil);

auto key = [NSString stringWithFormat:@"%@", connObj];
if ([mDiscoveredResults objectForKey:key] == nil) {
Expand All @@ -319,6 +328,12 @@ void OnBleScanRemove(BLE_CONNECTION_OBJECT connObj) override
[mDelegate controller:mController didFindCommissionableDevice:result];
});
}

void OnBleScanStopped() override
{
mBleScannerDelegateOwner = nil;
}

#endif // CONFIG_NETWORK_LAYER_BLE

private:
Expand Down Expand Up @@ -360,7 +375,7 @@ - (BOOL)start

- (BOOL)stop
{
VerifyOrReturnValue(CHIP_NO_ERROR == _browser.Stop(), NO);
VerifyOrReturnValue(CHIP_NO_ERROR == _browser.Stop(self), NO);
_delegate = nil;
_controller = nil;
_queue = nil;
Expand Down

0 comments on commit 4084036

Please sign in to comment.