# capset _____________________________________________________
/*
 *  NOTE
 *   this  is  probably not a good function
 *   to probe. The  structures  are  always
 *   changing.  It also  seems  like  it is
 *   not really  used. Cscope  produced  no
 *   reference  of  this  function  in  the
 *   kernel  (returned no callers). Perhaps
 *   cap_get_proc / cap_set_proc are better
 *   functions to export.
 */
# long sys_capset(cap_user_header_t header, const cap_user_data_t data)

@define _SYSCALL_CAPSET_NAME
%(
	name = "capset"
%)

@define _SYSCALL_CAPSET_ARGSTR
%(
	argstr = sprintf("%p, %p", header_uaddr, data_uaddr)
%)

@define _SYSCALL_CAPSET_REGARGS
%(
	header_uaddr = pointer_arg(1)
	data_uaddr = pointer_arg(2)
%)

@define _SYSCALL_CAPSET_REGARGS_STORE
%(
        if (@probewrite(header_uaddr))
          set_pointer_arg(1, header_uaddr)

	if (@probewrite(data_uaddr))
	  set_pointer_arg(2, data_uaddr)
%)

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

# dw_capset _____________________________________________________

probe dw_syscall.capset = kernel.function("sys_capset").call
{
	@_SYSCALL_CAPSET_NAME
	header_uaddr = $header
	data_uaddr = $data
	@_SYSCALL_CAPSET_ARGSTR
}
probe dw_syscall.capset.return = kernel.function("sys_capset").return
{
	@_SYSCALL_CAPSET_NAME
	@SYSC_RETVALSTR($return)
}

# nd_capset _____________________________________________________

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

probe nd1_syscall.capset = kprobe.function("sys_capset") ?
{
	@_SYSCALL_CAPSET_NAME
	asmlinkage()
	@_SYSCALL_CAPSET_REGARGS
	@_SYSCALL_CAPSET_ARGSTR
}

/* kernel 4.17+ */
probe nd2_syscall.capset = kprobe.function(@arch_syscall_prefix "sys_capset")
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_CAPSET_NAME
	@_SYSCALL_CAPSET_REGARGS
	@_SYSCALL_CAPSET_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_CAPSET_REGARGS_STORE %)
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.capset = kernel.trace("sys_enter")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_capset"), @const("__NR_compat_capset"))
	@_SYSCALL_CAPSET_NAME
	@_SYSCALL_CAPSET_REGARGS
	@_SYSCALL_CAPSET_ARGSTR
},
{
        %( @_IS_SREG_KERNEL %? @_SYSCALL_CAPSET_REGARGS_STORE %)
}

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

probe nd1_syscall.capset.return = kprobe.function("sys_capset").return ?
{
	@_SYSCALL_CAPSET_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 4.17+ */
probe nd2_syscall.capset.return = kprobe.function(@arch_syscall_prefix "sys_capset").return
{
	@_SYSCALL_CAPSET_NAME
	@SYSC_RETVALSTR(returnval())
}
 
/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.capset.return = kernel.trace("sys_exit")
{
	__set_syscall_pt_regs($regs)
	@__syscall_compat_gate(@const("__NR_capset"), @const("__NR_compat_capset"))
	@_SYSCALL_CAPSET_NAME
	@SYSC_RETVALSTR($ret)
}
