Blame SOURCES/0012-goodix-moc-Fix-several-places-where-the-plugin-code-.patch

c5a379
From e80f277f4c268d69c162123bc8cbb1819224cea2 Mon Sep 17 00:00:00 2001
c5a379
From: Richard Hughes <richard@hughsie.com>
c5a379
Date: Wed, 10 Feb 2021 13:22:59 +0000
c5a379
Subject: [PATCH 12/12] goodix-moc: Fix several places where the plugin code
c5a379
 might crash
c5a379
c5a379
Fixes https://github.com/fwupd/fwupd/issues/2850
c5a379
---
c5a379
 plugins/goodix-moc/fu-goodixmoc-common.c |  83 ----------------
c5a379
 plugins/goodix-moc/fu-goodixmoc-common.h |  19 +---
c5a379
 plugins/goodix-moc/fu-goodixmoc-device.c | 120 +++++++++++++----------
c5a379
 plugins/goodix-moc/meson.build           |   1 -
c5a379
 4 files changed, 72 insertions(+), 151 deletions(-)
c5a379
 delete mode 100644 plugins/goodix-moc/fu-goodixmoc-common.c
c5a379
c5a379
diff --git plugins/goodix-moc/fu-goodixmoc-common.c plugins/goodix-moc/fu-goodixmoc-common.c
c5a379
deleted file mode 100644
c5a379
index 7c81434d..00000000
c5a379
--- plugins/goodix-moc/fu-goodixmoc-common.c
c5a379
+++ /dev/null
c5a379
@@ -1,83 +0,0 @@
c5a379
-/*
c5a379
- * Copyright (C) 2016 Richard Hughes <richard@hughsie.com>
c5a379
- * Copyright (C) 2020 boger wang <boger@goodix.com>
c5a379
- *
c5a379
- * SPDX-License-Identifier: LGPL-2.1+
c5a379
- */
c5a379
-
c5a379
-#include "config.h"
c5a379
-
c5a379
-#include <fwupd.h>
c5a379
-#include <string.h>
c5a379
-
c5a379
-#include "fu-common.h"
c5a379
-#include "fu-goodixmoc-common.h"
c5a379
-
c5a379
-void
c5a379
-fu_goodixmoc_build_header (GxfpPkgHeader *pheader,
c5a379
-			   guint16	  len,
c5a379
-			   guint8	  cmd0,
c5a379
-			   guint8	  cmd1,
c5a379
-			   GxPkgType	  type)
c5a379
-{
c5a379
-	static guint8 dummy_seq = 0;
c5a379
-
c5a379
-	g_return_if_fail (pheader != NULL);
c5a379
-
c5a379
-	pheader->cmd0 = (cmd0);
c5a379
-	pheader->cmd1 = (cmd1);
c5a379
-	pheader->pkg_flag = (guint8)type;
c5a379
-	pheader->reserved = dummy_seq++;
c5a379
-	pheader->len = len + GX_SIZE_CRC32;
c5a379
-	pheader->crc8 = fu_common_crc8 ((guint8 *)pheader, 6);
c5a379
-	pheader->rev_crc8 = ~pheader->crc8;
c5a379
-}
c5a379
-
c5a379
-gboolean
c5a379
-fu_goodixmoc_parse_header (guint8 *buf, guint32 bufsz,
c5a379
-			   GxfpPkgHeader *pheader, GError **error)
c5a379
-{
c5a379
-	g_return_val_if_fail (buf != NULL, FALSE);
c5a379
-	g_return_val_if_fail (pheader != NULL, FALSE);
c5a379
-
c5a379
-	if (!fu_memcpy_safe ((guint8 *) &pheader, sizeof(*pheader), 0x0,	/* dst */
c5a379
-			     buf, bufsz, 0x01,					/* src */
c5a379
-			     sizeof(*pheader), error))
c5a379
-		return FALSE;
c5a379
-	memcpy (pheader, buf, sizeof(*pheader));
c5a379
-	pheader->len = GUINT16_FROM_LE(*(buf + 4));
c5a379
-	pheader->len -= GX_SIZE_CRC32;
c5a379
-	return TRUE;
c5a379
-}
c5a379
-
c5a379
-gboolean
c5a379
-fu_goodixmoc_parse_body (guint8 cmd, guint8 *buf, guint32 bufsz,
c5a379
-			 GxfpCmdResp *presp, GError **error)
c5a379
-{
c5a379
-	g_return_val_if_fail (buf != NULL, FALSE);
c5a379
-	g_return_val_if_fail (presp != NULL, FALSE);
c5a379
-
c5a379
-	presp->result = buf[0];
c5a379
-	switch (cmd) {
c5a379
-	case GX_CMD_ACK:
c5a379
-		if (bufsz == 0) {
c5a379
-			g_set_error_literal (error,
c5a379
-					     FWUPD_ERROR,
c5a379
-					     FWUPD_ERROR_INTERNAL,
c5a379
-					     "invalid bufsz");
c5a379
-			return FALSE;
c5a379
-		}
c5a379
-		presp->ack_msg.cmd = buf[1];
c5a379
-		break;
c5a379
-	case GX_CMD_VERSION:
c5a379
-		if (!fu_memcpy_safe ((guint8 *) &presp->version_info,
c5a379
-				     sizeof(presp->version_info), 0x0,		/* dst */
c5a379
-				     buf, bufsz, 0x01,				/* src */
c5a379
-				     sizeof(GxfpVersiomInfo), error))
c5a379
-			return FALSE;
c5a379
-		break;
c5a379
-	default:
c5a379
-		break;
c5a379
-	}
c5a379
-	return TRUE;
c5a379
-}
c5a379
diff --git plugins/goodix-moc/fu-goodixmoc-common.h plugins/goodix-moc/fu-goodixmoc-common.h
c5a379
index 4bbdc0c8..c4b69954 100644
c5a379
--- plugins/goodix-moc/fu-goodixmoc-common.h
c5a379
+++ plugins/goodix-moc/fu-goodixmoc-common.h
c5a379
@@ -35,7 +35,7 @@ typedef struct {
c5a379
 	guint8		 protocol[8];
c5a379
 	guint8		 flashVersion[8];
c5a379
 	guint8		 reserved[62];
c5a379
-} GxfpVersiomInfo;
c5a379
+} GxfpVersionInfo;
c5a379
 
c5a379
 typedef struct {
c5a379
 	guint8		 cmd;
c5a379
@@ -46,7 +46,7 @@ typedef struct {
c5a379
 	guint8		 result;
c5a379
 	union {
c5a379
 		GxfpAckMsg	ack_msg;
c5a379
-		GxfpVersiomInfo version_info;
c5a379
+		GxfpVersionInfo version_info;
c5a379
 	};
c5a379
 } GxfpCmdResp;
c5a379
 
c5a379
@@ -64,18 +64,3 @@ typedef struct __attribute__((__packed__)) {
c5a379
 	guint8		 crc8;
c5a379
 	guint8		 rev_crc8;
c5a379
 } GxfpPkgHeader;
c5a379
-
c5a379
-void		 fu_goodixmoc_build_header	(GxfpPkgHeader	*pheader,
c5a379
-						 guint16	 len,
c5a379
-						 guint8		 cmd0,
c5a379
-						 guint8		 cmd1,
c5a379
-						 GxPkgType	 type);
c5a379
-gboolean	 fu_goodixmoc_parse_header	(guint8		*buf,
c5a379
-						 guint32	 bufsz,
c5a379
-						 GxfpPkgHeader	*pheader,
c5a379
-						 GError		**error);
c5a379
-gboolean	 fu_goodixmoc_parse_body	(guint8		 cmd,
c5a379
-						 guint8		*buf,
c5a379
-						 guint32	 bufsz,
c5a379
-						 GxfpCmdResp	*presp,
c5a379
-						 GError		**error);
c5a379
diff --git plugins/goodix-moc/fu-goodixmoc-device.c plugins/goodix-moc/fu-goodixmoc-device.c
c5a379
index f216aec7..3d359dab 100644
c5a379
--- plugins/goodix-moc/fu-goodixmoc-device.c
c5a379
+++ plugins/goodix-moc/fu-goodixmoc-device.c
c5a379
@@ -14,6 +14,7 @@
c5a379
 
c5a379
 struct _FuGoodixMocDevice {
c5a379
 	FuUsbDevice	parent_instance;
c5a379
+	guint8		dummy_seq;
c5a379
 };
c5a379
 
c5a379
 G_DEFINE_TYPE (FuGoodixMocDevice, fu_goodixmoc_device, FU_TYPE_USB_DEVICE)
c5a379
@@ -27,26 +28,34 @@ G_DEFINE_TYPE (FuGoodixMocDevice, fu_goodixmoc_device, FU_TYPE_USB_DEVICE)
c5a379
 #define GX_FLASH_TRANSFER_BLOCK_SIZE	1000	/* 1000 */
c5a379
 
c5a379
 static gboolean
c5a379
-goodixmoc_device_cmd_send (GUsbDevice *usbdevice,
c5a379
+goodixmoc_device_cmd_send (FuGoodixMocDevice *self,
c5a379
 			   guint8      cmd0,
c5a379
 			   guint8      cmd1,
c5a379
 			   GxPkgType   type,
c5a379
 			   GByteArray *req,
c5a379
 			   GError    **error)
c5a379
 {
c5a379
-	GxfpPkgHeader header = { 0 };
c5a379
-	guint32 crc_actual = 0;
c5a379
+	GUsbDevice *usb_device = fu_usb_device_get_dev (FU_USB_DEVICE (self));
c5a379
+	guint32 crc_all = 0;
c5a379
+	guint32 crc_hdr = 0;
c5a379
 	gsize actual_len = 0;
c5a379
 	g_autoptr(GByteArray) buf = g_byte_array_new ();
c5a379
 
c5a379
-	fu_goodixmoc_build_header (&header, req->len, cmd0, cmd1, type);
c5a379
-	g_byte_array_append (buf, (guint8 *)&header, sizeof(header));
c5a379
+	/* build header */
c5a379
+	fu_byte_array_append_uint8 (buf, cmd0);
c5a379
+	fu_byte_array_append_uint8 (buf, cmd1);
c5a379
+	fu_byte_array_append_uint8 (buf, type);			/* pkg_flag */
c5a379
+	fu_byte_array_append_uint8 (buf, self->dummy_seq++);	/* reserved */
c5a379
+	fu_byte_array_append_uint16 (buf, req->len + GX_SIZE_CRC32, G_LITTLE_ENDIAN);
c5a379
+	crc_hdr = fu_common_crc8 (buf->data, buf->len);
c5a379
+	fu_byte_array_append_uint8 (buf, crc_hdr);
c5a379
+	fu_byte_array_append_uint8 (buf, ~crc_hdr);
c5a379
 	g_byte_array_append (buf, req->data, req->len);
c5a379
-	crc_actual = fu_common_crc32 (buf->data, sizeof(header) + req->len);
c5a379
-	fu_byte_array_append_uint32 (buf, crc_actual, G_LITTLE_ENDIAN);
c5a379
+	crc_all = fu_common_crc32 (buf->data, buf->len);
c5a379
+	fu_byte_array_append_uint32 (buf, crc_all, G_LITTLE_ENDIAN);
c5a379
 
c5a379
 	/* send zero length package */
c5a379
-	if (!g_usb_device_bulk_transfer (usbdevice,
c5a379
+	if (!g_usb_device_bulk_transfer (usb_device,
c5a379
 					 GX_USB_BULK_EP_OUT,
c5a379
 					 NULL,
c5a379
 					 0,
c5a379
@@ -62,7 +71,7 @@ goodixmoc_device_cmd_send (GUsbDevice *usbdevice,
c5a379
 	}
c5a379
 
c5a379
 	/* send data */
c5a379
-	if (!g_usb_device_bulk_transfer (usbdevice,
c5a379
+	if (!g_usb_device_bulk_transfer (usb_device,
c5a379
 					 GX_USB_BULK_EP_OUT,
c5a379
 					 buf->data,
c5a379
 					 buf->len,
c5a379
@@ -84,12 +93,12 @@ goodixmoc_device_cmd_send (GUsbDevice *usbdevice,
c5a379
 }
c5a379
 
c5a379
 static gboolean
c5a379
-goodixmoc_device_cmd_recv (GUsbDevice  *usbdevice,
c5a379
+goodixmoc_device_cmd_recv (FuGoodixMocDevice *self,
c5a379
 			   GxfpCmdResp *presponse,
c5a379
 			   gboolean     data_reply,
c5a379
 			   GError     **error)
c5a379
 {
c5a379
-	GxfpPkgHeader header = { 0 };
c5a379
+	GUsbDevice *usb_device = fu_usb_device_get_dev (FU_USB_DEVICE (self));
c5a379
 	guint32 crc_actual = 0;
c5a379
 	guint32 crc_calculated = 0;
c5a379
 	gsize actual_len = 0;
c5a379
@@ -102,9 +111,11 @@ goodixmoc_device_cmd_recv (GUsbDevice  *usbdevice,
c5a379
 	* | zlp | ack | zlp | data |
c5a379
 	*/
c5a379
 	while (1) {
c5a379
+		guint16 header_len = 0x0;
c5a379
+		guint8 header_cmd0 = 0x0;
c5a379
 		g_autoptr(GByteArray) reply = g_byte_array_new ();
c5a379
 		fu_byte_array_set_size (reply, GX_FLASH_TRANSFER_BLOCK_SIZE);
c5a379
-		if (!g_usb_device_bulk_transfer (usbdevice,
c5a379
+		if (!g_usb_device_bulk_transfer (usb_device,
c5a379
 						 GX_USB_BULK_EP_IN,
c5a379
 						 reply->data,
c5a379
 						 reply->len,
c5a379
@@ -125,12 +136,14 @@ goodixmoc_device_cmd_recv (GUsbDevice  *usbdevice,
c5a379
 		}
c5a379
 
c5a379
 		/* parse package header */
c5a379
-		if (!fu_goodixmoc_parse_header (reply->data,
c5a379
-						actual_len,
c5a379
-						&header,
c5a379
-						error))
c5a379
+		if (!fu_common_read_uint8_safe (reply->data, reply->len, 0x0,
c5a379
+						&header_cmd0, error))
c5a379
+			return FALSE;
c5a379
+		if (!fu_common_read_uint16_safe	(reply->data, reply->len, 0x4,
c5a379
+						 &header_len, G_LITTLE_ENDIAN,
c5a379
+						 error))
c5a379
 			return FALSE;
c5a379
-		offset = sizeof(header) + header.len;
c5a379
+		offset = sizeof(GxfpPkgHeader) + header_len - GX_SIZE_CRC32;
c5a379
 		crc_actual = fu_common_crc32 (reply->data, offset);
c5a379
 		if (!fu_common_read_uint32_safe	(reply->data,
c5a379
 						 reply->len,
c5a379
@@ -149,15 +162,33 @@ goodixmoc_device_cmd_recv (GUsbDevice  *usbdevice,
c5a379
 		}
c5a379
 
c5a379
 		/* parse package data */
c5a379
-		if (!fu_goodixmoc_parse_body (header.cmd0,
c5a379
-					      reply->data + sizeof(header),
c5a379
-					      header.len,
c5a379
-					      presponse,
c5a379
-					      error))
c5a379
+		if (!fu_common_read_uint8_safe (reply->data, reply->len,
c5a379
+						sizeof(GxfpPkgHeader) + 0x00,
c5a379
+						&presponse->result, error))
c5a379
 			return FALSE;
c5a379
+		if (header_cmd0 == GX_CMD_ACK) {
c5a379
+			if (header_len == 0) {
c5a379
+				g_set_error_literal (error,
c5a379
+						     FWUPD_ERROR,
c5a379
+						     FWUPD_ERROR_INTERNAL,
c5a379
+						     "invalid bufsz");
c5a379
+				return FALSE;
c5a379
+			}
c5a379
+			if (!fu_common_read_uint8_safe (reply->data, reply->len,
c5a379
+							sizeof(GxfpPkgHeader) + 0x01,
c5a379
+							&presponse->ack_msg.cmd, error))
c5a379
+				return FALSE;
c5a379
+		} else if (header_cmd0 == GX_CMD_VERSION) {
c5a379
+			if (!fu_memcpy_safe ((guint8 *) &presponse->version_info,
c5a379
+					     sizeof(presponse->version_info), 0x0,	/* dst */
c5a379
+					     reply->data, reply->len,
c5a379
+					     sizeof(GxfpPkgHeader) + 0x01,		/* src */
c5a379
+					     sizeof(GxfpVersionInfo), error))
c5a379
+				return FALSE;
c5a379
+		}
c5a379
 
c5a379
 		/* continue after ack received */
c5a379
-		if (header.cmd0 == GX_CMD_ACK && data_reply)
c5a379
+		if (header_cmd0 == GX_CMD_ACK && data_reply)
c5a379
 			continue;
c5a379
 		break;
c5a379
 	}
c5a379
@@ -176,36 +207,27 @@ fu_goodixmoc_device_cmd_xfer (FuGoodixMocDevice	*device,
c5a379
 			      gboolean		 data_reply,
c5a379
 			      GError		**error)
c5a379
 {
c5a379
-	GUsbDevice *usb_device = fu_usb_device_get_dev (FU_USB_DEVICE(device));
c5a379
-	if (!goodixmoc_device_cmd_send (usb_device, cmd0, cmd1, type, req, error))
c5a379
+	FuGoodixMocDevice *self = FU_GOODIXMOC_DEVICE(device);
c5a379
+	if (!goodixmoc_device_cmd_send (self, cmd0, cmd1, type, req, error))
c5a379
 		return FALSE;
c5a379
-	return goodixmoc_device_cmd_recv (usb_device, presponse, data_reply, error);
c5a379
+	return goodixmoc_device_cmd_recv (self, presponse, data_reply, error);
c5a379
 }
c5a379
 
c5a379
-static gchar *
c5a379
-fu_goodixmoc_device_get_version (FuGoodixMocDevice *self, GError **error)
c5a379
+static gboolean
c5a379
+fu_goodixmoc_device_setup_version (FuGoodixMocDevice *self, GError **error)
c5a379
 {
c5a379
 	GxfpCmdResp rsp = { 0 };
c5a379
-	gchar ver[9] = { 0 };
c5a379
-	guint8 dummy = 0;
c5a379
+	g_autofree gchar *version = NULL;
c5a379
 	g_autoptr(GByteArray) req = g_byte_array_new ();
c5a379
 
c5a379
-	fu_byte_array_append_uint8 (req, dummy);
c5a379
+	fu_byte_array_append_uint8 (req, 0);	/* dummy */
c5a379
 	if (!fu_goodixmoc_device_cmd_xfer (self, GX_CMD_VERSION, GX_CMD1_DEFAULT,
c5a379
-					  GX_PKG_TYPE_EOP,
c5a379
-					  req,
c5a379
-					  &rsp,
c5a379
-					  TRUE,
c5a379
-					  error))
c5a379
-		return NULL;
c5a379
-	if (!fu_memcpy_safe ((guint8 *) ver, sizeof(ver), 0x0,
c5a379
-			      rsp.version_info.fwversion,
c5a379
-			      sizeof(rsp.version_info.fwversion),
c5a379
-			      0x0,
c5a379
-			      sizeof(rsp.version_info.fwversion),
c5a379
-			      error))
c5a379
-		return NULL;
c5a379
-	return g_strndup (ver, sizeof(ver));
c5a379
+					  GX_PKG_TYPE_EOP, req, &rsp, TRUE, error))
c5a379
+		return FALSE;
c5a379
+	version = g_strndup ((const gchar *) rsp.version_info.fwversion,
c5a379
+			     sizeof(rsp.version_info.fwversion));
c5a379
+	fu_device_set_version (FU_DEVICE (self), version);
c5a379
+	return TRUE;
c5a379
 }
c5a379
 
c5a379
 static gboolean
c5a379
@@ -281,15 +303,13 @@ fu_goodixmoc_device_open (FuUsbDevice *device, GError **error)
c5a379
 static gboolean
c5a379
 fu_goodixmoc_device_setup (FuDevice *device, GError **error)
c5a379
 {
c5a379
-	FuGoodixMocDevice *self = FU_GOODIXMOC_DEVICE(device);
c5a379
-	g_autofree gchar *version = NULL;
c5a379
+	FuGoodixMocDevice *self = FU_GOODIXMOC_DEVICE (device);
c5a379
 
c5a379
-	version = fu_goodixmoc_device_get_version (self, error);
c5a379
-	if (version == NULL) {
c5a379
+	/* ensure version */
c5a379
+	if (!fu_goodixmoc_device_setup_version (self, error)) {
c5a379
 		g_prefix_error (error, "failed to get firmware version: ");
c5a379
 		return FALSE;
c5a379
 	}
c5a379
-	fu_device_set_version (device, version);
c5a379
 
c5a379
 	/* success */
c5a379
 	return TRUE;
c5a379
diff --git plugins/goodix-moc/meson.build plugins/goodix-moc/meson.build
c5a379
index 4e1287e4..178b35d8 100644
c5a379
--- plugins/goodix-moc/meson.build
c5a379
+++ plugins/goodix-moc/meson.build
c5a379
@@ -9,7 +9,6 @@ install_data([
c5a379
 shared_module('fu_plugin_goodixmoc',
c5a379
   fu_hash,
c5a379
   sources : [
c5a379
-    'fu-goodixmoc-common.c',
c5a379
     'fu-goodixmoc-device.c',
c5a379
     'fu-plugin-goodixmoc.c',
c5a379
   ],
c5a379
-- 
c5a379
2.29.2
c5a379