memdup_user.cocci 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /// Use memdup_user rather than duplicating its implementation
  3. /// This is a little bit restricted to reduce false positives
  4. ///
  5. // Confidence: High
  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.
  9. // URL: https://coccinelle.gitlabpages.inria.fr/website
  10. // Comments:
  11. // Options: --no-includes --include-headers
  12. virtual patch
  13. virtual context
  14. virtual org
  15. virtual report
  16. @initialize:python@
  17. @@
  18. filter = frozenset(['memdup_user', 'vmemdup_user'])
  19. def relevant(p):
  20. return not (filter & {el.current_element for el in p})
  21. @depends on patch@
  22. expression from,to,size;
  23. identifier l1,l2;
  24. position p : script:python() { relevant(p) };
  25. @@
  26. - to = \(kmalloc@p\|kzalloc@p\)
  27. - (size,\(GFP_KERNEL\|GFP_USER\|
  28. - \(GFP_KERNEL\|GFP_USER\)|__GFP_NOWARN\));
  29. + to = memdup_user(from,size);
  30. if (
  31. - to==NULL
  32. + IS_ERR(to)
  33. || ...) {
  34. <+... when != goto l1;
  35. - -ENOMEM
  36. + PTR_ERR(to)
  37. ...+>
  38. }
  39. - if (copy_from_user(to, from, size) != 0) {
  40. - <+... when != goto l2;
  41. - -EFAULT
  42. - ...+>
  43. - }
  44. @depends on patch@
  45. expression from,to,size;
  46. identifier l1,l2;
  47. position p : script:python() { relevant(p) };
  48. @@
  49. - to = \(kvmalloc@p\|kvzalloc@p\)(size,\(GFP_KERNEL\|GFP_USER\));
  50. + to = vmemdup_user(from,size);
  51. if (
  52. - to==NULL
  53. + IS_ERR(to)
  54. || ...) {
  55. <+... when != goto l1;
  56. - -ENOMEM
  57. + PTR_ERR(to)
  58. ...+>
  59. }
  60. - if (copy_from_user(to, from, size) != 0) {
  61. - <+... when != goto l2;
  62. - -EFAULT
  63. - ...+>
  64. - }
  65. @r depends on !patch@
  66. expression from,to,size;
  67. position p : script:python() { relevant(p) };
  68. statement S1,S2;
  69. @@
  70. * to = \(kmalloc@p\|kzalloc@p\)
  71. (size,\(GFP_KERNEL\|GFP_USER\|
  72. \(GFP_KERNEL\|GFP_USER\)|__GFP_NOWARN\));
  73. if (to==NULL || ...) S1
  74. if (copy_from_user(to, from, size) != 0)
  75. S2
  76. @rv depends on !patch@
  77. expression from,to,size;
  78. position p : script:python() { relevant(p) };
  79. statement S1,S2;
  80. @@
  81. * to = \(kvmalloc@p\|kvzalloc@p\)(size,\(GFP_KERNEL\|GFP_USER\));
  82. if (to==NULL || ...) S1
  83. if (copy_from_user(to, from, size) != 0)
  84. S2
  85. @script:python depends on org@
  86. p << r.p;
  87. @@
  88. coccilib.org.print_todo(p[0], "WARNING opportunity for memdup_user")
  89. @script:python depends on report@
  90. p << r.p;
  91. @@
  92. coccilib.report.print_report(p[0], "WARNING opportunity for memdup_user")
  93. @script:python depends on org@
  94. p << rv.p;
  95. @@
  96. coccilib.org.print_todo(p[0], "WARNING opportunity for vmemdup_user")
  97. @script:python depends on report@
  98. p << rv.p;
  99. @@
  100. coccilib.report.print_report(p[0], "WARNING opportunity for vmemdup_user")