OpenCVCompilerDefenses.cmake 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Enable build defense flags.
  2. # Performance may be affected.
  3. # More information:
  4. # - https://www.owasp.org/index.php/C-Based_Toolchain_Hardening
  5. # - https://wiki.debian.org/Hardening
  6. # - https://wiki.gentoo.org/wiki/Hardened/Toolchain
  7. # - https://docs.microsoft.com/en-us/cpp/build/reference/sdl-enable-additional-security-checks
  8. # - https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/BufferOverflows.html
  9. set(OPENCV_LINKER_DEFENSES_FLAGS_COMMON "")
  10. macro(ocv_add_defense_compiler_flag option)
  11. ocv_check_flag_support(CXX "${option}" _varname "${ARGN}")
  12. if(${_varname})
  13. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${option}")
  14. endif()
  15. ocv_check_flag_support(C "${option}" _varname "${ARGN}")
  16. if(${_varname})
  17. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${option}")
  18. endif()
  19. endmacro()
  20. macro(ocv_add_defense_compiler_flag_release option)
  21. ocv_check_flag_support(CXX "${option}" _varname "${ARGN}")
  22. if(${_varname})
  23. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${option}")
  24. endif()
  25. ocv_check_flag_support(C "${option}" _varname "${ARGN}")
  26. if(${_varname})
  27. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${option}")
  28. endif()
  29. endmacro()
  30. # Define flags
  31. if(MSVC)
  32. ocv_add_defense_compiler_flag("/GS")
  33. ocv_add_defense_compiler_flag("/sdl")
  34. ocv_add_defense_compiler_flag("/guard:cf")
  35. ocv_add_defense_compiler_flag("/w34018 /w34146 /w34244 /w34267 /w34302 /w34308 /w34509 /w34532 /w34533 /w34700 /w34789 /w34995 /w34996")
  36. set(OPENCV_LINKER_DEFENSES_FLAGS_COMMON "${OPENCV_LINKER_DEFENSES_FLAGS_COMMON} /guard:cf /dynamicbase" )
  37. if(NOT X86_64)
  38. set(OPENCV_LINKER_DEFENSES_FLAGS_COMMON "${OPENCV_LINKER_DEFENSES_FLAGS_COMMON} /safeseh")
  39. endif()
  40. elseif(CV_CLANG)
  41. ocv_add_defense_compiler_flag("-fstack-protector-strong")
  42. ocv_add_defense_compiler_flag_release("-D_FORTIFY_SOURCE=2")
  43. if (NOT APPLE)
  44. set(OPENCV_LINKER_DEFENSES_FLAGS_COMMON "${OPENCV_LINKER_DEFENSES_FLAGS_COMMON} -z noexecstack -z relro -z now" )
  45. endif()
  46. elseif(CV_GCC)
  47. if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9")
  48. ocv_add_defense_compiler_flag("-fstack-protector")
  49. else()
  50. ocv_add_defense_compiler_flag("-fstack-protector-strong")
  51. endif()
  52. # These flags is added by general options: -Wformat -Wformat-security
  53. if(NOT CMAKE_CXX_FLAGS MATCHES "-Wformat" OR NOT CMAKE_CXX_FLAGS MATCHES "format-security")
  54. message(FATAL_ERROR "Defense flags: uncompatible options")
  55. endif()
  56. if(ANDROID)
  57. ocv_add_defense_compiler_flag_release("-D_FORTIFY_SOURCE=2")
  58. if(NOT CMAKE_CXX_FLAGS_RELEASE MATCHES "-D_FORTIFY_SOURCE=2") # TODO Check this
  59. ocv_add_defense_compiler_flag_release("-D_FORTIFY_SOURCE=1")
  60. endif()
  61. else()
  62. ocv_add_defense_compiler_flag_release("-D_FORTIFY_SOURCE=2")
  63. endif()
  64. set(OPENCV_LINKER_DEFENSES_FLAGS_COMMON "${OPENCV_LINKER_DEFENSES_FLAGS_COMMON} -z noexecstack -z relro -z now" )
  65. else()
  66. # not supported
  67. endif()
  68. set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
  69. if(CV_GCC OR CV_CLANG)
  70. if(NOT CMAKE_CXX_FLAGS MATCHES "-fPIC")
  71. ocv_add_defense_compiler_flag("-fPIC")
  72. endif()
  73. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIE -pie")
  74. endif()
  75. set( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OPENCV_LINKER_DEFENSES_FLAGS_COMMON}" )
  76. set( CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${OPENCV_LINKER_DEFENSES_FLAGS_COMMON}" )
  77. set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OPENCV_LINKER_DEFENSES_FLAGS_COMMON}" )