+ // (Wouldn't it be nice if the lsm functions worked like openat(),
+ // fchmodat(), mknodat(), readlinkat() so we could do this without
+ // even O_PATH? But no, this is 1990's tech.)
+ int fd = openat(dirtree_parentfd(new), new->name,
+ O_PATH|(O_NOFOLLOW*!(toys.optflags&FLAG_L)));
+
+ if (fd != -1) {
+ if (-1 == lsm_fget_context(fd, (char **)&new->extra) && errno == EBADF)
+ {
+ char hack[32];
+
+ // Work around kernel bug that won't let us read this "metadata" from
+ // the filehandle unless we have permission to read the data. (We can
+ // query the same data in by path, but can't do it through an O_PATH
+ // filehandle, because reasons. But for some reason, THIS is ok? If
+ // they ever fix the kernel, this should stop triggering.)
+
+ sprintf(hack, "/proc/self/fd/%d", fd);
+ lsm_lget_context(hack, (char **)&new->extra);
+ }