Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp289716lqo; Thu, 16 May 2024 06:33:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXSHwtZZZyfgoxodvtYxYtQtkgoJqFNsn8NHFTFhvAELtyE2vs1LMg39sZ3KCE1tG3AIPj4SKf/IAgRfr+RiV+6gJcSIEyKZX02DWKCPg== X-Google-Smtp-Source: AGHT+IFLVHGN7lyx8PQVBzOnQXqs+VZuPAiV67EmM2a05yWkVyo8hY44oBvl8k1Gk8DJij+h0qJa X-Received: by 2002:a05:6512:4003:b0:522:221:d19d with SMTP id 2adb3069b0e04-5220fd7bfccmr18456177e87.15.1715866380334; Thu, 16 May 2024 06:33:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715866380; cv=pass; d=google.com; s=arc-20160816; b=PvHGX50xIKrglz/yRmObwdoqwvkpzO9JxOTc3kg464ttwWWBLXIcduVyeGfrC1fDMr abGsYgn7JogQ9Vylw/vVoeoCI8ZpPgtIbJogsOgrRXuyIWfCUdq8dReC2f03gJjEDIdC J5xHAfyGLZpRpVT4R604UDOhEzmTguo/tHsstxpLOeuNA0YBsdYUKulZTiNPB+lT6to7 FFoXkv+beChmMucT/2jR7HbCuVSBAgOzYY+vs4K9yNRVDaqibDdCRUz3WEOwB9eaBnPO 244cE8tc9xDNbLmfl0UR4hTgFtaS3Bxu1u1ESulhMuoJEk0FtuGygKiAdE7cHCXXXdLr rJXg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=mS8518wsgnaabu/RTGprCNrsL2t9NX+Z+fv48jOoshc=; fh=n8RwaS09DESRBf8rZsy0FAeWRbH7NUAQ3UsY2bFS+sw=; b=UWGouOETCrzDC1I0OWSIMF3CEz3TGwHHqx/CWrhLlCKLmqp/tdx7mFJuP5bV9IdtzB LmfDTilpqodAQ3i0wB2NOxe5T/LgFMgGESvDHT6B+rJPokneQYXrzfBPIfNbxst2hSKc PVNIcAUkCPQKrsYSdKhCTDrwRD+1/opm1ue5RUrZAG+1eBGGjbuMztJQDf7g0/wp2k63 VN8xMBSKzsRlDiezCkkiii1Wf3iZi+Y2Vi7X/JLI43QD+PMPNtrv4mDLQpTuV7M6Z6xK WRNHMoZEJjMlZyeAPeNKjV61Nb5XFkVYxIAdzk/byTHdoTAqOwc4U4nOylhBATygiEN7 LTzQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=suse.cz); spf=pass (google.com: domain of linux-kernel+bounces-181121-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181121-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a179463a1si841672866b.27.2024.05.16.06.33.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 06:33:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-181121-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=suse.cz); spf=pass (google.com: domain of linux-kernel+bounces-181121-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181121-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id EB41B1F2343F for ; Thu, 16 May 2024 13:32:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DE7B7147C74; Thu, 16 May 2024 13:30:43 +0000 (UTC) Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F38414D71A; Thu, 16 May 2024 13:30:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715866243; cv=none; b=tf3hDu3cPJX/6LpOLJysy8I/Cc7RuYONHysYB4RkqnCoMNFwSKUWDaWS5t80PmxDczbWN/JJEsRmzpQpYAijT1ji0QnRmcSSJba9ob07fHy21rq8g+aR4V0vY0en7MaQ8kml3BIcWCYOWx6CCKmI4zaAyG6r2kwd5HaQZBTHl9k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715866243; c=relaxed/simple; bh=piBfHsdrx+bDkPiULo4ylAnu5ZGbeLfxNVM1FW2By5I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AS3gQFAfBSSvLbR6X0O1a8hb2eK+5rSJQlc4eDhBnX9fGHWYKL4loXQIPL4jzJrRIytsJcjIj49TmU1y/HnQaezPVI9CxXUvLfe49k5M5lN0KNUkREtFKuG9d248q9Z8VW7Gq+RSkqeCAq3Um/Movkdl9+Iknv3I45z0oCJ3ByU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz; spf=pass smtp.mailfrom=suse.cz; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 94170349E1; Thu, 16 May 2024 13:30:39 +0000 (UTC) Authentication-Results: smtp-out1.suse.de; none Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 8786113991; Thu, 16 May 2024 13:30:39 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id OYRbIH8KRmYkDQAAD6G6ig (envelope-from ); Thu, 16 May 2024 13:30:39 +0000 From: Lukas Hruska To: pmladek@suse.com, mbenes@suse.cz, jpoimboe@kernel.org Cc: joe.lawrence@redhat.com, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, mpdesouza@suse.com, lhruska@suse.cz Subject: [PATCH v2 6/6] selftests: livepatch: Test livepatching function using an external symbol Date: Thu, 16 May 2024 15:30:09 +0200 Message-ID: <20240516133009.20224-7-lhruska@suse.cz> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240516133009.20224-1-lhruska@suse.cz> References: <20240516133009.20224-1-lhruska@suse.cz> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spam-Level: X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] X-Spam-Flag: NO X-Spam-Score: -4.00 X-Rspamd-Queue-Id: 94170349E1 X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action The test proves that klp-convert works as intended and it is possible to livepatch a function that use an external symbol. Signed-off-by: Lukas Hruska --- .../testing/selftests/livepatch/functions.sh | 16 +++++- .../selftests/livepatch/test-extern.sh | 57 +++++++++++++++++++ .../selftests/livepatch/test_modules/Makefile | 2 + .../livepatch/test_modules/test_klp_extern.c | 51 +++++++++++++++++ .../test_modules/test_klp_extern_hello.c | 36 ++++++++++++ 5 files changed, 161 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/livepatch/test-extern.sh create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_extern.c create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_extern_hello.c diff --git a/tools/testing/selftests/livepatch/functions.sh b/tools/testing/selftests/livepatch/functions.sh index fc4c6a016d38..801d55dc06ac 100644 --- a/tools/testing/selftests/livepatch/functions.sh +++ b/tools/testing/selftests/livepatch/functions.sh @@ -7,6 +7,7 @@ MAX_RETRIES=600 RETRY_INTERVAL=".1" # seconds KLP_SYSFS_DIR="/sys/kernel/livepatch" +MODULE_SYSFS_DIR="/sys/module" # Kselftest framework requirement - SKIP code is 4 ksft_skip=4 @@ -299,7 +300,7 @@ function check_result { result=$(dmesg | awk -v last_dmesg="$LAST_DMESG" 'p; $0 == last_dmesg { p=1 }' | \ grep -e 'livepatch:' -e 'test_klp' | \ grep -v '\(tainting\|taints\) kernel' | \ - sed 's/^\[[ 0-9.]*\] //') + sed 's/^\[[ 0-9.]*\] //' | sed 's/^test_klp_log: //') if [[ "$expect" == "$result" ]] ; then echo "ok" @@ -344,3 +345,16 @@ function check_sysfs_value() { die "Unexpected value in $path: $expected_value vs. $value" fi } + +# read_module_param_value(modname, param) - read module parameter value +# modname - livepatch module creating the sysfs interface +# param - parameter name +function read_module_param() { + local mod="$1"; shift + local param="$1"; shift + + local path="$MODULE_SYSFS_DIR/$mod/parameters/$param" + + log "% echo \"$mod/parameters/$param: \$(cat $path)\"" + log "$mod/parameters/$param: $(cat $path)" +} diff --git a/tools/testing/selftests/livepatch/test-extern.sh b/tools/testing/selftests/livepatch/test-extern.sh new file mode 100755 index 000000000000..3dde6cabb07c --- /dev/null +++ b/tools/testing/selftests/livepatch/test-extern.sh @@ -0,0 +1,57 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2024 Lukas Hruska + +. $(dirname $0)/functions.sh + +MOD_LIVEPATCH=test_klp_extern +MOD_HELLO=test_klp_extern_hello +PARAM_HELLO=hello + +setup_config + +# - load a module to be livepatched +# - load a livepatch that modifies the output from 'hello' parameter +# of the previously loaded module and verify correct behaviour +# - unload the livepatch and make sure the patch was removed +# - unload the module that was livepatched + +start_test "livepatch with external symbol" + +load_mod $MOD_HELLO + +read_module_param $MOD_HELLO $PARAM_HELLO + +load_lp $MOD_LIVEPATCH + +read_module_param $MOD_HELLO $PARAM_HELLO + +disable_lp $MOD_LIVEPATCH +unload_lp $MOD_LIVEPATCH + +read_module_param $MOD_HELLO $PARAM_HELLO + +unload_mod $MOD_HELLO + +check_result "% insmod test_modules/$MOD_HELLO.ko +% echo \"$MOD_HELLO/parameters/$PARAM_HELLO: \$(cat /sys/module/$MOD_HELLO/parameters/$PARAM_HELLO)\" +$MOD_HELLO/parameters/$PARAM_HELLO: Hello from kernel module. +% insmod test_modules/$MOD_LIVEPATCH.ko +livepatch: enabling patch '$MOD_LIVEPATCH' +livepatch: '$MOD_LIVEPATCH': initializing patching transition +livepatch: '$MOD_LIVEPATCH': starting patching transition +livepatch: '$MOD_LIVEPATCH': completing patching transition +livepatch: '$MOD_LIVEPATCH': patching complete +% echo \"$MOD_HELLO/parameters/$PARAM_HELLO: \$(cat /sys/module/$MOD_HELLO/parameters/$PARAM_HELLO)\" +$MOD_HELLO/parameters/$PARAM_HELLO: Hello from livepatched module. +% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled +livepatch: '$MOD_LIVEPATCH': initializing unpatching transition +livepatch: '$MOD_LIVEPATCH': starting unpatching transition +livepatch: '$MOD_LIVEPATCH': completing unpatching transition +livepatch: '$MOD_LIVEPATCH': unpatching complete +% rmmod $MOD_LIVEPATCH +% echo \"$MOD_HELLO/parameters/$PARAM_HELLO: \$(cat /sys/module/$MOD_HELLO/parameters/$PARAM_HELLO)\" +$MOD_HELLO/parameters/$PARAM_HELLO: Hello from kernel module. +% rmmod $MOD_HELLO" + +exit 0 diff --git a/tools/testing/selftests/livepatch/test_modules/Makefile b/tools/testing/selftests/livepatch/test_modules/Makefile index e6e638c4bcba..0d6df14787da 100644 --- a/tools/testing/selftests/livepatch/test_modules/Makefile +++ b/tools/testing/selftests/livepatch/test_modules/Makefile @@ -6,6 +6,8 @@ obj-m += test_klp_atomic_replace.o \ test_klp_callbacks_demo.o \ test_klp_callbacks_demo2.o \ test_klp_callbacks_mod.o \ + test_klp_extern.o \ + test_klp_extern_hello.o \ test_klp_livepatch.o \ test_klp_state.o \ test_klp_state2.o \ diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_extern.c b/tools/testing/selftests/livepatch/test_modules/test_klp_extern.c new file mode 100644 index 000000000000..2a88ae289668 --- /dev/null +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_extern.c @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2024 Lukas Hruska + +#define pr_fmt(fmt) "test_klp_extern_hello: " fmt + +#include +#include +#include + +extern const char *hello_msg \ + KLP_RELOC_SYMBOL(test_klp_extern_hello, test_klp_extern_hello, hello_msg); + +static int hello_get(char *buffer, const struct kernel_param *kp) +{ + return sysfs_emit(buffer, "%s livepatched module.\n", hello_msg); +} + +static struct klp_func funcs[] = { + { + .old_name = "hello_get", + .new_func = hello_get, + }, { } +}; + +static struct klp_object objs[] = { + { + .name = "test_klp_extern_hello", + .funcs = funcs, + }, { } +}; + +static struct klp_patch patch = { + .mod = THIS_MODULE, + .objs = objs, +}; + +static int test_klp_extern_init(void) +{ + return klp_enable_patch(&patch); +} + +static void test_klp_extern_exit(void) +{ +} + +module_init(test_klp_extern_init); +module_exit(test_klp_extern_exit); +MODULE_LICENSE("GPL"); +MODULE_INFO(livepatch, "Y"); +MODULE_AUTHOR("Lukas Hruska "); +MODULE_DESCRIPTION("Livepatch test: external symbol relocation"); diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_extern_hello.c b/tools/testing/selftests/livepatch/test_modules/test_klp_extern_hello.c new file mode 100644 index 000000000000..58ce4e655eee --- /dev/null +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_extern_hello.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2024 Lukas Hruska + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include + +const char *hello_msg = "Hello from"; + +static int hello_get(char *buffer, const struct kernel_param *kp) +{ + return sysfs_emit(buffer, "%s kernel module.\n", hello_msg); +} + +static const struct kernel_param_ops hello_ops = { + .get = hello_get +}; + +module_param_cb(hello, &hello_ops, NULL, 0400); +MODULE_PARM_DESC(hello, "Read only parameter greeting the reader."); + +static int test_klp_extern_hello_init(void) +{ + return 0; +} + +static void test_klp_extern_hello_exit(void) +{ +} + +module_init(test_klp_extern_hello_init); +module_exit(test_klp_extern_hello_exit); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Lukas Hruska "); +MODULE_DESCRIPTION("Livepatch test: external symbol relocation - test module"); -- 2.45.0