From patchwork Fri May 30 17:15:12 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chema Gonzalez X-Patchwork-Id: 354281 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 715911400D8 for ; Sat, 31 May 2014 03:15:38 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1030286AbaE3RPS (ORCPT ); Fri, 30 May 2014 13:15:18 -0400 Received: from mail-yh0-f74.google.com ([209.85.213.74]:48096 "EHLO mail-yh0-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965068AbaE3RPO (ORCPT ); Fri, 30 May 2014 13:15:14 -0400 Received: by mail-yh0-f74.google.com with SMTP id 29so461840yhl.1 for ; Fri, 30 May 2014 10:15:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=P/Iu3IF0zYETyUiLI/iuZh/0zeXG5F9TjWs+a7mpApk=; b=IDpd4SRuVlZNsB1agP+gDAr+7AjxUJJQi5USsPxFkYxJJU6K4RIfzi5M+H1CiuaTxd b4+ZS3NFyGe9vokm4UA9LVtWI2rG3ohnr70mF/TDCu/ZjT80XY/nUNVwg2DwgmRJekmz BboOITR90X1spVSoA8hGgcYtRUt5Udwyr0lrEopYvkr62ekHm0fpsnun/sDlxkr3Ex6n 96RYUg/N8kGS/mSixpqoezy16mAM06dKXdxqj+d0+VkryO8PXN8TIgF7E1/CpFg7PtWF XDqkhsRYruSmcA0sJBomjMeS7dDdOYbb2o/Pn9kGxUQynL0RabqnJ48VDor+M/kR+oyG kUuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=P/Iu3IF0zYETyUiLI/iuZh/0zeXG5F9TjWs+a7mpApk=; b=CplNYKTZwM924Nx3X4nRmrc2Yso4ztr+nuzlAZlMN2/3uDupqnRQj0xob2G3GbdYk8 A9U8zLRMVbkR1pQ/Yv/f++zY3YoimwShR7SD99v7Neq71uOxjqvib3PpHlgn1Og8BCCV UWtjqAD40VSZG2R+Cuj9t6cP9bpSs23KCJm6D67KRrHDqIVQ442T/moWTVrYZsux8i3w yPJ4LnRkFd2nhYdQ5P8780QjsvgdhdZcXcjdVBfHel6RGomE/G+5UymqNLTEZ2dOt1ET N3Obk8svF4SdgHQbRxiSq72r4b1DVRRlhHG/77/5n2d2c/HzDGqOmTis8Qq6KnKtYpC1 3V3w== X-Gm-Message-State: ALoCoQmbSrALl7R9fF29pgvQEGlLNkdLYW6j+9ogEtBDe6wWCar54lRzF2rfHFuQZuKtDGFDthbe X-Received: by 10.236.176.225 with SMTP id b61mr4058720yhm.48.1401470113683; Fri, 30 May 2014 10:15:13 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id h13si320122yhj.0.2014.05.30.10.15.13 for (version=TLSv1.1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 30 May 2014 10:15:13 -0700 (PDT) Received: from cardoso.mtv.corp.google.com (cardoso.mtv.corp.google.com [172.17.130.95]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTP id 8736531C605; Fri, 30 May 2014 10:15:13 -0700 (PDT) Received: by cardoso.mtv.corp.google.com (Postfix, from userid 141503) id 34ED8940248; Fri, 30 May 2014 10:15:13 -0700 (PDT) From: Chema Gonzalez To: David Miller , Eric Dumazet , Daniel Borkmann , Alexei Starovoitov Cc: netdev@vger.kernel.org, Chema Gonzalez Subject: [PATCH v2 net-next] net: filter: fix length calculation in BPF testsuite Date: Fri, 30 May 2014 10:15:12 -0700 Message-Id: <1401470112-3933-1-git-send-email-chema@google.com> X-Mailer: git-send-email 1.9.1.423.g4596e3a In-Reply-To: <53879B78.5050106@redhat.com> References: <53879B78.5050106@redhat.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org The current probe_filter_length() (the function that calculates the length of a test BPF filter) behavior is to declare the end of the filter as soon as it finds {0, *, *, 0}. This is actually a valid insn ("ld #0"), so any filter with includes "BPF_STMT(BPF_LD | BPF_IMM, 0)" fails (its length is cut short). We are changing probe_filter_length() so as to start from the end, and declare the end of the filter as the first instruction which is not {0, *, *, 0}. This solution produces a simpler patch than the alternative of using an explicit end-of-filter mark. It is technically incorrect if your filter ends up with "ld #0", but that should not happen anyway. We also add a new test (LD_IMM_0) that includes ld #0 (does not work without this patch). Signed-off-by: Chema Gonzalez Acked-by: Daniel Borkmann Acked-by: Alexei Starovoitov --- lib/test_bpf.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/test_bpf.c b/lib/test_bpf.c index af677cb..e60f766 100644 --- a/lib/test_bpf.c +++ b/lib/test_bpf.c @@ -158,6 +158,18 @@ static struct bpf_test tests[] = { { { 0, 0x800000ff }, { 1, 0x800000ff } }, }, { + "LD_IMM_0", + .u.insns = { + BPF_STMT(BPF_LD | BPF_IMM, 0), /* ld #0 */ + BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0), + BPF_STMT(BPF_RET | BPF_K, 0), + BPF_STMT(BPF_RET | BPF_K, 1), + }, + CLASSIC, + { }, + { { 1, 1 } }, + }, + { "LD_IND", .u.insns = { BPF_STMT(BPF_LDX | BPF_LEN, 0), @@ -1542,12 +1554,11 @@ static int probe_filter_length(struct sock_filter *fp) { int len = 0; - while (fp->code != 0 || fp->k != 0) { - fp++; - len++; - } + for (len = MAX_INSNS - 1; len > 0; --len) + if (fp[len].code != 0 || fp[len].k != 0) + break; - return len; + return len + 1; } static struct sk_filter *generate_filter(int which, int *err)