From patchwork Mon Aug 16 13:22:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Georgia Garcia X-Patchwork-Id: 1517181 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=MM78Qkkt; 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 4GpFG017Pyz9vFl; Mon, 16 Aug 2021 23:22:48 +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 1mFcZf-0001wD-NA; Mon, 16 Aug 2021 13:22:43 +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 1mFcZa-0001vY-Qm for kernel-team@lists.ubuntu.com; Mon, 16 Aug 2021 13:22:38 +0000 Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) (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 939354066D for ; Mon, 16 Aug 2021 13:22:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1629120158; bh=H8dIEB3LRbm3iYcaTBXsF5OMfg14ogv5reT5Qsjf9TU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MM78Qkkta7kMa9pNHgqpeoJ46FU9Gjns2x0ULX32t+E05Z2ZfT2rcHBL+BbAWcJpj QzqmMtEIa7/ZnazoDTlgKZunW5auIZDvl5TCVacQOjEiuNy21u5S5BnTWH3zi5tb0o dQKGX6YTDSGTnAr74VoyifVlzYCVBEllHr/KZxecMaANJuThYjNzBvS8kQyfkf3TaS xmdoSg6MTLewwVkQ3uy8x4kLjvKEDyy8ijX2dntmlN9yQj2G19lzzzzcp/xjwUhGkI x/3alaxtBc+bubFTroPCDQ4QkUgD1tDEoaAtaJXIFQFxt5efrQLOeSOWyrM7ZhnoDk BiKv8QRsbuS+A== Received: by mail-ej1-f70.google.com with SMTP id v3-20020a1709063383b02905b4d1d1e27cso4667404eja.19 for ; Mon, 16 Aug 2021 06:22:38 -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=H8dIEB3LRbm3iYcaTBXsF5OMfg14ogv5reT5Qsjf9TU=; b=uZIapoXJBuXMLNl8V5ES5THfi2Dba6HChRrObA6TKC/vbzCt/J/bLOJO5/mThPZWNn OE+WV8mAVZJAoQ0/8/c3RIYdiML+PfdpE82N2HUvl9L6mfZkaARnC1pCyvkvsC65b/9F SgAjGZZJITlq4xEGhMYoh1yzz7eYcMjpftq7rJLS+6cX8icR001CAt+IvY/FIfNh1mh1 QQ/a6gmZD9YkFRHJ2PiOE2EnfFAGLgMqOTDvc+ngPbjhL8Iy4AVT4z8/3s+kW7Hg24WD 6qcMfFVcNpxNc2QNZ2ElNdYskoREWleDk3+iiwSKJYWazKMg8Esz+F/lHilBLuEUMydQ 9J0Q== X-Gm-Message-State: AOAM5302dtN78/9rpXw0RoJ8g+ARDQ3zMlnsU8aIBUbTzKT+tn+MMvsG I5Yarobh5ry7znjg2alFspFG4r+OEtG1aqe5BJAv9ETWZSCNNq0AI7l9fqD8ed3kIU/w2xY49zX adNNYqp4A65T7Rf7iVWQj9EXhy1FRaxifNbXqICipdg== X-Received: by 2002:a17:906:a28d:: with SMTP id i13mr3709767ejz.535.1629120156995; Mon, 16 Aug 2021 06:22:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyyH2TLkzD+Z9OgE0OKQf95/1+Kn39vOJ7eBKo1Vpd4fhXf3gWv0lpRtWaNAd2cgy6RWEZjlw== X-Received: by 2002:a17:906:a28d:: with SMTP id i13mr3709751ejz.535.1629120156840; Mon, 16 Aug 2021 06:22:36 -0700 (PDT) Received: from localhost ([2001:67c:1562:8007::aac:4557]) by smtp.gmail.com with ESMTPSA id x20sm3677250ejb.100.2021.08.16.06.22.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Aug 2021 06:22:36 -0700 (PDT) From: Georgia Garcia To: kernel-team@lists.ubuntu.com Subject: [SRU][F][PATCH v2 1/1] apparmor: Fix memory leak of profile proxy Date: Mon, 16 Aug 2021 10:22:25 -0300 Message-Id: <20210816132226.225501-2-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 | 13 +++++++------ security/apparmor/policy.c | 1 + 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/security/apparmor/include/label.h b/security/apparmor/include/label.h index b1aeb282ca22..9cd61eaa630e 100644 --- a/security/apparmor/include/label.h +++ b/security/apparmor/include/label.h @@ -275,6 +275,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 de3b83f5377b..0afe670742e8 100644 --- a/security/apparmor/label.c +++ b/security/apparmor/label.c @@ -309,7 +309,7 @@ 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) { AA_BUG(!label); @@ -326,12 +326,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); - aa_put_proxy(label->proxy); label->proxy = (struct aa_proxy *) PROXY_POISON + 1; } @@ -340,7 +341,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 0e9f8e5bae62..eb7ba4d3df41 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); }