summaryrefslogtreecommitdiff
path: root/libs/vr/libpdx/fuzz/helpers.h
blob: 83ec4096f6c16577edb71d47f2310526aafc3a66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
// Authors: corbin.souffrant@leviathansecurity.com
//          brian.balling@leviathansecurity.com

#ifndef LEV_FUZZERS_LIBPDX_HELPERS_H_
#define LEV_FUZZERS_LIBPDX_HELPERS_H_

#define UNUSED(expr) \
  do {               \
    (void)(expr);    \
  } while (0)

#include <fuzzer/FuzzedDataProvider.h>
#include <pdx/client.h>
#include <pdx/service.h>
#include <pdx/service_dispatcher.h>
#include <pdx/service_endpoint.h>
#include <sys/eventfd.h>
#include <memory>
#include <vector>

using namespace android::pdx;

// Vector of operations we can call in the dispatcher.
static const std::vector<std::function<void(
    const std::unique_ptr<ServiceDispatcher>&, FuzzedDataProvider*)>>
    dispatcher_operations = {
        [](const std::unique_ptr<ServiceDispatcher>& dispatcher,
           FuzzedDataProvider*) -> void { dispatcher->EnterDispatchLoop(); },
        [](const std::unique_ptr<ServiceDispatcher>& dispatcher,
           FuzzedDataProvider*) -> void { dispatcher->ReceiveAndDispatch(); },
        [](const std::unique_ptr<ServiceDispatcher>& dispatcher,
           FuzzedDataProvider* fdp) -> void {
          dispatcher->ReceiveAndDispatch(fdp->ConsumeIntegral<int>());
        }};

// Most of the fuzzing occurs within the endpoint, which is derived from an
// abstract class. So we are returning garbage data for most functions besides
// the ones we added or need to actually use.
class FuzzEndpoint : public Endpoint {
 public:
  explicit FuzzEndpoint(FuzzedDataProvider* fdp) {
    _fdp = fdp;
    _epoll_fd = eventfd(0, 0);
  }

  ~FuzzEndpoint() { close(_epoll_fd); }

  // Returns an fd that can be used with epoll() to wait for incoming messages
  // from this endpoint.
  int epoll_fd() const { return _epoll_fd; }

  // Associates a Service instance with an endpoint by setting the service
  // context pointer to the address of the Service. Only one Service may be
  // associated with a given endpoint.
  Status<void> SetService(Service* service) {
    _service = service;
    return Status<void>(0);
  }

  // Set the channel context for the given channel.
  Status<void> SetChannel(int channel_id, Channel* channel) {
    UNUSED(channel_id);
    _channel = std::shared_ptr<Channel>(channel);
    return Status<void>(0);
  }

  // Receives a message on the given endpoint file descriptor.
  // This is called by the dispatcher to determine what operations
  // to make, so we are fuzzing the response.
  Status<void> MessageReceive(Message* message) {
    // Create a randomized MessageInfo struct.
    MessageInfo info;
    eventfd_t wakeup_val = 0;
    info.pid = _fdp->ConsumeIntegral<int>();
    info.tid = _fdp->ConsumeIntegral<int>();
    info.cid = _fdp->ConsumeIntegral<int>();
    info.mid = _fdp->ConsumeIntegral<int>();
    info.euid = _fdp->ConsumeIntegral<int>();
    info.egid = _fdp->ConsumeIntegral<int>();
    info.op = _fdp->ConsumeIntegral<int32_t>();
    info.flags = _fdp->ConsumeIntegral<uint32_t>();
    info.service = _service;
    info.channel = _channel.get();
    info.send_len = _fdp->ConsumeIntegral<size_t>();
    info.recv_len = _fdp->ConsumeIntegral<size_t>();
    info.fd_count = _fdp->ConsumeIntegral<size_t>();
    if (_fdp->remaining_bytes() >= 32) {
      std::vector<uint8_t> impulse_vec = _fdp->ConsumeBytes<uint8_t>(32);
      memcpy(info.impulse, impulse_vec.data(), 32);
    }

    *message = Message(info);
    eventfd_read(_epoll_fd, &wakeup_val);

    return Status<void>();
  }

  // Returns a tag that uniquely identifies a specific underlying IPC
  // transport.
  uint32_t GetIpcTag() const { return 0; }

  // Close a channel, signaling the client file object and freeing the channel
  // id. Once closed, the client side of the channel always returns the error
  // ESHUTDOWN and signals the poll/epoll events POLLHUP and POLLFREE.
  Status<void> CloseChannel(int channel_id) {
    UNUSED(channel_id);
    return Status<void>();
  }

  // Update the event bits for the given channel (given by id), using the
  // given clear and set masks.
  Status<void> ModifyChannelEvents(int channel_id, int clear_mask,
                                   int set_mask) {
    UNUSED(channel_id);
    UNUSED(clear_mask);
    UNUSED(set_mask);
    return Status<void>();
  }

  // Create a new channel and push it as a file descriptor to the process
  // sending the |message|. |flags| may be set to O_NONBLOCK and/or
  // O_CLOEXEC to control the initial behavior of the new file descriptor (the
  // sending process may change these later using fcntl()). The internal
  // Channel instance associated with this channel is set to |channel|,
  // which may be nullptr. The new channel id allocated for this channel is
  // returned in |channel_id|, which may also be nullptr if not needed.
  Status<RemoteChannelHandle> PushChannel(Message* message, int flags,
                                          Channel* channel, int* channel_id) {
    UNUSED(message);
    UNUSED(flags);
    UNUSED(channel);
    UNUSED(channel_id);
    return Status<RemoteChannelHandle>();
  }

  // Check whether the |ref| is a reference to a channel to the service
  // represented by the |endpoint|. If the channel reference in question is
  // valid, the Channel object is returned in |channel| when non-nullptr and
  // the channel ID is returned through the Status object.
  Status<int> CheckChannel(const Message* message, ChannelReference ref,
                           Channel** channel) {
    UNUSED(message);
    UNUSED(ref);
    UNUSED(channel);
    return Status<int>();
  }

  // Replies to the message with a return code.
  Status<void> MessageReply(Message* message, int return_code) {
    UNUSED(message);
    UNUSED(return_code);
    return Status<void>();
  }

  // Replies to the message with a file descriptor.
  Status<void> MessageReplyFd(Message* message, unsigned int push_fd) {
    UNUSED(message);
    UNUSED(push_fd);
    return Status<void>();
  }

  // Replies to the message with a local channel handle.
  Status<void> MessageReplyChannelHandle(Message* message,
                                         const LocalChannelHandle& handle) {
    UNUSED(message);
    UNUSED(handle);
    return Status<void>();
  }

  // Replies to the message with a borrowed local channel handle.
  Status<void> MessageReplyChannelHandle(Message* message,
                                         const BorrowedChannelHandle& handle) {
    UNUSED(message);
    UNUSED(handle);
    return Status<void>();
  }

  // Replies to the message with a remote channel handle.
  Status<void> MessageReplyChannelHandle(Message* message,
                                         const RemoteChannelHandle& handle) {
    UNUSED(message);
    UNUSED(handle);
    return Status<void>();
  }

  // Reads message data into an array of memory buffers.
  Status<size_t> ReadMessageData(Message* message, const iovec* vector,
                                 size_t vector_length) {
    UNUSED(message);
    UNUSED(vector);
    UNUSED(vector_length);
    return Status<size_t>();
  }

  // Sends reply data for message.
  Status<size_t> WriteMessageData(Message* message, const iovec* vector,
                                  size_t vector_length) {
    UNUSED(message);
    UNUSED(vector);
    UNUSED(vector_length);
    return Status<size_t>();
  }

  // Records a file descriptor into the message buffer and returns the
  // remapped reference to be sent to the remote process.
  Status<FileReference> PushFileHandle(Message* message,
                                       const LocalHandle& handle) {
    UNUSED(message);
    UNUSED(handle);
    return Status<FileReference>();
  }

  Status<FileReference> PushFileHandle(Message* message,
                                       const BorrowedHandle& handle) {
    UNUSED(message);
    UNUSED(handle);
    return Status<FileReference>();
  }

  Status<FileReference> PushFileHandle(Message* message,
                                       const RemoteHandle& handle) {
    UNUSED(message);
    UNUSED(handle);
    return Status<FileReference>();
  }

  Status<ChannelReference> PushChannelHandle(Message* message,
                                             const LocalChannelHandle& handle) {
    UNUSED(message);
    UNUSED(handle);
    return Status<ChannelReference>();
  }

  Status<ChannelReference> PushChannelHandle(
      Message* message, const BorrowedChannelHandle& handle) {
    UNUSED(message);
    UNUSED(handle);
    return Status<ChannelReference>();
  }

  Status<ChannelReference> PushChannelHandle(
      Message* message, const RemoteChannelHandle& handle) {
    UNUSED(message);
    UNUSED(handle);
    return Status<ChannelReference>();
  }

  // Obtains a file descriptor/channel handle from a message for the given
  // reference.
  LocalHandle GetFileHandle(Message* message, FileReference ref) const {
    UNUSED(message);
    UNUSED(ref);
    return LocalHandle();
  }

  LocalChannelHandle GetChannelHandle(Message* message,
                                      ChannelReference ref) const {
    UNUSED(message);
    UNUSED(ref);
    return LocalChannelHandle();
  }

  // Transport-specific message state management.
  void* AllocateMessageState() { return nullptr; }

  void FreeMessageState(void* state) { UNUSED(state); }

  // Cancels the endpoint, unblocking any receiver threads waiting for a
  // message.
  Status<void> Cancel() { return Status<void>(); }

 private:
  FuzzedDataProvider* _fdp;
  std::shared_ptr<Channel> _channel;
  Service* _service;
  int _epoll_fd;
};

#endif  // LEV_FUZZERS_LIBPDX_HELPERS_H_