Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp1205379ybf; Thu, 27 Feb 2020 06:47:36 -0800 (PST) X-Google-Smtp-Source: APXvYqyPhnGrwjoRvVe5Z92UjEQzpT82vzMIfsutIphX6W8ggnXIbfQBUlKCniywTf86tmYE8iLu X-Received: by 2002:a9d:554a:: with SMTP id h10mr3479587oti.344.1582814856427; Thu, 27 Feb 2020 06:47:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582814856; cv=none; d=google.com; s=arc-20160816; b=YVj42tx65OzlQWRW2NttwDav8FoIfddACyVm8FjTn00of+sIdv/a2CW7AcujZr9CQI 9Z17Hv6rr8r4XMclzNjsqvt5xxpgRYiL4Q9XgDFbmIttHpWrbvmkvMalJf5o0cG1gBT8 4n8aTsHxf8ilhrXYgARpyyjt8Nz/0pc06RIlsESCrmyUEv5HdGmB2Cb1suhL+1VRXOoe r33cQzXfuNBqtfR8WmsoYJnmh9N+r1jiLITJOgfffxBAxQUObD/0XueEO4BJ2XZiYjFC 0DdMVLgM1gP8sta3BSj+oltnGoKe0J0BAh33MNxkcQFio3DvWt0mmRxaMMYrOTLrvRcA B9FQ== 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=BuwEwfXPfdPKq+1P/1fVe+10/AJxoVHQu/56jxwlyhY=; b=FlqMaNNciN14j5mokaSLudzHyjEzHENaJ0pWSXyZLnHoRPxPVJDt/I7mzPkEAVvpnW lnQb0G8YxoiUobWyb6x0K+XNg5uINgw1oExuBIXv0xKkTyYBdUu2P8xajovR9Hrn6BdU PMLh4VCiMgcST/tNV1V1h9dP13e+M5Ove0lmBr2KfG9GfKkFOBd5L7d7e+/vmpslmdCJ wMJeUgd9ZaiUhAK2LKR+jf+8CF+kefIwM6MeZxbhz3yanYdYEnQRbamd9/mBTTBZrAGR d140zMD7IL3kgDMRhBGiK2Yz5Q+xRgaCAWsXcOrDqjlZ4M5KPhOCx+wm4Z4qwXujjtR6 dq9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0UpdUNz6; 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 i20si1269215oie.119.2020.02.27.06.47.24; Thu, 27 Feb 2020 06:47:36 -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=0UpdUNz6; 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 S1730335AbgB0NpH (ORCPT + 99 others); Thu, 27 Feb 2020 08:45:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:40712 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729949AbgB0No6 (ORCPT ); Thu, 27 Feb 2020 08:44:58 -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 DB42A20578; Thu, 27 Feb 2020 13:44:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1582811097; bh=NhUQGsnafUe+C5Bx0IIl7hrgX87gytrWYkGTQoNos+0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0UpdUNz6Jtza2BQnHQH7DZvRtMBSIlufY4eFpexim7O9j4bztuWDZ9ZvG+vWZOSNb XpFpE0p1UlFTdR4tMsw60mpZ6CQjXj5sGJGgCCZ9ztUrW2uqNQl4bMDmg5WKWKVD6p PmHfk2Y+2dbiDI8m7pwUVk6C/pGkR3qXPcJQcTgw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , Pablo Neira Ayuso , Zubin Mithra , Sasha Levin Subject: [PATCH 4.4 100/113] netfilter: xt_bpf: add overflow checks Date: Thu, 27 Feb 2020 14:36:56 +0100 Message-Id: <20200227132227.741017495@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200227132211.791484803@linuxfoundation.org> References: <20200227132211.791484803@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: Jann Horn [ Upstream commit 6ab405114b0b229151ef06f4e31c7834dd09d0c0 ] Check whether inputs from userspace are too long (explicit length field too big or string not null-terminated) to avoid out-of-bounds reads. As far as I can tell, this can at worst lead to very limited kernel heap memory disclosure or oopses. This bug can be triggered by an unprivileged user even if the xt_bpf module is not loaded: iptables is available in network namespaces, and the xt_bpf module can be autoloaded. Triggering the bug with a classic BPF filter with fake length 0x1000 causes the following KASAN report: ================================================================== BUG: KASAN: slab-out-of-bounds in bpf_prog_create+0x84/0xf0 Read of size 32768 at addr ffff8801eff2c494 by task test/4627 CPU: 0 PID: 4627 Comm: test Not tainted 4.15.0-rc1+ #1 [...] Call Trace: dump_stack+0x5c/0x85 print_address_description+0x6a/0x260 kasan_report+0x254/0x370 ? bpf_prog_create+0x84/0xf0 memcpy+0x1f/0x50 bpf_prog_create+0x84/0xf0 bpf_mt_check+0x90/0xd6 [xt_bpf] [...] Allocated by task 4627: kasan_kmalloc+0xa0/0xd0 __kmalloc_node+0x47/0x60 xt_alloc_table_info+0x41/0x70 [x_tables] [...] The buggy address belongs to the object at ffff8801eff2c3c0 which belongs to the cache kmalloc-2048 of size 2048 The buggy address is located 212 bytes inside of 2048-byte region [ffff8801eff2c3c0, ffff8801eff2cbc0) [...] ================================================================== Fixes: e6f30c731718 ("netfilter: x_tables: add xt_bpf match") Signed-off-by: Jann Horn Signed-off-by: Pablo Neira Ayuso Signed-off-by: Zubin Mithra Signed-off-by: Sasha Levin --- net/netfilter/xt_bpf.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/net/netfilter/xt_bpf.c b/net/netfilter/xt_bpf.c index dffee9d47ec4b..7b993f25aab92 100644 --- a/net/netfilter/xt_bpf.c +++ b/net/netfilter/xt_bpf.c @@ -25,6 +25,9 @@ static int bpf_mt_check(const struct xt_mtchk_param *par) struct xt_bpf_info *info = par->matchinfo; struct sock_fprog_kern program; + if (info->bpf_program_num_elem > XT_BPF_MAX_NUM_INSTR) + return -EINVAL; + program.len = info->bpf_program_num_elem; program.filter = info->bpf_program; -- 2.20.1