Ruby  2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
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 #define SAFE_LEVEL_MAX RUBY_SAFE_LEVEL_MAX
13 
14 #include "ruby/ruby.h"
15 #include "vm_core.h"
16 
17 /* $SAFE accessor */
18 
19 #undef rb_secure
20 #undef rb_set_safe_level
21 #undef ruby_safe_level_2_warning
22 
23 int
25 {
26  rb_warn("rb_safe_level_2_warning will be removed in Ruby 3.0");
27  return 2;
28 }
29 
30 int
32 {
33  rb_warn("rb_safe_level will be removed in Ruby 3.0");
34  return GET_VM()->safe_level_;
35 }
36 
37 void
39 {
40  rb_warn("rb_set_safe_level_force will be removed in Ruby 3.0");
41  GET_VM()->safe_level_ = safe;
42 }
43 
44 void
46 {
47  rb_vm_t *vm = GET_VM();
48 
49  rb_warn("rb_set_safe_level will be removed in Ruby 3.0");
50  if (level > SAFE_LEVEL_MAX) {
51  rb_raise(rb_eArgError, "$SAFE=2 to 4 are obsolete");
52  }
53  else if (level < 0) {
54  rb_raise(rb_eArgError, "$SAFE should be >= 0");
55  }
56  else {
57  int line;
58  const char *path = rb_source_location_cstr(&line);
59 
60  if (0) fprintf(stderr, "%s:%d $SAFE %d -> %d\n",
61  path ? path : "-", line, vm->safe_level_, level);
62 
63  vm->safe_level_ = level;
64  }
65 }
66 
67 static VALUE
68 safe_getter(ID _x, VALUE *_y)
69 {
70  rb_warn("$SAFE will become a normal global variable in Ruby 3.0");
71  return INT2NUM(GET_VM()->safe_level_);
72 }
73 
74 static void
75 safe_setter(VALUE val, ID _x, VALUE *_y)
76 {
77  int level = NUM2INT(val);
78  rb_vm_t *vm = GET_VM();
79 
80  rb_warn("$SAFE will become a normal global variable in Ruby 3.0");
81  if (level > SAFE_LEVEL_MAX) {
82  rb_raise(rb_eArgError, "$SAFE=2 to 4 are obsolete");
83  }
84  else if (level < 0) {
85  rb_raise(rb_eArgError, "$SAFE should be >= 0");
86  }
87  else {
88  int line;
89  const char *path = rb_source_location_cstr(&line);
90 
91  if (0) fprintf(stderr, "%s:%d $SAFE %d -> %d\n",
92  path ? path : "-", line, vm->safe_level_, level);
93 
94  vm->safe_level_ = level;
95  }
96 }
97 
98 void
99 rb_secure(int level)
100 {
101  rb_warn("rb_secure will be removed in Ruby 3.0");
102  if (level <= GET_VM()->safe_level_) {
103  ID caller_name = rb_frame_callee();
104  if (caller_name) {
105  rb_raise(rb_eSecurityError, "Insecure operation `%"PRIsVALUE"' at level %d",
106  rb_id2str(caller_name), GET_VM()->safe_level_);
107  }
108  else {
109  rb_raise(rb_eSecurityError, "Insecure operation at level %d",
110  GET_VM()->safe_level_);
111  }
112  }
113 }
114 
115 void
117 {
118  rb_warn("rb_secure_update will be removed in Ruby 3.0");
119 }
120 
121 void
123 {
124  rb_warn("rb_insecure_operation will be removed in Ruby 3.0");
125  ID caller_name = rb_frame_callee();
126  if (caller_name) {
127  rb_raise(rb_eSecurityError, "Insecure operation - %"PRIsVALUE,
128  rb_id2str(caller_name));
129  }
130  else {
131  rb_raise(rb_eSecurityError, "Insecure operation: -r");
132  }
133 }
134 
135 void
137 {
138  rb_warn("rb_check_safe_obj will be removed in Ruby 3.0");
139 }
140 
141 void
143 {
144  rb_define_virtual_variable("$SAFE", safe_getter, safe_setter);
145 }
ID
unsigned long ID
Definition: ruby.h:103
obj
const VALUE VALUE obj
Definition: rb_mjit_min_header-2.7.1.h:5742
ruby_safe_level_2_warning
int ruby_safe_level_2_warning(void)
Definition: safe.c:24
SAFE_LEVEL_MAX
#define SAFE_LEVEL_MAX
Definition: safe.c:12
rb_secure_update
void rb_secure_update(VALUE obj)
Definition: safe.c:116
rb_set_safe_level_force
void rb_set_safe_level_force(int safe)
Definition: safe.c:38
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:313
rb_set_safe_level
void rb_set_safe_level(int level)
Definition: safe.c:45
rb_safe_level
int rb_safe_level(void)
Definition: safe.c:31
Init_safe
void Init_safe(void)
Definition: safe.c:142
VALUE
unsigned long VALUE
Definition: ruby.h:102
GET_VM
#define GET_VM()
Definition: vm_core.h:1764
rb_eArgError
VALUE rb_eArgError
Definition: error.c:923
INT2NUM
#define INT2NUM(x)
Definition: ruby.h:1609
rb_check_safe_obj
void rb_check_safe_obj(VALUE x)
Definition: safe.c:136
rb_id2str
#define rb_id2str(id)
Definition: vm_backtrace.c:30
PRIsVALUE
#define PRIsVALUE
Definition: ruby.h:166
ruby.h
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2669
rb_insecure_operation
void rb_insecure_operation(void)
Definition: safe.c:122
vm_core.h
rb_source_location_cstr
const char * rb_source_location_cstr(int *pline)
Definition: vm.c:1376
path
VALUE path
Definition: rb_mjit_min_header-2.7.1.h:7353
rb_frame_callee
ID rb_frame_callee(void)
The name of the current method.
Definition: eval.c:1200
rb_eSecurityError
VALUE rb_eSecurityError
Definition: error.c:931
rb_vm_struct::safe_level_
unsigned int safe_level_
Definition: vm_core.h:606
rb_secure
void rb_secure(int level)
Definition: safe.c:99
rb_vm_struct
Definition: vm_core.h:576
stderr
#define stderr
Definition: rb_mjit_min_header-2.7.1.h:1485
ruby::backward::cxxanyargs::rb_define_virtual_variable
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
Definition: cxxanyargs.hpp:59
NUM2INT
#define NUM2INT(x)
Definition: ruby.h:715
fprintf
int fprintf(FILE *__restrict, const char *__restrict,...) __attribute__((__format__(__printf__