From patchwork Sat Dec 22 12:27:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin King X-Patchwork-Id: 10741443 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9F3A81399 for ; Sat, 22 Dec 2018 18:32:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8FDF828A32 for ; Sat, 22 Dec 2018 18:32:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 83AAA28A35; Sat, 22 Dec 2018 18:32:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.4 required=2.0 tests=BAYES_00,DATE_IN_PAST_06_12, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2ABD828A32 for ; Sat, 22 Dec 2018 18:32:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392005AbeLVSbP (ORCPT ); Sat, 22 Dec 2018 13:31:15 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:42517 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391986AbeLVSbO (ORCPT ); Sat, 22 Dec 2018 13:31:14 -0500 Received: from 1.general.cking.uk.vpn ([10.172.193.212] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.76) (envelope-from ) id 1gagNi-0007IB-TD; Sat, 22 Dec 2018 12:27:50 +0000 From: Colin King To: Al Viro , Casey Schaufler , James Morris , "Serge E . Hallyn" , linux-security-module@vger.kernel.org Cc: kernel-janitors@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH][next] smack: fix two memory leaks in smack_add_opt Date: Sat, 22 Dec 2018 12:27:50 +0000 Message-Id: <20181222122750.13075-1-colin.king@canonical.com> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Colin Ian King Currently if s is null or when returning via the error exit label out_opt_err leaks of the allocated opts can occur. Fix the leak on the null s case by checking s is null before the allocation. Fix the leak on the exit path by checking if opts was allocated by kfree'ing opts. Detected by CoverityScan, CID#1475953 ("Resource leak") Fixes: b2130173efae ("smack: take the guts of smack_parse_opts_str() into a new helper") Signed-off-by: Colin Ian King --- security/smack/smack_lsm.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c index 84f72116a027..b5dc520adaa2 100644 --- a/security/smack/smack_lsm.c +++ b/security/smack/smack_lsm.c @@ -604,13 +604,13 @@ static int smack_add_opt(int token, const char *s, void **mnt_opts) { struct smack_mnt_opts *opts = *mnt_opts; + if (!s) + return -ENOMEM; if (!opts) { opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL); if (!opts) return -ENOMEM; } - if (!s) - return -ENOMEM; switch (token) { case Opt_fsdefault: @@ -644,6 +644,8 @@ static int smack_add_opt(int token, const char *s, void **mnt_opts) out_opt_err: pr_warn("Smack: duplicate mount options\n"); + if (opts != *mnt_opts) + kfree(opts); return -EINVAL; }