# setfsuid ___________________________________________________
# long sys_setfsuid(uid_t uid)
# long sys_setfsuid16(old_uid_t uid)
#

@define _SYSCALL_SETFSUID_NAME
%(
	name = "setfsuid"
%)

@define _SYSCALL_SETFSUID_ARGSTR
%(
	argstr = sprint(fsuid)
%)

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

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

# dw_setfsuid _____________________________________________________

probe dw_syscall.setfsuid = kernel.function("sys_setfsuid16").call ?,
			    __syscall.setfsuid ?
{
	@_SYSCALL_SETFSUID_NAME
	fsuid = __int32($uid)
	@_SYSCALL_SETFSUID_ARGSTR
}
probe __syscall.setfsuid = kernel.function("sys_setfsuid").call ?
{
	@_SYSCALL_GATE
}
probe dw_syscall.setfsuid.return = kernel.function("sys_setfsuid16").return ?,
				   __syscall.setfsuid.return ?
{
	@_SYSCALL_SETFSUID_NAME
	retstr = return_str(1, $return)
}
probe __syscall.setfsuid.return = kernel.function("sys_setfsuid").return ?
{
	@_SYSCALL_GATE
}

# nd_setfsuid _____________________________________________________

probe nd_syscall.setfsuid = kprobe.function("sys_setfsuid16") ?,
                            __nd_syscall.setfsuid ?
{
	@_SYSCALL_SETFSUID_NAME
	asmlinkage()
	fsuid = int_arg(1)
	@_SYSCALL_SETFSUID_ARGSTR
}
probe __nd_syscall.setfsuid = kprobe.function("sys_setfsuid") ?
{
	@_SYSCALL_GATE
}
probe nd_syscall.setfsuid.return = kprobe.function("sys_setfsuid16").return ?,
                                   __nd_syscall.setfsuid.return ?
{
	@_SYSCALL_SETFSUID_NAME
	retstr = returnstr(1)
}
probe __nd_syscall.setfsuid.return = kprobe.function("sys_setfsuid").return ?
{
	@_SYSCALL_GATE
}
