Debian Codegraph @

tests/fuzz/README.md

raw
1 # Simple fuzzers for libssh
2
3 This directory contains fuzzer programs, that are usable either in
4 oss-fuzz infrastructure or suitable for running fuzzing locally or
5 even for reproducing crashes with given trace files.
6
7 When building with clang, fuzzers are automatically built with address
8 sanitizer. With gcc, they are built as they are without instrumentation,
9 but they are suitable for debugging.
10
11 ## Background
12
13 ### Turn off encryption
14
15 Fuzzing ssh protocol is complicated by the way that all the communication
16 between client and server is encrypted and authenticated using keys based
17 on random data, making it impossible to fuzz the actual underlying protocol
18 as every change in the encrypted data causes integrity errors. For that reason,
19 libssh needs to implement "none" cipher and MAC as described in RFC 4253
20 and these need to be used during fuzzing to be able to accomplish
21 reproducibility and for fuzzers to be able to progress behind key exchange.
22 This is enabled with the `WITH_INSECURE_NONE` CMake option.
23
24 ### Do not allow filesystem modification
25
26 The OpenSSH configuration files are quite rich and expects users to know what
27 they do when they write their configuration files. The fuzzer driver is not an
28 average user so it is very happy to try whatever commands come to its "mind",
29 including `rm -rf /` and libssh would be very happy to run it by default. This
30 might remove some parts of the system that are mandatory for fuzzing.
31 To avoid executing dangerous commands like this, the `WITH_EXEC=OFF` CMake
32 option prevents invoking any external command through `exec()` syscall.
33
34 ## Corpus creation
35
36 For effective fuzzing, we need to provide corpus of initial (valid) inputs that
37 can be used for deriving other inputs. libssh already supports creation of pcap
38 files (packet capture), which include all the information we need for fuzzing.
39 This file is also created from date before encryption and after decryption so
40 it is in plain text as we expect it, but we still need to adjust configuration
41 to use none cipher for the key exchange to be plausible.
42
43 ### Creating packet capture using example libssh client
44
45 * Compile libssh with support for none cipher and pcap:
46
47 cmake -DWITH_INSECURE_NONE=ON -DWITH_EXEC=OFF -DWITH_PCAP=ON ../
48
49 * Create a configuration file enabling none cipher and mac:
50
51 printf 'Ciphers none\nMACs none' > /tmp/ssh_config
52
53 * Generate test host key:
54
55 ./examples/keygen2 -f /tmp/hostkey -t rsa
56
57 * Run example libssh server:
58
59 ./examples/samplesshd-cb -f /tmp/ssh_config -k /tmp/hostkey -p 22222 127.0.0.1
60
61 * In other terminal, run the example libssh client with pcap enabled (use mypassword for password):
62
63 ./examples/ssh-client -F /tmp/ssh_config -l myuser -P /tmp/ssh.pcap -p 22222 127.0.0.1
64
65 * Kill the server (in the first terminal, press Ctrl+C)
66
67 * Convert the pcap file to raw traces (separate client and server messages) usable by fuzzer:
68
69 tshark -r /tmp/ssh.pcap -T fields -e data -Y "tcp.dstport==22222" | tr -d '\n',':' | xxd -r -ps > /tmp/ssh_server
70 tshark -r /tmp/ssh.pcap -T fields -e data -Y "tcp.dstport!=22222" | tr -d '\n',':' | xxd -r -ps > /tmp/ssh_client
71
72 * Now we should be able to "replay" the sessions in respective fuzzers, getting some more coverage:
73
74 LIBSSH_VERBOSITY=9 ./tests/fuzz/ssh_client_fuzzer /tmp/ssh_client
75 LIBSSH_VERBOSITY=9 ./tests/fuzz/ssh_server_fuzzer /tmp/ssh_server
76
77 (note, that the client fuzzer fails now because of invalid hostkey signature; TODO)
78
79 * Store the appropriately named traces in the fuzers directory:
80
81 cp /tmp/ssh_client tests/fuzz/ssh_client_fuzzer_corpus/$(sha1sum /tmp/ssh_client | cut -d ' ' -f 1)
82 cp /tmp/ssh_server tests/fuzz/ssh_server_fuzzer_corpus/$(sha1sum /tmp/ssh_server | cut -d ' ' -f 1)
83
84 ## Debugging issues reported by oss-fuzz
85
86 OSS Fuzz provides helper scripts to reproduce issues locally. Even though the
87 fuzzing scripts can ran anywhere, the best bet for reproducing is to use
88 their container infrastructure. There is a
89 [complete documentation](https://google.github.io/oss-fuzz/advanced-topics/reproducing/)
90 but I will try to focus here on the workflow I use and libssh specifics.
91
92 ### Environment
93
94 The helper scripts are written in Python and use docker to run containers
95 so these needs to be installed. I am using podman instead of docker for
96 some time, but it has some quirks that needs to be addressed in advance
97 and that I describe in the rejected [PR](https://github.com/google/oss-fuzz/pull/4774).
98 You can either pick up my branch or workaround them locally:
99
100 * Package `podman-docker` installs symlink from `/bin/docker` to `/bin/podman`
101 * The directories mounted to the containers need to have `container_file_t`
102 SELinux labels -- this is needed for the `build` directory that is created
103 under the oss-fuzz repository, for testcases and for source files
104 * `podman` does not like combination of `--privileged` and
105 `--cap-add SYS_PTRACE` flags. Podman can work with non-privileged containers
106 so you can just remove the `--privileged` from the `infra/helper.py`
107
108 ### Reproduce locally
109
110 Clone the above repository from https://github.com/google/oss-fuzz/, apply
111 changes from previous section if needed, setup local clone of libssh repository
112 and build the fuzzers locally (where `~/devel/libssh` is path to local libssh
113 checkout):
114
115 python infra/helper.py build_fuzzers libssh ~/devel/libssh/
116
117 Now, download the testcase from oss-fuzz.com (the file under `~/Downloads`)
118 and we are ready to reproduce the issue locally (replace the `ssh_client_fuzzer`
119 with the fuzzer name if the issue happens in other fuzzer):
120
121 python infra/helper.py reproduce libssh ssh_client_fuzzer ~/Downloads/clusterfuzz-testcase-ssh_client_fuzzer-4637376441483264
122
123 This should give you the same error/leak/crash as you see on the testcase
124 detail in oss-fuzz.com.
125
126 I find it very useful to run libssh in debug mode, to see what happened and
127 what exit path was taken to get to the error. Fortunately, we can simply
128 pass environment variables to the container:
129
130 python infra/helper.py reproduce -eLIBSSH_VERBOSITY=9 libssh ssh_client_fuzzer ~/Downloads/clusterfuzz-testcase-ssh_client_fuzzer-4637376441483264
131
132 ### Fix the issue and verify the fix
133
134 Now, we can properly investigate the issue and once we have a fix, we can
135 make changes in our local checkout and repeat the steps above (from building
136 fuzzers) to verify the issue is no longer present.
137
138 ### Fuzzing locally
139
140 We can use the oss-fuzz tools even further and run the fuzzing process
141 locally, to verify there are no similar issues happening very close to
142 existing code paths and which would cause more reports very soon after
143 we would fix the current issue. The following command will run fuzzer
144 until it finds an issue or until killed:
145
146 python infra/helper.py run_fuzzer libssh ssh_client_fuzzer