memdup_user.cocci 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /// Use memdup_user rather than duplicating its implementation
  2. /// This is a little bit restricted to reduce false positives
  3. ///
  4. // 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.
  8. // URL: http://coccinelle.lip6.fr/
  9. // Comments:
  10. // Options: --no-includes --include-headers
  11. virtual patch
  12. virtual context
  13. virtual org
  14. virtual report
  15. @depends on patch@
  16. expression from,to,size,flag;
  17. identifier l1,l2;
  18. @@
  19. - to = \(kmalloc\|kzalloc\)(size,flag);
  20. + to = memdup_user(from,size);
  21. if (
  22. - to==NULL
  23. + IS_ERR(to)
  24. || ...) {
  25. <+... when != goto l1;
  26. - -ENOMEM
  27. + PTR_ERR(to)
  28. ...+>
  29. }
  30. - if (copy_from_user(to, from, size) != 0) {
  31. - <+... when != goto l2;
  32. - -EFAULT
  33. - ...+>
  34. - }
  35. @r depends on !patch@
  36. expression from,to,size,flag;
  37. position p;
  38. statement S1,S2;
  39. @@
  40. * to = \(kmalloc@p\|kzalloc@p\)(size,flag);
  41. if (to==NULL || ...) S1
  42. if (copy_from_user(to, from, size) != 0)
  43. S2
  44. @script:python depends on org@
  45. p << r.p;
  46. @@
  47. coccilib.org.print_todo(p[0], "WARNING opportunity for memdup_user")
  48. @script:python depends on report@
  49. p << r.p;
  50. @@
  51. coccilib.report.print_report(p[0], "WARNING opportunity for memdup_user")