From 29a3967e61334d0c6a1a7d391f0e751272d77b1d Mon Sep 17 00:00:00 2001
From: David Bauer <mail@david-bauer.net>
Date: Tue, 10 Aug 2021 00:28:01 +0200
Subject: [PATCH] generic: fix kernel panic on existing mac-address node

Calling free for the OF property can result in a kernel panic, as the
buffer in question might be referenced elsewhere. Also, it is not
removed from the tree.

Always allocate a new property and updating the tree with it fixes both
issues.

Fixes commit 91a52f22a13d ("treewide: backport support for nvmem on non platform devices")

Signed-off-by: David Bauer <mail@david-bauer.net>
---
 .../683-of_net-add-mac-address-to-of-tree.patch      | 12 ++++--------
 .../683-of_net-add-mac-address-to-of-tree.patch      | 12 ++++--------
 2 files changed, 8 insertions(+), 16 deletions(-)

diff --git a/target/linux/generic/pending-5.10/683-of_net-add-mac-address-to-of-tree.patch b/target/linux/generic/pending-5.10/683-of_net-add-mac-address-to-of-tree.patch
index 509d721403..ea5883f037 100644
--- a/target/linux/generic/pending-5.10/683-of_net-add-mac-address-to-of-tree.patch
+++ b/target/linux/generic/pending-5.10/683-of_net-add-mac-address-to-of-tree.patch
@@ -1,6 +1,6 @@
 --- a/drivers/of/of_net.c
 +++ b/drivers/of/of_net.c
-@@ -141,6 +141,30 @@ static int of_get_mac_address_mtd(struct
+@@ -135,6 +135,26 @@ static int of_get_mac_address_mtd(struct
  	return -EINVAL;
  }
  
@@ -8,18 +8,14 @@
 +{
 +	struct property *prop;
 +
-+	prop = of_find_property(np, "mac-address", NULL);
-+	if (prop)
-+		kfree(prop);
-+
 +	prop = kzalloc(sizeof(*prop), GFP_KERNEL);
 +	if (!prop)
-+		return 0;
++		return -ENOMEM;
 +
 +	prop->name = "mac-address";
 +	prop->length = ETH_ALEN;
 +	prop->value = kmemdup(addr, ETH_ALEN, GFP_KERNEL);
-+	if (!prop->value || of_add_property(np, prop))
++	if (!prop->value || of_update_property(np, prop))
 +		goto free;
 +
 +	return 0;
@@ -31,7 +27,7 @@
  
  /**
   * Search the device tree for the best MAC address to use.  'mac-address' is
-@@ -216,6 +240,7 @@ found:
+@@ -210,6 +230,7 @@ found:
  	if (!of_property_read_u32(np, "mac-address-increment", &mac_inc))
  		addr[inc_idx] += mac_inc;
  
diff --git a/target/linux/generic/pending-5.4/683-of_net-add-mac-address-to-of-tree.patch b/target/linux/generic/pending-5.4/683-of_net-add-mac-address-to-of-tree.patch
index 622d19d4ff..ea5883f037 100644
--- a/target/linux/generic/pending-5.4/683-of_net-add-mac-address-to-of-tree.patch
+++ b/target/linux/generic/pending-5.4/683-of_net-add-mac-address-to-of-tree.patch
@@ -1,6 +1,6 @@
 --- a/drivers/of/of_net.c
 +++ b/drivers/of/of_net.c
-@@ -135,6 +135,30 @@ static int of_get_mac_address_mtd(struct
+@@ -135,6 +135,26 @@ static int of_get_mac_address_mtd(struct
  	return -EINVAL;
  }
  
@@ -8,18 +8,14 @@
 +{
 +	struct property *prop;
 +
-+	prop = of_find_property(np, "mac-address", NULL);
-+	if (prop)
-+		kfree(prop);
-+
 +	prop = kzalloc(sizeof(*prop), GFP_KERNEL);
 +	if (!prop)
-+		return 0;
++		return -ENOMEM;
 +
 +	prop->name = "mac-address";
 +	prop->length = ETH_ALEN;
 +	prop->value = kmemdup(addr, ETH_ALEN, GFP_KERNEL);
-+	if (!prop->value || of_add_property(np, prop))
++	if (!prop->value || of_update_property(np, prop))
 +		goto free;
 +
 +	return 0;
@@ -31,7 +27,7 @@
  
  /**
   * Search the device tree for the best MAC address to use.  'mac-address' is
-@@ -210,6 +234,7 @@ found:
+@@ -210,6 +230,7 @@ found:
  	if (!of_property_read_u32(np, "mac-address-increment", &mac_inc))
  		addr[inc_idx] += mac_inc;
  
-- 
2.30.2