Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp372170imu; Fri, 16 Nov 2018 04:01:10 -0800 (PST) X-Google-Smtp-Source: AJdET5f/e+YuFAKDZKj+GZq65LXSUQAp2m7E0RzjHvB/CKF/PHuRMKeTYJOsrkauaIF5d16KDGXW X-Received: by 2002:a17:902:110a:: with SMTP id d10-v6mr10622545pla.85.1542369670395; Fri, 16 Nov 2018 04:01:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542369670; cv=none; d=google.com; s=arc-20160816; b=NgppINITW0OcQEcFlSGf9EXBGRo2M8FW82cpPp2CEuLnS/Gr2hnlHE2YoDftsiv+7e 8woOC0N3v9PzxeOLrtV3Qh43i0hHQAgL+vrVrRn+qJAv64qCi/0WjlZ+xMjQMFnQySgr W7XoLeVqav4NlDhMkTwTcRgO6UnEcgFln5oxIys+lQBZZ5KYLpoC1JRPgKAdlVluTk/E uFlNe2nlhqZvK8puwU7CWBOjIvnTdvQOcxHbSAIQovGcq8VD0OqACPoEyzjPnM1TfXaM 48Q5kBYTh/h4+eas7sTxj6/71dRaqwa0jhL5a4qIUF0KteEDmcuLCHhlRlYfnvsxV4i/ 9YNg== 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 :content-language:mime-version:user-agent:date:message-id:cc:to :subject:from; bh=gf0BMsT+OLx2ykGGfXXxinV7b/9yokcTTdncylXJiQQ=; b=XPxY68yJzKk+36OIIxrQgBdAybbwdbZfps9iMW0+XCfeZwcHKwzM6vD6dlX5sWIxN/ xu4ve1xyBhnvBJN0mXWgmdzieazpuC4Vc5vYgZC8yE+4ocD98GVo3TseoI4GOUVeD7Vt TLyZvlOBLZl44p+iUIEz/hacqnGRqvBfvD1xLHJKu6JU9eb6sVeRRq3EebT0at3/JkXt 8Fyne9uCIoK3ygxwsV5UNcWZEkAjHxtZFnjxQr8Y+WUNSFR0YWyKTQdB3I2r/iAuJOFe LADr0BD34QUCX5KCEJRMEdjbdixyjKlXMGBPP17EDr7skLMYglsZTULjk4PtTYT1fa79 BnPg== ARC-Authentication-Results: i=1; mx.google.com; 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 w1-v6si33833092plk.317.2018.11.16.04.00.54; Fri, 16 Nov 2018 04:01:10 -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; 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 S2389504AbeKPWMU (ORCPT + 99 others); Fri, 16 Nov 2018 17:12:20 -0500 Received: from dispatch1-us1.ppe-hosted.com ([67.231.154.164]:57022 "EHLO dispatch1-us1.ppe-hosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727727AbeKPWMT (ORCPT ); Fri, 16 Nov 2018 17:12:19 -0500 X-Virus-Scanned: Proofpoint Essentials engine Received: from webmail.solarflare.com (webmail.solarflare.com [12.187.104.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mx1-us4.ppe-hosted.com (Proofpoint Essentials ESMTP Server) with ESMTPS id AE6862800B9; Fri, 16 Nov 2018 12:00:14 +0000 (UTC) Received: from ec-desktop.uk.solarflarecom.com (10.17.20.45) by ocex03.SolarFlarecom.com (10.20.40.36) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Fri, 16 Nov 2018 04:00:08 -0800 From: Edward Cree Subject: [PATCH bpf] bpf: fix off-by-one error in adjust_subprog_starts To: , CC: , , Message-ID: Date: Fri, 16 Nov 2018 12:00:07 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Language: en-GB Content-Transfer-Encoding: 7bit X-Originating-IP: [10.17.20.45] X-TM-AS-Product-Ver: SMEX-12.5.0.1300-8.5.1010-24226.005 X-TM-AS-Result: No-1.187500-4.000000-10 X-TMASE-MatchedRID: 0WNpcueVtr4vRbVu13x7nsAmcZEx8XHJt0+V7ekgRpgTf11Tnw43V+Qt 5At0KMO6+E4F49aGGdllp59T3E6O3GlYwyKLpRk4syNb+yeIRAooUVkB7ifJnjUsHjosUACSU7g EPucszGdOFu8ssjxG8/JKsIzII36T6EovbiVesMlL6wfFL8xa+EDwlkRNC6PCwDR44lliPu1TsN hCV6hrfjUJquLkByfMYnAKxv9nxqVNfs8n85Te8oMbH85DUZXy3QfwsVk0UbvqwGfCk7KUsxpjA miOmZG+8Mh6PZPsN9L45dQ8KkjDhRteT8ovypFSHLXwhhVPjvkCPSTf+GaamgHQGhjI0OyEFaJy Z91ZT5m8OV5ZShvUdVrMCKBXF1d6I2VNggMWJCP4LggrmsRgvTwNB+BE7PnlftwZ3X11IV0= X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--1.187500-4.000000 X-TMASE-Version: SMEX-12.5.0.1300-8.5.1010-24226.005 X-MDID: 1542369615-odVFHRiXW1wA Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When patching in a new sequence for the first insn of a subprog, the start of that subprog does not change (it's the first insn of the sequence), so adjust_subprog_starts should check start <= off (rather than < off). Also added a test to test_verifier.c (it's essentially the syz reproducer). Fixes: cc8b0b92a169 ("bpf: introduce function calls (function boundaries)") Reported-by: syzbot+4fc427c7af994b0948be@syzkaller.appspotmail.com Signed-off-by: Edward Cree --- I'm assuming I don't need to get a Signed-off-by from syzkaller to use its reproducer like this; I'm not an expert on the copyright niceties of works written by bots. kernel/bpf/verifier.c | 2 +- tools/testing/selftests/bpf/test_verifier.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 1971ca325fb4..6dd419550aba 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5650,7 +5650,7 @@ static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len return; /* NOTE: fake 'exit' subprog should be updated as well. */ for (i = 0; i <= env->subprog_cnt; i++) { - if (env->subprog_info[i].start < off) + if (env->subprog_info[i].start <= off) continue; env->subprog_info[i].start += len - 1; } diff --git a/tools/testing/selftests/bpf/test_verifier.c b/tools/testing/selftests/bpf/test_verifier.c index 6f61df62f690..550b7e46bf4a 100644 --- a/tools/testing/selftests/bpf/test_verifier.c +++ b/tools/testing/selftests/bpf/test_verifier.c @@ -13896,6 +13896,25 @@ static struct bpf_test tests[] = { .prog_type = BPF_PROG_TYPE_SCHED_CLS, .result = ACCEPT, }, + { + "calls: ctx read at start of subprog", + .insns = { + BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 5), + BPF_JMP_REG(BPF_JSGT, BPF_REG_0, BPF_REG_0, 0), + BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2), + BPF_MOV64_REG(BPF_REG_1, BPF_REG_0), + BPF_EXIT_INSN(), + BPF_LDX_MEM(BPF_B, BPF_REG_9, BPF_REG_1, 0), + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }, + .prog_type = BPF_PROG_TYPE_SOCKET_FILTER, + .errstr_unpriv = "function calls to other bpf functions are allowed for root only", + .result_unpriv = REJECT, + .result = ACCEPT, + }, }; static int probe_filter_length(const struct bpf_insn *fp)