# add_key ____________________________________________________
# long sys_add_key(const char __user *_type,
#             const char __user *_description,
#             const void __user *_payload,
#             size_t plen,
#             key_serial_t ringid)
#

@define _SYSCALL_ADD_KEY_NAME
%(
	name = "add_key"
%)

@define _SYSCALL_ADD_KEY_ARGSTR
%(
	argstr = sprintf("%s, %s, %s, %u, %d",
	                 user_string_quoted(type_uaddr),
	                 user_string_quoted(description_uaddr),
	                 user_buffer_quoted(payload_uaddr, plen, syscall_string_trunc),
	                 plen, ringid)
%)

@define _SYSCALL_ADD_KEY_REGARGS
%(
	type_uaddr = pointer_arg(1)
	description_uaddr = pointer_arg(2)
	payload_uaddr = pointer_arg(3)
	plen = ulong_arg(4)
	ringid = int_arg(5)
%)

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

# dw_add_key _____________________________________________________

probe dw_syscall.add_key = kernel.function("sys_add_key").call ?
{
	@_SYSCALL_ADD_KEY_NAME
	type_uaddr = $_type
	description_uaddr = $_description
	payload_uaddr = $_payload
	plen = __ulong($plen)
	ringid = __int32($ringid)
	@_SYSCALL_ADD_KEY_ARGSTR
}
probe dw_syscall.add_key.return = kernel.function("sys_add_key").return ?
{
	@_SYSCALL_ADD_KEY_NAME
	@SYSC_RETVALSTR($return)
}

# nd_add_key _____________________________________________________

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

probe nd1_syscall.add_key = kprobe.function("sys_add_key") ?
{
	@_SYSCALL_ADD_KEY_NAME
	asmlinkage()
	@_SYSCALL_ADD_KEY_REGARGS
	@_SYSCALL_ADD_KEY_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.add_key = kprobe.function(@arch_syscall_prefix "sys_add_key") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_ADD_KEY_NAME
	@_SYSCALL_ADD_KEY_REGARGS
	@_SYSCALL_ADD_KEY_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.add_key = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_add_key"), @const("__NR_compat_add_key"))
	@_SYSCALL_ADD_KEY_NAME
	@_SYSCALL_ADD_KEY_REGARGS
	@_SYSCALL_ADD_KEY_ARGSTR
}

probe nd_syscall.add_key.return = nd1_syscall.add_key.return!, nd2_syscall.add_key.return!, tp_syscall.add_key.return
  { }
  
probe nd1_syscall.add_key.return = kprobe.function("sys_add_key").return ?
{
	@_SYSCALL_ADD_KEY_NAME
	@SYSC_RETVALSTR(returnval())
}

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