# request_key ________________________________________________
#
# long sys_request_key(const char __user *_type,
#		     const char __user *_description,
#		     const char __user *_callout_info,
#		     key_serial_t destringid)
# compat_sys_request_key() calls sys_request_key, so don't need probe there.
#

@define _SYSCALL_REQUEST_KEY_NAME
%(
	name = "request_key"
%)

@define _SYSCALL_REQUEST_KEY_ARGSTR
%(
	argstr = sprintf("%s, %s, %p, %d", type_str, description_str,
	                 callout_info_uaddr, destringid)
%)

@define _SYSCALL_REQUEST_KEY_REGARGS
%(
	type_uaddr = pointer_arg(1)
	type_str = user_string_quoted(type_uaddr)
	type_str_unquoted = user_string_nofault(pointer_arg(1))
	description_uaddr = pointer_arg(2)
	description_str = user_string_quoted(description_uaddr)
	description_str_unquoted = user_string_nofault(pointer_arg(2))
	callout_info_uaddr = pointer_arg(3)
	destringid = int_arg(4)
%)

@define _SYSCALL_REQUEST_KEY_REGARGS_STORE
%(
        if (@probewrite(type_uaddr))
          set_pointer_arg(1, type_uaddr)

	if (@probewrite(type_str_unquoted))
	  set_user_string_arg(pointer_arg(1), type_str_unquoted)

	if (@probewrite(description_uaddr))
	  set_pointer_arg(2, description_uaddr)

	if (@probewrite(description_str_unquoted))
	  set_user_string_arg(pointer_arg(2), description_str_unquoted)

	if (@probewrite(callout_info_uaddr))
	  set_pointer_arg(3, callout_info_uaddr)

	if (@probewrite(destringid))
	  set_int_arg(4, destringid)
%)

probe syscall.request_key = dw_syscall.request_key !, nd_syscall.request_key ? {}
probe syscall.request_key.return = dw_syscall.request_key.return !,
                                   nd_syscall.request_key.return ? {}

# dw_request_key _____________________________________________________

probe dw_syscall.request_key = kernel.function("sys_request_key").call ?
{
	@_SYSCALL_REQUEST_KEY_NAME
	type_uaddr = $_type
	type_str = user_string_quoted(type_uaddr)
	description_uaddr = $_description
	description_str = user_string_quoted(description_uaddr)
	callout_info_uaddr = $_callout_info
	destringid = __int32($destringid)
	@_SYSCALL_REQUEST_KEY_ARGSTR
}
probe dw_syscall.request_key.return = kernel.function("sys_request_key").return ?
{
	@_SYSCALL_REQUEST_KEY_NAME
	@SYSC_RETVALSTR($return)
}

# nd_request_key _____________________________________________________

probe nd_syscall.request_key = nd1_syscall.request_key!, nd2_syscall.request_key!, tp_syscall.request_key
  { }

probe nd1_syscall.request_key = kprobe.function("sys_request_key") ?
{
	@_SYSCALL_REQUEST_KEY_NAME
	asmlinkage()
	@_SYSCALL_REQUEST_KEY_REGARGS
	@_SYSCALL_REQUEST_KEY_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.request_key = kprobe.function(@arch_syscall_prefix "sys_request_key") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_REQUEST_KEY_NAME
	@_SYSCALL_REQUEST_KEY_REGARGS
	@_SYSCALL_REQUEST_KEY_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_REQUEST_KEY_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.request_key = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_request_key"), @const("__NR_compat_request_key"))
	@_SYSCALL_REQUEST_KEY_NAME
	@_SYSCALL_REQUEST_KEY_REGARGS
	@_SYSCALL_REQUEST_KEY_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_REQUEST_KEY_REGARGS_STORE %)
}

probe nd_syscall.request_key.return = nd1_syscall.request_key.return!, nd2_syscall.request_key.return!, tp_syscall.request_key.return
  { }

probe nd1_syscall.request_key.return = kprobe.function("sys_request_key").return ?
{
	@_SYSCALL_REQUEST_KEY_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.request_key.return = kprobe.function(@arch_syscall_prefix "sys_request_key").return ?
{
	@_SYSCALL_REQUEST_KEY_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.request_key.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_request_key"), @const("__NR_compat_request_key"))
	@_SYSCALL_REQUEST_KEY_NAME
	@SYSC_RETVALSTR($ret)
}
