.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /// Use memdup_user rather than duplicating its implementation |
---|
2 | 3 | /// This is a little bit restricted to reduce false positives |
---|
3 | 4 | /// |
---|
4 | 5 | // Confidence: High |
---|
5 | | -// Copyright: (C) 2010-2012 Nicolas Palix. GPLv2. |
---|
6 | | -// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2. |
---|
7 | | -// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2. |
---|
| 6 | +// Copyright: (C) 2010-2012 Nicolas Palix. |
---|
| 7 | +// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. |
---|
| 8 | +// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. |
---|
8 | 9 | // URL: http://coccinelle.lip6.fr/ |
---|
9 | 10 | // Comments: |
---|
10 | 11 | // Options: --no-includes --include-headers |
---|
.. | .. |
---|
14 | 15 | virtual org |
---|
15 | 16 | virtual report |
---|
16 | 17 | |
---|
| 18 | +@initialize:python@ |
---|
| 19 | +@@ |
---|
| 20 | +filter = frozenset(['memdup_user', 'vmemdup_user']) |
---|
| 21 | + |
---|
| 22 | +def relevant(p): |
---|
| 23 | + return not (filter & {el.current_element for el in p}) |
---|
| 24 | + |
---|
17 | 25 | @depends on patch@ |
---|
18 | 26 | expression from,to,size; |
---|
19 | 27 | identifier l1,l2; |
---|
| 28 | +position p : script:python() { relevant(p) }; |
---|
20 | 29 | @@ |
---|
21 | 30 | |
---|
22 | | -- to = \(kmalloc\|kzalloc\)(size,GFP_KERNEL); |
---|
| 31 | +- to = \(kmalloc@p\|kzalloc@p\) |
---|
| 32 | +- (size,\(GFP_KERNEL\|GFP_USER\| |
---|
| 33 | +- \(GFP_KERNEL\|GFP_USER\)|__GFP_NOWARN\)); |
---|
23 | 34 | + to = memdup_user(from,size); |
---|
| 35 | + if ( |
---|
| 36 | +- to==NULL |
---|
| 37 | ++ IS_ERR(to) |
---|
| 38 | + || ...) { |
---|
| 39 | + <+... when != goto l1; |
---|
| 40 | +- -ENOMEM |
---|
| 41 | ++ PTR_ERR(to) |
---|
| 42 | + ...+> |
---|
| 43 | + } |
---|
| 44 | +- if (copy_from_user(to, from, size) != 0) { |
---|
| 45 | +- <+... when != goto l2; |
---|
| 46 | +- -EFAULT |
---|
| 47 | +- ...+> |
---|
| 48 | +- } |
---|
| 49 | + |
---|
| 50 | +@depends on patch@ |
---|
| 51 | +expression from,to,size; |
---|
| 52 | +identifier l1,l2; |
---|
| 53 | +position p : script:python() { relevant(p) }; |
---|
| 54 | +@@ |
---|
| 55 | + |
---|
| 56 | +- to = \(kvmalloc@p\|kvzalloc@p\)(size,\(GFP_KERNEL\|GFP_USER\)); |
---|
| 57 | ++ to = vmemdup_user(from,size); |
---|
24 | 58 | if ( |
---|
25 | 59 | - to==NULL |
---|
26 | 60 | + IS_ERR(to) |
---|
.. | .. |
---|
38 | 72 | |
---|
39 | 73 | @r depends on !patch@ |
---|
40 | 74 | expression from,to,size; |
---|
41 | | -position p; |
---|
| 75 | +position p : script:python() { relevant(p) }; |
---|
42 | 76 | statement S1,S2; |
---|
43 | 77 | @@ |
---|
44 | 78 | |
---|
45 | | -* to = \(kmalloc@p\|kzalloc@p\)(size,GFP_KERNEL); |
---|
| 79 | +* to = \(kmalloc@p\|kzalloc@p\) |
---|
| 80 | + (size,\(GFP_KERNEL\|GFP_USER\| |
---|
| 81 | + \(GFP_KERNEL\|GFP_USER\)|__GFP_NOWARN\)); |
---|
| 82 | + if (to==NULL || ...) S1 |
---|
| 83 | + if (copy_from_user(to, from, size) != 0) |
---|
| 84 | + S2 |
---|
| 85 | + |
---|
| 86 | +@rv depends on !patch@ |
---|
| 87 | +expression from,to,size; |
---|
| 88 | +position p : script:python() { relevant(p) }; |
---|
| 89 | +statement S1,S2; |
---|
| 90 | +@@ |
---|
| 91 | + |
---|
| 92 | +* to = \(kvmalloc@p\|kvzalloc@p\)(size,\(GFP_KERNEL\|GFP_USER\)); |
---|
46 | 93 | if (to==NULL || ...) S1 |
---|
47 | 94 | if (copy_from_user(to, from, size) != 0) |
---|
48 | 95 | S2 |
---|
.. | .. |
---|
58 | 105 | @@ |
---|
59 | 106 | |
---|
60 | 107 | coccilib.report.print_report(p[0], "WARNING opportunity for memdup_user") |
---|
| 108 | + |
---|
| 109 | +@script:python depends on org@ |
---|
| 110 | +p << rv.p; |
---|
| 111 | +@@ |
---|
| 112 | + |
---|
| 113 | +coccilib.org.print_todo(p[0], "WARNING opportunity for vmemdup_user") |
---|
| 114 | + |
---|
| 115 | +@script:python depends on report@ |
---|
| 116 | +p << rv.p; |
---|
| 117 | +@@ |
---|
| 118 | + |
---|
| 119 | +coccilib.report.print_report(p[0], "WARNING opportunity for vmemdup_user") |
---|