ceph: fix potential NULL dereferenced issue in ceph_fill_trace()
Subsystems (2)
Viacheslav Dubeyko
maintainerPATCH 1/1 ceph: fix potential NULL dereferenced issue in ceph_fill_trace()
From: Viacheslav Dubeyko <[email protected]>
The Coverity Scan service has detected a potential dereference of an explicit NULL value in ceph_fill_trace() [1].
The variable in is declared in the beggining ofceph_fill_trace() [2]:
struct inode *in = NULL;
However, the initialization of the variable is happening under condition [3]:
if (rinfo->head->is_target) {
<skipped>
in = req->r_target_inode;
<skipped>
}Potentially, if rinfo->head->is_target == FALSE, then in variable continues to be NULL and later the dereference of NULL value could happen in ceph_fill_trace() logic [4,5]:
else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
req->r_op == CEPH_MDS_OP_MKSNAP) &&
test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
<skipped>
ihold(in);
err = splice_dentry(&req->r_dentry, in);
if (err < 0)
goto done;
}This patch adds the checking of in variable for NULL value and it returns -EINVAL error code if it has NULL value.
[1] https://scan5.scan.coverity.com/#/project-view/64304/10063?selectedIssue=1141197
[2] https://elixir.bootlin.com/linux/v6.17-rc3/source/fs/ceph/inode.c#L1522
[3] https://elixir.bootlin.com/linux/v6.17-rc3/source/fs/ceph/inode.c#L1629
[4] https://elixir.bootlin.com/linux/v6.17-rc3/source/fs/ceph/inode.c#L1745
[5] https://elixir.bootlin.com/linux/v6.17-rc3/source/fs/ceph/inode.c#L1777
--- fs/ceph/inode.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
fs/ceph/inode.c +11 -0
@@ -1739,6 +1739,11 @@ int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req) goto done; } +if (!in) { + err = -EINVAL ; + goto done; + } +/* attach proper inode */ if (d_really_is_negative (dn)) {ceph_dir_clear_ordered (dir);
@@ -1774,6 +1779,12 @@ int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)doutc (cl," linking snapped dir %p to dn %p\n" , in, req->r_dentry);ceph_dir_clear_ordered (dir); + +if (!in) { + err = -EINVAL ; + goto done; + } +ihold (in); err =splice_dentry (&req->r_dentry, in);if (err <0 )
Signature · 1 line
2.51.0
Alex Markuze
maintainerConsidering we hadn't seen any related issues, I would add an unlikely macro for that if.
Quoted 84 lines · Viacheslav Dubeyko
From: Viacheslav Dubeyko <[email protected]>
The Coverity Scan service has detected a potential dereference of an explicit NULL value in
ceph_fill_trace()[1].The variable in is declared in the beggining of
ceph_fill_trace()[2]:struct inode *in = NULL;
However, the initialization of the variable is happening under condition [3]:
if (rinfo->head->is_target) { <skipped> in = req->r_target_inode; <skipped> }Potentially, if
rinfo->head->is_target== FALSE, then in variable continues to be NULL and later the dereference of NULL value could happen inceph_fill_trace()logic [4,5]:else if ((
req->r_op==CEPH_MDS_OP_LOOKUPSNAP||req->r_op == CEPH_MDS_OP_MKSNAP) && test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { <skipped> ihold(in); err = splice_dentry(&req->r_dentry, in); if (err < 0) goto done; }This patch adds the checking of in variable for NULL value and it returns
-EINVALerror code if it has NULL value.[1] https://scan5.scan.coverity.com/#/project-view/64304/10063?selectedIssue=1141197
[2] https://elixir.bootlin.com/linux/v6.17-rc3/source/fs/ceph/inode.c#L1522
[3] https://elixir.bootlin.com/linux/v6.17-rc3/source/fs/ceph/inode.c#L1629
[4] https://elixir.bootlin.com/linux/v6.17-rc3/source/fs/ceph/inode.c#L1745
[5] https://elixir.bootlin.com/linux/v6.17-rc3/source/fs/ceph/inode.c#L1777Signed-off-by Viacheslav Dubeyko <[email protected]>cc Alex Markuze <[email protected]>cc Ilya Dryomov <[email protected]>cc Ceph Development <[email protected]>--- fs/ceph/inode.c | 11 +++++++++++ 1 file changed, 11 insertions(+)fs/ceph/inode.c +11
-1@@ -1739,6 +1739,11 @@ int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req) goto done; } +if (!in) { + err = -EINVAL ; + goto done; + } +/* attach proper inode */ if (d_really_is_negative (dn)) {ceph_dir_clear_ordered (dir);@@ -1774,6 +1779,12 @@ int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)doutc (cl," linking snapped dir %p to dn %p\n" , in, req->r_dentry);ceph_dir_clear_ordered (dir); + +if (!in) { + err = -EINVAL ; + goto done; + } +ihold (in); err =splice_dentry (&req->r_dentry, in);if (err <0 ) --2.51.0
Viacheslav Dubeyko
maintainerQuoted 4 lines · Alex Markuze
Considering we hadn't seen any related issues, I would add an unlikely
macro for that if.
Makes sense to me. Let me rework the patch.
Thanks,
Slava.
Quoted 3 lines · Alex Markuze
Considering we hadn't seen any related issues, I would add an unlikely macro for that if.
Using likely/unlikely() should only be done if there is a reason to think it will affect benchmarking data. Otherwise, you're just making the code messy for no reason.
Quoted 11 lines · Viacheslav Dubeyko
Quoted 10 lines
From: Viacheslav Dubeyko <[email protected]>
The Coverity Scan service has detected a potential dereference of an explicit NULL value in
ceph_fill_trace()[1].The variable in is declared in the beggining of
ceph_fill_trace()[2]:struct inode *in = NULL;
Most of the time, these sorts of NULL initializers are just there to silence "uninitialize variable" warnings when the code is too complicated for GCC to parse.
Quoted 28 lines
Quoted 28 lines
However, the initialization of the variable is happening under condition [3]:
if (rinfo->head->is_target) { <skipped> in = req->r_target_inode; <skipped> }Potentially, if
rinfo->head->is_target== FALSE, then in variable continues to be NULL and later the dereference of NULL value could happen inceph_fill_trace()logic [4,5]:else if ((
req->r_op==CEPH_MDS_OP_LOOKUPSNAP||req->r_op == CEPH_MDS_OP_MKSNAP) && test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { <skipped> ihold(in); err = splice_dentry(&req->r_dentry, in); if (err < 0) goto done; }This patch adds the checking of in variable for NULL value and it returns
-EINVALerror code if it has NULL value.
Quoted 7 lines
Quoted 7 lines
[1] https://scan5.scan.coverity.com/#/project-view/64304/10063?selectedIssue=1141197
[2] https://elixir.bootlin.com/linux/v6.17-rc3/source/fs/ceph/inode.c#L1522
[3] https://elixir.bootlin.com/linux/v6.17-rc3/source/fs/ceph/inode.c#L1629
[4] https://elixir.bootlin.com/linux/v6.17-rc3/source/fs/ceph/inode.c#L1745
[5] https://elixir.bootlin.com/linux/v6.17-rc3/source/fs/ceph/inode.c#L1777Signed-off-by Viacheslav Dubeyko <[email protected]>
If this is really a bug it should have a Fixes tag.
Quoted 7 lines
Quoted 7 lines
cc Alex Markuze <[email protected]>cc Ilya Dryomov <[email protected]>cc Ceph Development <[email protected]>--- fs/ceph/inode.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
Quoted 7 lines
fs/ceph/inode.c int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req) · line 1740
Quoted 7 lines
fs/ceph/inode.c +0
-0@@ -1739,6 +1739,11 @@ int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req) goto done; }
This "goto done;" is from the if (!rinfo->head->is_target) { test, so we know
Quoted 2 lines
fs/ceph/inode.c int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req) · line 1741
Quoted 2 lines
fs/ceph/inode.c +1
-0+if (!in) {
that this NULL check is not required.
regards,
dan carpenter
Quoted 24 lines
Quoted 23 lines
fs/ceph/inode.c +10
-1+ err = -EINVAL ; + goto done; + } +/* attach proper inode */ if (d_really_is_negative (dn)) {ceph_dir_clear_ordered (dir);@@ -1774,6 +1779,12 @@ int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)doutc (cl," linking snapped dir %p to dn %p\n" , in, req->r_dentry);ceph_dir_clear_ordered (dir); + +if (!in) { + err = -EINVAL ; + goto done; + } +ihold (in); err =splice_dentry (&req->r_dentry, in);if (err <0 ) --2.51.0