Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3701879pxb; Mon, 24 Jan 2022 15:47:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJwwTVDIA7tvIdKqgGqilFz7QfBKgIpVjwq87Codh493YLWI5LeRdfphOnO8jtWCbCxJV+PJ X-Received: by 2002:a63:5b63:: with SMTP id l35mr13484452pgm.406.1643067966618; Mon, 24 Jan 2022 15:46:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643067966; cv=none; d=google.com; s=arc-20160816; b=EqVC5a5JPuDhh1WiNmj/nYQPH3j4JTqi296UdD3eQpxP4zA7kedQlmojNcVvC0q3mH 1MBGqgYQG1ATYeVa5V2lc66xzQnMIdxnqPmbhnoPeDrzrcOyyduWupyCczTDLmlnizYh x2Gb3sPXh04ZiD+ZTRhO1V9e8jApHnDt691JBcBxyDYx/d36hznAou2gytV+MKgBbtF8 6EMeCFe9pD0qtkghOc1+7w1NfFgKBWnoLg/l9oOnyQQDV8R8t8UN/PBUBsRM/1iaBQqX E0fTSCc/A9Lu91WHgb4vqi1XKtC4IYX0ILz6gXZrNasGz8UxlUbs/+iRV7zCk5ZpgiBn sjMQ== 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=HzgSaHI2Q+PMjg78squ8kiLhhxf33MnGyv3a9wfw+LQ=; b=I8NMFCtFevLYIe1b1ESxM8xowG4AzIPCLYwcoS71/ENRxeSrbZBaiOR2KVG6KmMQjg 0q8b714Ay+4r6SKfostjdGH6Su/sXIH3KK6jVr+AGRGXQiKtVq5Mr7fO0DoRXlqV5/E9 ++5DcdWmh/WmRGgsNQRG+0pXqkW/OreMtXXsitpUFKG+6aQgNmeZlvRFjRJekb+XL+HF 0Cq1Od1/apm1zaPf2aLjPIFnEpNoHYz/WGmDL4I4CHlO0ETxT57X0ceDaVtuKImmXMu3 iji1AykAS446o0RIFBO9E/d/UpFwZFPkkIQoXf5QbsC3UKhb0y1RVtC49MozI5t5MaeN gk4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=dWlCZEk4; 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 27si14131112pgl.734.2022.01.24.15.45.55; Mon, 24 Jan 2022 15:46:06 -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=dWlCZEk4; 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 S2360675AbiAXXhL (ORCPT + 99 others); Mon, 24 Jan 2022 18:37:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382474AbiAXW4C (ORCPT ); Mon, 24 Jan 2022 17:56:02 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDA4EC055AA0; Mon, 24 Jan 2022 13:10:34 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 7CD006141C; Mon, 24 Jan 2022 21:10:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5FC78C340E5; Mon, 24 Jan 2022 21:10:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643058633; bh=1sxxLQIPWRG6aWSNdg8Rs1aLFGBqVedpdVrcpjRSgSU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dWlCZEk4OmQgk1laMwEUiMQ0BDnklTLeJXYWMQAOMATGv6MD9Ru0l0Boj9Y+WowtM AYZNNcnm/dpxOL5Le41aEQzbWqLkr4jRLbTDPaIEO5Rhf2PVwV7CXcidDongP3pKsu /YJfgrJQci4JMNLDa+6IEzINPNb+Kvj11B8lLBMw= 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.16 0335/1039] selinux: fix potential memleak in selinux_add_opt() Date: Mon, 24 Jan 2022 19:35:24 +0100 Message-Id: <20220124184136.568570374@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@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 dde4ecc0cd186..49b4f59db35e7 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -988,18 +988,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) @@ -1024,6 +1028,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