提交 3b523a1d 编写于 作者: M Mikulas Patocka 提交者: Yongqiang Liu

hex2bin: fix access beyond string end

stable inclusion
from stable-4.19.242
commit a4981bed962f82513c878f87dc9affd157ee7cb9
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I5A6BA
CVE: NA

--------------------------------

commit e4d8a299 upstream.

If we pass too short string to "hex2bin" (and the string size without
the terminating NUL character is even), "hex2bin" reads one byte after
the terminating NUL character.  This patch fixes it.

Note that hex_to_bin returns -1 on error and hex2bin return -EINVAL on
error - so we can't just return the variable "hi" or "lo" on error.
This inconsistency may be fixed in the next merge window, but for the
purpose of fixing this bug, we just preserve the existing behavior and
return -1 and -EINVAL.
Signed-off-by: NMikulas Patocka <mpatocka@redhat.com>
Reviewed-by: NAndy Shevchenko <andy.shevchenko@gmail.com>
Fixes: b7804983 ("lib: add error checking to hex2bin")
Cc: stable@vger.kernel.org
Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: NYongqiang Liu <liuyongqiang13@huawei.com>
上级 84affeaf
...@@ -48,10 +48,13 @@ EXPORT_SYMBOL(hex_to_bin); ...@@ -48,10 +48,13 @@ EXPORT_SYMBOL(hex_to_bin);
int hex2bin(u8 *dst, const char *src, size_t count) int hex2bin(u8 *dst, const char *src, size_t count)
{ {
while (count--) { while (count--) {
int hi = hex_to_bin(*src++); int hi, lo;
int lo = hex_to_bin(*src++);
if ((hi < 0) || (lo < 0)) hi = hex_to_bin(*src++);
if (unlikely(hi < 0))
return -EINVAL;
lo = hex_to_bin(*src++);
if (unlikely(lo < 0))
return -EINVAL; return -EINVAL;
*dst++ = (hi << 4) | lo; *dst++ = (hi << 4) | lo;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册