提交 47e0a208 编写于 作者: E Eric Biggers 提交者: David Howells

X.509: fix buffer overflow detection in sprint_oid()

In sprint_oid(), if the input buffer were to be more than 1 byte too
small for the first snprintf(), 'bufsize' would underflow, causing a
buffer overflow when printing the remainder of the OID.

Fortunately this cannot actually happen currently, because no users pass
in a buffer that can be too small for the first snprintf().

Regardless, fix it by checking the snprintf() return value correctly.

For consistency also tweak the second snprintf() check to look the same.

Fixes: 4f73175d ("X.509: Add utility functions to render OIDs as strings")
Cc: Takashi Iwai <tiwai@suse.de>
Signed-off-by: NEric Biggers <ebiggers@google.com>
Signed-off-by: NDavid Howells <dhowells@redhat.com>
Reviewed-by: NJames Morris <james.l.morris@oracle.com>
上级 0f30cbea
...@@ -120,10 +120,10 @@ int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize) ...@@ -120,10 +120,10 @@ int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize)
n = *v++; n = *v++;
ret = count = snprintf(buffer, bufsize, "%u.%u", n / 40, n % 40); ret = count = snprintf(buffer, bufsize, "%u.%u", n / 40, n % 40);
if (count >= bufsize)
return -ENOBUFS;
buffer += count; buffer += count;
bufsize -= count; bufsize -= count;
if (bufsize == 0)
return -ENOBUFS;
while (v < end) { while (v < end) {
num = 0; num = 0;
...@@ -141,9 +141,9 @@ int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize) ...@@ -141,9 +141,9 @@ int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize)
} while (n & 0x80); } while (n & 0x80);
} }
ret += count = snprintf(buffer, bufsize, ".%lu", num); ret += count = snprintf(buffer, bufsize, ".%lu", num);
buffer += count; if (count >= bufsize)
if (bufsize <= count)
return -ENOBUFS; return -ENOBUFS;
buffer += count;
bufsize -= count; bufsize -= count;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册