Skip to content
This repository
Newer
Older
100644 117 lines (103 sloc) 2.365 kb
95957256 » Laurent Sansonetti
2011-01-15 update copyrights to 2011
1 /*
2 * This file is covered by the Ruby license. See COPYING for more details.
3 *
4 * Copyright (C) 2007-2011, Apple Inc. All rights reserved.
5 * Copyright (C) 1993-2007 Yukihiro Matsumoto
6 * Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
7 * Copyright (C) 2000 Information-technology Promotion Agency, Japan
8 */
9
9c1d2307 » Laurent Sansonetti
2009-03-11 committing experimental branch content
10 /*
11 * This file is included by eval.c
12 */
13
14 /* safe-level:
15 0 - strings from streams/environment/ARGV are tainted (default)
16 1 - no dangerous operation by tainted value
17 2 - process/file operations prohibited
18 3 - all generated objects are tainted
19 4 - no global (non-tainted) variable modification/no direct output
20 */
21
22 #define SAFE_LEVEL_MAX 4
23
24 /* $SAFE accessor */
25
26 int
27 rb_safe_level(void)
28 {
29 return rb_vm_safe_level();
30 }
31
32 void
33 rb_set_safe_level_force(int safe)
34 {
35 rb_vm_set_safe_level(safe);
36 }
37
38 void
39 rb_set_safe_level(int level)
40 {
41 if (level > rb_vm_safe_level()) {
42 if (level > SAFE_LEVEL_MAX) {
43 level = SAFE_LEVEL_MAX;
44 }
45 rb_vm_set_safe_level(level);
46 }
47 }
48
49 static VALUE
50 safe_getter(void)
51 {
52 return INT2NUM(rb_safe_level());
53 }
54
55 static void
56 safe_setter(VALUE val)
57 {
58 int level = NUM2INT(val);
59 int current_level = rb_vm_safe_level();
60
61 if (level < current_level) {
62 rb_raise(rb_eSecurityError,
63 "tried to downgrade safe level from %d to %d",
64 current_level, level);
65 }
66 if (level > SAFE_LEVEL_MAX) {
67 level = SAFE_LEVEL_MAX;
68 }
69 rb_vm_set_safe_level(level);
70 }
71
72 void
73 rb_secure(int level)
74 {
75 if (level <= rb_safe_level()) {
76 if (rb_frame_callee()) {
77 rb_raise(rb_eSecurityError, "Insecure operation `%s' at level %d",
78 rb_id2name(rb_frame_callee()), rb_safe_level());
79 }
80 else {
81 rb_raise(rb_eSecurityError, "Insecure operation at level %d",
82 rb_safe_level());
83 }
84 }
85 }
86
87 void
88 rb_secure_update(VALUE obj)
89 {
90 if (!OBJ_TAINTED(obj))
91 rb_secure(4);
92 }
93
94 void
95 rb_check_safe_obj(VALUE x)
96 {
97 if (rb_safe_level() > 0 && OBJ_TAINTED(x)) {
98 if (rb_frame_callee()) {
99 rb_raise(rb_eSecurityError, "Insecure operation - %s",
100 rb_id2name(rb_frame_callee()));
101 }
102 else {
103 rb_raise(rb_eSecurityError, "Insecure operation: -r");
104 }
105 }
106 rb_secure(4);
107 }
108
109 void
110 rb_check_safe_str(VALUE x)
111 {
112 rb_check_safe_obj(x);
113 if (TYPE(x) != T_STRING) {
114 rb_raise(rb_eTypeError, "wrong argument type %s (expected String)",
115 rb_obj_classname(x));
116 }
117 }
Something went wrong with that request. Please try again.