Implement decoding of linux socket filter programs
* bpf_sock_filter.c: New file.
* Makefile.am (strace_SOURCES): Add it.
* xlat/skf_ad.in: New file.
* defs.h (decode_sock_fprog, print_sock_fprog): New prototypes.
* fetch_bpf_fprog.c (get_bpf_fprog_size): New mpers printer.
* net.c (print_getsockopt): Use decode_sock_fprog to print
socket filter programs for SO_GET_FILTER socket option.
(print_setsockopt): Use decode_sock_fprog and get_bpf_fprog_size
to print socket filter programs for SO_ATTACH_FILTER
and SO_ATTACH_REUSEPORT_CBPF socket options.
* NEWS: Mention this.
tests: fix print_quoted_hex output of bytes with high bit set
* tests/tests.h (print_quoted_memory, print_quoted_hex): Change the type
of first argument from "const char *" to "const void *".
* tests/print_quoted_string.c: Likewise.
(print_quoted_hex): Print bytes as unsigned char objects to avoid
unwanted sign extension.
* tests/netlink_protocol.c (send_query): Remove the cast of print_quoted_hex
first argument which is now redundant.
net: accept arbitrary option length for getsockopt's SO_LINGER
* net.c (print_linger): Rename to print_set_linger.
(print_setsockopt): Replace print_linger with print_set_linger.
(print_get_linger): New function that accepts arbitrary option length
to match the kernel behaviour.
(print_getsockopt): Replace print_linger with print_get_linger.
* NEWS: Mention this.
net: enhance decoding of getsockopt's optlen argument
As the last argument of getsockopt syscall has read-write semantics,
print both user and kernel values when they differ.
* net.c (SYS_FUNC(getsockopt)): On entering syscall, fetch and save
the length specified to the kernel. On error, print the length saved
on entering. When the saved length and the length returned by the
kernel differ, print both values.
* NEWS: Mention this.
* tests/net-icmp_filter.c (main): Update expected output.
Linux socket filter uses almost the same classic BPF as seccomp filter,
The only difference noticeable from strace PoV is the meaning of generic
multiuse field.
Transform the parser of seccomp filters to a more generic parser
of classic BPF, parametrized with a method of parsing the generic
multiuse field in BPF_STMT.
* bpf_filter.c: New file.
* bpf_filter.h: Likewise.
* bpf_fprog.h: Likewise.
* bpf_seccomp_filter.c: Likewise.
* fetch_bpf_fprog.c: Likewise.
* fetch_seccomp_fprog.c: Remove.
* seccomp_fprog.h: Likewise.
* Makefile.am (strace_SOURCES): Add bpf_filter.c, bpf_filter.h,
bpf_fprog.h, bpf_seccomp_filter.c, and fetch_bpf_fprog.c.
Remove fetch_seccomp_fprog.c and seccomp_fprog.h.
* seccomp.c: Do not include linux/filter.h and xlat header files.
Do not define SECCOMP_RET_ACTION.
(bpf_filter, decode_bpf_code, decode_bpf_stmt, decode_bpf_jump,
print_bpf_filter, print_seccomp_fprog, print_seccomp_filter): Remove.
* defs.h (print_seccomp_filter): Rename to decode_seccomp_fprog.
(SYS_FUNC(seccomp)): Replace print_seccomp_filter
with decode_seccomp_fprog.
* prctl.c (SYS_FUNC(prctl)): Likewise.
* nlattr.c (decode_nlattr_with_data): Replace printstrn
with printstr_ex and set QUOTE_FORCE_HEX flag.
* tests/tests.h (print_quoted_hex): New prototype.
* tests/print_quoted_string.c (print_quoted_hex): New function.
* tests/test_nlattr.h (TEST_NLATTR_OBJECT, TEST_NLATTR_ARRAY): Use it
for updated expected output. Change the type of "plen" variable
to unsigned int.
* tests/nlattr.c (test_nlattr): Update expected output.
tests: print quotation marks in print_quoted_memory
* tests/print_quoted_string.c (print_quoted_memory): Print opening
and closing quotation marks.
* tests/getcwd.c (main): Do not print quotation marks around
print_quoted_string.
* tests/uname.c (main): Likewise.
* tests/keyctl.c (print_quoted_string_limit): Do not print quotation
marks around print_quoted_memory.
* tests/netlink_protocol.c (send_query): Likewise.
* tests/xattr.c (main): Likewise.
tests: robustify create_nl_socket based tests against the race condition
There is a race condition between the moment a netlink socket is created
and the moment it is reported via SOCK_DIAG_BY_FAMILY interface.
Add one more operation on the socket created by create_nl_socket
to increase chances of winning the race.
* tests/create_nl_socket.c (create_nl_socket_ext): Add a getsockopt call
with the created socket.
nios2: fix wrong access to "SP" register in user_pt_regs
Starting with linux commit v4.0-rc4~21^2, "SP" register
is available via regs[PTR_SP] member of struct user_pt_regs.
* linux/nios2/arch_regs.h (nios2_sp_ptr): Change type
to "unsigned int *".
* linux/nios2/arch_regs.c (nios2_sp_ptr): Likewise.
Replace .sp with .regs[PTR_SP].
Check that syscall counters used for tampering are per-tcb.
* tests/qual_fault.c: Include <fcntl.h> for open(),
<sys/param.h> for PATH_MAX, and <sys/wait.h> for wait().
(expfd): Rename to exp_fd, remove const qualifier and initialization.
(gotfd): Rename to got_fd, remove const qualifier and initialization.
(out_fd): New variable, for the expected strace output.
(open_file): New helper function.
(main): Remove st, add num_procs, proc, exp_prefix, got_prefix,
out_prefix, pid_prefix. Remove stat asserts, update argc assert.
Add num_procs, exp_prefix, got_prefix, out_prefix, pid_prefix
initialization. Add per-process loop, open expfd, gotfd, out_fd, pidfd
in each one, print process's pid to pidfd, print exit message to out_fd.
Add wait() call for each forked process.
* tests/qual_fault.test (N): increase to 100 in order to check
concurrent process execution.
(check_fault_injection): Take additional argument for the process
count, pass it to qual_fault, add filename variables for expected strace
output and pid file, pass them to qual_fault.
Specify -ff parameter to strace invocation.
Compare write and strace output for the each process.
Update all check_fault_injection invocation with process count argument,
add an invocation with 4 concurrent processes.
* tests/init.sh (TIMEOUT_DURATION): Raise from 120 to 300.
Co-authored-by: Dmitry V. Levin <ldv@altlinux.org>
* strace.c (init): Adjust argc along with argv,
check that adjusted argc is sane.
* tests/zeroargc.c: New file.
* tests/.gitignore: Add zeroargc.
* tests/Makefile.am (check_PROGRAMS): Likewise.
* tests/options-syntax.test: Check strace error diagnostics
in case of zero argc.
Use program_invocation_name instead of a local progname variable
Emulate program_invocation_name only if it is not provided by libc.
* configure.ac: Check for program_invocation_name variable.
* strace.c (progname): Remove.
[!HAVE_PROGRAM_INVOCATION_NAME] (program_invocation_name): New variable.
(verror_msg, error_msg_and_help): Use it instead of progname.
(init): Initialize program_invocation_name instead of progname.
Introduce a separate header file for generic macros shared between
strace and its tests. Start this unification with ARRAY_SIZE macro.
* defs.h: Include "macros.h".
(ARRAY_SIZE): Move ...
* macros.h: ... to new file.
* Makefile.am (strace_SOURCES): Add it.
* tests/tests.h: Include "macros.h".
(ARRAY_SIZE): Remove. All callers updated.
* tests/nlattr_unix_diag_msg.c: Include "test_nlattr.h".
(test_unix_diag_vfs, test_unix_diag_icons, test_unix_diag_rqlen):
Remove.
(init_unix_diag_msg): Add const qualifier to arguments and variables.
(print_unix_diag_msg, print_uint): New functions.
(main): Use them and macros from test_nlattr.h file.
Add const qualifiers to auto variables and function arguments.
This change does not affect the code generated by the compiler,
the purpose of these "const" qualifiers is to highlight the intent.
* netlink_sock_diag.c (decode_unix_diag_vfs, decode_unix_diag_inode,
decode_unix_diag_rqlen): Add const qualifier to tcp, addr, and len
arguments.
JingPiao Chen [Wed, 28 Jun 2017 01:40:06 +0000 (09:40 +0800)]
netlink: decode AF_UNIX unix_diag_msg attributes
* netlink_sock_diag.c (unix_diag_msg_nla_decoders): New array.
(decode_unix_diag_msg): Use it.
* linux/unix_diag.h (unix_diag_vfs, unix_diag_rqlen): New structures.
Dmitry V. Levin [Fri, 30 Jun 2017 21:38:49 +0000 (21:38 +0000)]
tests: enhance nlattr_netlink_diag_msg test
* tests/nlattr_netlink_diag_msg.c: Include "test_nlattr.h".
(test_netlink_diag_groups, test_netlink_diag_rx_ring,
test_netlink_diag_flags): Remove.
(init_netlink_diag_msg): Add const qualifier to arguments and variables.
(print_netlink_diag_msg, print_xlong): New functions.
(main): Use them and macros from test_nlattr.h file.
Dmitry V. Levin [Fri, 30 Jun 2017 21:38:49 +0000 (21:38 +0000)]
netlink_sock_diag: add const qualifiers
Add const qualifiers to auto variables and function arguments.
This change does not affect the code generated by the compiler,
the purpose of these "const" qualifiers is to highlight the intent.
* netlink_sock_diag.c (print_group, decode_netlink_diag_groups,
decode_netlink_diag_ring, decode_netlink_diag_flags): Add const
qualifier to tcp, addr, and len arguments.
* linux/netlink_diag.h (netlink_diag_ring): New structure.
(NDIAG_FLAG_*): New macros.
* netlink_sock_diag.c: Include "xlat/netlink_socket_flags.h".
(print_group, decode_netlink_diag_groups, decode_netlink_diag_ring,
decode_netlink_diag_flags): New functions.
(netlink_diag_msg_nla_decoders): New array.
(decode_netlink_diag_msg): Use it.
* xlat/netlink_socket_flags.in: New file.
Dmitry V. Levin [Fri, 30 Jun 2017 21:38:49 +0000 (21:38 +0000)]
netlink_sock_diag: print inet_diag_sockid.idiag_if as an interface index
* netlink_sock_diag.c (print_inet_diag_sockid): Print idiag_if field
using print_ifindex.
* tests/nlattr_inet_diag_msg.c: Include <net/if.h>.
(IFINDEX_LO): New macro.
(init_inet_diag_msg): Set inet_diag_sockid.idiag_if field.
(print_inet_diag_msg): Update expected output.
Dmitry V. Levin [Fri, 30 Jun 2017 21:38:49 +0000 (21:38 +0000)]
tests: enhance nlattr_inet_diag_msg test
* tests/test_nlattr.h: New file.
* tests/Makefile.am (libtests_a_SOURCES): Add it.
* tests/nlattr_inet_diag_msg.c: Include "test_nlattr.h".
(test_inet_diag_meminfo, test_inet_diag_vegasinfo,
test_inet_diag_dctcpinfo, test_inet_diag_bbrinfo): Remove.
(address): New variable.
(init_inet_diag_msg): Remove "address" argument, add const qualifier
to all remaining arguments.
(print_inet_diag_msg, print_uint): New functions.
(main): Use macros from test_nlattr.h file.
Dmitry V. Levin [Thu, 29 Jun 2017 09:22:52 +0000 (09:22 +0000)]
netlink_sock_diag: ensure that structure field names are printed properly
* netlink_sock_diag.c (PRINT_FIELD_X): New macro.
(decode_inet_diag_meminfo, decode_tcpvegas_info, decode_tcp_dctcp_info,
decode_tcp_bbr_info): Use it and PRINT_FIELD_U to print structure
fields.
Dmitry V. Levin [Wed, 28 Jun 2017 01:40:01 +0000 (01:40 +0000)]
netlink_sock_diag: add const qualifiers
Add const qualifiers to auto variables and function arguments.
This change does not affect the code generated by the compiler,
the purpose of these "const" qualifiers is to highlight the intent.
* netlink_sock_diag.c (print_meminfo, decode_meminfo,
decode_inet_diag_meminfo, decode_tcpvegas_info, decode_tcp_dctcp_info,
decode_tcp_bbr_info): Add const qualifier to tcp, addr, and len
arguments.
JingPiao Chen [Wed, 28 Jun 2017 01:40:01 +0000 (09:40 +0800)]
netlink: decode AF_INET inet_diag_msg attributes
* linux/inet_diag.h (inet_diag_meminfo, tcpvegas_info,
tcp_dctcp_info, tcp_bbr_info): New structures.
* linux/sock_diag.h (SK_MEMINFO_VARS): New macro.
* netlink_sock_diag.c: Include <linux/sock_diag.h>.
(print_meminfo, decode_meminfo, decode_inet_diag_meminfo,
decode_tcpvegas_info, decode_tcp_dctcp_info, decode_tcp_bbr_info):
New functions.
(inet_diag_msg_nla_decoders): New array.
(decode_inet_diag_msg): Use it.
JingPiao Chen [Tue, 27 Jun 2017 10:10:05 +0000 (18:10 +0800)]
netlink: add a basic netlink attribute parser of AF_INET diag
* linux/inet_diag.h (INET_DIAG_REQ_*): New enum.
* netlink_sock_diag.c: Include "xlat/inet_diag_attrs.h"
and "xlat/inet_diag_req_attrs.h".
(decode_inet_diag_req_compat, decode_inet_diag_req_v2,
decode_inet_diag_msg): Use decode_nlattr.
* xlat/inet_diag_attrs.in: New file.
* xlat/inet_diag_req_attrs.in: Likewise.
JingPiao Chen [Tue, 27 Jun 2017 10:10:04 +0000 (18:10 +0800)]
netlink: add a basic netlink attribute parser of AF_PACKET diag
* linux/packet_diag.h (PACKET_DIAG_*): New enum.
* netlink_sock_diag.c: Include "xlat/packet_diag_attrs.h".
(decode_packet_diag_msg): Use decode_nlattr.
* xlat/packet_diag_attrs.in: New file.
JingPiao Chen [Tue, 27 Jun 2017 10:10:03 +0000 (18:10 +0800)]
netlink: add a basic netlink attribute parser of AF_NETLINK diag
* linux/netlink_diag.h (NETLINK_DIAG_*): New enum.
* netlink_sock_diag.c: Include "xlat/netlink_diag_attrs.h".
(decode_netlink_diag_msg): Use decode_nlattr.
* xlat/netlink_diag_attrs.in: New file.
When the list of headers do not fit into the command line, xargs splits
it into several lists and then passes them to grep. If no headers from
a list match the grep pattern, grep exits with a non-zero status that
causes xargs to exit with a non-zero status, too.
* maint/ioctls_sym.sh: Use "find -exec +" instead of xargs,
ignore its exit status.
JingPiao Chen [Tue, 27 Jun 2017 08:16:56 +0000 (16:16 +0800)]
netlink: pass NLMSG_DONE messages to family specific payload decoders
While many NLMSG_DONE messages indeed have payload containing
just one integer, there are exceptions. Handle this by passing
payloads of NLMSG_DONE messages to family specific netlink
payload decoders.
* netlink.c (print_nlmsghdr): Do not skip family detection
for nlmsg_type == NLMSG_DONE.
(decode_nlmsg_type): Skip family specific type decoders
for type == NLMSG_DONE.
(decode_nlmsg_flags): Skip family specific decoding of flags
for type == NLMSG_DONE.
(decode_netlink_sock_diag): Skip for nlmsg_type == NLMSG_DONE.
Dmitry V. Levin [Mon, 26 Jun 2017 22:30:19 +0000 (22:30 +0000)]
Check -s argument early
* util.c (printstr_ex): Move the check that -s argument
does not exceed -1U / 4 ...
* strace.c (init): ... here.
* tests/options-syntax.test: Check it.
Dmitry V. Levin [Sat, 24 Jun 2017 22:25:28 +0000 (22:25 +0000)]
netlink: provide a fallback definition of NETLINK_SOCK_DIAG
Add a fallback definition of NETLINK_SOCK_DIAG to netlink.h
instead of adding these fallback definitions to every file.
* netlink.h: Provide a fallback definition of NETLINK_SOCK_DIAG.
* socketutils.c: Include "netlink.h" instead of <linux/netlink.h>,
remove fallback definition of NETLINK_SOCK_DIAG.
Dmitry V. Levin [Thu, 22 Jun 2017 21:48:00 +0000 (21:48 +0000)]
Update V4L2_* constants
* xlat/v4l2_buf_types.in: Add V4L2_BUF_TYPE_META_CAPTURE introduced
by linux kernel commit v4.12-rc1~85^2~71.
* xlat/v4l2_device_capabilities_flags.in: Add V4L2_CAP_META_CAPTURE
introduced by the same commit.
* xlat/v4l2_control_flags.in: Add V4L2_CTRL_FLAG_HAS_PAYLOAD,
V4L2_CTRL_FLAG_EXECUTE_ON_WRITE, and V4L2_CTRL_FLAG_MODIFY_LAYOUT
introduced by linux kernel commits v3.17-rc1~112^2~363,
v4.1-rc1~59^2~1^2~63, and v4.12-rc1~85^2~62, respectively.
* xlat/v4l2_control_types.in: Add V4L2_CTRL_TYPE_U8, V4L2_CTRL_TYPE_U16,
and V4L2_CTRL_TYPE_U32 introduced by linux kernel commits
v3.17-rc1~112^2~343 and v3.17-rc1~112^2~112.
Dmitry V. Levin [Thu, 22 Jun 2017 18:08:24 +0000 (18:08 +0000)]
Update SO_* constants
* xlat/sockoptions.in: Add SO_MEMINFO, SO_INCOMING_NAPI_ID,
and SO_COOKIE introduced by linux kernel commits v4.12-rc1~64^3~389,
v4.12-rc1~64^3~341^2, and v4.12-rc1~64^3~221^2~1, respectively.
JingPiao Chen [Wed, 21 Jun 2017 16:24:23 +0000 (00:24 +0800)]
netlink: introduce a basic netlink attributes parser
* linux/unix_diag.h (UNIX_DIAG_*): New enum.
* nlattr.c: New file.
* nlattr.h: Likewise.
* Makefile.am (strace_SOURCES): Add them.
* netlink_sock_diag.c: Include "nlattr.h" and "xlat/unix_diag_attrs.h".
(decode_unix_diag_msg): Use decode_nlattr.
* xlat/unix_diag_attrs.in: New file.
Dmitry V. Levin [Mon, 19 Jun 2017 21:36:47 +0000 (21:36 +0000)]
tests: fix potential unaligned access in netlink_sock_diag.test
Despite our efforts made to avoid unaligned access, gcc appears to be
generating instructions that might cause unaligned access when copying
structures to unaligned addresses.
Fix this issue by replacing copying of structures to unaligned addresses
with memcpy calls.
* tests/netlink_sock_diag.c (INIT_STRUCT): New macro.
(test_odd_family_req, test_odd_family_msg, test_unix_diag_req,
test_unix_diag_msg, test_netlink_diag_req, test_netlink_diag_msg,
test_packet_diag_req, test_packet_diag_msg, test_inet_diag_sockid,
test_inet_diag_req, test_inet_diag_req_v2, test_inet_diag_msg,
test_smc_diag_req, test_smc_diag_msg): Use it to initialize potentially
unaligned struct nlmsghdr. Do not access nlh->nlmsg_len.
Masatake YAMATO [Tue, 13 Jun 2017 08:26:44 +0000 (17:26 +0900)]
netlink: add decoding of NETLINK_GENERIC protocol families
NETLINK_GENERIC is a protocol stacked on netlink protocol.
When a NETLINK_GENERIC subsystem registers its "family" name in the
kernel at run time, an integer value associated with this name is
assigned by the kernel. This integer value is specified in struct
nlmsghdr.nlmsg_type of subsequent communications with the
NETLINK_GENERIC subsystem.
This change implements decoding of NETLINK_GENERIC message types in
symbolic form, for example:
$ ./strace -yy -e 'sendto' ip tcp_metrics > /dev/null
sendto(4<NETLINK:[GENERIC:12345]>, {{len=20, type=tcp_metrics, ...
+++ exited with 0 +++
Note that type= specifying the NETLINK_GENERIC protocol family
is decoded properly.
* netlink.c (decode_nlmsg_type_generic): New function.
(nlmsg_types): Add NETLINK_GENERIC entry.
Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Masatake YAMATO [Tue, 13 Jun 2017 08:26:43 +0000 (17:26 +0900)]
socketutils: build a dynamic xlat table for NETLINK_GENERIC decoding
* configure.ac (AC_CHECK_HEADERS): Add linux/genetlink.h.
* defs.h (genl_families_xlat): New prototype.
* socketutils.c [HAVE_LINUX_GENETLINK_H]: Include <linux/genetlink.h>.
[HAVE_LINUX_GENETLINK_H] (genl_send_dump_families,
genl_parse_families_response): New helper functions.
(genl_families_xlat): New function for building the xlat table
for NETLINK_GENERIC decoding.
Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Masatake YAMATO [Tue, 13 Jun 2017 08:26:41 +0000 (17:26 +0900)]
dyxlat: new interface for building xlat tables dynamically
xlat tables are usually generated at build time.
printxval is suitable for printing struct nlmsghdr.nlmsg_type for
NETLINK_GENERIC protocol. However, the corresponding xlat table
cannot be defined at build time because the values are registered
by the kernel at run time.
This change introduces dyxlat functions for building xlat tables
dynamically. The primary use case is decoding nlmsg_type but this
interface can be used for other purposes.
* defs.h (struct dyxlat): New opaque data type.
(dyxlat_alloc, dyxlat_free, dyxlat_add_pair): New function declarations.
* dyxlat.c: New file.
* Makefile.am (strace_SOURCES): Add it.
Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin [Sat, 17 Jun 2017 21:47:57 +0000 (21:47 +0000)]
Fix macros encosure in a do/while loop
Enclose macros with multiple statements and macros starting
with "if" statement in a do/while loop.
Do not enclose single statement macros in a do/while loop.