ruby-changes:7945
From: ko1 <ko1@a...>
Date: Tue, 23 Sep 2008 10:10:18 +0900 (JST)
Subject: [ruby-changes:7945] Ruby:r19467 (trunk): * eval_safe.c, safe.c: rename eval_safe.c to safe.c.
ko1 2008-09-23 10:10:02 +0900 (Tue, 23 Sep 2008) New Revision: 19467 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19467 Log: * eval_safe.c, safe.c: rename eval_safe.c to safe.c. * common.mk, eval.c, safe.c, inits.c: separate safe.c from eval.c and make Init_safe(). Added files: trunk/safe.c Removed files: trunk/eval_safe.c Modified files: trunk/ChangeLog trunk/common.mk trunk/eval.c trunk/inits.c Index: eval_safe.c =================================================================== --- eval_safe.c (revision 19466) +++ eval_safe.c (revision 19467) @@ -1,111 +0,0 @@ -/* -*-c-*- */ -/* - * This file is included by eval.c - */ - -/* safe-level: - 0 - strings from streams/environment/ARGV are tainted (default) - 1 - no dangerous operation by tainted value - 2 - process/file operations prohibited - 3 - all generated objects are tainted - 4 - no global (non-tainted) variable modification/no direct output -*/ - -#define SAFE_LEVEL_MAX 4 - -/* $SAFE accessor */ - -int -rb_safe_level(void) -{ - return GET_THREAD()->safe_level; -} - -void -rb_set_safe_level_force(int safe) -{ - GET_THREAD()->safe_level = safe; -} - -void -rb_set_safe_level(int level) -{ - rb_thread_t *th = GET_THREAD(); - - if (level > th->safe_level) { - if (level > SAFE_LEVEL_MAX) { - level = SAFE_LEVEL_MAX; - } - th->safe_level = level; - } -} - -static VALUE -safe_getter(void) -{ - return INT2NUM(rb_safe_level()); -} - -static void -safe_setter(VALUE val) -{ - int level = NUM2INT(val); - rb_thread_t *th = GET_THREAD(); - - if (level < th->safe_level) { - rb_raise(rb_eSecurityError, - "tried to downgrade safe level from %d to %d", - th->safe_level, level); - } - if (level > SAFE_LEVEL_MAX) { - level = SAFE_LEVEL_MAX; - } - th->safe_level = level; -} - -void -rb_secure(int level) -{ - if (level <= rb_safe_level()) { - if (rb_frame_callee()) { - rb_raise(rb_eSecurityError, "Insecure operation `%s' at level %d", - rb_id2name(rb_frame_callee()), rb_safe_level()); - } - else { - rb_raise(rb_eSecurityError, "Insecure operation at level %d", - rb_safe_level()); - } - } -} - -void -rb_secure_update(VALUE obj) -{ - if (!OBJ_TAINTED(obj)) - rb_secure(4); -} - -void -rb_check_safe_obj(VALUE x) -{ - if (rb_safe_level() > 0 && OBJ_TAINTED(x)) { - if (rb_frame_callee()) { - rb_raise(rb_eSecurityError, "Insecure operation - %s", - rb_id2name(rb_frame_callee())); - } - else { - rb_raise(rb_eSecurityError, "Insecure operation: -r"); - } - } - rb_secure(4); -} - -void -rb_check_safe_str(VALUE x) -{ - rb_check_safe_obj(x); - if (TYPE(x) != T_STRING) { - rb_raise(rb_eTypeError, "wrong argument type %s (expected String)", - rb_obj_classname(x)); - } -} Index: ChangeLog =================================================================== --- ChangeLog (revision 19466) +++ ChangeLog (revision 19467) @@ -1,3 +1,10 @@ +Tue Sep 23 09:52:07 2008 Koichi Sasada <ko1@a...> + + * eval_safe.c, safe.c: rename eval_safe.c to safe.c. + + * common.mk, eval.c, safe.c, inits.c: separate safe.c from eval.c and + make Init_safe(). + Tue Sep 23 09:20:00 2008 Koichi Sasada <ko1@a...> * common.mk: clean up Index: common.mk =================================================================== --- common.mk (revision 19466) +++ common.mk (revision 19467) @@ -60,6 +60,7 @@ regparse.$(OBJEXT) \ regsyntax.$(OBJEXT) \ ruby.$(OBJEXT) \ + safe.$(OBJEXT) \ signal.$(OBJEXT) \ sprintf.$(OBJEXT) \ st.$(OBJEXT) \ @@ -463,7 +464,7 @@ error.$(OBJEXT): {$(VPATH)}error.c $(RUBY_H_INCLUDES) $(VM_CORE_H_INCLUDES) eval.$(OBJEXT): {$(VPATH)}eval.c {$(VPATH)}eval_intern.h \ $(RUBY_H_INCLUDES) {$(VPATH)}util.h $(VM_CORE_H_INCLUDES) \ - {$(VPATH)}eval_error.c {$(VPATH)}eval_safe.c {$(VPATH)}eval_jump.c + {$(VPATH)}eval_error.c {$(VPATH)}eval_jump.c load.$(OBJEXT): {$(VPATH)}load.c {$(VPATH)}eval_intern.h \ {$(VPATH)}util.h $(RUBY_H_INCLUDES) $(VM_CORE_H_INCLUDES) file.$(OBJEXT): {$(VPATH)}file.c $(RUBY_H_INCLUDES) {$(VPATH)}io.h \ @@ -514,6 +515,7 @@ ruby.$(OBJEXT): {$(VPATH)}ruby.c $(RUBY_H_INCLUDES) {$(VPATH)}util.h \ $(ENCODING_H_INCLDUES) {$(VPATH)}eval_intern.h \ $(VM_CORE_H_INCLUDES) {$(VPATH)}dln.h +safe.$(OBJEXT): {$(VPATH)}safe.c $(RUBY_H_INCLUDES) $(VM_CORE_H_INCLUDES) signal.$(OBJEXT): {$(VPATH)}signal.c $(RUBY_H_INCLUDES) $(VM_CORE_H_INCLUDES) sprintf.$(OBJEXT): {$(VPATH)}sprintf.c $(RUBY_H_INCLUDES) {$(VPATH)}re.h \ {$(VPATH)}regex.h {$(VPATH)}vsnprintf.c $(ENCODING_H_INCLDUES) Index: eval.c =================================================================== --- eval.c (revision 19466) +++ eval.c (revision 19467) @@ -24,7 +24,6 @@ #define exception_error GET_VM()->special_exceptions[ruby_error_reenter] #include "eval_error.c" -#include "eval_safe.c" #include "eval_jump.c" /* initialize ruby */ @@ -1170,8 +1169,6 @@ rb_define_global_function("trace_var", rb_f_trace_var, -1); /* in variable.c */ rb_define_global_function("untrace_var", rb_f_untrace_var, -1); /* in variable.c */ - rb_define_virtual_variable("$SAFE", safe_getter, safe_setter); - exception_error = rb_exc_new3(rb_eFatal, rb_obj_freeze(rb_str_new2("exception reentered"))); rb_ivar_set(exception_error, idThrowState, INT2FIX(TAG_FATAL)); Index: inits.c =================================================================== --- inits.c (revision 19466) +++ inits.c (revision 19467) @@ -23,6 +23,7 @@ void Init_Exception(void); void Init_syserr(void); void Init_eval(void); +void Init_safe(void); void Init_load(void); void Init_Proc(void); void Init_File(void); @@ -68,6 +69,7 @@ Init_String(); Init_Exception(); Init_eval(); + Init_safe(); Init_jump(); Init_Numeric(); Init_Bignum(); Index: safe.c =================================================================== --- safe.c (revision 0) +++ safe.c (revision 19467) @@ -0,0 +1,126 @@ +/********************************************************************** + + eval.c - + + $Author$ + created at: Tue Sep 23 09:44:32 JST 2008 + + Copyright (C) 2008 Yukihiro Matsumoto + +**********************************************************************/ + +/* safe-level: + 0 - strings from streams/environment/ARGV are tainted (default) + 1 - no dangerous operation by tainted value + 2 - process/file operations prohibited + 3 - all generated objects are tainted + 4 - no global (non-tainted) variable modification/no direct output +*/ + +#define SAFE_LEVEL_MAX 4 + +#include "ruby/ruby.h" +#include "vm_core.h" + +/* $SAFE accessor */ + +int +rb_safe_level(void) +{ + return GET_THREAD()->safe_level; +} + +void +rb_set_safe_level_force(int safe) +{ + GET_THREAD()->safe_level = safe; +} + +void +rb_set_safe_level(int level) +{ + rb_thread_t *th = GET_THREAD(); + + if (level > th->safe_level) { + if (level > SAFE_LEVEL_MAX) { + level = SAFE_LEVEL_MAX; + } + th->safe_level = level; + } +} + +static VALUE +safe_getter(void) +{ + return INT2NUM(rb_safe_level()); +} + +static void +safe_setter(VALUE val) +{ + int level = NUM2INT(val); + rb_thread_t *th = GET_THREAD(); + + if (level < th->safe_level) { + rb_raise(rb_eSecurityError, + "tried to downgrade safe level from %d to %d", + th->safe_level, level); + } + if (level > SAFE_LEVEL_MAX) { + level = SAFE_LEVEL_MAX; + } + th->safe_level = level; +} + +void +rb_secure(int level) +{ + if (level <= rb_safe_level()) { + if (rb_frame_callee()) { + rb_raise(rb_eSecurityError, "Insecure operation `%s' at level %d", + rb_id2name(rb_frame_callee()), rb_safe_level()); + } + else { + rb_raise(rb_eSecurityError, "Insecure operation at level %d", + rb_safe_level()); + } + } +} + +void +rb_secure_update(VALUE obj) +{ + if (!OBJ_TAINTED(obj)) + rb_secure(4); +} + +void +rb_check_safe_obj(VALUE x) +{ + if (rb_safe_level() > 0 && OBJ_TAINTED(x)) { + if (rb_frame_callee()) { + rb_raise(rb_eSecurityError, "Insecure operation - %s", + rb_id2name(rb_frame_callee())); + } + else { + rb_raise(rb_eSecurityError, "Insecure operation: -r"); + } + } + rb_secure(4); +} + +void +rb_check_safe_str(VALUE x) +{ + rb_check_safe_obj(x); + if (TYPE(x) != T_STRING) { + rb_raise(rb_eTypeError, "wrong argument type %s (expected String)", + rb_obj_classname(x)); + } +} + +void +Init_safe(void) +{ + rb_define_virtual_variable("$SAFE", safe_getter, safe_setter); +} Property changes on: safe.c ___________________________________________________________________ Name: svn:eol-style + LF Name: svn:keywords + Author Date Id Revision -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/