Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/attr-malloc-CVE-2019-19078-usb-leak.c

raw
1 /* Adapted from linux 5.3.11: drivers/net/wireless/ath/ath10k/usb.c
2 Reduced reproducer for CVE-2019-19078 (leak of struct urb). */
3
4 typedef unsigned char u8;
5 typedef unsigned short u16;
6
7 #define ENOMEM 12
8 #define EINVAL 22
9
10 /* The original file has this licence header. */
11
12 // SPDX-License-Identifier: ISC
13 /*
14 * Copyright (c) 2007-2011 Atheros Communications Inc.
15 * Copyright (c) 2011-2012,2017 Qualcomm Atheros, Inc.
16 * Copyright (c) 2016-2017 Erik Stromdahl <[email protected]>
17 */
18
19 /* Adapted from include/linux/compiler_attributes.h. */
20 #define __aligned(x) __attribute__((__aligned__(x)))
21 #define __printf(a, b) __attribute__((__format__(printf, a, b)))
22
23 /* Possible macro for the new attribute. */
24 #define __malloc(f) __attribute__((malloc(f)));
25
26 /* From include/linux/types.h. */
27
28 typedef unsigned int gfp_t;
29
30 /* Not the real value, which is in include/linux/gfp.h. */
31 #define GFP_ATOMIC 32
32
33 /* From include/linux/usb.h. */
34
35 struct urb;
36 extern void usb_free_urb(struct urb *urb);
37 extern struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags)
38 __malloc(usb_free_urb);
39 /* attribute added as part of testcase */
40
41 extern int usb_submit_urb(/*struct urb *urb, */gfp_t mem_flags);
42 extern void usb_unanchor_urb(struct urb *urb);
43
44 /* From drivers/net/wireless/ath/ath10k/core.h. */
45
46 struct ath10k;
47
48 struct ath10k {
49 /* [...many other fields removed...] */
50
51 /* must be last */
52 u8 drv_priv[0] __aligned(sizeof(void *));
53 };
54
55 /* From drivers/net/wireless/ath/ath10k/debug.h. */
56
57 enum ath10k_debug_mask {
58 /* [...other values removed...] */
59 ATH10K_DBG_USB_BULK = 0x00080000,
60 };
61
62 extern unsigned int ath10k_debug_mask;
63
64 __printf(3, 4) void __ath10k_dbg(struct ath10k *ar,
65 enum ath10k_debug_mask mask,
66 const char *fmt, ...);
67
68 /* Simplified for now, to avoid pulling in tracepoint code. */
69 static inline
70 bool trace_ath10k_log_dbg_enabled(void) { return 0; }
71
72 #define ath10k_dbg(ar, dbg_mask, fmt, ...) \
73 do { \
74 if ((ath10k_debug_mask & dbg_mask) || \
75 trace_ath10k_log_dbg_enabled()) \
76 __ath10k_dbg(ar, dbg_mask, fmt, ##__VA_ARGS__); \
77 } while (0)
78
79 /* From drivers/net/wireless/ath/ath10k/hif.h. */
80
81 struct ath10k_hif_sg_item {
82 /* [...other fields removed...] */
83 void *transfer_context; /* NULL = tx completion callback not called */
84 };
85
86 struct ath10k_hif_ops {
87 /* send a scatter-gather list to the target */
88 int (*tx_sg)(struct ath10k *ar, u8 pipe_id,
89 struct ath10k_hif_sg_item *items, int n_items);
90 /* [...other fields removed...] */
91 };
92
93 /* From drivers/net/wireless/ath/ath10k/usb.h. */
94
95 /* tx/rx pipes for usb */
96 enum ath10k_usb_pipe_id {
97 /* [...other values removed...] */
98 ATH10K_USB_PIPE_MAX = 8
99 };
100
101 struct ath10k_usb_pipe {
102 /* [...all fields removed...] */
103 };
104
105 /* usb device object */
106 struct ath10k_usb {
107 /* [...other fields removed...] */
108 struct ath10k_usb_pipe pipes[ATH10K_USB_PIPE_MAX];
109 };
110
111 /* usb urb object */
112 struct ath10k_urb_context {
113 /* [...other fields removed...] */
114 struct ath10k_usb_pipe *pipe;
115 struct sk_buff *skb;
116 };
117
118 static inline struct ath10k_usb *ath10k_usb_priv(struct ath10k *ar)
119 {
120 return (struct ath10k_usb *)ar->drv_priv;
121 }
122
123 /* The source file. */
124
125 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
126 struct ath10k_usb_pipe *recv_pipe);
127
128 struct ath10k_urb_context *
129 ath10k_usb_alloc_urb_from_pipe(struct ath10k_usb_pipe *pipe);
130
131 void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe *pipe,
132 struct ath10k_urb_context *urb_context);
133
134 static int ath10k_usb_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
135 struct ath10k_hif_sg_item *items, int n_items)
136 {
137 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
138 struct ath10k_usb_pipe *pipe = &ar_usb->pipes[pipe_id];
139 struct ath10k_urb_context *urb_context;
140 struct sk_buff *skb;
141 struct urb *urb;
142 int ret, i;
143
144 for (i = 0; i < n_items; i++) {
145 urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
146 if (!urb_context) {
147 ret = -ENOMEM;
148 goto err;
149 }
150
151 skb = (struct sk_buff *) items[i].transfer_context;
152 urb_context->skb = skb;
153
154 urb = usb_alloc_urb(0, GFP_ATOMIC); /* { dg-message "allocated here" } */
155 if (!urb) {
156 ret = -ENOMEM;
157 goto err_free_urb_to_pipe;
158 }
159
160 /* TODO: these are disabled, otherwise we conservatively
161 assume that they could free urb. */
162 #if 0
163 usb_fill_bulk_urb(urb,
164 ar_usb->udev,
165 pipe->usb_pipe_handle,
166 skb->data,
167 skb->len,
168 ath10k_usb_transmit_complete, urb_context);
169 if (!(skb->len % pipe->max_packet_size)) {
170 /* hit a max packet boundary on this pipe */
171 urb->transfer_flags |= URB_ZERO_PACKET;
172 }
173
174 usb_anchor_urb(urb, &pipe->urb_submitted);
175 #endif
176 /* TODO: initial argument disabled, otherwise we conservatively
177 assume that it could free urb. */
178 ret = usb_submit_urb(/*urb, */GFP_ATOMIC);
179 if (ret) { /* TODO: why doesn't it show this condition at default verbosity? */
180 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
181 "usb bulk transmit failed: %d\n", ret);
182
183 /* TODO: this is disabled, otherwise we conservatively
184 assume that it could free urb. */
185 #if 0
186 usb_unanchor_urb(urb);
187 #endif
188
189 ret = -EINVAL;
190 /* Leak of urb happens here. */
191 goto err_free_urb_to_pipe;
192 }
193
194 usb_free_urb(urb); /* { dg-bogus "double-'usb_free_urb' of 'urb'" } */
195 }
196
197 return 0;
198
199 err_free_urb_to_pipe:
200 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
201 err:
202 return ret; /* { dg-warning "leak of 'urb'" } */
203 }
204
205 static const struct ath10k_hif_ops ath10k_usb_hif_ops = {
206 .tx_sg = ath10k_usb_hif_tx_sg,
207 };
208
209 /* Simulate code to register the callback. */
210 extern void callback_registration (const void *);
211 void ath10k_usb_probe(void)
212 {
213 callback_registration(&ath10k_usb_hif_ops);
214 }
215
216
217 /* The original source file ends with:
218 MODULE_AUTHOR("Atheros Communications, Inc.");
219 MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN USB devices");
220 MODULE_LICENSE("Dual BSD/GPL");
221 */