ceph: fix invalid pointer access if get_quota_realm return ERR_PTR

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 <haowenchao2@huawei.com>
Reviewed-by: Xiubo Li <xiubli@redhat.com>
Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
This commit is contained in:
Wenchao Hao 2023-11-23 09:53:40 +08:00 committed by Ilya Dryomov
parent b36b03344f
commit 0f4cf64eab
1 changed files with 22 additions and 17 deletions

View File

@ -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 * This function walks through the snaprealm for an inode and set the
* ceph_snap_realm for the first snaprealm that has quotas set (max_files, * 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 * 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 * Note that the caller is responsible for calling ceph_put_snap_realm() on the
* returned realm. * 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 * this function will return -EAGAIN; otherwise, the snaprealms walk-through
* will be restarted. * will be restarted.
*/ */
static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc, static int get_quota_realm(struct ceph_mds_client *mdsc, struct inode *inode,
struct inode *inode,
enum quota_get_realm which_quota, enum quota_get_realm which_quota,
bool retry) struct ceph_snap_realm **realmp, bool retry)
{ {
struct ceph_client *cl = mdsc->fsc->client; struct ceph_client *cl = mdsc->fsc->client;
struct ceph_inode_info *ci = NULL; 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; struct inode *in;
bool has_quota; bool has_quota;
if (realmp)
*realmp = NULL;
if (ceph_snap(inode) != CEPH_NOSNAP) if (ceph_snap(inode) != CEPH_NOSNAP)
return NULL; return 0;
restart: restart:
realm = ceph_inode(inode)->i_snap_realm; realm = ceph_inode(inode)->i_snap_realm;
@ -250,7 +251,7 @@ restart:
break; break;
ceph_put_snap_realm(mdsc, realm); ceph_put_snap_realm(mdsc, realm);
if (!retry) if (!retry)
return ERR_PTR(-EAGAIN); return -EAGAIN;
goto restart; goto restart;
} }
@ -259,8 +260,11 @@ restart:
iput(in); iput(in);
next = realm->parent; next = realm->parent;
if (has_quota || !next) if (has_quota || !next) {
return realm; if (realmp)
*realmp = realm;
return 0;
}
ceph_get_snap_realm(mdsc, next); ceph_get_snap_realm(mdsc, next);
ceph_put_snap_realm(mdsc, realm); ceph_put_snap_realm(mdsc, realm);
@ -269,7 +273,7 @@ restart:
if (realm) if (realm)
ceph_put_snap_realm(mdsc, realm); ceph_put_snap_realm(mdsc, realm);
return NULL; return 0;
} }
bool ceph_quota_is_same_realm(struct inode *old, struct inode *new) 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_mds_client *mdsc = ceph_sb_to_mdsc(old->i_sb);
struct ceph_snap_realm *old_realm, *new_realm; struct ceph_snap_realm *old_realm, *new_realm;
bool is_same; bool is_same;
int ret;
restart: restart:
/* /*
@ -286,9 +291,9 @@ restart:
* dropped and we can then restart the whole operation. * dropped and we can then restart the whole operation.
*/ */
down_read(&mdsc->snap_rwsem); down_read(&mdsc->snap_rwsem);
old_realm = get_quota_realm(mdsc, old, QUOTA_GET_ANY, true); get_quota_realm(mdsc, old, QUOTA_GET_ANY, &old_realm, true);
new_realm = get_quota_realm(mdsc, new, QUOTA_GET_ANY, false); ret = get_quota_realm(mdsc, new, QUOTA_GET_ANY, &new_realm, false);
if (PTR_ERR(new_realm) == -EAGAIN) { if (ret == -EAGAIN) {
up_read(&mdsc->snap_rwsem); up_read(&mdsc->snap_rwsem);
if (old_realm) if (old_realm)
ceph_put_snap_realm(mdsc, 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; bool is_updated = false;
down_read(&mdsc->snap_rwsem); down_read(&mdsc->snap_rwsem);
realm = get_quota_realm(mdsc, d_inode(fsc->sb->s_root), get_quota_realm(mdsc, d_inode(fsc->sb->s_root), QUOTA_GET_MAX_BYTES,
QUOTA_GET_MAX_BYTES, true); &realm, true);
up_read(&mdsc->snap_rwsem); up_read(&mdsc->snap_rwsem);
if (!realm) if (!realm)
return false; return false;