Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1346612pxf; Fri, 19 Mar 2021 05:22:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyWTplBuKDEXxE5xuwUpAmBkGosH0bjXR31AZwpq2/b11TKeNmfzSMG6ofWoR+Xc5QmOWcb X-Received: by 2002:aa7:df14:: with SMTP id c20mr9149763edy.197.1616156520970; Fri, 19 Mar 2021 05:22:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616156520; cv=none; d=google.com; s=arc-20160816; b=Q+jfxlx4+++ejl2FTyzqgGHgnvxnzrdwcUhc74pXrSW8fzAHkXUqyfiEAL9M3a37L3 iRVgYMw2ii4IpLi2OPLHUvLOrNAG9DpORuR23ma7ilin4iMhnYYqNqgPNaO7YuiRD9eS 1wU7kS96Sdba5FY4wm+mWOYy/VyMSc4h4CddGy9HcXCFsglxvq3+jinQtuhAbtJrt99L cOJZOZ1cHYVM0CkSz/h0skVW8P/sdf0Q6LmwyLx+xAPdYMPZ9h+SO7rxhGliPB9f4ff4 Y53zGaDrBEsueEiaXOpsUQPlvcuzKsEwojunHDiMvmoBjU14PO9AUcT6f4BvOfZqXlg8 vpbA== 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=6fW0y0Of8XrtgGhLmYCQFHCM8r2vy3l38ArJ+8/6/ZE=; b=k990Kr7Qrzmuj7cP4IZE4+jW9HODze9uw772XbnivQN0yRvSVBIeZCr+30QDmY24Wk ZxM6Rpsd37Pm93eefSI9Hj/tFYzZArYX+Py3rMBCN2BqZcmuxQsMp8C7F2B+Kblis6Di 21qPNXct127IW/OIfzncuT2bVmJlxtim8yUcs1Am4k244WpLA5SMGtoNzrqXqTVL+4Gk g1w/M2ydePwrchqB3tJOlOo/kMDHfjUsH2X2rX315av3CR11+7m98MjuygRZS2+ISpim BeAwXxtgRyV1G4i4/uDHPxv59+k+UTslWFziSllHOMaI0lD8MIIM5EqIR9NukeQ0BnAW K5Bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gY0mbhaM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a10si4047824ejr.227.2021.03.19.05.21.38; Fri, 19 Mar 2021 05:22:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gY0mbhaM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S230259AbhCSMTq (ORCPT + 99 others); Fri, 19 Mar 2021 08:19:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:56830 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229952AbhCSMTS (ORCPT ); Fri, 19 Mar 2021 08:19:18 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A594A6146D; Fri, 19 Mar 2021 12:19:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1616156347; bh=OYKv3CR+Mdvx7BrfK/S9xvEhp/7LLkEQD7iXCkypgcg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gY0mbhaMCbuqSHipgnCS7gR/rbY/092OvmWM9B5XQJzQ3cRTJzZniC4zEHVInYIz7 XUN0jBiiVLkIAK+jlUvFOw/EPfCHY4F1IXS1WPZvBA4zMJNr9AFAjutcnw9qpsqejM vWhkHb02EvapLLLZZ96v34KjHqurTeMI7IqDQOF4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Piotr Krysiuk , Daniel Borkmann , Alexei Starovoitov Subject: [PATCH 5.4 02/18] bpf: Prohibit alu ops for pointer types not defining ptr_limit Date: Fri, 19 Mar 2021 13:18:40 +0100 Message-Id: <20210319121745.533544288@linuxfoundation.org> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210319121745.449875976@linuxfoundation.org> References: <20210319121745.449875976@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Piotr Krysiuk commit f232326f6966cf2a1d1db7bc917a4ce5f9f55f76 upstream. The purpose of this patch is to streamline error propagation and in particular to propagate retrieve_ptr_limit() errors for pointer types that are not defining a ptr_limit such that register-based alu ops against these types can be rejected. The main rationale is that a gap has been identified by Piotr in the existing protection against speculatively out-of-bounds loads, for example, in case of ctx pointers, unprivileged programs can still perform pointer arithmetic. This can be abused to execute speculatively out-of-bounds loads without restrictions and thus extract contents of kernel memory. Fix this by rejecting unprivileged programs that attempt any pointer arithmetic on unprotected pointer types. The two affected ones are pointer to ctx as well as pointer to map. Field access to a modified ctx' pointer is rejected at a later point in time in the verifier, and 7c6967326267 ("bpf: Permit map_ptr arithmetic with opcode add and offset 0") only relevant for root-only use cases. Risk of unprivileged program breakage is considered very low. Fixes: 7c6967326267 ("bpf: Permit map_ptr arithmetic with opcode add and offset 0") Fixes: b2157399cc98 ("bpf: prevent out-of-bounds speculation") Signed-off-by: Piotr Krysiuk Co-developed-by: Daniel Borkmann Signed-off-by: Daniel Borkmann Acked-by: Alexei Starovoitov Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/verifier.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -4341,6 +4341,7 @@ static int sanitize_ptr_alu(struct bpf_v u32 alu_state, alu_limit; struct bpf_reg_state tmp; bool ret; + int err; if (can_skip_alu_sanitation(env, insn)) return 0; @@ -4356,10 +4357,13 @@ static int sanitize_ptr_alu(struct bpf_v alu_state |= ptr_is_dst_reg ? BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; - if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) - return 0; - if (update_alu_sanitation_state(aux, alu_state, alu_limit)) - return -EACCES; + err = retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg); + if (err < 0) + return err; + + err = update_alu_sanitation_state(aux, alu_state, alu_limit); + if (err < 0) + return err; do_sim: /* Simulate and find potential out-of-bounds access under * speculative execution from truncation as a result of @@ -4467,7 +4471,7 @@ static int adjust_ptr_min_max_vals(struc case BPF_ADD: ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); if (ret < 0) { - verbose(env, "R%d tried to add from different maps or paths\n", dst); + verbose(env, "R%d tried to add from different maps, paths, or prohibited types\n", dst); return ret; } /* We can take a fixed offset as long as it doesn't overflow @@ -4522,7 +4526,7 @@ static int adjust_ptr_min_max_vals(struc case BPF_SUB: ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); if (ret < 0) { - verbose(env, "R%d tried to sub from different maps or paths\n", dst); + verbose(env, "R%d tried to sub from different maps, paths, or prohibited types\n", dst); return ret; } if (dst_reg == off_reg) {