Skip to content

Commit d1ac554

Browse files
committed
KEYS: Check hex2bin()'s return when generating an asymmetric key ID
As it stands, the code to generate an asymmetric key ID prechecks the hex string it is given whilst determining the length, before it allocates the buffer for hex2bin() to translate into - which mean that checking the result of hex2bin() is redundant. Unfortunately, hex2bin() is marked as __must_check, which means that the following warning may be generated if the return value isn't checked: crypto/asymmetric_keys/asymmetric_type.c: In function asymmetric_key_hex_to_key_id: crypto/asymmetric_keys/asymmetric_type.c:110: warning: ignoring return value of hex2bin, declared with attribute warn_unused_result The warning can't be avoided by casting the result to void. Instead, use strlen() to check the length of the string and ignore the fact that the string might not be entirely valid hex until after the allocation has been done - in which case we can use the result of hex2bin() for this. Signed-off-by: David Howells <dhowells@redhat.com>
1 parent d3e4f41 commit d1ac554

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

‎crypto/asymmetric_keys/asymmetric_type.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,12 @@ EXPORT_SYMBOL_GPL(asymmetric_match_key_ids);
9090
struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
9191
{
9292
struct asymmetric_key_id *match_id;
93-
const char *p;
94-
ptrdiff_t hexlen;
93+
size_t hexlen;
94+
int ret;
9595

9696
if (!*id)
9797
return ERR_PTR(-EINVAL);
98-
for (p = id; *p; p++)
99-
if (!isxdigit(*p))
100-
return ERR_PTR(-EINVAL);
101-
hexlen = p - id;
98+
hexlen = strlen(id);
10299
if (hexlen & 1)
103100
return ERR_PTR(-EINVAL);
104101

@@ -107,7 +104,11 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
107104
if (!match_id)
108105
return ERR_PTR(-ENOMEM);
109106
match_id->len = hexlen / 2;
110-
(void)hex2bin(match_id->data, id, hexlen / 2);
107+
ret = hex2bin(match_id->data, id, hexlen / 2);
108+
if (ret < 0) {
109+
kfree(match_id);
110+
return ERR_PTR(-EINVAL);
111+
}
111112
return match_id;
112113
}
113114

0 commit comments

Comments
 (0)