Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3028284rwb; Mon, 15 Aug 2022 16:26:44 -0700 (PDT) X-Google-Smtp-Source: AA6agR5WgkL3noHtdjwofjSF/Vjj7uTlYFo2rZFucO8L5TCWvWn7RwdJiQ6GUWTToeG//IARC83p X-Received: by 2002:a17:906:4796:b0:734:b5b5:96ed with SMTP id cw22-20020a170906479600b00734b5b596edmr11608589ejc.251.1660605889980; Mon, 15 Aug 2022 16:24:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660605889; cv=none; d=google.com; s=arc-20160816; b=qSOwt5Er5Hmm2Odp4rMKGLextidiT7k9qkdqvz5Ga71zfYMVsUD8ZkSprmHcl2iNn3 TEvyeCsoJihFQ3BffDreugbOXm5wDpyOvuPqSpckIKIIJ+YRv0o1ZFNPgaiixhPQTIh1 KnSmwQcnjKU8PsizGs0WNoY5udqg5joJoqDnjBqSpMN8MrVw3OMN7H0vxZMYlBOtBHAq PBCKSbhIhX2cHn3q27212g+CjiV6e1OoHTjiGLNSc6Jlw7YC14Xn/W/sTGZH2Do2R5gU 4B8dL7fLvrc8pznL3hXPMaXTILJRDeOXbBd5rx5W02iGpbXkqIlnG302deVmsJ0YE77U Raaw== 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=Q2aX3xrk0/ZFOlRHFGM4KYcaxAHXyrE6jQHsqqsBzpY=; b=fe+M2atSbXz1VhYle1Yq4nUq9xIpbTa2sWj1vilce0F6KhsDN5saFtP4IMlDICEFsb Or422b/IyQq9zdVd8J9Ye4YgvzBg20qwZAlqDs3mZUnimnS+87U6AkxrSgBpllOiPUE0 h4EVLQEy0lz2wDvB4bdhQWeqndb0tGrikoBia/BV8RYnHa2/fZB0hFoY5HIqub09wPvu LwQeOn7Mm5owY43BsOLDF9X7GKhgHVfm1fU6kQ4t+FHta4GFq8xqK5DhoFBnqFPi2IuJ smCWzOh+oyM8jgAJpxH7tw6ogQZztk9z3NPx+NkrgODh5+8i6D+ZSFBPOZ9Mcw8Q7tdI UTrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=NjWIL1xt; 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 qf6-20020a1709077f0600b00730babc11e3si11148460ejc.640.2022.08.15.16.24.24; Mon, 15 Aug 2022 16:24:49 -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=NjWIL1xt; 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 S1350704AbiHOWk2 (ORCPT + 99 others); Mon, 15 Aug 2022 18:40:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349057AbiHOWfM (ORCPT ); Mon, 15 Aug 2022 18:35:12 -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 0219972B73; Mon, 15 Aug 2022 12:49:53 -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 C6F0CB81151; Mon, 15 Aug 2022 19:49:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 11D43C433D6; Mon, 15 Aug 2022 19:49:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660592961; bh=D3RJWswlgttNeCNoxN0JrftIQMlO2blD6GKPcxf8Rqo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NjWIL1xtygkICe9IPJuw4HzBR3h8ZiZAXsqz3zTPz0yLb99nEhKq5NhBOdly72CCW OrPwBTZQ5fxD0yJLy6caEZzDvXahbxv8I2k7pIij1r4wlScaQA2cMwJfLR5lwsIrqb fbM/O+/peVSKgpz2rB9k7oP2OjhS23A41RXIRD6U= 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.19 0193/1157] selinux: fix memleak in security_read_state_kernel() Date: Mon, 15 Aug 2022 19:52:29 +0200 Message-Id: <20220815180447.464001926@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@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 69b2734311a6..fe5fcf571c56 100644 --- a/security/selinux/ss/services.c +++ b/security/selinux/ss/services.c @@ -4048,6 +4048,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( @@ -4060,5 +4061,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