Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3660511pxb; Mon, 24 Jan 2022 14:42:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJz4x29O02//OzzpdH/uPGsQjWiw5qmRpVRp2fViPT9tsGaVK2Ynz7UAJC16M4jXJuNYhBpn X-Received: by 2002:a17:90a:d584:: with SMTP id v4mr404439pju.139.1643064168283; Mon, 24 Jan 2022 14:42:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643064168; cv=none; d=google.com; s=arc-20160816; b=e/nu4fm8BiaNru+vTesLvym71XjfXO00S3aZb6SAtlWBg/3Ljl7rVE1GM3nits0MFn MAECH2yZTn8EjWn9PcLApwBv7pRJ//Jdq7GkBhNBrXe8dpuns9CezkWsFFzMmo+DhrIU MUTNJEs8jfqS2N3PWyl+0Y2sSfsdOH2/EZumZ0RjJi63MP9FopGMKEgyoHQ/JyypLEO+ tCsW1JVfVMbwBKK3/oOvFIvLAwBKkaPLXEgPbuhDIU/8OwTWLxFcyYqG9dkdpFab5C9i SWukGP+LxQve3ReTeukgF4/Qi+xDHCkh/xtVajyGXIKPhPcZ0Hmkl9U7P+/5BOvyu0cz 8KQg== 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=LdnV79C+srX7ldlslIkEQUsS5IpJZ0NA6dutDM6S7Ls=; b=CozQzJmMKhciqSlWFiWTRqpoNOqBk59iHQZVvFH0o5lFzMSerAm7yjOepliQV7Q1+Q Un+Q4rdOTvEbcsyROQJfrnc4i3qIqxlGx0sGl0AkkS7AB63v9zj58IWGpHQ4jXri4J2n gIvhSzr9xLxlHBzGHJ3bBiGOh59+U77mD3+bYkIWGKhjwOLsgMU+NS28xgIlWsPlwSbe jTwsYsCrjP/rRnfcS0uVAX7zn96Z0cvph+QQoby9LRrVbn1dnAe/tBRLy/XmPcyvVUi1 IuDZd8av73lRzpiDCiJ0iqRYH+V/EjI5YwY6JmKGe9NOEp+UNu4tUM23wMz8QXlNhjGF y+Kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=m+YHDcP5; 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 ml7si530350pjb.127.2022.01.24.14.42.36; Mon, 24 Jan 2022 14:42:48 -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=m+YHDcP5; 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 S1835623AbiAXWhD (ORCPT + 99 others); Mon, 24 Jan 2022 17:37:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378450AbiAXVha (ORCPT ); Mon, 24 Jan 2022 16:37:30 -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 CEBDCC0BD11F; Mon, 24 Jan 2022 12:23:43 -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 6ED1061508; Mon, 24 Jan 2022 20:23:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 56A98C340E5; Mon, 24 Jan 2022 20:23:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643055822; bh=QpiE+taEYJblwXqiRaGhxJmlRwyfXr5z1r7JVboBS40=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m+YHDcP5U3gNO+pqiirB5BpN8QKCAcQxlhMB7Pm2SO0G4j/bL0BojXgaAxAr4klBw 1rWC/mWih6DzL76BnypzMse7MWCdA6JYXfyP7FbAFp3XanpJjpuB9aHP9odmlP7eoz liMYcUli98AkkgolUvSYZFYg9iwf+SCqMwCNPs9w= 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.15 277/846] selinux: fix potential memleak in selinux_add_opt() Date: Mon, 24 Jan 2022 19:36:34 +0100 Message-Id: <20220124184110.492248490@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184100.867127425@linuxfoundation.org> References: <20220124184100.867127425@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 9309e62d46eda..baa12d1007c7c 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -987,18 +987,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) @@ -1023,6 +1027,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