From patchwork Fri Aug 23 04:09:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ivanhu X-Patchwork-Id: 1975836 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=fwts-devel-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WqmmS18Mtz1ybW for ; Fri, 23 Aug 2024 14:09:28 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1shLc4-0006dy-1s; Fri, 23 Aug 2024 04:09:24 +0000 Received: from smtp-relay-canonical-1.internal ([10.131.114.174] helo=smtp-relay-canonical-1.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1shLc2-0006cz-Dd for fwts-devel@lists.ubuntu.com; Fri, 23 Aug 2024 04:09:22 +0000 Received: from canonical.com (unknown [122.147.171.160]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-1.canonical.com (Postfix) with ESMTPSA id 55AB13F3A7 for ; Fri, 23 Aug 2024 04:09:21 +0000 (UTC) From: Ivan Hu To: fwts-devel@lists.ubuntu.com Subject: [PATCH] lib: modprobe: add checking the compressed zst module format Date: Fri, 23 Aug 2024 12:09:17 +0800 Message-Id: <20240823040917.24674-1-ivan.hu@canonical.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: fwts-devel-bounces@lists.ubuntu.com Sender: "fwts-devel" BugLink: https://bugs.launchpad.net/fwts/+bug/2077528 Newer kernel support modules compressed format zst, this make the modules cannot be spotted correctly, add the zst checking for the compressed modules. Using __NR_finit_module syscall may result in an error=8(Exec format error) So, using the modprobe command directly instead of relying on the __NR_finit_module syscall. Signed-off-by: Ivan Hu --- src/lib/src/fwts_modprobe.c | 44 +++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/src/lib/src/fwts_modprobe.c b/src/lib/src/fwts_modprobe.c index 57d934ed..02456ec5 100644 --- a/src/lib/src/fwts_modprobe.c +++ b/src/lib/src/fwts_modprobe.c @@ -182,12 +182,12 @@ int fwts_module_load(fwts_framework *fw, const char *module) struct utsname u; const size_t modlen = strlen(module); char module_ko[modlen + 4]; + char module_ko_zst[modlen + 8]; char path[PATH_MAX]; char modpath[PATH_MAX]; - const char *params = ""; - int fd; bool loaded = false; - + int status = 0; + char modeprobe_ko[64]; /* * No need to unload if it's not already loaded */ @@ -207,26 +207,19 @@ int fwts_module_load(fwts_framework *fw, const char *module) (void)snprintf(module_ko, sizeof(module_ko), "%s.ko", module); (void)snprintf(modpath, sizeof(modpath), "/lib/modules/%s", u.release); if (!fwts_module_find(module_ko, modpath, path, sizeof(path))) { - fwts_log_error(fw, "Cannot find module %s\n", module); - return FWTS_ERROR; + (void)snprintf(module_ko_zst, sizeof(module_ko_zst), "%s.ko.zst", module); + if (!fwts_module_find(module_ko_zst, modpath, path, sizeof(path))) { + fwts_log_error(fw, "Cannot find module %s\n", module); + return FWTS_ERROR; + } } - /* - * We've found it, now try and load it - */ - fd = open(path, O_RDONLY); - if (fd < 0) { - fwts_log_error(fw, "Cannot open module %s, errno=%d (%s)\n", - path, errno, strerror(errno)); + (void)snprintf(modeprobe_ko, sizeof("modprobe ") + modlen, "modprobe %s", module); + (void)fwts_exec(modeprobe_ko, &status); + if (status != FWTS_OK) { + fwts_log_error(fw, "modprobe module %s failed\n", module); return FWTS_ERROR; } - if (sys_finit_module(fd, params, 0) < 0) { - fwts_log_error(fw, "Cannot load module %s, errno=%d (%s)\n", - path, errno, strerror(errno)); - (void)close(fd); - return FWTS_ERROR; - } - (void)close(fd); return FWTS_OK; } @@ -241,7 +234,9 @@ int fwts_module_load(fwts_framework *fw, const char *module) int fwts_module_unload(fwts_framework *fw, const char *module) { bool loaded = false; - int ret; + char modeprobe_ko[64]; + const size_t modlen = strlen(module); + int status = 0; /* * No need to unload if it's not already loaded @@ -251,11 +246,12 @@ int fwts_module_unload(fwts_framework *fw, const char *module) return FWTS_OK; } - ret = sys_delete_module(module, O_NONBLOCK); - if (ret < 0) { - fwts_log_error(fw, "Cannot unload module %s, errno=%d (%s)\n", - module, errno, strerror(errno)); + (void)snprintf(modeprobe_ko, sizeof("modprobe -r") + modlen, "modprobe -r %s", module); + (void)fwts_exec(modeprobe_ko, &status); + if (status != FWTS_OK) { + fwts_log_error(fw, "Cannot unload module %s\n", module); return FWTS_ERROR; } + return FWTS_OK; }