From: Mateus Rodrigues de Morais Date: Tue, 5 Sep 2023 13:41:50 +0200 Subject: Fix byte ordering in iputil operations Previously, the byte ordering on all byte array operations in iputil were hard-coded to 'little'. This patch makes the byteorder parameter arch-aware by using sys.byteorder instead. Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/nvme-stas/+bug/2026878 Last-Update: 2023-09-04 Forwarded: not-needed --- staslib/iputil.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/staslib/iputil.py b/staslib/iputil.py index 9199a49..57f3100 100644 --- a/staslib/iputil.py +++ b/staslib/iputil.py @@ -8,6 +8,7 @@ '''A collection of IP address and network interface utilities''' +import sys import socket import logging import ipaddress @@ -27,14 +28,14 @@ RTATTR_SZ = 4 GETADDRCMD = ( # BEGIN: struct nlmsghdr b'\0' * 4 # nlmsg_len (placeholder - actual length calculated below) - + (RTM_GETADDR).to_bytes(2, byteorder='little', signed=False) # nlmsg_type - + (NLM_F_REQUEST | NLM_F_ROOT).to_bytes(2, byteorder='little', signed=False) # nlmsg_flags + + (RTM_GETADDR).to_bytes(2, byteorder=sys.byteorder, signed=False) # nlmsg_type + + (NLM_F_REQUEST | NLM_F_ROOT).to_bytes(2, byteorder=sys.byteorder, signed=False) # nlmsg_flags + b'\0' * 2 # nlmsg_seq + b'\0' * 2 # nlmsg_pid # END: struct nlmsghdr + b'\0' * 8 # struct ifaddrmsg ) -GETADDRCMD = len(GETADDRCMD).to_bytes(4, byteorder='little') + GETADDRCMD[4:] # nlmsg_len +GETADDRCMD = len(GETADDRCMD).to_bytes(4, byteorder=sys.byteorder) + GETADDRCMD[4:] # nlmsg_len # ****************************************************************************** @@ -85,25 +86,25 @@ def iface_of(src_addr): if nlmsg_idx >= len(nlmsg): nlmsg += sock.recv(8192) - nlmsg_type = int.from_bytes(nlmsg[nlmsg_idx + 4 : nlmsg_idx + 6], byteorder='little', signed=False) + nlmsg_type = int.from_bytes(nlmsg[nlmsg_idx + 4 : nlmsg_idx + 6], byteorder=sys.byteorder, signed=False) if nlmsg_type == NLMSG_DONE: break if nlmsg_type != RTM_NEWADDR: break - nlmsg_len = int.from_bytes(nlmsg[nlmsg_idx : nlmsg_idx + 4], byteorder='little', signed=False) + nlmsg_len = int.from_bytes(nlmsg[nlmsg_idx : nlmsg_idx + 4], byteorder=sys.byteorder, signed=False) if nlmsg_len % 4: # Is msg length not a multiple of 4? break ifaddrmsg_indx = nlmsg_idx + NLMSGHDR_SZ ifa_family = nlmsg[ifaddrmsg_indx] - ifa_index = int.from_bytes(nlmsg[ifaddrmsg_indx + 4 : ifaddrmsg_indx + 8], byteorder='little', signed=False) + ifa_index = int.from_bytes(nlmsg[ifaddrmsg_indx + 4 : ifaddrmsg_indx + 8], byteorder=sys.byteorder, signed=False) rtattr_indx = ifaddrmsg_indx + IFADDRMSG_SZ while rtattr_indx < (nlmsg_idx + nlmsg_len): - rta_len = int.from_bytes(nlmsg[rtattr_indx : rtattr_indx + 2], byteorder='little', signed=False) - rta_type = int.from_bytes(nlmsg[rtattr_indx + 2 : rtattr_indx + 4], byteorder='little', signed=False) + rta_len = int.from_bytes(nlmsg[rtattr_indx : rtattr_indx + 2], byteorder=sys.byteorder, signed=False) + rta_type = int.from_bytes(nlmsg[rtattr_indx + 2 : rtattr_indx + 4], byteorder=sys.byteorder, signed=False) if rta_type == IFLA_ADDRESS: data = nlmsg[rtattr_indx + RTATTR_SZ : rtattr_indx + rta_len] if _data_matches_ip(ifa_family, data, src_addr):