Fix a small mistake in modinfo output, signat instead of sig_id. Implement signature hex ouput as people asked for it. Yauheni Kaliuta (4): libkmod: modinfo: fix sig_id output libkmod: modinfo: use own function for sig_key hex output libkmod: modinfo: implement line splitting in hex_to_str libkmod: modinfo: implement signature output libkmod/libkmod-internal.h | 2 + libkmod/libkmod-module.c | 100 ++++++++++++++++++++++++++++++++------------ libkmod/libkmod-signature.c | 6 ++- 3 files changed, 81 insertions(+), 27 deletions(-) -- 2.9.2.368.g08bb350
For some reason the key for sig_id was set to "signature". The length was calculated against the proper string, as the result in the output it was truncated to "signat". Pass the proper key to the kmod_module_info_append() call. Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com> --- libkmod/libkmod-module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 57da0a2d1147..34c7f76e4db5 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -2257,7 +2257,7 @@ KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_ struct kmod_list *n; char *key_hex; - n = kmod_module_info_append(list, "signature", strlen("sig_id"), + n = kmod_module_info_append(list, "sig_id", strlen("sig_id"), sig_info.id_type, strlen(sig_info.id_type)); if (n == NULL) goto list_error; -- 2.9.2.368.g08bb350
Refactor the code a bit to make it easier to extend for signature output. kmod_module_get_info() creats a hex string for the sig_key data inplace. Separate it into own kmod_module_hex_to_string function and handle the branch in the new kmod_module_info_append_hex, keeping the same signature as the non-hex version. Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com> --- libkmod/libkmod-module.c | 79 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 25 deletions(-) diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 34c7f76e4db5..22c8f4c852a2 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -2193,6 +2193,53 @@ static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const return n; } +static char *kmod_module_hex_to_str(const char *hex, size_t len) +{ + char *str; + int i; + + str = malloc(len * 3); + if (str == NULL) + return NULL; + + for (i = 0; i < (int)len; i++) { + sprintf(str + i * 3, "%02X", (unsigned char)hex[i]); + if (i < (int)len - 1) + str[i * 3 + 2] = ':'; + } + return str; +} + +static struct kmod_list *kmod_module_info_append_hex(struct kmod_list **list, + const char *key, + size_t keylen, + const char *value, + size_t valuelen) +{ + char *hex; + struct kmod_list *n; + + if (valuelen > 0) { + /* Display as 01:12:DE:AD:BE:EF:... */ + hex = kmod_module_hex_to_str(value, valuelen); + if (hex == NULL) + goto list_error; + n = kmod_module_info_append(list, key, keylen, hex, strlen(hex)); + free(hex); + if (n == NULL) + goto list_error; + } else { + n = kmod_module_info_append(list, key, keylen, NULL, 0); + if (n == NULL) + goto list_error; + } + + return n; + +list_error: + return NULL; +} + /** * kmod_module_get_info: * @mod: kmod module @@ -2255,7 +2302,6 @@ KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_ if (kmod_module_signature_info(mod->file, &sig_info)) { struct kmod_list *n; - char *key_hex; n = kmod_module_info_append(list, "sig_id", strlen("sig_id"), sig_info.id_type, strlen(sig_info.id_type)); @@ -2269,30 +2315,13 @@ KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_ goto list_error; count++; - if (sig_info.key_id_len) { - /* Display the key id as 01:12:DE:AD:BE:EF:... */ - key_hex = malloc(sig_info.key_id_len * 3); - if (key_hex == NULL) - goto list_error; - for (i = 0; i < (int)sig_info.key_id_len; i++) { - sprintf(key_hex + i * 3, "%02X", - (unsigned char)sig_info.key_id[i]); - if (i < (int)sig_info.key_id_len - 1) - key_hex[i * 3 + 2] = ':'; - } - n = kmod_module_info_append(list, "sig_key", strlen("sig_key"), - key_hex, sig_info.key_id_len * 3 - 1); - free(key_hex); - if (n == NULL) - goto list_error; - count++; - } else { - n = kmod_module_info_append(list, "sig_key", strlen("sig_key"), - NULL, 0); - if (n == NULL) - goto list_error; - count++; - } + + n = kmod_module_info_append_hex(list, "sig_key", strlen("sig_key"), + sig_info.key_id, + sig_info.key_id_len); + if (n == NULL) + goto list_error; + count++; n = kmod_module_info_append(list, "sig_hashalgo", strlen("sig_hashalgo"), -- 2.9.2.368.g08bb350
The key output is usually short, but for signature it is more readable to output it in several lines. Implement line splitting. Set line limit hardcoded to 20 hex numbers (not characters). Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com> --- libkmod/libkmod-module.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 22c8f4c852a2..9e155f080277 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -2197,15 +2197,25 @@ static char *kmod_module_hex_to_str(const char *hex, size_t len) { char *str; int i; + int j; + const size_t line_limit = 20; + size_t str_len; - str = malloc(len * 3); + str_len = len * 3; /* XX: or XX\0 */ + str_len += ((str_len + line_limit - 1) / line_limit - 1) * 3; /* \n\t\t */ + + str = malloc(str_len); if (str == NULL) return NULL; - for (i = 0; i < (int)len; i++) { - sprintf(str + i * 3, "%02X", (unsigned char)hex[i]); - if (i < (int)len - 1) - str[i * 3 + 2] = ':'; + for (i = 0, j = 0; i < (int)len; i++) { + j += sprintf(str + j, "%02X", (unsigned char)hex[i]); + if (i < (int)len - 1) { + str[j++] = ':'; + + if ((i + 1) % line_limit == 0) + j += sprintf(str + j, "\n\t\t"); + } } return str; } -- 2.9.2.368.g08bb350
Signature was ignored from the modinfo. Implement its parsing from the module data and add its output to the modinfo utility. Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com> --- libkmod/libkmod-internal.h | 2 ++ libkmod/libkmod-module.c | 9 +++++++++ libkmod/libkmod-signature.c | 6 +++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/libkmod/libkmod-internal.h b/libkmod/libkmod-internal.h index 4d9db6bc7845..346579c71aab 100644 --- a/libkmod/libkmod-internal.h +++ b/libkmod/libkmod-internal.h @@ -186,5 +186,7 @@ struct kmod_signature_info { const char *key_id; size_t key_id_len; const char *algo, *hash_algo, *id_type; + const char *sig; + size_t sig_len; }; bool kmod_module_signature_info(const struct kmod_file *file, struct kmod_signature_info *sig_info) _must_check_ __attribute__((nonnull(1, 2))); diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 9e155f080277..0a3ef11c860f 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -2344,6 +2344,15 @@ KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_ * Omit sig_info.algo for now, as these * are currently constant. */ + n = kmod_module_info_append_hex(list, "signature", + strlen("signature"), + sig_info.sig, + sig_info.sig_len); + + if (n == NULL) + goto list_error; + count++; + } ret = count; diff --git a/libkmod/libkmod-signature.c b/libkmod/libkmod-signature.c index ef5fe6e7bb7f..1f3e26dea203 100644 --- a/libkmod/libkmod-signature.c +++ b/libkmod/libkmod-signature.c @@ -134,7 +134,11 @@ bool kmod_module_signature_info(const struct kmod_file *file, struct kmod_signat size < (int64_t)(modsig->signer_len + modsig->key_id_len + sig_len)) return false; - size -= modsig->key_id_len + sig_len; + size -= sig_len; + sig_info->sig = mem + size; + sig_info->sig_len = sig_len; + + size -= modsig->key_id_len; sig_info->key_id = mem + size; sig_info->key_id_len = modsig->key_id_len; -- 2.9.2.368.g08bb350
On Tue, Apr 11, 2017 at 5:14 AM, Yauheni Kaliuta
<yauheni.kaliuta@redhat.com> wrote:
> Fix a small mistake in modinfo output, signat instead of sig_id.
> Implement signature hex ouput as people asked for it.
>
> Yauheni Kaliuta (4):
> libkmod: modinfo: fix sig_id output
> libkmod: modinfo: use own function for sig_key hex output
> libkmod: modinfo: implement line splitting in hex_to_str
> libkmod: modinfo: implement signature output
All 4 patches applied.
Thanks
Lucas De Marchi