strace/printsiginfo.c

262 lines
6.0 KiB
C
Raw Normal View History

/*
* Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
* Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
* Copyright (c) 1993-1996 Rick Sladkey <jrs@world.std.com>
* Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
* Copyright (c) 2001 John Hughes <john@Calva.COM>
* Copyright (c) 2013 Denys Vlasenko <vda.linux@googlemail.com>
* Copyright (c) 2011-2015 Dmitry V. Levin <ldv@altlinux.org>
* Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
* Copyright (c) 2015-2018 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "defs.h"
#include DEF_MPERS_TYPE(siginfo_t)
#include <signal.h>
#include <linux/audit.h>
#include MPERS_DEFS
#include "nr_prefix.c"
#ifndef IN_MPERS
#include "printsiginfo.h"
#endif
#include "xlat/audit_arch.h"
#include "xlat/sigbus_codes.h"
#include "xlat/sigchld_codes.h"
#include "xlat/sigfpe_codes.h"
#include "xlat/sigill_codes.h"
#include "xlat/siginfo_codes.h"
#include "xlat/sigpoll_codes.h"
#include "xlat/sigprof_codes.h"
#include "xlat/sigsegv_codes.h"
#include "xlat/sigsys_codes.h"
#include "xlat/sigtrap_codes.h"
#ifdef SIGEMT
# include "xlat/sigemt_codes.h"
#endif
#ifndef SI_FROMUSER
# define SI_FROMUSER(sip) ((sip)->si_code <= 0)
#endif
static void
printsigsource(const siginfo_t *sip)
{
tprintf(", si_pid=%u, si_uid=%u",
(unsigned int) sip->si_pid,
(unsigned int) sip->si_uid);
}
static void
printsigval(const siginfo_t *sip)
{
tprintf(", si_value={int=%d, ptr=", sip->si_int);
printaddr(ptr_to_kulong(sip->si_ptr));
tprints("}");
}
static void
print_si_code(int si_signo, unsigned int si_code)
{
const char *code = xlookup(siginfo_codes, si_code);
if (!code) {
switch (si_signo) {
case SIGTRAP:
code = xlookup(sigtrap_codes, si_code);
break;
case SIGCHLD:
code = xlookup(sigchld_codes, si_code);
break;
case SIGPOLL:
code = xlookup(sigpoll_codes, si_code);
break;
case SIGPROF:
code = xlookup(sigprof_codes, si_code);
break;
case SIGILL:
code = xlookup(sigill_codes, si_code);
break;
#ifdef SIGEMT
case SIGEMT:
code = xlookup(sigemt_codes, si_code);
break;
#endif
case SIGFPE:
code = xlookup(sigfpe_codes, si_code);
break;
case SIGSEGV:
code = xlookup(sigsegv_codes, si_code);
break;
case SIGBUS:
code = xlookup(sigbus_codes, si_code);
break;
case SIGSYS:
code = xlookup(sigsys_codes, si_code);
break;
}
}
print_xlat_ex(si_code, code, XLAT_STYLE_DEFAULT);
}
static void
print_si_info(const siginfo_t *sip)
{
if (sip->si_errno) {
tprints(", si_errno=");
if ((unsigned) sip->si_errno < nerrnos
&& errnoent[sip->si_errno])
tprints(errnoent[sip->si_errno]);
else
tprintf("%d", sip->si_errno);
}
if (SI_FROMUSER(sip)) {
switch (sip->si_code) {
case SI_USER:
printsigsource(sip);
break;
case SI_TKILL:
printsigsource(sip);
break;
#if defined HAVE_SIGINFO_T_SI_TIMERID && defined HAVE_SIGINFO_T_SI_OVERRUN
case SI_TIMER:
tprintf(", si_timerid=%#x, si_overrun=%d",
sip->si_timerid, sip->si_overrun);
printsigval(sip);
break;
#endif
default:
printsigsource(sip);
if (sip->si_ptr)
printsigval(sip);
break;
}
} else {
switch (sip->si_signo) {
case SIGCHLD:
printsigsource(sip);
tprints(", si_status=");
if (sip->si_code == CLD_EXITED)
tprintf("%d", sip->si_status);
else
printsignal(sip->si_status);
tprintf(", si_utime=%llu, si_stime=%llu",
zero_extend_signed_to_ull(sip->si_utime),
zero_extend_signed_to_ull(sip->si_stime));
break;
case SIGILL: case SIGFPE:
case SIGSEGV: case SIGBUS:
tprints(", si_addr=");
printaddr(ptr_to_kulong(sip->si_addr));
break;
case SIGPOLL:
switch (sip->si_code) {
case POLL_IN: case POLL_OUT: case POLL_MSG:
tprintf(", si_band=%ld",
(long) sip->si_band);
break;
}
break;
#ifdef HAVE_SIGINFO_T_SI_SYSCALL
case SIGSYS: {
/*
* Note that we can safely use the personlity set in
* current_personality here (and don't have to guess it
* based on X32_SYSCALL_BIT and si_arch, for example):
* - The signal is delivered as a result of seccomp
* filtering to the process executing forbidden
* syscall.
* - We have set the personality for the tracee during
* the syscall entering.
* - The current_personality is reliably switched in
* the next_event routine, it is set to the
* personality of the last call made (the one that
* triggered the signal delivery).
* - Looks like there are no other cases where SIGSYS
* is delivered from the kernel so far.
*/
const char *scname = syscall_name(shuffle_scno(
(unsigned) sip->si_syscall));
tprints(", si_call_addr=");
printaddr(ptr_to_kulong(sip->si_call_addr));
tprints(", si_syscall=");
if (scname)
tprintf("%s%s",
nr_prefix(sip->si_syscall), scname);
else
tprintf("%u", (unsigned) sip->si_syscall);
tprints(", si_arch=");
printxval(audit_arch, sip->si_arch, "AUDIT_ARCH_???");
break;
}
#endif
default:
if (sip->si_pid || sip->si_uid)
printsigsource(sip);
if (sip->si_ptr)
printsigval(sip);
}
}
}
#ifdef IN_MPERS
static
#endif
void
printsiginfo(const siginfo_t *sip)
{
if (sip->si_signo == 0) {
tprints("{}");
return;
}
tprints("{si_signo=");
printsignal(sip->si_signo);
tprints(", si_code=");
print_si_code(sip->si_signo, sip->si_code);
#ifdef SI_NOINFO
if (sip->si_code != SI_NOINFO)
#endif
print_si_info(sip);
tprints("}");
}
MPERS_PRINTER_DECL(void, printsiginfo_at,
struct tcb *const tcp, const kernel_ulong_t addr)
{
siginfo_t si;
if (!umove_or_printaddr(tcp, addr, &si))
printsiginfo(&si);
}
static bool
print_siginfo_t(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
{
printsiginfo((const siginfo_t *) elem_buf);
return true;
}
MPERS_PRINTER_DECL(void, print_siginfo_array, struct tcb *const tcp,
const kernel_ulong_t addr, const kernel_ulong_t len)
{
siginfo_t si;
print_array(tcp, addr, len, &si, sizeof(si),
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 04:15:19 +03:00
tfetch_mem, print_siginfo_t, 0);
}