[RFC,05/19] ktf: Implementation of ktf support for overriding function entry and return.
diff mbox series

Message ID 1fccfe3062ba6c7a2c8171df8e41975e28dae9ec.1565676440.git-series.knut.omang@oracle.com
State New
Headers show
Series
  • Integration of Kernel Test Framework (KTF) into the kernel tree
Related show

Commit Message

Knut Omang Aug. 13, 2019, 6:09 a.m. UTC
From: Alan Maguire <alan.maguire@oracle.com>

This is a very powerful and yet simple way to verify or modify
behaviour of kernel calls. It uses the same technique as the error
injection framework in kernel/fail_function.c to to override function
entry and return. In addition to error injection, this is very useful
to for instance verify that a particular API actually ends up being
called, and in the right way, as an effect of a test.

ktf_override.c:  support for overriding function entry.
ktf_override.h:  Function override support interface for KTF.

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Signed-off-by: Knut Omang <knut.omang@oracle.com>
---
 tools/testing/selftests/ktf/kernel/ktf_override.c | 45 ++++++++++++++++-
 tools/testing/selftests/ktf/kernel/ktf_override.h | 15 +++++-
 2 files changed, 60 insertions(+)
 create mode 100644 tools/testing/selftests/ktf/kernel/ktf_override.c
 create mode 100644 tools/testing/selftests/ktf/kernel/ktf_override.h

Patch
diff mbox series

diff --git a/tools/testing/selftests/ktf/kernel/ktf_override.c b/tools/testing/selftests/ktf/kernel/ktf_override.c
new file mode 100644
index 0000000..7f046c8
--- /dev/null
+++ b/tools/testing/selftests/ktf/kernel/ktf_override.c
@@ -0,0 +1,45 @@ 
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ *    Author: Alan Maguire <alan.maguire@oracle.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ * ktf_override.c: support for overriding function entry.
+ */
+#include <linux/kprobes.h>
+#include <linux/ptrace.h>
+#include "ktf.h"
+#include "ktf_override.h"
+
+asmlinkage void ktf_just_return_func(void);
+
+asm(
+	".type ktf_just_return_func, @function\n"
+	".globl ktf_just_return_func\n"
+	"ktf_just_return_func:\n"
+	"	ret\n"
+	".size ktf_just_return_func, .-ktf_just_return_func\n"
+);
+
+void ktf_post_handler(struct kprobe *kp, struct pt_regs *regs,
+		      unsigned long flags)
+{
+	/*
+	 * A dummy post handler is required to prohibit optimizing, because
+	 * jump optimization does not support execution path overriding.
+	 */
+}
+EXPORT_SYMBOL(ktf_post_handler);
+
+void ktf_override_function_with_return(struct pt_regs *regs)
+{
+	KTF_SET_INSTRUCTION_POINTER(regs, (unsigned long)&ktf_just_return_func);
+}
+EXPORT_SYMBOL(ktf_override_function_with_return);
+NOKPROBE_SYMBOL(ktf_override_function_with_return);
+
+int ktf_register_override(struct kprobe *kp)
+{
+	return register_kprobe(kp);
+}
+EXPORT_SYMBOL(ktf_register_override);
diff --git a/tools/testing/selftests/ktf/kernel/ktf_override.h b/tools/testing/selftests/ktf/kernel/ktf_override.h
new file mode 100644
index 0000000..8a9cf39
--- /dev/null
+++ b/tools/testing/selftests/ktf/kernel/ktf_override.h
@@ -0,0 +1,15 @@ 
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ *    Author: Alan Maguire <alan.maguire@oracle.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ * ktf_override.h: Function override support interface for KTF.
+ */
+#include <linux/kprobes.h>
+#include "ktf.h"
+
+void ktf_post_handler(struct kprobe *kp, struct pt_regs *regs,
+		      unsigned long flags);
+void ktf_override_function_with_return(struct pt_regs *regs);
+int ktf_register_override(struct kprobe *kp);