From 946af205a1ebc04e04f4a840bd86424207905497 Mon Sep 17 00:00:00 2001 From: xusiyu Date: Tue, 6 Jan 2026 05:14:00 +0800 Subject: [PATCH] [mac] clear mode2 key only on mac destruction (#12243) This commit adjusts how the `MAC Mode2 key` is cleared to avoid invalidating it in certain runtime flows. In the previous behavior, when the OpenThread instance is initialized, Mac is constructed and sets the Mode2 key. If otInstanceErasePersistentInfo is called afterwards, the call chain reaches KeyManager::DestroyTemporaryKeys(), where the Mode2 key is cleared. However, there is no subsequent point where the Mode2 key is re-set. If Thread is then enabled and a frame using Key ID Mode 2 is received, the Mode2 key remains invalid, which can lead to incorrect behavior. To fix this, this commit removes the Mode2 key clearing from `KeyManager::DestroyTemporaryKeys()` and clears the Mode2 key in the Mac destructor. This ensures that the Mode2 key stays valid for the lifetime of the Mac (and thus the OpenThread instance) after initialization, and is only cleared when Mac is destroyed. --- src/core/mac/mac.hpp | 5 +++++ src/core/thread/key_manager.cpp | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/mac/mac.hpp b/src/core/mac/mac.hpp index 790823b73..7d7ca9083 100644 --- a/src/core/mac/mac.hpp +++ b/src/core/mac/mac.hpp @@ -134,6 +134,11 @@ public: */ explicit Mac(Instance &aInstance); + /** + * Clears the Mode2Key on destruction. + */ + ~Mac(void) { ClearMode2Key(); } + /** * Starts an IEEE 802.15.4 Active Scan. * diff --git a/src/core/thread/key_manager.cpp b/src/core/thread/key_manager.cpp index 68c851321..b29da08a3 100644 --- a/src/core/thread/key_manager.cpp +++ b/src/core/thread/key_manager.cpp @@ -693,7 +693,6 @@ void KeyManager::DestroyTemporaryKeys(void) mMleKey.Clear(); mKek.Clear(); Get().ClearMacKeys(); - Get().ClearMode2Key(); } void KeyManager::DestroyPersistentKeys(void) { Get().DestroyPersistentKeys(); }