udhcpc6 expects string for bootfile-param opt(60)
Geoff Hanson
ghanson at arista.com
Mon Jan 31 17:29:15 UTC 2022
Bug report:
Package: busybox
Version: 1.35.0
RFC 5970 (DHCPv6 options for network boot) section 3.2 specifies that the
boot file parameters option consists of multiple UTF-8 strings, each string
starting with a length field.
However, the udhcpc6 code is expecting a single null-terminated string.
When testing with the Kea DHCPv6 server, it encoded the parameter correctly,
but because the first octet was 0, the udhcpc6 command interpreted this as
a zero-length string.
I have noticed at least one server encoding the option as a single string,
so the
existing udhcpc6 command works for some servers.
I have attached a suggested fix (patch) which includes a bit of refactoring.
The fix is written in such a way to be compatible with non-compliant servers
that encode the parameter as a single string.
Let me know if you need any clarification on the problem.
Thanks,
Geoff Hanson
Arista Networks
ghanson at arista.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20220131/a28dd22c/attachment-0001.html>
-------------- next part --------------
Index: busybox-1.33.1/networking/udhcp/d6_dhcpc.c
===================================================================
--- busybox-1.33.1.orig/networking/udhcp/d6_dhcpc.c
+++ busybox-1.33.1/networking/udhcp/d6_dhcpc.c
@@ -79,7 +79,7 @@ static const struct dhcp_optflag d6_optf
#endif
#if ENABLE_FEATURE_UDHCPC6_RFC5970
{ OPTION_STRING, D6_OPT_BOOT_URL },
- { OPTION_STRING, D6_OPT_BOOT_PARAM },
+ { OPTION_STRING | OPTION_LIST, D6_OPT_BOOT_PARAM },
#endif
{ OPTION_STRING, 0xd1 }, /* DHCP_PXE_CONF_FILE */
{ OPTION_STRING, 0xd2 }, /* DHCP_PXE_PATH_PREFIX */
@@ -214,19 +214,16 @@ static char** new_env(void)
return &client6_data.env_ptr[client6_data.env_idx++];
}
-static char *string_option_to_env(const uint8_t *option,
- const uint8_t *option_end)
+static const char *get_option_name(const uint8_t *option)
{
- const char *ptr, *name = NULL;
- unsigned val_len;
+ const char *ptr = NULL;
int i;
ptr = d6_option_strings;
i = 0;
while (*ptr) {
if (d6_optflags[i].code == option[1]) {
- name = ptr;
- goto found;
+ return ptr;
}
ptr += strlen(ptr) + 1;
i++;
@@ -234,7 +231,19 @@ static char *string_option_to_env(const
bb_error_msg("can't find option name for 0x%x, skipping", option[1]);
return NULL;
- found:
+}
+
+static char *string_option_to_env(const uint8_t *option,
+ const uint8_t *option_end)
+{
+ const char *name = NULL;
+ unsigned val_len;
+
+ name = get_option_name(option);
+ if (!name) {
+ return NULL;
+ }
+
val_len = (option[2] << 8) | option[3];
if (val_len + &option[D6_OPT_DATA] > option_end) {
bb_simple_error_msg("option data exceeds option length");
@@ -243,6 +252,51 @@ static char *string_option_to_env(const
return xasprintf("%s=%.*s", name, val_len, (char*)option + 4);
}
+/* parse list of variable length strings. The length of each string
+ is the first two bytes */
+static char *string_list_option_to_env(const uint8_t *option,
+ const uint8_t *option_end)
+{
+ const char *name = NULL;
+ unsigned val_len, name_len, parm_len;
+ int i;
+ char *envstr, *envptr, *envval;
+ const uint8_t *optptr;
+
+ name = get_option_name(option);
+ if (!name)
+ return NULL;
+ name_len = strlen(name);
+
+ val_len = (option[2] << 8) | option[3];
+ if (val_len + &option[D6_OPT_DATA] > option_end) {
+ bb_simple_error_msg("option data exceeds option length");
+ return NULL;
+ }
+
+ envptr = envstr = xmalloc(name_len + 1 + val_len);
+ envptr = stpcpy(envptr, name);
+ envval = envptr = stpcpy(envptr, "=");
+
+ optptr = option + D6_OPT_DATA;
+ while (optptr + 1 < option_end) {
+ parm_len = (*optptr++ << 8) | *optptr++;
+ if (optptr + parm_len > option_end) {
+ bb_simple_error_msg("option parm length overflows option length");
+ return NULL;
+ }
+ if (parm_len > 0) {
+ if (envptr > envval)
+ *envptr++ = ' ';
+ snprintf(envptr, parm_len + 1, optptr);
+ envptr += parm_len;
+ optptr += parm_len;
+ }
+ }
+
+ return envstr;
+}
+
/* put all the parameters into the environment */
static void option_to_env(const uint8_t *option, const uint8_t *option_end)
{
@@ -407,7 +461,6 @@ static void option_to_env(const uint8_t
break;
#endif
case D6_OPT_BOOT_URL:
- case D6_OPT_BOOT_PARAM:
case 0xd1: /* DHCP_PXE_CONF_FILE */
case 0xd2: /* DHCP_PXE_PATH_PREFIX */
{
@@ -415,6 +468,38 @@ static void option_to_env(const uint8_t
if (tmp)
*new_env() = tmp;
break;
+ }
+ case D6_OPT_BOOT_PARAM:
+/* 0 1 2 3
+ * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * | OPT_BOOTFILE_PARAM | option-len |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * | param-len 1 | |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ parameter 1 .
+ * . (variable length) |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * . .
+ * . <multiple Parameters> .
+ * . .
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * | param-len n | |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ parameter n .
+ * . (variable length) |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ */
+ {
+ char *tmp;
+ if (option[4])
+ /* If the high byte of param-len 1 is non-zero, most
+ likely this was caused by a non-compliant server
+ sending the param as a single string. */
+ tmp = string_option_to_env(option, option_end);
+ else
+ tmp = string_list_option_to_env(option, option_end);
+ if (tmp)
+ *new_env() = tmp;
+ break;
}
}
len_m4 -= 4 + option[3];
More information about the busybox
mailing list