From patchwork Fri Oct 21 09:36:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiu Jianfeng X-Patchwork-Id: 6605 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp592761wrr; Fri, 21 Oct 2022 02:45:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7zjI4bim5BBZO+p7gOcdrqpUqej0Zk2D5NRBMk+01qtrAlSpvy5dq4vb5N+1JVp7cFr7XS X-Received: by 2002:a17:90b:4d07:b0:1ef:521c:f051 with SMTP id mw7-20020a17090b4d0700b001ef521cf051mr58208178pjb.164.1666345528176; Fri, 21 Oct 2022 02:45:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666345528; cv=none; d=google.com; s=arc-20160816; b=auIiaqc3z+lems83H8eMXBm/eFUpj5SKNG8WyK5JhKa+zUfmtHuZUrbxTi0lMtl5QM nht/to6BHXxjkDS80TLdlkPv8iMDZoaCkZCJ32QzfiMUHW4i6RghBHP7a8Sqly9dahcj ztwcDpoHoDJcoxuTQXtbHdgfZgtLwcuCtqjLStZJv2a4L9/JVGqSVV2bi/ledi7VAV7C 2FWLJDXjrz11tIsTMrWGt9r0kS/sfg02nragB896OPnR7CFUB2iTSD6WVaXRssrwmOxz /PJhVm7HmlPz5ZK5bRUC3ublXkcHRqU8hpRp+siA9DECfvCvT2ju+1g4xI5h1yAnZ7Co +w8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=YV4ogQ0OVf5tl3AIzlWTAJrlfO0B9FfediQb0VnN8tk=; b=tZqkzh1QtZEqiGoPgeu7jvHNXIIv7hD8Th9KK+p48LeE8b0AuSeEDimHt6IwdHuP5v I2XIDa4m4FJEA3ewa8szNYDjLRG9I+4GAV43WUV1x245ROPcZK5sDLvi2qQ8eGg3KaCm 6BEjZ80hcNKyUiB/zaXXjOPMO3jhw86Im0sJqTK37y7AMB5YAaUfnrZLjeUmHC/C/MIj LtzGP455Ha8kcCO/qEU8O/HnfTPAh43p+J5ZRj5p4BAy4JLye/PpsSKtGlUT7ACvbN2O t9sEfzLrC+YfqgNyaZHo8ZFex2jazBGTRd9ZIaGYR8BzJ6HzU75kU3BaIhQCnIpwwdbt AJOg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l10-20020a170902f68a00b001733a212ccfsi28993536plg.330.2022.10.21.02.45.13; Fri, 21 Oct 2022 02:45:28 -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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230178AbiJUJjo (ORCPT + 99 others); Fri, 21 Oct 2022 05:39:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229648AbiJUJjl (ORCPT ); Fri, 21 Oct 2022 05:39:41 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D4624362F; Fri, 21 Oct 2022 02:39:36 -0700 (PDT) Received: from dggpeml500023.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Mtzp02GvDz15Lwd; Fri, 21 Oct 2022 17:34:48 +0800 (CST) Received: from ubuntu1804.huawei.com (10.67.174.58) by dggpeml500023.china.huawei.com (7.185.36.114) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 21 Oct 2022 17:39:34 +0800 From: Xiu Jianfeng To: , , CC: , , Subject: [PATCH] apparmor: Fix memleak issue in unpack_profile() Date: Fri, 21 Oct 2022 17:36:02 +0800 Message-ID: <20221021093602.102839-1-xiujianfeng@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Originating-IP: [10.67.174.58] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml500023.china.huawei.com (7.185.36.114) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747289928653206341?= X-GMAIL-MSGID: =?utf-8?q?1747289928653206341?= Before aa_alloc_profile(), it has allocated string for @*ns_name if @tmpns is not NULL, so directly return -ENOMEM if aa_alloc_profile() failed will cause a memleak issue, and even if aa_alloc_profile() succeed, in the @fail_profile tag of aa_unpack(), it need to free @ns_name as well, this patch fixes them. Fixes: 736ec752d95e ("AppArmor: policy routines for loading and unpacking policy") Fixes: 04dc715e24d0 ("apparmor: audit policy ns specified in policy load") Signed-off-by: Xiu Jianfeng Acked-by: John Johansen --- security/apparmor/policy_unpack.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/security/apparmor/policy_unpack.c b/security/apparmor/policy_unpack.c index 2e028d540c6b..1bf8cfb8700a 100644 --- a/security/apparmor/policy_unpack.c +++ b/security/apparmor/policy_unpack.c @@ -858,8 +858,11 @@ static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name) } profile = aa_alloc_profile(name, NULL, GFP_KERNEL); - if (!profile) - return ERR_PTR(-ENOMEM); + if (!profile) { + info = "out of memory"; + error = -ENOMEM; + goto fail; + } rules = list_first_entry(&profile->rules, typeof(*rules), list); /* profile renaming is optional */ @@ -1090,6 +1093,10 @@ static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name) if (error == 0) /* default error covers most cases */ error = -EPROTO; + if (*ns_name) { + kfree(*ns_name); + *ns_name = NULL; + } if (profile) name = NULL; else if (!name) @@ -1392,6 +1399,7 @@ int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, { struct aa_load_ent *tmp, *ent; struct aa_profile *profile = NULL; + char *ns_name = NULL; int error; struct aa_ext e = { .start = udata->data, @@ -1401,7 +1409,6 @@ int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, *ns = NULL; while (e.pos < e.end) { - char *ns_name = NULL; void *start; error = verify_header(&e, e.pos == e.start, ns); if (error) @@ -1432,6 +1439,7 @@ int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, ent->new = profile; ent->ns_name = ns_name; + ns_name = NULL; list_add_tail(&ent->list, lh); } udata->abi = e.version & K_ABI_MASK; @@ -1452,6 +1460,7 @@ int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, return 0; fail_profile: + kfree(ns_name); aa_put_profile(profile); fail: