Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3408157rwa; Tue, 23 Aug 2022 04:24:00 -0700 (PDT) X-Google-Smtp-Source: AA6agR7JtVokiIexrGaQOK2YefhkL0mhsgMl0wUU0lCTpRopF5YSIiJjVzMwHRYZNiPidtM8k/mB X-Received: by 2002:a17:90b:4c52:b0:1f6:3a26:9f08 with SMTP id np18-20020a17090b4c5200b001f63a269f08mr2901538pjb.206.1661253840085; Tue, 23 Aug 2022 04:24:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661253840; cv=none; d=google.com; s=arc-20160816; b=uO0Exe8r+jTF86LoJshUpYn1CsBBrisKuUIn2DBZ3RVkl0kaSvU5N8RoSRhC2Fsem3 VyyZQvlWlXUPAw5elfTUNr+KMutZFJz0DkwKQSTkeU0V0AASbK1YvSFwmBAx/tfvilwu TMSZCHxgMgCNCvxKIJaCFzcnR0gffdgN36vuOOivMtBiu/7yfZUkZZelyGwlSBp2OjBK F2XywRrkyBkC70NmcUC/MlyQSaNpocIZwx7RaFSjTLXbguYzX6o+h3gAvfbKhc7/W2mW t3/urHm8ys3NPN/GT/MtszeTJGpAo3JUqncwx8Zzb2z6poKLT1h3qYZS+r1xvWVlFcwc uKcg== 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=snaICVKbySpxJe/U0d8rBgWCeq2FqAIC6BCgDBjj5tb+0mqX5b6YiBPQV4CtRek4MA 9EF9AcWhl9xqJ4njeZIrJM7SlHfAtfaNJH2qLOTKbAEy/exId3oUCTmp2CFJDP/GI0Fq EGy8XU4qRjPoQO5NPCm+CllyMR+jvjJsaYT53QFpeLOlwHc2J7+H/7lvTN+2Wl/X38GE 2x0kF0W90z3r3SmgXSfID1du8oxt2IqVaNjSAOs6YyoJhVA5qIHE5mf99qe4nOD7ulC2 AAm+nf+0httkjXhBgwLNxRUke5OPVH/LU6X5xH6ywTw/S8gF9tDEhTcyz1VoXHFmF7C1 W85w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QJo5bn2c; 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 v128-20020a626186000000b0052b8a12a057si13791561pfb.287.2022.08.23.04.23.45; Tue, 23 Aug 2022 04:24:00 -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=QJo5bn2c; 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 S1353427AbiHWKPD (ORCPT + 99 others); Tue, 23 Aug 2022 06:15:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352646AbiHWKGE (ORCPT ); Tue, 23 Aug 2022 06:06:04 -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 85BB56308; Tue, 23 Aug 2022 01:52:20 -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 3E626B8105C; Tue, 23 Aug 2022 08:52:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 85167C433C1; Tue, 23 Aug 2022 08:52:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661244737; bh=f6OXRID0uCEqVUMBcp3Ki0tg3pVmjI2z9M8ylE8p0OY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QJo5bn2c7MGKGiGbzif5OiF05wMd5hvpH56CdH3YWQWGLBlkng3s5A5peQmsMY8cc FSdajP7FkQbj8UiVq5G56Dwo8tC3/lTa+Qs077m99O+p0QXCtOWc9TduH2FxlqIcJg XpHPlTf+IUyUMQbkQmFjh4tZcu7FzTBs0B6G6TFo= 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.15 141/244] net: genl: fix error path memory leak in policy dumping Date: Tue, 23 Aug 2022 10:25:00 +0200 Message-Id: <20220823080103.868849234@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080059.091088642@linuxfoundation.org> References: <20220823080059.091088642@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