Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp777455ybl; Wed, 11 Dec 2019 07:22:24 -0800 (PST) X-Google-Smtp-Source: APXvYqw5z4PcHycjWfxc6bJVMTPqtZGZ4v/KJpqbnAulUGkJRpqV2Ol9S+u/6kp6N3q7XPVsByrp X-Received: by 2002:aca:500f:: with SMTP id e15mr3342478oib.172.1576077743940; Wed, 11 Dec 2019 07:22:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576077743; cv=none; d=google.com; s=arc-20160816; b=LDYWl6u2HoP1jUSKro4bepTIiaSbw2G2YmfMRwHAm002mO1Hfx53aPIHyDDxTPMURM AvErQhUoQhUK9qGZYP+OoURcF7/YJiR71Ce83Wu/QtM/yTtvPSqRUlc6cd/hfa6BvDty sufYBYtwHoUW4uyosBbQ42QpN4yxSwD9ZJfs1ZfGsBca25oJmM/YIL/t/5t9ZVV0jsma th0oURcMgbv4TtADKpcCYQbYrsLyPYOfCkM1EVLoQP1sM/6teR8nCBqriJi69D6TJeu6 17sjeayCqbpMeccoFdkj0GP6rUlc7b0FVQzjVMccWWSZ7cuAO9WP6dJqafLm0nE1v5mz +nMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=K0yfzrHC14hTRMTzMYa2nYUuuSazlbfygMUw7ZjVfII=; b=PO7TpTsOZnSvCaO9gBM2oRxMcSWR5G3OEjAYui+E1jUn4gN6gTWF6a8A/j0TJ0y+I8 G5dXYd7kAAQ/SCx3lPJJJp+NQgvmO9gi1T7gixfDR4pcws3OWvWzTuLIjvZBjHwg2tPK YU88g3MXvvkItt+kbQCsZWotJ6hFQiK6LZVC7eUC65EEs1Yn82VngZ4nHu7qQLbziBa/ OmrLADdBibN54WU0/Nda0To9S5PKO54HezQYeyRg45r/cJpbsVTCDEMgaVXj7Q0rVOVR 0bJ4zaL9TEOvv2Ax1B/TPaaRf0+473pT8Cwt5381BusxgocXFm0vrlT16AWRC97JuGos DgZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=E5Do0nxJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c24si1139955otf.14.2019.12.11.07.22.11; Wed, 11 Dec 2019 07:22:23 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=E5Do0nxJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732477AbfLKPVg (ORCPT + 99 others); Wed, 11 Dec 2019 10:21:36 -0500 Received: from mail.kernel.org ([198.145.29.99]:51922 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732461AbfLKPVd (ORCPT ); Wed, 11 Dec 2019 10:21:33 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BB66422527; Wed, 11 Dec 2019 15:21:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576077692; bh=eIdXhAGFn/ispwl6WOaxmFhWcCg+DsVNdF85o1I24SE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E5Do0nxJJFi7yBZ0DIAbl5DRfcNlViYXa8bdde1DXKwDaWbPvg3NBryeKDEXx4tuK uO62km2FSCiLO61VAozxJN3gjmjXcWDWCNZSnCYDu6YKYC/gUnDUxK6GMacOH+oHn+ qVHmTmY/fptl7FYuBl71fsNDSLlAz60iKATtWln8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Martin KaFai Lau , Yonghong Song , Alexei Starovoitov , Sasha Levin Subject: [PATCH 4.19 134/243] bpf: btf: check name validity for various types Date: Wed, 11 Dec 2019 16:04:56 +0100 Message-Id: <20191211150348.188128369@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191211150339.185439726@linuxfoundation.org> References: <20191211150339.185439726@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yonghong Song [ Upstream commit eb04bbb608e683f8fd3ef7f716e2fa32dd90861f ] This patch added name checking for the following types: . BTF_KIND_PTR, BTF_KIND_ARRAY, BTF_KIND_VOLATILE, BTF_KIND_CONST, BTF_KIND_RESTRICT: the name must be null . BTF_KIND_STRUCT, BTF_KIND_UNION: the struct/member name is either null or a valid identifier . BTF_KIND_ENUM: the enum type name is either null or a valid identifier; the enumerator name must be a valid identifier. . BTF_KIND_FWD: the name must be a valid identifier . BTF_KIND_TYPEDEF: the name must be a valid identifier For those places a valid name is required, the name must be a valid C identifier. This can be relaxed later if we found use cases for a different (non-C) frontend. Fixes: 69b693f0aefa ("bpf: btf: Introduce BPF Type Format (BTF)") Acked-by: Martin KaFai Lau Signed-off-by: Yonghong Song Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/bpf/btf.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index f0f9109f59bac..3e2413345e718 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -1168,6 +1168,22 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env, return -EINVAL; } + /* typedef type must have a valid name, and other ref types, + * volatile, const, restrict, should have a null name. + */ + if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) { + if (!t->name_off || + !btf_name_valid_identifier(env->btf, t->name_off)) { + btf_verifier_log_type(env, t, "Invalid name"); + return -EINVAL; + } + } else { + if (t->name_off) { + btf_verifier_log_type(env, t, "Invalid name"); + return -EINVAL; + } + } + btf_verifier_log_type(env, t, NULL); return 0; @@ -1325,6 +1341,13 @@ static s32 btf_fwd_check_meta(struct btf_verifier_env *env, return -EINVAL; } + /* fwd type must have a valid name */ + if (!t->name_off || + !btf_name_valid_identifier(env->btf, t->name_off)) { + btf_verifier_log_type(env, t, "Invalid name"); + return -EINVAL; + } + btf_verifier_log_type(env, t, NULL); return 0; @@ -1381,6 +1404,12 @@ static s32 btf_array_check_meta(struct btf_verifier_env *env, return -EINVAL; } + /* array type should not have a name */ + if (t->name_off) { + btf_verifier_log_type(env, t, "Invalid name"); + return -EINVAL; + } + if (btf_type_vlen(t)) { btf_verifier_log_type(env, t, "vlen != 0"); return -EINVAL; @@ -1557,6 +1586,13 @@ static s32 btf_struct_check_meta(struct btf_verifier_env *env, return -EINVAL; } + /* struct type either no name or a valid one */ + if (t->name_off && + !btf_name_valid_identifier(env->btf, t->name_off)) { + btf_verifier_log_type(env, t, "Invalid name"); + return -EINVAL; + } + btf_verifier_log_type(env, t, NULL); last_offset = 0; @@ -1568,6 +1604,12 @@ static s32 btf_struct_check_meta(struct btf_verifier_env *env, return -EINVAL; } + /* struct member either no name or a valid one */ + if (member->name_off && + !btf_name_valid_identifier(btf, member->name_off)) { + btf_verifier_log_member(env, t, member, "Invalid name"); + return -EINVAL; + } /* A member cannot be in type void */ if (!member->type || !BTF_TYPE_ID_VALID(member->type)) { btf_verifier_log_member(env, t, member, @@ -1755,6 +1797,13 @@ static s32 btf_enum_check_meta(struct btf_verifier_env *env, return -EINVAL; } + /* enum type either no name or a valid one */ + if (t->name_off && + !btf_name_valid_identifier(env->btf, t->name_off)) { + btf_verifier_log_type(env, t, "Invalid name"); + return -EINVAL; + } + btf_verifier_log_type(env, t, NULL); for (i = 0; i < nr_enums; i++) { @@ -1764,6 +1813,14 @@ static s32 btf_enum_check_meta(struct btf_verifier_env *env, return -EINVAL; } + /* enum member must have a valid name */ + if (!enums[i].name_off || + !btf_name_valid_identifier(btf, enums[i].name_off)) { + btf_verifier_log_type(env, t, "Invalid name"); + return -EINVAL; + } + + btf_verifier_log(env, "\t%s val=%d\n", btf_name_by_offset(btf, enums[i].name_off), enums[i].val); -- 2.20.1