Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usbip: Fix unsafe unaligned pointer usage

Fix unsafe unaligned pointer usage in usbip network interfaces. usbip tool
build fails with new gcc -Werror=address-of-packed-member checks.

usbip_network.c: In function ‘usbip_net_pack_usb_device’:
usbip_network.c:79:32: error: taking address of packed member of ‘struct usbip_usb_device’ may result in an unaligned pointer value [-Werror=address-of-packed-member]
79 | usbip_net_pack_uint32_t(pack, &udev->busnum);

Fix with minor changes to pass by value instead of by address.

Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Link: https://lore.kernel.org/r/20200109012416.2875-1-skhan@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Shuah Khan and committed by
Greg Kroah-Hartman
585c91f4 bc57ecbd

+27 -25
+24 -16
tools/usb/usbip/src/usbip_network.c
··· 50 50 info("using port %d (\"%s\")", usbip_port, usbip_port_string); 51 51 } 52 52 53 - void usbip_net_pack_uint32_t(int pack, uint32_t *num) 53 + uint32_t usbip_net_pack_uint32_t(int pack, uint32_t num) 54 54 { 55 55 uint32_t i; 56 56 57 57 if (pack) 58 - i = htonl(*num); 58 + i = htonl(num); 59 59 else 60 - i = ntohl(*num); 60 + i = ntohl(num); 61 61 62 - *num = i; 62 + return i; 63 63 } 64 64 65 - void usbip_net_pack_uint16_t(int pack, uint16_t *num) 65 + uint16_t usbip_net_pack_uint16_t(int pack, uint16_t num) 66 66 { 67 67 uint16_t i; 68 68 69 69 if (pack) 70 - i = htons(*num); 70 + i = htons(num); 71 71 else 72 - i = ntohs(*num); 72 + i = ntohs(num); 73 73 74 - *num = i; 74 + return i; 75 75 } 76 76 77 77 void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev) 78 78 { 79 - usbip_net_pack_uint32_t(pack, &udev->busnum); 80 - usbip_net_pack_uint32_t(pack, &udev->devnum); 81 - usbip_net_pack_uint32_t(pack, &udev->speed); 79 + udev->busnum = usbip_net_pack_uint32_t(pack, udev->busnum); 80 + udev->devnum = usbip_net_pack_uint32_t(pack, udev->devnum); 81 + udev->speed = usbip_net_pack_uint32_t(pack, udev->speed); 82 82 83 - usbip_net_pack_uint16_t(pack, &udev->idVendor); 84 - usbip_net_pack_uint16_t(pack, &udev->idProduct); 85 - usbip_net_pack_uint16_t(pack, &udev->bcdDevice); 83 + udev->idVendor = usbip_net_pack_uint16_t(pack, udev->idVendor); 84 + udev->idProduct = usbip_net_pack_uint16_t(pack, udev->idProduct); 85 + udev->bcdDevice = usbip_net_pack_uint16_t(pack, udev->bcdDevice); 86 86 } 87 87 88 88 void usbip_net_pack_usb_interface(int pack __attribute__((unused)), ··· 129 129 return usbip_net_xmit(sockfd, buff, bufflen, 1); 130 130 } 131 131 132 + static inline void usbip_net_pack_op_common(int pack, 133 + struct op_common *op_common) 134 + { 135 + op_common->version = usbip_net_pack_uint16_t(pack, op_common->version); 136 + op_common->code = usbip_net_pack_uint16_t(pack, op_common->code); 137 + op_common->status = usbip_net_pack_uint32_t(pack, op_common->status); 138 + } 139 + 132 140 int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status) 133 141 { 134 142 struct op_common op_common; ··· 148 140 op_common.code = code; 149 141 op_common.status = status; 150 142 151 - PACK_OP_COMMON(1, &op_common); 143 + usbip_net_pack_op_common(1, &op_common); 152 144 153 145 rc = usbip_net_send(sockfd, &op_common, sizeof(op_common)); 154 146 if (rc < 0) { ··· 172 164 goto err; 173 165 } 174 166 175 - PACK_OP_COMMON(0, &op_common); 167 + usbip_net_pack_op_common(0, &op_common); 176 168 177 169 if (op_common.version != USBIP_VERSION) { 178 170 err("USBIP Kernel and tool version mismatch: %d %d:",
+3 -9
tools/usb/usbip/src/usbip_network.h
··· 32 32 33 33 } __attribute__((packed)); 34 34 35 - #define PACK_OP_COMMON(pack, op_common) do {\ 36 - usbip_net_pack_uint16_t(pack, &(op_common)->version);\ 37 - usbip_net_pack_uint16_t(pack, &(op_common)->code);\ 38 - usbip_net_pack_uint32_t(pack, &(op_common)->status);\ 39 - } while (0) 40 - 41 35 /* ---------------------------------------------------------------------- */ 42 36 /* Dummy Code */ 43 37 #define OP_UNSPEC 0x00 ··· 157 163 } while (0) 158 164 159 165 #define PACK_OP_DEVLIST_REPLY(pack, reply) do {\ 160 - usbip_net_pack_uint32_t(pack, &(reply)->ndev);\ 166 + (reply)->ndev = usbip_net_pack_uint32_t(pack, (reply)->ndev);\ 161 167 } while (0) 162 168 163 - void usbip_net_pack_uint32_t(int pack, uint32_t *num); 164 - void usbip_net_pack_uint16_t(int pack, uint16_t *num); 169 + uint32_t usbip_net_pack_uint32_t(int pack, uint32_t num); 170 + uint16_t usbip_net_pack_uint16_t(int pack, uint16_t num); 165 171 void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev); 166 172 void usbip_net_pack_usb_interface(int pack, struct usbip_usb_interface *uinf); 167 173