Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 110 lines (96 sloc) 2.048 kB
9c1d230 committing experimental branch content
Laurent Sansonetti authored
1 /* -*-c-*- */
2 /*
3 * This file is included by eval.c
4 */
5
6 /* safe-level:
7 0 - strings from streams/environment/ARGV are tainted (default)
8 1 - no dangerous operation by tainted value
9 2 - process/file operations prohibited
10 3 - all generated objects are tainted
11 4 - no global (non-tainted) variable modification/no direct output
12 */
13
14 #define SAFE_LEVEL_MAX 4
15
16 /* $SAFE accessor */
17
18 int
19 rb_safe_level(void)
20 {
21 return rb_vm_safe_level();
22 }
23
24 void
25 rb_set_safe_level_force(int safe)
26 {
27 rb_vm_set_safe_level(safe);
28 }
29
30 void
31 rb_set_safe_level(int level)
32 {
33 if (level > rb_vm_safe_level()) {
34 if (level > SAFE_LEVEL_MAX) {
35 level = SAFE_LEVEL_MAX;
36 }
37 rb_vm_set_safe_level(level);
38 }
39 }
40
41 static VALUE
42 safe_getter(void)
43 {
44 return INT2NUM(rb_safe_level());
45 }
46
47 static void
48 safe_setter(VALUE val)
49 {
50 int level = NUM2INT(val);
51 int current_level = rb_vm_safe_level();
52
53 if (level < current_level) {
54 rb_raise(rb_eSecurityError,
55 "tried to downgrade safe level from %d to %d",
56 current_level, level);
57 }
58 if (level > SAFE_LEVEL_MAX) {
59 level = SAFE_LEVEL_MAX;
60 }
61 rb_vm_set_safe_level(level);
62 }
63
64 void
65 rb_secure(int level)
66 {
67 if (level <= rb_safe_level()) {
68 if (rb_frame_callee()) {
69 rb_raise(rb_eSecurityError, "Insecure operation `%s' at level %d",
70 rb_id2name(rb_frame_callee()), rb_safe_level());
71 }
72 else {
73 rb_raise(rb_eSecurityError, "Insecure operation at level %d",
74 rb_safe_level());
75 }
76 }
77 }
78
79 void
80 rb_secure_update(VALUE obj)
81 {
82 if (!OBJ_TAINTED(obj))
83 rb_secure(4);
84 }
85
86 void
87 rb_check_safe_obj(VALUE x)
88 {
89 if (rb_safe_level() > 0 && OBJ_TAINTED(x)) {
90 if (rb_frame_callee()) {
91 rb_raise(rb_eSecurityError, "Insecure operation - %s",
92 rb_id2name(rb_frame_callee()));
93 }
94 else {
95 rb_raise(rb_eSecurityError, "Insecure operation: -r");
96 }
97 }
98 rb_secure(4);
99 }
100
101 void
102 rb_check_safe_str(VALUE x)
103 {
104 rb_check_safe_obj(x);
105 if (TYPE(x) != T_STRING) {
106 rb_raise(rb_eTypeError, "wrong argument type %s (expected String)",
107 rb_obj_classname(x));
108 }
109 }
Something went wrong with that request. Please try again.