This repository was archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathplugin-ruby.c
480 lines (376 loc) · 12.6 KB
/
plugin-ruby.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef _RUBY_PLUGIN
/*******************************************************************************
*
* Ruby plugin
*
*/
#include <pthread.h>
#include <string.h>
#include <ctype.h>
#include <ruby.h>
#include <ruby/thread.h>
#include "core.h"
#include "plugin.h"
#include "plugin-ruby.h"
#include "utils.h"
#include "main.h"
struct proxenet_ruby_args {
VALUE rVM;
ID rFunc;
VALUE rArgs[3];
};
#define xlog_ruby(t, ...) xlog(t, "["_RUBY_VERSION_"] " __VA_ARGS__)
/**
* proxenet wrapper for Ruby rb_intern()
*/
static VALUE rb_intern_wrap(VALUE arg)
{
return rb_intern((char*)arg);
}
/**
* proxenet wrapper for Ruby rb_const_get()
*/
static VALUE rb_const_get_wrap(VALUE arg)
{
return rb_const_get(rb_cObject, arg);
}
/**
* Print message from last exception triggered by ruby
*/
static void proxenet_ruby_print_last_exception()
{
VALUE rException, rExceptStr;
rException = rb_errinfo(); /* get last exception */
rb_set_errinfo(Qnil); /* clear last exception */
rExceptStr = rb_funcall(rException, rb_intern("to_s"), 0, Qnil);
xlog_ruby(LOG_ERROR, "Exception: %s\n", StringValuePtr(rExceptStr));
return;
}
/**
*
*/
static VALUE proxenet_ruby_get_module_reference(char* name)
{
VALUE rModule, rPlugin;
int err;
char *plugin_name;
plugin_name = (isdigit(name[0])) ? name + 1 : name;
rPlugin = rb_protect( rb_intern_wrap, (VALUE)name, &err);
if (err){
xlog_ruby(LOG_ERROR, "%s(%s) failed miserably\n", "rb_intern", plugin_name);
proxenet_ruby_print_last_exception();
return 0;
}
rModule = rb_protect( rb_const_get_wrap, rPlugin, &err);
if (err){
xlog_ruby(LOG_ERROR, "%s(%s) failed miserably\n", "rb_const_get", plugin_name);
proxenet_ruby_print_last_exception();
return 0;
}
return rModule;
}
/**
* Initialize Ruby VM.
*/
int proxenet_ruby_initialize_vm(plugin_t* plugin)
{
static char* rArgs[2] = { "ruby", "/dev/null" };
interpreter_t *interpreter;
VALUE rRet;
interpreter = plugin->interpreter;
/* checks */
if (interpreter->ready)
return 0;
#ifdef DEBUG
xlog_ruby(LOG_DEBUG, "Initializing Ruby VM version %s\n", _RUBY_VERSION_);
#endif
/* init vm */
ruby_init();
/*
* The hack of calling ruby_process_options() with /dev/null allows to simply (in one call)
* init all the structures and encoding params by the vm
* Details: http://rxr.whitequark.org/mri/source/ruby.c#1915
*/
rRet = (VALUE)ruby_process_options(2, rArgs);
if (rRet == Qnil) {
xlog_ruby(LOG_ERROR, "Error on ruby_process_options(): %#x\n", rRet);
proxenet_ruby_print_last_exception();
return -1;
}
interpreter->vm = (void*) rb_mKernel;
interpreter->ready = true;
return 0;
}
/**
* Clean up Ruby VM context and disable the interpreter for proxenet.
*/
int proxenet_ruby_destroy_vm(interpreter_t* interpreter)
{
ruby_cleanup(0);
interpreter->ready = false;
interpreter->vm = NULL;
return 0;
}
/**
* Initialize Ruby plugin
*/
static int proxenet_ruby_initialize_function(plugin_t* plugin, req_t type)
{
int err;
/* get function ID */
switch(type) {
case REQUEST:
if (plugin->pre_function) {
return 0;
}
plugin->pre_function = (void*)rb_protect(rb_intern_wrap, (VALUE)CFG_REQUEST_PLUGIN_FUNCTION, &err);
if (err){
xlog_ruby(LOG_ERROR, "Failed to get '%s'\n", CFG_REQUEST_PLUGIN_FUNCTION);
proxenet_ruby_print_last_exception();
return -1;
}
if (plugin->pre_function) {
#ifdef DEBUG
xlog_ruby(LOG_DEBUG, "Loaded %s:%s\n", plugin->filename, CFG_REQUEST_PLUGIN_FUNCTION);
#endif
return 0;
}
break;
case RESPONSE:
if (plugin->post_function) {
return 0;
}
plugin->post_function = (void*)rb_protect(rb_intern_wrap, (VALUE)CFG_RESPONSE_PLUGIN_FUNCTION, &err);
if (err){
xlog_ruby(LOG_ERROR, "Failed to get '%s'\n", CFG_RESPONSE_PLUGIN_FUNCTION);
proxenet_ruby_print_last_exception();
return -1;
}
if (plugin->post_function) {
#ifdef DEBUG
xlog_ruby(LOG_DEBUG, "Loaded %s:%s\n", plugin->filename, CFG_RESPONSE_PLUGIN_FUNCTION);
#endif
return 0;
}
break;
case ONLOAD:
plugin->onload_function = (void*)rb_protect(rb_intern_wrap, (VALUE)CFG_ONLOAD_PLUGIN_FUNCTION, &err);
if (err){
xlog_ruby(LOG_WARNING, "Failed to get '%s'\n", CFG_ONLOAD_PLUGIN_FUNCTION);
}
return 0;
break;
case ONLEAVE:
plugin->onleave_function = (void*)rb_protect(rb_intern_wrap, (VALUE)CFG_ONLEAVE_PLUGIN_FUNCTION, &err);
if (err){
xlog_ruby(LOG_WARNING, "Failed to get '%s'\n", CFG_ONLEAVE_PLUGIN_FUNCTION);
}
return 0;
break;
default:
xlog_ruby(LOG_CRITICAL, "%s\n", "Should never be here, autokill !");
abort();
break;
}
xlog_ruby(LOG_ERROR, "%s\n", "Failed to find function");
return -1;
}
/**
* This function will safely be executed (gvl acquired)
* this should not create a blocking/bottleneck situation since the access to rb_mKernel is
* mutex-protected by proxenet before.
* This function assumes that exactly 3 arguments are expected: the id, the buffer, the size
* of this buffer.
*
* @return the result of rb_funcall2()
*/
static VALUE proxenet_ruby_safe_plugin_funcall2(VALUE arg)
{
struct proxenet_ruby_args* args = (struct proxenet_ruby_args*) arg;
return rb_funcall2(args->rVM, args->rFunc, 3, args->rArgs);
}
/**
*
*/
static VALUE proxenet_ruby_safe_funcall2(VALUE arg)
{
struct proxenet_ruby_args* args = (struct proxenet_ruby_args*) arg;
return rb_funcall2(args->rVM, args->rFunc, 0, NULL);
}
/**
* Safely load a Ruby script in the VM
*
* @return 0 upon success, -1 otherwise
*/
int proxenet_ruby_load_file(plugin_t* plugin)
{
char* pathname;
int res = 0;
VALUE rRet;
struct proxenet_ruby_args args;
if (!plugin->interpreter || !plugin->interpreter->ready){
xlog_ruby(LOG_ERROR, "Interpreter '%s' is not ready\n", _RUBY_VERSION_);
return -1;
}
if(plugin->state != INACTIVE){
#ifdef DEBUG
if(cfg->verbose > 2)
xlog_ruby(LOG_DEBUG, "Plugin '%s' is already loaded. Skipping...\n", plugin->name);
#endif
return 0;
}
pathname = plugin->fullpath;
rb_load_protect(rb_str_new2(pathname), false, &res);
if (res != 0) {
xlog_ruby(LOG_ERROR, "Error %d when load file '%s'\n", res, pathname);
proxenet_ruby_print_last_exception();
return -1;
}
if (cfg->verbose)
xlog_ruby(LOG_INFO, "File '%s' is loaded\n", pathname);
/* Storing function pointers in structure */
if (proxenet_ruby_initialize_function(plugin, REQUEST) < 0) {
proxenet_plugin_set_state(plugin, INACTIVE);
xlog_ruby(LOG_ERROR, "Failed to init %s in %s\n", CFG_REQUEST_PLUGIN_FUNCTION, plugin->name);
return -1;
}
if (proxenet_ruby_initialize_function(plugin, RESPONSE) < 0) {
proxenet_plugin_set_state(plugin, INACTIVE);
xlog_ruby(LOG_ERROR, "Failed to init %s in %s\n", CFG_RESPONSE_PLUGIN_FUNCTION, plugin->name);
return -1;
}
if (proxenet_ruby_initialize_function(plugin, ONLOAD) < 0) {
xlog_ruby(LOG_ERROR, "An error occured during initialization of %s in %s\n", CFG_ONLOAD_PLUGIN_FUNCTION, plugin->name);
}
if (proxenet_ruby_initialize_function(plugin, ONLEAVE) < 0) {
xlog_ruby(LOG_ERROR, "An error occured during initialization of %s in %s\n", CFG_ONLEAVE_PLUGIN_FUNCTION, plugin->name);
}
/* Calling the on_load() function */
if(plugin->onload_function){
args.rVM = (VALUE)proxenet_ruby_get_module_reference(plugin->name);
args.rFunc = (ID)plugin->onload_function;
rRet = rb_protect( proxenet_ruby_safe_funcall2, (VALUE)&args, &res );
if (res || !rRet){
proxenet_ruby_print_last_exception();
}
}
return 0;
}
/**
* Cleanly disable the plugin, call the onleave() function (if any) and unallocate the plugin
* structures.
*/
int proxenet_ruby_destroy_plugin(plugin_t* plugin)
{
int res;
VALUE rRet;
struct proxenet_ruby_args args;
proxenet_plugin_set_state(plugin, INACTIVE);
/* Calling the on_leave() function */
if(plugin->onleave_function){
args.rVM = (VALUE)proxenet_ruby_get_module_reference(plugin->name);
args.rFunc = (ID)plugin->onleave_function;
rRet = rb_protect( proxenet_ruby_safe_funcall2, (VALUE)&args, &res );
if (res || !rRet){
proxenet_ruby_print_last_exception();
}
}
plugin->pre_function = NULL;
plugin->post_function = NULL;
plugin->onload_function = NULL;
plugin->onleave_function = NULL;
return 0;
}
/**
*
*/
static char* proxenet_ruby_execute_function(VALUE module, ID rFunc, request_t* request)
{
char *buf, *data;
int buflen, i, state;
VALUE rRet;
char *uri;
struct proxenet_ruby_args args;
uri = request->http_infos.uri;
if (!uri)
return NULL;
/* build args */
args.rVM = module;
args.rFunc = rFunc;
args.rArgs[0] = INT2NUM(request->id);
args.rArgs[1] = rb_str_new(request->data, request->size);
args.rArgs[2] = rb_str_new2(uri);
for(i=0; i<3; i++) {
rb_gc_register_address(&args.rArgs[i]);
}
/* safe function call */
rRet = rb_protect(proxenet_ruby_safe_plugin_funcall2, (VALUE)&args, &state);
if (state){
proxenet_ruby_print_last_exception();
data = NULL;
goto call_end;
}
if (!rRet) {
xlog_ruby(LOG_ERROR, "%s\n", "funcall2() failed");
data = NULL;
goto call_end;
}
rb_check_type(rRet, T_STRING);
/* copy result to exploitable buffer */
buf = RSTRING_PTR(rRet);
buflen = RSTRING_LEN(rRet);
data = proxenet_xmalloc(buflen + 1);
data = memcpy(data, buf, buflen);
request->data = data;
request->size = buflen;
call_end:
for(i=0; i<3; i++) {
rb_gc_unregister_address(&args.rArgs[i]);
}
return data;
}
/**
* Lock Ruby VM
*/
static inline void proxenet_ruby_lock_vm(interpreter_t *interpreter)
{
pthread_mutex_lock(&interpreter->mutex);
}
/**
* Unlock Ruby VM
*/
static inline void proxenet_ruby_unlock_vm(interpreter_t *interpreter)
{
pthread_mutex_unlock(&interpreter->mutex);
}
/**
*
*/
char* proxenet_ruby_plugin(plugin_t* plugin, request_t* request)
{
char *buf = NULL;
interpreter_t *interpreter = plugin->interpreter;;
VALUE module;
ID rFunc;
module = proxenet_ruby_get_module_reference(plugin->name);
if(!module)
return buf;
switch(request->type){
case REQUEST:
rFunc = (ID) plugin->pre_function;
break;
case RESPONSE:
rFunc = (ID) plugin->post_function;
break;
default:
abort();
}
proxenet_ruby_lock_vm(interpreter);
buf = proxenet_ruby_execute_function(module, rFunc, request);
proxenet_ruby_unlock_vm(interpreter);
return buf;
}
#endif /* _RUBY_PLUGIN */