Skip to content

Commit

Permalink
os: fdtable: restore errno if optional locking ioctl fails
Browse files Browse the repository at this point in the history
The `zvfs_finalize_typed_fd()` function notifies some backends
via `ioctl()` with `ZFD_IOCTL_SET_LOCK`. However, support for
this method and functionality is optional.

In backends that do not support locking, this benign failure can
set `errno` to 95 (`EOPNOTSUPP`) in many circumstances where a
change in `errno` (indicating some kind of failure) is not
appropriate.

Prevent errno poisoning by backing-up and restoring `errno`.

Signed-off-by: Chris Friedt <[email protected]>
  • Loading branch information
cfriedt authored and henrikbrixandersen committed Jan 5, 2025
1 parent ba8025f commit 2a2806b
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/os/fdtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,14 @@ void zvfs_finalize_typed_fd(int fd, void *obj, const struct fd_op_vtable *vtable
* variables to avoid keeping the lock for a long period of time.
*/
if (vtable && vtable->ioctl) {
int prev_errno = errno;

(void)zvfs_fdtable_call_ioctl(vtable, obj, ZFD_IOCTL_SET_LOCK,
&fdtable[fd].lock);
if ((prev_errno != EOPNOTSUPP) && (errno == EOPNOTSUPP)) {
/* restore backed-up errno value if the backend does not support locking */
errno = prev_errno;
}
}
}

Expand Down

0 comments on commit 2a2806b

Please sign in to comment.