sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

readlink returns ENOENT instead of EINVAL for non-symlinks (v0.8.9 regression)

Open ka2n opened this issue 1 month ago • 0 comments

Since v0.8.9, readlink(2) on an existing non-symlink returns ENOENT instead of EINVAL inside the sandbox, so glibc realpath(3) fails (breaks Turborepo, mise, Rust canonicalize, Node realpathSync.native). v0.8.8 is fine; main (a0eb434) still affected.

// realpath3.c
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int c, char **v) {
  for (int i = 1; i < c; i++) {
    char *r = realpath(v[i], NULL);
    printf("realpath(%s) = %s\n", v[i], r ? r : strerror(errno));
  }
}
cc -o realpath3 realpath3.c
sandlock run -r / -- readlink -v /etc   # v0.8.8: Invalid argument / v0.8.9: No such file or directory
sandlock run -r / -- ./realpath3 /etc   # v0.8.8: /etc               / v0.8.9: No such file or directory

Cause: the Readlink handler added in #259 calls readlinkat(fd, "") on an O_PATH fd and forwards its errno. For an empty path the kernel returns ENOENT on a non-symlink, not EINVAL (fs/stat.c: error = (name->name[0] == '\0') ? -ENOENT : -EINVAL;).

Suggested fix: when the caller passed a non-empty path and readlinkat(fd, "") fails with ENOENT, return EINVAL.

ka2n avatar Oct 06 '26 01:10 ka2n