Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1349318pxf; Fri, 19 Mar 2021 05:25:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzOWQjCyFA2j9Wwc0y8E5qGcaqW7KUxuu0h6R8d902vUZDDG5a1wxUKMEM1mUjmsKuJKi6M X-Received: by 2002:a50:f314:: with SMTP id p20mr9102120edm.236.1616156750223; Fri, 19 Mar 2021 05:25:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616156750; cv=none; d=google.com; s=arc-20160816; b=k0KdNMT8uOh7NwGzJ7Jgpa3Ls9zjGkU/kGlghQyuPLAWgufGcf+tFLAVOf1WbYsikM qig8hB7hq2f/p/bdOdfmC0M2gcVS4jjO/POu/PB+xj0fpE2HtxAbL5Mr28TIToHQkDsJ AU299cVtItQHJuTs0rIYZChqMEPf9k1NhRpRPy+nbwfe17vQ/IEQN81GYXOoOGG1Ddt7 2ccSbzVv5M5JzFJMuR68zwvqRAWtvdWAtTUp+rMkeBCXseR6c/xm0FhtiVvYuZzvmRfH +SZ7oLkPw6+4maRmD0rKArC53lf30bTbuXu7Ltq12eLQ5QCZQdLkN/mrKTcsHasXUbY0 oZGA== 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=S+Z1jqarvj7gbq98MU08+mcuJa+HxtrLhYr7PPAsKy4=; b=OCuz9lHY7kUJNfB7tHMmLguI2Re37hAGdyH0KCJNzxZZ6oKzgSwgREECEUnlP1xsZc xHikyko1jBEvGlJH24405e+3KBmkgMSEYU5ZmkVVCLcWdGo6l2c9jbPQSJ3yNZZj8jCl geOib+VZ+FjJ30AZd+YKFDHS1lncwSLZabU+qHTHhqovIUFNsMSJ8F2xQhm8zRR5kKgB 2W7SiTVi6xy4YUgHTo9Z33XrCk+Wntk2onlHxG81x7QYgCAjTxwVQYrhTbntvFlvoYVb f/NhJRkxjfixxwe6SSedLYppy47E4UnUb2JgANjQSbZ5EPKofP7xllcoHwz03/JGD907 Z8GQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=13TnhMr6; 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 ds18si3995807ejc.311.2021.03.19.05.25.27; Fri, 19 Mar 2021 05:25:50 -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=13TnhMr6; 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 S231745AbhCSMWT (ORCPT + 99 others); Fri, 19 Mar 2021 08:22:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:60476 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231492AbhCSMVe (ORCPT ); Fri, 19 Mar 2021 08:21:34 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id E2E8964F78; Fri, 19 Mar 2021 12:21:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1616156494; bh=iFnsLUmsfMqS12sCw6XbUg0R64wS1TBt045tHe0NMtU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=13TnhMr6Ocqu5VHp97b18KyTGzVwaq1AgKzgLI6KtmBg3Rfo9Hn/OmVfAWo7k+HkA R3xknxU5wtIZqxKIV1zqkbDviAYv+yIX9FQfTYwQu7GrzqeX6Bi/v4GF+Tzc+Pk4Uj CQbTkHY+3hK5sDiB67QJPNj8GtBuXGD5yoX7jlDI= 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.11 21/31] bpf: Fix off-by-one for area size in creating mask to left Date: Fri, 19 Mar 2021 13:19:15 +0100 Message-Id: <20210319121747.889614823@linuxfoundation.org> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210319121747.203523570@linuxfoundation.org> References: <20210319121747.203523570@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 10d2bb2e6b1d8c4576c56a748f697dbeb8388899 upstream. retrieve_ptr_limit() computes the ptr_limit for registers with stack and map_value type. ptr_limit is the size of the memory area that is still valid / in-bounds from the point of the current position and direction of the operation (add / sub). This size will later be used for masking the operation such that attempting out-of-bounds access in the speculative domain is redirected to remain within the bounds of the current map value. When masking to the right the size is correct, however, when masking to the left, the size is off-by-one which would lead to an incorrect mask and thus incorrect arithmetic operation in the non-speculative domain. Piotr found that if the resulting alu_limit value is zero, then the BPF_MOV32_IMM() from the fixup_bpf_calls() rewrite will end up loading 0xffffffff into AX instead of sign-extending to the full 64 bit range, and as a result, this allows abuse for executing speculatively out-of- bounds loads against 4GB window of address space and thus extracting the contents of kernel memory via side-channel. Fixes: 979d63d50c0c ("bpf: prevent out of bounds speculation on pointer arithmetic") 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 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5398,13 +5398,13 @@ static int retrieve_ptr_limit(const stru */ off = ptr_reg->off + ptr_reg->var_off.value; if (mask_to_left) - *ptr_limit = MAX_BPF_STACK + off; + *ptr_limit = MAX_BPF_STACK + off + 1; else *ptr_limit = -off; return 0; case PTR_TO_MAP_VALUE: if (mask_to_left) { - *ptr_limit = ptr_reg->umax_value + ptr_reg->off; + *ptr_limit = ptr_reg->umax_value + ptr_reg->off + 1; } else { off = ptr_reg->smin_value + ptr_reg->off; *ptr_limit = ptr_reg->map_ptr->value_size - off;