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

Kirill Tkhai ktkhai at odin.com
Mon Dec 14 03:07:54 PST 2015


Generate random binfmt_misc entries of different types
and check that they remain registered after the signal.

v2: pr_perror() in cleanup, .desc file and TST_DIR in Makefile

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 test/zdtm/live/static/Makefile         |    1 
 test/zdtm/live/static/binfmt_misc.c    |  194 ++++++++++++++++++++++++++++++++
 test/zdtm/live/static/binfmt_misc.desc |    1 
 3 files changed, 196 insertions(+)
 create mode 100644 test/zdtm/live/static/binfmt_misc.c
 create mode 100644 test/zdtm/live/static/binfmt_misc.desc

diff --git a/test/zdtm/live/static/Makefile b/test/zdtm/live/static/Makefile
index d546832..9f3b519 100644
--- a/test/zdtm/live/static/Makefile
+++ b/test/zdtm/live/static/Makefile
@@ -219,6 +219,7 @@ TST_DIR		=				\
 		mnt_ext_auto			\
 		mnt_ext_master			\
 		mntns_deleted			\
+		binfmt_misc			\
 
 TST_DIR_FILE	=				\
 		chroot				\
diff --git a/test/zdtm/live/static/binfmt_misc.c b/test/zdtm/live/static/binfmt_misc.c
new file mode 100644
index 0000000..9d7394c
--- /dev/null
+++ b/test/zdtm/live/static/binfmt_misc.c
@@ -0,0 +1,194 @@
+#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
+
+char *dirname = "binfmt_misc.dir";
+TEST_OPTION(dirname, string, "binfmt_misc mount directory name", 1);
+
+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(dirname, 0777)) {
+		fail("mkdir");
+		exit(1);
+	}
+
+	if (mount("none", dirname, "binfmt_misc", 0, NULL)) {
+		fail("mount failed");
+		exit(1);
+	}
+
+	/* Register binfmt_entries */
+	sprintf(path, "%s/" "register", dirname);
+	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, "%s/%s", dirname, 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, "%s/%s", dirname, 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, "%s/%s", dirname, 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, "%s/%s", dirname, NAME[i]);
+		fd = open(path, O_WRONLY);
+		if (fd < 0) {
+			pr_perror("open %s", path);
+			continue;
+		}
+		if (write(fd, "-1", 2) != 2)
+			pr_perror("cleanup %s", path);
+		close(fd);
+	}
+
+	umount(dirname);
+	rmdir(dirname);
+
+	return 0;
+}
diff --git a/test/zdtm/live/static/binfmt_misc.desc b/test/zdtm/live/static/binfmt_misc.desc
new file mode 100644
index 0000000..e31c680
--- /dev/null
+++ b/test/zdtm/live/static/binfmt_misc.desc
@@ -0,0 +1 @@
+{'flavor': 'ns', 'flags': 'excl'}



More information about the CRIU mailing list