ruby-changes:58804
From: Jeremy <ko1@a...>
Date: Mon, 18 Nov 2019 08:00:55 +0900 (JST)
Subject: [ruby-changes:58804] c5c05460ac (master): Warn on access/modify of $SAFE, and remove effects of modifying $SAFE
https://git.ruby-lang.org/ruby.git/commit/?id=c5c05460ac From c5c05460ac20abcbc0ed686eb4acf06da7a39a79 Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Fri, 20 Sep 2019 19:06:22 -0700 Subject: Warn on access/modify of $SAFE, and remove effects of modifying $SAFE This removes the security features added by $SAFE = 1, and warns for access or modification of $SAFE from Ruby-level, as well as warning when calling all public C functions related to $SAFE. This modifies some internal functions that took a safe level argument to no longer take the argument. rb_require_safe now warns, rb_require_string has been added as a version that takes a VALUE and does not warn. One public C function that still takes a safe level argument and that this doesn't warn for is rb_eval_cmd. We may want to consider adding an alternative method that does not take a safe level argument, and warn for rb_eval_cmd. diff --git a/dir.c b/dir.c index 5221490..cefb5e7 100644 --- a/dir.c +++ b/dir.c @@ -2719,7 +2719,6 @@ rb_push_glob(VALUE str, VALUE base, int flags) /* '\0' is delimiter */ https://github.com/ruby/ruby/blob/trunk/dir.c#L2719 rb_raise(rb_eArgError, "nul-separated glob pattern is deprecated"); } else { - rb_check_safe_obj(str); rb_enc_check(str, rb_enc_from_encoding(rb_usascii_encoding())); } ary = rb_ary_new(); diff --git a/encoding.c b/encoding.c index c136908..b000e0f 100644 --- a/encoding.c +++ b/encoding.c @@ -654,7 +654,7 @@ load_encoding(const char *name) https://github.com/ruby/ruby/blob/trunk/encoding.c#L654 ruby_verbose = Qfalse; ruby_debug = Qfalse; errinfo = rb_errinfo(); - loaded = rb_require_internal(enclib, rb_safe_level()); + loaded = rb_require_internal(enclib); ruby_verbose = verbose; ruby_debug = debug; rb_set_errinfo(errinfo); diff --git a/error.c b/error.c index 7a88ccb..6ce49e0 100644 --- a/error.c +++ b/error.c @@ -2985,12 +2985,6 @@ rb_check_copyable(VALUE obj, VALUE orig) https://github.com/ruby/ruby/blob/trunk/error.c#L2985 if (!FL_ABLE(obj)) return; rb_check_frozen_internal(obj); if (!FL_ABLE(orig)) return; - if ((~RBASIC(obj)->flags & RBASIC(orig)->flags) & FL_TAINT) { - if (rb_safe_level() > 0) { - rb_raise(rb_eSecurityError, "Insecure: can't modify %"PRIsVALUE, - RBASIC(obj)->klass); - } - } } void diff --git a/eval.c b/eval.c index 64149d2..77b0efa 100644 --- a/eval.c +++ b/eval.c @@ -204,7 +204,6 @@ rb_ec_cleanup(rb_execution_context_t *ec, volatile int ex) https://github.com/ruby/ruby/blob/trunk/eval.c#L204 th = th0; errs[1] = ec->errinfo; if (THROW_DATA_P(ec->errinfo)) ec->errinfo = Qnil; - rb_set_safe_level_force(0); ruby_init_stack(&errs[STACK_UPPER(errs, 0, 1)]); SAVE_ROOT_JMPBUF(th, rb_ec_teardown(ec)); diff --git a/ext/etc/etc.c b/ext/etc/etc.c index 28761df..1bb10e0 100644 --- a/ext/etc/etc.c +++ b/ext/etc/etc.c @@ -219,7 +219,6 @@ etc_getpwnam(VALUE obj, VALUE nam) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L219 struct passwd *pwd; const char *p = StringValueCStr(nam); - rb_check_safe_obj(nam); pwd = getpwnam(p); if (pwd == 0) rb_raise(rb_eArgError, "can't find user for %"PRIsVALUE, nam); return setup_passwd(pwd); @@ -463,7 +462,6 @@ etc_getgrnam(VALUE obj, VALUE nam) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L462 struct group *grp; const char *p = StringValueCStr(nam); - rb_check_safe_obj(nam); grp = getgrnam(p); if (grp == 0) rb_raise(rb_eArgError, "can't find group for %"PRIsVALUE, nam); return setup_group(grp); diff --git a/ext/io/console/console.c b/ext/io/console/console.c index 4f04709..42b000f 100644 --- a/ext/io/console/console.c +++ b/ext/io/console/console.c @@ -1483,7 +1483,6 @@ prompt(int argc, VALUE *argv, VALUE io) https://github.com/ruby/ruby/blob/trunk/ext/io/console/console.c#L1483 if (argc > 0 && !NIL_P(argv[0])) { VALUE str = argv[0]; StringValueCStr(str); - rb_check_safe_obj(str); rb_io_write(io, str); } } diff --git a/ext/openssl/ossl_rand.c b/ext/openssl/ossl_rand.c index c958570..4a4f9dd 100644 --- a/ext/openssl/ossl_rand.c +++ b/ext/openssl/ossl_rand.c @@ -67,8 +67,6 @@ ossl_rand_add(VALUE self, VALUE str, VALUE entropy) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_rand.c#L67 static VALUE ossl_rand_load_file(VALUE self, VALUE filename) { - rb_check_safe_obj(filename); - if(!RAND_load_file(StringValueCStr(filename), -1)) { ossl_raise(eRandomError, NULL); } @@ -86,8 +84,6 @@ ossl_rand_load_file(VALUE self, VALUE filename) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_rand.c#L84 static VALUE ossl_rand_write_file(VALUE self, VALUE filename) { - rb_check_safe_obj(filename); - if (RAND_write_file(StringValueCStr(filename)) == -1) { ossl_raise(eRandomError, NULL); } @@ -164,8 +160,6 @@ ossl_rand_pseudo_bytes(VALUE self, VALUE len) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_rand.c#L160 static VALUE ossl_rand_egd(VALUE self, VALUE filename) { - rb_check_safe_obj(filename); - if (RAND_egd(StringValueCStr(filename)) == -1) { ossl_raise(eRandomError, NULL); } @@ -186,8 +180,6 @@ ossl_rand_egd_bytes(VALUE self, VALUE filename, VALUE len) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_rand.c#L180 { int n = NUM2INT(len); - rb_check_safe_obj(filename); - if (RAND_egd_bytes(StringValueCStr(filename), n) == -1) { ossl_raise(eRandomError, NULL); } diff --git a/ext/openssl/ossl_x509store.c b/ext/openssl/ossl_x509store.c index 2909eed..61543d4 100644 --- a/ext/openssl/ossl_x509store.c +++ b/ext/openssl/ossl_x509store.c @@ -304,7 +304,6 @@ ossl_x509store_add_file(VALUE self, VALUE file) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_x509store.c#L304 char *path = NULL; if(file != Qnil){ - rb_check_safe_obj(file); path = StringValueCStr(file); } GetX509Store(self, store); @@ -340,7 +339,6 @@ ossl_x509store_add_path(VALUE self, VALUE dir) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_x509store.c#L339 char *path = NULL; if(dir != Qnil){ - rb_check_safe_obj(dir); path = StringValueCStr(dir); } GetX509Store(self, store); diff --git a/ext/readline/readline.c b/ext/readline/readline.c index b686f99..646be2b 100644 --- a/ext/readline/readline.c +++ b/ext/readline/readline.c @@ -95,7 +95,6 @@ static char **readline_attempted_completion_function(const char *text, https://github.com/ruby/ruby/blob/trunk/ext/readline/readline.c#L95 #define OutputStringValue(str) do {\ StringValueCStr(str);\ - rb_check_safe_obj(str);\ (str) = rb_str_conv_enc((str), rb_enc_get(str), rb_locale_encoding());\ } while (0)\ diff --git a/ext/socket/constants.c b/ext/socket/constants.c index 6fc8627..1bbb53b 100644 --- a/ext/socket/constants.c +++ b/ext/socket/constants.c @@ -28,7 +28,6 @@ constant_arg(VALUE arg, int (*str_to_int)(const char*, long, int*), const char * https://github.com/ruby/ruby/blob/trunk/ext/socket/constants.c#L28 else if (!NIL_P(tmp = rb_check_string_type(arg))) { arg = tmp; str: - rb_check_safe_obj(arg); ptr = RSTRING_PTR(arg); if (str_to_int(ptr, RSTRING_LEN(arg), &ret) == -1) rb_raise(rb_eSocket, "%s: %s", errmsg, ptr); diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c index 0499b84..cc29674 100644 --- a/ext/socket/raddrinfo.c +++ b/ext/socket/raddrinfo.c @@ -503,10 +503,6 @@ str_is_number(const char *p) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L503 #define str_equal(ptr, len, name) \ ((ptr)[0] == name[0] && \ rb_strlen_lit(name) == (len) && memcmp(ptr, name, len) == 0) -#define SafeStringValueCStr(v) do {\ - StringValueCStr(v);\ - rb_check_safe_obj(v);\ -} while(0) static char* host_str(VALUE host, char *hbuf, size_t hbuflen, int *flags_ptr) @@ -525,7 +521,7 @@ host_str(VALUE host, char *hbuf, size_t hbuflen, int *flags_ptr) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L521 const char *name; size_t len; - SafeStringValueCStr(host); + StringValueCStr(host); RSTRING_GETMEM(host, name, len); if (!len || str_equal(name, len, "<any>")) { make_inetaddr(INADDR_ANY, hbuf, hbuflen); @@ -564,7 +560,7 @@ port_str(VALUE port, char *pbuf, size_t pbuflen, int *flags_ptr) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L560 const char *serv; size_t len; - SafeStringValueCStr(port); + StringValueCStr(port); RSTRING_GETMEM(port, serv, len); if (len >= pbuflen) { rb_raise(rb_eArgError, "service name too long (%"PRIuSIZE")", diff --git a/ext/socket/unixsocket.c b/ext/socket/unixsocket.c index 8bdfc84..0c3a01d 100644 --- a/ext/socket/unixsocket.c +++ b/ext/socket/unixsocket.c @@ -39,7 +39,6 @@ unixsock_path_value(VALUE path) https://github.com/ruby/ruby/blob/trunk/ext/socket/unixsocket.c#L39 #endif if (isstr) { if (RSTRING_LEN(name) == 0 || RSTRING_PTR(name)[0] == '\0') { - rb_check_safe_obj(name); return name; /* ignore encoding */ } } diff --git a/ext/syslog/syslog.c b/ext/syslog/syslog.c index 23dcf6c..4c540fc 100644 --- a/ext/syslog/syslog.c +++ b/ext/syslog/syslog.c @@ -162,7 +162,6 @@ static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/syslog/syslog.c#L162 ident = rb_gv_get("$0"); } ident_ptr = StringValueCStr(ident); - rb_check_safe_obj(ident); syslog_ident = strdup(ident_ptr); if (NIL_P(opt)) { diff --git a/ext/win32ole/win32ole.c b/ext/win32ole/win32ole.c index c46d393..f20bfc8 100644 --- a/ext/win32ole/win32ole.c +++ b/ext/win32ole/win32ole.c @@ -1985,10 +1985,6 @@ fole_s_connect(int argc, VALUE *argv, VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/win32ole/win32ole.c#L1985 rb (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/