On Mon, Feb 28, 2022 at 05:48:27PM -0800, Daniel Latypov wrote: > On Thu, Feb 24, 2022 at 6:15 AM Steven Rostedt <rostedt@xxxxxxxxxxx> wrote: > > > > On Wed, 23 Feb 2022 22:13:25 -0800 > > Kees Cook <keescook@xxxxxxxxxxxx> 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); > > Ahah! > This was the missing bit. > > David and I both got so excited by this we prototyped experimental > APIs around this over the weekend. > He also prototyped a more intrusive alternative to using ftrace and > kernel livepatch since they don't work on all arches, like UML. Yay! That's excellent. I didn't have time to try this myself, so I'm delighted to see y'all got it working. Nice! > We're splitting up responsibility and will each submit RFCs to the > list in the coming days. > I'll send the ftrace one based on this. > He'll send his alternative one as well. > I think we'll end up having both approaches as they both have their usecases. > > It'll take some iteration to bikeshed stuff like names and make them > more consistent with each other. > I've posted my working copy on Gerrit for now, if people want to take > a look: https://kunit-review.googlesource.com/c/linux/+/5109 Great! I'll go comment on it there. -- Kees Cook