Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4457678ioa; Wed, 27 Apr 2022 04:26:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyy25gGh7kaBE27e0779ij3VmOS1tLO9KEDC8Rih3RHQM4Wvex7InCwplZZg0/c/Mr2gnQ/ X-Received: by 2002:a17:902:9a0a:b0:158:a22a:5448 with SMTP id v10-20020a1709029a0a00b00158a22a5448mr28674494plp.20.1651058761613; Wed, 27 Apr 2022 04:26:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651058761; cv=none; d=google.com; s=arc-20160816; b=i+3emxkth0d84Tp3NQWS9niTN6k3HHUnXVGSrWUrfA0S5yekhosh5bbegGbl+5B32V GWvgKJpJdn18BowRzkGV9iaHScDraFUOu+th6FoWpQ7f5wsnHeXnRitdm7IL6pFl6Wij 7bVmv+cN2J0HvOCv9bkJv3B+2VVrCRtrAEn2dXWXKIk7ak7+umCDLcdUZL68xcaJhSUM dPzOCXn28PJMZx2QtZY48sx4zNt2ymNC7dhsk0DWTS7DoVawS0Behtzr4+D460UBsuQU GWtxrEUU1XYAlUeAmW0SG2sGj7tE+alehvyvEeMivKQlfmhIJ2Uj/lDlw0Cx58hWQ68l L0HA== 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=2qKZijAcG5FnNZWq/yxIDfK6ntD5I+ccdVZumzX2o+Y=; b=fhsEFTLhwmNj67sViTX7h8FiKGb2W75+E+3cp3Wy0xECZD8fHb5ieNepEABcMR03hh PzuSMnhOTZMi0CYCRGa+cHvyDyJI9d6gn9r/ksfNlUP4J3DzmqprSOEDsCO56L5E+KgY fXhh4Mjxs2BTDEtaUtCe2SPoCg2UYbiOf96YligS63VYsz7NizGVQgqfjuLPCRZvgdJF 1aftACb4t+u2NYEMRqcKJFQ0uRYgW9ttRjJ0ldfke8tG/kM59IrtVyFxqhrEdBrCuspK Qc/r2GGbW01Ui7VSCMTaG8ksjkH1qCF4FuHk+CWQSBFojS2lZMHombjTHu6nVYwKpSEj YhFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BxikVOgb; 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 j187-20020a636ec4000000b003ab573bd7dfsi1302894pgc.80.2022.04.27.04.26.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 04:26:01 -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=BxikVOgb; 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 E489B48E84B; Wed, 27 Apr 2022 03:22:38 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348462AbiDZJjz (ORCPT + 99 others); Tue, 26 Apr 2022 05:39:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346041AbiDZJGn (ORCPT ); Tue, 26 Apr 2022 05:06:43 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC2D816C176; Tue, 26 Apr 2022 01:48:01 -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 94D04B81A2F; Tue, 26 Apr 2022 08:48:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D2868C385A0; Tue, 26 Apr 2022 08:47:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650962879; bh=nQ2HJD5nf4oTxFG5OTLGaKQFzVChDheTQyaznQhlggM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BxikVOgbi9IRH98bkYRfUza14uuyED0VbG1fxtc81JT3TFxkCEGOLzxojD9dr8M3n Jn0GNtiHf8WK3/F+aVONKguEsV5EDhTcAvaM5H8/wyODv5kCnBqsGK6TVQxtB33Hzk feB/YmN6SId9KPs15uCFXp0MrAr8JqPFnBaeOso8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Paolo Valerio , Eelco Chaudron , "David S. Miller" Subject: [PATCH 5.17 119/146] openvswitch: fix OOB access in reserve_sfa_size() Date: Tue, 26 Apr 2022 10:21:54 +0200 Message-Id: <20220426081753.401779005@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220426081750.051179617@linuxfoundation.org> References: <20220426081750.051179617@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=-2.5 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 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: Paolo Valerio commit cefa91b2332d7009bc0be5d951d6cbbf349f90f8 upstream. Given a sufficiently large number of actions, while copying and reserving memory for a new action of a new flow, if next_offset is greater than MAX_ACTIONS_BUFSIZE, the function reserve_sfa_size() does not return -EMSGSIZE as expected, but it allocates MAX_ACTIONS_BUFSIZE bytes increasing actions_len by req_size. This can then lead to an OOB write access, especially when further actions need to be copied. Fix it by rearranging the flow action size check. KASAN splat below: ================================================================== BUG: KASAN: slab-out-of-bounds in reserve_sfa_size+0x1ba/0x380 [openvswitch] Write of size 65360 at addr ffff888147e4001c by task handler15/836 CPU: 1 PID: 836 Comm: handler15 Not tainted 5.18.0-rc1+ #27 ... Call Trace: dump_stack_lvl+0x45/0x5a print_report.cold+0x5e/0x5db ? __lock_text_start+0x8/0x8 ? reserve_sfa_size+0x1ba/0x380 [openvswitch] kasan_report+0xb5/0x130 ? reserve_sfa_size+0x1ba/0x380 [openvswitch] kasan_check_range+0xf5/0x1d0 memcpy+0x39/0x60 reserve_sfa_size+0x1ba/0x380 [openvswitch] __add_action+0x24/0x120 [openvswitch] ovs_nla_add_action+0xe/0x20 [openvswitch] ovs_ct_copy_action+0x29d/0x1130 [openvswitch] ? __kernel_text_address+0xe/0x30 ? unwind_get_return_address+0x56/0xa0 ? create_prof_cpu_mask+0x20/0x20 ? ovs_ct_verify+0xf0/0xf0 [openvswitch] ? prep_compound_page+0x198/0x2a0 ? __kasan_check_byte+0x10/0x40 ? kasan_unpoison+0x40/0x70 ? ksize+0x44/0x60 ? reserve_sfa_size+0x75/0x380 [openvswitch] __ovs_nla_copy_actions+0xc26/0x2070 [openvswitch] ? __zone_watermark_ok+0x420/0x420 ? validate_set.constprop.0+0xc90/0xc90 [openvswitch] ? __alloc_pages+0x1a9/0x3e0 ? __alloc_pages_slowpath.constprop.0+0x1da0/0x1da0 ? unwind_next_frame+0x991/0x1e40 ? __mod_node_page_state+0x99/0x120 ? __mod_lruvec_page_state+0x2e3/0x470 ? __kasan_kmalloc_large+0x90/0xe0 ovs_nla_copy_actions+0x1b4/0x2c0 [openvswitch] ovs_flow_cmd_new+0x3cd/0xb10 [openvswitch] ... Cc: stable@vger.kernel.org Fixes: f28cd2af22a0 ("openvswitch: fix flow actions reallocation") Signed-off-by: Paolo Valerio Acked-by: Eelco Chaudron Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/openvswitch/flow_netlink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/net/openvswitch/flow_netlink.c +++ b/net/openvswitch/flow_netlink.c @@ -2436,7 +2436,7 @@ static struct nlattr *reserve_sfa_size(s new_acts_size = max(next_offset + req_size, ksize(*sfa) * 2); if (new_acts_size > MAX_ACTIONS_BUFSIZE) { - if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size) { + if ((next_offset + req_size) > MAX_ACTIONS_BUFSIZE) { OVS_NLERR(log, "Flow action size exceeds max %u", MAX_ACTIONS_BUFSIZE); return ERR_PTR(-EMSGSIZE);