]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/kcov.c
usb: gadget: storage: Fix Spectre v1 vulnerability
[mirror_ubuntu-bionic-kernel.git] / kernel / kcov.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
5c9a8750
DV
2#define pr_fmt(fmt) "kcov: " fmt
3
36f05ae8 4#define DISABLE_BRANCH_PROFILING
db862358 5#include <linux/atomic.h>
5c9a8750 6#include <linux/compiler.h>
db862358
KW
7#include <linux/errno.h>
8#include <linux/export.h>
5c9a8750
DV
9#include <linux/types.h>
10#include <linux/file.h>
11#include <linux/fs.h>
db862358 12#include <linux/init.h>
5c9a8750 13#include <linux/mm.h>
db862358 14#include <linux/preempt.h>
5c9a8750 15#include <linux/printk.h>
166ad0e1 16#include <linux/sched.h>
5c9a8750
DV
17#include <linux/slab.h>
18#include <linux/spinlock.h>
19#include <linux/vmalloc.h>
20#include <linux/debugfs.h>
21#include <linux/uaccess.h>
22#include <linux/kcov.h>
4983f0ab 23#include <asm/setup.h>
5c9a8750 24
ded97d2c
VC
25/* Number of 64-bit words written per one comparison: */
26#define KCOV_WORDS_PER_CMP 4
27
5c9a8750
DV
28/*
29 * kcov descriptor (one per opened debugfs file).
30 * State transitions of the descriptor:
31 * - initial state after open()
32 * - then there must be a single ioctl(KCOV_INIT_TRACE) call
33 * - then, mmap() call (several calls are allowed but not useful)
ded97d2c
VC
34 * - then, ioctl(KCOV_ENABLE, arg), where arg is
35 * KCOV_TRACE_PC - to trace only the PCs
36 * or
37 * KCOV_TRACE_CMP - to trace only the comparison operands
38 * - then, ioctl(KCOV_DISABLE) to disable the task.
39 * Enabling/disabling ioctls can be repeated (only one task a time allowed).
5c9a8750
DV
40 */
41struct kcov {
42 /*
43 * Reference counter. We keep one for:
44 * - opened file descriptor
45 * - task with enabled coverage (we can't unwire it from another task)
46 */
47 atomic_t refcount;
48 /* The lock protects mode, size, area and t. */
49 spinlock_t lock;
50 enum kcov_mode mode;
51 /* Size of arena (in long's for KCOV_MODE_TRACE). */
52 unsigned size;
53 /* Coverage buffer shared with user space. */
54 void *area;
55 /* Task for which we collect coverage, or NULL. */
56 struct task_struct *t;
57};
58
ded97d2c 59static bool check_kcov_mode(enum kcov_mode needed_mode, struct task_struct *t)
5c9a8750 60{
5c9a8750
DV
61 enum kcov_mode mode;
62
5c9a8750
DV
63 /*
64 * We are interested in code coverage as a function of a syscall inputs,
65 * so we ignore code executed in interrupts.
66 */
fcf4edac 67 if (!in_task())
ded97d2c 68 return false;
5c9a8750 69 mode = READ_ONCE(t->kcov_mode);
ded97d2c
VC
70 /*
71 * There is some code that runs in interrupts but for which
72 * in_interrupt() returns false (e.g. preempt_schedule_irq()).
73 * READ_ONCE()/barrier() effectively provides load-acquire wrt
74 * interrupts, there are paired barrier()/WRITE_ONCE() in
75 * kcov_ioctl_locked().
76 */
77 barrier();
78 return mode == needed_mode;
79}
4983f0ab 80
ded97d2c
VC
81static unsigned long canonicalize_ip(unsigned long ip)
82{
4983f0ab 83#ifdef CONFIG_RANDOMIZE_BASE
ded97d2c 84 ip -= kaslr_offset();
4983f0ab 85#endif
ded97d2c
VC
86 return ip;
87}
5c9a8750 88
ded97d2c
VC
89/*
90 * Entry point from instrumented code.
91 * This is called once per basic-block/edge.
92 */
93void notrace __sanitizer_cov_trace_pc(void)
94{
95 struct task_struct *t;
96 unsigned long *area;
97 unsigned long ip = canonicalize_ip(_RET_IP_);
98 unsigned long pos;
99
100 t = current;
101 if (!check_kcov_mode(KCOV_MODE_TRACE_PC, t))
102 return;
103
104 area = t->kcov_area;
105 /* The first 64-bit word is the number of subsequent PCs. */
106 pos = READ_ONCE(area[0]) + 1;
107 if (likely(pos < t->kcov_size)) {
108 area[pos] = ip;
109 WRITE_ONCE(area[0], pos);
5c9a8750
DV
110 }
111}
112EXPORT_SYMBOL(__sanitizer_cov_trace_pc);
113
ded97d2c
VC
114#ifdef CONFIG_KCOV_ENABLE_COMPARISONS
115static void write_comp_data(u64 type, u64 arg1, u64 arg2, u64 ip)
116{
117 struct task_struct *t;
118 u64 *area;
119 u64 count, start_index, end_pos, max_pos;
120
121 t = current;
122 if (!check_kcov_mode(KCOV_MODE_TRACE_CMP, t))
123 return;
124
125 ip = canonicalize_ip(ip);
126
127 /*
128 * We write all comparison arguments and types as u64.
129 * The buffer was allocated for t->kcov_size unsigned longs.
130 */
131 area = (u64 *)t->kcov_area;
132 max_pos = t->kcov_size * sizeof(unsigned long);
133
134 count = READ_ONCE(area[0]);
135
136 /* Every record is KCOV_WORDS_PER_CMP 64-bit words. */
137 start_index = 1 + count * KCOV_WORDS_PER_CMP;
138 end_pos = (start_index + KCOV_WORDS_PER_CMP) * sizeof(u64);
139 if (likely(end_pos <= max_pos)) {
140 area[start_index] = type;
141 area[start_index + 1] = arg1;
142 area[start_index + 2] = arg2;
143 area[start_index + 3] = ip;
144 WRITE_ONCE(area[0], count + 1);
145 }
146}
147
148void notrace __sanitizer_cov_trace_cmp1(u8 arg1, u8 arg2)
149{
150 write_comp_data(KCOV_CMP_SIZE(0), arg1, arg2, _RET_IP_);
151}
152EXPORT_SYMBOL(__sanitizer_cov_trace_cmp1);
153
154void notrace __sanitizer_cov_trace_cmp2(u16 arg1, u16 arg2)
155{
156 write_comp_data(KCOV_CMP_SIZE(1), arg1, arg2, _RET_IP_);
157}
158EXPORT_SYMBOL(__sanitizer_cov_trace_cmp2);
159
689d77f0 160void notrace __sanitizer_cov_trace_cmp4(u32 arg1, u32 arg2)
ded97d2c
VC
161{
162 write_comp_data(KCOV_CMP_SIZE(2), arg1, arg2, _RET_IP_);
163}
164EXPORT_SYMBOL(__sanitizer_cov_trace_cmp4);
165
166void notrace __sanitizer_cov_trace_cmp8(u64 arg1, u64 arg2)
167{
168 write_comp_data(KCOV_CMP_SIZE(3), arg1, arg2, _RET_IP_);
169}
170EXPORT_SYMBOL(__sanitizer_cov_trace_cmp8);
171
172void notrace __sanitizer_cov_trace_const_cmp1(u8 arg1, u8 arg2)
173{
174 write_comp_data(KCOV_CMP_SIZE(0) | KCOV_CMP_CONST, arg1, arg2,
175 _RET_IP_);
176}
177EXPORT_SYMBOL(__sanitizer_cov_trace_const_cmp1);
178
179void notrace __sanitizer_cov_trace_const_cmp2(u16 arg1, u16 arg2)
180{
181 write_comp_data(KCOV_CMP_SIZE(1) | KCOV_CMP_CONST, arg1, arg2,
182 _RET_IP_);
183}
184EXPORT_SYMBOL(__sanitizer_cov_trace_const_cmp2);
185
689d77f0 186void notrace __sanitizer_cov_trace_const_cmp4(u32 arg1, u32 arg2)
ded97d2c
VC
187{
188 write_comp_data(KCOV_CMP_SIZE(2) | KCOV_CMP_CONST, arg1, arg2,
189 _RET_IP_);
190}
191EXPORT_SYMBOL(__sanitizer_cov_trace_const_cmp4);
192
193void notrace __sanitizer_cov_trace_const_cmp8(u64 arg1, u64 arg2)
194{
195 write_comp_data(KCOV_CMP_SIZE(3) | KCOV_CMP_CONST, arg1, arg2,
196 _RET_IP_);
197}
198EXPORT_SYMBOL(__sanitizer_cov_trace_const_cmp8);
199
200void notrace __sanitizer_cov_trace_switch(u64 val, u64 *cases)
201{
202 u64 i;
203 u64 count = cases[0];
204 u64 size = cases[1];
205 u64 type = KCOV_CMP_CONST;
206
207 switch (size) {
208 case 8:
209 type |= KCOV_CMP_SIZE(0);
210 break;
211 case 16:
212 type |= KCOV_CMP_SIZE(1);
213 break;
214 case 32:
215 type |= KCOV_CMP_SIZE(2);
216 break;
217 case 64:
218 type |= KCOV_CMP_SIZE(3);
219 break;
220 default:
221 return;
222 }
223 for (i = 0; i < count; i++)
224 write_comp_data(type, cases[i + 2], val, _RET_IP_);
225}
226EXPORT_SYMBOL(__sanitizer_cov_trace_switch);
227#endif /* ifdef CONFIG_KCOV_ENABLE_COMPARISONS */
228
5c9a8750
DV
229static void kcov_get(struct kcov *kcov)
230{
231 atomic_inc(&kcov->refcount);
232}
233
234static void kcov_put(struct kcov *kcov)
235{
236 if (atomic_dec_and_test(&kcov->refcount)) {
237 vfree(kcov->area);
238 kfree(kcov);
239 }
240}
241
242void kcov_task_init(struct task_struct *t)
243{
e90e4ae2
MR
244 WRITE_ONCE(t->kcov_mode, KCOV_MODE_DISABLED);
245 barrier();
5c9a8750
DV
246 t->kcov_size = 0;
247 t->kcov_area = NULL;
248 t->kcov = NULL;
249}
250
251void kcov_task_exit(struct task_struct *t)
252{
253 struct kcov *kcov;
254
255 kcov = t->kcov;
256 if (kcov == NULL)
257 return;
258 spin_lock(&kcov->lock);
259 if (WARN_ON(kcov->t != t)) {
260 spin_unlock(&kcov->lock);
261 return;
262 }
263 /* Just to not leave dangling references behind. */
264 kcov_task_init(t);
265 kcov->t = NULL;
ded97d2c 266 kcov->mode = KCOV_MODE_INIT;
5c9a8750
DV
267 spin_unlock(&kcov->lock);
268 kcov_put(kcov);
269}
270
271static int kcov_mmap(struct file *filep, struct vm_area_struct *vma)
272{
273 int res = 0;
274 void *area;
275 struct kcov *kcov = vma->vm_file->private_data;
276 unsigned long size, off;
277 struct page *page;
278
279 area = vmalloc_user(vma->vm_end - vma->vm_start);
280 if (!area)
281 return -ENOMEM;
282
283 spin_lock(&kcov->lock);
284 size = kcov->size * sizeof(unsigned long);
ded97d2c 285 if (kcov->mode != KCOV_MODE_INIT || vma->vm_pgoff != 0 ||
5c9a8750
DV
286 vma->vm_end - vma->vm_start != size) {
287 res = -EINVAL;
288 goto exit;
289 }
290 if (!kcov->area) {
291 kcov->area = area;
292 vma->vm_flags |= VM_DONTEXPAND;
293 spin_unlock(&kcov->lock);
294 for (off = 0; off < size; off += PAGE_SIZE) {
295 page = vmalloc_to_page(kcov->area + off);
296 if (vm_insert_page(vma, vma->vm_start + off, page))
297 WARN_ONCE(1, "vm_insert_page() failed");
298 }
299 return 0;
300 }
301exit:
302 spin_unlock(&kcov->lock);
303 vfree(area);
304 return res;
305}
306
307static int kcov_open(struct inode *inode, struct file *filep)
308{
309 struct kcov *kcov;
310
311 kcov = kzalloc(sizeof(*kcov), GFP_KERNEL);
312 if (!kcov)
313 return -ENOMEM;
ded97d2c 314 kcov->mode = KCOV_MODE_DISABLED;
5c9a8750
DV
315 atomic_set(&kcov->refcount, 1);
316 spin_lock_init(&kcov->lock);
317 filep->private_data = kcov;
318 return nonseekable_open(inode, filep);
319}
320
321static int kcov_close(struct inode *inode, struct file *filep)
322{
323 kcov_put(filep->private_data);
324 return 0;
325}
326
327static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
328 unsigned long arg)
329{
330 struct task_struct *t;
331 unsigned long size, unused;
332
333 switch (cmd) {
334 case KCOV_INIT_TRACE:
335 /*
336 * Enable kcov in trace mode and setup buffer size.
337 * Must happen before anything else.
338 */
339 if (kcov->mode != KCOV_MODE_DISABLED)
340 return -EBUSY;
341 /*
342 * Size must be at least 2 to hold current position and one PC.
343 * Later we allocate size * sizeof(unsigned long) memory,
344 * that must not overflow.
345 */
346 size = arg;
347 if (size < 2 || size > INT_MAX / sizeof(unsigned long))
348 return -EINVAL;
349 kcov->size = size;
ded97d2c 350 kcov->mode = KCOV_MODE_INIT;
5c9a8750
DV
351 return 0;
352 case KCOV_ENABLE:
353 /*
354 * Enable coverage for the current task.
355 * At this point user must have been enabled trace mode,
356 * and mmapped the file. Coverage collection is disabled only
357 * at task exit or voluntary by KCOV_DISABLE. After that it can
358 * be enabled for another task.
359 */
ded97d2c 360 if (kcov->mode != KCOV_MODE_INIT || !kcov->area)
5c9a8750 361 return -EINVAL;
9bdc046c
DV
362 t = current;
363 if (kcov->t != NULL || t->kcov != NULL)
5c9a8750 364 return -EBUSY;
ded97d2c
VC
365 if (arg == KCOV_TRACE_PC)
366 kcov->mode = KCOV_MODE_TRACE_PC;
367 else if (arg == KCOV_TRACE_CMP)
368#ifdef CONFIG_KCOV_ENABLE_COMPARISONS
369 kcov->mode = KCOV_MODE_TRACE_CMP;
370#else
371 return -ENOTSUPP;
372#endif
373 else
374 return -EINVAL;
5c9a8750
DV
375 /* Cache in task struct for performance. */
376 t->kcov_size = kcov->size;
377 t->kcov_area = kcov->area;
ded97d2c 378 /* See comment in check_kcov_mode(). */
5c9a8750
DV
379 barrier();
380 WRITE_ONCE(t->kcov_mode, kcov->mode);
381 t->kcov = kcov;
382 kcov->t = t;
383 /* This is put either in kcov_task_exit() or in KCOV_DISABLE. */
384 kcov_get(kcov);
385 return 0;
386 case KCOV_DISABLE:
387 /* Disable coverage for the current task. */
388 unused = arg;
389 if (unused != 0 || current->kcov != kcov)
390 return -EINVAL;
391 t = current;
392 if (WARN_ON(kcov->t != t))
393 return -EINVAL;
394 kcov_task_init(t);
395 kcov->t = NULL;
ded97d2c 396 kcov->mode = KCOV_MODE_INIT;
5c9a8750
DV
397 kcov_put(kcov);
398 return 0;
399 default:
400 return -ENOTTY;
401 }
402}
403
404static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
405{
406 struct kcov *kcov;
407 int res;
408
409 kcov = filep->private_data;
410 spin_lock(&kcov->lock);
411 res = kcov_ioctl_locked(kcov, cmd, arg);
412 spin_unlock(&kcov->lock);
413 return res;
414}
415
416static const struct file_operations kcov_fops = {
417 .open = kcov_open,
418 .unlocked_ioctl = kcov_ioctl,
7483e5d4 419 .compat_ioctl = kcov_ioctl,
5c9a8750
DV
420 .mmap = kcov_mmap,
421 .release = kcov_close,
422};
423
424static int __init kcov_init(void)
425{
df4565f9
NS
426 /*
427 * The kcov debugfs file won't ever get removed and thus,
428 * there is no need to protect it against removal races. The
429 * use of debugfs_create_file_unsafe() is actually safe here.
430 */
431 if (!debugfs_create_file_unsafe("kcov", 0600, NULL, NULL, &kcov_fops)) {
5c9a8750
DV
432 pr_err("failed to create kcov in debugfs\n");
433 return -ENOMEM;
434 }
435 return 0;
436}
437
438device_initcall(kcov_init);