Skip to content

eBPF-based security platform

3.1 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 precise-mitigation probe.
#
# On one AF_ALG aead socket it issues an ordinary copying sendmsg and then
# a zero-copy splice, so the word it prints says which the kernel refused:
#
#   SPLICE_OK        the copying sendmsg and the splice were both allowed
#   SPLICE_BLOCKED   the copying sendmsg was allowed, the splice refused
#   COPY_BLOCKED     even the copying sendmsg was refused (too broad)
#   UNSUPPORTED      no usable AF_ALG aead interface to probe
#
# The precise shield turns SPLICE_OK into SPLICE_BLOCKED while leaving the
# copying path working; a socket_create block would give UNSUPPORTED.

import os
import socket
import struct

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


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 main():
    unrestrict()
    try:
        alg = socket.socket(AF_ALG, socket.SOCK_SEQPACKET, 0)
    except OSError:
        print("UNSUPPORTED")
        return
    with alg:
        try:
            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()
        except OSError:
            print("UNSUPPORTED")
            return
        with op:
            aad = b"\x00" * 16
            try:
                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,
                )
            except PermissionError:
                print("COPY_BLOCKED")
                return
            except OSError:
                print("UNSUPPORTED")
                return
            r_pipe, w_pipe = os.pipe()
            try:
                os.write(w_pipe, b"\x00" * 32)
                try:
                    os.splice(r_pipe, op.fileno(), 32)
                except PermissionError:
                    print("SPLICE_BLOCKED")
                    return
                except (OSError, AttributeError):
                    print("UNSUPPORTED")
                    return
                print("SPLICE_OK")
            finally:
                os.close(r_pipe)
                os.close(w_pipe)


if __name__ == "__main__":
    main()