[CRIU] [PATCH 1/2] zdtm: Add binfmt_misc test

Andrew Vagin avagin at odin.com
Thu Dec 10 22:59:06 PST 2015


On Thu, Dec 10, 2015 at 01:06:10PM +0300, Kirill Tkhai wrote:
> Generate random binfmt_misc entries of different types
> and check that they remain registered after the signal.
> 
> Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
> ---
>  test/zdtm/live/static/binfmt_misc.c |  193 +++++++++++++++++++++++++++++++++++
>  1 file changed, 193 insertions(+)
>  create mode 100644 test/zdtm/live/static/binfmt_misc.c

You need to add binfmt_misc.desc, because this test should be executed in a
new mntns.

Is binfmt_misc containerized in the upstream kernel? If it isn't, should
we dump and restore it on the upstream kernel?

> 
> diff --git a/test/zdtm/live/static/binfmt_misc.c b/test/zdtm/live/static/binfmt_misc.c
> new file mode 100644
> index 0000000..ea12993
> --- /dev/null
> +++ b/test/zdtm/live/static/binfmt_misc.c
> @@ -0,0 +1,193 @@
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <sys/types.h>
> +#include <sys/stat.h>
> +#include <sys/mount.h>
> +#include <linux/limits.h>
> +
> +#include "zdtmtst.h"
> +
> +const char *test_doc	= "Check that binfmt_misc entries remain registered";
> +const char *test_author	= "Kirill Tkhai <ktkhai at odin.com";
> +
> +#define MAX_REG_STR 256
> +#define MAX_MAGIC 16
> +#define MAX_MAGIC_OFFSET 128 /* Max magic+offset */
> +#define MAX_EXTENSION 128
> +
> +#define MNTDIR "binfmt_misc.dir"
> +
> +const char *NAME[2] = { "magic_file", "extension_file" };
> +
> +/* :name:type:offset:magic:mask:interpreter:flags */
> +
> +void create_magic_pattern(char *buf, const char *name)
> +{
> +	int i, magic, mask, offset;
> +
> +	magic = rand() % (MAX_MAGIC + 1);
> +	mask  = (rand() % 2) ? magic : 0;
> +	offset = MAX_MAGIC_OFFSET - magic;
> +	offset = rand() % (offset + 1);
> +
> +	buf += sprintf(buf, ":%s:M:%d:", name, offset);
> +
> +	for (i = 0; i < magic; i++)
> +		buf += sprintf(buf, "\\x%02x", rand() % 256);
> +
> +	buf += sprintf(buf, ":");
> +
> +	for (i = 0; i < mask; i++)
> +		buf += sprintf(buf, "\\x%02x", rand() % 256);
> +
> +	sprintf(buf, ":/bin/interpreter:OCP");
> +}
> +
> +void create_extension_pattern(char *buf, const char *name)
> +{
> +	int i, extension;
> +
> +	extension = rand() % (MAX_EXTENSION + 1);
> +	buf += sprintf(buf, ":%s:E::", name);
> +
> +	for (i = 0; i < extension; i++) {
> +		int c = rand();
> +
> +		if (c == '\0' || c == ':' || c == '\n' || c == '/')
> +			c = '1';
> +		buf += sprintf(buf, "%c", c);
> +	}
> +
> +	sprintf(buf, "::/bin/bash:");
> +}
> +
> +int dump_content(const char *path, char **dump)
> +{
> +	int fd, len;
> +	char *p;
> +
> +	p = *dump = malloc(PAGE_SIZE);
> +	if (!p) {
> +		fail("malloc");
> +		return -1;
> +	}
> +
> +	fd = open(path, O_RDONLY);
> +	if (fd < 0) {
> +		fail("open");
> +		return -1;
> +	}
> +
> +	len = read(fd, p, PAGE_SIZE-1);
> +	close(fd);
> +	if (len <= 0) {
> +		fail("read");
> +		return -1;
> +	}
> +
> +	p[len] = '\0';
> +
> +	return 0;
> +}
> +
> +int main(int argc, char **argv)
> +{
> +	char buf[MAX_REG_STR + 1];
> +	char path[PATH_MAX];
> +	char *dump[2];
> +	int i, fd, len;
> +
> +	test_init(argc, argv);
> +
> +	if (mkdir(MNTDIR, 0777)) {
> +		fail("mkdir");
> +		exit(1);
> +	}
> +
> +	if (mount("none", MNTDIR, "binfmt_misc", 0, NULL)) {
> +		fail("mount failed");
> +		exit(1);
> +	}
> +
> +	/* Register binfmt_entries */
> +	sprintf(path, MNTDIR "/" "register");
> +	fd = open(path, O_WRONLY);
> +	if (!fd) {
> +		fail("open");
> +		exit(1);
> +	}
> +
> +	for (i = 0; i < 2; i++) {
> +		if (i % 2 == 0)
> +			create_magic_pattern(buf, NAME[i]);
> +		else
> +			create_extension_pattern(buf, NAME[i]);
> +
> +		test_msg("string: %s\n", buf);
> +		len = strlen(buf);
> +
> +		if (len != write(fd, buf, len)) {
> +			fail("write %s", NAME[i]);
> +			exit(1);
> +		}
> +	}
> +
> +	close(fd);
> +
> +	/* Disable one of the entries */
> +	sprintf(path, MNTDIR "/%s", NAME[0]);
> +	fd = open(path, O_WRONLY);
> +	if (!fd || write(fd, "0", 1) != 1) {
> +		fail("Can't disable %s\n", path);
> +		exit(1);
> +	}
> +	close(fd);
> +
> +	/* Dump files content */
> +	for (i = 0; i < 2; i ++) {
> +		sprintf(path, MNTDIR "/%s", NAME[i]);
> +		if (dump_content(path, &dump[i]))
> +			exit(1);
> +	}
> +
> +	test_daemon();
> +	test_waitsig();
> +
> +	/* Check */
> +	for (i = 0; i < 2; i ++) {
> +		char *tmp;
> +
> +		sprintf(path, MNTDIR "/%s", NAME[i]);
> +		if (dump_content(path, &tmp))
> +			exit(1);
> +
> +		if (strcmp(tmp, dump[i])) {
> +			fail("Content differs:\n%s\nand\n%s\n", tmp, dump[i]);
> +			exit(1);
> +		}
> +		free(dump[i]);
> +		free(tmp);
> +	}
> +
> +	pass();
> +
> +	/* Clean up */
> +	for (i = 0; i < 2; i++) {
> +		sprintf(path, MNTDIR "/%s", NAME[i]);
> +		fd = open(path, O_WRONLY);
> +		if (fd < 0) {
> +			fail("open %s", path);
> +			continue;
> +		}
> +		if (write(fd, "-1", 2) != 2)
> +			fail("cleanup %s", path);

You need to use pr_perror here. fail() is used only when you check
results.

> +		close(fd);
> +	}
> +
> +	umount(MNTDIR);
> +	rmdir(MNTDIR);
> +
> +	return 0;
> +}
> 


More information about the CRIU mailing list