Issue #13954 has been updated by nagachika (Tomoyuki Chikanaga).


ruby_2_4 r61456 merged revision(s) 60071.

----------------------------------------
Bug #13954: Etc.getpwnam, getgrnam が SHARABLE_MIDDLE_SUBSTRING=1 を考慮していない
https://bugs.ruby-lang.org/issues/13954#change-68630

* Author: tommy (Masahiro Tomita)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.0dev (2017-09-29 trunk 60062) [x86_64-linux]
* Backport: 2.3: REQUIRED, 2.4: DONE
----------------------------------------
SHARABLE_MIDDLE_SUBSTRING=1 でコンパイルしたRubyで Etc.getpwnam, getgrnam が共有元の文字列終端まで使用してしまいます。

```
% sudo useradd abcdefghijklmnopqrstuvwxyz
% ruby -retc -e 'p Etc.getpwnam("abcdefghijklmnopqrstuvwxyz")'           
#<struct Etc::Passwd name="abcdefghijklmnopqrstuvwxyz", passwd="x", uid=1002, gid=1002, gecos="", dir="/home/abcdefghijklmnopqrstuvwxyz", shell="">
% ruby -retc -e 'p Etc.getpwnam(("abcdefghijklmnopqrstuvwxyz"*10)[0,26])'
Traceback (most recent call last):
	1: from -e:1:in `<main>'
-e:1:in `getpwnam': can't find user for abcdefghijklmnopqrstuvwxyz (ArgumentError)
```

次のパッチで直ると思います。

```diff
diff --git a/ext/etc/etc.c b/ext/etc/etc.c
index 5d964ba518..66fd26f436 100644
--- a/ext/etc/etc.c
+++ b/ext/etc/etc.c
@@ -217,6 +217,7 @@ etc_getpwnam(VALUE obj, VALUE nam)
     struct passwd *pwd;
 
     SafeStringValue(nam);
+    StringValueCStr(nam);
     pwd = getpwnam(RSTRING_PTR(nam));
     if (pwd == 0) rb_raise(rb_eArgError, "can't find user for %"PRIsVALUE, nam);
     return setup_passwd(pwd);
@@ -460,6 +461,7 @@ etc_getgrnam(VALUE obj, VALUE nam)
     struct group *grp;
 
     SafeStringValue(nam);
+    StringValueCStr(nam);
     grp = getgrnam(RSTRING_PTR(nam));
     if (grp == 0) rb_raise(rb_eArgError, "can't find group for %"PRIsVALUE, nam);
     return setup_group(grp);
```



-- 
https://bugs.ruby-lang.org/