Commit Graph

458 Commits

Author SHA1 Message Date
Dmitry Vyukov
986fa4971c prog: don't squash objects that contain pointers
Squashing pointers creates several problems:
- we need to generate pointer types on the fly,
  something we don't do in any other contexts,
  it complicates other changes
- pointers are very special as values,
  if we change size of the surrounding blobs,
  offsets changes and we will use something that's
  not a pointer as pointer and vise versa,
  boths things are most likley very bad as inputs
- squashing/any implementation is just too complex

This disqualifies several types for squashing:

    <         alloc_pd_cmd
    <         arpt_replace
    <         array[cmsghdr_rds]
    <         create_cq_cmd
    <         create_flow_cmd
    <         create_qp_cmd
    <         create_srq_cmd
    <         ebt_counters_info
    <         ip6t_replace
    <         ipt_replace
    <         mlx5_alloc_pd_cmd
    <         mlx5_create_dv_qp_cmd
    <         open_xrcd_cmd
    <         post_recv_cmd
    <         post_send_cmd
    <         post_srq_recv_cmd
    <         query_qp_cmd
    <         query_srq_cmd
    <         reg_mr_cmd
    <         rereg_mr_cmd
    <         resize_cq_cmd
    <         usbdevfs_urb
    <         vhost_memory
    <         vusb_connect_descriptors

and adds few new:

    >         binder_objects
    >         query_qp_resp
    >         resize_cq_resp
    >         usb_bos_descriptor
    >         usb_string_descriptor

Overall this looks sane.
Majority is still unchanged.
2020-05-01 13:31:17 +02:00
Dmitry Vyukov
4c9b435b67 prog: reduce code nesting level 2020-04-28 16:52:22 +02:00
Dmitry Vyukov
15862c0b45 prog: simplify hints test
It assumes too much about prog internals.
Parse complete program instead.
2020-04-28 16:52:22 +02:00
Dmitry Vyukov
dcd4b58785 prog: make program parsing more permissive
Don't error on wrong vma with value in non strict mode.
Add more tests and fix use of cmp package (prog.Syscall is not comparable anymore).
2020-04-28 16:52:22 +02:00
Dmitry Vyukov
0ce7569ee7 pkg/compiler: deduplicate Types in descriptions
Add prog.Ref Type that serves as a proxy for real types
and allows to deduplicate Types in generated descriptions.
The Ref type is effectively an index in an array of types.
Just before serialization pkg/compiler replaces real types
with the Ref types and prepares corresponding array of real types.
When a Target is registered in prog package, we do the opposite
operation and replace Ref's with the corresponding real types.

This brings improvements across the board:
compiler memory consumption is reduced by 15%,
test building time by 25%, descriptions size by 33%.

Before:
$ du -h sys/linux/gen
54M	sys/linux/gen

$ time GOMAXPROCS=1 go test -p=1 -c ./prog
real	0m54.200s
real	0m53.883s

$ time GOMAXPROCS=1 go install -p=1 ./tools/syz-execprog
real	0m27.911s
real	0m27.767s

$ TIME="%e %P %M" GOMAXPROCS=1 time go tool compile ./sys/linux/gen
20.59 100% 3200016
20.97 100% 3445976
20.25 100% 3209684

After:
$ du -h sys/linux/gen
36M	sys/linux/gen

$ time GOMAXPROCS=1 go test -p=1 -c ./prog
real	0m42.290s
real	0m43.230s

$ time GOMAXPROCS=1 go install -p=1 ./tools/syz-execprog
real	0m24.337s
real	0m24.727s

$ TIME="%e %P %M" GOMAXPROCS=1 time go tool compile ./sys/linux/gen
19.11 100% 2764952
19.66 100% 2787624
19.35 100% 2749376

Update #1580
2020-04-26 05:58:31 +02:00
Dmitry Vyukov
a113ba3838 prog: dedup code in ForeachType 2020-04-25 07:40:50 +02:00
Dmitry Vyukov
91db3ed8ce prog: add ignore_return and breaks_returns call attribtues
We had these hard-coded for fuchsia and linux accordingly.
Replace with call attributes.
2020-04-19 10:26:57 +02:00
Dmitry Vyukov
90d17ab898 prog: introduce call attributes
Add common infrastructure for syscall attributes.
Add few attributes we want, but they are not implemented for now
(don't affect behavior, this will follow).
2020-04-19 10:26:57 +02:00
Dmitry Vyukov
67234372ef prog: refactor target.MakeMmap
Make MakeMmap return more than 1 call.
This is a preparation for future changes.
Also remove addr/size as they are effectively
always the same and can be inferred from the target
(will also conflict with the future changes).
Also rename to MakeDataMmap to better represent
the new purpose: it's just some arbitrary mmap,
but rather mapping of the data segment.
2020-04-18 14:35:45 +02:00
Dmitry Vyukov
8950ec3f51 prog: add MaxArgs const
Move the const from the compiler.
In preparation for future changes.
2020-03-31 15:28:17 +02:00
Dmitry Vyukov
2ab437bb1e prog: improve TestDeserializeHelper
1. Allow to not provide Out if it's the same as In.
2. Always check Out.
2020-03-24 08:43:00 +01:00
Dmitry Vyukov
2a504af1a3 prog: add test for truncateToBitSize 2020-03-24 08:42:59 +01:00
Dmitry Vyukov
f211a294e5 prog: don't use spaces in hints test names
Spaces are replaced with _ in testing output.
The first thing you do on failure is search for the test name.
No match. Figure out spaces were replaced. Replace each _ in the name with space.
Counterproductive.
2020-03-24 08:42:59 +01:00
Dmitry Vyukov
624509aadb prog: add helper to hints tests
Add a help to create uint64 sets with less code.
2020-03-24 08:42:59 +01:00
Dmitry Vyukov
97bc55cead pkg/compiler: check that flags values fit into base type
flags[foo, int8]
foo = 0x12345678

is always an error, detect these cases.
Found some bugs in mptcp, packet sockets, kvm.
2020-03-17 21:19:13 +01:00
Dmitry Vyukov
80d43738f1 prog: rename target.SanitizeCall to Neutralize
We will need a wrapper for target.SanitizeCall that will do more
than just calling the target-provided function. To avoid confusion
and potential mistakes, give the target function and prog function
different names. Prog package will continue to call this "sanitize",
which will include target's "neutralize" + more.
Also refactor API a bit: we need a helper function that sanitizes
the whole program because that's needed most of the time.

Fixes #477
Fixes #502
2020-03-17 21:19:13 +01:00
Dmitry Vyukov
a2f9a44649 prog: export deserialization test helper for sys/{linux,openbsd}
sys/{linux,openbsd} duplicate deserialization test logic as well.
Export and reuse the existing helper function.
2020-03-17 21:19:13 +01:00
Dmitry Vyukov
0a4d69469b prog: factor out common code in tests
Factor out a common test helper for tests that deserialize and check programs.
2020-03-17 21:19:13 +01:00
Dmitry Vyukov
924f760604 pkg/compiler: ensure consistency of syscall argument types
Ensure that we don't have conflicting sizes for the same argument
of the same syscall, e.g.:

foo$1(a int16)
foo$2(a int32)

This is useful for several reasons:
 - we will be able avoid morphing syscalls into other syscalls
 - we will be able to figure out more precise sizes for args
   (lots of them are implicitly intptr, which is the largest
   type on most important arches)
 - found few bugs in linux descriptions

Update #477
Update #502
2020-03-17 21:19:13 +01:00
Dmitry Vyukov
9b1f3e6653 prog: control program length
We have _some_ limits on program length, but they are really soft.
When we ask to generate a program with 10 calls, sometimes we get
100-150 calls. There are also no checks when we accept external
programs from corpus/hub. Issue #1630 contains an example where
this crashes VM (executor limit on number of 1000 resources is
violated). Larger programs also harm the process overall (slower,
consume more memory, lead to monster reproducers, etc).

Add a set of measure for hard control over program length.
Ensure that generated/mutated programs are not too long;
drop too long programs coming from corpus/hub in manager;
drop too long programs in hub.
As a bonus ensure that mutation don't produce programs with
0 calls (which is currently possible and happens).

Fixes #1630
2020-03-13 13:16:53 +01:00
Dmitry Vyukov
7fb694ef82 prog: sort enabled syscalls for determinism
Makes tests deterministic and syz-mutate with -seed flag.

Pointed out by Jordan Frank (@jwf).
2020-03-06 09:01:48 +01:00
Dmitry Vyukov
b6ed147834 prog: dump orig prog if Deserialize panics
We are seeing some one-off panics during Deserialization
and it's unclear if it's machine memory corrpution or
an actual bug in prog. I leam towards machine memory corruption
but it's impossible to prove without seeing the orig program.

Move git revision to prog and it's more base package
(sys can import prog, prog can't import sys).
2020-02-21 10:22:07 +01:00
Dmitry Vyukov
135c18aadb tools: add script that checks copyright headers
Fixes #1604
2020-02-18 16:05:10 +01:00
Dmitry Vyukov
6805e90523 executor: increase input buffer size
I bumped input buffer size on Go side in:
a2af37f0 prog: increase encodingexec buffer size
But I forgot to increase the size on the executor side.
Do this and add comments re keeping them in sync.
2020-02-10 10:51:25 +01:00
Dmitry Vyukov
eb99c7d3da prog: remove use of unsafe
Unsafe is, well, unsafe.
Plus it fails under the new checkptr mode in go1.14.
Remove use of unsafe.

No statistically significant change in performance:

name            old time/op  new time/op  delta
StoreLoadInt-8  21.2ns ± 5%  21.6ns ± 9%   ~     (p=0.136 n=20+20)
2020-02-09 14:09:19 +02:00
Dmitry Vyukov
dd56146d26 prog: remove unused ResourceDesc.Type 2020-01-26 11:30:18 +01:00
Dmitry Vyukov
2fd6bd0d1b prog: reduce len mutation priority
Mutating LenType only produces "incorrect" results according to descriptions,
we generally try to do it less often (there is infinite space of incorrect inputs).
2020-01-18 21:02:24 +01:00
Dmitry Vyukov
de577addbf prog: tune flags generation/mutation
Tune flags to generate more sane values over insane values
based on examination of results for common cases.
2020-01-18 21:02:24 +01:00
Dmitry Vyukov
a2af37f08c prog: increase encodingexec buffer size
Some of the programs involving netfilter syscalls
produce errors about insufficient buffer size. Bump it more.
2020-01-18 21:02:24 +01:00
Dmitry Vyukov
4272e40b21 prog: don't add fallback coverage after clone+ptrace
clone+ptrace combo cause fallback coverage explosion under gvisor.
Mechanics of that are unclear, but effect is very clear.
2020-01-15 18:04:21 +01:00
Dmitry Vyukov
b803944b58 prog: don't add fallback coverage after prctl
The same reason as with seccomp.
2020-01-15 17:35:31 +01:00
Dmitry Vyukov
36860d8b25 prog: increase array size during mutation
We have strict upper bound of array size 10.
However, for netlink we frequently need lots of attributes in arrays.
Add a mutation that increases array size by few elements
without an upper bound (we should not grow them infinitely due
to coverage feedback?).
2020-01-07 14:31:14 +01:00
Dmitry Vyukov
d2bde102ff pkg/compiler: fix another bitfield layout bug
See the added test for details.
2020-01-07 10:02:09 +01:00
Dmitry Vyukov
d646e21ff4 prog: fix tests for string enforcement
String value enforcement broke a number of tests
where we use different values.
Be more string as to what string values we use in tests.
Required to add tmpfs descriptions to test syz_mount_image.
Also special-casing AF_ALG algorithms as these are auto-generated.
2020-01-05 12:50:29 +01:00
Dmitry Vyukov
026aaeb2b5 prog: don't mutate strings with enumerated values
Strings with enumerated values are frequently file names
or have complete enumeration of relevant values.
Mutating complete enumeration if not very profitable.
Mutating file names leads to escaping paths and
fuzzer messing with things it is not supposed to mess with as in:

r0 = openat$apparmor_task_exec(0xffffffffffffff9c, &(0x7f0000000440)='/proc/self//exe\x00', 0x3, 0x0)
2020-01-05 11:46:35 +01:00
Dmitry Vyukov
21d4f173c5 sys/linux: improve ipv4/ipv6 vnet descriptions
1. Use optional[T] instead of array[T, 0:1].
2. Deduplicate 3 copies of ARP packet.
3. Deduplicate IPOPT_LSRR/IPOPT_SSRR/IPOPT_RR.
4. More precise description of IPOPT_TIMESTAMP/IPOPT_LSRR/IPOPT_SSRR/IPOPT_RR.
5. Don't use IPOPT_END/IPOPT_NOOP in generic option (they have different format).
6. Restrict cipso doi values.
7. Fix IPOPT_RA value type (int16 instead of int32).
8. Match ipv4/ipv6 packet type with payload.
9. Prefer 0 frag_off for ipv4 packets (they are extremely hard to get right).
2020-01-03 16:11:49 +01:00
Dmitry Vyukov
6b36d33868 syz-manager: corpus rotation
Use a random subset of syscalls/corpus/coverage for each individual VM run.
Hypothesis is that this should allow fuzzer to get more coverage
find more bugs in saturated state (stuck in local optimum).
See the issue and comments for details.

Update #1348
2019-12-30 16:37:38 +01:00
Dmitry Vyukov
3203771359 prog: fix a typo in a comment 2019-12-30 16:37:38 +01:00
Dmitry Vyukov
bfdfc2603c prog: don't fail decoding on non-default out args
We get them in cross-compilation test where an out const
arg has different values in different archs.
No reason to fail deserialization in that case, replace with default
arg instead.
2019-12-21 14:53:37 +01:00
Dmitry Vyukov
ae5ed0b140 pkg/compiler: fix bitfield layout bug
Fixes #1542

Found thanks to syz-check. Update #590
2019-12-20 16:45:34 +01:00
Dmitry Vyukov
20c6855484 prog: refactor bitfields representation
All callers of BitfieldMiddle just want static size (0 for middle).
Make it so: Size for middle bitfields just returns 0. Removes lots of if's.
Introduce Type.UnitSize, which now holds the underlying type for bitfields.
This will be needed to fix #1542 b/c even if UnitSize=4 for last bitfield
Size can be anywhere from 0 to 4 (not necessary equal to UnitSize due to overlapping).
2019-12-19 19:32:45 +01:00
Dmitry Vyukov
f950e82d47 prog: export RestoreLinks function
Allows to use compiled descriptions.
Will be useful for static checking utility.
2019-12-17 19:03:39 +01:00
Dmitry Vyukov
8b4aa626ce prog: fix typo in comment
Linter says:

prog/prio_test.go:68:15: `probablistic` is a misspelling of `probabilistic` (misspell)
		// for this probablistic test.
		            ^
2019-12-04 09:46:40 +01:00
Dmitry Vyukov
0ecb9746a7 prog: fix TestStaticPriorities
With -short and -race we get only 10 iterations
which is not enough for this probablistic test.
Use at least 100 interations always.
2019-12-03 19:34:24 +01:00
Dmitry Vyukov
1048481f27 prog: extend panic messages
We see this panic firing sometimes. Print the actual arg.
2019-11-26 14:29:58 +01:00
Paul Chaignon
997ccc675b pkg/compiler: define fileoff template
Signed-off-by: Paul Chaignon <paul.chaignon@orange.com>
2019-11-01 19:14:49 +01:00
Paul Chaignon
713f727d98 prog, pkg/compiler: alignment for integer ranges
Enables the syntax intN[start:end, alignment] for integer ranges.  For
instance, int32[0:10, 2] represents even 32-bit numbers between 0 and 10
included.  With this change, two NEED tags in syscall descriptions can be
addressed.

Signed-off-by: Paul Chaignon <paul.chaignon@orange.com>
2019-10-25 18:16:59 +02:00
Veronica Radu
1a3bad9041 prog: mutate length of output buffers
Update #480
2019-10-10 14:37:42 +02:00
Veronica Radu
fc17ba4941 prog: add size checks for const arguments during hints mutation
Update #507
2019-10-03 10:57:55 +02:00
Veronica Radu
2e29b53400 prog: fix plain priority for integers 2019-10-02 11:14:42 +02:00