From: Steven Rostedt <rostedt@goodmis.org>
To: Kees Cook <keescook@chromium.org>
Cc: "Daniel Latypov" <dlatypov@google.com>,
"Eric Biederman" <ebiederm@xmission.com>,
"David Gow" <davidgow@google.com>,
"Alexey Dobriyan" <adobriyan@gmail.com>,
"Magnus Groß" <magnus.gross@rwth-aachen.de>,
kunit-dev@googlegroups.com, linux-fsdevel@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-hardening@vger.kernel.org
Subject: Re: [PATCH] binfmt_elf: Introduce KUnit test
Date: Thu, 24 Feb 2022 09:15:50 -0500 [thread overview]
Message-ID: <20220224091550.2b7e8784@gandalf.local.home> (raw)
In-Reply-To: <202202232208.B416701@keescook>
On Wed, 23 Feb 2022 22:13:25 -0800
Kees Cook <keescook@chromium.org> wrote:
> Steven, I want to do fancy live-patch kind or things to replace functions,
> but it doesn't need to be particularly fancy because KUnit tests (usually)
> run single-threaded, etc. It looks like kprobes could almost do it, but
> I don't see a way to have it _avoid_ making a function call.
// This is called just before the hijacked function is called
static void notrace my_tramp(unsigned long ip, unsigned long parent_ip,
struct ftrace_ops *ops,
struct ftrace_regs *fregs)
{
int bit;
bit = ftrace_test_recursion_trylock(ip, parent_ip);
if (WARN_ON_ONCE(bit < 0))
return;
/*
* This uses the live kernel patching arch code to now return
* to new_function() instead of the one that was called.
* If you want to do a lookup, you can look at the "ip"
* which will give you the function you are about to replace.
* Note, it may not be equal to the function address,
* but for that, you can have this:
* ip = ftrace_location(function_ip);
* which will give the ip that is passed here.
*/
klp_arch_set_pc(fregs, new_function);
ftrace_test_recursion_unlock(bit);
}
static struct ftrace_ops my_ops = {
.func = my_tramp;
.flags = FTRACE_OPS_FL_IPMODIFY |
#ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
FTRACE_OPS_FL_SAVE_REGS |
#endif
FTRACE_OPS_FL_DYNAMIC;
};
// Assuming you know the function ip you want to hijack
register_my_kutest_ip(unsigned long func_ip)
{
unsigned long ip;
int ret;
ip = ftrace_location(func_ip);
if (!ip) // not a ftrace function?
return;
ret = ftrace_set_filter_ip(&my_ops, ip, 0, 0);
if (ret < 0)
return;
// you can register more than one ip if the last parameter
// is zero (1 means to reset the list)
ret = register_ftrace_function(&my_ops);
if (ret < 0)
return;
}
That's pretty much it. Note, I did not compile nor test the above, so there
may be some mistakes.
For more information about how to use ftrace, see
Documentation/trace/ftrace-uses.rst
Which should probably get a section on how to do kernel patching.
-- Steve
next prev parent reply other threads:[~2022-02-24 14:15 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-24 5:43 [PATCH] binfmt_elf: Introduce KUnit test Kees Cook
2022-02-24 6:07 ` Daniel Latypov
2022-02-24 6:13 ` Kees Cook
2022-02-24 7:57 ` David Gow
2022-02-24 14:15 ` Steven Rostedt [this message]
2022-03-01 1:48 ` Daniel Latypov
2022-03-01 3:17 ` Kees Cook
2022-03-01 4:21 ` Steven Rostedt
2022-03-01 6:42 ` Daniel Latypov
2022-03-01 15:06 ` Steven Rostedt
2022-02-24 7:41 ` David Gow
2022-02-24 9:45 ` Alexey Dobriyan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220224091550.2b7e8784@gandalf.local.home \
--to=rostedt@goodmis.org \
--cc=adobriyan@gmail.com \
--cc=davidgow@google.com \
--cc=dlatypov@google.com \
--cc=ebiederm@xmission.com \
--cc=keescook@chromium.org \
--cc=kunit-dev@googlegroups.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=magnus.gross@rwth-aachen.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).