Return-Path: Received: from mail-bn3nam01on0119.outbound.protection.outlook.com ([104.47.33.119]:51237 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S934995AbeCHE5V (ORCPT ); Wed, 7 Mar 2018 23:57:21 -0500 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Yonghong Song , Daniel Borkmann , Sasha Levin Subject: [PATCH AUTOSEL for 4.15 45/78] bpf/cgroup: fix a verification error for a CGROUP_DEVICE type prog Date: Thu, 8 Mar 2018 04:56:21 +0000 Message-ID: <20180308045525.7662-45-alexander.levin@microsoft.com> References: <20180308045525.7662-1-alexander.levin@microsoft.com> In-Reply-To: <20180308045525.7662-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Yonghong Song [ Upstream commit 06ef0ccb5a36e1feba9b413ff59a04ecc4407c1c ] The tools/testing/selftests/bpf test program test_dev_cgroup fails with the following error when compiled with llvm 6.0. (I did not try with earlier versions.) libbpf: load bpf program failed: Permission denied libbpf: -- BEGIN DUMP LOG --- libbpf: 0: (61) r2 =3D *(u32 *)(r1 +4) 1: (b7) r0 =3D 0 2: (55) if r2 !=3D 0x1 goto pc+8 R0=3Dinv0 R1=3Dctx(id=3D0,off=3D0,imm=3D0) R2=3Dinv1 R10=3Dfp0 3: (69) r2 =3D *(u16 *)(r1 +0) invalid bpf_context access off=3D0 size=3D2 ... The culprit is the following statement in dev_cgroup.c: short type =3D ctx->access_type & 0xFFFF; This code is typical as the ctx->access_type is assigned as below in kernel/bpf/cgroup.c: struct bpf_cgroup_dev_ctx ctx =3D { .access_type =3D (access << 16) | dev_type, .major =3D major, .minor =3D minor, }; The compiler converts it to u16 access while the verifier cgroup_dev_is_valid_access rejects any non u32 access. This patch permits the field access_type to be accessible with type u16 and u8 as well. Signed-off-by: Yonghong Song Tested-by: Roman Gushchin Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin --- include/uapi/linux/bpf.h | 3 ++- kernel/bpf/cgroup.c | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 4c223ab30293..04b2f613dd06 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -995,7 +995,8 @@ struct bpf_perf_event_value { #define BPF_DEVCG_DEV_CHAR (1ULL << 1) =20 struct bpf_cgroup_dev_ctx { - __u32 access_type; /* (access << 16) | type */ + /* access_type encoded as (BPF_DEVCG_ACC_* << 16) | BPF_DEVCG_DEV_* */ + __u32 access_type; __u32 major; __u32 minor; }; diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index b789ab78d28f..c1c0b60d3f2f 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -568,6 +568,8 @@ static bool cgroup_dev_is_valid_access(int off, int siz= e, enum bpf_access_type type, struct bpf_insn_access_aux *info) { + const int size_default =3D sizeof(__u32); + if (type =3D=3D BPF_WRITE) return false; =20 @@ -576,8 +578,17 @@ static bool cgroup_dev_is_valid_access(int off, int si= ze, /* The verifier guarantees that size > 0. */ if (off % size !=3D 0) return false; - if (size !=3D sizeof(__u32)) - return false; + + switch (off) { + case bpf_ctx_range(struct bpf_cgroup_dev_ctx, access_type): + bpf_ctx_record_field_size(info, size_default); + if (!bpf_ctx_narrow_access_ok(off, size, size_default)) + return false; + break; + default: + if (size !=3D size_default) + return false; + } =20 return true; } --=20 2.14.1