VLC : Integer overflow in vnc module <= 3.0.18 CVE-2022-41325
An integer overflow in VLC's VNC module allows a malicious VNC server to allocate a smaller memory buffer than expected, leading to a relative write-what-where primitive. This can crash VLC or potentially execute arbitrary code under certain conditions.
Published on synacktiv.com
Vulnerability description Presentation of VLC VLC media player (previously the VideoLAN Client and commonly known as simply VLC) is a free and open-source, portable, cross-platform media player software and streaming media server developed by the VideoLAN project. VLC can display a vnc video stream by using its URI…
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
VLC media player, previously the VideoLAN Client, is a free and open-source, portable, cross-platform media player and streaming media server developed by the VideoLAN project. Among its input modules it ships a VNC client, so VLC can display a VNC video stream directly from a URI:
vlc vnc://ip_address_of_server:port/
The vulnerability described here lives in that VNC module. The VNC support itself is provided through a third-party library, libvncClient, but the affected code is in VLC. An attacker who controls a VNC server can make VLC allocate a memory buffer shorter than expected, which gives a relative write-what-where primitive: VLC can be crashed, and arbitrary code execution is possible under conditions.
The issue is tracked as CVE-2022-41325, assigned by MITRE, with a CVSS score of 7.8. Versions [redacted] and earlier are affected.
Setup
The ./configure script tries to compile VNC support by default:
$ ./configure --help | grep vnc
--enable-vnc (VNC/rfb client support) [default=auto]
You can check whether a given VLC build has VNC support by listing its modules:
$ vlc --list | grep vnc
VLC media player [redacted] Vetinari (revision 3.0.13-8-g41878ff4f2)
vnc Accès client VNC
The attack requires the victim to open a vnc:// URI pointing at a server the attacker controls.
Finding it
The bug is in mallocFrameBufferHandler in modules/access/vnc.c. The function computes the framebuffer size from three values supplied by the VNC server and then allocates that many bytes:
static rfbBool mallocFrameBufferHandler( rfbClient* p_client )
{
(...)
/* Set up framebuffer */
p_sys->i_framebuffersize = i_width * i_height * i_depth / 8;
/* Reuse unsent block */
if ( p_sys->p_block )
p_sys->p_block = block_Realloc( p_sys->p_block, 0, p_sys->i_framebuffersize );
else
p_sys->p_block = block_Alloc( p_sys->i_framebuffersize );
(...)
}
The types involved are the key:
p_sys->i_framebuffersizeis anint.i_widthis anint, but the VNC protocol forces it by design to be auint16.i_heightis anint, also forced by the protocol to be auint16.i_depthcan be at most 32.
The calculation i_width * i_height * i_depth / 8 can overflow the int size of p_sys->i_framebuffersize, and the result can be truncated. The width, height and depth all come from the VNC server and are used without any verification.
Exploitation
Step 1: choose dimensions that truncate to a small value
Pick a screen size whose product overflows a 32-bit int and whose truncated result is small:
- width:
0x4004 - height:
0xfff1 - depth: 32 bpp
The calculation gives:
0x4004*0xfff1*32/8 = 0x10000ff10
which is truncated to 0xff10. block_Alloc() therefore allocates a 0xff10 byte buffer, far smaller than what is needed to store the data the VNC server will send.
The specific values matter because block_Alloc() checks that the requested size is smaller than 2^27. Any other values can work, as long as the int truncation produces a number below 2^27.
Step 2: write past the end of the buffer
A VNC server can update portions of the screen, from the entire buffer down to a small rectangle, or even individual pixels, by sending packets to VLC. Because the buffer is smaller than the screen, it is trivial to write any byte outside the allocated memory, anywhere from the start of the buffer up to buffer + 2^32 bytes.
For example, sending an RFB pixel coded on 4 bytes such as 0x41414141 at coordinates X=0x4000, Y=0x3 performs a write past the end of the buffer:
0x4000*3*4 = 0x30000
0x30000 lies after the boundary of the 0xff10 byte buffer that was allocated. This can result in unexpected behaviour.
Step 3: assess exploitability
In theory, command execution is possible by rewriting a chosen section of data. In practice, with stack randomisation, heap randomisation and a binary compiled with PIE, exploitation is very unlikely.
Root cause
The framebuffer size is computed as i_width * i_height * i_depth / 8, with all three operands taken from the VNC server and never validated. A width and height that are individually valid uint16 values can still produce a product that overflows a 32-bit int, and the truncated result is then passed straight to block_Alloc() or block_Realloc(). The only check in the allocation path is block_Alloc()'s limit that the size be below 2^27, which constrains the attacker's choice of dimensions but does not prevent the overflow. The result is a buffer sized for the truncated value while the server is free to address the full, much larger screen.
Impact
An attacker who controls the VNC server that a victim connects to can cause VLC to allocate a buffer shorter than expected and then write outside it, with a relative write-what-where primitive covering a range from the start of the buffer up to buffer + 2^32 bytes. This allows crashing VLC, and in theory arbitrary code execution by rewriting a chosen section of data. In practice, exploitation is very unlikely because of stack randomisation, heap randomisation and PIE. The attack requires the victim to open a vnc:// URI pointing at the attacker's server.
Fix
The VLC team fixed the vulnerability in commit [redacted]. The patch adds a check that the buffer size has not overflowed:
static rfbBool mallocFrameBufferHandler( rfbClient* p_client )
(...)
assert(!(p_client->width & ~0xffff)); // fits in 16 bits
uint16_t i_width = p_client->width;
assert(!(p_client->height & ~0xffff)); // fits in 16 bits
uint16_t i_height = p_client->height;
uint8_t i_bits_per_pixel = p_client->format.bitsPerPixel;
assert((i_bits_per_pixel & 0x7) == 0); // multiple of 8
(...)
/* Set up framebuffer */
if (mul_overflow(i_width, i_height * (i_bits_per_pixel / 8), &p_sys->i_framebuffersize))
{
msg_Err(p_demux, "VNC framebuffersize overflow");
return FALSE;
}
(...)
The mul_overflow() function explicitly checks that the multiplication will not overflow, and on overflow the handler logs an error and returns FALSE. Version 3.0.18, published on 2022-11-30, contains the fix.
Timeline
| Date | Action |
|---|---|
| 2022-09-19 | Ticket opened in VLC tracker |
| 2022-09-19 | Developers proposed a patch fixing the vulnerability |
| 2022-09-23 | CVE-2022-41325 assigned by MITRE |
| 2022-11-30 | Publication of 3.0.18 |
Variants to test
- Other integer-width mismatches in protocol-driven allocations. Look for size calculations that multiply server-supplied 16-bit fields (width, height, depth, stride) in a narrower type than the result needs. A positive result is a computed size that wraps to a small value while the server can still address the full logical buffer.
- Allocation-size caps as the only guard. Where a helper such as
block_Alloc()rejects sizes above a threshold, try dimension pairs whose truncated product falls just below that threshold, as with0x4004x0xfff1here. A positive result is a successful allocation of a buffer smaller than the screen it represents. - Reallocation paths. The same computation feeds
block_Realloc()when a block already exists, so test resize or reconnect flows as well as first allocation. A positive result is a reallocated buffer whose size does not match the advertised framebuffer. - Pixel-level writes. Once a short buffer exists, send single-pixel updates at coordinates near the end of the logical screen and observe whether writes land beyond the allocation. A positive result is memory corruption or a crash at an offset past the allocated size.
What happened, in plain words
VLC is a very popular free video player. Besides playing files on your computer, it can also connect to a remote screen-sharing server and show what is happening on that server's screen. The technology used for that is called VNC. You may have seen it in IT support tools: someone shares their screen and another person watches or controls it.
A security researcher found that when VLC connects to a VNC server, it trusts some numbers the server sends it. Those numbers describe the size of the remote screen (how many pixels wide, how many pixels high, and how many bits are used per pixel). VLC uses them to work out how much memory it needs to hold one picture of that screen. If the numbers are chosen cleverly, the arithmetic goes wrong and VLC reserves far less memory than it should. The server can then send screen updates that make VLC write data past the end of that small reserved area. That kind of mistake is called a memory corruption bug, and it can crash the player. In theory it can also let an attacker run their own code on the victim's machine, though in practice that is very unlikely here.
The bug is tracked as CVE-2022-41325, a public identifier given to security issues so people can refer to them unambiguously. It was assigned by MITRE, an organisation that helps run the CVE system. The CVSS score is 7.8, a standard way of rating how serious a vulnerability is. VLC versions [redacted] and earlier are affected, and version 3.0.18, published on 2022-11-30, contains the fix.
Background: what VLC and VNC are
VLC media player, previously called the VideoLAN Client and usually just VLC, is a free and open-source media player and streaming server made by the VideoLAN project. "Open source" means the program's source code is public, so anyone can read it, including security researchers.
VLC is built from many small pieces called modules. Each module handles one kind of input or output: playing an MP3, reading a DVD, showing a network stream, and so on. One of those modules is a VNC client. That means VLC can act as the viewer in a VNC session and display the remote screen as if it were a video.
You start it by giving VLC a URI, which is just an address in a standard format. Here the address starts with vnc:// instead of the http:// you see in a browser:
vlc vnc://ip_address_of_server:port/
Reading that line: vlc is the program, vnc:// tells it to use the VNC module, ip_address_of_server is the network address of the machine running the VNC server, and port is the numbered channel on that machine where the VNC service listens. The trailing slash is just part of the address format.
The VNC support inside VLC is not written from scratch. It uses a third-party library called libvncClient, which handles the low-level conversation with the server. However, the bug described here is not in that library: the faulty code is in VLC itself.
The problem in one paragraph
If an attacker controls the VNC server, they can make VLC reserve a memory buffer that is shorter than expected. A buffer is simply a block of memory set aside to hold data, like a row of numbered boxes. Once the buffer is too small, the attacker has what security people call a relative write-what-where primitive: they can choose, within a range, what data gets written and roughly where in memory it lands. That is enough to crash VLC, and under certain conditions to execute arbitrary code, meaning the attacker's own instructions run on the victim's computer.
Checking whether a VLC build supports VNC
Before anything else, the researcher checked that VNC support is normally compiled into VLC. The build script, called configure, lists its options when asked for help, and the output was filtered to show only lines mentioning vnc:
$ ./configure --help | grep vnc
--enable-vnc (VNC/rfb client support) [default=auto]
Line by line: ./configure runs the configuration script that prepares VLC for compilation. --help asks it to print its options instead of configuring anything. The vertical bar | takes the output of the left command and feeds it to the right one. grep vnc prints only the lines containing the text vnc. The result shows an option --enable-vnc with the note [default=auto], meaning the build system decides automatically whether to include VNC support.
The second check lists the modules a particular VLC build actually contains:
$ vlc --list | grep vnc
VLC media player [redacted] Vetinari (revision 3.0.13-8-g41878ff4f2)
vnc Accès client VNC
Here vlc --list asks the program to print its available modules, and grep vnc keeps only the matching line. The output confirms the build is version 3.0.17.4 (codename Vetinari) and that a module named vnc is present, described in French as "Accès client VNC", which means VNC client access. So the vulnerable code is reachable in this build.
The attack requires the victim to open a vnc:// address pointing at a server the attacker controls. The attacker does not need to break into the victim's machine first; they just need the victim to connect to their server.
Finding the bug in the code
The faulty function is called mallocFrameBufferHandler and lives in the file modules/access/vnc.c. Its job is to reserve memory for the framebuffer, which is the block of memory that holds the picture of the remote screen. Here is the relevant part:
static rfbBool mallocFrameBufferHandler( rfbClient* p_client )
{
(...)
/* Set up framebuffer */
p_sys->i_framebuffersize = i_width * i_height * i_depth / 8;
/* Reuse unsent block */
if ( p_sys->p_block )
p_sys->p_block = block_Realloc( p_sys->p_block, 0, p_sys->i_framebuffersize );
else
p_sys->p_block = block_Alloc( p_sys->i_framebuffersize );
(...)
}
Line by line: the function receives a pointer to a client structure, p_client, which holds the connection details. The (...) marks code left out of the excerpt. The line p_sys->i_framebuffersize = i_width * i_height * i_depth / 8; computes the buffer size: width times height gives the number of pixels, depth is the number of bits per pixel, and dividing by 8 converts bits to bytes, because one byte holds 8 bits. The result is stored in p_sys->i_framebuffersize. The if checks whether a block of memory already exists; if so, block_Realloc resizes it, otherwise block_Alloc creates a new one. Both take the computed size.
The important part is the data types, meaning how much room each variable has to store a number:
p_sys->i_framebuffersizeis anint, a whole number stored in a fixed amount of space, typically 32 bits.i_widthis anint, but the VNC protocol forces it by design to be auint16, an unsigned 16-bit number, so its maximum value is 65535.i_heightis anint, also forced by the protocol to be auint16.i_depthcan be at most 32.
Because the result is stored in an int, it has to fit in that fixed space. When a calculation produces a number too large for the type, it wraps around, like a car odometer rolling over from 99999 to 00000. That is called an integer overflow, and the wrapped result is said to be truncated. Here the truncated value is then used as the buffer size, so VLC reserves far less memory than the screen actually needs. The width, height and depth all come from the VNC server and are used without any verification.
Exploitation, step by step
Step 1: choose dimensions that truncate to a small value
The attacker picks a screen size whose product overflows a 32-bit int and whose truncated result is small:
- width:
0x4004 - height:
0xfff1 - depth: 32 bpp
The numbers are written in hexadecimal, a base-16 notation common in programming; the 0x prefix marks them as such. The calculation gives:
0x4004*0xfff1*32/8 = 0x10000ff10
That true result is larger than a 32-bit int can hold, so it wraps around and is truncated to 0xff10. block_Alloc() therefore reserves a 0xff10 byte buffer, far smaller than what is needed to store the data the server will send for a screen of that size.
The specific values matter because block_Alloc() checks that the size to allocate is smaller than 2^27. The attacker needs the truncated size to fall below that limit, otherwise the allocation fails and nothing happens. Any other values can work as long as the truncation produces a number below 2^27.
Step 2: write past the end of the buffer
A VNC server can update portions of the screen: the whole thing, a small rectangle, or even a single pixel, by sending packets to VLC. A packet is just a formatted message sent over the network. Because the buffer is smaller than the screen it is supposed to represent, it is easy to write data outside the reserved area, anywhere from the start of the buffer up to buffer + 2^32 bytes.
For example, sending an RFB pixel coded on 4 bytes such as 0x41414141 at coordinates X=0x4000, Y=0x3 performs a write past the end of the buffer. RFB is the protocol VNC uses to exchange screen updates. The offset is computed as:
0x4000*3*4 = 0x30000
That is, the horizontal position times the vertical position times the number of bytes per pixel. The result, 0x30000, lies after the boundary of the 0xff10 byte buffer that was allocated, so the write lands in memory that belongs to something else. This can result in unexpected behaviour, which is a polite way of saying crashes or worse.
Step 3: assess exploitability
In theory, command execution is possible by rewriting a chosen section of data. In practice, exploitation is very unlikely. Three protections get in the way: stack randomisation and heap randomisation, which shuffle the locations of memory areas each time the program runs so the attacker cannot predict where to write, and PIE, short for position-independent executable, which loads the program itself at a random address. Together they make it hard to turn a memory corruption into working code execution.
Root cause
The framebuffer size is computed as i_width * i_height * i_depth / 8, with all three operands taken from the VNC server and never validated. A width and height that are individually valid uint16 values can still produce a product that overflows a 32-bit int, and the truncated result is passed straight to block_Alloc() or block_Realloc(). The only check in the allocation path is block_Alloc()'s limit that the size be below 2^27, which constrains the attacker's choice of dimensions but does not prevent the overflow. The result is a buffer sized for the truncated value while the server is free to address the full, much larger screen.
Impact
An attacker who controls the VNC server that a victim connects to can cause VLC to allocate a buffer shorter than expected and then write outside it, with a relative write-what-where primitive covering a range from the start of the buffer up to buffer + 2^32 bytes. This allows crashing VLC, and in theory arbitrary code execution by rewriting a chosen section of data. In practice, exploitation is very unlikely because of stack randomisation, heap randomisation and PIE. The attack requires the victim to open a vnc:// URI pointing at the attacker's server.
The fix
The VLC team fixed the vulnerability in commit [redacted]. A commit is a recorded change in the project's source code history. The patch adds a check that the buffer size has not overflowed:
static rfbBool mallocFrameBufferHandler( rfbClient* p_client )
(...)
assert(!(p_client->width & ~0xffff)); // fits in 16 bits
uint16_t i_width = p_client->width;
assert(!(p_client->height & ~0xffff)); // fits in 16 bits
uint16_t i_height = p_client->height;
uint8_t i_bits_per_pixel = p_client->format.bitsPerPixel;
assert((i_bits_per_pixel & 0x7) == 0); // multiple of 8
(...)
/* Set up framebuffer */
if (mul_overflow(i_width, i_height * (i_bits_per_pixel / 8), &p_sys->i_framebuffersize))
{
msg_Err(p_demux, "VNC framebuffersize overflow");
return FALSE;
}
(...)
Line by line: the assert lines are sanity checks that stop the program in debug builds if a value does not fit where it should. !(p_client->width & ~0xffff) checks that the width has no bits set above the 16th, in other words that it fits in 16 bits; the same is done for the height. The variables are then stored in explicitly sized types, uint16_t for width and height and uint8_t for bits per pixel, so the compiler knows exactly how much room each number has. The check (i_bits_per_pixel & 0x7) == 0 confirms the depth is a multiple of 8, so dividing by 8 is exact. The key change is the if around mul_overflow(...): that function explicitly checks whether the multiplication would overflow before performing it. If it would, VLC logs the error "VNC framebuffersize overflow" and returns FALSE, refusing to allocate a truncated buffer. Version 3.0.18, published on 2022-11-30, contains the fix.
Timeline
| Date | Action |
|---|---|
| 2022-09-19 | Ticket opened in VLC tracker |
| 2022-09-19 | Developers proposed a patch fixing the vulnerability |
| 2022-09-23 | CVE-2022-41325 assigned by MITRE |
| 2022-11-30 | Publication of 3.0.18 |
A tracker is the bug-tracking system where a project records issues. The same day the ticket was opened, the developers proposed a patch, and the CVE identifier followed a few days later.
Variants to test
If you want to look for similar bugs in other software, these are the patterns to watch for:
- Other integer-width mismatches in protocol-driven allocations. Look for size calculations that multiply server-supplied 16-bit fields (width, height, depth, stride) in a narrower type than the result needs. A positive result is a computed size that wraps to a small value while the server can still address the full logical buffer.
- Allocation-size caps as the only guard. Where a helper such as
block_Alloc()rejects sizes above a threshold, try dimension pairs whose truncated product falls just below that threshold, as with0x4004x0xfff1here. A positive result is a successful allocation of a buffer smaller than the screen it represents. - Reallocation paths. The same computation feeds
block_Realloc()when a block already exists, so test resize or reconnect flows as well as first allocation. A positive result is a reallocated buffer whose size does not match the advertised framebuffer. - Pixel-level writes. Once a short buffer exists, send single-pixel updates at coordinates near the end of the logical screen and observe whether writes land beyond the allocation. A positive result is memory corruption or a crash at an offset past the allocated size.
Glossary
- VLC: a free, open-source media player and streaming server made by the VideoLAN project.
- Module: a self-contained piece of a program that handles one kind of task, such as reading a particular input format.
- VNC: a protocol for sharing and viewing a remote computer's screen over a network.
- URI: a standard address format, such as
vnc://host:port/orhttp://example.com/. - Port: a numbered channel on a networked machine where a particular service listens.
- libvncClient: a third-party library that implements the low-level VNC conversation.
- Buffer: a block of memory set aside to hold data.
- Framebuffer: the buffer that holds the picture of a screen, pixel by pixel.
- Memory corruption: a bug where a program writes data outside the memory area it reserved, potentially affecting other data or code.
- Write-what-where primitive: the ability, gained by an attacker, to choose what data is written and roughly where in memory.
- Arbitrary code execution: running the attacker's own instructions on the victim's machine.
- Integer overflow: when a calculation produces a number too large for its data type, so it wraps around to a smaller value.
- Truncation: the loss of the high part of a number when it does not fit in the available space.
- Data type: the category and size of a variable, such as
int,uint16oruint8_t, which determines how large a number it can hold. int: a whole-number type, typically 32 bits, which can hold values up to about 2.1 billion.uint16/uint16_t: an unsigned 16-bit whole number, holding values from 0 to 65535.uint8_t: an unsigned 8-bit whole number, holding values from 0 to 255.- Hexadecimal: base-16 number notation, written with a
0xprefix, common in programming. - bpp: bits per pixel, the number of bits used to describe one pixel's colour.
- Packet: a formatted message sent over a network.
- RFB: the protocol VNC uses to exchange screen updates.
- CVE: a public identifier for a security vulnerability, assigned through the CVE system.
- MITRE: the organisation that helps run the CVE system.
- CVSS: a standard scoring system for rating the severity of vulnerabilities, from 0 to 10.
- Commit: a recorded change in a project's source code history.
- Patch: a change that fixes a bug or vulnerability.
assert: a check in code that stops the program if a condition is not met, used to catch impossible values.mul_overflow(): a helper function that checks whether a multiplication would overflow before performing it.- Stack randomisation: a protection that randomises the location of the stack in memory each run.
- Heap randomisation: a protection that randomises the location of heap memory each run.
- PIE (position-independent executable): a build option that loads the program itself at a random address each run.
- Exploitability: how practical it is to turn a bug into a working attack.
- Tracker: a bug-tracking system where a project records and discusses issues.