Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 1 | /// Use memdup_user rather than duplicating its implementation |
Nicolas Palix | 0341745 | 2010-08-24 17:39:07 +0200 | [diff] [blame] | 2 | /// This is a little bit restricted to reduce false positives |
| 3 | /// |
| 4 | // Confidence: High |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 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. |
Nicolas Palix | 0341745 | 2010-08-24 17:39:07 +0200 | [diff] [blame] | 8 | // URL: http://coccinelle.lip6.fr/ |
| 9 | // Comments: |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 10 | // Options: --no-includes --include-headers |
Nicolas Palix | 0341745 | 2010-08-24 17:39:07 +0200 | [diff] [blame] | 11 | |
| 12 | virtual patch |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 13 | virtual context |
| 14 | virtual org |
| 15 | virtual report |
Nicolas Palix | 0341745 | 2010-08-24 17:39:07 +0200 | [diff] [blame] | 16 | |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 17 | @depends on patch@ |
Julia Lawall | 43d9639 | 2016-09-21 17:48:39 +0200 | [diff] [blame] | 18 | expression from,to,size; |
Nicolas Palix | 0341745 | 2010-08-24 17:39:07 +0200 | [diff] [blame] | 19 | identifier l1,l2; |
| 20 | @@ |
| 21 | |
Julia Lawall | 43d9639 | 2016-09-21 17:48:39 +0200 | [diff] [blame] | 22 | - to = \(kmalloc\|kzalloc\)(size,GFP_KERNEL); |
Nicolas Palix | 0341745 | 2010-08-24 17:39:07 +0200 | [diff] [blame] | 23 | + to = memdup_user(from,size); |
| 24 | if ( |
| 25 | - to==NULL |
| 26 | + IS_ERR(to) |
| 27 | || ...) { |
| 28 | <+... when != goto l1; |
| 29 | - -ENOMEM |
| 30 | + PTR_ERR(to) |
| 31 | ...+> |
| 32 | } |
| 33 | - if (copy_from_user(to, from, size) != 0) { |
| 34 | - <+... when != goto l2; |
| 35 | - -EFAULT |
| 36 | - ...+> |
| 37 | - } |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 38 | |
| 39 | @r depends on !patch@ |
Julia Lawall | 43d9639 | 2016-09-21 17:48:39 +0200 | [diff] [blame] | 40 | expression from,to,size; |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 41 | position p; |
| 42 | statement S1,S2; |
| 43 | @@ |
| 44 | |
Julia Lawall | 43d9639 | 2016-09-21 17:48:39 +0200 | [diff] [blame] | 45 | * to = \(kmalloc@p\|kzalloc@p\)(size,GFP_KERNEL); |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 46 | if (to==NULL || ...) S1 |
| 47 | if (copy_from_user(to, from, size) != 0) |
| 48 | S2 |
| 49 | |
| 50 | @script:python depends on org@ |
| 51 | p << r.p; |
| 52 | @@ |
| 53 | |
Fengguang Wu | 7c6c4d5 | 2012-07-25 19:35:30 +0800 | [diff] [blame] | 54 | coccilib.org.print_todo(p[0], "WARNING opportunity for memdup_user") |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 55 | |
| 56 | @script:python depends on report@ |
| 57 | p << r.p; |
| 58 | @@ |
| 59 | |
Fengguang Wu | 7c6c4d5 | 2012-07-25 19:35:30 +0800 | [diff] [blame] | 60 | coccilib.report.print_report(p[0], "WARNING opportunity for memdup_user") |