contrast-agent 3.12.1 → 3.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (287) hide show
  1. checksums.yaml +4 -4
  2. data/.dockerignore +0 -1
  3. data/.gitignore +1 -1
  4. data/.simplecov +1 -1
  5. data/Rakefile +31 -0
  6. data/exe/contrast_service +1 -7
  7. data/ext/build_funchook.rb +0 -2
  8. data/ext/cs__assess_active_record_named/cs__active_record_named.c +8 -7
  9. data/ext/cs__assess_array/cs__assess_array.c +6 -5
  10. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +5 -5
  11. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +4 -9
  12. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.h +0 -1
  13. data/ext/cs__assess_hash/cs__assess_hash.c +18 -17
  14. data/ext/cs__assess_hash/cs__assess_hash.h +2 -1
  15. data/ext/cs__assess_kernel/cs__assess_kernel.c +7 -8
  16. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +4 -5
  17. data/ext/cs__assess_module/cs__assess_module.c +6 -6
  18. data/ext/cs__assess_regexp/cs__assess_regexp.c +4 -4
  19. data/ext/cs__assess_string/cs__assess_string.c +20 -4
  20. data/ext/cs__assess_string/cs__assess_string.h +4 -1
  21. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +4 -7
  22. data/ext/cs__assess_yield_track/cs__assess_yield_track.c +3 -7
  23. data/ext/cs__assess_yield_track/cs__assess_yield_track.h +0 -1
  24. data/ext/cs__common/cs__common.c +59 -28
  25. data/ext/cs__common/cs__common.h +18 -20
  26. data/ext/cs__common/extconf.rb +0 -14
  27. data/ext/cs__contrast_patch/cs__contrast_patch.c +27 -25
  28. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -7
  29. data/ext/cs__protect_kernel/cs__protect_kernel.c +11 -12
  30. data/ext/cs__protect_kernel/cs__protect_kernel.h +2 -2
  31. data/ext/extconf_common.rb +0 -28
  32. data/lib/contrast-agent.rb +1 -1
  33. data/lib/contrast.rb +15 -23
  34. data/lib/contrast/agent.rb +51 -39
  35. data/lib/contrast/agent/assess.rb +11 -12
  36. data/lib/contrast/agent/assess/contrast_event.rb +39 -176
  37. data/lib/contrast/agent/assess/events/event_factory.rb +2 -2
  38. data/lib/contrast/agent/assess/events/source_event.rb +5 -9
  39. data/lib/contrast/agent/assess/finalizers/finalize.rb +21 -0
  40. data/lib/contrast/agent/assess/finalizers/freeze.rb +13 -0
  41. data/lib/contrast/agent/assess/finalizers/hash.rb +53 -0
  42. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +2 -2
  43. data/lib/contrast/agent/assess/policy/patcher.rb +5 -5
  44. data/lib/contrast/agent/assess/policy/policy.rb +9 -9
  45. data/lib/contrast/agent/assess/policy/policy_node.rb +5 -99
  46. data/lib/contrast/agent/assess/policy/policy_scanner.rb +3 -3
  47. data/lib/contrast/agent/assess/policy/preshift.rb +15 -5
  48. data/lib/contrast/agent/assess/policy/propagation_method.rb +26 -24
  49. data/lib/contrast/agent/assess/policy/propagation_node.rb +6 -2
  50. data/lib/contrast/agent/assess/policy/propagator.rb +18 -18
  51. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  52. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  53. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -1
  54. data/lib/contrast/agent/assess/policy/propagator/insert.rb +1 -4
  55. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +12 -3
  56. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +5 -0
  57. data/lib/contrast/agent/assess/policy/propagator/remove.rb +6 -11
  58. data/lib/contrast/agent/assess/policy/propagator/select.rb +4 -4
  59. data/lib/contrast/agent/assess/policy/propagator/split.rb +5 -5
  60. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +8 -7
  61. data/lib/contrast/agent/assess/policy/propagator/trim.rb +6 -10
  62. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +5 -5
  63. data/lib/contrast/agent/assess/policy/source_method.rb +6 -16
  64. data/lib/contrast/agent/assess/policy/source_validation/cross_site_validator.rb +1 -1
  65. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +1 -1
  66. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +1 -1
  67. data/lib/contrast/agent/assess/policy/trigger_method.rb +7 -15
  68. data/lib/contrast/agent/assess/policy/trigger_node.rb +18 -6
  69. data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +2 -2
  70. data/lib/contrast/agent/assess/properties.rb +13 -383
  71. data/lib/contrast/agent/assess/property/evented.rb +78 -0
  72. data/lib/contrast/agent/assess/property/tagged.rb +339 -0
  73. data/lib/contrast/agent/assess/rule.rb +2 -2
  74. data/lib/contrast/agent/assess/rule/base.rb +3 -4
  75. data/lib/contrast/agent/assess/rule/provider.rb +3 -3
  76. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +1 -1
  77. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +4 -22
  78. data/lib/contrast/agent/assess/tag.rb +27 -12
  79. data/lib/contrast/agent/at_exit_hook.rb +4 -2
  80. data/lib/contrast/agent/class_reopener.rb +7 -6
  81. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +1 -1
  82. data/lib/contrast/agent/deadzone/policy/policy.rb +2 -2
  83. data/lib/contrast/agent/disable_reaction.rb +1 -1
  84. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  85. data/lib/contrast/agent/inventory/policy/datastores.rb +2 -3
  86. data/lib/contrast/agent/inventory/policy/policy.rb +3 -3
  87. data/lib/contrast/agent/inventory/policy/trigger_node.rb +1 -1
  88. data/lib/contrast/agent/middleware.rb +37 -48
  89. data/lib/contrast/agent/patching/policy/after_load_patch.rb +4 -4
  90. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +6 -5
  91. data/lib/contrast/agent/patching/policy/module_policy.rb +1 -1
  92. data/lib/contrast/agent/patching/policy/patch.rb +16 -16
  93. data/lib/contrast/agent/patching/policy/patcher.rb +30 -22
  94. data/lib/contrast/agent/patching/policy/policy.rb +9 -9
  95. data/lib/contrast/agent/patching/policy/policy_node.rb +3 -3
  96. data/lib/contrast/agent/patching/policy/trigger_node.rb +2 -2
  97. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +3 -3
  98. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +2 -2
  99. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  100. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +3 -3
  101. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +2 -2
  102. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +3 -3
  103. data/lib/contrast/agent/protect/policy/policy.rb +8 -8
  104. data/lib/contrast/agent/protect/policy/rule_applicator.rb +3 -3
  105. data/lib/contrast/agent/protect/policy/trigger_node.rb +1 -1
  106. data/lib/contrast/agent/protect/rule.rb +18 -18
  107. data/lib/contrast/agent/protect/rule/base.rb +21 -32
  108. data/lib/contrast/agent/protect/rule/base_service.rb +2 -2
  109. data/lib/contrast/agent/protect/rule/cmd_injection.rb +5 -5
  110. data/lib/contrast/agent/protect/rule/deserialization.rb +1 -1
  111. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +3 -8
  112. data/lib/contrast/agent/protect/rule/no_sqli.rb +1 -1
  113. data/lib/contrast/agent/protect/rule/path_traversal.rb +4 -5
  114. data/lib/contrast/agent/protect/rule/sqli.rb +2 -2
  115. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +1 -1
  116. data/lib/contrast/agent/protect/rule/xss.rb +1 -1
  117. data/lib/contrast/agent/protect/rule/xxe.rb +4 -5
  118. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +2 -2
  119. data/lib/contrast/agent/railtie.rb +1 -1
  120. data/lib/contrast/agent/reaction_processor.rb +5 -5
  121. data/lib/contrast/agent/request.rb +103 -340
  122. data/lib/contrast/agent/request_context.rb +25 -21
  123. data/lib/contrast/agent/request_handler.rb +1 -1
  124. data/lib/contrast/agent/response.rb +8 -20
  125. data/lib/contrast/agent/rewriter.rb +5 -6
  126. data/lib/contrast/agent/scope.rb +1 -1
  127. data/lib/contrast/agent/service_heartbeat.rb +8 -10
  128. data/lib/contrast/agent/static_analysis.rb +4 -4
  129. data/lib/contrast/agent/thread.rb +1 -1
  130. data/lib/contrast/agent/thread_watcher.rb +49 -0
  131. data/lib/contrast/agent/tracepoint_hook.rb +1 -1
  132. data/lib/contrast/agent/version.rb +1 -1
  133. data/lib/contrast/agent/worker_thread.rb +24 -0
  134. data/lib/contrast/api.rb +4 -6
  135. data/lib/contrast/api/communication.rb +20 -0
  136. data/lib/contrast/api/communication/connection_status.rb +41 -0
  137. data/lib/contrast/api/communication/messaging_queue.rb +79 -0
  138. data/lib/contrast/{utils/service_response_util.rb → api/communication/response_processor.rb} +10 -19
  139. data/lib/contrast/api/communication/service_lifecycle.rb +61 -0
  140. data/lib/contrast/api/communication/socket.rb +45 -0
  141. data/lib/contrast/api/communication/socket_client.rb +76 -0
  142. data/lib/contrast/api/communication/speedracer.rb +111 -0
  143. data/lib/contrast/api/communication/tcp_socket.rb +31 -0
  144. data/lib/contrast/api/communication/unix_socket.rb +27 -0
  145. data/lib/contrast/api/decorators.rb +14 -4
  146. data/lib/contrast/api/decorators/address.rb +61 -0
  147. data/lib/contrast/api/decorators/application_settings.rb +10 -5
  148. data/lib/contrast/api/decorators/application_update.rb +5 -13
  149. data/lib/contrast/api/decorators/http_request.rb +140 -0
  150. data/lib/contrast/api/decorators/input_analysis.rb +3 -2
  151. data/lib/contrast/api/decorators/message.rb +76 -0
  152. data/lib/contrast/api/decorators/rasp_rule_sample.rb +29 -0
  153. data/lib/contrast/api/decorators/route_coverage.rb +58 -0
  154. data/lib/contrast/api/decorators/server_features.rb +3 -2
  155. data/lib/contrast/api/decorators/trace_event.rb +100 -0
  156. data/lib/contrast/api/decorators/trace_event_object.rb +58 -0
  157. data/lib/contrast/api/decorators/trace_event_signature.rb +47 -0
  158. data/lib/contrast/api/decorators/trace_taint_range.rb +52 -0
  159. data/lib/contrast/api/decorators/trace_taint_range_tags.rb +109 -0
  160. data/lib/contrast/api/decorators/user_input.rb +40 -0
  161. data/lib/contrast/common_agent_configuration.rb +2 -2
  162. data/lib/contrast/components/agent.rb +2 -2
  163. data/lib/contrast/components/app_context.rb +1 -8
  164. data/lib/contrast/components/config.rb +7 -12
  165. data/lib/contrast/components/contrast_service.rb +1 -1
  166. data/lib/contrast/components/interface.rb +16 -16
  167. data/lib/contrast/components/logger.rb +1 -1
  168. data/lib/contrast/components/scope.rb +3 -3
  169. data/lib/contrast/components/settings.rb +0 -6
  170. data/lib/contrast/config.rb +18 -18
  171. data/lib/contrast/config/base_configuration.rb +2 -2
  172. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  173. data/lib/contrast/configuration.rb +5 -5
  174. data/lib/contrast/extension/assess.rb +22 -22
  175. data/lib/contrast/extension/assess/array.rb +4 -4
  176. data/lib/contrast/extension/assess/assess_extension.rb +28 -31
  177. data/lib/contrast/extension/assess/eval_trigger.rb +3 -3
  178. data/lib/contrast/extension/assess/exec_trigger.rb +1 -1
  179. data/lib/contrast/extension/assess/fiber.rb +3 -3
  180. data/lib/contrast/extension/assess/hash.rb +5 -3
  181. data/lib/contrast/extension/assess/kernel.rb +7 -6
  182. data/lib/contrast/extension/assess/marshal.rb +62 -0
  183. data/lib/contrast/extension/assess/regexp.rb +3 -3
  184. data/lib/contrast/extension/assess/string.rb +6 -6
  185. data/lib/contrast/extension/inventory.rb +4 -5
  186. data/lib/contrast/extension/kernel.rb +1 -1
  187. data/lib/contrast/extension/module.rb +1 -1
  188. data/lib/contrast/extension/protect.rb +3 -3
  189. data/lib/contrast/extension/protect/kernel.rb +2 -2
  190. data/lib/contrast/extension/protect/psych.rb +2 -2
  191. data/lib/contrast/framework/base_support.rb +0 -23
  192. data/lib/contrast/framework/manager.rb +7 -17
  193. data/lib/contrast/framework/rack/patch/session_cookie.rb +1 -1
  194. data/lib/contrast/framework/rack/patch/support.rb +1 -1
  195. data/lib/contrast/framework/rack/support.rb +2 -2
  196. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +1 -3
  197. data/lib/contrast/framework/rails/patch/assess_configuration.rb +5 -6
  198. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +1 -1
  199. data/lib/contrast/framework/rails/patch/support.rb +3 -3
  200. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +2 -2
  201. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +2 -2
  202. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +2 -2
  203. data/lib/contrast/framework/rails/support.rb +56 -37
  204. data/lib/contrast/framework/sinatra/patch/base.rb +2 -2
  205. data/lib/contrast/framework/sinatra/patch/support.rb +1 -1
  206. data/lib/contrast/framework/sinatra/support.rb +13 -24
  207. data/lib/contrast/funchook/funchook.rb +45 -0
  208. data/lib/contrast/logger/application.rb +3 -3
  209. data/lib/contrast/logger/format.rb +64 -0
  210. data/lib/contrast/logger/log.rb +17 -9
  211. data/lib/contrast/logger/request.rb +30 -0
  212. data/lib/contrast/tasks/config.rb +1 -1
  213. data/lib/contrast/tasks/service.rb +2 -2
  214. data/lib/contrast/utils/assess/sampling_util.rb +2 -2
  215. data/lib/contrast/utils/assess/tracking_util.rb +46 -21
  216. data/lib/contrast/utils/boolean_util.rb +1 -1
  217. data/lib/contrast/utils/class_util.rb +5 -3
  218. data/lib/contrast/utils/freeze_util.rb +1 -1
  219. data/lib/contrast/utils/gemfile_reader.rb +5 -5
  220. data/lib/contrast/utils/hash_digest.rb +13 -3
  221. data/lib/contrast/utils/heap_dump_util.rb +2 -2
  222. data/lib/contrast/utils/invalid_configuration_util.rb +2 -18
  223. data/lib/contrast/utils/inventory_util.rb +6 -11
  224. data/lib/contrast/utils/io_util.rb +1 -1
  225. data/lib/contrast/utils/object_share.rb +0 -1
  226. data/lib/contrast/utils/os.rb +16 -4
  227. data/lib/contrast/utils/ruby_ast_rewriter.rb +1 -1
  228. data/lib/contrast/utils/sha256_builder.rb +2 -2
  229. data/lib/contrast/utils/stack_trace_utils.rb +2 -3
  230. data/lib/contrast/utils/string_utils.rb +1 -1
  231. data/lib/contrast/utils/tag_util.rb +1 -1
  232. data/lib/contrast/utils/thread_tracker.rb +1 -14
  233. data/lib/contrast/utils/timer.rb +1 -17
  234. data/resources/deadzone/policy.json +5 -0
  235. data/ruby-agent.gemspec +9 -9
  236. data/service_executables/VERSION +1 -1
  237. data/service_executables/linux/contrast-service +0 -0
  238. data/service_executables/mac/contrast-service +0 -0
  239. metadata +60 -79
  240. data/funchook/Makefile +0 -29
  241. data/funchook/autom4te.cache/output.0 +0 -4976
  242. data/funchook/autom4te.cache/requests +0 -78
  243. data/funchook/autom4te.cache/traces.0 +0 -364
  244. data/funchook/config.log +0 -490
  245. data/funchook/config.status +0 -1016
  246. data/funchook/configure +0 -4976
  247. data/funchook/src/Makefile +0 -70
  248. data/funchook/src/config.h +0 -101
  249. data/funchook/src/config.h.in +0 -100
  250. data/funchook/src/decoder.o +0 -0
  251. data/funchook/src/distorm.o +0 -0
  252. data/funchook/src/funchook.o +0 -0
  253. data/funchook/src/funchook_io.o +0 -0
  254. data/funchook/src/funchook_syscall.o +0 -0
  255. data/funchook/src/funchook_unix.o +0 -0
  256. data/funchook/src/funchook_x86.o +0 -0
  257. data/funchook/src/instructions.o +0 -0
  258. data/funchook/src/insts.o +0 -0
  259. data/funchook/src/libfunchook.so +0 -0
  260. data/funchook/src/mnemonics.o +0 -0
  261. data/funchook/src/operands.o +0 -0
  262. data/funchook/src/os_func.o +0 -0
  263. data/funchook/src/os_func_unix.o +0 -0
  264. data/funchook/src/prefix.o +0 -0
  265. data/funchook/src/printf_base.o +0 -0
  266. data/funchook/src/textdefs.o +0 -0
  267. data/funchook/src/wstring.o +0 -0
  268. data/funchook/test/Makefile +0 -43
  269. data/funchook/test/funchook_test +0 -0
  270. data/funchook/test/libfunchook_test.so +0 -0
  271. data/funchook/test/test_main.o +0 -0
  272. data/funchook/test/x86_64_test.o +0 -0
  273. data/lib/contrast/agent/assess/adjusted_span.rb +0 -27
  274. data/lib/contrast/agent/assess/insulator.rb +0 -49
  275. data/lib/contrast/agent/require_state.rb +0 -61
  276. data/lib/contrast/agent/socket_client.rb +0 -134
  277. data/lib/contrast/api/connection_status.rb +0 -49
  278. data/lib/contrast/api/socket.rb +0 -43
  279. data/lib/contrast/api/speedracer.rb +0 -188
  280. data/lib/contrast/api/tcp_socket.rb +0 -29
  281. data/lib/contrast/api/unix_socket.rb +0 -25
  282. data/lib/contrast/framework/sinatra/application_helper.rb +0 -51
  283. data/lib/contrast/framework/view_technologies_descriptor.rb +0 -21
  284. data/lib/contrast/internal_exception.rb +0 -8
  285. data/lib/contrast/utils/cache.rb +0 -58
  286. data/lib/contrast/utils/service_sender_util.rb +0 -167
  287. data/lib/contrast/utils/sinatra_helper.rb +0 -49
@@ -1,6 +1,9 @@
1
1
  #include <ruby.h>
2
2
 
3
3
  static VALUE rb_sym_assess_string_uminus;
4
+ static VALUE rb_sym_assess_string_freeze;
5
+ // Contrast::Agent::Assess::Finalizers::Finalize::PROPERTIES_HASH
6
+ static VALUE properties_hash;
4
7
  static VALUE rb_sym_dup;
5
8
  static VALUE rb_sym_freeze;
6
9
 
@@ -15,6 +18,6 @@ static VALUE rb_sym_freeze;
15
18
  * -HM
16
19
  */
17
20
  static VALUE contrast_assess_string_uminus(const int argc, VALUE *argv,
18
- const VALUE obj);
21
+ const VALUE obj);
19
22
 
20
23
  void Init_cs__assess_string(void);
@@ -3,7 +3,6 @@
3
3
 
4
4
  #include "cs__assess_string_interpolation26.h"
5
5
  #include "../cs__common/cs__common.h"
6
- #include <funchook.h>
7
6
  #include <ruby.h>
8
7
 
9
8
  static VALUE rb_str_concat_literals_hook(size_t num, VALUE *strary) {
@@ -14,18 +13,16 @@ static VALUE rb_str_concat_literals_hook(size_t num, VALUE *strary) {
14
13
  }
15
14
 
16
15
  static int install_hooks() {
17
- funchook_t *funchook = funchook_create();
18
-
19
16
  rb_str_concat_literals_original = rb_str_concat_literals;
20
- funchook_prepare(funchook, (void **)&rb_str_concat_literals_original,
21
- rb_str_concat_literals_hook);
17
+ patch_via_funchook(&rb_str_concat_literals_original,
18
+ &rb_str_concat_literals_hook);
22
19
 
23
- funchook_install(funchook, 0);
24
20
  return 0;
25
21
  }
26
22
 
27
23
  void Init_cs__assess_string_interpolation26(void) {
28
- string_propagator = rb_define_class_under(core_assess, "StringPropagator", rb_cObject);
24
+ string_propagator =
25
+ rb_define_class_under(core_assess, "StringPropagator", rb_cObject);
29
26
  track_interpolation = rb_intern("track_interpolation");
30
27
  install_hooks();
31
28
  }
@@ -3,13 +3,12 @@
3
3
 
4
4
  #include "cs__assess_yield_track.h"
5
5
  #include "../cs__common/cs__common.h"
6
- #include <funchook.h>
7
6
  #include <ruby.h>
8
7
 
9
8
  static VALUE rb_yield_hook(VALUE val, const VALUE self) {
10
9
  VALUE method = rb_funcall(rb_mKernel, rb_sym_method, 0);
11
10
 
12
- if(method == split_method && RB_TYPE_P(val, T_STRING)) {
11
+ if (method == split_method && RB_TYPE_P(val, T_STRING)) {
13
12
  rb_funcall(split_class, propagate_yield, 1, val);
14
13
  }
15
14
  VALUE result = rb_yield_original(val);
@@ -17,11 +16,8 @@ static VALUE rb_yield_hook(VALUE val, const VALUE self) {
17
16
  }
18
17
 
19
18
  static int install_yield_hooks() {
20
- funchook_t *funchook = funchook_create();
21
19
  rb_yield_original = rb_yield;
22
- funchook_prepare(funchook, (void **)&rb_yield_original,
23
- rb_yield_hook);
24
- funchook_install(funchook, 0);
20
+ patch_via_funchook(&rb_yield_original, &rb_yield_hook);
25
21
  return 0;
26
22
  }
27
23
 
@@ -29,6 +25,6 @@ void Init_cs__assess_yield_track(void) {
29
25
  VALUE base = rb_define_class_under(assess_propagator, "Base", rb_cObject);
30
26
  split_class = rb_define_class_under(assess_propagator, "Split", base);
31
27
  propagate_yield = rb_intern("propagate_yield");
32
- split_method = ID2SYM(rb_intern("split"));
28
+ split_method = ID2SYM(rb_intern("split"));
33
29
  install_yield_hooks();
34
30
  }
@@ -1,4 +1,3 @@
1
- #include <funchook.h>
2
1
  #include <ruby.h>
3
2
 
4
3
  static VALUE split_class;
@@ -2,6 +2,7 @@
2
2
  * https://www.contrastsecurity.com/enduser-terms-0317a for more details. */
3
3
 
4
4
  #include "cs__common.h"
5
+ #include <dlfcn.h>
5
6
  #include <ruby.h>
6
7
 
7
8
  /* Globals */
@@ -9,6 +10,7 @@
9
10
  VALUE contrast, agent, patching, policy, assess;
10
11
  VALUE core_extensions, core_assess;
11
12
  VALUE assess_policy, assess_propagator;
13
+ VALUE funchook_path;
12
14
 
13
15
  VALUE rb_sym_enter_scope;
14
16
  VALUE rb_sym_exit_scope;
@@ -19,6 +21,33 @@ VALUE rb_sym_method;
19
21
  VALUE rb_sym_cs_tracked;
20
22
  /* end globals */
21
23
 
24
+ void patch_via_funchook(void *original_function, void *hook_function) {
25
+ VALUE funchook_module_wrapper = rb_define_module("Funchook");
26
+ funchook_path = rb_iv_get(funchook_module_wrapper, "@path");
27
+
28
+ void *funchook_lib_handle;
29
+ void *funchook_reference, *(*funchook_create)(void);
30
+ int prepareResult, (*funchook_prepare)(void *, void **, void *);
31
+ int installResult, (*funchook_install)(void *, int);
32
+
33
+ funchook_lib_handle =
34
+ dlopen(StringValueCStr(funchook_path), RTLD_NOW | RTLD_GLOBAL);
35
+
36
+ /* Load the funchook methods we need */
37
+ funchook_create =
38
+ (void *(*)(void))dlsym(funchook_lib_handle, "funchook_create");
39
+ funchook_prepare = (int (*)(void *, void **, void *))dlsym(
40
+ funchook_lib_handle, "funchook_prepare");
41
+ funchook_install =
42
+ (int (*)(void *, int))dlsym(funchook_lib_handle, "funchook_install");
43
+
44
+ funchook_reference = (void *)(*funchook_create)();
45
+
46
+ prepareResult = (*funchook_prepare)(
47
+ funchook_reference, (void **)original_function, hook_function);
48
+ installResult = (*funchook_install)(funchook_reference, 0);
49
+ }
50
+
22
51
  void contrast_alias_method(const VALUE target, const char *to,
23
52
  const char *from) {
24
53
  rb_funcall(target, cs__send_method, 3, cs__alias_method_sym,
@@ -29,33 +58,32 @@ VALUE contrast_patcher() {
29
58
  return patcher;
30
59
  }
31
60
 
32
- VALUE contrast_register_patch(const char *module_name,
33
- const char *method_name,
34
- VALUE(c_fn)(const int, const VALUE*, const VALUE)
35
- ) {
36
- return _contrast_register_patch(module_name, method_name, c_fn, IMPL_ALIAS_INSTANCE);
61
+ VALUE contrast_register_patch(const char *module_name, const char *method_name,
62
+ VALUE(c_fn)(const int, const VALUE *,
63
+ const VALUE)) {
64
+ return _contrast_register_patch(module_name, method_name, c_fn,
65
+ IMPL_ALIAS_INSTANCE);
37
66
  }
38
67
 
39
-
40
68
  VALUE contrast_register_singleton_patch(const char *module_name,
41
69
  const char *method_name,
42
- VALUE(c_fn)(const int, const VALUE*, const VALUE)
43
- ) {
44
- return _contrast_register_patch(module_name, method_name, c_fn, IMPL_ALIAS_SINGLETON);
70
+ VALUE(c_fn)(const int, const VALUE *,
71
+ const VALUE)) {
72
+ return _contrast_register_patch(module_name, method_name, c_fn,
73
+ IMPL_ALIAS_SINGLETON);
45
74
  }
46
75
 
47
- VALUE contrast_register_singleton_prepend_patch(const char *module_name,
48
- const char *method_name,
49
- VALUE(c_fn)(const int, const VALUE*, const VALUE)
50
- ) {
51
- return _contrast_register_patch(module_name, method_name, c_fn, IMPL_PREPEND);
76
+ VALUE contrast_register_singleton_prepend_patch(
77
+ const char *module_name, const char *method_name,
78
+ VALUE(c_fn)(const int, const VALUE *, const VALUE)) {
79
+ return _contrast_register_patch(module_name, method_name, c_fn,
80
+ IMPL_PREPEND);
52
81
  }
53
82
 
54
- static VALUE _contrast_register_patch(const char *module_name,
55
- const char *method_name,
56
- VALUE(c_fn)(const int, const VALUE*, const VALUE),
57
- patch_impl patch
58
- ) {
83
+ static VALUE
84
+ _contrast_register_patch(const char *module_name, const char *method_name,
85
+ VALUE(c_fn)(const int, const VALUE *, const VALUE),
86
+ patch_impl patch) {
59
87
  VALUE contrast_bind_module = rb_funcall(rb_cModule, rb_intern("new"), 0);
60
88
  VALUE unbound_method = Qnil;
61
89
  VALUE rb_str_module_name = rb_str_new_cstr(module_name);
@@ -81,24 +109,27 @@ static VALUE _contrast_register_patch(const char *module_name,
81
109
  */
82
110
  rb_define_method(contrast_bind_module, method_name, (VALUE(*)())c_fn, -1);
83
111
  VALUE rb_sym_instance_method = rb_intern("instance_method");
84
- unbound_method = rb_funcall(contrast_bind_module, rb_sym_instance_method, 1, rb_str_method_name);
112
+ unbound_method = rb_funcall(contrast_bind_module, rb_sym_instance_method, 1,
113
+ rb_str_method_name);
85
114
  rb_undef_method(contrast_bind_module, method_name);
86
115
 
87
116
  /* map impl enum -> ruby symbol */
88
117
  VALUE impl = Qnil;
89
- switch(patch) {
90
- case IMPL_ALIAS_INSTANCE:
118
+ switch (patch) {
119
+ case IMPL_ALIAS_INSTANCE:
91
120
  impl = ID2SYM(rb_sym_alias_instance);
92
121
  break;
93
- case IMPL_ALIAS_SINGLETON:
122
+ case IMPL_ALIAS_SINGLETON:
94
123
  impl = ID2SYM(rb_sym_alias_singleton);
95
124
  break;
96
- case IMPL_PREPEND:
125
+ case IMPL_PREPEND:
97
126
  impl = ID2SYM(rb_sym_prepend);
98
127
  break;
99
128
  }
100
129
 
101
- VALUE underlying_method_name = rb_funcall(contrast_patcher(), rb_sym_register_c_patch, 3, rb_str_module_name, unbound_method, impl);
130
+ VALUE underlying_method_name =
131
+ rb_funcall(contrast_patcher(), rb_sym_register_c_patch, 3,
132
+ rb_str_module_name, unbound_method, impl);
102
133
  return SYM2ID(underlying_method_name);
103
134
  }
104
135
 
@@ -117,9 +148,9 @@ void Init_cs__common(void) {
117
148
 
118
149
  /* Used for returning unbound C functions */
119
150
  rb_sym_register_c_patch = rb_intern("register_c_patch");
120
- rb_sym_alias_instance = rb_intern("alias_instance");
121
- rb_sym_alias_singleton = rb_intern("alias_singleton");
122
- rb_sym_prepend = rb_intern("prepend");
151
+ rb_sym_alias_instance = rb_intern("alias_instance");
152
+ rb_sym_alias_singleton = rb_intern("alias_singleton");
153
+ rb_sym_prepend = rb_intern("prepend");
123
154
 
124
155
  /* Ensure definition of core Contrast instrumentation modules */
125
156
  contrast = rb_define_module("Contrast");
@@ -4,18 +4,18 @@
4
4
  #include <ruby.h>
5
5
 
6
6
  typedef enum {
7
- IMPL_ALIAS_INSTANCE,
8
- IMPL_ALIAS_SINGLETON,
9
- IMPL_PREPEND
7
+ IMPL_ALIAS_INSTANCE,
8
+ IMPL_ALIAS_SINGLETON,
9
+ IMPL_PREPEND
10
10
  } patch_impl;
11
11
 
12
-
13
12
  static VALUE cs__send_method;
14
13
  static VALUE cs__alias_method_sym;
15
14
 
16
15
  extern VALUE contrast, agent, patching, policy, assess;
17
16
  extern VALUE core_extensions, core_assess;
18
17
  extern VALUE assess_policy, assess_propagator;
18
+ extern VALUE funchook_path;
19
19
 
20
20
  extern VALUE rb_sym_enter_scope;
21
21
  extern VALUE rb_sym_exit_scope;
@@ -32,30 +32,28 @@ static VALUE rb_sym_alias_instance;
32
32
  static VALUE rb_sym_alias_singleton;
33
33
  static VALUE rb_sym_prepend;
34
34
 
35
+ void patch_via_funchook(void *original_function, void *hook_function);
36
+
35
37
  void contrast_alias_method(const VALUE target, const char *to,
36
38
  const char *from);
37
39
 
38
- VALUE contrast_register_patch(const char *module_name,
39
- const char *method_name,
40
- VALUE(c_fn)(const int, const VALUE*, const VALUE)
41
- );
40
+ VALUE contrast_register_patch(const char *module_name, const char *method_name,
41
+ VALUE(c_fn)(const int, const VALUE *,
42
+ const VALUE));
42
43
 
43
44
  VALUE contrast_register_singleton_patch(const char *module_name,
44
45
  const char *method_name,
45
- VALUE(c_fn)(const int, const VALUE*, const VALUE)
46
- );
47
-
48
- VALUE contrast_register_singleton_prepend_patch(const char *module_name,
49
- const char *method_name,
50
- VALUE(c_fn)(const int, const VALUE*, const VALUE)
51
- );
46
+ VALUE(c_fn)(const int, const VALUE *,
47
+ const VALUE));
52
48
 
53
- static VALUE _contrast_register_patch(const char *module_name,
54
- const char *method_name,
55
- VALUE(c_fn)(const int, const VALUE*, const VALUE),
56
- patch_impl patch_impl
57
- );
49
+ VALUE contrast_register_singleton_prepend_patch(
50
+ const char *module_name, const char *method_name,
51
+ VALUE(c_fn)(const int, const VALUE *, const VALUE));
58
52
 
53
+ static VALUE
54
+ _contrast_register_patch(const char *module_name, const char *method_name,
55
+ VALUE(c_fn)(const int, const VALUE *, const VALUE),
56
+ patch_impl patch_impl);
59
57
 
60
58
  VALUE contrast_patcher();
61
59
 
@@ -4,18 +4,4 @@
4
4
  require 'mkmf'
5
5
  require_relative '../../lib/contrast/agent/version'
6
6
 
7
- installed_path = __dir__
8
-
9
- origin = if !(/darwin/ =~ RUBY_PLATFORM).nil?
10
- '@loader_path'
11
- else
12
- '\$${ORIGIN}'
13
- end
14
-
15
- options = " -Wl,-rpath,#{ origin }/../../shared_libraries"
16
-
17
- $LDFLAGS << options if try_link('int main() {return 0;}', options)
18
-
19
- $LIBPATH << installed_path
20
-
21
7
  create_makefile 'cs__common/cs__common'
@@ -34,7 +34,6 @@ VALUE contrast_patch_call_original(const VALUE *args) {
34
34
  method = args[3];
35
35
  method_id = SYM2ID(method);
36
36
 
37
-
38
37
  /* It looks like we can find the last Ruby block given so long as we don't
39
38
  * change Ruby method scope (always call this function from C, not Ruby),
40
39
  * which is the point of this C call.
@@ -111,7 +110,8 @@ VALUE contrast_call_super(const VALUE *args) {
111
110
  }
112
111
 
113
112
  VALUE contrast_run_patches(const VALUE *wrapped_args) {
114
- VALUE impl, method, method_policy, object, original_args, original_ret, preshift, transformed_ret;
113
+ VALUE impl, method, method_policy, object, original_args, original_ret,
114
+ preshift, transformed_ret;
115
115
  int argc;
116
116
  VALUE *argv;
117
117
  VALUE rescue_args[6];
@@ -168,17 +168,17 @@ VALUE contrast_run_patches(const VALUE *wrapped_args) {
168
168
  */
169
169
 
170
170
  switch (impl) {
171
- case IMPL_ALIAS_INSTANCE:
172
- case IMPL_ALIAS_SINGLETON:
173
- original_ret = rb_rescue2(
174
- contrast_patch_call_original, original_args,
175
- contrast_patch_call_rescue, (VALUE)rescue_args, rb_eException, 0);
176
- break;
177
- case IMPL_PREPEND:
178
- original_ret = rb_rescue2(contrast_call_super, original_args,
179
- contrast_patch_call_rescue,
180
- (VALUE)rescue_args, rb_eException, 0);
181
- break;
171
+ case IMPL_ALIAS_INSTANCE:
172
+ case IMPL_ALIAS_SINGLETON:
173
+ original_ret = rb_rescue2(contrast_patch_call_original, original_args,
174
+ contrast_patch_call_rescue,
175
+ (VALUE)rescue_args, rb_eException, 0);
176
+ break;
177
+ case IMPL_PREPEND:
178
+ original_ret = rb_rescue2(contrast_call_super, original_args,
179
+ contrast_patch_call_rescue,
180
+ (VALUE)rescue_args, rb_eException, 0);
181
+ break;
182
182
  };
183
183
 
184
184
  /* If you're here, the original method did not throw an exception
@@ -302,16 +302,17 @@ VALUE contrast_patch_dispatch(const int argc, const VALUE *argv,
302
302
  }
303
303
 
304
304
  /* Otherwise, invoke Contrast analysis. */
305
- VALUE wrapped_args[7];
306
- wrapped_args[0] = impl;
307
- wrapped_args[1] = (VALUE)original_args;
308
- wrapped_args[2] = method;
309
- wrapped_args[3] = method_policy;
310
- wrapped_args[4] = object;
311
- wrapped_args[5] = INT2NUM(argc);
312
- wrapped_args[6] = (VALUE)argv;
313
-
314
- return rb_ensure(contrast_run_patches, (VALUE)wrapped_args, contrast_ensure_function, method_policy);
305
+ VALUE wrapped_args[7];
306
+ wrapped_args[0] = impl;
307
+ wrapped_args[1] = (VALUE)original_args;
308
+ wrapped_args[2] = method;
309
+ wrapped_args[3] = method_policy;
310
+ wrapped_args[4] = object;
311
+ wrapped_args[5] = INT2NUM(argc);
312
+ wrapped_args[6] = (VALUE)argv;
313
+
314
+ return rb_ensure(contrast_run_patches, (VALUE)wrapped_args,
315
+ contrast_ensure_function, method_policy);
315
316
 
316
317
  call_original:
317
318
 
@@ -351,8 +352,9 @@ VALUE contrast_patch_define_method(const VALUE self, const VALUE clazz,
351
352
  rb_funcall(method_policy, rb_sym_instance_method, 0);
352
353
  char *cStr;
353
354
  VALUE str;
354
- rb_funcall(patch_status, rb_sym_set_info_for, 5, clazz, original_method_name,
355
- method_policy, is_instance_method, cs_method);
355
+ rb_funcall(patch_status, rb_sym_set_info_for, 5, clazz,
356
+ original_method_name, method_policy, is_instance_method,
357
+ cs_method);
356
358
 
357
359
  /* Some methods we patch rely on a specific C level patch,
358
360
  * in those cases we should still add the method to the info_for hash
@@ -1,5 +1,5 @@
1
- #include <ruby.h>
2
1
  #include "../cs__common/cs__common.h"
2
+ #include <ruby.h>
3
3
 
4
4
  /* Calls to Contrast modules */
5
5
  /* Contrast::Agent::Patching::Policy::PatchStatus */
@@ -140,8 +140,7 @@ VALUE contrast_call_super(const VALUE *args);
140
140
  * instance (specifically for frozen sources)
141
141
  */
142
142
  VALUE contrast_patch_dispatch(const int argc, const VALUE *argv,
143
- const patch_impl impl,
144
- const VALUE object);
143
+ const patch_impl impl, const VALUE object);
145
144
 
146
145
  VALUE contrast_alias_instance_patch(const int argc, const VALUE *argv,
147
146
  const VALUE object);
@@ -149,7 +148,6 @@ VALUE contrast_alias_instance_patch(const int argc, const VALUE *argv,
149
148
  VALUE contrast_alias_singleton_patch(const int argc, const VALUE *argv,
150
149
  const VALUE object);
151
150
 
152
-
153
151
  VALUE contrast_prepend_patch(const int argc, const VALUE *argv,
154
152
  const VALUE object);
155
153
 
@@ -169,12 +167,12 @@ VALUE contrast_prepend_patch(const int argc, const VALUE *argv,
169
167
  * - prepending Foo with Foo::ContrastPrepend
170
168
  *
171
169
  * originalModule - Module; the actual Module being prepended
172
- * methodPolicy - :MethodPolicy; the method policy that apply to the method being redefined
170
+ * methodPolicy - :MethodPolicy; the method policy that apply to the method
171
+ * being redefined
173
172
  *
174
173
  * return - Boolean; if the prepend occurred or not
175
174
  */
176
- VALUE contrast_patch_prepend(const VALUE self,
177
- const VALUE originalModule,
175
+ VALUE contrast_patch_prepend(const VALUE self, const VALUE originalModule,
178
176
  const VALUE methodPolicy);
179
177
 
180
178
  /*