Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1226391pxf; Fri, 9 Apr 2021 03:18:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxKYnQsbqRUlRYU381mbEJnXwwp5VBKD5SwJXAOdl7tJsk93zSZURI21H/WCA6+TSc0G/i X-Received: by 2002:a17:906:4f15:: with SMTP id t21mr4759775eju.338.1617963532856; Fri, 09 Apr 2021 03:18:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617963532; cv=none; d=google.com; s=arc-20160816; b=eZhRJ+LTteJnSXsTkJlIDNk8lig2eOFbKHrPNtF+PlaQfZwctF/k9eJOzPjm9TryWv M09hU//tlFdIusHChMr/+og3QFuIvMNQ3EgsZvqwJ71GFP9XdowkdF067EgepCW6GoNZ Br2Q7UGe/89CeAApvh8GMLhz1sv5bXEaJebYPBvmLju5R33a4RIIrb+0ggnYkPTvfMWk oq3XA81np4EXHqCBygHTRTfBNEm0rOmtVPrpWvPm/o0oFRH2/rU6aU8RvfRlc1fuaZje sBJsbRW6HUTK393O4I71zACg9eT9YIr2yPFFjRMKcp6WNbsZmqXf6Lwv72ARCTYMhaEW 51xw== 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=kY/7UIvs+aW8Qm8pBsWCAxXq97IMDSMrOobaZZt6jl0=; b=XczeCrw4yJjF08eT3ba10uYK22sdGVfyreDvwaI/5+F0BBuTT/Eo19WJZcQqOPeCZ1 WX/1Knpk6GsoAdzeDT7Tyag9BlbCz+o89FWsh4vg/94QkmyicDgS2T7dvWL09UNkngH6 fJFtp/vTZgrut2rrjzfV8vz+eXl9wrQq6lMzXYlUe50QikW+ShPZLF40W/il4mN1XH/I 8bMh8HMET7vxYwDsD4S72aaJqRXWX4YdZBj8oH4HA9wsAZxt+l7WBwENyW97rGlCsKTt QQZfOGCghfdMZh0SEn9AEhhqCzLN8M7s2bk0qyXUIFwyDIAB3g1sSIWSXcaBp6zcQV3V 8h/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=b1c2Iq9K; 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 b11si1974607edq.208.2021.04.09.03.18.29; Fri, 09 Apr 2021 03:18:52 -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=b1c2Iq9K; 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 S233702AbhDIKQI (ORCPT + 99 others); Fri, 9 Apr 2021 06:16:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:51088 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233838AbhDIKCV (ORCPT ); Fri, 9 Apr 2021 06:02:21 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id EEADD61263; Fri, 9 Apr 2021 10:00:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1617962410; bh=j+LuwkqM7U3QhRWAjsn+caADMu8VJkSKPKc+BGNZ2YM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b1c2Iq9K4FKHSQzIyeFuCT72Wru1hom/1k20tjbY3WTgEm3j9iIhdj/KoYO3OeGlM WpagJEht/1CkYg0BpfXg4HZ4lQcuMhuXgiNYdC/HvZcHyjSEi3qvbs/6Ob3yrKcFkp LiEf+XcRQIJf5jhjY3UA4Tpd3jNPkB7UY5O7pi+g= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Piotr Krysiuk , Daniel Borkmann Subject: [PATCH 5.10 39/41] bpf, x86: Validate computation of branch displacements for x86-32 Date: Fri, 9 Apr 2021 11:54:01 +0200 Message-Id: <20210409095306.075652415@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210409095304.818847860@linuxfoundation.org> References: <20210409095304.818847860@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 26f55a59dc65ff77cd1c4b37991e26497fc68049 upstream. The branch displacement logic in the BPF JIT compilers for x86 assumes that, for any generated branch instruction, the distance cannot increase between optimization passes. But this assumption can be violated due to how the distances are computed. Specifically, whenever a backward branch is processed in do_jit(), the distance is computed by subtracting the positions in the machine code from different optimization passes. This is because part of addrs[] is already updated for the current optimization pass, before the branch instruction is visited. And so the optimizer can expand blocks of machine code in some cases. This can confuse the optimizer logic, where it assumes that a fixed point has been reached for all machine code blocks once the total program size stops changing. And then the JIT compiler can output abnormal machine code containing incorrect branch displacements. To mitigate this issue, we assert that a fixed point is reached while populating the output image. This rejects any problematic programs. The issue affects both x86-32 and x86-64. We mitigate separately to ease backporting. Signed-off-by: Piotr Krysiuk Reviewed-by: Daniel Borkmann Signed-off-by: Daniel Borkmann Signed-off-by: Greg Kroah-Hartman --- arch/x86/net/bpf_jit_comp32.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) --- a/arch/x86/net/bpf_jit_comp32.c +++ b/arch/x86/net/bpf_jit_comp32.c @@ -2278,7 +2278,16 @@ notyet: } if (image) { - if (unlikely(proglen + ilen > oldproglen)) { + /* + * When populating the image, assert that: + * + * i) We do not write beyond the allocated space, and + * ii) addrs[i] did not change from the prior run, in order + * to validate assumptions made for computing branch + * displacements. + */ + if (unlikely(proglen + ilen > oldproglen || + proglen + ilen != addrs[i])) { pr_err("bpf_jit: fatal error\n"); return -EFAULT; }