# setgid _____________________________________________________
#
# long sys_setgid(gid_t gid)
# long sys_setgid16(old_gid_t gid)
#

@define _SYSCALL_SETGID_NAME
%(
	name = "setgid"
%)

@define _SYSCALL_SETGID_ARGSTR
%(
	argstr = sprint(gid)
%)

@define _SYSCALL_GATE
%(
    %( arch == "x86_64" %?
	// There are actually 3 x86_64 setgid syscalls numbers (which
	// get mapped to 2 syscall functions:
	//   - __NR_setgid which gets mapped to sys_setgid()
	//   - __NR_ia32_setgid32 which also gets mapped to
	//     sys_setgid() 
	//   - __NR_ia32_setgid which gets mapped to sys_setgid16()
	//     (which is a wrapper around sys_setgid())
	// So, we need to avoid sys_setgid() calls that come from
	// sys_setgid16().
	@__syscall_compat_gate(@const("__NR_setgid"),
			       @const("__NR_ia32_setgid32"))
    %:
	%( arch == "i386" %?
	    // On i386, there are 2 setgid syscall numbers:
	    //   - __NR_setgid which gets mapped to sys_setgid16
	    //   - __NR_setgid32 which gets mapped to sys_setgid
	    // Since this gets called from a probe on sys_setgid, we'll
	    // make sure this is really __NR_setgid32.
	    @__syscall_nr_gate(@const("__NR_setgid32"))
	%)
    %)
%)

@define _SYSCALL_SETGID_REGARGS
%(
	gid = int_arg(1)
%)

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

# dw_setgid _____________________________________________________

probe dw_syscall.setgid = kernel.function("sys_setgid16").call ?,
			  __syscall.setgid ?
{
	@_SYSCALL_SETGID_NAME
	gid = __int32($gid)
	@_SYSCALL_SETGID_ARGSTR
}
probe __syscall.setgid = kernel.function("sys_setgid").call ?
{
	@_SYSCALL_GATE
}
probe dw_syscall.setgid.return = kernel.function("sys_setgid16").return ?,
				 __syscall.setgid.return ?
{
	@_SYSCALL_SETGID_NAME
	@SYSC_RETVALSTR($return)
}
probe __syscall.setgid.return = kernel.function("sys_setgid").return ?
{
	@_SYSCALL_GATE
}

# nd_setgid _____________________________________________________

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

probe nd1_syscall.setgid = kprobe.function("sys_setgid16") ?,
			   __nd1_syscall.setgid ?
{
	@_SYSCALL_SETGID_NAME
	asmlinkage()
	@_SYSCALL_SETGID_REGARGS
	@_SYSCALL_SETGID_ARGSTR
}
probe __nd1_syscall.setgid = kprobe.function("sys_setgid") ?
{
	@_SYSCALL_GATE
}

/* kernel 4.17+ */
probe nd2_syscall.setgid = kprobe.function(@arch_syscall_prefix "sys_setgid") ?,
                           kprobe.function(@arch_syscall_prefix "sys_setgid16") ?
{
	__set_syscall_pt_regs(pointer_arg(1))
	@_SYSCALL_SETGID_NAME
	@_SYSCALL_SETGID_REGARGS
	@_SYSCALL_SETGID_ARGSTR
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.setgid = __tp_syscall.setgid, __tp_syscall.setgid16
{
	__set_syscall_pt_regs($regs)
	@_SYSCALL_SETGID_NAME
	@_SYSCALL_SETGID_REGARGS
	@_SYSCALL_SETGID_ARGSTR
}
probe __tp_syscall.setgid = kernel.trace("sys_enter")
{
	@__syscall_compat_gate(@const("__NR_setgid"), @const("__NR_compat_setgid32"))
}
probe __tp_syscall.setgid16 = kernel.trace("sys_enter")
{
	@__compat_syscall_gate(@const("__NR_compat_setgid"))
}

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

probe nd1_syscall.setgid.return = kprobe.function("sys_setgid16").return ?,
				  __nd1_syscall.setgid.return ?
{
	@_SYSCALL_SETGID_NAME
	@SYSC_RETVALSTR(returnval())
}
probe __nd1_syscall.setgid.return = kprobe.function("sys_setgid").return ?
{
	@_SYSCALL_GATE
}

/* kernel 4.17+ */
probe nd2_syscall.setgid.return =
	kprobe.function(@arch_syscall_prefix "sys_setgid").return ?,
	kprobe.function(@arch_syscall_prefix "sys_setgid16").return ?
{
	@_SYSCALL_SETGID_NAME
	@SYSC_RETVALSTR(returnval())
}

/* kernel 3.5+, but undesirable because it affects all syscalls */
probe tp_syscall.setgid.return = __tp_syscall.setgid.return, __tp_syscall.setgid16.return
{
	__set_syscall_pt_regs($regs)
	@_SYSCALL_SETGID_NAME
	@SYSC_RETVALSTR($ret)
}
probe __tp_syscall.setgid.return = kernel.trace("sys_exit")
{
	@__syscall_compat_gate(@const("__NR_setgid"), @const("__NR_compat_setgid32"))
}
probe __tp_syscall.setgid16.return = kernel.trace("sys_exit")
{
	@__compat_syscall_gate(@const("__NR_compat_setgid"))
}
