GHSL-2020-125: integer signedness mismatch vulnerability in FreeRDP leads to OOB read - CVE-2020-4032
An integer signedness mismatch in FreeRDP's update_recv_secondary_order function causes a negative difference to be stored in an unsigned size_t, leading to an extremely large value passed to Stream_Seek. This moves the stream pointer to an invalid address, resulting in an out-of-bounds read.
Originally disclosed on GitHub Security Lab
An integer casting vulnerability has been detected in updaterecvsecondaryorder in orders.c.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
FreeRDP is an open source implementation of the Remote Desktop Protocol (RDP). The advisory concerns the parsing of secondary drawing orders on the client side, specifically the function update_recv_secondary_order in libfreerdp/core/orders.c. Secondary orders are the mechanism RDP uses to send drawing primitives such as glyph cache updates and other cached drawing operations to the client. The function reads the order header, dispatches to the handler for the specific order type, and then checks whether the handler consumed the expected number of bytes from the stream.
The bug is an integer signedness mismatch: a signed subtraction result is stored in an unsigned size_t, and the resulting huge value is then used to seek the stream pointer. The advisory classifies the impact as an out-of-bounds read and assigns CVE-2020-4032.
Setup
The issue was found in the development version of FreeRDP, master branch, as of 27 May 2020. Two runtime conditions are required for the vulnerable path to be reached: glyph-cache must be enabled and relax-order-checks must be enabled. The advisory does not describe any further tooling, build flags or test harness.
Finding it
The researcher looked at the post-processing that update_recv_secondary_order performs after a secondary order handler returns. The function computes start - end and stores the result in a size_t variable diff:
/* libfreerdp/core/orders.c */
...
diff = start - end; // [1]
if (diff > 0)
{
WLog_Print(update->log, WLOG_DEBUG,
"SECONDARY_ORDER %s: read %" PRIuz "bytes short, skipping", name, diff);
Stream_Seek(s, diff); // [2]
}
return rc;
...
The problem is the type of diff. It is an unsigned integer type, but start - end is a signed integer arithmetic expression that can return a negative value. When such a negative value is assigned to the unsigned size_t diff variable, it becomes a very large positive value at [1], up to and including SIZE_MAX. Consequently, Stream_Seek is called with an extremely large diff value, moving the s stream pointer to an invalid address at [2].
The if (diff > 0) guard does not help, because after the conversion the value is unsigned and therefore always greater than zero whenever the subtraction was negative.
Exploitation
Step 1: Reach the secondary order parser
A malicious or compromised RDP server sends a secondary drawing order to a connecting client. The client must have glyph-cache enabled and relax-order-checks enabled for the vulnerable code path to be exercised. The advisory does not include a packet capture or a concrete order payload.
Step 2: Make the handler consume more bytes than were available
Assigned to size_t diff, that negative difference wraps to a value close to SIZE_MAX.
Step 3: The oversized seek
Because diff > 0 is true for the wrapped value, Stream_Seek(s, diff) is called with the huge offset. The stream pointer s is moved to an invalid address.
Root cause
The subtraction start - end is performed in signed arithmetic and can be negative, but the destination variable diff is unsigned. The implicit conversion turns a small negative number into a value near SIZE_MAX, and the subsequent if (diff > 0) check cannot distinguish the wrapped value from a legitimate positive shortfall. The seek then trusts that value. The advisory does not describe any earlier validation that would have rejected the inconsistent stream positions, and the relax-order-checks option is one of the preconditions for reaching this state.
Impact
The advisory states that this issue may lead to an out-of-bounds read. It does not describe data disclosure, code execution or any other consequence, and it does not state which data could be read or how far past the buffer the pointer lands. The affected party is a FreeRDP client connecting to a server that sends the crafted secondary order, with glyph-cache and relax-order-checks enabled.
Fix
The vendor fixed the bug and released a patch on 22 June 2020. The advisory links the fix commit [redacted] and the repository security advisory GHSA-3898-mc89-x2vc. The advisory does not spell out the exact code change; the correction implied by the analysis is to keep the difference in a signed type (or otherwise reject a negative difference) before it is used as a seek offset.
Variants to test
- Other order handlers in the same file that compute a consumed-byte count and feed it to
Stream_Seek. A positive result is a handler whose return path can produce a negative difference that is stored in an unsigned variable. - Any place where a signed subtraction is assigned to
size_tand then compared with> 0. A positive result is a comparison that is always true after conversion. - Length or offset fields taken from the wire and used directly as seek or skip amounts. A positive result is a seek that is not bounded by the remaining stream length.
- Code paths guarded by relax-order-checks or similar leniency options. A positive result is a validation that is skipped only when the option is enabled, leaving the parser to trust inconsistent positions.
Hunting notes
This class of bug hides in protocol parsers that track a read cursor and reconcile it after dispatching to per-message handlers, especially where the reconciliation uses unsigned types for what is conceptually a signed delta. During review, look for size_t variables assigned from subtractions of two positions, and for > 0 checks on unsigned values. The advisory traces the wrapped value into Stream_Seek; no exploit output is included.
Coordinated disclosure timeline
- 27/05/2020: Report sent to vendor
- 27/05/2020: Vendor acknowledges report
- 22/06/2020: Bug fixed and patch released by the vendor
The issue was discovered and reported by GHSL team member @antonio-morales (Antonio Morales). The advisory is tracked as GHSL-2020-125, and the GHSL team can be contacted at [redacted] with that identifier in any communication about the issue.
What happened, in plain words
FreeRDP is a free, open source program that lets your computer act as a remote desktop client. It speaks a language called RDP (Remote Desktop Protocol), which is the same language Microsoft's Remote Desktop uses. When you connect to a remote computer with FreeRDP, the remote computer sends your program a stream of instructions about what to draw on your screen: text, images, and cached pieces of drawing that get reused. Those drawing instructions are called orders, and a particular group of them is called secondary orders.
A security researcher looked at the part of FreeRDP that reads these secondary orders and found a counting mistake. The program tries to work out how many bytes of an incoming message a handler left unread, by subtracting one position from another. The result of that subtraction can be a negative number, but the program stores it in a kind of variable that cannot hold negative numbers. A negative number put into such a variable turns into a gigantic positive number. The program then uses that gigantic number to jump ahead in the message, which sends its reading position far outside the actual data. The advisory calls this an out-of-bounds read and tracks it as CVE-2020-4032.
This is a beginner's walkthrough of the advisory. Every technical term is explained where it first appears, and the small piece of code from the report is explained line by line.
Context
What FreeRDP and RDP are
RDP, the Remote Desktop Protocol, is a set of rules two computers follow so that one can show the other's screen and pass along keyboard and mouse input. Think of it like a very detailed phone conversation: one side describes what the screen should look like, the other side draws it. FreeRDP is an open source implementation of that protocol, meaning its source code is public and anyone can read it, use it or improve it.
The advisory concerns the client side of FreeRDP, the program running on your computer that connects out to a remote machine. The specific function involved is update_recv_secondary_order, which lives in the file libfreerdp/core/orders.c. A function is a named block of code that does one job; you can think of it as a recipe with a name.
What secondary orders are
Secondary orders are the mechanism RDP uses to send drawing primitives such as glyph cache updates and other cached drawing operations to the client. A glyph is a single drawn character, like the letter A. A glyph cache is a small store of already-drawn characters that the client keeps so it does not have to redraw the same letter over and over; the server can say "use cached glyph number 12" instead of sending the shape again. A drawing primitive is a basic drawing instruction, such as "draw this shape here".
So a secondary order is a message from the server saying something like "here is an update to your cache of drawn characters". The client has to read that message carefully, because it arrives as a stream of bytes and the client must know exactly where each part starts and ends.
What the function does
The function reads the order header, dispatches to the handler for the specific order type, and then checks whether the handler consumed the expected number of bytes from the stream. A header is the short introductory part of a message that says what kind of message it is. To dispatch means to hand the message off to the piece of code that knows how to deal with that particular kind. A handler is that piece of code. A stream, in programming, is data that is read in sequence, one position after another, like reading a book with your finger moving along the line. The current position of that finger is called the stream position or cursor.
After the handler finishes, the function wants to know: did the handler read exactly as many bytes as the message contained, or did it stop short? If it stopped short, the function skips the leftover bytes so the next message can be read from the right place. That skipping is where the bug lives.
What the bug class means
The bug is an integer signedness mismatch: a signed subtraction result is stored in an unsigned size_t, and the resulting huge value is then used to seek the stream pointer. The advisory classifies the impact as an out-of-bounds read and assigns CVE-2020-4032.
Let us unpack that. An integer is a whole number, like 5, 0 or -3. Signed means the number is allowed to be negative. Unsigned means it is not: only zero and positive values are allowed. size_t is a standard type in the C programming language used for sizes and counts; it is unsigned, because a size can never sensibly be negative. A mismatch between signed and unsigned is a classic source of bugs, because converting a negative signed number into an unsigned type does not produce an error. It silently produces a huge positive number instead.
To seek a stream means to move its reading position to a new place, like moving your finger to a different line in the book. An out-of-bounds read means reading data from outside the area the program was supposed to read from, like reaching past the edge of a page and touching whatever is on the desk. CVE stands for Common Vulnerabilities and Exposures; it is a public catalogue number given to a known security flaw so people can refer to it unambiguously. CVE-2020-4032 is the number assigned to this one.
Setup
The issue was found in the development version of FreeRDP, master branch, as of 27 May 2020. The master branch is the main line of development where the newest, not-yet-released code lives. Two runtime conditions are required for the vulnerable path to be reached: glyph-cache must be enabled and relax-order-checks must be enabled.
A runtime condition is a setting that has to be true while the program is running, as opposed to something baked into the code. Glyph-cache being enabled means the client is actually using the cache of drawn characters described earlier. Relax-order-checks being enabled means the client is configured to be less strict about checking the orders it receives. The name says it plainly: the checks are relaxed. That leniency is what allows the program to reach the inconsistent state that triggers the bug.
The advisory does not describe any further tooling, build flags or test harness. In other words, it does not say which editor, compiler options or testing setup the researcher used.
Finding it
The researcher looked at the post-processing that update_recv_secondary_order performs after a secondary order handler returns. Post-processing means the cleanup and bookkeeping done after the main work. The function subtracts end from start and stores the result in a size_t variable called diff:
/* libfreerdp/core/orders.c */
...
diff = start - end; // [1]
if (diff > 0)
{
WLog_Print(update->log, WLOG_DEBUG,
"SECONDARY_ORDER %s: read %" PRIuz "bytes short, skipping", name, diff);
Stream_Seek(s, diff); // [2]
}
return rc;
...
Here is what each line does, in plain words:
/* libfreerdp/core/orders.c */is a comment naming the file this code comes from. Comments are notes for humans; the computer ignores them....means some code has been left out of the excerpt.diff = start - end; // [1]subtracts the position after parsing (end) from the position before parsing (start) and stores the answer in a variable calleddiff. The// [1]is a marker the advisory uses to point at this line later.if (diff > 0)asks whetherdiffis greater than zero. The intent is: only do the skipping if there are leftover bytes.WLog_Print(update->log, WLOG_DEBUG, ...)writes a debug message to a log. A log is a running record of what a program did, useful for troubleshooting.WLOG_DEBUGmarks the message as debug-level detail."SECONDARY_ORDER %s: read %" PRIuz "bytes short, skipping"is the text of that log message.%sis a placeholder for a name, andPRIuzis a standard way of formatting asize_tvalue. The message is meant to say something like "read N bytes short, skipping".name, diffare the values that fill those placeholders: the order's name and the byte count.Stream_Seek(s, diff); // [2]moves the streamsforward bydiffbytes. The// [2]marker points at this line.return rc;hands back a result code,rc, to whoever called the function.
The problem is the type of diff. It is an unsigned integer type, but start - end is a signed integer arithmetic expression that can return a negative value. When such a negative value is assigned to the unsigned size_t diff variable, it becomes a very large positive value at [1], up to and including SIZE_MAX. Consequently, Stream_Seek is called with an extremely large diff value, moving the s stream pointer to an invalid address at [2].
SIZE_MAX is the largest number a size_t can hold. Think of an unsigned counter like a car odometer that cannot show a minus sign: if you try to go one kilometre below zero, it rolls around to the highest number it can display. In the same way, a small negative number stored in an unsigned variable wraps around to a value near the top of the range.
The if (diff > 0) guard does not help, because after the conversion the value is unsigned and therefore always greater than zero whenever the subtraction was negative. In other words, the safety check was written as if diff could still be negative, but by the time the check runs, that information has already been lost.
Exploitation
Step 1: Reach the secondary order parser
A malicious or compromised RDP server sends a secondary drawing order to a connecting client. The client must have glyph-cache enabled and relax-order-checks enabled for the vulnerable code path to be exercised. The advisory does not include a packet capture or a concrete order payload.
A malicious server is one set up deliberately to send harmful messages; a compromised server is a legitimate one that an attacker has taken over. Either way, the attacker controls what arrives at the client. A packet capture is a recording of network traffic, and a payload is the actual data carried inside a message. The advisory does not provide either, so we know the shape of the attack but not a ready-made example message.
Step 2: Make the handler consume more bytes than were available
Assigned to size_t diff, that negative difference wraps to a value close to SIZE_MAX.
To advance the stream means to move its reading position forward. Normally a handler reads exactly the bytes belonging to its message and stops. The subtraction then goes negative, and the wrap-around described above turns it into a number near the maximum.
Step 3: The oversized seek
Because diff > 0 is true for the wrapped value, Stream_Seek(s, diff) is called with the huge offset. The stream pointer s is moved to an invalid address.
An offset is a distance from a starting point. The program believes it is skipping a few leftover bytes, but it is actually jumping an enormous distance. The next time it reads from the stream, it reads from a place it was never meant to touch, which is the out-of-bounds read.
Root cause
The subtraction start - end is performed in signed arithmetic and can be negative, but the destination variable diff is unsigned. The implicit conversion turns a small negative number into a value near SIZE_MAX, and the subsequent if (diff > 0) check cannot distinguish the wrapped value from a legitimate positive shortfall. The seek then trusts that value. The advisory does not describe any earlier validation that would have rejected the inconsistent stream positions, and the relax-order-checks option is one of the preconditions for reaching this state.
An implicit conversion is a type change the computer performs automatically, without being asked. A legitimate positive shortfall is the normal, harmless case: the handler really did stop a few bytes early, and those bytes really should be skipped. The check cannot tell that case apart from the wrapped negative one, because both look like positive numbers once stored in an unsigned variable.
Impact
The advisory states that this issue may lead to an out-of-bounds read. It does not describe data disclosure, code execution or any other consequence, and it does not state which data could be read or how far past the buffer the pointer lands. The affected party is a FreeRDP client connecting to a server that sends the crafted secondary order, with glyph-cache and relax-order-checks enabled.
Data disclosure means leaking information to an attacker, and code execution means getting the attacker's code to run on the victim's machine. The advisory does not claim either. A buffer is a block of memory set aside for data; reading past its edge is the out-of-bounds read itself. The person at risk is the user of the FreeRDP client, not the server.
Fix
The vendor fixed the bug and released a patch on 22 June 2020. The vendor is the organisation that maintains the software. A patch is an update that corrects the flaw. The advisory links the fix commit [redacted] and the repository security advisory GHSA-3898-mc89-x2vc. A commit is a recorded change in a source code repository, identified by a long string of characters. The advisory does not spell out the exact code change; the correction implied by the analysis is to keep the difference in a signed type (or otherwise reject a negative difference) before it is used as a seek offset.
In everyday terms: the fix is to keep the number in a container that can hold negative values, or to refuse to continue when the number is negative, so the huge wrapped value never reaches the seek.
Variants to test
If you were reviewing code like this yourself, these are the places worth checking. Each item names a pattern and what a positive result looks like.
- Other order handlers in the same file that compute a consumed-byte count and feed it to
Stream_Seek. A positive result is a handler whose return path can produce a negative difference that is stored in an unsigned variable. - Any place where a signed subtraction is assigned to
size_tand then compared with> 0. A positive result is a comparison that is always true after conversion. - Length or offset fields taken from the wire and used directly as seek or skip amounts. A positive result is a seek that is not bounded by the remaining stream length. "Taken from the wire" means the value came from the network rather than from the program itself, so an attacker can choose it.
- Code paths guarded by relax-order-checks or similar leniency options. A positive result is a validation that is skipped only when the option is enabled, leaving the parser to trust inconsistent positions.
Hunting notes
This class of bug hides in protocol parsers that track a read cursor and reconcile it after dispatching to per-message handlers, especially where the reconciliation uses unsigned types for what is conceptually a signed delta. A parser is the part of a program that takes apart an incoming message and makes sense of its pieces. To reconcile the cursor means to bring it back into line after a handler has moved it. A delta is simply a difference between two values.
During review, look for size_t variables assigned from subtractions of two positions, and for > 0 checks on unsigned values. The advisory traces the wrapped value into Stream_Seek; no exploit output is included. In other words, the reasoning alone was convincing: the researcher showed how the bad number is produced and where it is used, without needing to demonstrate a live attack.
Coordinated disclosure timeline
Coordinated disclosure is an arrangement where a researcher tells the vendor privately first, giving them time to fix the problem before the details become public. The timeline was:
- 27/05/2020: Report sent to vendor
- 27/05/2020: Vendor acknowledges report
- 22/06/2020: Bug fixed and patch released by the vendor
The issue was discovered and reported by GHSL team member @antonio-morales (Antonio Morales). GHSL is the GitHub Security Lab, the team that published this advisory. The advisory is tracked as GHSL-2020-125, and the GHSL team can be contacted at [redacted] with that identifier in any communication about the issue.
Glossary
- Advisory: a public write-up of a security flaw, usually published by the team that found or coordinated it.
- Buffer: a block of memory set aside to hold data.
- Client: the program on your computer that connects out to a remote machine.
- Commit: a recorded change in a source code repository, identified by a long string of characters.
- Coordinated disclosure: telling a vendor privately first, then publishing details after a fix is ready.
- CVE: Common Vulnerabilities and Exposures, a public catalogue number for a known security flaw.
- Data disclosure: leaking information to someone who should not have it.
- Delta: the difference between two values.
- Dispatch: handing a message off to the piece of code that knows how to handle that kind.
- Drawing primitive: a basic drawing instruction, such as drawing a shape at a position.
- Function: a named block of code that performs one job.
- Glyph: a single drawn character, such as a letter.
- Glyph cache: a store of already-drawn characters kept so they do not have to be redrawn.
- Handler: the piece of code that deals with one particular kind of message.
- Header: the short introductory part of a message that says what kind it is.
- Implicit conversion: an automatic change of a value's type performed by the computer without being asked.
- Integer: a whole number, such as 5, 0 or -3.
- Log: a running record of what a program did, used for troubleshooting.
- Master branch: the main line of development holding the newest, not-yet-released code.
- Offset: a distance from a starting point.
- Out-of-bounds read: reading data from outside the area the program was supposed to read from.
- Packet capture: a recording of network traffic.
- Parser: the part of a program that takes apart an incoming message and makes sense of its pieces.
- Patch: an update that corrects a flaw in software.
- Payload: the actual data carried inside a message.
- Post-processing: cleanup and bookkeeping done after the main work.
- RDP: Remote Desktop Protocol, the rules two computers follow so one can show the other's screen and pass input.
- Runtime condition: a setting that must be true while the program is running.
- Secondary order: an RDP message carrying drawing instructions such as glyph cache updates.
- Seek: to move a stream's reading position to a new place.
- Signed: a number type that is allowed to hold negative values.
size_t: a standard C type for sizes and counts; it is unsigned.SIZE_MAX: the largest value asize_tcan hold.- Stream: data read in sequence, one position after another.
- Stream position (cursor): the current place in a stream where reading will happen next.
- Unsigned: a number type that can hold only zero and positive values.
- Vendor: the organisation that maintains the software.
- Wire: the network; a value "taken from the wire" came from the network and can be chosen by an attacker.