Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3617468pxb; Mon, 24 Jan 2022 13:37:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJyGYg473pivYG+5XGPyHmHIi86AYfii5JRiP+Pj2ocRexLeWZHHPkOTaQeOdW/megXm+Uq9 X-Received: by 2002:a05:6a00:1a15:b0:4c1:ec02:640 with SMTP id g21-20020a056a001a1500b004c1ec020640mr15757070pfv.81.1643060242202; Mon, 24 Jan 2022 13:37:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643060242; cv=none; d=google.com; s=arc-20160816; b=AlsfAZIu0cD2eBoB0ZwOW7UqqHrFFCojbevTWgNtOOSLtH3iSgqKNZA54lpO7Qq0ds m+gf83cMyMMu04/YQYbw4VknCgv17g8cyhJZ08sh5qKqbBhh09B8S34eC0NrTmVRPzE6 J8yOyn8iuFh059wmNnxNRG8QnpMvJSXdpfpacAh7LcRCcXsNx+f9pOCYcXuu2wHp+xNa 4pigbKN9CtqVxOd6DyqhEYoCa85GHrTCFSZwMJXaHK+u30VT9X+2ACdkxJcbVyUPfqn7 I3EOhOYBF0s8SbULw/G4f3pfvSjRRo8q5jRDoGr3hN3nf7CXzvy+r68pm8D7RTOEp8ZQ I3Fw== 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=sh6/i0ISbl4Bstt+X5wKW7r4uV8i9XV90FlJ0UDnuFM=; b=xWfI5GuIWBU5V5do2aP8GwOx8bu+KnGNUhmaXtf5g2BBapwk36mOl3B3a0pvVp5QVw bKOtk6hi5dAwfTpon6GhmbgljG4Y6KiGSQJQzlkOUcers0U3nehAo5Fdq1sDrmXVR+6l 3Wg3zYkPXWf+keBALP4D963S0qUqiDbxp/Nhi3jA7jZcxOa100vUEFrh+4f7ARcCAP4o hAvHmxFfsDi3tdKK8vuDH/I0m1tML3+xz1VtgNN1BzuQoJoQmLZ0z8KfCA7frfkBOy5m pBb6wbprz0maYtUtLYjO+2V5AjyQOYNdLgpM4G26BH3FoZjJxhV7Tph9JJ72M8y+IqaO zn9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="r345/yzN"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y23si2353570pfa.375.2022.01.24.13.37.10; Mon, 24 Jan 2022 13:37:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="r345/yzN"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1355652AbiAXVNM (ORCPT + 99 others); Mon, 24 Jan 2022 16:13:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1387549AbiAXUgz (ORCPT ); Mon, 24 Jan 2022 15:36:55 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76737C02B8CE; Mon, 24 Jan 2022 11:50:10 -0800 (PST) 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 EDBACB811F9; Mon, 24 Jan 2022 19:50:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 23B2AC340E5; Mon, 24 Jan 2022 19:50:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643053807; bh=Z4WqW7aiWhpSpvVTjLKFKzmmQSWflf5Gz0MzVnFB0Xs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r345/yzNxGFQa7OADeUtBrjg7IZY1Oil2SEMPU+FjbOmm9x8akedWawS9hyYPWJXw Z/3lcfwSwFbJrjpvjUjyDsaFqLooZRk0bXOzDGd5mjbfacojHBNnbK4WEG/3mZ1iGv etZihHhNrZyIgk6RSf7GYOgKuPerhOlUHhgHrq9M= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Bernard Zhao , Paul Moore , Sasha Levin Subject: [PATCH 5.10 184/563] selinux: fix potential memleak in selinux_add_opt() Date: Mon, 24 Jan 2022 19:39:09 +0100 Message-Id: <20220124184030.782697105@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184024.407936072@linuxfoundation.org> References: <20220124184024.407936072@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bernard Zhao [ Upstream commit 2e08df3c7c4e4e74e3dd5104c100f0bf6288aaa8 ] This patch try to fix potential memleak in error branch. Fixes: ba6418623385 ("selinux: new helper - selinux_add_opt()") Signed-off-by: Bernard Zhao [PM: tweak the subject line, add Fixes tag] Signed-off-by: Paul Moore Signed-off-by: Sasha Levin --- security/selinux/hooks.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index ff2191ae53528..86159b32921cc 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -947,18 +947,22 @@ out: static int selinux_add_opt(int token, const char *s, void **mnt_opts) { struct selinux_mnt_opts *opts = *mnt_opts; + bool is_alloc_opts = false; if (token == Opt_seclabel) /* eaten and completely ignored */ return 0; + if (!s) + return -ENOMEM; + if (!opts) { opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); if (!opts) return -ENOMEM; *mnt_opts = opts; + is_alloc_opts = true; } - if (!s) - return -ENOMEM; + switch (token) { case Opt_context: if (opts->context || opts->defcontext) @@ -983,6 +987,10 @@ static int selinux_add_opt(int token, const char *s, void **mnt_opts) } return 0; Einval: + if (is_alloc_opts) { + kfree(opts); + *mnt_opts = NULL; + } pr_warn(SEL_MOUNT_FAIL_MSG); return -EINVAL; } -- 2.34.1