Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: execute statically-linked linux files



On Thu, 6 Jan 2022, Manuel Bouyer wrote:

the second issue is that it expects /emul/linux/proc/self/fd/4 to be a working
symlink, and on NetBSD it's not. Note that with /bin/ls I get something
similar:
armandeche:/local/armandeche1/tmp#ktrace -i ls -l /proc/self/fd/
total 2
crw--w----  1 bouyer  tty    5, 0 Jan  6 17:54 0
crw--w----  1 bouyer  tty    5, 0 Jan  6 17:54 1
crw--w----  1 bouyer  tty    5, 0 Jan  6 17:54 2
lr-xr-xr-x  1 root    wheel  2048 Jan  6 17:54 3 -> /local/armandeche1/tmp

ls: /proc/self/fd//4: Invalid argument
lr-xr-xr-x  1 root    wheel     0 Jan  6 17:54 4

22875      1 ls       CALL  readlink(0x7f7fffb98200,0x7f7fffb98610,0x400)
22875      1 ls       NAMI  "/proc/self/fd//4"
22875      1 ls       RET   readlink -1 errno 22 Invalid argument

If I can trust the ktrace output, fd/4 should point to /etc/spwd.db

On linux, strace shows it reading the link from /proc/self/exec, getting back


This 2nd issue I think I can explain: the fd existed at the start of a
readdir(), but, then is closed sometime when the listing is still in
progress as in the code below:

---
#include <sys/stat.h>
#include <dirent.h>
#include <err.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int
main(void)
{
	const char* const P = "/etc/passwd";
	const char* const D = "/proc/self/fd";
	struct dirent* d;
	DIR* dir;

	int fd, rc = EXIT_FAILURE;

	if ((fd = open(P, O_RDONLY)) == -1)
		err(rc, "open(%s)", P);

	if ((dir = opendir(D)) == NULL)
		err(rc, "opendir(%s)", D);

	int count = 0;
	while ((d = readdir(dir)) != NULL) {
		char buf[PATH_MAX];
		struct stat sb;

		/* close before stat of P happens */
		if (++count == 6) {
			close(fd);
			fd = -1;
		}
		snprintf(buf, sizeof buf, "%s/%s", D, d->d_name);
		if (stat(buf, &sb) == -1) {
			warn("stat(%s)", buf);
			continue;
		}
		printf("%s: stat OK\n", buf);
	}
	closedir(dir);
	close(fd);
	rc = EXIT_SUCCESS;

	return rc;
}
---

Not sure how to fix it though...

-RVP


Home | Main Index | Thread Index | Old Index