Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3294687rwa; Tue, 23 Aug 2022 02:10:15 -0700 (PDT) X-Google-Smtp-Source: AA6agR6VklPSxvq2xlO1pbvOawvSSMMfnCCgelUelBns5l64SwVZC0NF/kixMqzDLF5Ew0SVArTP X-Received: by 2002:a17:907:72ce:b0:73d:a98b:5c8 with SMTP id du14-20020a17090772ce00b0073da98b05c8mr37184ejc.648.1661245815537; Tue, 23 Aug 2022 02:10:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661245815; cv=none; d=google.com; s=arc-20160816; b=ygFeK2RtahfDc3ILM6SJZlX30v3Hy7+Jyabx0hzcR404yaoZe9IFBfsTMZJTrV2MhE BSl2QP6HaXcczx7P6czINF7K3SBjHKtvbBbUxq+ysRAYoNBNjEUnl8+rz6HUQaZ2dBQB 7iQ3FEcGgAtGkknozNcrZbBZG+vI2dYLm+gWKTQqNBgCTbWpakADSGEZSSxCQXN9I99w qt0hh3Md3uNa+bw01vN+gnEPFuW0NBnmjp/IfRa0qgYTRV9qdl89r3eoIs3rsZqLgzP5 X0pUrnlAtMKRJkP9ZbJsp2oWlLI2ebyvc4iP84hXGj96H6TveSm9JoID7xZUuf0jDWEh fjzw== 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=NQo3yxGb40nKqR05WyfcxEe2OAlDg9fBcO0Wm2In904=; b=szUjFT2ABoPug6v6/EoliAkD8WJXfxCa+RyZ3V/fydIFrXRTTkY3NngN9CDSgTQXcW ZYJG/XW8AhZiETbSxD34W7/AG6UmqNWHNj5moalhNU5SmfPunLW+KZ8WG8r4CtwO/Mlb bXe0U4ZBpx5lqonxvVPmoatOCmEGDRBYksykpriXmdRhWEzIviWVTodjo53TycHOW5F6 AKzM+b/X3Tmw29CwFG0JDMCr8iC5btKj6mx+cSFVmRNHOy9bzOEtylv2lGNRAw41MlgN e+Xe71WBicb8yt3Mgifj3n4TOcvlCAllExY8X5c1fmgDylAUFf4aF0bfSJONwJE98Xpi pYCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EYfLJNgE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s4-20020a17090699c400b0073d19e6ce0bsi8903604ejn.301.2022.08.23.02.09.45; Tue, 23 Aug 2022 02:10:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EYfLJNgE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242416AbiHWJEo (ORCPT + 99 others); Tue, 23 Aug 2022 05:04:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242765AbiHWJD5 (ORCPT ); Tue, 23 Aug 2022 05:03:57 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FF861C107; Tue, 23 Aug 2022 01:28:40 -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 6F5986132D; Tue, 23 Aug 2022 08:27:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 80F8CC433D6; Tue, 23 Aug 2022 08:27:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661243255; bh=f6OXRID0uCEqVUMBcp3Ki0tg3pVmjI2z9M8ylE8p0OY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EYfLJNgECBwzL955JzDH34ZdKWYXmFIjyycW7GXJcIw8vVXHr94dgwxa7zPuiny0F FABfIqga23h2HDr1ox1soHprfdpM6RH4Ety0iBA+N+oYtoD6gmaj5WEUlVIKAYUlcW pB5/bdRxETVDmfttMOzYslXU/81ezE7qdTBaii5w= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+dc54d9ba8153b216cae0@syzkaller.appspotmail.com, Jakub Kicinski Subject: [PATCH 5.19 218/365] net: genl: fix error path memory leak in policy dumping Date: Tue, 23 Aug 2022 10:01:59 +0200 Message-Id: <20220823080127.307948327@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080118.128342613@linuxfoundation.org> References: <20220823080118.128342613@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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: Jakub Kicinski commit 249801360db3dec4f73768c502192020bfddeacc upstream. If construction of the array of policies fails when recording non-first policy we need to unwind. netlink_policy_dump_add_policy() itself also needs fixing as it currently gives up on error without recording the allocated pointer in the pstate pointer. Reported-by: syzbot+dc54d9ba8153b216cae0@syzkaller.appspotmail.com Fixes: 50a896cf2d6f ("genetlink: properly support per-op policy dumping") Link: https://lore.kernel.org/r/20220816161939.577583-1-kuba@kernel.org Signed-off-by: Jakub Kicinski Signed-off-by: Greg Kroah-Hartman --- net/netlink/genetlink.c | 6 +++++- net/netlink/policy.c | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) --- a/net/netlink/genetlink.c +++ b/net/netlink/genetlink.c @@ -1174,13 +1174,17 @@ static int ctrl_dumppolicy_start(struct op.policy, op.maxattr); if (err) - return err; + goto err_free_state; } } if (!ctx->state) return -ENODATA; return 0; + +err_free_state: + netlink_policy_dump_free(ctx->state); + return err; } static void *ctrl_dumppolicy_prep(struct sk_buff *skb, --- a/net/netlink/policy.c +++ b/net/netlink/policy.c @@ -144,7 +144,7 @@ int netlink_policy_dump_add_policy(struc err = add_policy(&state, policy, maxtype); if (err) - return err; + goto err_try_undo; for (policy_idx = 0; policy_idx < state->n_alloc && state->policies[policy_idx].policy; @@ -164,7 +164,7 @@ int netlink_policy_dump_add_policy(struc policy[type].nested_policy, policy[type].len); if (err) - return err; + goto err_try_undo; break; default: break; @@ -174,6 +174,16 @@ int netlink_policy_dump_add_policy(struc *pstate = state; return 0; + +err_try_undo: + /* Try to preserve reasonable unwind semantics - if we're starting from + * scratch clean up fully, otherwise record what we got and caller will. + */ + if (!*pstate) + netlink_policy_dump_free(state); + else + *pstate = state; + return err; } static bool