From 677e9f2542a9a6bdf31c3fa41d6aa1d987bb735a Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Wed, 23 Nov 2022 11:34:59 +0100 Subject: [PATCH 1/3] GH-85724: Ignore the MAC from the Touch Bar interface on macOS in uuid All Intel MacBook Pro devices with a Touch Bar have a network interface connected to a controller for that device that has the same MAC address for all MacBooks. Ignore these MAC adresses in the UUID library. --- Lib/test/test_uuid.py | 1 + Lib/uuid.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index 411eec0f406215..c6c4aa44d9734d 100755 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -830,6 +830,7 @@ def check_node(self, node, requires=None): print(hex, end=' ') self.assertTrue(0 < node < (1 << 48), "%s is not an RFC 4122 node ID" % hex) + self.assertNotEqual(node, int("ac:de:48:00:11:22".replace(":", ""), 16)) @unittest.skipUnless(_uuid._ifconfig_getnode in _uuid._GETTERS, "ifconfig is not used for introspection on this platform") diff --git a/Lib/uuid.py b/Lib/uuid.py index e863b631877f6d..3b9b54fbb8733a 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -403,9 +403,16 @@ def _get_command_stdout(command, *args): # # See https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local + def _is_universal(mac): return not (mac & (1 << 41)) +# MAC adresses that are blacklisted for finding a node id +_MAC_BLACKLIST={ + # (GH-85724) Hardcoded MAC adres for all Intel MacBook's with Touch Bar. + int("ac:de:48:00:11:22".replace(":", ""), 16), +} + def _find_mac_near_keyword(command, args, keywords, get_word_index): """Searches a command's output for a MAC address near a keyword. @@ -436,6 +443,8 @@ def _find_mac_near_keyword(command, args, keywords, get_word_index): # real MAC address pass else: + if mac in _MAC_BLACKLIST: + continue if _is_universal(mac): return mac first_local_mac = first_local_mac or mac @@ -498,6 +507,8 @@ def _find_mac_under_heading(command, args, heading): mac = _parse_mac(word) if mac is None: continue + if mac in _MAC_BLACKLIST: + continue if _is_universal(mac): return mac if first_local_mac is None: @@ -581,6 +592,18 @@ def _netbios_getnode(): _generate_time_safe = getattr(_uuid, "generate_time_safe", None) _UuidCreate = getattr(_uuid, "UuidCreate", None) _has_uuid_generate_time_safe = _uuid.has_uuid_generate_time_safe + + if sys.platform == "darwin" and _generate_time_safe is not None: + # On macOS check if generate_time_safe uses the MAC adddress + # of the Touch Bar on Intel Macbooks and ignore the function + # if it does because this is a single MAC adress for all + # devices. See GH-85724 + _x = _generate_time_safe()[0] + if _x.endswith(bytes.fromhex("ac:de:48:00:11:22".replace(":", ""))): + _has_uuid_generate_time_safe = False + _generate_time_safe = None + del _x + except ImportError: _uuid = None _generate_time_safe = None From d4685762fd4b7a519b9d762a69d29d5df14b92ff Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Thu, 24 Nov 2022 09:39:48 +0100 Subject: [PATCH 2/3] Various cleanups 1. Remove the "BLACKLIST" variable as it is both lazy naming not necessary 2. Be more explicit in tests 3. Add test that checks that the mac touchbar mac is actually ignored. --- Lib/test/test_uuid.py | 8 +++++++- Lib/uuid.py | 15 ++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index c6c4aa44d9734d..80fd5f213a6448 100755 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -805,10 +805,13 @@ def test_find_mac_near_keyword(self): data = ''' fake Link encap:UNSPEC hwaddr 00-00 cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 +en5 Link encap:UNSPEC HWaddr ac:de:48:00:11:22 eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab ''' # The above data will only be parsed properly on non-AIX unixes. + # The value for 'en5' is the single MAC address used by Touch Bar Mac laptops + # and should be ignored. with mock.patch.multiple(self.uuid, _MAC_DELIM=b':', _MAC_OMITS_LEADING_ZEROES=False, @@ -830,7 +833,10 @@ def check_node(self, node, requires=None): print(hex, end=' ') self.assertTrue(0 < node < (1 << 48), "%s is not an RFC 4122 node ID" % hex) - self.assertNotEqual(node, int("ac:de:48:00:11:22".replace(":", ""), 16)) + + # GH-85724: Ensure that the MAC address of the TouchBar interface + # on Intel Macbooks is not used in a UUID. + self.assertNotEqual(node, self.uuid._MACOS_TOUCHBAR_MAC_AS_INT) @unittest.skipUnless(_uuid._ifconfig_getnode in _uuid._GETTERS, "ifconfig is not used for introspection on this platform") diff --git a/Lib/uuid.py b/Lib/uuid.py index 3b9b54fbb8733a..4e7c69f65a20cc 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -407,12 +407,9 @@ def _get_command_stdout(command, *args): def _is_universal(mac): return not (mac & (1 << 41)) -# MAC adresses that are blacklisted for finding a node id -_MAC_BLACKLIST={ - # (GH-85724) Hardcoded MAC adres for all Intel MacBook's with Touch Bar. - int("ac:de:48:00:11:22".replace(":", ""), 16), -} - +# (GH-85724) Hardcoded MAC adres for all Intel MacBook's with Touch Bar. +_MACOS_TOUCHBAR_MAC="ac:de:48:00:11:22" +_MACOS_TOUCHBAR_MAC_AS_INT=int(_MACOS_TOUCHBAR_MAC.replace(":", ""), 16) def _find_mac_near_keyword(command, args, keywords, get_word_index): """Searches a command's output for a MAC address near a keyword. @@ -443,7 +440,7 @@ def _find_mac_near_keyword(command, args, keywords, get_word_index): # real MAC address pass else: - if mac in _MAC_BLACKLIST: + if mac == _MACOS_TOUCHBAR_MAC_AS_INT: continue if _is_universal(mac): return mac @@ -507,7 +504,7 @@ def _find_mac_under_heading(command, args, heading): mac = _parse_mac(word) if mac is None: continue - if mac in _MAC_BLACKLIST: + if mac == _MACOS_TOUCHBAR_MAC_AS_INT: continue if _is_universal(mac): return mac @@ -599,7 +596,7 @@ def _netbios_getnode(): # if it does because this is a single MAC adress for all # devices. See GH-85724 _x = _generate_time_safe()[0] - if _x.endswith(bytes.fromhex("ac:de:48:00:11:22".replace(":", ""))): + if _x.endswith(bytes.fromhex(_MACOS_TOUCHBAR_MAC.replace(":", ""))): _has_uuid_generate_time_safe = False _generate_time_safe = None del _x From 6788da0249acc74f62f4f01c89397e798729832e Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Thu, 24 Nov 2022 09:44:30 +0100 Subject: [PATCH 3/3] Fix typo --- Lib/uuid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/uuid.py b/Lib/uuid.py index 4e7c69f65a20cc..ca207b36a4d6a4 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -407,7 +407,7 @@ def _get_command_stdout(command, *args): def _is_universal(mac): return not (mac & (1 << 41)) -# (GH-85724) Hardcoded MAC adres for all Intel MacBook's with Touch Bar. +# (GH-85724) Hardcoded MAC address for all Intel MacBook's with Touch Bar. _MACOS_TOUCHBAR_MAC="ac:de:48:00:11:22" _MACOS_TOUCHBAR_MAC_AS_INT=int(_MACOS_TOUCHBAR_MAC.replace(":", ""), 16)