Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp3230460imc; Wed, 13 Mar 2019 12:12:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqzn1JL5nZ4nS8ERQ0RAoqtcYWhS9REaJguwq0dbWJfvnRIgI+Xb5RS64mu9k7u1s7VWKvUz X-Received: by 2002:a65:424d:: with SMTP id d13mr27805993pgq.203.1552504348931; Wed, 13 Mar 2019 12:12:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552504348; cv=none; d=google.com; s=arc-20160816; b=tmRcW54L2hRBduqlc3V+fQq3b5p2L9MRPhb56SAL5C3fpLuuuJBin/A67w4g5pdPoH PZKIoegINsboFmGC6TKCqpw/OHmhtut4n0w+mc5Z62uF0FgczaUCxuxOf7akyWVheIG7 CkgDgJfpipFg5EJpGeAtMDc2Vu+o5pDgu2wPhNV5LliJtz4JjDjq+VvQC6QGt7j4+tJb 7cT50rjhh/bdTSmF01uyMVovJMmnFwWOP44GV1uVhV2PYgnIKKkQProM5QnUAfz410xh zPmM205SYKrchTJCPcNmjDEaY44Xb0nqE38Njitk2UB0xinCDLHu6gnJsrtnCU+Ah9d5 Vm8Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XD9Afzzpt0g7XrAwQwyLa02JtuNBn8IoTGgVqG+Qg6E=; b=zr/QKxkqAzo8yznyXqPxtzKfThv0wAg5WTlH7Cx+ywqfGrwfYWZZ7mnws/RUp2lvJE 4VaP+EmwIyhzVt8kCMttDQV5rE50Al8Vtpgh3X37cN5Oo9tDVnMbz59KPUF2pLk+qu+y QaJlulv3uB4dMSXhC7wDr1PEo22M0GJWrSSVNKh/RWYxrWsncYNZ2Q8TzVe/fdg3GFxZ IQKwg1p/5SSD6+QDbNzRM5Y/wFtW4TwfOkqRPrqlnz7/PBUSD9nW7RMBJN2Ds8gLSUmT uWke8NIvdqNz0gIo4+jqMeZ3HmyOq3f/sHtZz4fnCpmqz+OMMPcPGf13hCw3nvU9dtWj EhIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=NhLTjyT0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i185si11433224pfe.4.2019.03.13.12.12.13; Wed, 13 Mar 2019 12:12:28 -0700 (PDT) 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=NhLTjyT0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727109AbfCMTKh (ORCPT + 99 others); Wed, 13 Mar 2019 15:10:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:41178 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727085AbfCMTKf (ORCPT ); Wed, 13 Mar 2019 15:10:35 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A8B332147C; Wed, 13 Mar 2019 19:10:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552504234; bh=6Tq97tdil9VtO3y1H5xtH2kXYL50kpyBtSt5K0yW0Y0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NhLTjyT0AmquwCMNbQ0NXMJ/ugZlnRoQb3pM/V5xZZhg46pU5JwBz+smbmIHMfBSn phW2inJPq+9W7FDD1524ky0smdwXj0JcbfAedqah4inIac2agxfs47WlwrXglohJjO yZYpxj0xa1rH5+C2AMQw9ecxBN3I/2fxZRAA4NlE= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Martin KaFai Lau , Joe Stringer , Alexei Starovoitov , Sasha Levin Subject: [PATCH AUTOSEL 4.20 06/60] bpf: Fix narrow load on a bpf_sock returned from sk_lookup() Date: Wed, 13 Mar 2019 15:09:27 -0400 Message-Id: <20190313191021.158171-6-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190313191021.158171-1-sashal@kernel.org> References: <20190313191021.158171-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Martin KaFai Lau [ Upstream commit d623876646be119439999a229a2c3ce30fd197fb ] By adding this test to test_verifier: { "reference tracking: access sk->src_ip4 (narrow load)", .insns = { BPF_SK_LOOKUP, BPF_MOV64_REG(BPF_REG_6, BPF_REG_0), BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3), BPF_LDX_MEM(BPF_H, BPF_REG_2, BPF_REG_0, offsetof(struct bpf_sock, src_ip4) + 2), BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), BPF_EMIT_CALL(BPF_FUNC_sk_release), BPF_EXIT_INSN(), }, .prog_type = BPF_PROG_TYPE_SCHED_CLS, .result = ACCEPT, }, The above test loads 2 bytes from sk->src_ip4 where sk is obtained by bpf_sk_lookup_tcp(). It hits an internal verifier error from convert_ctx_accesses(): [root@arch-fb-vm1 bpf]# ./test_verifier 665 665 Failed to load prog 'Invalid argument'! 0: (b7) r2 = 0 1: (63) *(u32 *)(r10 -8) = r2 2: (7b) *(u64 *)(r10 -16) = r2 3: (7b) *(u64 *)(r10 -24) = r2 4: (7b) *(u64 *)(r10 -32) = r2 5: (7b) *(u64 *)(r10 -40) = r2 6: (7b) *(u64 *)(r10 -48) = r2 7: (bf) r2 = r10 8: (07) r2 += -48 9: (b7) r3 = 36 10: (b7) r4 = 0 11: (b7) r5 = 0 12: (85) call bpf_sk_lookup_tcp#84 13: (bf) r6 = r0 14: (15) if r0 == 0x0 goto pc+3 R0=sock(id=1,off=0,imm=0) R6=sock(id=1,off=0,imm=0) R10=fp0,call_-1 fp-8=????0000 fp-16=0000mmmm fp-24=mmmmmmmm fp-32=mmmmmmmm fp-40=mmmmmmmm fp-48=mmmmmmmm refs=1 15: (69) r2 = *(u16 *)(r0 +26) 16: (bf) r1 = r6 17: (85) call bpf_sk_release#86 18: (95) exit from 14 to 18: safe processed 20 insns (limit 131072), stack depth 48 bpf verifier is misconfigured Summary: 0 PASSED, 0 SKIPPED, 1 FAILED The bpf_sock_is_valid_access() is expecting src_ip4 can be narrowly loaded (meaning load any 1 or 2 bytes of the src_ip4) by marking info->ctx_field_size. However, this marked ctx_field_size is not used. This patch fixes it. Due to the recent refactoring in test_verifier, this new test will be added to the bpf-next branch (together with the bpf_tcp_sock patchset) to avoid merge conflict. Fixes: c64b7983288e ("bpf: Add PTR_TO_SOCKET verifier type") Cc: Joe Stringer Signed-off-by: Martin KaFai Lau Acked-by: Joe Stringer Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/bpf/verifier.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 63a9253679f2..c0fb76d1e771 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -1533,12 +1533,13 @@ static int check_flow_keys_access(struct bpf_verifier_env *env, int off, return 0; } -static int check_sock_access(struct bpf_verifier_env *env, u32 regno, int off, - int size, enum bpf_access_type t) +static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, + u32 regno, int off, int size, + enum bpf_access_type t) { struct bpf_reg_state *regs = cur_regs(env); struct bpf_reg_state *reg = ®s[regno]; - struct bpf_insn_access_aux info; + struct bpf_insn_access_aux info = {}; if (reg->smin_value < 0) { verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", @@ -1552,6 +1553,8 @@ static int check_sock_access(struct bpf_verifier_env *env, u32 regno, int off, return -EACCES; } + env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; + return 0; } @@ -1948,7 +1951,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn verbose(env, "cannot write into socket\n"); return -EACCES; } - err = check_sock_access(env, regno, off, size, t); + err = check_sock_access(env, insn_idx, regno, off, size, t); if (!err && value_regno >= 0) mark_reg_unknown(env, regs, value_regno); } else { -- 2.19.1