kfree.cocci 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /// Find a use after free.
  2. //# Values of variables may imply that some
  3. //# execution paths are not possible, resulting in false positives.
  4. //# Another source of false positives are macros such as
  5. //# SCTP_DBG_OBJCNT_DEC that do not actually evaluate their argument
  6. ///
  7. // Confidence: Moderate
  8. // Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
  9. // Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
  10. // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
  11. // URL: http://coccinelle.lip6.fr/
  12. // Comments:
  13. // Options: -no_includes -include_headers
  14. virtual org
  15. virtual report
  16. @free@
  17. expression E;
  18. position p1;
  19. @@
  20. kfree@p1(E)
  21. @print expression@
  22. constant char *c;
  23. expression free.E,E2;
  24. type T;
  25. position p;
  26. identifier f;
  27. @@
  28. (
  29. f(...,c,...,(T)E@p,...)
  30. |
  31. E@p == E2
  32. |
  33. E@p != E2
  34. |
  35. !E@p
  36. |
  37. E@p || ...
  38. )
  39. @sz@
  40. expression free.E;
  41. position p;
  42. @@
  43. sizeof(<+...E@p...+>)
  44. @loop exists@
  45. expression E;
  46. identifier l;
  47. position ok;
  48. @@
  49. while (1) { ...
  50. kfree@ok(E)
  51. ... when != break;
  52. when != goto l;
  53. when forall
  54. }
  55. @r exists@
  56. expression free.E, subE<=free.E, E2;
  57. expression E1;
  58. iterator iter;
  59. statement S;
  60. position free.p1!=loop.ok,p2!={print.p,sz.p};
  61. @@
  62. kfree@p1(E,...)
  63. ...
  64. (
  65. iter(...,subE,...) S // no use
  66. |
  67. list_remove_head(E1,subE,...)
  68. |
  69. subE = E2
  70. |
  71. subE++
  72. |
  73. ++subE
  74. |
  75. --subE
  76. |
  77. subE--
  78. |
  79. &subE
  80. |
  81. BUG(...)
  82. |
  83. BUG_ON(...)
  84. |
  85. return_VALUE(...)
  86. |
  87. return_ACPI_STATUS(...)
  88. |
  89. E@p2 // bad use
  90. )
  91. @script:python depends on org@
  92. p1 << free.p1;
  93. p2 << r.p2;
  94. @@
  95. cocci.print_main("kfree",p1)
  96. cocci.print_secs("ref",p2)
  97. @script:python depends on report@
  98. p1 << free.p1;
  99. p2 << r.p2;
  100. @@
  101. msg = "reference preceded by free on line %s" % (p1[0].line)
  102. coccilib.report.print_report(p2[0],msg)