From patchwork Thu Nov 3 09:34:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Youlin Li X-Patchwork-Id: 13029822 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1D7FC4332F for ; Thu, 3 Nov 2022 09:34:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229553AbiKCJey (ORCPT ); Thu, 3 Nov 2022 05:34:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229501AbiKCJev (ORCPT ); Thu, 3 Nov 2022 05:34:51 -0400 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC4602AD3 for ; Thu, 3 Nov 2022 02:34:50 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id f63so1192897pgc.2 for ; Thu, 03 Nov 2022 02:34:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=PqAFBiOS42xsITjoRr3FHC0NWlSrAxOw+pt6izNPrvk=; b=krPKlqMc8L/GDXZP15AMH6goEoFjKKk+CuGC+VtJTxr6XkJe7Ltzv4WHKAhwafy48s hUVVaquT4Szpyom2kSjASfxKu9Z9ZGHHNNfpXUdLboVZrUSpDdYVJ6WAHhn1nnb0r5hJ ukSN7FVS6UKnOOu+h8mz9WIGHreeTTRYoC/7gsz/hwJ88bzup7O/STGASCkSVJ2ncIXt QXAuAMfmJX0tx4j7e5ML044b2+fmBrKIdi1rMfQlran3L6XrlcFgXkbfhD+0nIuq7iS/ lZee7vu/5oCd0417d56cQJlwlMQVlYn979U5/clO+eihjFi9geFzZgLryL5siQzOX4CM 1CwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PqAFBiOS42xsITjoRr3FHC0NWlSrAxOw+pt6izNPrvk=; b=YRt+t4qvd/bHgjXuyVab1FKzf7L0fXDWD2fvihxWu02TdCzgxPXZMzAVeilqqYXN0d YPQuwaQp1GJEW/PD6/LegM/kkgb1c574Zlzp2eJPuY8wkgAd55eBuzCyQYgfiSyuRuxa wP57YVImIHXe/OX9Co1i1OiKnwB+bhce2i2vUgjVJB5FiTJ5z1GkMHZGpLdm2IiuKEQa yp0K2IQ7WuBUZkqj8qYZZyslTP4dbyfWvBJVfeV4L+vGxlC2E3qTDb3MrsJK0Dl1PenN 7SRnH+wZ/SRUwt929a44UAXb1DpsKTgQUeiXmzl6xq/XL4YowXP0jTP/hhr6GPpnZt3p uSXA== X-Gm-Message-State: ACrzQf176Me4Vk7VFYf8+bldNVJ2CbOo5dKFeRxmh/9BlHb1HAA4HYWn UcNtMCdS0GHZ+00UbEuFzlEitulEXk2j9IQY X-Google-Smtp-Source: AMsMyM7ZxFc2+FfbH4t4ReLCFOnnVjyTx4/vZhkhF/QbxKeQwd5OUE105OazyVO7eoJlYP76bO7sMg== X-Received: by 2002:aa7:9298:0:b0:56b:b6dc:988a with SMTP id j24-20020aa79298000000b0056bb6dc988amr29617876pfa.5.1667468089907; Thu, 03 Nov 2022 02:34:49 -0700 (PDT) Received: from k1r0aKuee.localdomain ([20.255.2.235]) by smtp.gmail.com with ESMTPSA id b4-20020a170902d50400b0018668bee7cdsm177786plg.77.2022.11.03.02.34.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 02:34:49 -0700 (PDT) From: Youlin Li To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, Youlin Li Subject: [PATCH bpf 1/2 v2] bpf: Fix wrong reg type conversion in release_reference() Date: Thu, 3 Nov 2022 17:34:39 +0800 Message-Id: <20221103093440.3161-1-liulin063@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Some helper functions will allocate memory. To avoid memory leaks, the verifier requires the eBPF program to release these memories by calling the corresponding helper functions. When a resource is released, all pointer registers corresponding to the resource should be invalidated. The verifier use release_references() to do this job, by apply __mark_reg_unknown() to each relevant register. It will give these registers the type of SCALAR_VALUE. A register that will contain a pointer value at runtime, but of type SCALAR_VALUE, which may allow the unprivileged user to get a kernel pointer by storing this register into a map. Using __mark_reg_not_init() while NOT allow_ptr_leaks can mitigate this problem. Fixes: fd978bf7fd31 ("bpf: Add reference tracking to verifier") Signed-off-by: Youlin Li --- v1->v2: Use __mark_reg_not_init() only when !allow_ptr_leaks. kernel/bpf/verifier.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 9ab7188d8f68..1bb797bf9bbc 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -6618,8 +6618,12 @@ static int release_reference(struct bpf_verifier_env *env, return err; bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ - if (reg->ref_obj_id == ref_obj_id) - __mark_reg_unknown(env, reg); + if (reg->ref_obj_id == ref_obj_id) { + if (!env->allow_ptr_leaks) + __mark_reg_not_init(env, reg); + else + __mark_reg_unknown(env, reg); + } })); return 0; From patchwork Thu Nov 3 09:34:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Youlin Li X-Patchwork-Id: 13029823 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9566DC433FE for ; Thu, 3 Nov 2022 09:35:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230017AbiKCJe7 (ORCPT ); Thu, 3 Nov 2022 05:34:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229771AbiKCJe5 (ORCPT ); Thu, 3 Nov 2022 05:34:57 -0400 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98522DF78 for ; Thu, 3 Nov 2022 02:34:56 -0700 (PDT) Received: by mail-pf1-x432.google.com with SMTP id g62so1075905pfb.10 for ; Thu, 03 Nov 2022 02:34:56 -0700 (PDT) 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=2QbGEY4J71OX2r9n4LqKfmPqOIgB9erqJsHB6/sGCpk=; b=DaO5+jedHAQSK3ekGj9SQTlNTTjLyRU/JhTf1qoRfBrFF61AV8FSiEezJ8Urv6GRpd IBxuEfAcqIGlgDVO8Dzs59mJh/iz1xQcaGfjJTuvBnfogaM/v4qcWx94i6p5ZNncWCTX djm/s0FlokRR8lZZ/crUHDjxcA2fVvZcnFlpywrD99IOGFNajYtj4w7S1B0ofddJlP3F IBeGjX1OXcoh7F1rozq7IEBGtYj4NCAYdZMtO+i5TyNdFOfDu1l7BuvI29r6McOnH4NE 3MrFFeIoj1gU4tWpieUA1Xt5YxlENQWIi4aXnQD7fSBXtdxlKPmBsEG7mOGSPLJnlEf4 yCLw== 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=2QbGEY4J71OX2r9n4LqKfmPqOIgB9erqJsHB6/sGCpk=; b=lzvegOw8H2GCTZMcXTjaVPQTQlHqEJ1+WXLWwMnOGPnYgpMBIgXR0cNZN/ljapbuK/ K8B0iZjRevFNtLg7TjCG0jW7Q+gKE2ek2JtKxT9Lckp79XZQdJf7VmEQUWOWbNtx2V8l e9NM9riKfT1MNIW28SYltNeilgNnrbWn9cjmrxjt7lQjB9JU5TDfT642O5BQvUgJ+vhy XOrEr13xZTmeYfd8yLp7ewW+qAKVshMGNM4i+cct6KnfnS35kJCvdcft/qtYnj+2PFAH Qy5yqa/eYAgXn3izbIYpo3UxWSPq/dyUesfneBfAY/h6whhPBxzpNlACkn8hyqaNQ8Uk Td6g== X-Gm-Message-State: ACrzQf1GFBiqknbNKJUIjBRyI/gBxrdGkR+4beWOQ/o1IbI6g0spSDpu Tj9fCMOifeIOB4NWzB7jl1JKyJ4bMwEHxBGKLJI= X-Google-Smtp-Source: AMsMyM6vTWfs1HirVaFhEOcSQnXCzvAPkz1CRgCDz0AkDUVAuyBZJvi6cPDk17AczVtN9THiaafHmQ== X-Received: by 2002:a05:6a00:1251:b0:56d:cabd:7207 with SMTP id u17-20020a056a00125100b0056dcabd7207mr12447923pfi.45.1667468095983; Thu, 03 Nov 2022 02:34:55 -0700 (PDT) Received: from k1r0aKuee.localdomain ([20.255.2.235]) by smtp.gmail.com with ESMTPSA id b4-20020a170902d50400b0018668bee7cdsm177786plg.77.2022.11.03.02.34.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 02:34:55 -0700 (PDT) From: Youlin Li To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, Youlin Li Subject: [PATCH bpf 2/2] selftests/bpf: Add verifier test for release_reference() Date: Thu, 3 Nov 2022 17:34:40 +0800 Message-Id: <20221103093440.3161-2-liulin063@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221103093440.3161-1-liulin063@gmail.com> References: <20221103093440.3161-1-liulin063@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add a test case to ensure that released pointer registers will not be leaked into the MAP. Before fix: ./test_verifier 984 984/u reference tracking: try to leak released ptr reg FAIL Unexpected success to load! verification time 67 usec stack depth 4 processed 23 insns (limit 1000000) max_states_per_insn 0 total_states 2 peak_states 2 mark_read 1 984/p reference tracking: try to leak released ptr reg OK Summary: 1 PASSED, 0 SKIPPED, 1 FAILED After fix: ./test_verifier 984 984/u reference tracking: try to leak released ptr reg OK 984/p reference tracking: try to leak released ptr reg OK Summary: 2 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Youlin Li --- .../selftests/bpf/verifier/ref_tracking.c | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tools/testing/selftests/bpf/verifier/ref_tracking.c b/tools/testing/selftests/bpf/verifier/ref_tracking.c index f18ce867271f..fd683a32a276 100644 --- a/tools/testing/selftests/bpf/verifier/ref_tracking.c +++ b/tools/testing/selftests/bpf/verifier/ref_tracking.c @@ -1044,3 +1044,39 @@ .result_unpriv = REJECT, .errstr_unpriv = "unknown func", }, +{ + "reference tracking: try to leak released ptr reg", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), + BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), + BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), + BPF_LD_MAP_FD(BPF_REG_1, 0), + BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem), + BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), + BPF_EXIT_INSN(), + BPF_MOV64_REG(BPF_REG_9, BPF_REG_0), + + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_LD_MAP_FD(BPF_REG_1, 0), + BPF_MOV64_IMM(BPF_REG_2, 8), + BPF_MOV64_IMM(BPF_REG_3, 0), + BPF_EMIT_CALL(BPF_FUNC_ringbuf_reserve), + BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), + BPF_EXIT_INSN(), + BPF_MOV64_REG(BPF_REG_8, BPF_REG_0), + + BPF_MOV64_REG(BPF_REG_1, BPF_REG_8), + BPF_MOV64_IMM(BPF_REG_2, 0), + BPF_EMIT_CALL(BPF_FUNC_ringbuf_discard), + BPF_MOV64_IMM(BPF_REG_0, 0), + + BPF_STX_MEM(BPF_DW, BPF_REG_9, BPF_REG_8, 0), + BPF_EXIT_INSN() + }, + .fixup_map_array_48b = { 4 }, + .fixup_map_ringbuf = { 11 }, + .result = ACCEPT, + .result_unpriv = REJECT, + .errstr_unpriv = "R8 !read_ok" +},