From patchwork Fri Nov 25 06:36:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Sun X-Patchwork-Id: 25846 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3806878wrr; Thu, 24 Nov 2022 22:40:16 -0800 (PST) X-Google-Smtp-Source: AA0mqf577Wm6jra/d3TcSM60Wi4ovGh5ivMxY6ZUXRcZq0ZNRuqRv51LiM+t6d9VQqJSzc1oKZIF X-Received: by 2002:a17:906:160b:b0:78d:dddb:3974 with SMTP id m11-20020a170906160b00b0078ddddb3974mr30090590ejd.411.1669358416175; Thu, 24 Nov 2022 22:40:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669358416; cv=none; d=google.com; s=arc-20160816; b=CJ+nNPUVVqlWTrw0zIqX8USHZYfHTb8SYdVtq+6mrtBUI5jMzkV/ZxgaM3TBgKERtR THKaFvvSfRIk21toNJYnX5EyxgY1zQU7COVe7DQlkC9kn1asuhYBX89f2fZ4itFwT4JL cc/V0Hm37J+LjTzAGSiH3aeoZVtQG0yBOph2fI7uPkiznl9G74yFHp8C20vG/yPjcZIA IPMRBpPS7geh6qN3A3oQoexutPOWVUtLdCEeCZKYP1RAwmHyXK8ztrJxkKPk3l5A4F83 trBBl9sEBg7p4QRKl5QT3QiooSi9qEg++ETqaS8s143yP26O+3yoCvClqUSZatRBI4fD EQGA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=vBJjxQmOrEZrnpEuQrvFjGMPHF9guw+LWjz0eneC8SE=; b=eGjt5ayWsKj+Us4XpYLQhXxGs7omOwRfBwK+ugjcspKwrIuAAhK2OtRjQVOyaWIe3P wrmQ1nhhCBj/pq2Cm0CqX4s8Z5cqFVSZlm0XJlBL7B+XwmBjo2JgoRJA2L26BNVmovYd hc86E+UuwU99l8lDbyk8Lhks7DS2Hz/LIUOjqZL5b/V9SYSbyHOLIgj55AfEJ3ADycVP VtI7ZMHOUpkO3+hFSLe3PeIemEd7+WRAPWW6/z4ZwbcvRd7UOxYXWDLThYWHcGLmVLpQ kn3XEAj969joTEsx1lrTaC7OHMQtgBz5w62rirpLLt5pqAw9VOaH+xOvy7rIb38399rx xYqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BiH1Nypr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ji11-20020a170907980b00b0078dd22dd569si1813305ejc.121.2022.11.24.22.39.52; Thu, 24 Nov 2022 22:40:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BiH1Nypr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229602AbiKYGg5 (ORCPT + 99 others); Fri, 25 Nov 2022 01:36:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229712AbiKYGgz (ORCPT ); Fri, 25 Nov 2022 01:36:55 -0500 Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C11C329838; Thu, 24 Nov 2022 22:36:48 -0800 (PST) Received: by mail-pg1-x529.google.com with SMTP id b62so3221929pgc.0; Thu, 24 Nov 2022 22:36:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vBJjxQmOrEZrnpEuQrvFjGMPHF9guw+LWjz0eneC8SE=; b=BiH1Nypr9XqG9qII974B13vHBqKSfJOB+OrFzA0+L2WUCatoReNQS/f4d49Ls/4j8t K9Wi7Uu71i6dZawT2IhD2GeJGojOn2UQjAksAJPL88Har5uTiFCAZlmckU7tCR7mPFD+ uTcNFKi/aAu6X7nQc5ooqdDOzKhGdo0vO9xAELTV1pBSPmOfkrCBZTxZbPim5k0kUyuK PrAhc0ZtVjBwwj07CpN12+5kK4lIYcmTgba/RXuwIX3zizYFrTGiIjVt6PtqxY0u8mz5 qPO7qzffSPnfclXmiWO9jAbJh/aDGVMt2UPLXW+InxhvqZZyPMYrOgzs5spH9Jv1q7zP SUrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vBJjxQmOrEZrnpEuQrvFjGMPHF9guw+LWjz0eneC8SE=; b=Dn4FhZdcSRCWPWsI1gYYQ+ctmA4bPtTYaQp0wVnGQyBSHsLOxn49DUc8Fo2DBhePZR 55p9TIL0aoIVeYuxS4+7xbf1IvBWvJQFIOrgIF89ZhvdcenY5djBmHRi1hZx6F3ujYYq TzJ/LXqna/snQLK2tW6+i3ayXI/fZBXssVoZvRAZkXmzTirsg2F9tBLseLZaBFtBmajy frRspPZ0SIJV/LSAHbOt+KBEv7JjRas7sAu6sRLmak8BDR7/kcHH05fTfCVXQo9bzxul 6o5i1OFrNn9Qqi6WisVNtrdg6LH15s3nOnc5BBN8FEVLNVvIpZXw7BuvM8BFsQKXTNBW Aq+Q== X-Gm-Message-State: ANoB5plp/w8R937ABmVaLw4S7X6HGYZ9So5Pm+6f3md68NSE+ZWF1wyS pVqro4vctwwb+giNp9RdhWBqPjHFtcZV X-Received: by 2002:a63:1e62:0:b0:46b:3acb:77b2 with SMTP id p34-20020a631e62000000b0046b3acb77b2mr13934331pgm.560.1669358207936; Thu, 24 Nov 2022 22:36:47 -0800 (PST) Received: from pc.localdomain ([166.111.83.15]) by smtp.gmail.com with ESMTPSA id nm18-20020a17090b19d200b001fd6066284dsm2214891pjb.6.2022.11.24.22.36.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Nov 2022 22:36:47 -0800 (PST) From: Hao Sun To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, linux-kernel@vger.kernel.org, Hao Sun Subject: [PATCH bpf-next v2 2/3] bpf: Sanitize LDX in jited BPF progs with KASAN Date: Fri, 25 Nov 2022 14:36:29 +0800 Message-Id: <20221125063630.536657-3-sunhao.th@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221125063630.536657-1-sunhao.th@gmail.com> References: <20221125063630.536657-1-sunhao.th@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750449170489700826?= X-GMAIL-MSGID: =?utf-8?q?1750449170489700826?= Make the verifier sanitize LDX insns in jited BPF programs. The dst_reg and AX are free here, different insns that backup R0&R1 are inserted based on their relationships with dst_reg and src, all the scratch regs are backed up to extended stack space before calling the checking functions. Finally, the checking funcs are inserted, and regs are restored. Signed-off-by: Hao Sun --- kernel/bpf/verifier.c | 90 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 5519c24c5bd4..4e253fc20bf2 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -15344,6 +15344,17 @@ BPF_ASAN_STORE(16); BPF_ASAN_STORE(32); BPF_ASAN_STORE(64); +#define BPF_ASAN_LOAD(n) \ + notrace u64 bpf_asan_load##n(u##n *addr) \ + { \ + return *addr; \ + } + +BPF_ASAN_LOAD(8); +BPF_ASAN_LOAD(16); +BPF_ASAN_LOAD(32); +BPF_ASAN_LOAD(64); + #endif /* Do various post-verification rewrites in a single program pass. @@ -15588,6 +15599,85 @@ static int do_misc_fixups(struct bpf_verifier_env *env) insn = new_prog->insnsi + i + delta; continue; } + + /* Sanitize LDX operation*/ + if (BPF_CLASS(insn->code) == BPF_LDX) { + struct bpf_insn sanitize_fn; + struct bpf_insn *patch = &insn_buf[0]; + bool dst_is_r0 = insn->dst_reg == BPF_REG_0; + bool dst_is_r1 = insn->dst_reg == BPF_REG_1; + + if (in_patch_use_ax || insn->src_reg == BPF_REG_10) + continue; + + switch (BPF_SIZE(insn->code)) { + case BPF_B: + sanitize_fn = BPF_EMIT_CALL(bpf_asan_load8); + break; + case BPF_H: + sanitize_fn = BPF_EMIT_CALL(bpf_asan_load16); + break; + case BPF_W: + sanitize_fn = BPF_EMIT_CALL(bpf_asan_load32); + break; + case BPF_DW: + sanitize_fn = BPF_EMIT_CALL(bpf_asan_load64); + break; + } + + /* Backup R0 and R1, REG_AX and dst_reg are free. */ + if (insn->src_reg == BPF_REG_1) { + if (!dst_is_r0) + *patch++ = BPF_MOV64_REG(BPF_REG_AX, BPF_REG_0); + } else if (insn->src_reg == BPF_REG_0) { + if (!dst_is_r1) + *patch++ = BPF_MOV64_REG(BPF_REG_AX, BPF_REG_1); + *patch++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_0); + } else if (!dst_is_r1) { + *patch++ = BPF_MOV64_REG(BPF_REG_AX, BPF_REG_1); + *patch++ = BPF_MOV64_REG(BPF_REG_1, insn->src_reg); + if (!dst_is_r0) + *patch++ = BPF_MOV64_REG(insn->dst_reg, BPF_REG_0); + } else { + *patch++ = BPF_MOV64_REG(BPF_REG_1, insn->src_reg); + *patch++ = BPF_MOV64_REG(BPF_REG_AX, BPF_REG_0); + } + if (insn->off != 0) + *patch++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, insn->off); + BACKUP_SCRATCH_REGS; + /* Invoke sanitize fn, R1~R5 are stored to stack during jit. */ + *patch++ = sanitize_fn; + RESTORE_SCRATCH_REGS; + if (insn->off != 0) + *patch++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -insn->off); + if (insn->src_reg == BPF_REG_1) { + if (!dst_is_r0) + *patch++ = BPF_MOV64_REG(BPF_REG_0, BPF_REG_AX); + } else if (insn->src_reg == BPF_REG_0) { + *patch++ = BPF_MOV64_REG(BPF_REG_0, BPF_REG_1); + if (!dst_is_r1) + *patch++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_AX); + } else if (!dst_is_r1) { + if (!dst_is_r0) + *patch++ = BPF_MOV64_REG(BPF_REG_0, insn->dst_reg); + if (insn->src_reg == insn->dst_reg) + *patch++ = BPF_MOV64_REG(insn->src_reg, BPF_REG_1); + *patch++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_AX); + } else { + *patch++ = BPF_MOV64_REG(BPF_REG_0, BPF_REG_AX); + } + *patch++ = *insn; + cnt = patch - insn_buf; + + new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); + if (!new_prog) + return -ENOMEM; + + delta += cnt - 1; + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; + continue; + } #endif if (insn->code != (BPF_JMP | BPF_CALL))