From 2918f066d5539d2c35d3dbc87463585a3aefbeec Mon Sep 17 00:00:00 2001 From: LuK1337 Date: Mon, 4 Nov 2024 18:41:05 +0100 Subject: [PATCH] BpfHandler: Add kver checks around V BPF attach program checks Same as the netd change prior, but this is just to make it easier for AOSP forks to work on legacy devices not meeting V requirements. Change-Id: I2ebb479b6c9d3d82ee300bb6bf978184bb6c802b --- bpf/netd/BpfHandler.cpp | 44 ++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/bpf/netd/BpfHandler.cpp b/bpf/netd/BpfHandler.cpp index 8e4c2c630b..50e03299c1 100644 --- a/bpf/netd/BpfHandler.cpp +++ b/bpf/netd/BpfHandler.cpp @@ -120,18 +120,22 @@ static Status initPrograms(const char* cg2_path) { } if (modules::sdklevel::IsAtLeastV()) { - RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_CONNECT4_PROG_PATH, - cg_fd, BPF_CGROUP_INET4_CONNECT)); - RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_CONNECT6_PROG_PATH, - cg_fd, BPF_CGROUP_INET6_CONNECT)); - RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_UDP4_RECVMSG_PROG_PATH, - cg_fd, BPF_CGROUP_UDP4_RECVMSG)); - RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_UDP6_RECVMSG_PROG_PATH, - cg_fd, BPF_CGROUP_UDP6_RECVMSG)); - RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_UDP4_SENDMSG_PROG_PATH, - cg_fd, BPF_CGROUP_UDP4_SENDMSG)); - RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_UDP6_SENDMSG_PROG_PATH, - cg_fd, BPF_CGROUP_UDP6_SENDMSG)); + // V requires 4.19+, so technically this 2nd 'if' is not required, but it + // doesn't hurt us to try to support AOSP forks that try to support older kernels. + if (bpf::isAtLeastKernelVersion(4, 19, 0)) { + RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_CONNECT4_PROG_PATH, + cg_fd, BPF_CGROUP_INET4_CONNECT)); + RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_CONNECT6_PROG_PATH, + cg_fd, BPF_CGROUP_INET6_CONNECT)); + RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_UDP4_RECVMSG_PROG_PATH, + cg_fd, BPF_CGROUP_UDP4_RECVMSG)); + RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_UDP6_RECVMSG_PROG_PATH, + cg_fd, BPF_CGROUP_UDP6_RECVMSG)); + RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_UDP4_SENDMSG_PROG_PATH, + cg_fd, BPF_CGROUP_UDP4_SENDMSG)); + RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_UDP6_SENDMSG_PROG_PATH, + cg_fd, BPF_CGROUP_UDP6_SENDMSG)); + } if (bpf::isAtLeastKernelVersion(5, 4, 0)) { RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_GETSOCKOPT_PROG_PATH, @@ -161,12 +165,16 @@ static Status initPrograms(const char* cg2_path) { } if (modules::sdklevel::IsAtLeastV()) { - if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET4_CONNECT) <= 0) abort(); - if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET6_CONNECT) <= 0) abort(); - if (bpf::queryProgram(cg_fd, BPF_CGROUP_UDP4_RECVMSG) <= 0) abort(); - if (bpf::queryProgram(cg_fd, BPF_CGROUP_UDP6_RECVMSG) <= 0) abort(); - if (bpf::queryProgram(cg_fd, BPF_CGROUP_UDP4_SENDMSG) <= 0) abort(); - if (bpf::queryProgram(cg_fd, BPF_CGROUP_UDP6_SENDMSG) <= 0) abort(); + // V requires 4.19+, so technically this 2nd 'if' is not required, but it + // doesn't hurt us to try to support AOSP forks that try to support older kernels. + if (bpf::isAtLeastKernelVersion(4, 19, 0)) { + if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET4_CONNECT) <= 0) abort(); + if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET6_CONNECT) <= 0) abort(); + if (bpf::queryProgram(cg_fd, BPF_CGROUP_UDP4_RECVMSG) <= 0) abort(); + if (bpf::queryProgram(cg_fd, BPF_CGROUP_UDP6_RECVMSG) <= 0) abort(); + if (bpf::queryProgram(cg_fd, BPF_CGROUP_UDP4_SENDMSG) <= 0) abort(); + if (bpf::queryProgram(cg_fd, BPF_CGROUP_UDP6_SENDMSG) <= 0) abort(); + } if (bpf::isAtLeastKernelVersion(5, 4, 0)) { if (bpf::queryProgram(cg_fd, BPF_CGROUP_GETSOCKOPT) <= 0) abort();