Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp3829793pxb; Mon, 21 Feb 2022 06:34:04 -0800 (PST) X-Google-Smtp-Source: ABdhPJzSX6ffXlchyMCngo+hUiehyksF+T+sscQw4wlT4gJrNhI3K1dEpwomQkJ5VVU8be1POjp8 X-Received: by 2002:aa7:d7da:0:b0:40f:f16f:dc25 with SMTP id e26-20020aa7d7da000000b0040ff16fdc25mr20910935eds.17.1645454043759; Mon, 21 Feb 2022 06:34:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645454043; cv=none; d=google.com; s=arc-20160816; b=CrWSi4lFw5ZfUr0T3tUaPsIE5X+wn8m4mqKaWVGPaooXndWvPXujAo+w9ajBubILJX l29X2OVFteHkNaIL5ynccjLjcyef6tw9WDZMRvlBJ0fCN3BcMJPmEfLCn3sO0NvVzlRV zWQTd4ftMwcB8KS00mQEOdt4Ls6nWH9GtEJPz9niCCI5Vrf4qZMJKG5YUh6I/HdGbUeB LhtRE/rxKM+qcPeez2n5QrGFjXWxhrBA28iF0D1Xh56BS1QoKvXQ/MnBZScR2vE4+JXx tUb/CRA4LEb6526LvSe269B0lUsMmh4jYXc2A3A+Ee8VPbRAa5tfWH7FdFJVkgxP5kyd jKyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=M7FvI5fGjM4RpUr0+SFeDGITnmLkOw/rpSkooL63HPo=; b=ZWyR/VMAbuU1ImaN4jeupnEY8wO0TXSujuCRld7t3vdrHjuO+Prp3x9uGj/UJJo03u GlRmZb2Em1WEMaYaQ8A9eqw21L92FDsjuC4uX/UsTezjiJMqeDOa+Dyy3+/CMbUhUK8n 4aqsSHP377kb68LAk9kuswh9hkp6x+0c1qRGWuOg3Vl2m5OLT0Zx9z1M4ucaj3rXcVZn LrIgFBTTJfcLWSERW6v1La7ev79GBk92rFHidr/WPeutcoNeXVV11MRZz+/WqNtewWax HPms8T9ozCzRFd19zDAgCGTJ1qmF2lb5six7cqhDEa/d25MUcpVNcO9S7+k27e5SKoH3 IEFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SxSdJ3Cp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o12si8984632edv.387.2022.02.21.06.33.40; Mon, 21 Feb 2022 06:34:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SxSdJ3Cp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353164AbiBUJsO (ORCPT + 99 others); Mon, 21 Feb 2022 04:48:14 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:34324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351164AbiBUJmz (ORCPT ); Mon, 21 Feb 2022 04:42:55 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CE193EA99; Mon, 21 Feb 2022 01:17:56 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 668BC60F22; Mon, 21 Feb 2022 09:17:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77C3DC340E9; Mon, 21 Feb 2022 09:17:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1645435074; bh=qXx8LLN8OdrK+AodAKJg3msw5uNFgNNxaDRX0Eumxzo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SxSdJ3Cpi09PRwVju4BT8Kt60TMb5JhmwUJPwV/tLjatDgNm36H9HrJXIyspjB6s9 tBMeuMPOhUgSVoIKcm5qQTa2LnhRWVU108ZwQ0OhtkRLz1U+nWv25Q1uc8JBXczMWE 3xbj8N7NNJ24Kzgsw09yeQ/mbKldYyr88n+ZOqEk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hao Luo , Alexei Starovoitov Subject: [PATCH 5.16 008/227] bpf: Make per_cpu_ptr return rdonly PTR_TO_MEM. Date: Mon, 21 Feb 2022 09:47:07 +0100 Message-Id: <20220221084935.121206549@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220221084934.836145070@linuxfoundation.org> References: <20220221084934.836145070@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hao Luo commit 34d3a78c681e8e7844b43d1a2f4671a04249c821 upstream. Tag the return type of {per, this}_cpu_ptr with RDONLY_MEM. The returned value of this pair of helpers is kernel object, which can not be updated by bpf programs. Previously these two helpers return PTR_OT_MEM for kernel objects of scalar type, which allows one to directly modify the memory. Now with RDONLY_MEM tagging, the verifier will reject programs that write into RDONLY_MEM. Fixes: 63d9b80dcf2c ("bpf: Introducte bpf_this_cpu_ptr()") Fixes: eaa6bcb71ef6 ("bpf: Introduce bpf_per_cpu_ptr()") Fixes: 4976b718c355 ("bpf: Introduce pseudo_btf_id") Signed-off-by: Hao Luo Signed-off-by: Alexei Starovoitov Link: https://lore.kernel.org/bpf/20211217003152.48334-8-haoluo@google.com Cc: stable@vger.kernel.org # 5.16.x Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/helpers.c | 4 ++-- kernel/bpf/verifier.c | 30 ++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -667,7 +667,7 @@ BPF_CALL_2(bpf_per_cpu_ptr, const void * const struct bpf_func_proto bpf_per_cpu_ptr_proto = { .func = bpf_per_cpu_ptr, .gpl_only = false, - .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | PTR_MAYBE_NULL, + .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | PTR_MAYBE_NULL | MEM_RDONLY, .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID, .arg2_type = ARG_ANYTHING, }; @@ -680,7 +680,7 @@ BPF_CALL_1(bpf_this_cpu_ptr, const void const struct bpf_func_proto bpf_this_cpu_ptr_proto = { .func = bpf_this_cpu_ptr, .gpl_only = false, - .ret_type = RET_PTR_TO_MEM_OR_BTF_ID, + .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | MEM_RDONLY, .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID, }; --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -4333,15 +4333,30 @@ static int check_mem_access(struct bpf_v mark_reg_unknown(env, regs, value_regno); } } - } else if (reg->type == PTR_TO_MEM) { + } else if (base_type(reg->type) == PTR_TO_MEM) { + bool rdonly_mem = type_is_rdonly_mem(reg->type); + + if (type_may_be_null(reg->type)) { + verbose(env, "R%d invalid mem access '%s'\n", regno, + reg_type_str(env, reg->type)); + return -EACCES; + } + + if (t == BPF_WRITE && rdonly_mem) { + verbose(env, "R%d cannot write into %s\n", + regno, reg_type_str(env, reg->type)); + return -EACCES; + } + if (t == BPF_WRITE && value_regno >= 0 && is_pointer_value(env, value_regno)) { verbose(env, "R%d leaks addr into mem\n", value_regno); return -EACCES; } + err = check_mem_region_access(env, regno, off, size, reg->mem_size, false); - if (!err && t == BPF_READ && value_regno >= 0) + if (!err && value_regno >= 0 && (t == BPF_READ || rdonly_mem)) mark_reg_unknown(env, regs, value_regno); } else if (reg->type == PTR_TO_CTX) { enum bpf_reg_type reg_type = SCALAR_VALUE; @@ -6550,6 +6565,13 @@ static int check_helper_call(struct bpf_ regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; regs[BPF_REG_0].mem_size = tsize; } else { + /* MEM_RDONLY may be carried from ret_flag, but it + * doesn't apply on PTR_TO_BTF_ID. Fold it, otherwise + * it will confuse the check of PTR_TO_BTF_ID in + * check_mem_access(). + */ + ret_flag &= ~MEM_RDONLY; + regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; regs[BPF_REG_0].btf = meta.ret_btf; regs[BPF_REG_0].btf_id = meta.ret_btf_id; @@ -9362,7 +9384,7 @@ static int check_ld_imm(struct bpf_verif if (insn->src_reg == BPF_PSEUDO_BTF_ID) { dst_reg->type = aux->btf_var.reg_type; - switch (dst_reg->type) { + switch (base_type(dst_reg->type)) { case PTR_TO_MEM: dst_reg->mem_size = aux->btf_var.mem_size; break; @@ -11505,7 +11527,7 @@ static int check_pseudo_btf_id(struct bp err = -EINVAL; goto err_put; } - aux->btf_var.reg_type = PTR_TO_MEM; + aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; aux->btf_var.mem_size = tsize; } else { aux->btf_var.reg_type = PTR_TO_BTF_ID;