Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2884810rwb; Mon, 15 Aug 2022 13:17:20 -0700 (PDT) X-Google-Smtp-Source: AA6agR65NsqrqWS7a4i6GcTU0aD6cSzT89ML5JcMXiNZ9cnbvqGyEXE18JU48ARSG/U1k6w3s7iv X-Received: by 2002:a05:6402:1618:b0:440:ced0:e555 with SMTP id f24-20020a056402161800b00440ced0e555mr16377576edv.68.1660594639816; Mon, 15 Aug 2022 13:17:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660594639; cv=none; d=google.com; s=arc-20160816; b=A7PrJ7xwK5GprUFwlH268OimO0nOT7sS/7uCujNUZGsrZqn6S2cA9E6jc6214GfA7A 2zSWKdebzDyd66ll8nX1VgB8H0HAw2mg7hY6traVxNnXxXIEyBBRHngRhsXcsW7vSS0H d0fdoloz72vRyfgfZKfteWx+lyhapKTZJ5Y2uTfnVtCjf7UAytfzn8MTWbyWsA+p4WqV GnIyrl06M05vsBqletWXUxHdc0rmwBVkSRgaxJ2Y0d/AvtTozBgmvrtgM+Nso9I0yt7d bzWcQ+rDXrlRq5rItXgeCcfD3WN+s+bz7NwhQv/advpfkKkOpKh46jWNtwz1YJMJPAHt gOnw== 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=RWCaf6K2gD47hpazXCfwV81gYegaQt/DfKN7UKj7Nmc=; b=VUQHmPzn8JE798f+dxTvpoXP/hpy5L5PV+TI5QSiWNbaCoYBZmVEaLafarLC42tRcR svaNMyZehae3fcIchV4+2tlUsAtlVHTeHEaE+iXtIWMiOLdtSpxIIwYvm06KeIDF/hwZ YHt7gJtXcREuAbw0mXwpMpwuRCTurNK8gDircj9+YiWMEg87LueqbYGEy2dojFHjrOzm +L412WneLVvL/6NPC3UWoB3+SH7U/mo+xsr+E+BV75+FYvDYR35LK7GNimiHYd9D7CVr RgRZstysJ4kEG6ohn/Ce/VvK7QCc2EGcf+V1NA51XSf3xeijyfqf5QoYz29QwvgxQd0K zgJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=y9GUuTrg; 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 hd14-20020a170907968e00b00702d8b7438csi9204278ejc.326.2022.08.15.13.16.54; Mon, 15 Aug 2022 13:17:19 -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=y9GUuTrg; 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 S242043AbiHOSbg (ORCPT + 99 others); Mon, 15 Aug 2022 14:31:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242848AbiHOSaa (ORCPT ); Mon, 15 Aug 2022 14:30:30 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF8FA2A728; Mon, 15 Aug 2022 11:20:43 -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 312126068D; Mon, 15 Aug 2022 18:20:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 37601C433C1; Mon, 15 Aug 2022 18:20:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660587642; bh=6CRLhn7oCpLe3R4QHpr0/0MYXVi1LdC9PER/71MjKW4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=y9GUuTrgkjDlfmiU1v8il9U2Yppm/voxFmAX+KXH6NcRCvOV7a6lPAwTzf0Z3cGYc myT0qdAIqnFObbXz/iZCwLe2duvMe2nZaYU5XaKTAhHHFu2umNf47dS3cW6s2wPy3i eQRQHJPX8M98yoWDAE0mszv0Xye5NV7m9dW/BzzY= 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.15 148/779] selinux: fix memleak in security_read_state_kernel() Date: Mon, 15 Aug 2022 19:56:32 +0200 Message-Id: <20220815180343.640854417@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@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 c4931bf6f92a..e8035e4876df 100644 --- a/security/selinux/ss/services.c +++ b/security/selinux/ss/services.c @@ -4045,6 +4045,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( @@ -4057,5 +4058,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