From patchwork Tue Apr 11 12:15:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yauheni Kaliuta X-Patchwork-Id: 9675041 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9781E60381 for ; Tue, 11 Apr 2017 12:12:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9193428567 for ; Tue, 11 Apr 2017 12:12:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8598128565; Tue, 11 Apr 2017 12:12:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2979328565 for ; Tue, 11 Apr 2017 12:12:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752232AbdDKMMm (ORCPT ); Tue, 11 Apr 2017 08:12:42 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43652 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751951AbdDKMMm (ORCPT ); Tue, 11 Apr 2017 08:12:42 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E4A0DC04B950; Tue, 11 Apr 2017 12:12:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E4A0DC04B950 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=yauheni.kaliuta@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com E4A0DC04B950 Received: from astarta.redhat.com (ovpn-117-212.ams2.redhat.com [10.36.117.212]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0AAA9627DE; Tue, 11 Apr 2017 12:12:40 +0000 (UTC) From: Yauheni Kaliuta To: linux-modules Cc: Lucas De Marchi , Mark van Dijk Subject: [PATCH 2/4] libkmod: modinfo: use own function for sig_key hex output Date: Tue, 11 Apr 2017 15:15:01 +0300 Message-Id: <20170411121503.26181-3-yauheni.kaliuta@redhat.com> In-Reply-To: <20170411121503.26181-1-yauheni.kaliuta@redhat.com> References: <20170411121503.26181-1-yauheni.kaliuta@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 11 Apr 2017 12:12:42 +0000 (UTC) Sender: owner-linux-modules@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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"),