Coverage Report

Created: 2026-09-14 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hostap/tests/fuzzing/p2p/p2p.c
Line
Count
Source
1
/*
2
 * wpa_supplicant - P2P fuzzer
3
 * Copyright (c) 2015, Jouni Malinen <j@w1.fi>
4
 *
5
 * This software may be distributed under the terms of the BSD license.
6
 * See README for more details.
7
 */
8
9
#include "utils/includes.h"
10
11
#include "utils/common.h"
12
#include "utils/eloop.h"
13
#include "common/ieee802_11_defs.h"
14
#include "p2p/p2p.h"
15
#include "ap/hostapd.h"
16
#include "ap/ieee802_1x.h"
17
#include "ap/pmksa_cache_auth.h"
18
#include "../fuzzer-common.h"
19
20
21
int pasn_responder_pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
22
           const u8 *bssid, u8 *pmkid, u8 *pmk,
23
           size_t *pmk_len)
24
0
{
25
0
  return -1;
26
0
}
27
28
29
static void debug_print(void *ctx, int level, const char *msg)
30
95.2k
{
31
95.2k
  wpa_printf(level, "P2P: %s", msg);
32
95.2k
}
33
34
35
static void find_stopped(void *ctx)
36
0
{
37
0
}
38
39
40
static int start_listen(void *ctx, unsigned int freq,
41
      unsigned int duration,
42
      const struct wpabuf *probe_resp_ie)
43
3
{
44
3
  return 0;
45
3
}
46
47
48
static void stop_listen(void *ctx)
49
6.67k
{
50
6.67k
}
51
52
53
static void dev_found(void *ctx, const u8 *addr,
54
          const struct p2p_peer_info *info,
55
          int new_device)
56
789
{
57
789
}
58
59
60
static void dev_lost(void *ctx, const u8 *dev_addr)
61
634
{
62
634
}
63
64
65
static int send_action(void *ctx, unsigned int freq, const u8 *dst,
66
           const u8 *src, const u8 *bssid, const u8 *buf,
67
           size_t len, unsigned int wait_time, int *scheduled)
68
1.63k
{
69
1.63k
  *scheduled = 0;
70
1.63k
  return 0;
71
1.63k
}
72
73
74
static void send_action_done(void *ctx)
75
4
{
76
4
}
77
78
79
static void go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
80
        u8 go_intent)
81
37
{
82
37
}
83
84
85
static struct p2p_data * init_p2p(void)
86
6.67k
{
87
6.67k
  struct p2p_config p2p;
88
89
6.67k
  os_memset(&p2p, 0, sizeof(p2p));
90
6.67k
  p2p.max_peers = 100;
91
6.67k
  p2p.passphrase_len = 8;
92
6.67k
  p2p.channels.reg_classes = 1;
93
6.67k
  p2p.channels.reg_class[0].reg_class = 81;
94
6.67k
  p2p.channels.reg_class[0].channel[0] = 1;
95
6.67k
  p2p.channels.reg_class[0].channel[1] = 2;
96
6.67k
  p2p.channels.reg_class[0].channels = 2;
97
6.67k
  p2p.debug_print = debug_print;
98
6.67k
  p2p.find_stopped = find_stopped;
99
6.67k
  p2p.start_listen = start_listen;
100
6.67k
  p2p.stop_listen = stop_listen;
101
6.67k
  p2p.dev_found = dev_found;
102
6.67k
  p2p.dev_lost = dev_lost;
103
6.67k
  p2p.send_action = send_action;
104
6.67k
  p2p.send_action_done = send_action_done;
105
6.67k
  p2p.go_neg_req_rx = go_neg_req_rx;
106
107
6.67k
  return p2p_init(&p2p);
108
6.67k
}
109
110
111
struct arg_ctx {
112
  const u8 *data;
113
  size_t data_len;
114
  struct p2p_data *p2p;
115
  int count;
116
};
117
118
119
static void test_send(void *eloop_data, void *user_ctx)
120
6.67k
{
121
6.67k
  struct arg_ctx *ctx = eloop_data;
122
6.67k
  struct os_reltime rx_time;
123
124
6.67k
  wpa_hexdump(MSG_MSGDUMP, "fuzzer - IEs", ctx->data, ctx->data_len);
125
126
6.67k
  os_memset(&rx_time, 0, sizeof(rx_time));
127
6.67k
  p2p_scan_res_handler(ctx->p2p, (u8 *) "\x02\x00\x00\x00\x01\x00", 2412,
128
6.67k
           &rx_time, 0, ctx->data, ctx->data_len);
129
6.67k
  p2p_scan_res_handled(ctx->p2p, 0);
130
131
6.67k
  p2p_probe_req_rx(ctx->p2p, (u8 *) "\x02\x00\x00\x00\x01\x00",
132
6.67k
       (u8 *) "\x02\x00\x00\x00\x00\x00",
133
6.67k
       (u8 *) "\x02\x00\x00\x00\x00\x00",
134
6.67k
       ctx->data, ctx->data_len, 2412, 0);
135
136
6.67k
  if (ctx->data_len >= IEEE80211_HDRLEN + 1) {
137
4.50k
    struct os_reltime rx_time;
138
4.50k
    const struct ieee80211_mgmt *mgmt;
139
140
4.50k
    mgmt = (const struct ieee80211_mgmt *) ctx->data;
141
4.50k
    os_memset(&rx_time, 0, sizeof(rx_time));
142
4.50k
    p2p_rx_action(ctx->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
143
4.50k
            mgmt->u.action.category,
144
4.50k
            (const u8 *) ctx->data + IEEE80211_HDRLEN + 1,
145
4.50k
            ctx->data_len - IEEE80211_HDRLEN - 1, 2412);
146
4.50k
  }
147
148
6.67k
  eloop_terminate();
149
6.67k
}
150
151
152
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
153
6.67k
{
154
6.67k
  struct p2p_data *p2p;
155
6.67k
  struct arg_ctx ctx;
156
157
6.67k
  wpa_fuzzer_set_debug_level();
158
159
6.67k
  if (os_program_init())
160
0
    return -1;
161
162
6.67k
  if (eloop_init()) {
163
0
    wpa_printf(MSG_ERROR, "Failed to initialize event loop");
164
0
    return 0;
165
0
  }
166
167
6.67k
  p2p = init_p2p();
168
6.67k
  if (!p2p) {
169
0
    wpa_printf(MSG_ERROR, "P2P init failed");
170
0
    return 0;
171
0
  }
172
173
6.67k
  os_memset(&ctx, 0, sizeof(ctx));
174
6.67k
  ctx.p2p = p2p;
175
6.67k
  ctx.data = data;
176
6.67k
  ctx.data_len = size;
177
178
6.67k
  eloop_register_timeout(0, 0, test_send, &ctx, NULL);
179
180
6.67k
  wpa_printf(MSG_DEBUG, "Starting eloop");
181
6.67k
  eloop_run();
182
6.67k
  wpa_printf(MSG_DEBUG, "eloop done");
183
184
6.67k
  p2p_deinit(p2p);
185
6.67k
  eloop_destroy();
186
6.67k
  os_program_deinit();
187
188
6.67k
  return 0;
189
6.67k
}