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

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