[PATCH] Implement decoding of NS_* ioctl commands

Dmitry V. Levin ldv at altlinux.org
Thu Apr 13 14:40:08 UTC 2017


On Thu, Apr 13, 2017 at 07:37:59PM +0700, Nikolay Marchuk wrote:
> * configure.ac (AC_CHECK_HEADERS): Add linux/nsfs.h.
> * defs.h (DECL_IOCTL(nsfs)): New prototype.
> (setns_types): Make global.
> * ioctl.c (ioctl_decode): Call nsfs_ioctl for 0xb7 code.
> * nsfs.c: New file.
> * nsfs.h: Likewise.
> * Makefile.am (strace_SOURCES): Add them.
> * tests/ioctl_nsfs.c: New file.
> * tests/ioctl_nsfs.test: Likewise.
> * tests/.gitignore: Add ioctl_nsfs.
> * tests/Makefile.am (check_PROGRAMS): Likewise.
> (DECODER_TESTS): Add ioctl_nsfs.test.

btw, this change deserves a NEWS entry.

[...]
> +#include "tests.h"
> +
> +#include <fcntl.h>
> +#include <sched.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <sys/ioctl.h>
> +#include <sys/wait.h>
> +#include <unistd.h>
> +#include "nsfs.h"
> +
> +#ifndef CLONE_NEWUSER
> +# define CLONE_NEWUSER 0x10000000
> +#endif
> +
> +static void
> +test_no_namespace(void)
> +{
> +	ioctl(-1, NS_GET_USERNS);
> +	printf("ioctl(-1, NS_GET_USERNS) = -1 EBADF (%m)\n");
> +	ioctl(-1, NS_GET_PARENT);
> +	printf("ioctl(-1, NS_GET_PARENT) = -1 EBADF (%m)\n");
> +	ioctl(-1, NS_GET_NSTYPE);
> +	printf("ioctl(-1, NS_GET_NSTYPE) = -1 EBADF (%m)\n");
> +	ioctl(-1, NS_GET_OWNER_UID, NULL);
> +	printf("ioctl(-1, NS_GET_OWNER_UID, NULL) = -1 EBADF (%m)\n");
> +}
> +
> +static void
> +test_clone(pid_t pid)
> +{
> +	char path[sizeof("/proc/%d/ns/user") + sizeof(int)*3];
> +	snprintf(path, sizeof(path), "/proc/%d/ns/user", pid);
> +
> +	int ns_fd = open(path, O_RDONLY);
> +	if (ns_fd == -1)
> +		perror_msg_and_skip("open: %s", path);
> +
> +	int userns_fd = ioctl(ns_fd, NS_GET_USERNS);
> +	printf("ioctl(%d, NS_GET_USERNS) = %s\n", ns_fd, sprintrc(userns_fd));
> +
> +	int parent_ns_fd = ioctl(userns_fd, NS_GET_PARENT);
> +	printf("ioctl(%d, NS_GET_PARENT) = %s\n", userns_fd,
> +	       sprintrc(parent_ns_fd));

Please fold after format string.

> +
> +	int nstype = ioctl(userns_fd, NS_GET_NSTYPE);
> +	if (nstype == -1) {
> +		printf("ioctl(%d, NS_GET_NSTYPE) = %s\n",
> +		       userns_fd, sprintrc(nstype));
> +	} else {
> +		printf("ioctl(%d, NS_GET_NSTYPE) = %d (CLONE_NEWUSER)\n",
> +		       userns_fd, nstype);
> +	}
> +
> +	TAIL_ALLOC_OBJECT_CONST_PTR(unsigned int, uid);
> +	int rc = ioctl(userns_fd, NS_GET_OWNER_UID, uid);
> +	if (rc == -1) {
> +		printf("ioctl(%d, NS_GET_OWNER_UID, %p) = %s\n",
> +		       userns_fd, uid, sprintrc(rc));
> +	} else {
> +		printf("ioctl(%d, NS_GET_OWNER_UID, [%u]) = %d\n",
> +		       userns_fd, *uid, rc);
> +	}
> +}
> +
> +static int
> +child(void *arg)
> +{
> +	int *pipefd = (int *) arg;
> +	close(pipefd[1]);
> +	/* Wait for EOF from pipe. */
> +	if(read(pipefd[0], &pipefd[1], 1))

Please put a space after if.

> +		perror_msg_and_fail("read");
> +	return 0;
> +}
> +
> +static void
> +test_user_namespace(void)
> +{
> +	pid_t pid;
> +	int pipefd[2];
> +	int rc, status;
> +
> +	rc = pipe(pipefd);
> +	if (rc == -1)
> +		perror_msg_and_fail("pipe");
> +
> +	pid = clone(child, tail_alloc(1) + 1, (CLONE_NEWUSER | CLONE_UNTRACED
> +	            | SIGCHLD), pipefd);

Please fold after the 2nd argument.
Also, the brackets around flags are redundant.

> +	if (pid == -1) {
> +		perror("clone");
> +		return;
> +	}
> +	close(pipefd[0]);
> +	test_clone(pid);
> +	close(pipefd[1]);
> +	if (wait(&status) != pid || !WIFEXITED(status)
> +	    || WEXITSTATUS(status) != 0) {
> +		perror_msg_and_fail("wait");
> +	}

It's fine to perror_msg_and_fail if wait failed, otherwise errno
is undefined and error_msg_and_fail should be used instead.

No need to use these macros for status check, it's either zero or not.

If wait returned pid but status is nonzero, the diagnostics message
should include the status value.


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20170413/e835378d/attachment.bin>


More information about the Strace-devel mailing list