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

doc: Fix recently reported issues #181

Merged
merged 2 commits into from
Aug 30, 2024
Merged

Conversation

cfergeau
Copy link
Collaborator

@cfergeau cfergeau commented Aug 20, 2024

This fixes minor (confusing) errors in the documentation and some lint errors.

@openshift-ci openshift-ci bot requested review from anjannath and baude August 20, 2024 09:31
@cfergeau cfergeau changed the title Fix 2 recently reported issues Fix recently reported issues Aug 20, 2024
Copy link
Collaborator

@baude baude left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

openshift-ci bot commented Aug 20, 2024

New changes are detected. LGTM label has been removed.

@cfergeau
Copy link
Collaborator Author

I dropped the SetTrustedProxies commit as it was alreadypresent in git master, and I added another commit to fix recent linting errors.

Copy link
Contributor

@nirs nirs left a comment

Choose a reason for hiding this comment

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

Looks good

Copy link

openshift-ci bot commented Aug 22, 2024

@nirs: changing LGTM is restricted to collaborators

In response to this:

Looks good

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Copy link

openshift-ci bot commented Aug 22, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: baude, nirs

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@cfergeau cfergeau force-pushed the restfuluri branch 4 times, most recently from 73c9e13 to a8420d9 Compare August 22, 2024 16:35
pkg/vf/virtio.go Outdated
@@ -202,7 +202,7 @@ func (dev *VirtioRng) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfigur
// https://developer.apple.com/documentation/virtualization/running_linux_in_a_virtual_machine?language=objc#:~:text=Configure%20the%20Serial%20Port%20Device%20for%20Standard%20In%20and%20Out
func setRawMode(f *os.File) error {
// Get settings for terminal
attr, _ := unix.IoctlGetTermios(int(f.Fd()), unix.TIOCGETA)
attr, _ := unix.IoctlGetTermios(int(f.Fd()), unix.TIOCGETA) //#nosec G115
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this related? this is not mentioned in the commit message.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is the first case in the commit message: « it assumes casting an uintptr file descriptor to int will always be fine ». f.Fd() is an uintptr file descriptor which we cast to int

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, always adding a note to the error number will make this more clear:

//#nosec G115 potential integer overflow 

I wonder why Go is using uintptr for Fd() when internally it is using int:

os/file_unix.go:

func (f *File) Fd() uintptr {
        ...
	return uintptr(f.pfd.Sysfd)
}

Maybe to handle this issue consistently and avoid littering the code with #nosec notes, with add a helper function like:

func unixFd(f *os.File) int {
    // On unix the underlying fd is int, overflow is not possible.
    return int(f.Fd()) //#nosec G115  potential integer overflow
}

Ideally we can prove this in compile time type assertion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good points about adding comments explaining what G115 is and about the helper. It's only func unixFd(fd uintptr) int so that it can be used both in virtio.go and virtionet.go, even if it's trivial, it makes the code easier to read.

pkg/vf/vsock.go Outdated
@@ -19,13 +20,17 @@ func ExposeVsock(vm *VirtualMachine, port uint, vsockPath string, listen bool) (
}

func ConnectVsockSync(vm *VirtualMachine, port uint) (net.Conn, error) {
if port > math.MaxUint32 {
Copy link
Contributor

Choose a reason for hiding this comment

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

If port is actually uint32, why accept uint in this function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changing the function arg to uint32 involves changes in quite a few places, including the public pkg/config API, or it means we need to duplicate this check in several places instead of in a single one.
Since it's quite unlikely one will try to use a huge vsock port, I favoured having the check in a single place, and not changing the rest of the code.

Below is a partial diff of the changes we'd need to make to pass the correct uint32 type over the codebase. I haven't tried to compile it so there are most likely more places which need to change.

diff --git a/pkg/config/config.go b/pkg/config/config.go
index d836d34..648e523 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -23,7 +23,7 @@ type VirtualMachine struct {
 // TimeSync enables synchronization of the host time to the linux guest after the host was suspended.
 // This requires qemu-guest-agent to be running in the guest, and to be listening on a vsock socket
 type TimeSync struct {
-	VsockPort uint `json:"vsockPort"`
+	VsockPort uint32 `json:"vsockPort"`
 }
 
 // The VMComponent interface represents a VM element (device, bootloader, ...)
@@ -179,7 +179,7 @@ func (vm *VirtualMachine) TimeSync() *TimeSync {
 	return vm.Timesync
 }
 
-func TimeSyncNew(vsockPort uint) (VMComponent, error) {
+func TimeSyncNew(vsockPort uint32) (VMComponent, error) {
 	return &TimeSync{
 		VsockPort: vsockPort,
 	}, nil
@@ -201,7 +201,7 @@ func (ts *TimeSync) FromOptions(options []option) error {
 			if err != nil {
 				return err
 			}
-			ts.VsockPort = uint(vsockPort)
+			ts.VsockPort = uint32(vsockPort)
 		default:
 			return fmt.Errorf("unknown option for timesync parameter: %s", option.key)
 		}
diff --git a/pkg/config/virtio.go b/pkg/config/virtio.go
index cba539a..c7acfb7 100644
--- a/pkg/config/virtio.go
+++ b/pkg/config/virtio.go
@@ -47,7 +47,7 @@ type VirtioGPU struct {
 type VirtioVsock struct {
 	// Port is the virtio-vsock port used for this device, see `man vsock` for more
 	// details.
-	Port uint `json:"port"`
+	Port uint32 `json:"port"`
 	// SocketURL is the path to a unix socket on the host to use for the virtio-vsock communication with the guest.
 	SocketURL string `json:"socketURL"`
 	// If true, vsock connections will have to be done from guest to host. If false, vsock connections will only be possible
@@ -531,7 +531,7 @@ func (dev *VirtioBlk) ToCmdLine() ([]string, error) {
 // vsock port, and on the host it will use the unix socket at socketURL.
 // When listen is true, the host will be listening for connections over vsock.
 // When listen  is false, the guest will be listening for connections over vsock.
-func VirtioVsockNew(port uint, socketURL string, listen bool) (VirtioDevice, error) {
+func VirtioVsockNew(port uint32, socketURL string, listen bool) (VirtioDevice, error) {
 	return &VirtioVsock{
 		Port:      port,
 		SocketURL: socketURL,
@@ -560,11 +560,11 @@ func (dev *VirtioVsock) FromOptions(options []option) error {
 		case "socketURL":
 			dev.SocketURL = option.value
 		case "port":
-			port, err := strconv.Atoi(option.value)
+			port, err := strconv.ParseUint(option.value, 10, 32)
 			if err != nil {
 				return err
 			}
-			dev.Port = uint(port)
+			dev.Port = uint32(port)
 		case "listen":
 			dev.Listen = true
 		case "connect":

Copy link
Contributor

Choose a reason for hiding this comment

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

Changing the function arg to uint32 involves changes in quite a few places, including the public pkg/config API,

This sounds like the right way to me. We should use the type system to make errors in runtime impossible if we can.

The example diff needed to change the type looks good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This sounds like the right way to me. We should use the type system to make errors in runtime impossible if we can.

I'll look into this,but I need to double check this does not break compilation for external users of pkg/config though.

pkg/vf/vsock.go Outdated Show resolved Hide resolved
.github/workflows/compile.yml Outdated Show resolved Hide resolved
@cfergeau cfergeau force-pushed the restfuluri branch 2 times, most recently from be4f255 to fe0d39d Compare August 23, 2024 08:59
@nirs
Copy link
Contributor

nirs commented Aug 23, 2024

I think it will be better to split the commit handling possible integer overflow to another PR, and merge the easy changes now.

@cfergeau
Copy link
Collaborator Author

I think it will be better to split the commit handling possible integer overflow to another PR, and merge the easy changes now.

Yep, I was also getting to that conclusion, I initially thought the overflow changes would be a quick fix ;)

@cfergeau
Copy link
Collaborator Author

I think it will be better to split the commit handling possible integer overflow to another PR, and merge the easy changes now.

Yep, I was also getting to that conclusion, I initially thought the overflow changes would be a quick fix ;)

This is #185

This fixes crc-org#173

Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
The doc is using 'mibibytes', and 1024*1024*1024, this commit fixes both
issues, and adds a link to the wikipedia article.

This fixes crc-org#176

Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
@cfergeau cfergeau changed the title Fix recently reported issues doc: Fix recently reported issues Aug 30, 2024
@cfergeau cfergeau merged commit e3e642d into crc-org:main Aug 30, 2024
5 of 6 checks passed
@cfergeau cfergeau deleted the restfuluri branch September 20, 2024 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants