Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix coverity defects with CID 147626-28 #5304

Merged
merged 1 commit into from
Nov 8, 2016

Conversation

heary-cao
Copy link
Contributor

@heary-cao heary-cao commented Oct 19, 2016

issues:
fix coverity defects
coverity scan CID:147626, Type:Dereference before null check
coverity scan CID:147628, Type:Dereference before null check

Signed-off-by: cao.xuewen cao.xuewen@zte.com.cn

@mention-bot
Copy link

@heary-cao, thanks for your PR! By analyzing the history of the files in this pull request, we identified @behlendorf, @grwilson and @ahrens to be potential reviewers.

zfs_putpage_commit_cb, pp);
if (zsb->z_log != NULL)
zfs_log_write(zsb->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0,
zfs_putpage_commit_cb, pp);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should never be a possible case in the existing code. Pages which are part of a snapshot should never be allowed to be dirtied, see commit 1e8db77. This check was added just in case somehow a page got dirtied. Now that pretty clearly can never happen or we would have NULL dereferenced here long ago and there are no issues like this open in the tracker. Given that I'd rather remove the NULL check around zil_commit() a few lines down.

This also has the advantage of bringing this code slightly closer inline with upstream at least in terms of intent and assumptions.

@tuxoko what are your thoughts on this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behlendorf
Yes, I agree with you. I don't see why we need this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behlendorf @tuxoko
thank your for review it.

@@ -1701,8 +1701,7 @@ zfs_resume_fs(zfs_sb_t *zsb, const char *osname)
* Since we couldn't setup the sa framework, try to force
* unmount this file system.
*/
if (zsb->z_os)
(void) zfs_umount(zsb->z_sb);
(void) zfs_umount(zsb->z_sb);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good catch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original fix here doesn't look quite right. Take a look at the stack from the original bug report. If zsb->z_os were NULL we should never have been able to make it to where the node finally crashed.

#1518

zfs_resume_fs
  zfs_umount
    zfs_sb_teardown
      dmu_objset_pool <- PANIC

In zfs_sb_teardown several checks for zsb->z_os == NULL are made before it's used which should have prevented this. What exactly in this first check preventing that the other ones aren't?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behlendorf
The commit message says concurrent umount, so it's likely caused by race. Though I don't think the patch actually fix the race. Also, using zfs_umount here looks strange, I'm not sure how things would work in terms of VFS after this call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that zsb->z_os can change outside a lock is definitely racy and problematic. Rather than needlessly change this let's leave it as is for now and I'll make a note in the Coverity analysis. @heary-cao go ahead and drop this hunk when you next refresh this patch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behlendorf @tuxoko
ok,
thanks.

@@ -68,7 +68,8 @@ uu_dprintf_create(const char *name, uu_dprintf_severity_t severity,
{
uu_dprintf_t *D;

if (uu_check_name(name, UU_NAME_DOMAIN) == -1) {
if (name != NULL ||
uu_check_name(name, UU_NAME_DOMAIN) == -1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you trying to do...

@@ -131,7 +131,8 @@ zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
* If we are doing a spa_tryimport() or in recovery mode,
* ignore errors.
*/
if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
if (spa != NULL ||
spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you trying to do....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all defects identified by the Coverity static analysis tool. Specifically these are all cases where Coverity determined there's a NULL check latter in the function but it's pointless because by some call path we've already dereferenced the variable. So either the NULL check is pointless or it needs to be done sooner.

Type:Dereference before null check

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behlendorf
If you look carefully, it doesn't fix null dereference...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right the fix itself is wrong here. It should be spa == NULL in the check so it returns immediately return when no spa is passed. Or better yet just drop think hunk completely. This is an internal function all callers know to always pass a spa. Period.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This chunk isn't correct. Yes it prevents a NULL dereference in this block but the same thing can happen 5 lines down. Please drop think hunk from the patch and we'll address it if need be in a different PR.

@@ -1701,8 +1701,7 @@ zfs_resume_fs(zfs_sb_t *zsb, const char *osname)
* Since we couldn't setup the sa framework, try to force
* unmount this file system.
*/
if (zsb->z_os)
(void) zfs_umount(zsb->z_sb);
(void) zfs_umount(zsb->z_sb);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this change?

zfs_putpage_commit_cb, pp);
if (zsb->z_log != NULL)
zfs_log_write(zsb->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0,
zfs_putpage_commit_cb, pp);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behlendorf
Yes, I agree with you. I don't see why we need this.

@tuxoko
Copy link
Contributor

tuxoko commented Oct 19, 2016

@behlendorf
TBH, I really don't like a mechanical fix like this. Just as this patch shows, when you just try to silence a static analysis, but not really understand what the code you touch is doing. It often make things worse.

@behlendorf
Copy link
Contributor

@tuxoko it's a useful tool as long as it's not used purely mechanically. Each change needs to be understood and you're right it shouldn't be used just to silence a warning but to make the code clearer.

@heary-cao heary-cao force-pushed the before_null_check branch 2 times, most recently from e12cdcb to 18d865c Compare October 21, 2016 08:47
@heary-cao heary-cao changed the title fix coverity defects with CID 147625-26-28-29 fix coverity defects with CID 147626-28-29 Oct 21, 2016
Copy link
Contributor Author

@heary-cao heary-cao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behlendorf , @tuxoko
please review again.
thank your.

zfs_putpage_commit_cb, pp);
if (zsb->z_log != NULL)
zfs_log_write(zsb->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0,
zfs_putpage_commit_cb, pp);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behlendorf @tuxoko
thank your for review it.

@@ -1701,8 +1701,7 @@ zfs_resume_fs(zfs_sb_t *zsb, const char *osname)
* Since we couldn't setup the sa framework, try to force
* unmount this file system.
*/
if (zsb->z_os)
(void) zfs_umount(zsb->z_sb);
(void) zfs_umount(zsb->z_sb);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behlendorf @tuxoko
ok,
thanks.

@@ -131,7 +131,8 @@ zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
* If we are doing a spa_tryimport() or in recovery mode,
* ignore errors.
*/
if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
if (spa != NULL ||
spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This chunk isn't correct. Yes it prevents a NULL dereference in this block but the same thing can happen 5 lines down. Please drop think hunk from the patch and we'll address it if need be in a different PR.

D->uud_name = strdup(name);
if (D->uud_name == NULL) {
uu_free(D);
return (NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention of this function is clearly to allow name to be NULL. Rather than removing this code I'd recommend only running the uu_check_name() when name != NULL. I'd suggest moving to it's own PR too so we can merge the other parts of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behlendorf
thanks for your suggest
commit other PR:#5376
please review it.
thanks.

@heary-cao heary-cao changed the title fix coverity defects with CID 147626-28-29 fix coverity defects with CID 147626-28 Nov 8, 2016
issues:
fix coverity defects
coverity scan CID:147626, Type:Dereference before null check
coverity scan CID:147628, Type:Dereference before null check

Signed-off-by: cao.xuewen cao.xuewen@zte.com.cn
@behlendorf
Copy link
Contributor

@tuxoko any remaining concerns with the trimmed down patch proposed.

@behlendorf behlendorf merged commit a36cc8d into openzfs:master Nov 8, 2016
akatrevorjay pushed a commit to akatrevorjay/zfs that referenced this pull request Nov 29, 2016
CID 147626: Type:Dereference before null check
CID 147628: Type:Dereference before null check

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov
Reviewed-by: Chunwei Chen <david.chen@osnexus.com>
Signed-off-by: cao.xuewen <cao.xuewen@zte.com.cn>
Closes openzfs#5304
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants