From patchwork Mon Aug 16 13:22:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Georgia Garcia X-Patchwork-Id: 1517184 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=h+mDzVP1; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GpFG52ztFz9vFs; Mon, 16 Aug 2021 23:22:53 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1mFcZk-0001xu-Sr; Mon, 16 Aug 2021 13:22:48 +0000 Received: from smtp-relay-canonical-1.internal ([10.131.114.174] helo=smtp-relay-canonical-1.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1mFcZf-0001w3-7E for kernel-team@lists.ubuntu.com; Mon, 16 Aug 2021 13:22:43 +0000 Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-1.canonical.com (Postfix) with ESMTPS id 19CC33F070 for ; Mon, 16 Aug 2021 13:22:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1629120163; bh=6aTowX3hZd9Trym5mVaumQISyx5+5UIKe1azVIhtjzk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=h+mDzVP1cHsb+uJsprm4pMwhjrW0IlMVrzUh4adP5d5U/HG+8fQuaekyLHvUZMsvE CZr7RJlj/cnEUacwkL+EK/01f5xELASmbIZmGkrqz82/Noj7b0fZRBTSayR4nEff3r T1JDENkkHLK6OfRVank4ppEvB77rUICNvt4GbsQqxRPI1FnEY4UbEc5SJY5yPQWViS m1zw44gjq5m1Qk5EqsBtPEIrhReHF6Qnlg5xR3IAm4CWydo5sbeCeZyhYRO2dfcREH R2/zZpX+DVf5t3DIssQ7vqelnXB1urJtkxEr+UsRN0duFBJf5/zT2PYdCEC7qQXakf nFrLT627QUXuQ== Received: by mail-ej1-f69.google.com with SMTP id ne21-20020a1709077b95b029057eb61c6fdfso4678169ejc.22 for ; Mon, 16 Aug 2021 06:22:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6aTowX3hZd9Trym5mVaumQISyx5+5UIKe1azVIhtjzk=; b=AKrOioxvrvtBE0Tw5VCy34mB2jUrfYzPxWzpiIMQDqDpL75t6jE+1DQ8QCRsbcWlnt kJeH1tS/PTAXRcJOJ7B6a3XKSVzudH6GcSEsuLHgPcC1Win4ISwsPitCwCpPNqj6UaKj SOVdCqCzLpgBQlriQeamDB21AdiDQLE0IeffqcXwrOwyJvFauWNhNJif4Ymh5usTHDXm lpYgNSgNzWgjwD5xS02SKqaxaiO2wWOzr2v6yt4WM+ZDndUyAC5rKKnmxiId7hkk0cmT 4wK1wcY7Io+m+IW2rKMPH2wOx+Srp7LPO4j35i/UT4RHgkmJ5ut2C68cGXes/Zdd++ER T/jA== X-Gm-Message-State: AOAM532vHruizAanTUAu+MnBxVVONiYpQRtEW/psXiORsfP50gS9kcG9 hGCIhpKxO0H7q77Zj0YStllihy6MfpJ/FGjbieuLmOM9MK+yoQ7iSFV+VParOPWmnOMD6pvlw+o KJ9f1g1D/N81NMQUF6zqcJP6nYS0Wtofze0HC7ldAbw== X-Received: by 2002:a17:907:aa8:: with SMTP id bz8mr15695468ejc.511.1629120162591; Mon, 16 Aug 2021 06:22:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7IgF4myOh6N0eYXzwd175ASBK4lFHJ/9pq5vl6qHFELfasE+r0XKYQeRHkR4EXqdY1FYn+w== X-Received: by 2002:a17:907:aa8:: with SMTP id bz8mr15695454ejc.511.1629120162443; Mon, 16 Aug 2021 06:22:42 -0700 (PDT) Received: from localhost ([2001:67c:1562:8007::aac:4557]) by smtp.gmail.com with ESMTPSA id b11sm3671030eja.104.2021.08.16.06.22.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Aug 2021 06:22:42 -0700 (PDT) From: Georgia Garcia To: kernel-team@lists.ubuntu.com Subject: [SRU][B][PATCH v2 1/1] apparmor: Fix memory leak of profile proxy Date: Mon, 16 Aug 2021 10:22:26 -0300 Message-Id: <20210816132226.225501-3-georgia.garcia@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210816132226.225501-1-georgia.garcia@canonical.com> References: <20210816132226.225501-1-georgia.garcia@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: John Johansen BugLink: https://bugs.launchpad.net/bugs/1939915 When the proxy isn't replaced and the profile is removed, the proxy is being leaked resulting in a kmemleak check message of unreferenced object 0xffff888077a3a490 (size 16): comm "apparmor_parser", pid 128041, jiffies 4322684109 (age 1097.028s) hex dump (first 16 bytes): 03 00 00 00 00 00 00 00 b0 92 fd 4b 81 88 ff ff ...........K.... backtrace: [<0000000084d5daf2>] aa_alloc_proxy+0x58/0xe0 [<00000000ecc0e21a>] aa_alloc_profile+0x159/0x1a0 [<000000004cc9ce15>] unpack_profile+0x275/0x1c40 [<000000007332b3ca>] aa_unpack+0x1e7/0x7e0 [<00000000e25e31bd>] aa_replace_profiles+0x18a/0x1d10 [<00000000350d9415>] policy_update+0x237/0x650 [<000000003fbf934e>] profile_load+0x122/0x160 [<0000000047f7b781>] vfs_write+0x139/0x290 [<000000008ad12358>] ksys_write+0xcd/0x170 [<000000001a9daa7b>] do_syscall_64+0x70/0x310 [<00000000b9efb0cf>] entry_SYSCALL_64_after_hwframe+0x49/0xb3 Make sure to cleanup the profile's embedded label which will result on the proxy being properly freed. Fixes: 637f688dc3dc ("apparmor: switch from profiles to using labels on contexts") Signed-off-by: John Johansen (cherry picked from commit 3622ad25d4d68fcbdef3bc084b5916873e785344) Signed-off-by: Georgia Garcia --- security/apparmor/include/label.h | 1 + security/apparmor/label.c | 19 +++++++------------ security/apparmor/policy.c | 1 + 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/security/apparmor/include/label.h b/security/apparmor/include/label.h index af22dcbbcb8a..8ef94d6ffa09 100644 --- a/security/apparmor/include/label.h +++ b/security/apparmor/include/label.h @@ -279,6 +279,7 @@ void aa_labelset_destroy(struct aa_labelset *ls); void aa_labelset_init(struct aa_labelset *ls); void __aa_labelset_update_subtree(struct aa_ns *ns); +void aa_label_destroy(struct aa_label *label); void aa_label_free(struct aa_label *label); void aa_label_kref(struct kref *kref); bool aa_label_init(struct aa_label *label, int size); diff --git a/security/apparmor/label.c b/security/apparmor/label.c index 9171cd8ec032..b5978f310ced 100644 --- a/security/apparmor/label.c +++ b/security/apparmor/label.c @@ -313,10 +313,8 @@ int aa_vec_unique(struct aa_profile **vec, int n, int flags) } -static void label_destroy(struct aa_label *label) +void aa_label_destroy(struct aa_label *label) { - struct aa_label *tmp; - AA_BUG(!label); if (!label_isprofile(label)) { @@ -332,16 +330,13 @@ static void label_destroy(struct aa_label *label) } } - if (rcu_dereference_protected(label->proxy->label, true) == label) - rcu_assign_pointer(label->proxy->label, NULL); - + if (label->proxy) { + if (rcu_dereference_protected(label->proxy->label, true) == label) + rcu_assign_pointer(label->proxy->label, NULL); + aa_put_proxy(label->proxy); + } aa_free_secid(label->secid); - tmp = rcu_dereference_protected(label->proxy->label, true); - if (tmp == label) - rcu_assign_pointer(label->proxy->label, NULL); - - aa_put_proxy(label->proxy); label->proxy = (struct aa_proxy *) PROXY_POISON + 1; } @@ -350,7 +345,7 @@ void aa_label_free(struct aa_label *label) if (!label) return; - label_destroy(label); + aa_label_destroy(label); kfree(label); } diff --git a/security/apparmor/policy.c b/security/apparmor/policy.c index a92c167c9249..1a7aa0ad5d0b 100644 --- a/security/apparmor/policy.c +++ b/security/apparmor/policy.c @@ -240,6 +240,7 @@ void aa_free_profile(struct aa_profile *profile) kzfree(profile->hash); aa_put_loaddata(profile->rawdata); + aa_label_destroy(&profile->label); kzfree(profile); }