Skip to content

Commit

Permalink
vfio-ccw: Do not read region ret_code after write
Browse files Browse the repository at this point in the history
A pwrite() call returns the number of bytes written (or -1 on error),
and vfio-ccw compares this number with the size of the region to
determine if an error had occurred or not.

If they are not equal, this is a failure and the errno is used to
determine exactly how things failed. An errno of zero is possible
(though unlikely) in this situation and would be translated to a
successful operation.

If they ARE equal, the ret_code field is read from the region to
determine how to proceed. While the kernel sets the ret_code field
as necessary, the region and thus this field is not "written back"
to the user. So the value can only be what it was initialized to,
which is zero.

So, let's convert an unexpected length with errno of zero to a
return code of -EFAULT, and explicitly set an expected length to
a return code of zero. This will be a little safer and clearer.

Suggested-by: Matthew Rosato <mjrosato@linux.ibm.com>
Signed-off-by: Eric Farman <farman@linux.ibm.com>
Message-Id: <20210303160739.2179378-1-farman@linux.ibm.com>
Signed-off-by: Cornelia Huck <cohuck@redhat.com>
  • Loading branch information
efarman authored and cohuck committed Mar 4, 2021
1 parent a54b8ac commit d6cd663
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions hw/vfio/ccw.c
Expand Up @@ -104,9 +104,9 @@ static IOInstEnding vfio_ccw_handle_request(SubchDev *sch)
goto again;
}
error_report("vfio-ccw: write I/O region failed with errno=%d", errno);
ret = -errno;
ret = errno ? -errno : -EFAULT;
} else {
ret = region->ret_code;
ret = 0;
}
switch (ret) {
case 0:
Expand Down Expand Up @@ -192,9 +192,9 @@ static int vfio_ccw_handle_clear(SubchDev *sch)
goto again;
}
error_report("vfio-ccw: write cmd region failed with errno=%d", errno);
ret = -errno;
ret = errno ? -errno : -EFAULT;
} else {
ret = region->ret_code;
ret = 0;
}
switch (ret) {
case 0:
Expand Down Expand Up @@ -232,9 +232,9 @@ static int vfio_ccw_handle_halt(SubchDev *sch)
goto again;
}
error_report("vfio-ccw: write cmd region failed with errno=%d", errno);
ret = -errno;
ret = errno ? -errno : -EFAULT;
} else {
ret = region->ret_code;
ret = 0;
}
switch (ret) {
case 0:
Expand Down

0 comments on commit d6cd663

Please sign in to comment.