[U-Boot] [PATCHv2 02/21] net: core: Sanitize get/set operations for enetaddr
Olliver Schinagl
oliver at schinagl.nl
Mon Apr 10 15:33:37 UTC 2017
In the current net stack, we have a few functions to get and set
the "ethaddr" and "ethNaddr" environment variables, which use magic
values to get and set these environment variables. Remove the magicness
of the buffer by defining it proper and also check the input for its
length.
Additionally use the define in fdt parser where the ethaddr variables
are also used.
Signed-off-by: Olliver Schinagl <oliver at schinagl.nl>
---
common/fdt_support.c | 2 +-
include/net.h | 1 +
net/eth_common.c | 14 +++++++++-----
3 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/common/fdt_support.c b/common/fdt_support.c
index c6a76b7ad2..d462bf0642 100644
--- a/common/fdt_support.c
+++ b/common/fdt_support.c
@@ -471,7 +471,7 @@ void fdt_fixup_ethernet(void *fdt)
{
int i, j, prop;
char *tmp, *end;
- char mac[16];
+ char mac[ETH_ENETADDR_ENV_NAME_LEN];
const char *path;
unsigned char mac_addr[ARP_HLEN];
int offset;
diff --git a/include/net.h b/include/net.h
index ed5259a807..b4af8eaae4 100644
--- a/include/net.h
+++ b/include/net.h
@@ -243,6 +243,7 @@ void eth_set_current(void); /* set nterface to ethcur var */
int eth_get_dev_index(void); /* get the device index */
void eth_parse_enetaddr(const char *addr, uchar *enetaddr);
+#define ETH_ENETADDR_ENV_NAME_LEN 32
int eth_getenv_enetaddr(const char *name, uchar *enetaddr);
int eth_setenv_enetaddr(const char *name, const uchar *enetaddr);
diff --git a/net/eth_common.c b/net/eth_common.c
index 049c1ee6f9..0fe4d260d8 100644
--- a/net/eth_common.c
+++ b/net/eth_common.c
@@ -45,16 +45,20 @@ int eth_setenv_enetaddr(const char *name, const uchar *enetaddr)
int eth_getenv_enetaddr_by_index(const char *base_name, int index,
uchar *enetaddr)
{
- char enetvar[32];
- sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
+ char enetvar[ETH_ENETADDR_ENV_NAME_LEN];
+
+ snprintf(enetvar, ETH_ENETADDR_ENV_NAME_LEN,
+ index ? "%s%daddr" : "%saddr", base_name, index);
return eth_getenv_enetaddr(enetvar, enetaddr);
}
int eth_setenv_enetaddr_by_index(const char *base_name, int index,
uchar *enetaddr)
{
- char enetvar[32];
- sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
+ char enetvar[ETH_ENETADDR_ENV_NAME_LEN];
+
+ snprintf(enetvar, ETH_ENETADDR_ENV_NAME_LEN,
+ index ? "%s%daddr" : "%saddr", base_name, index);
return eth_setenv_enetaddr(enetvar, enetaddr);
}
@@ -72,7 +76,7 @@ void eth_common_init(void)
int eth_mac_skip(int index)
{
- char enetvar[15];
+ char enetvar[ETH_ENETADDR_ENV_NAME_LEN];
char *skip_state;
sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
--
2.11.0
More information about the U-Boot
mailing list