When a file descriptor with the flag UF_RESOLVE_BENEATH set is reopened via
openat(fd, "", O_EMPTY_PATH), the kernel performs a fresh VOP_OPEN on
the underlying vnode using the caller's credentials. This allows a
process to escalate access rights (e.g. read-only to read-write) if the
file permissions permit it.
For file descriptors received cross-jail via SCM_RIGHTS, restrict_rights()
sets UF_RESOLVE_BENEATH to confine lookups. However, VOP_OPEN checks
file permissions against the opener's credentials, which may have broad
access within their jail (e.g. root) while the file originated from outside it.
The file's permission bits are not a meaningful in this context.
A exploitable example would be jail(8)/jexec(8) leaking the jail.conf(5) into
a jail read-only and the jail gaining write access to add exec.prepare hooks.
Add a check in fgetvp_lookup() that rejects O_EMPTY_PATH reopens when
the requested access mode (OPENREAD/OPENWRITE) exceeds the original
file descriptor's access mode (FREAD/FWRITE), if UF_RESOLVE_BENEATH is set.
This mirrors the access rights enforcement added to fdescfs nodup reopens.