diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2016-02-26 15:54:56 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2016-02-26 15:54:56 +0100 |
commit | 352f79acbd759c14399e39baef21fc4ffe180ac2 (patch) | |
tree | ce5e4d90cb44d6320eca3fc1fc794654b9c01952 | |
parent | 5bec08cebd559c906eb94b8b957afb9f0b8db338 (diff) | |
download | busybox-w32-352f79acbd759c14399e39baef21fc4ffe180ac2.tar.gz busybox-w32-352f79acbd759c14399e39baef21fc4ffe180ac2.tar.bz2 busybox-w32-352f79acbd759c14399e39baef21fc4ffe180ac2.zip |
udhcpc: fix OPTION_6RD parsing (could overflow its malloced buffer)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | networking/udhcp/common.c | 15 | ||||
-rw-r--r-- | networking/udhcp/dhcpc.c | 4 |
2 files changed, 15 insertions, 4 deletions
diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c index bc41c8d4d..680852ce4 100644 --- a/networking/udhcp/common.c +++ b/networking/udhcp/common.c | |||
@@ -142,7 +142,7 @@ const char dhcp_option_strings[] ALIGN1 = | |||
142 | * udhcp_str2optset: to determine how many bytes to allocate. | 142 | * udhcp_str2optset: to determine how many bytes to allocate. |
143 | * xmalloc_optname_optval: to estimate string length | 143 | * xmalloc_optname_optval: to estimate string length |
144 | * from binary option length: (option[LEN] / dhcp_option_lengths[opt_type]) | 144 | * from binary option length: (option[LEN] / dhcp_option_lengths[opt_type]) |
145 | * is the number of elements, multiply in by one element's string width | 145 | * is the number of elements, multiply it by one element's string width |
146 | * (len_of_option_as_string[opt_type]) and you know how wide string you need. | 146 | * (len_of_option_as_string[opt_type]) and you know how wide string you need. |
147 | */ | 147 | */ |
148 | const uint8_t dhcp_option_lengths[] ALIGN1 = { | 148 | const uint8_t dhcp_option_lengths[] ALIGN1 = { |
@@ -162,7 +162,18 @@ const uint8_t dhcp_option_lengths[] ALIGN1 = { | |||
162 | [OPTION_S32] = 4, | 162 | [OPTION_S32] = 4, |
163 | /* Just like OPTION_STRING, we use minimum length here */ | 163 | /* Just like OPTION_STRING, we use minimum length here */ |
164 | [OPTION_STATIC_ROUTES] = 5, | 164 | [OPTION_STATIC_ROUTES] = 5, |
165 | [OPTION_6RD] = 22, /* ignored by udhcp_str2optset */ | 165 | [OPTION_6RD] = 12, /* ignored by udhcp_str2optset */ |
166 | /* The above value was chosen as follows: | ||
167 | * len_of_option_as_string[] for this option is >60: it's a string of the form | ||
168 | * "32 128 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 255.255.255.255 ". | ||
169 | * Each additional ipv4 address takes 4 bytes in binary option and appends | ||
170 | * another "255.255.255.255 " 16-byte string. We can set [OPTION_6RD] = 4 | ||
171 | * but this severely overestimates string length: instead of 16 bytes, | ||
172 | * it adds >60 for every 4 bytes in binary option. | ||
173 | * We cheat and declare here that option is in units of 12 bytes. | ||
174 | * This adds more than 60 bytes for every three ipv4 addresses - more than enough. | ||
175 | * (Even 16 instead of 12 should work, but let's be paranoid). | ||
176 | */ | ||
166 | }; | 177 | }; |
167 | 178 | ||
168 | 179 | ||
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 48097bc24..2fe84e1ca 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c | |||
@@ -113,7 +113,7 @@ static const uint8_t len_of_option_as_string[] = { | |||
113 | [OPTION_IP ] = sizeof("255.255.255.255 "), | 113 | [OPTION_IP ] = sizeof("255.255.255.255 "), |
114 | [OPTION_IP_PAIR ] = sizeof("255.255.255.255 ") * 2, | 114 | [OPTION_IP_PAIR ] = sizeof("255.255.255.255 ") * 2, |
115 | [OPTION_STATIC_ROUTES ] = sizeof("255.255.255.255/32 255.255.255.255 "), | 115 | [OPTION_STATIC_ROUTES ] = sizeof("255.255.255.255/32 255.255.255.255 "), |
116 | [OPTION_6RD ] = sizeof("32 128 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 255.255.255.255 "), | 116 | [OPTION_6RD ] = sizeof("132 128 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 255.255.255.255 "), |
117 | [OPTION_STRING ] = 1, | 117 | [OPTION_STRING ] = 1, |
118 | [OPTION_STRING_HOST ] = 1, | 118 | [OPTION_STRING_HOST ] = 1, |
119 | #if ENABLE_FEATURE_UDHCP_RFC3397 | 119 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
@@ -222,7 +222,7 @@ static NOINLINE char *xmalloc_optname_optval(uint8_t *option, const struct dhcp_ | |||
222 | type = optflag->flags & OPTION_TYPE_MASK; | 222 | type = optflag->flags & OPTION_TYPE_MASK; |
223 | optlen = dhcp_option_lengths[type]; | 223 | optlen = dhcp_option_lengths[type]; |
224 | upper_length = len_of_option_as_string[type] | 224 | upper_length = len_of_option_as_string[type] |
225 | * ((unsigned)(len + optlen - 1) / (unsigned)optlen); | 225 | * ((unsigned)(len + optlen) / (unsigned)optlen); |
226 | 226 | ||
227 | dest = ret = xmalloc(upper_length + strlen(opt_name) + 2); | 227 | dest = ret = xmalloc(upper_length + strlen(opt_name) + 2); |
228 | dest += sprintf(ret, "%s=", opt_name); | 228 | dest += sprintf(ret, "%s=", opt_name); |