-
Notifications
You must be signed in to change notification settings - Fork 113
Support readonly feature of VirtIO block device #584
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmarks
Benchmark suite | Current: c9e2b5f | Previous: b26e7fc | Ratio |
---|---|---|---|
Dhrystone |
1274 Average DMIPS over 10 runs |
1311 Average DMIPS over 10 runs |
1.03 |
Coremark |
913.979 Average iterations/sec over 10 runs |
920.165 Average iterations/sec over 10 runs |
1.01 |
This comment was automatically generated by workflow using github-action-benchmark.
7beced0
to
d34087d
Compare
I would like to ask @otteryc for reviewing. |
src/riscv.c
Outdated
/* Currently, only used for block image path and permission */ | ||
#define MAX_OPTS 2 | ||
char *vblk_opts[MAX_OPTS]; | ||
memset(vblk_opts, 0, sizeof(char *[MAX_OPTS])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace sizeof(char *[MAX_OPTS])
with ARRAY_SIZE
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace
sizeof(char *[MAX_OPTS])
withARRAY_SIZE
.
ARRAY_SIZE
gets the element count in an array but it does not know the size of each element. To use it, it should be used like this: ARRAY_SIZE(vblk_opts) * sizeof(vblk_opts[0])
.
Thus, for more shorter, I would go with "partial Initialization" which would initialize the rest of the element to 0 by declaring the vblk_opts
as char *vblk_opts[MAX_OPTS] = {NULL}
.
d34087d
to
b26e7fc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
Please consider:
- Refine the corresponding statements in README.md for clarity.
- Fix the
device_features_sel
error also in semu as well.
Add support for comma-separated options for VirtIO devices. For the readonly option, if the string readonly is specified, subsequent VirtIO block device access will be read-only; otherwise, the default behavior is read and write. To implement this, a device_features field is introduced to store device features during initialization. VIRTIO_BLK_F_RO is the feature bit to enable readonly. This feature enhances the security of the VirtIO block device by preventing unintended data corruption in the guest OS when accessing critical data. Additionally, after implementing the VirtIO block device in sysprog21#539, some register access errors were shown and they are fixed in this commit: - reading from the VirtIO DeviceFeatures register now correctly checks the device_features_sel instead of driver_features_sel. - writing to the VirtIO DeviceFeaturesSel register now correctly updates device_features_sel instead of driver_features_sel.
b26e7fc
to
c9e2b5f
Compare
Thank @ChinYikMing for contributing! |
PR sysprog21#584 initializes the vblk only when the vblk CLI option is provided. This leads to a segmentation fault when vblk is not specified, as the capacity of the vblk device is left uninitialized (not set to 0). Previously, the code would always initialize vblk but set its capacity to 0, preventing the VirtIO block driver from interacting with it and avoiding the segmentation fault. However, this original approach was functionally redundant. A better solution is to dynamically load or unload the VirtIO node in the SoC node of the Device Tree based on configuration. To support future others VirtIO devices, the interface of load_dtb() has been refined, it now can parse all options stored in the attribute.
PR sysprog21#584 initializes the vblk only when the vblk CLI option is provided. This leads to a segmentation fault when vblk is not specified, as the capacity of the vblk device is left uninitialized (not set to 0). Previously, the code would always initialize vblk but set its capacity to 0, preventing the VirtIO block driver from interacting with it and avoiding the segmentation fault. However, this original approach was functionally redundant. A better solution is to dynamically load or unload the VirtIO node in the SoC node of the Device Tree based on configuration. To support future others VirtIO devices, the interface of load_dtb() has been refined, it now can parse all options stored in the attribute.
PR sysprog21#584 initializes the vblk only when the vblk CLI option is provided. This leads to a segmentation fault when vblk is not specified, as the capacity of the vblk device is left uninitialized (not set to 0). Previously, the code would always initialize vblk but set its capacity to 0, preventing the VirtIO block driver from interacting with it and avoiding the segmentation fault. However, this original approach was functionally redundant. A better solution is to dynamically load or unload the VirtIO node in the SoC node of the Device Tree based on configuration. To support future others VirtIO devices, the interface of load_dtb() has been refined, it now can parse all options stored in the attribute.
Why?
Enhances the security of the VirtIO block device by preventing unintended data corruption in the guest OS when accessing critical data.
Test
macOS:
Linux
# Create a disk image $ dd if=/dev/zero of=disk.img bs=4M count=32 $ mkfs.ext4 disk.img
Might see something similar:
Might see something similar:
For ext4 image, to maintain the consistency of the file system, the ext4 does the journal replay (which is failed by this error log: "recovery required on readonly filesystem) and
mount
command default use read and write permission. Thus, it has to mount with noload option.# mount -o noload /dev/vda mnt
Try to create a file which should fail on readonly mounted device:
# touch mnt/apple touch: mnt/apple: Read-only file system
Summary by Bito
This pull request introduces a readonly feature for the VirtIO block device, enhancing security by preventing unintended data corruption. Key changes include a readonly flag during initialization, updated function signatures, and improved command line options for usability.Unit tests added: False
Estimated effort to review (1-5, lower is better): 2