Received: by 2002:a05:7412:9c07:b0:fa:6e18:a558 with SMTP id lr7csp777174rdb; Sun, 28 Jan 2024 08:28:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IGNRKhbExDEJGcZqU6d4VygUb2fNdPfBPi65nLAYRZQ+MwKkN8fwqefKHybWOaU88mybjDA X-Received: by 2002:aa7:db48:0:b0:55c:a3db:b250 with SMTP id n8-20020aa7db48000000b0055ca3dbb250mr2392617edt.32.1706459285709; Sun, 28 Jan 2024 08:28:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706459285; cv=pass; d=google.com; s=arc-20160816; b=fDG7aY01aGtEDb73qHF/EAaJi+T3+HPSSDSlYULY4h7lvldg6oZ1zOvMXAxt2lE86L MQ8PEg6CrtLc53LdRrril2a3e7HKw7cMXsoUzIvEHIXh1okGE/rbZQJQ75ZGnAvS5TI2 W3t8StIKStyLnIeOuIgBnUe+OS+iAoKlxyjkqFX/nc89ZSE2cqFjprJfSyqBc3FnzEQe M5G6273dHM4aS6rsICVwpE1D1BVSYxu+bJt1Fa2dtQmaO2v3k8QVNgI8/Uqozas6YV1U uaK2hx6tMU9MkHtvmh4ZzoO++xdc09qlJGk85lZVQrLhdKtz79zMaRnrzAf1+xHv1E/x IS3w== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=xTN8PPu+5IwNNlH0+0Eq9iEzO8TmIR/2q+yiPPKWmRw=; fh=JYsjrny/RfKzRJIOopfNjf5B3d43UqcHjnOMzYeaQvE=; b=boGfLyjv5uOWoRXQVr7QbhnWxMHzp3xtTNvHmVJZtvTmWMfPwHqHlbFfwxCOEHeFHz fQJ7aiM5reXYVJtUVzQ8yXKK1zlts4+C+q0uw+xsNHB9jcRbiKCaFQ3WBazSV59vjGhs IXp6+G2AYfT/ROc4HtJELCM1bZ3h0WLWGKm+bQybP63n8awt4e/Lk50Dv39wBpSpqO0v rc5/JNK6xIUPPaY3RDROQmxuIXCUT9EoboZ3vl5vPeO55H597JpQFX7dx6/hR3bsspli Nt/vEjkuehocYmaZy64EmY7dRHbAfHnjRWETJNxITHDzrnZBCQYT2g2k/8YAAZBEwmPh GDJQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LeBYJCAz; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-41715-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41715-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id i7-20020a50fc07000000b0055ef8ac594dsi230844edr.407.2024.01.28.08.28.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jan 2024 08:28:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41715-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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LeBYJCAz; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-41715-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41715-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 074CB1F27CDA for ; Sun, 28 Jan 2024 16:20:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BC61856453; Sun, 28 Jan 2024 16:12:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LeBYJCAz" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 CEE6356756; Sun, 28 Jan 2024 16:12:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706458363; cv=none; b=eLabQlaOL23/jF5z/LzUYo7TY2+EvKbHrT20Kwy3PxoPAsjUEtBMrv4CEsdpLyTQq7TO9wGijcYT/6Zm7sJvuj5SHtQLcLaCAapdPFQ+/EzTQOoMqCHjowUK26pgbO+EQuQjBpL040Him5X79zqJ7utsdlvKEYpY0luN2uc9GJg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706458363; c=relaxed/simple; bh=o8g1+2rXOR97ybCxDwk0SDOZvIEgqhmE0URwimesaZY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=W7nV2V9AgkJ6w09e+FX1+Nfq70x4fv8dD1rVAW+B/TFMXBCEdQQWr3xqxheX6ckgGJHdLAAR12yEys3r/UCMTC3OhnYERJDbeOlai8BrTCd7ZGeACXacH5uPEAw+gBwxTmZzg5EnnCVEJlE6fXM4V9jtYyvcpL+3gJWPvto+cRM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LeBYJCAz; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id DBF24C433F1; Sun, 28 Jan 2024 16:12:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706458363; bh=o8g1+2rXOR97ybCxDwk0SDOZvIEgqhmE0URwimesaZY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LeBYJCAzCWKw7c7QrtEusg/Ilua5aNrZYeoixLCnbPO9oNAF0iaEmsS4AV7hP1S7Y XFxk5g/GzvYNlitCaWAZUQbKlbJXrEpRLDh3btzxHoAPllk3VuvQjowrxVmeLsK7ut YM2lEkmVBK8xbOU7X8eGSCm7wooDdpY2I+Z7pJYPIEp7sgZHoYiaitXskauM3bSNgM rtosx82xsiNN2i81Iw7R1oa1FFs1CxXMeNSpkTzQ3KKzoZPgOf25N2bXxGP8/je6Al DtxmVXnBSNBh2Y9vYdKsAiTIZZvH6Fzcmpwh3fw4VwZDAnxSFayZn9O3knn2AfmVRU yS/k1EWN48ZVA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Wenchao Hao , Xiubo Li , Ilya Dryomov , Sasha Levin , ceph-devel@vger.kernel.org Subject: [PATCH AUTOSEL 6.7 33/39] ceph: fix invalid pointer access if get_quota_realm return ERR_PTR Date: Sun, 28 Jan 2024 11:10:53 -0500 Message-ID: <20240128161130.200783-33-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240128161130.200783-1-sashal@kernel.org> References: <20240128161130.200783-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.7.2 Content-Transfer-Encoding: 8bit From: Wenchao Hao [ Upstream commit 0f4cf64eabc6e16cfc2704f1960e82dc79d91c8d ] This issue is reported by smatch that get_quota_realm() might return ERR_PTR but we did not handle it. It's not a immediate bug, while we still should address it to avoid potential bugs if get_quota_realm() is changed to return other ERR_PTR in future. Set ceph_snap_realm's pointer in get_quota_realm()'s to address this issue, the pointer would be set to NULL if get_quota_realm() failed to get struct ceph_snap_realm, so no ERR_PTR would happen any more. [ xiubli: minor code style clean up ] Signed-off-by: Wenchao Hao Reviewed-by: Xiubo Li Signed-off-by: Ilya Dryomov Signed-off-by: Sasha Levin --- fs/ceph/quota.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/fs/ceph/quota.c b/fs/ceph/quota.c index 9d36c3532de1..06ee397e0c3a 100644 --- a/fs/ceph/quota.c +++ b/fs/ceph/quota.c @@ -197,10 +197,10 @@ void ceph_cleanup_quotarealms_inodes(struct ceph_mds_client *mdsc) } /* - * This function walks through the snaprealm for an inode and returns the - * ceph_snap_realm for the first snaprealm that has quotas set (max_files, + * This function walks through the snaprealm for an inode and set the + * realmp with the first snaprealm that has quotas set (max_files, * max_bytes, or any, depending on the 'which_quota' argument). If the root is - * reached, return the root ceph_snap_realm instead. + * reached, set the realmp with the root ceph_snap_realm instead. * * Note that the caller is responsible for calling ceph_put_snap_realm() on the * returned realm. @@ -211,10 +211,9 @@ void ceph_cleanup_quotarealms_inodes(struct ceph_mds_client *mdsc) * this function will return -EAGAIN; otherwise, the snaprealms walk-through * will be restarted. */ -static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc, - struct inode *inode, - enum quota_get_realm which_quota, - bool retry) +static int get_quota_realm(struct ceph_mds_client *mdsc, struct inode *inode, + enum quota_get_realm which_quota, + struct ceph_snap_realm **realmp, bool retry) { struct ceph_client *cl = mdsc->fsc->client; struct ceph_inode_info *ci = NULL; @@ -222,8 +221,10 @@ static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc, struct inode *in; bool has_quota; + if (realmp) + *realmp = NULL; if (ceph_snap(inode) != CEPH_NOSNAP) - return NULL; + return 0; restart: realm = ceph_inode(inode)->i_snap_realm; @@ -250,7 +251,7 @@ static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc, break; ceph_put_snap_realm(mdsc, realm); if (!retry) - return ERR_PTR(-EAGAIN); + return -EAGAIN; goto restart; } @@ -259,8 +260,11 @@ static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc, iput(in); next = realm->parent; - if (has_quota || !next) - return realm; + if (has_quota || !next) { + if (realmp) + *realmp = realm; + return 0; + } ceph_get_snap_realm(mdsc, next); ceph_put_snap_realm(mdsc, realm); @@ -269,7 +273,7 @@ static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc, if (realm) ceph_put_snap_realm(mdsc, realm); - return NULL; + return 0; } bool ceph_quota_is_same_realm(struct inode *old, struct inode *new) @@ -277,6 +281,7 @@ bool ceph_quota_is_same_realm(struct inode *old, struct inode *new) struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(old->i_sb); struct ceph_snap_realm *old_realm, *new_realm; bool is_same; + int ret; restart: /* @@ -286,9 +291,9 @@ bool ceph_quota_is_same_realm(struct inode *old, struct inode *new) * dropped and we can then restart the whole operation. */ down_read(&mdsc->snap_rwsem); - old_realm = get_quota_realm(mdsc, old, QUOTA_GET_ANY, true); - new_realm = get_quota_realm(mdsc, new, QUOTA_GET_ANY, false); - if (PTR_ERR(new_realm) == -EAGAIN) { + get_quota_realm(mdsc, old, QUOTA_GET_ANY, &old_realm, true); + ret = get_quota_realm(mdsc, new, QUOTA_GET_ANY, &new_realm, false); + if (ret == -EAGAIN) { up_read(&mdsc->snap_rwsem); if (old_realm) ceph_put_snap_realm(mdsc, old_realm); @@ -492,8 +497,8 @@ bool ceph_quota_update_statfs(struct ceph_fs_client *fsc, struct kstatfs *buf) bool is_updated = false; down_read(&mdsc->snap_rwsem); - realm = get_quota_realm(mdsc, d_inode(fsc->sb->s_root), - QUOTA_GET_MAX_BYTES, true); + get_quota_realm(mdsc, d_inode(fsc->sb->s_root), QUOTA_GET_MAX_BYTES, + &realm, true); up_read(&mdsc->snap_rwsem); if (!realm) return false; -- 2.43.0