kfree.cocci 1.6 KB

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