Ruby 2.1.5p273(2014-11-13revision48405)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

safe.c
Go to the documentation of this file.
1 /**********************************************************************
2
3 safe.c -
4
5 $Author: nobu $
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 2 - process/file operations prohibited
16 3 - all generated objects are tainted
17 */
18
19 #define SAFE_LEVEL_MAX RUBY_SAFE_LEVEL_MAX
20
21 #include "ruby/ruby.h"
22 #include "vm_core.h"
23
24 /* $SAFE accessor */
25
26 #undef rb_secure
27 #undef rb_set_safe_level
28 #undef ruby_safe_level_4_warning
29
30 int
31 ruby_safe_level_4_warning(void)
32 {
33 return 4;
34 }
35
36 int
37 rb_safe_level(void)
38 {
39 return GET_THREAD()->safe_level;
40 }
41
42 void
43 rb_set_safe_level_force(int safe)
44 {
45 GET_THREAD()->safe_level = safe;
46 }
47
48 void
49 rb_set_safe_level(int level)
50 {
51 rb_thread_t *th = GET_THREAD();
52
53 if (level > th->safe_level) {
54 if (level > SAFE_LEVEL_MAX) {
55 rb_raise(rb_eArgError, "$SAFE=4 is obsolete");
56 }
57 th->safe_level = level;
58 }
59 }
60
61 static VALUE
62 safe_getter(void)
63 {
64 return INT2NUM(rb_safe_level());
65 }
66
67 static void
68 safe_setter(VALUE val)
69 {
70 int level = NUM2INT(val);
71 rb_thread_t *th = GET_THREAD();
72
73 if (level < th->safe_level) {
74 rb_raise(rb_eSecurityError,
75 "tried to downgrade safe level from %d to %d",
76 th->safe_level, level);
77 }
78 if (level == 3) {
79 rb_warning("$SAFE=3 does no sandboxing");
80 }
81 if (level > SAFE_LEVEL_MAX) {
82 rb_raise(rb_eArgError, "$SAFE=4 is obsolete");
83 }
84 th->safe_level = level;
85 }
86
87 void
88 rb_secure(int level)
89 {
90 if (level <= rb_safe_level()) {
91 ID caller_name = rb_frame_callee();
92 if (caller_name) {
93 rb_raise(rb_eSecurityError, "Insecure operation `%s' at level %d",
94 rb_id2name(caller_name), rb_safe_level());
95 }
96 else {
97 rb_raise(rb_eSecurityError, "Insecure operation at level %d",
98 rb_safe_level());
99 }
100 }
101 }
102
103 void
104 rb_secure_update(VALUE obj)
105 {
106 }
107
108 void
109 rb_insecure_operation(void)
110 {
111 ID caller_name = rb_frame_callee();
112 if (caller_name) {
113 rb_raise(rb_eSecurityError, "Insecure operation - %s",
114 rb_id2name(caller_name));
115 }
116 else {
117 rb_raise(rb_eSecurityError, "Insecure operation: -r");
118 }
119 }
120
121 void
122 rb_check_safe_obj(VALUE x)
123 {
124 if (rb_safe_level() > 0 && OBJ_TAINTED(x)) {
125 rb_insecure_operation();
126 }
127 }
128
129 void
130 rb_check_safe_str(VALUE x)
131 {
132 rb_check_safe_obj(x);
133 if (!RB_TYPE_P(x, T_STRING)) {
134 rb_raise(rb_eTypeError, "wrong argument type %s (expected String)",
135 rb_obj_classname(x));
136 }
137 }
138
139 void
140 Init_safe(void)
141 {
142 rb_define_virtual_variable("$SAFE", safe_getter, safe_setter);
143 }
rb_check_safe_obj
void rb_check_safe_obj(VALUE x)
Definition: safe.c:122
INT2NUM
#define INT2NUM(x)
Definition: ruby.h:1288
rb_define_virtual_variable
void rb_define_virtual_variable(const char *, VALUE(*)(ANYARGS), void(*)(ANYARGS))
Definition: variable.c:616
NUM2INT
#define NUM2INT(x)
Definition: ruby.h:630
ruby.h
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:548
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
rb_secure_update
void rb_secure_update(VALUE obj)
Definition: safe.c:104
rb_set_safe_level_force
void rb_set_safe_level_force(int safe)
Definition: safe.c:43
rb_eSecurityError
VALUE rb_eSecurityError
Definition: error.c:557
safe_getter
static VALUE safe_getter(void)
Definition: safe.c:62
rb_thread_struct
Definition: vm_core.h:524
OBJ_TAINTED
#define OBJ_TAINTED(x)
Definition: ruby.h:1176
rb_obj_classname
const char * rb_obj_classname(VALUE)
Definition: variable.c:406
Init_safe
void Init_safe(void)
Definition: safe.c:140
RB_TYPE_P
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1664
level
#define level
ruby_safe_level_4_warning
int ruby_safe_level_4_warning(void)
Definition: safe.c:31
SAFE_LEVEL_MAX
#define SAFE_LEVEL_MAX
Definition: safe.c:19
ID
unsigned long ID
Definition: ruby.h:89
vm_core.h
VALUE
unsigned long VALUE
Definition: ruby.h:88
rb_insecure_operation
void rb_insecure_operation(void)
Definition: safe.c:109
rb_frame_callee
ID rb_frame_callee(void)
Definition: eval.c:949
rb_safe_level
int rb_safe_level(void)
Definition: safe.c:37
safe_setter
static void safe_setter(VALUE val)
Definition: safe.c:68
T_STRING
#define T_STRING
Definition: ruby.h:482
rb_check_safe_str
void rb_check_safe_str(VALUE x)
Definition: safe.c:130
rb_set_safe_level
void rb_set_safe_level(int level)
Definition: safe.c:49
rb_id2name
const char * rb_id2name(ID id)
Definition: ripper.c:17230
rb_thread_struct::safe_level
int safe_level
Definition: vm_core.h:532
rb_warning
void rb_warning(const char *fmt,...)
Definition: error.c:236
rb_secure
void rb_secure(int level)
Definition: safe.c:88
GET_THREAD
static rb_thread_t * GET_THREAD(void)
Definition: vm_core.h:927
rb_eArgError
VALUE rb_eArgError
Definition: error.c:549