Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3005112rwb; Mon, 15 Aug 2022 16:00:27 -0700 (PDT) X-Google-Smtp-Source: AA6agR7jlYu5ik/Bw0tuvyqGIJyArgJ5c952L/Os+JbXqoJhEp41xuZcZH4TtEJcvYNkDxQ/QL+D X-Received: by 2002:a05:6a00:148f:b0:52d:4524:8622 with SMTP id v15-20020a056a00148f00b0052d45248622mr18570913pfu.84.1660604427280; Mon, 15 Aug 2022 16:00:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660604427; cv=none; d=google.com; s=arc-20160816; b=zWayhbiOLZ5W7lZ1zxDPEr/RRL+o6xE8X57Qr3GluWui28WHhVaeXGF9QmPvaASgLI sIi9AR3DhSQZbi/+ROqo+/Q5rAYb/gjkvHhRDvv3lnupp630ztCTJS/2Cg8LmxNfS3sJ NFDRE/f1SPnHn8mzZqtEQT0qXDBYjivt798NaQxFDLJqnVi8jUEyJsZviw+i4Bfhgf7p 90SNntfl5fMVwOh+MeEEx0GcxBJEs/X8rXYDECGiNjOTA8uAx9mO6FCCApDDKx6/PdGa J+PW0t/OjKAc9+S8NYXmJsKCGekKX5ap3pz5l5jgBM3kx2CSeK9wrv/EwUTuD1kzMfo6 pVDg== 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=OeuL69YndelPTFW21Aq5SGLn1uJY9xxIYd46VYavxd8=; b=PtcbISWp2q2HbDWMExABK5Efbh5dcj3MCPmzclGqkzaeKqPDNBDMRcNvoVY3QB1/D8 GJAVeI1e75rhckJFepG4L7D2woTvUZixF43rRS3l7S/wZlUqek9r4obeZth8Zu8UDqnJ /FlVgnZfnBPG1C6uykiVEMh3JSXNN0B4LHqxEQagI419Lx9K67a95TdvGoujSbyfrZxW qApsaB4myPvnWHiotap52Nt7UsPZ5S5QIjWwpV2rVcyF1P9YCPI0CjO+5D++NQiIDuzH YCJAvwCB/22xmDIKI+catSGTxyApFaLK1UAgzZL+uFZZjCntJRyrZKVoKrMjy2HM+Mbx joLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="bOF/BLmJ"; 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 12-20020a63104c000000b0041998718e9bsi11889539pgq.528.2022.08.15.16.00.11; Mon, 15 Aug 2022 16:00:27 -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="bOF/BLmJ"; 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 S1347839AbiHOUbo (ORCPT + 99 others); Mon, 15 Aug 2022 16:31:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347346AbiHOUZH (ORCPT ); Mon, 15 Aug 2022 16:25:07 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 488AF9AFE3; Mon, 15 Aug 2022 12:03:13 -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 20FD861281; Mon, 15 Aug 2022 19:03:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21E8FC433C1; Mon, 15 Aug 2022 19:02:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660590180; bh=FlO2+rc4HsuR7lvaoZ66kubdG1FhTpV3XY3NhuCS+jk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bOF/BLmJ+js4eyKcO+h5vqvSV3K6g7PgMm71rRKG5WfEEDUCiufKCMR6CBJEVU4QJ PABaR7JzY4juvcZLxVXvdZd6cCmvccdRtow/JtWOJA7/KloAXxpQu7VypvyV5Nw0Do cjirQJZqLybdi4KQ70LyLfAPkcMhpUK4OCc7vvB8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xiu Jianfeng , Paul Moore , Sasha Levin Subject: [PATCH 5.18 0178/1095] selinux: fix memleak in security_read_state_kernel() Date: Mon, 15 Aug 2022 19:52:57 +0200 Message-Id: <20220815180437.001001168@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@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: Xiu Jianfeng [ Upstream commit 73de1befcc53a7c68b0c5e76b9b5ac41c517760f ] In this function, it directly returns the result of __security_read_policy without freeing the allocated memory in *data, cause memory leak issue, so free the memory if __security_read_policy failed. Signed-off-by: Xiu Jianfeng [PM: subject line tweak] Signed-off-by: Paul Moore Signed-off-by: Sasha Levin --- security/selinux/ss/services.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/security/selinux/ss/services.c b/security/selinux/ss/services.c index 6901dc07680d..cad54f454d01 100644 --- a/security/selinux/ss/services.c +++ b/security/selinux/ss/services.c @@ -4049,6 +4049,7 @@ int security_read_policy(struct selinux_state *state, int security_read_state_kernel(struct selinux_state *state, void **data, size_t *len) { + int err; struct selinux_policy *policy; policy = rcu_dereference_protected( @@ -4061,5 +4062,11 @@ int security_read_state_kernel(struct selinux_state *state, if (!*data) return -ENOMEM; - return __security_read_policy(policy, *data, len); + err = __security_read_policy(policy, *data, len); + if (err) { + vfree(*data); + *data = NULL; + *len = 0; + } + return err; } -- 2.35.1