Skip to content

RHEL-183405: [viostor] Validate blk_size to prevent divide-by-zero - #1586

Open
harshapa-rh wants to merge 1 commit into
virtio-win:masterfrom
harshapa-rh:viostor-blk-size
Open

RHEL-183405: [viostor] Validate blk_size to prevent divide-by-zero#1586
harshapa-rh wants to merge 1 commit into
virtio-win:masterfrom
harshapa-rh:viostor-blk-size

Conversation

@harshapa-rh

Copy link
Copy Markdown

…de-by-zero

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request adds a validation check for the block size (blk_size) retrieved from the device configuration in viostor/virtio_stor_hw_helper.c to ensure it is not smaller than SECTOR_SIZE. If it is, a default value is used. The review feedback suggests correcting the format specifier in the debug print from %d to %u to match the unsigned type of the variable, and adjusting the brace placement to maintain consistent coding style.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread viostor/virtio_stor_hw_helper.c Outdated
@harshapa-rh
harshapa-rh marked this pull request as draft July 1, 2026 04:25
@YanVugenfirer

Copy link
Copy Markdown
Collaborator

Hi @harshapa-rh

Please squash all the commits

It is better to run clang-format locally first

@YanVugenfirer YanVugenfirer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Changes requested in the previous comment

@YanVugenfirer

Copy link
Copy Markdown
Collaborator

BTW: in PR and commits use the parent Jira key: https://redhat.atlassian.net/browse/RHEL-183405

@harshapa-rh harshapa-rh changed the title RHEL-188074: [viostor] Adjust blk_size to SECTOR_SIZE to prevent divide-by-zero RHEL-183405: [viostor] Adjust blk_size to SECTOR_SIZE to prevent divide-by-zero Jul 1, 2026
@harshapa-rh
harshapa-rh requested a review from YanVugenfirer July 1, 2026 09:51

@YanVugenfirer YanVugenfirer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  1. Fix the commit message. The squashed commit message should contain only one message
  2. We want to validate that v is not zero. If it is zero, propagate error to initialization functions so they will fail.
  3. We probably cannot do anything useful on reconfiguration notification.

Comment thread viostor/virtio_stor_hw_helper.c Outdated
if (CHECKBIT(adaptExt->features, VIRTIO_BLK_F_BLK_SIZE))
{
virtio_get_config(&adaptExt->vdev, FIELD_OFFSET(blk_config, blk_size), &v, sizeof(v));
if (v < SECTOR_SIZE)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  1. Fix the commit message. The squashed commit message should contain only one message
  2. We want to validate that v is not zero. If it is zero, propagate error to initialization functions so they will fail.
  3. We probably cannot do anything useful on reconfiguration notification.

@harshapa-rh
harshapa-rh force-pushed the viostor-blk-size branch 2 times, most recently from ac46fcd to c92b214 Compare July 3, 2026 07:05
@harshapa-rh harshapa-rh changed the title RHEL-183405: [viostor] Adjust blk_size to SECTOR_SIZE to prevent divide-by-zero RHEL-183405: [viostor] Validate blk_size to prevent divide-by-zero Jul 3, 2026
@harshapa-rh
harshapa-rh force-pushed the viostor-blk-size branch 4 times, most recently from 513de0c to 11e7ccc Compare July 3, 2026 09:37
@harshapa-rh
harshapa-rh requested a review from YanVugenfirer July 3, 2026 09:44
Signed-off-by: Harshal Patil <harshapa@redhat.com>
Comment thread viostor/virtio_stor_hw_helper.c Outdated
virtio_get_config(&adaptExt->vdev, FIELD_OFFSET(blk_config, blk_size), &v, sizeof(v));
if (v == 0)
{
RhelDbgPrint(TRACE_LEVEL_ERROR, "Invalid blk_size %u from device config, using default\n", v);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The debug print is misleading. Please remove the ", using default" part.
The failure of the function will cause the device not to initialize.

BTW: Did you test the fix by installing the driver? Also it is worth to return 0 on purpose from the host (use virtio-blk as secondary device in such case)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I fixed the debug print. I was able to install the driver but I couldn't simulate this specific test case.

@harshapa-rh
harshapa-rh requested a review from YanVugenfirer July 5, 2026 13:51
@YanVugenfirer
YanVugenfirer marked this pull request as ready for review July 5, 2026 14:15
@YanVugenfirer

Copy link
Copy Markdown
Collaborator

run tests

Comment on lines +667 to +671
if (v == 0)
{
RhelDbgPrint(TRACE_LEVEL_ERROR, "Invalid blk_size %u from device config\n", v);
return FALSE;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This early return means you get a partially updated adaptExt. I think this is unexpected, you always want a valid new state or the unchanged old state.

It probably doesn't matter when called from VirtIoFindAdapter() or VirtIoHwReinitialize(), where failure kills the whole device anyway, but VirtIoConfigUpdated() ignores the error silently. The driver would then keep partially updated values, but never notify StorPort about the changes, potentially making assumptions inconsistent between the two layers (though of course, both are now inconsistent with the device anyway).

The other, probably better option is making sure that the failure path in VirtIoConfigUpdated() actually leads to a disabled device, too.

@kevmw

kevmw commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Please update the commit message with the motivation for the fix. Is this purely theoretical or is there a way to actually end up in this situation? Please also state there why the chosen solution (fail device initialisation on error, but silently ignore the error on runtime updates) is the correct/desirable one.

@harshapa-rh

Copy link
Copy Markdown
Author

@kevmw While reproducing I noticed that standard QEMU already prevents this. It will reject these parameters. So this can either be closed as is to reduce redundancy or we can keep this for good measure after I make any suggested fixes.

@YanVugenfirer please review the discussion and decide the best course of action. Thank you.

@kevmw

kevmw commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Hardening can still be a good idea, so I'm not against addressing it even though it can't be reproduced with QEMU. The commit message should just point this out for clarity.

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.

3 participants