Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp102144iof; Sun, 5 Jun 2022 22:26:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzZr/+Az79msvzAmqvnDAuNxKgIiK5Jg7GmbtXt8s20BmvfeE5MFQfhFiyWhP4EtXEM6NVl X-Received: by 2002:aa7:88d5:0:b0:518:8ef4:f378 with SMTP id k21-20020aa788d5000000b005188ef4f378mr22731217pff.2.1654493179099; Sun, 05 Jun 2022 22:26:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654493179; cv=none; d=google.com; s=arc-20160816; b=zv2iSPLT+u8U6WMLNbk3Bz3RSFITW6oDcCRQiH/GjOcoDkYkSnEXUbWdPq6pOPlfdF wimSVvBalCPhGCzrED85aIQhcyfNAcRouaWbg6sfuoME81Fs37VutRusB2wfYDOo2rDz XnfbtYVyk0Sid+b9wnmxNu5xozSOw9TkumVqPHjf/nlXFUWr2CehTc4E627vXK05Co7t EO+uEBSHzxFdQavGdq7FASh3Al1efM/4w1GhU3hVLHl/ebcaeWl71ztKqFqdjnTlx+YG mfl18cQTwFd7WeJ43QXHRd14CDJ+B/cCMIouMa5mOfDCYMXt66g8bF8zoW0KnyvZ3V2J 3Xsw== 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=wUPT9Z0bZzQMPrwGaf5TNitaPONgOp9kIi4IQHGPIkY=; b=Qw/SxALnEjOWPVkFgtW9YyRB88gQk1Hp+21dqpSxUKcBjoc6IFBXjQXXr5jwOBFZl1 XsiFNPWVTuX1dik0SG4lqPhIuhdtdBzAFITBkYLwVxBB8WRKWJYSwRnANAwZrjfzlZDl yrr5kJFCyIan7renf5UF7diDI6NU1FCY1ZzdqiXSTLVpZI2Xv+t+gzXmi5mnGfqUm+Ey DbJnzpPF5k/+O0D3hMvh2gBo+YhaXkjOzKTx31X1SlhlPC+Cmsti21eCj0NVYlXlpRiF itGFGI9GdSYoZXDmIHYUV3bgn5o97v9h2SNqZl2HvEiDFDL5ktf6M/5ueaMLAmbjvsyh aycQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MRcBTePm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id u11-20020a170902e80b00b0015d2ca1afcfsi22305847plg.89.2022.06.05.22.26.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 22:26:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MRcBTePm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 664EE1DB1CD; Sun, 5 Jun 2022 21:28:07 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347785AbiFCSVJ (ORCPT + 99 others); Fri, 3 Jun 2022 14:21:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348048AbiFCSQM (ORCPT ); Fri, 3 Jun 2022 14:16:12 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A635662CEB; Fri, 3 Jun 2022 11:03:47 -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 09E7DB82435; Fri, 3 Jun 2022 17:58:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 38258C385A9; Fri, 3 Jun 2022 17:58:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654279123; bh=sO/fkfrAJ5blRicrhVjpzLcwBfyTi7/AnGJfvCfo6Hs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MRcBTePm7ERn55Dq8+oe8MSZeUlwSjKF40P06u3f7NxCX23VuC5E3NJ7D+XXa8RtD ie1bi1KI6GUmabxpqwpkIgb+FcLAHCwvBpbMHbCkmWqCTihkiM6G8wSgbxPmcaSXxB hW2FNEMjUlwU8vQWwo6jXYfcmdT5fFPsEzS+fSdo= 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.18 60/67] bpf: Fix potential array overflow in bpf_trampoline_get_progs() Date: Fri, 3 Jun 2022 19:44:01 +0200 Message-Id: <20220603173822.448594205@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220603173820.731531504@linuxfoundation.org> References: <20220603173820.731531504@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 @@ -411,7 +411,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); @@ -422,7 +422,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) { @@ -500,16 +503,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