2016-04-19 01:59:52 +00:00
/*
* Check decoding of setgroups / setgroups32 syscalls .
*
* Copyright ( c ) 2016 Dmitry V . Levin < ldv @ altlinux . org >
2018-06-14 11:00:00 +00:00
* Copyright ( c ) 2016 - 2018 The strace developers .
2016-04-19 01:59:52 +00:00
* All rights reserved .
*
2018-12-10 00:00:00 +00:00
* SPDX - License - Identifier : GPL - 2.0 - or - later
2016-04-19 01:59:52 +00:00
*/
# ifdef __NR_setgroups32
# define SYSCALL_NR __NR_setgroups32
# define SYSCALL_NAME "setgroups32"
# define GID_TYPE unsigned int
# else
# include "tests.h"
2016-08-09 14:38:29 +00:00
# include <asm / unistd.h>
2016-04-19 01:59:52 +00:00
# ifdef __NR_setgroups
# define SYSCALL_NR __NR_setgroups
# define SYSCALL_NAME "setgroups"
# if defined __NR_setgroups32 && __NR_setgroups != __NR_setgroups32
# define GID_TYPE unsigned short
# else
# define GID_TYPE unsigned int
# endif
# endif
# endif
# ifdef GID_TYPE
# include <stdio.h>
# include <unistd.h>
2016-11-28 02:50:03 +03:00
void
printuid ( GID_TYPE id )
{
if ( id = = ( GID_TYPE ) - 1U )
printf ( " -1 " ) ;
else
printf ( " %u " , id ) ;
}
2016-04-19 01:59:52 +00:00
int
main ( void )
{
2016-11-28 02:50:03 +03:00
const char * errstr ;
2016-04-19 01:59:52 +00:00
/* check how the first argument is decoded */
2016-09-19 23:08:36 +00:00
long rc = syscall ( SYSCALL_NR , 0 , 0 ) ;
printf ( " %s(0, NULL) = %s \n " , SYSCALL_NAME , sprintrc ( rc ) ) ;
2016-04-19 01:59:52 +00:00
2017-02-16 16:36:14 +00:00
rc = syscall ( SYSCALL_NR , F8ILL_KULONG_MASK , 0 ) ;
2016-09-19 23:08:36 +00:00
printf ( " %s(0, NULL) = %s \n " , SYSCALL_NAME , sprintrc ( rc ) ) ;
2016-04-19 01:59:52 +00:00
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , 1 , 0 ) ;
printf ( " %s(1, NULL) = %s \n " , SYSCALL_NAME , sprintrc ( rc ) ) ;
2016-04-19 01:59:52 +00:00
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , ( long ) 0xffffffff00000001ULL , 0 ) ;
printf ( " %s(1, NULL) = %s \n " , SYSCALL_NAME , sprintrc ( rc ) ) ;
2016-04-19 01:59:52 +00:00
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , - 1U , 0 ) ;
2016-11-28 02:51:04 +03:00
printf ( " %s(%d, NULL) = %s \n " , SYSCALL_NAME , - 1 , sprintrc ( rc ) ) ;
2016-04-19 01:59:52 +00:00
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , - 1L , 0 ) ;
2016-11-28 02:51:04 +03:00
printf ( " %s(%d, NULL) = %s \n " , SYSCALL_NAME , - 1 , sprintrc ( rc ) ) ;
2016-04-19 01:59:52 +00:00
/* check how the second argument is decoded */
2017-03-16 13:46:36 +00:00
TAIL_ALLOC_OBJECT_CONST_PTR ( const GID_TYPE , g1 ) ;
2016-04-19 01:59:52 +00:00
GID_TYPE * const g2 = tail_alloc ( sizeof ( * g2 ) * 2 ) ;
GID_TYPE * const g3 = tail_alloc ( sizeof ( * g3 ) * 3 ) ;
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , 0 , g1 + 1 ) ;
printf ( " %s(0, []) = %s \n " , SYSCALL_NAME , sprintrc ( rc ) ) ;
2016-04-19 01:59:52 +00:00
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , 1 , g1 ) ;
2016-11-28 02:50:03 +03:00
errstr = sprintrc ( rc ) ;
printf ( " %s(1, [ " , SYSCALL_NAME ) ;
printuid ( * g1 ) ;
printf ( " ]) = %s \n " , errstr ) ;
2016-04-19 01:59:52 +00:00
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , 1 , g1 + 1 ) ;
printf ( " %s(1, %p) = %s \n " , SYSCALL_NAME , g1 + 1 , sprintrc ( rc ) ) ;
2016-04-19 01:59:52 +00:00
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , 1 , - 1L ) ;
printf ( " %s(1, %#lx) = %s \n " , SYSCALL_NAME , - 1L , sprintrc ( rc ) ) ;
2016-04-19 01:59:52 +00:00
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , 2 , g1 ) ;
2016-11-28 02:50:03 +03:00
errstr = sprintrc ( rc ) ;
printf ( " %s(2, [ " , SYSCALL_NAME ) ;
printuid ( * g1 ) ;
print_array: enhance printing of unfetchable object addresses
When umoven_func invocation fails to fetch data, it prints the faulty
address. If this happens to a subsequent umoven_func invocation,
the printed address may be undistinguishable from a valid data printed
by print_func, e.g. when the data is printed in a numeric form like
[0x1, 0x2, 0x3, 0xdefaced].
Fix this source of confusion by moving the printing of the faulty
address from umoven_func to print_array itself. This change renames
umoven_func to tfetch_mem_func and changes its semantics, so that
- tfetch_mem_func never prints anything;
- tfetch_mem_func returns true if the fetch succeeded,
and false otherwise.
* defs.h (print_array): Replace umoven_func argument with
tfetch_mem_func.
* util.c (print_array): Replace umoven_func argument with
tfetch_mem_func, document expected tfetch_mem_func return value
semantics. When tfetch_mem_func returns false, print either addr
or "... /* addr */" depending on the context (inside the array or not).
* bpf.c (print_ebpf_prog, print_bpf_prog_info,
BEGIN_BPF_CMD_DECODER(BPF_PROG_QUERY)): Replace umoven_or_printaddr
argument of print_array with tfetch_mem.
* bpf_filter.c (print_bpf_fprog): Likewise.
* btrfs.c (btrfs_print_logical_ino_container,
btrfs_print_ino_path_container, btrfs_print_qgroup_inherit,
btrfs_ioctl): Likewise.
* dm.c (dm_decode_dm_target_deps): Likewise.
* epoll.c (epoll_wait_common): Likewise.
* file_ioctl.c (file_ioctl): Likewise.
* ipc_sem.c (tprint_sembuf_array): Likewise.
* kexec.c (print_kexec_segments): Likewise.
* mem.c (SYS_FUNC(subpage_prot)): Likewise.
* net.c (print_getsockopt): Likewise.
* netlink.c (decode_nlmsgerr_attr_cookie): Likewise.
* netlink_netlink_diag.c (decode_netlink_diag_groups): Likewise.
* netlink_packet_diag.c (decode_packet_diag_mclist): Likewise.
* netlink_unix_diag.c (decode_unix_diag_inode): Likewise.
* nlattr.c (decode_nla_meminfo): Likewise.
* numa.c (print_nodemask, SYS_FUNC(move_pages),
* perf_ioctl.c (perf_ioctl_query_bpf): Likewise.
* poll.c (decode_poll_entering): Likewise.
* printsiginfo.c (print_siginfo_array): Likewise.
* rtnl_tc.c (decode_tca_stab_data): Likewise.
* sock.c (decode_ifconf): Likewise.
* uid.c (print_groups): Likewise.
* io.c (SYS_FUNC(io_submit), SYS_FUNC(io_getevents)): Replace
umoven_or_printaddr argument of print_array with tfetch_mem.
(tprint_iov_upto): Replace umoven_or_printaddr_ignore_syserror
with tfetch_mem_ignore_syserror.
* v4l2.c (print_v4l2_format_fmt): Replace umoven_or_printaddr argument
of print_array with tfetch_mem.
(print_v4l2_ext_controls): Replace umoven_or_printaddr_ignore_syserror
with tfetch_mem_ignore_syserror.
* mmsghdr.c (fetch_struct_mmsghdr_or_printaddr): Rename
to fetch_struct_mmsghdr_for_print, do not print address, return bool.
(decode_mmsgvec): Replace fetch_struct_mmsghdr_or_printaddr
with fetch_struct_mmsghdr_for_print.
* tests/aio.c (main): Update expected output.
* tests/bpf.c (print_BPF_PROG_QUERY_attr5): Likewise.
* tests/ioctl_perf-success.c (main): Likewise.
* tests/ioctl_v4l2.c (main): Update expected output.
* tests/kexec_load.c (main): Likewise.
* tests/mmsg_name.c (test_mmsg_name): Update expected output.
* tests/move_pages.c (print_page_array, print_node_array): Likewise.
* tests/poll.c (print_pollfd_array_entering): Likewise.
* tests/preadv-pwritev.c (main): Likewise.
* tests/preadv2-pwritev2.c (dumpio): Likewise.
* tests/process_vm_readv_writev.c (print_iov): Likewise.
* tests/pwritev.c (print_iovec): Likewise.
* tests/readv.c (main): Likewise.
* tests/seccomp-filter-v.c
* tests/semop.c (main): Likewise.
* tests/set_mempolicy.c (print_nodes): Likewise.
* tests/setgroups.c (main): Likewise.
* tests/test_nlattr.h (print_nlattr) Likewise.
Co-Authored-by: Eugene Syromyatnikov <evgsyr@gmail.com>
2018-05-29 01:15:19 +00:00
printf ( " , ... /* %p */]) = %s \n " , g1 + 1 , errstr ) ;
2016-04-19 01:59:52 +00:00
g2 [ 0 ] = - 2 ;
g2 [ 1 ] = - 3 ;
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , 2 , g2 ) ;
2016-11-28 02:50:03 +03:00
errstr = sprintrc ( rc ) ;
printf ( " %s(2, [ " , SYSCALL_NAME ) ;
printuid ( g2 [ 0 ] ) ;
printf ( " , " ) ;
printuid ( g2 [ 1 ] ) ;
printf ( " ]) = %s \n " , errstr ) ;
2016-04-19 01:59:52 +00:00
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , 3 , g2 ) ;
2016-11-28 02:50:03 +03:00
errstr = sprintrc ( rc ) ;
printf ( " %s(3, [ " , SYSCALL_NAME ) ;
printuid ( g2 [ 0 ] ) ;
printf ( " , " ) ;
printuid ( g2 [ 1 ] ) ;
print_array: enhance printing of unfetchable object addresses
When umoven_func invocation fails to fetch data, it prints the faulty
address. If this happens to a subsequent umoven_func invocation,
the printed address may be undistinguishable from a valid data printed
by print_func, e.g. when the data is printed in a numeric form like
[0x1, 0x2, 0x3, 0xdefaced].
Fix this source of confusion by moving the printing of the faulty
address from umoven_func to print_array itself. This change renames
umoven_func to tfetch_mem_func and changes its semantics, so that
- tfetch_mem_func never prints anything;
- tfetch_mem_func returns true if the fetch succeeded,
and false otherwise.
* defs.h (print_array): Replace umoven_func argument with
tfetch_mem_func.
* util.c (print_array): Replace umoven_func argument with
tfetch_mem_func, document expected tfetch_mem_func return value
semantics. When tfetch_mem_func returns false, print either addr
or "... /* addr */" depending on the context (inside the array or not).
* bpf.c (print_ebpf_prog, print_bpf_prog_info,
BEGIN_BPF_CMD_DECODER(BPF_PROG_QUERY)): Replace umoven_or_printaddr
argument of print_array with tfetch_mem.
* bpf_filter.c (print_bpf_fprog): Likewise.
* btrfs.c (btrfs_print_logical_ino_container,
btrfs_print_ino_path_container, btrfs_print_qgroup_inherit,
btrfs_ioctl): Likewise.
* dm.c (dm_decode_dm_target_deps): Likewise.
* epoll.c (epoll_wait_common): Likewise.
* file_ioctl.c (file_ioctl): Likewise.
* ipc_sem.c (tprint_sembuf_array): Likewise.
* kexec.c (print_kexec_segments): Likewise.
* mem.c (SYS_FUNC(subpage_prot)): Likewise.
* net.c (print_getsockopt): Likewise.
* netlink.c (decode_nlmsgerr_attr_cookie): Likewise.
* netlink_netlink_diag.c (decode_netlink_diag_groups): Likewise.
* netlink_packet_diag.c (decode_packet_diag_mclist): Likewise.
* netlink_unix_diag.c (decode_unix_diag_inode): Likewise.
* nlattr.c (decode_nla_meminfo): Likewise.
* numa.c (print_nodemask, SYS_FUNC(move_pages),
* perf_ioctl.c (perf_ioctl_query_bpf): Likewise.
* poll.c (decode_poll_entering): Likewise.
* printsiginfo.c (print_siginfo_array): Likewise.
* rtnl_tc.c (decode_tca_stab_data): Likewise.
* sock.c (decode_ifconf): Likewise.
* uid.c (print_groups): Likewise.
* io.c (SYS_FUNC(io_submit), SYS_FUNC(io_getevents)): Replace
umoven_or_printaddr argument of print_array with tfetch_mem.
(tprint_iov_upto): Replace umoven_or_printaddr_ignore_syserror
with tfetch_mem_ignore_syserror.
* v4l2.c (print_v4l2_format_fmt): Replace umoven_or_printaddr argument
of print_array with tfetch_mem.
(print_v4l2_ext_controls): Replace umoven_or_printaddr_ignore_syserror
with tfetch_mem_ignore_syserror.
* mmsghdr.c (fetch_struct_mmsghdr_or_printaddr): Rename
to fetch_struct_mmsghdr_for_print, do not print address, return bool.
(decode_mmsgvec): Replace fetch_struct_mmsghdr_or_printaddr
with fetch_struct_mmsghdr_for_print.
* tests/aio.c (main): Update expected output.
* tests/bpf.c (print_BPF_PROG_QUERY_attr5): Likewise.
* tests/ioctl_perf-success.c (main): Likewise.
* tests/ioctl_v4l2.c (main): Update expected output.
* tests/kexec_load.c (main): Likewise.
* tests/mmsg_name.c (test_mmsg_name): Update expected output.
* tests/move_pages.c (print_page_array, print_node_array): Likewise.
* tests/poll.c (print_pollfd_array_entering): Likewise.
* tests/preadv-pwritev.c (main): Likewise.
* tests/preadv2-pwritev2.c (dumpio): Likewise.
* tests/process_vm_readv_writev.c (print_iov): Likewise.
* tests/pwritev.c (print_iovec): Likewise.
* tests/readv.c (main): Likewise.
* tests/seccomp-filter-v.c
* tests/semop.c (main): Likewise.
* tests/set_mempolicy.c (print_nodes): Likewise.
* tests/setgroups.c (main): Likewise.
* tests/test_nlattr.h (print_nlattr) Likewise.
Co-Authored-by: Eugene Syromyatnikov <evgsyr@gmail.com>
2018-05-29 01:15:19 +00:00
printf ( " , ... /* %p */]) = %s \n " , g2 + 2 , errstr ) ;
2016-04-19 01:59:52 +00:00
g3 [ 0 ] = 0 ;
g3 [ 1 ] = 1 ;
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , 3 , g3 ) ;
2016-11-28 02:50:03 +03:00
errstr = sprintrc ( rc ) ;
printf ( " %s(3, [ " , SYSCALL_NAME ) ;
printuid ( g3 [ 0 ] ) ;
printf ( " , " ) ;
printuid ( g3 [ 1 ] ) ;
printf ( " , ...]) = %s \n " , errstr ) ;
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , 4 , g3 ) ;
2016-11-28 02:50:03 +03:00
errstr = sprintrc ( rc ) ;
printf ( " %s(4, [ " , SYSCALL_NAME ) ;
printuid ( g3 [ 0 ] ) ;
printf ( " , " ) ;
printuid ( g3 [ 1 ] ) ;
printf ( " , ...]) = %s \n " , errstr ) ;
2016-09-19 23:08:36 +00:00
rc = sysconf ( _SC_NGROUPS_MAX ) ;
2016-04-19 01:59:52 +00:00
const unsigned ngroups_max = rc ;
if ( ( unsigned long ) rc = = ngroups_max & & ( int ) ngroups_max > 0 ) {
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , ngroups_max , g3 ) ;
2016-11-28 02:50:03 +03:00
errstr = sprintrc ( rc ) ;
2016-11-28 02:51:04 +03:00
printf ( " %s(%d, [ " , SYSCALL_NAME , ngroups_max ) ;
2016-11-28 02:50:03 +03:00
printuid ( g3 [ 0 ] ) ;
printf ( " , " ) ;
printuid ( g3 [ 1 ] ) ;
printf ( " , ...]) = %s \n " , errstr ) ;
2016-04-19 01:59:52 +00:00
2017-02-16 16:36:14 +00:00
rc = syscall ( SYSCALL_NR , F8ILL_KULONG_MASK | ngroups_max , g3 ) ;
2016-11-28 02:50:03 +03:00
errstr = sprintrc ( rc ) ;
2016-11-28 02:51:04 +03:00
printf ( " %s(%d, [ " , SYSCALL_NAME , ngroups_max ) ;
2016-11-28 02:50:03 +03:00
printuid ( g3 [ 0 ] ) ;
printf ( " , " ) ;
printuid ( g3 [ 1 ] ) ;
printf ( " , ...]) = %s \n " , errstr ) ;
2016-04-19 01:59:52 +00:00
2016-09-19 23:08:36 +00:00
rc = syscall ( SYSCALL_NR , ngroups_max + 1 , g3 ) ;
2016-11-28 02:51:04 +03:00
printf ( " %s(%d, %p) = %s \n " , SYSCALL_NAME ,
2016-09-19 23:08:36 +00:00
ngroups_max + 1 , g3 , sprintrc ( rc ) ) ;
2016-04-19 01:59:52 +00:00
}
puts ( " +++ exited with 0 +++ " ) ;
return 0 ;
}
# else
SKIP_MAIN_UNDEFINED ( " __NR_setgroups " )
# endif