Skip to contentSkip to conversation

selftests: livepatch: test livepatching a kprobed function

PATCH Superseded Michael Vetter 2 participants 2 messages Sep 17, 2024 – Sep 19, 2024

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftests: livepatch: test livepatching a kprobed function

The test proves that a function that is being kprobed and uses a post_handler cannot be livepatched.

Only one ftrace_ops with FTRACE_OPS_FL_IPMODIFY set may be registered to any given function at a time.

---
V2:
Save and restore kprobe state.

 tools/testing/selftests/livepatch/Makefile    |  3 +-
 .../selftests/livepatch/test-kprobe.sh        | 65 +++++++++++++++++++
 .../selftests/livepatch/test_modules/Makefile |  3 +-
 .../livepatch/test_modules/test_klp_kprobe.c  | 38 +++++++++++
 4 files changed, 107 insertions(+), 2 deletions(-)
 create mode 100755 tools/testing/selftests/livepatch/test-kprobe.sh
 create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c
tools/testing/selftests/livepatch/Makefile +2 -1
index 35418a4790be..a080eb54a215 100644
@@ -10,7 +10,8 @@ TEST_PROGS := \
 	test-state.sh \
 	test-ftrace.sh \
 	test-sysfs.sh \
-	test-syscall.sh
+	test-syscall.sh \
+	test-kprobe.sh
 
 TEST_FILES := settings
 
tools/testing/selftests/livepatch/test-kprobe.sh new file +65 -0

65 lines not on this page · raw message

tools/testing/selftests/livepatch/test_modules/Makefile +2 -1
index e6e638c4bcba..4981d270f128 100644
@@ -11,7 +11,8 @@ obj-m += test_klp_atomic_replace.o \
 	test_klp_state2.o \
 	test_klp_state3.o \
 	test_klp_shadow_vars.o \
-	test_klp_syscall.o
+	test_klp_syscall.o \
+	test_klp_kprobe.o
 
 # Ensure that KDIR exists, otherwise skip the compilation
 modules:
tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c new file +38 -0
new file mode 100644
index 000000000000..49b579ea1054
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2024 Marcos Paulo de Souza <[email protected]>
+// Copyright (C) 2024 Michael Vetter <[email protected]>
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/kprobes.h>
+
+static bool has_post_handler = true;
+module_param(has_post_handler, bool, 0444);
+
+static void __kprobes post_handler(struct kprobe *p, struct pt_regs *regs,
+				unsigned long flags)
+{
+}
+
+static struct kprobe kp = {
+	.symbol_name = "cmdline_proc_show",
+};
+
+static int __init kprobe_init(void)
+{
+	if (has_post_handler)
+		kp.post_handler = post_handler;
+
+	return register_kprobe(&kp);
+}
+
+static void __exit kprobe_exit(void)
+{
+	unregister_kprobe(&kp);
+}
+
+module_init(kprobe_init)
+module_exit(kprobe_exit)
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Michael Vetter <[email protected]>");
+MODULE_DESCRIPTION("Livepatch test: livepatch kprobed function");
Signature · 1 line
2.46.0
maintainer
#1 lore raw
Quoted 33 lines · Michael Vetter

The test proves that a function that is being kprobed and uses a post_handler cannot be livepatched.

Only one ftrace_ops with FTRACE_OPS_FL_IPMODIFY set may be registered to any given function at a time.

tools/testing/selftests/livepatch/test-kprobe.sh new file +24 -0
@@ -0,0 +1,65 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2024 SUSE
+# Author: Michael Vetter <[email protected]>
+
+. $(dirname $0)/functions.sh
+
+MOD_LIVEPATCH=test_klp_livepatch
+MOD_KPROBE=test_klp_kprobe
+
+setup_config
+
+# Kprobe a function and verify that we can't livepatch that same function
+# when it uses a post_handler since only one IPMODIFY maybe be registered
+# to any given function at a time.
+
+start_test "livepatch interaction with kprobed function with post_handler"
+
+STATE=$(cat /sys/kernel/debug/kprobes/enabled)
+echo 1 > /sys/kernel/debug/kprobes/enabled
+
+load_mod $MOD_KPROBE has_post_handler=true
+load_failing_mod $MOD_LIVEPATCH
+unload_mod $MOD_KPROBE

[...]

Quoted 1 line

+echo $STATE > /sys/kernel/debug/kprobes/enabled

The original state won't get restored when the test fails.

A better solution would be to integrate this into setup_config() and cleanup() in /tools/testing/selftests/livepatch/functions.sh

Also it would be great to avoid hard-coding /sys/kernel/debug. See KLP_SYSFS_DIR="/sys/kernel/livepatch" in /tools/testing/selftests/livepatch/functions.sh

I would replace it with:

KERNEL_SYSFS_DIR="/sys/kernel"
KLP_SYSFS_DIR="$KERNEL_SYSFS_DIR/livepatch"
DEBUG_SYSFS_DIR="$KERNEL_SYSFS_DIR/debug"
KPROBES_SYSFS_DIR="$DEBUG_SYSFS_DIR/kprobes"

It would actually look better with switched ordering:

SYSFS_KERNEL_DIR="/sys/kernel"
SYSFS_KLP_DIR="$SYSFS_KERNEL_DIR/livepatch"
SYSFS_DEBUG_DIR="$SYSFS_KERNEL_DIR/debug"
SYSFS_KPROBES_DIR="$SYSFS_DEBUG_DIR/kprobes"

But this would require renaming KLP_SYSFS_DIR everywhere in a separate (prerequisite) patch. It would be nice but I do not want to force you to do it ;-)

tools/testing/selftests/livepatch/test_modules/Makefile obj-m += test_klp_atomic_replace.o \ · line 15

Quoted 9 lines
tools/testing/selftests/livepatch/test_modules/Makefile +2 -1
@@ -11,7 +11,8 @@ obj-m += test_klp_atomic_replace.o \
 	test_klp_state2.o \
 	test_klp_state3.o \
 	test_klp_shadow_vars.o \
-	test_klp_syscall.o
+	test_klp_syscall.o \
+	test_klp_kprobe.o

Nit: Please, put it in alphabetical order. It is not a big deal.
The ordering helps searching. The list already is almost sorted. And the patch needs to be updated anyway.

Quoted 8 lines
tools/testing/selftests/livepatch/test_modules/Makefile +0 -0
 # Ensure that KDIR exists, otherwise skip the compilation
 modules:
tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c new file +0 -0
new file mode 100644
index 000000000000..49b579ea1054
@@ -0,0 +1,38 @@

[...]

tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c line 38

Quoted 5 lines
tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c new file +5 -0
+module_init(kprobe_init)
+module_exit(kprobe_exit)
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Michael Vetter <[email protected]>");
+MODULE_DESCRIPTION("Livepatch test: livepatch kprobed function");

Nit: The module adds a kprobe. I would invert the logic:

MODULE_DESCRIPTION("Livepatch test: kprobe livepatched function");

Best Regards,
Petr