Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp1535721rdb; Wed, 31 Jan 2024 01:18:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IH6Z8GORsRUMAUviaX545y5spFrlaFxk03TWb7+yrTtvr+nv2exeuDFLFFBsyVgbusBQJAR X-Received: by 2002:a17:906:7f8d:b0:a36:4789:3629 with SMTP id f13-20020a1709067f8d00b00a3647893629mr633345ejr.25.1706692699516; Wed, 31 Jan 2024 01:18:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706692699; cv=pass; d=google.com; s=arc-20160816; b=X2ry2uD3srzpf9/LXrfeiLe8kExVDT2aiwAW4gduTJPY4n/cDrsvRMjZcD62PLKbzY IdLEba3CA7rZReZI7TprGnweXMHjrX5Hi2fcQVVwn2XXUZvty58O85/UIx0/Qw+kPlrU MGxEup2/uj0bzQf5souDHefnLnKlzu0a0G+uuEVkpfjFSKMlgLzgfsp+4Go70NSFkWsR FJZlFcxtQUKWEaQX0wIka7NpqUvoq0z8AAk+6tFLXOSZY8e2geX2uof6/JX/DyKvqt/W Rglo8Me0WYCMCncprt127YEefRGMvShKRoBtWs76DgGcdhULS4SNjYq45xmwnxoUwswe 6HfQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=e38NgAQUYFIEZ2aCgnnF2yZ7rE4peSq91s8jiVcYjQw=; fh=P485sRYho1p/RxM+YeeKL6xRr5M9tvNp9kIMgVSN12M=; b=PzA75F+j6QAlqJ7VHDkW6EIcJ/cTTHx0ktTHJ7K3pfzQB6qNpyizGwTnhqKSsYZbS9 HgK2Xzop1p7E8QOeYz0QRDYDGeE0BbA4PUrlRMAQZPSUD+bX3ZegT/WIeYyNmuYMt4/2 WXlRrb36Ch66zR/a45fF9dEd393fsC9fvE64Uv8jpImkjrcG7B88TPIrDlQdKGFBY9r6 x3czYwN7cgzJI9UmYw2Xuetq5/ir8JEzLRhi/sP/YGcPvawmBwjPaA1MTNIZ0kyG7nqT Ab7T5biwQ6TlExRkEE6W4V8qfMU9UmqQj2lVf8mPA4rpS328ZhbtJeXtRKkfA/9Q4eCt bCjQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-46119-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46119-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com X-Forwarded-Encrypted: i=1; AJvYcCXW0llO6qP9PiXhbjIEOjoAfD6mdYm6RiDVE29GJoa7i0hM656XVk0Rko6SbcoE3uDd7LBWFuX73i8kTxCrRDruOFfj5xfDrBX6KiqFBw== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id c21-20020a170906341500b00a35b8079ca4si2978190ejb.851.2024.01.31.01.18.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 01:18:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46119-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-46119-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46119-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 481F91F26E55 for ; Wed, 31 Jan 2024 09:18:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B64E66994D; Wed, 31 Jan 2024 09:15:00 +0000 (UTC) Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2896D60DCC; Wed, 31 Jan 2024 09:14:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.190 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706692500; cv=none; b=g2W0PuvHFL5XXxU9z/zguXwgbYdHzmitJNklyj/eRCSKvU5nwFFWVdZy789we434m+gEEKPLC4g3G823kMFJfu7Z7HXTxUqzao4K0Lg8mHX4sjPEqesSAseqBn+Q2uHgjMX33Hqyk/vDYVnQ7WFOGKU2uosNArUODzUEpUZdI40= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706692500; c=relaxed/simple; bh=76RXlR/msdWs4e9OX1WpKtSooLAmtPygvVwdSqDyayY=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=PAS1sesCzLQJWaXEoUnKchM2O0U+EZn8n/2ida0ToeX4/BujfexaQI3jP1Wky/xYWBgG7AW70/XhHA8XATyA0z8bLpmX5WAYI3DIFmqXjO3f5reONYudm3rzZ6tZ8bLCeExwkkWSZVkFfSQ7EMG1rIfYBjp2lIbW+DaStVBP1v0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.190 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.234]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4TPxDL2B49z1xmtd; Wed, 31 Jan 2024 17:13:54 +0800 (CST) Received: from dggpeml500021.china.huawei.com (unknown [7.185.36.21]) by mail.maildlp.com (Postfix) with ESMTPS id 347B71404D8; Wed, 31 Jan 2024 17:14:54 +0800 (CST) Received: from huawei.com (10.175.127.227) by dggpeml500021.china.huawei.com (7.185.36.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Wed, 31 Jan 2024 17:14:53 +0800 From: Baokun Li To: CC: , , , , Subject: [PATCH] cachefiles: fix memory leak in cachefiles_add_cache() Date: Wed, 31 Jan 2024 17:17:17 +0800 Message-ID: <20240131091717.1277013-1-libaokun1@huawei.com> X-Mailer: git-send-email 2.31.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml500021.china.huawei.com (7.185.36.21) The following memory leak was reported after unbinding /dev/cachefiles: ================================================================== unreferenced object 0xffff9b674176e3c0 (size 192): comm "cachefilesd2", pid 680, jiffies 4294881224 hex dump (first 32 bytes): 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace (crc ea38a44b): [] kmem_cache_alloc+0x2d5/0x370 [] prepare_creds+0x26/0x2e0 [] cachefiles_determine_cache_security+0x1f/0x120 [] cachefiles_add_cache+0x13c/0x3a0 [] cachefiles_daemon_write+0x146/0x1c0 [] vfs_write+0xcb/0x520 [] ksys_write+0x69/0xf0 [] do_syscall_64+0x72/0x140 [] entry_SYSCALL_64_after_hwframe+0x6e/0x76 ================================================================== Put the reference count of cache_cred in cachefiles_daemon_unbind() to fix the problem. And also put cache_cred in cachefiles_add_cache() error branch to avoid memory leaks. Fixes: 9ae326a69004 ("CacheFiles: A cache that backs onto a mounted filesystem") CC: stable@vger.kernel.org Signed-off-by: Baokun Li --- fs/cachefiles/cache.c | 2 ++ fs/cachefiles/daemon.c | 1 + 2 files changed, 3 insertions(+) diff --git a/fs/cachefiles/cache.c b/fs/cachefiles/cache.c index 7077f72e6f47..f449f7340aad 100644 --- a/fs/cachefiles/cache.c +++ b/fs/cachefiles/cache.c @@ -168,6 +168,8 @@ int cachefiles_add_cache(struct cachefiles_cache *cache) dput(root); error_open_root: cachefiles_end_secure(cache, saved_cred); + put_cred(cache->cache_cred); + cache->cache_cred = NULL; error_getsec: fscache_relinquish_cache(cache_cookie); cache->cache = NULL; diff --git a/fs/cachefiles/daemon.c b/fs/cachefiles/daemon.c index 3f24905f4066..6465e2574230 100644 --- a/fs/cachefiles/daemon.c +++ b/fs/cachefiles/daemon.c @@ -816,6 +816,7 @@ static void cachefiles_daemon_unbind(struct cachefiles_cache *cache) cachefiles_put_directory(cache->graveyard); cachefiles_put_directory(cache->store); mntput(cache->mnt); + put_cred(cache->cache_cred); kfree(cache->rootdirname); kfree(cache->secctx); -- 2.31.1