Bug 1118158 - (CVE-2014-4975) CVE-2014-4975 ruby: off-by-one stack-based buffer overflow in the encodes() function
CVE-2014-4975 ruby: off-by-one stack-based buffer overflow in the encodes() f...
Status: CLOSED ERRATA
Product: Security Response
Classification: Other
Component: vulnerability (Show other bugs)
unspecified
All Linux
low Severity low
: ---
: ---
Assigned To: Red Hat Product Security
impact=low,public=20140709,reported=2...
: Reopened, Security
Depends On: 1120856 1163998 1164000 1164004 1164005 1542178 1542179
Blocks: 1118161 1157711
  Show dependency treegraph
 
Reported: 2014-07-10 02:13 EDT by Murray McAllister
Modified: 2018-02-05 13:52 EST (History)
62 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2015-08-07 09:21:14 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Murray McAllister 2014-07-10 02:13:53 EDT
A possible stack-based buffer overflow flaw was reported in the Ruby encodes() function from pack.c. From the bug report, this function may be used on data received from a server, and could be triggered remotely.

As the affected stack buffer is a static size on the stack, FORTIFY_SOURCE may help mitigate this issue to only be a denial of service.

The original report suggests older versions (such as 1.9.3) are not affected.

Bug report: https://bugs.ruby-lang.org/issues/10019

CVE request: http://www.openwall.com/lists/oss-security/2014/07/09/13
Comment 1 Ján Rusnačko 2014-07-10 06:45:07 EDT
The proposed patch is not OK, since len can be up to ULONG_MAX big. 

The best I could get is:

2.1.2-p168 :015 > ['1'*(2**32)].pack 'm4566666666666666664'
Killed
[jrusnack@griffin ruby]$ irb
2.1.2-p168 :001 > ['1'*(2**32)].pack 'm4566666666666666664'
NoMemoryError: failed to allocate memory
	from (irb):1:in `pack'
	from (irb):1
	from /home/jrusnack/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'

which can be expected. From the report I don`t see how one can get SEGFAULT. I also compiled ruby with address sanitizer and found nothing.
Comment 2 Tomas Hoger 2014-07-10 15:01:35 EDT
It is possible to trigger an off-by-one write, with '\n' character written past the end of the input buffer.  That happens when the encodes() function is called with len of 3070 or 3071.  That can happen in the following cases:

- the length of the input string is 3070 (or 3071) and count value for the m
  format directive is 3072 or more
- count value for the m format directive is exactly 3072 and the length of the
  input string is n*3072 - 2 (or -1)

I.e. crash can be possibly reproduced with:

- ["a"*3070].pack("m4000")
- ["a"*(3072*3-2)].pack("m3072")

Both cases require rather non-standard format specification that requests long output lines.  Hence this is unlikely to be triggered in applications that may encode untrusted input strings, but use trusted format specification.

Also considering the limited overflow (single byte that is not attacker controlled), this is unlikely to have code execution impact.  For ruby builds using stack protector (such as those in Red Hat Enterprise Linux or Fedora), the overflow is detected if it reaches canary value.

It is currently unclear if the issue described above is the problem reporter ran into.
Comment 4 Tomas Hoger 2014-07-17 17:23:25 EDT
CVE-2014-4975 was assigned to the off-by-one overflow in encodes() as described in comment 2 above and upstream bug:
https://bugs.ruby-lang.org/issues/10019#note-6

For CVE assignment, see:
http://seclists.org/oss-sec/2014/q3/173

The issue was fixed upstream in the following commit:
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=46778

As noted above, this overflow can only happen when using specific format specification for Ruby Array's pack() method:
http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-pack

Format "m" (base64 encoding) with length argument of at least 3072 is affected.  This is unlikely in cases when format string is from a trusted source, as the length value defines the maximum length of the output line.  After the specified number of characters, a line break is inserted.  Typical use case would limit line length to less than ~100 characters, the default is 60 characters if no length is specified.  The alternative is output with no line breaks at all, which can be requested using format "m0", and is unaffected by this issue.

This is not very likely to be exposed, and with limited impact on Ruby builds with stack protector.  Considering this a Low impact issue.
Comment 5 Tomas Hoger 2014-07-17 17:25:26 EDT
Statement:

This issue did not affect the versions of ruby as shipped with Red Hat Enterprise Linux 5 and 6.
Comment 6 Tomas Hoger 2014-07-17 17:26:16 EDT
Created ruby tracking bugs for this issue:

Affects: fedora-all [bug 1120856]
Comment 7 Fedora Update System 2014-11-10 01:32:21 EST
ruby-2.1.4-24.fc21 has been pushed to the Fedora 21 stable repository.  If problems still persist, please make note of it in this bug report.
Comment 11 errata-xmlrpc 2014-11-26 11:52:55 EST
This issue has been addressed in the following products:

  Red Hat Software Collections 1 for Red Hat Enterprise Linux 7
  Red Hat Software Collections 1 for Red Hat Enterprise Linux 6
  Red Hat Software Collections 1 for Red Hat Enterprise Linux 6.5 EUS
  Red Hat Software Collections 1 for Red Hat Enterprise Linux 6.4 EUS
  Red Hat Software Collections 1 for Red Hat Enterprise Linux 6.6 EUS

Via RHSA-2014:1914 https://rhn.redhat.com/errata/RHSA-2014-1914.html
Comment 12 errata-xmlrpc 2014-11-26 11:53:24 EST
This issue has been addressed in the following products:

  Red Hat Software Collections 1 for Red Hat Enterprise Linux 7
  Red Hat Software Collections 1 for Red Hat Enterprise Linux 6
  Red Hat Software Collections 1 for Red Hat Enterprise Linux 6.5 EUS
  Red Hat Software Collections 1 for Red Hat Enterprise Linux 6.4 EUS
  Red Hat Software Collections 1 for Red Hat Enterprise Linux 6.6 EUS

Via RHSA-2014:1913 https://rhn.redhat.com/errata/RHSA-2014-1913.html
Comment 13 errata-xmlrpc 2014-11-26 17:37:48 EST
This issue has been addressed in the following products:

  Red Hat Enterprise Linux 7

Via RHSA-2014:1912 https://rhn.redhat.com/errata/RHSA-2014-1912.html

Note You need to log in before you can comment on or make changes to this bug.