Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4435267ioa; Wed, 27 Apr 2022 03:57:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgf5Ai1JGJvDr8G0lbZliFpnVS5tB28H27MqJXHv5oZ1AbyuPaarFtId6FpHOUIJAkTrSp X-Received: by 2002:a63:5917:0:b0:39c:c450:3143 with SMTP id n23-20020a635917000000b0039cc4503143mr23397838pgb.531.1651057034963; Wed, 27 Apr 2022 03:57:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651057034; cv=none; d=google.com; s=arc-20160816; b=Y08v92EgEDhUJRFpSySXRNzKrY6HPgvjX7QGzqPni9dtU/S3Fg2mjGHSIKBpz7H8zJ Cive+7wRmdT3Px8UYO2XqgeF900/45JakqlKLMG0s6KaEDBLfFC6Wddfx/DyusFL/hWE T02qLOiIM8znCTtCyFa71gtHi6RbcGJ+o3YNg/mjCMqZdlW6GAHCRivm8yTLYBJt2HFp LR1Liaz9mLQ5oZUBr3f7hrEsOdBxRTndH8R/9XAf+GqdfMRizcIYFOrM3l9N9IO/vhau Y7+8Y6S2cRS2fP7FYcARtPKK87yaOAfTK0PoOYFRs3y3JBku/x29J0/ptUbUhIOgeHGr VOhQ== 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=7IHYOrp/nSjRloO8PNT3hdFRKb8I37kygg9SAn2zGE8=; b=Zy3h2x7EXu+W2aCuIhVE/PpVE3t25qq6GZY+SjM3pc5vYMprwYmL6884lRpgxL+L+c yCrPeZq2ehT0Vj8M560vq3f/1pzsCUNGEuqnLoLzqtHxdETxJEnFMzDHl1MYpOMrme0Y u3lkdif2UnbN991JQNXiTQRhlLbtCKDnFjEMr9fklYwWPp8nShJU/DgD7z1RkBsELuLJ BhWcNol8158EAaci5wM+Psd0pTcSwjZYFDsJFBgKFgEoKTkobUFkg104R73ClF+iHFl+ 54lAMC5Skeu/T/i/5gL4kYZzkiry7DRF3S4XEYfL50TloUKRSrewtqS3iWJmcsu/gUmt abgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2iIJfjvt; 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 o1-20020a170902778100b00153bfa012bdsi1220054pll.378.2022.04.27.03.57.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 03:57:14 -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=2iIJfjvt; 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 out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6B7DA2E8836; Wed, 27 Apr 2022 03:04:09 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346101AbiDZIkB (ORCPT + 99 others); Tue, 26 Apr 2022 04:40:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345011AbiDZIfO (ORCPT ); Tue, 26 Apr 2022 04:35:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6016A7EA3B; Tue, 26 Apr 2022 01:28:28 -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 dfw.source.kernel.org (Postfix) with ESMTPS id F094C61864; Tue, 26 Apr 2022 08:28:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E28B8C385A0; Tue, 26 Apr 2022 08:28:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650961707; bh=wh0OcA9aveSrPKf5oWq/pQ8oMzPGZYv3kAAFOyWM4Qc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2iIJfjvtbGSQFbDqdB266rWhhGKSJ7ud/oCFbrc/n9PsEXEF3hpqHrJaORuRnDSmM pFPfc+5KeukYErChHUHYhy4T8+WAuMkBhPaLNJDG9sREg1eAXMFwTwBnzaarIuxs3p RjMSNrazpoivL2b9aFCxrUpc+n9JiAZnQOWJ1Pys= 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 4.19 34/53] openvswitch: fix OOB access in reserve_sfa_size() Date: Tue, 26 Apr 2022 10:21:14 +0200 Message-Id: <20220426081736.649729069@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220426081735.651926456@linuxfoundation.org> References: <20220426081735.651926456@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 @@ -2316,7 +2316,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);