libkernel: fstat on a socket descriptor throws (FillFileStat) and sceKernelFstat returns EBADF; fix branch ready #4051

Closed
opened 2026-10-11 22:11:27 +00:00 by milescoviello · 0 comments
milescoviello commented 2026-10-11 22:11:27 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-11T22:11:27Z.


fstat on a guest socket descriptor passed the guest number (from 0x10000000 up) to the host, which has no such descriptor, so it threw FillFileStat: fstat failed for fd 268435460. sceKernelFstat returned EBADF for the same descriptor. FreeBSD fills the stat buffer for a socket through soo_stat (sys/kern/sys_socket.c):

	bzero((caddr_t)ub, sizeof (*ub));
	ub->st_mode = S_IFSOCK;
	...
	if ((sb->sb_state & SBS_CANTRCVMORE) == 0 || sbavail(sb))
		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
	ub->st_size = sbavail(sb) - sb->sb_ctl;
	...
	if ((sb->sb_state & SBS_CANTSENDMORE) == 0)
		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;

pru_sense_null (sys/kern/uipc_socket.c) then sets st_blksize = so->so_snd.sb_hiwat.

  • fstat and sceKernelFstat answer a guest socket from the socket table: S_IFSOCK, the read bits until the receive side is shut down and drained, the write bits until the send side is shut down, st_size from the queued bytes (FIONREAD), st_blksize from SO_SNDBUF. Every other field is zero, as after FreeBSD's bzero.
  • shutdown records which side it closed.
  • A closed socket descriptor now gets EBADF instead of the throw.
  • TechnicalDebt.md lists the differences: a peer's shutdown does not clear the bits, Linux reports FIONREAD per datagram and doubles SO_SNDBUF, and socketpair ends report st_dev/st_ino 0.

Fix branch: milescoviello:fix/fstat-guest-socket, one commit (a84aa9ad) on main acc5d294.

Tested: Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags: guest_socket_pair aborts with main's sources (the FillFileStat throw) and passes with the change; all guest_* tests pass (289/289).

I'll open the pull request myself as soon as my open one (#3623) is merged, since the open-PR limit allows one until then.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5805704741 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4051). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-11T22:11:27Z. --- `fstat` on a guest socket descriptor passed the guest number (from `0x10000000` up) to the host, which has no such descriptor, so it threw `FillFileStat: fstat failed for fd 268435460`. `sceKernelFstat` returned `EBADF` for the same descriptor. FreeBSD fills the stat buffer for a socket through `soo_stat` (`sys/kern/sys_socket.c`): ```c bzero((caddr_t)ub, sizeof (*ub)); ub->st_mode = S_IFSOCK; ... if ((sb->sb_state & SBS_CANTRCVMORE) == 0 || sbavail(sb)) ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH; ub->st_size = sbavail(sb) - sb->sb_ctl; ... if ((sb->sb_state & SBS_CANTSENDMORE) == 0) ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH; ``` `pru_sense_null` (`sys/kern/uipc_socket.c`) then sets `st_blksize = so->so_snd.sb_hiwat`. - `fstat` and `sceKernelFstat` answer a guest socket from the socket table: `S_IFSOCK`, the read bits until the receive side is shut down and drained, the write bits until the send side is shut down, `st_size` from the queued bytes (`FIONREAD`), `st_blksize` from `SO_SNDBUF`. Every other field is zero, as after FreeBSD's `bzero`. - `shutdown` records which side it closed. - A closed socket descriptor now gets `EBADF` instead of the throw. - TechnicalDebt.md lists the differences: a peer's shutdown does not clear the bits, Linux reports `FIONREAD` per datagram and doubles `SO_SNDBUF`, and `socketpair` ends report `st_dev`/`st_ino` 0. **Fix branch:** [`milescoviello:fix/fstat-guest-socket`](https://github.com/milescoviello/AnyPS5/tree/fix/fstat-guest-socket), one commit (`a84aa9ad`) on `main` acc5d294. **Tested:** Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags: `guest_socket_pair` aborts with `main`'s sources (the `FillFileStat` throw) and passes with the change; all `guest_*` tests pass (289/289). I'll open the pull request myself as soon as my open one (#3623) is merged, since the open-PR limit allows one until then. AI-assisted: yes
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#4051
No description provided.