Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4446179ioa; Wed, 27 Apr 2022 04:10:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyBk/AHbpG3Ke6CZtsxMAmMYDjwDuY+W/qHgoyo/2JjGoGnNtleLsAUe5XQXUzr+Pw32Y9Z X-Received: by 2002:a17:902:e542:b0:15d:1ba:78d9 with SMTP id n2-20020a170902e54200b0015d01ba78d9mr17346036plf.107.1651057833698; Wed, 27 Apr 2022 04:10:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651057833; cv=none; d=google.com; s=arc-20160816; b=C6Zd0hzCwMNXMx3bA0vK2a7O2zuPrgG7cAU9rHOutqmVX9DXe8TNRApvNffGaTf8Uy wtLzy/Qa7K27s97yR/y61PwiA/J9Z2EEUFdENga89mhQ/zOuXoV+Aec4r/I9nClaR2ZV elU0xc0cmqqqnfZBEMmKIQew6am/8NbOjkqhAhCqdy/ZwRkRke4mVnXZyqrPyylkCcBR bIjeqPkqxYBCVV526rWKGtaJ9GMsHbIkeIYeGSgZGVfzW/yM+8RPGJaN8rICgJPJ45iL SSAveSXjJyM0XjseGmVZqENcPzuzt/nZQgM9uJ9Ugj4U63J+oF+CU0wRcFEx2uZzzoQ9 fJ2A== 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=lrfGiDA7LKLzqywwEjBDvq3VUelinIeu/QBZFQa/rMQIC+nd1DC7V8CiISqKl2Cvwm sJpgY6NRScJR44ml5qlvFjQwc6PXEn/RV2gtJCrtr90Bl+sRVAlBVvy+v/G7+totWow7 9ylgX9yItpEyiFEmnvbwvuidO69IY88mfUR60oCgUZ03NOHAVE/Eyu/md6g/enfoiTF8 pFpx8pzMU8GzpvGoVbECdForY1Cy8DCDRPztCn2S7B4zW0Dt+LdQiCZLdvWahH2jE2UU e1I0OzGGeAMYlbAtfe6qu/wR5IYy0I1IVfEcHx27GKlU4V8knDts1nIYPz9syvT+EwRP dUUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GtSMmYS2; 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 j34-20020a635962000000b0039924bcb6a9si1268532pgm.172.2022.04.27.04.10.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 04:10:33 -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=GtSMmYS2; 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 348252E7087; Wed, 27 Apr 2022 03:03:48 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348179AbiDZJPS (ORCPT + 99 others); Tue, 26 Apr 2022 05:15:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345519AbiDZIws (ORCPT ); Tue, 26 Apr 2022 04:52:48 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B153DD64F9; Tue, 26 Apr 2022 01:41: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 6FE2EB81CF2; Tue, 26 Apr 2022 08:40:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D0D9DC385A4; Tue, 26 Apr 2022 08:40:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650962451; bh=nQ2HJD5nf4oTxFG5OTLGaKQFzVChDheTQyaznQhlggM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GtSMmYS23Tz9kr33IyQ2SS052WqWsQjFTUi9VHPN+A4U++PEL5eAnkY093pFNc3Du j4ZZhxolP4e8T6Gj/U+c+iQN1IDVSlZdDmpcuAGd2CIX7il8id+bFTblxy8y1bqo7r xkxkSmvFSq2NgkrkMj2hbborRvmvSXmgNMsNDmKA= 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.15 101/124] openvswitch: fix OOB access in reserve_sfa_size() Date: Tue, 26 Apr 2022 10:21:42 +0200 Message-Id: <20220426081750.169334421@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220426081747.286685339@linuxfoundation.org> References: <20220426081747.286685339@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);