Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp119419iof; Sun, 5 Jun 2022 22:57:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxKbGwh2RHt/2V3rpItO5I/N4AhzYFJDVgKTT2RWgrh1mYmVQYhMrxu9/xsTCMX8TtQBnLn X-Received: by 2002:a63:688a:0:b0:3fa:a80c:a228 with SMTP id d132-20020a63688a000000b003faa80ca228mr19014254pgc.182.1654495062194; Sun, 05 Jun 2022 22:57:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654495062; cv=none; d=google.com; s=arc-20160816; b=qJaUPdXoK3Wy2SswU9D+uP8wnTAcOBAPpVxToOR9su6CbYVUkzhrd5MiXnFZuzRI6V vSdD737X2fJX5pSd4BCi1ZOluAx7/X75LGkz59sjaOSvyv4XA2auq1DM8yW3jnPIek/z +qVI94XFdL0lJrFnt1HO3PyBGOsJIlSEV0Ck06G0W3wm2k1/9Pi7OVnAmlsUojXpReRx gruQUVk69cYlOzwWLxzWXf4GWPYa1ca+yWzbM5eeT/PHNp00QGdwtQLBMo5jTmU6Z84E DzSr/RCePWaJjNjAIGov6xwA2Z5mhmUiEnkNlMNKnH5kEum+RW3VpAHhqkquYafoIXDD v2cQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=lX0USjuhoF66Qzcvv3w0brcs7MPlHGu9J+A4hDdHrUU=; b=X53weG6LSVFOjPz/sV4/1goWMs1eHh0TVqcRGQymap5W75zO0RLR6aX/D1N+vkOsHG G5i1WhcDJkGb22TiCVcPSAETTk0DH5GuV97HkYVqwH6zZ+t1fJPgpQupQX8+a1ubJv7k HQTRtwUGa1Ktbv7o70Dwnlq3YMl6BPU35KZPPLE7DD48/gin1UQlUTIyeqZixtCmmjV1 8+fTLW0VJIxSXvCV+aOhp7FnFCuby/FhKNG75YErPLfXuXjpvB7oh27r672AnyQADz8W qI4ouluHQg1cCI0+nGO6y/p3lBbAQbZr9QztZtfgmxXANV+VGoGv9Q9uqTXqelgJd3KY XFkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RpfZ1dWC; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id ix11-20020a170902f80b00b00163fa0cf3c2si18222445plb.224.2022.06.05.22.57.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 22:57:42 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RpfZ1dWC; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0DEA42E435A; Sun, 5 Jun 2022 21:46:11 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345844AbiFCR5A (ORCPT + 99 others); Fri, 3 Jun 2022 13:57:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346481AbiFCRvN (ORCPT ); Fri, 3 Jun 2022 13:51:13 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D051454FB7; Fri, 3 Jun 2022 10:48:24 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 773D9B82189; Fri, 3 Jun 2022 17:48:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CA2BFC3411C; Fri, 3 Jun 2022 17:48:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654278502; bh=dqFrCjkKesUQuBXwMi30MVJWQe5C7sFAW1S0r0P/XyE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RpfZ1dWCbJ8E0e6MA3nqzkRpLM3Afr4n3JHJt89rPw3YBn/umaQe2MrmayzGunhRw UtvdvUYIRh7nl9x+zJdgsVYsK3+kXP3Eh+AJ/mVqHWbYEI/BQ1H3BJPRqc0qeuLmGI /ganWLEOKYc3HvUgc4wz2+iyy/Txtf2vHRVGE8Uw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yuntao Wang , Alexei Starovoitov Subject: [PATCH 5.10 52/53] bpf: Fix potential array overflow in bpf_trampoline_get_progs() Date: Fri, 3 Jun 2022 19:43:37 +0200 Message-Id: <20220603173820.230531699@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220603173818.716010877@linuxfoundation.org> References: <20220603173818.716010877@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 From: Yuntao Wang commit a2aa95b71c9bbec793b5c5fa50f0a80d882b3e8d upstream. The cnt value in the 'cnt >= BPF_MAX_TRAMP_PROGS' check does not include BPF_TRAMP_MODIFY_RETURN bpf programs, so the number of the attached BPF_TRAMP_MODIFY_RETURN bpf programs in a trampoline can exceed BPF_MAX_TRAMP_PROGS. When this happens, the assignment '*progs++ = aux->prog' in bpf_trampoline_get_progs() will cause progs array overflow as the progs field in the bpf_tramp_progs struct can only hold at most BPF_MAX_TRAMP_PROGS bpf programs. Fixes: 88fd9e5352fe ("bpf: Refactor trampoline update code") Signed-off-by: Yuntao Wang Link: https://lore.kernel.org/r/20220430130803.210624-1-ytcoode@gmail.com Signed-off-by: Alexei Starovoitov Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/trampoline.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -378,7 +378,7 @@ int bpf_trampoline_link_prog(struct bpf_ { enum bpf_tramp_prog_type kind; int err = 0; - int cnt; + int cnt = 0, i; kind = bpf_attach_type_to_tramp(prog); mutex_lock(&tr->mutex); @@ -389,7 +389,10 @@ int bpf_trampoline_link_prog(struct bpf_ err = -EBUSY; goto out; } - cnt = tr->progs_cnt[BPF_TRAMP_FENTRY] + tr->progs_cnt[BPF_TRAMP_FEXIT]; + + for (i = 0; i < BPF_TRAMP_MAX; i++) + cnt += tr->progs_cnt[i]; + if (kind == BPF_TRAMP_REPLACE) { /* Cannot attach extension if fentry/fexit are in use. */ if (cnt) { @@ -467,16 +470,19 @@ out: void bpf_trampoline_put(struct bpf_trampoline *tr) { + int i; + if (!tr) return; mutex_lock(&trampoline_mutex); if (!refcount_dec_and_test(&tr->refcnt)) goto out; WARN_ON_ONCE(mutex_is_locked(&tr->mutex)); - if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FENTRY]))) - goto out; - if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT]))) - goto out; + + for (i = 0; i < BPF_TRAMP_MAX; i++) + if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i]))) + goto out; + /* This code will be executed even when the last bpf_tramp_image * is alive. All progs are detached from the trampoline and the * trampoline image is patched with jmp into epilogue to skip