Ruby  2.5.0dev(2017-10-22revision60238)
safe.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  safe.c -
4 
5  $Author$
6  created at: Tue Sep 23 09:44:32 JST 2008
7 
8  Copyright (C) 2008 Yukihiro Matsumoto
9 
10 **********************************************************************/
11 
12 /* safe-level:
13  0 - strings from streams/environment/ARGV are tainted (default)
14  1 - no dangerous operation by tainted value
15 */
16 
17 #define SAFE_LEVEL_MAX RUBY_SAFE_LEVEL_MAX
18 
19 #include "ruby/ruby.h"
20 #include "vm_core.h"
21 
22 /* $SAFE accessor */
23 
24 #undef rb_secure
25 #undef rb_set_safe_level
26 #undef ruby_safe_level_2_warning
27 
28 int
30 {
31  return 2;
32 }
33 
34 int
36 {
37  return GET_THREAD()->ec.safe_level;
38 }
39 
40 void
42 {
43  GET_THREAD()->ec.safe_level = safe;
44 }
45 
46 void
48 {
49  rb_thread_t *th = GET_THREAD();
50 
51  if (level > th->ec.safe_level) {
52  if (level > SAFE_LEVEL_MAX) {
53  rb_raise(rb_eArgError, "$SAFE=2 to 4 are obsolete");
54  }
55  th->ec.safe_level = level;
56  }
57 }
58 
59 static VALUE
60 safe_getter(void)
61 {
62  return INT2NUM(rb_safe_level());
63 }
64 
65 static void
66 safe_setter(VALUE val)
67 {
68  int level = NUM2INT(val);
69  rb_thread_t *th = GET_THREAD();
70 
71  if (level < th->ec.safe_level) {
73  "tried to downgrade safe level from %d to %d",
74  th->ec.safe_level, level);
75  }
76  if (level > SAFE_LEVEL_MAX) {
77  rb_raise(rb_eArgError, "$SAFE=2 to 4 are obsolete");
78  }
79  th->ec.safe_level = level;
80 }
81 
82 void
84 {
85  if (level <= rb_safe_level()) {
86  ID caller_name = rb_frame_callee();
87  if (caller_name) {
88  rb_raise(rb_eSecurityError, "Insecure operation `%"PRIsVALUE"' at level %d",
89  rb_id2str(caller_name), rb_safe_level());
90  }
91  else {
92  rb_raise(rb_eSecurityError, "Insecure operation at level %d",
93  rb_safe_level());
94  }
95  }
96 }
97 
98 void
100 {
101 }
102 
103 void
105 {
106  ID caller_name = rb_frame_callee();
107  if (caller_name) {
108  rb_raise(rb_eSecurityError, "Insecure operation - %"PRIsVALUE,
109  rb_id2str(caller_name));
110  }
111  else {
112  rb_raise(rb_eSecurityError, "Insecure operation: -r");
113  }
114 }
115 
116 void
118 {
119  if (rb_safe_level() > 0 && OBJ_TAINTED(x)) {
121  }
122 }
123 
124 void
126 {
127  rb_define_virtual_variable("$SAFE", safe_getter, safe_setter);
128 }
void rb_check_safe_obj(VALUE x)
Definition: safe.c:117
#define INT2NUM(x)
Definition: ruby.h:1538
void rb_define_virtual_variable(const char *, VALUE(*)(ANYARGS), void(*)(ANYARGS))
Definition: variable.c:648
#define NUM2INT(x)
Definition: ruby.h:684
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2284
#define rb_id2str(id)
Definition: vm_backtrace.c:29
void rb_secure_update(VALUE obj)
Definition: safe.c:99
void rb_set_safe_level_force(int safe)
Definition: safe.c:41
void Init_safe(void)
Definition: safe.c:125
#define GET_THREAD()
Definition: vm_core.h:1583
VALUE rb_eArgError
Definition: error.c:802
#define level
#define SAFE_LEVEL_MAX
Definition: safe.c:17
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
unsigned long VALUE
Definition: ruby.h:85
#define OBJ_TAINTED(x)
Definition: ruby.h:1296
VALUE rb_eSecurityError
Definition: error.c:810
void rb_insecure_operation(void)
Definition: safe.c:104
ID rb_frame_callee(void)
The name of the current method.
Definition: eval.c:1120
int rb_safe_level(void)
Definition: safe.c:35
int ruby_safe_level_2_warning(void)
Definition: safe.c:29
void rb_set_safe_level(int level)
Definition: safe.c:47
rb_execution_context_t ec
Definition: vm_core.h:790
void rb_secure(int level)
Definition: safe.c:83