Blame SOURCES/cryptsetup-2.0.4-add-blkid-utilities-for-fast-detection-of-device-sig.patch

ac737d
From 12d00da84239c3dcc4560dc60a0c36d534908cc0 Mon Sep 17 00:00:00 2001
ac737d
From: Ondrej Kozina <okozina@redhat.com>
ac737d
Date: Wed, 4 Jul 2018 15:39:11 +0200
ac737d
Subject: [PATCH 1/6] Add blkid utilities for fast detection of device
ac737d
 signatures.
ac737d
ac737d
---
ac737d
 configure.ac      |  21 ++++++++
ac737d
 lib/Makemodule.am |   5 +-
ac737d
 lib/utils_blkid.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
ac737d
 lib/utils_blkid.h |  48 +++++++++++++++++
ac737d
 4 files changed, 231 insertions(+), 1 deletion(-)
ac737d
 create mode 100644 lib/utils_blkid.c
ac737d
 create mode 100644 lib/utils_blkid.h
ac737d
ac737d
diff --git a/configure.ac b/configure.ac
ac737d
index 05da6d6..31508d0 100644
ac737d
--- a/configure.ac
ac737d
+++ b/configure.ac
ac737d
@@ -415,6 +415,26 @@ if test x$enable_internal_argon2 = xyes ; then
ac737d
 fi
ac737d
 AM_CONDITIONAL(CRYPTO_INTERNAL_ARGON2, test x$enable_internal_argon2 = xyes)
ac737d
 
ac737d
+dnl Link with blkid to check for other device types
ac737d
+AC_ARG_ENABLE(blkid, AS_HELP_STRING([--disable-blkid],
ac737d
+	[disable use of blkid for device signature detection and wiping.]), [], [enable_blkid=yes])
ac737d
+
ac737d
+if test x$enable_blkid = xyes ; then
ac737d
+	PKG_CHECK_MODULES([BLKID], [blkid],[AC_DEFINE([HAVE_BLKID], 1, [Define to 1 to use blkid for detection of disk signatures.])],[LIBBLKID_LIBS="-lblkid"])
ac737d
+
ac737d
+	AC_CHECK_HEADERS(blkid/blkid.h,,[AC_MSG_ERROR([You need blkid development library installed.])])
ac737d
+	AC_CHECK_DECLS([ blkid_reset_probe,
ac737d
+			 blkid_probe_set_device,
ac737d
+			 blkid_probe_filter_superblocks_type,
ac737d
+			 blkid_do_safeprobe,
ac737d
+			 blkid_do_probe,
ac737d
+			 blkid_probe_lookup_value
ac737d
+		       ],,
ac737d
+		       [AC_MSG_ERROR([Can not compile with blkid support, disable it by --disable-blkid.])],
ac737d
+		       [#include <blkid/blkid.h>])
ac737d
+fi
ac737d
+AM_CONDITIONAL(HAVE_BLKID, test x$enable_blkid = xyes)
ac737d
+
ac737d
 dnl Magic for cryptsetup.static build.
ac737d
 if test x$enable_static_cryptsetup = xyes; then
ac737d
 	saved_PKG_CONFIG=$PKG_CONFIG
ac737d
@@ -465,6 +485,7 @@ AC_SUBST([CRYPTO_STATIC_LIBS])
ac737d
 
ac737d
 AC_SUBST([JSON_C_LIBS])
ac737d
 AC_SUBST([LIBARGON2_LIBS])
ac737d
+AC_SUBST([BLKID_LIBS])
ac737d
 
ac737d
 AC_SUBST([LIBCRYPTSETUP_VERSION])
ac737d
 AC_SUBST([LIBCRYPTSETUP_VERSION_INFO])
ac737d
diff --git a/lib/Makemodule.am b/lib/Makemodule.am
ac737d
index 5e20039..26178b8 100644
ac737d
--- a/lib/Makemodule.am
ac737d
+++ b/lib/Makemodule.am
ac737d
@@ -30,6 +30,7 @@ libcryptsetup_la_LIBADD = \
ac737d
 	@CRYPTO_LIBS@		\
ac737d
 	@LIBARGON2_LIBS@	\
ac737d
 	@JSON_C_LIBS@		\
ac737d
+	@BLKID_LIBS@		\
ac737d
 	libcrypto_backend.la
ac737d
 
ac737d
 libcryptsetup_la_SOURCES = \
ac737d
@@ -92,4 +93,6 @@ libcryptsetup_la_SOURCES = \
ac737d
 	lib/luks2/luks2_token_keyring.c	\
ac737d
 	lib/luks2/luks2_token.c		\
ac737d
 	lib/luks2/luks2_internal.h	\
ac737d
-	lib/luks2/luks2.h
ac737d
+	lib/luks2/luks2.h		\
ac737d
+	lib/utils_blkid.c		\
ac737d
+	lib/utils_blkid.h
ac737d
diff --git a/lib/utils_blkid.c b/lib/utils_blkid.c
ac737d
new file mode 100644
ac737d
index 0000000..7425bc5
ac737d
--- /dev/null
ac737d
+++ b/lib/utils_blkid.c
ac737d
@@ -0,0 +1,158 @@
ac737d
+/*
ac737d
+ * blkid probe utilities
ac737d
+ *
ac737d
+ * Copyright (C) 2018, Red Hat, Inc. All rights reserved.
ac737d
+ *
ac737d
+ * This program is free software; you can redistribute it and/or
ac737d
+ * modify it under the terms of the GNU General Public License
ac737d
+ * as published by the Free Software Foundation; either version 2
ac737d
+ * of the License, or (at your option) any later version.
ac737d
+ *
ac737d
+ * This program is distributed in the hope that it will be useful,
ac737d
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
ac737d
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ac737d
+ * GNU General Public License for more details.
ac737d
+ *
ac737d
+ * You should have received a copy of the GNU General Public License
ac737d
+ * along with this program; if not, write to the Free Software
ac737d
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ac737d
+ */
ac737d
+
ac737d
+#include <errno.h>
ac737d
+#include <stdio.h>
ac737d
+#include <stdlib.h>
ac737d
+#include <string.h>
ac737d
+#include <unistd.h>
ac737d
+
ac737d
+#include "utils_blkid.h"
ac737d
+
ac737d
+#ifdef HAVE_BLKID
ac737d
+#include <blkid/blkid.h>
ac737d
+struct blkid_handle {
ac737d
+	int fd;
ac737d
+	blkid_probe pr;
ac737d
+};
ac737d
+#endif
ac737d
+
ac737d
+void blk_set_chains_for_fast_detection(struct blkid_handle *h)
ac737d
+{
ac737d
+#ifdef HAVE_BLKID
ac737d
+	blkid_probe_enable_partitions(h->pr, 1);
ac737d
+	blkid_probe_set_partitions_flags(h->pr, 0);
ac737d
+
ac737d
+	blkid_probe_enable_superblocks(h->pr, 1);
ac737d
+	blkid_probe_set_superblocks_flags(h->pr, BLKID_SUBLKS_TYPE);
ac737d
+#endif
ac737d
+}
ac737d
+
ac737d
+int blk_init_by_path(struct blkid_handle **h, const char *path)
ac737d
+{
ac737d
+	int r = -ENOTSUP;
ac737d
+#ifdef HAVE_BLKID
ac737d
+	struct blkid_handle *tmp = malloc(sizeof(*tmp));
ac737d
+	if (!tmp)
ac737d
+		return -ENOMEM;
ac737d
+
ac737d
+	tmp->fd = -1;
ac737d
+
ac737d
+	tmp->pr = blkid_new_probe_from_filename(path);
ac737d
+	if (!tmp->pr) {
ac737d
+		free(tmp);
ac737d
+		return -EINVAL;
ac737d
+	}
ac737d
+
ac737d
+	*h = tmp;
ac737d
+
ac737d
+	r = 0;
ac737d
+#endif
ac737d
+	return r;
ac737d
+}
ac737d
+
ac737d
+int blk_superblocks_filter_luks(struct blkid_handle *h)
ac737d
+{
ac737d
+	int r = -ENOTSUP;
ac737d
+#ifdef HAVE_BLKID
ac737d
+	char *luks_filter[] = {
ac737d
+		"crypto_LUKS",
ac737d
+		NULL
ac737d
+	};
ac737d
+	r = blkid_probe_filter_superblocks_type(h->pr, BLKID_FLTR_NOTIN, luks_filter);
ac737d
+#endif
ac737d
+	return r;
ac737d
+}
ac737d
+
ac737d
+blk_probe_status blk_safeprobe(struct blkid_handle *h)
ac737d
+{
ac737d
+	int r = -1;
ac737d
+#ifdef HAVE_BLKID
ac737d
+	r = blkid_do_safeprobe(h->pr);
ac737d
+#endif
ac737d
+	switch (r) {
ac737d
+	case -2:
ac737d
+		return PRB_AMBIGUOUS;
ac737d
+	case 1:
ac737d
+		return PRB_EMPTY;
ac737d
+	case 0:
ac737d
+		return PRB_OK;
ac737d
+	default:
ac737d
+		return PRB_FAIL;
ac737d
+	}
ac737d
+}
ac737d
+
ac737d
+int blk_is_partition(struct blkid_handle *h)
ac737d
+{
ac737d
+	int r = 0;
ac737d
+#ifdef HAVE_BLKID
ac737d
+	r = blkid_probe_has_value(h->pr, "PTTYPE");
ac737d
+#endif
ac737d
+	return r;
ac737d
+}
ac737d
+
ac737d
+int blk_is_superblock(struct blkid_handle *h)
ac737d
+{
ac737d
+	int r = 0;
ac737d
+#ifdef HAVE_BLKID
ac737d
+	r = blkid_probe_has_value(h->pr, "TYPE");
ac737d
+#endif
ac737d
+	return r;
ac737d
+}
ac737d
+
ac737d
+const char *blk_get_partition_type(struct blkid_handle *h)
ac737d
+{
ac737d
+	const char *value = NULL;
ac737d
+#ifdef HAVE_BLKID
ac737d
+	(void) blkid_probe_lookup_value(h->pr, "PTTYPE", &value, NULL);
ac737d
+#endif
ac737d
+	return value;
ac737d
+}
ac737d
+
ac737d
+const char *blk_get_superblock_type(struct blkid_handle *h)
ac737d
+{
ac737d
+	const char *value = NULL;
ac737d
+#ifdef HAVE_BLKID
ac737d
+	(void) blkid_probe_lookup_value(h->pr, "TYPE", &value, NULL);
ac737d
+#endif
ac737d
+	return value;
ac737d
+}
ac737d
+
ac737d
+void blk_free(struct blkid_handle *h)
ac737d
+{
ac737d
+#ifdef HAVE_BLKID
ac737d
+	if (!h)
ac737d
+		return;
ac737d
+
ac737d
+	if (h->pr)
ac737d
+		blkid_free_probe(h->pr);
ac737d
+
ac737d
+	free(h);
ac737d
+#endif
ac737d
+}
ac737d
+
ac737d
+int blk_supported(void)
ac737d
+{
ac737d
+	int r = 0;
ac737d
+#ifdef HAVE_BLKID
ac737d
+	r = 1;
ac737d
+#endif
ac737d
+	return r;
ac737d
+}
ac737d
diff --git a/lib/utils_blkid.h b/lib/utils_blkid.h
ac737d
new file mode 100644
ac737d
index 0000000..d18b0a0
ac737d
--- /dev/null
ac737d
+++ b/lib/utils_blkid.h
ac737d
@@ -0,0 +1,48 @@
ac737d
+/*
ac737d
+ * blkid probe utilities
ac737d
+ *
ac737d
+ * Copyright (C) 2018, Red Hat, Inc. All rights reserved.
ac737d
+ *
ac737d
+ * This program is free software; you can redistribute it and/or
ac737d
+ * modify it under the terms of the GNU General Public License
ac737d
+ * as published by the Free Software Foundation; either version 2
ac737d
+ * of the License, or (at your option) any later version.
ac737d
+ *
ac737d
+ * This program is distributed in the hope that it will be useful,
ac737d
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
ac737d
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ac737d
+ * GNU General Public License for more details.
ac737d
+ *
ac737d
+ * You should have received a copy of the GNU General Public License
ac737d
+ * along with this program; if not, write to the Free Software
ac737d
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ac737d
+ */
ac737d
+
ac737d
+#ifndef _UTILS_BLKID_H
ac737d
+#define _UTILS_BLKID_H
ac737d
+
ac737d
+struct blkid_handle;
ac737d
+
ac737d
+typedef enum { PRB_OK = 0, PRB_EMPTY, PRB_AMBIGUOUS, PRB_FAIL } blk_probe_status;
ac737d
+
ac737d
+int blk_init_by_path(struct blkid_handle **h, const char *path);
ac737d
+
ac737d
+void blk_free(struct blkid_handle *h);
ac737d
+
ac737d
+void blk_set_chains_for_fast_detection(struct blkid_handle *h);
ac737d
+
ac737d
+int blk_superblocks_filter_luks(struct blkid_handle *h);
ac737d
+
ac737d
+blk_probe_status blk_safeprobe(struct blkid_handle *h);
ac737d
+
ac737d
+int blk_is_partition(struct blkid_handle *h);
ac737d
+
ac737d
+int blk_is_superblock(struct blkid_handle *h);
ac737d
+
ac737d
+const char *blk_get_partition_type(struct blkid_handle *h);
ac737d
+
ac737d
+const char *blk_get_superblock_type(struct blkid_handle *h);
ac737d
+
ac737d
+int blk_supported(void);
ac737d
+
ac737d
+#endif
ac737d
-- 
ac737d
1.8.3.1
ac737d
ac737d
--- cryptsetup-2.0.3.old/aclocal.m4	2018-05-03 21:36:53.000000000 +0200
ac737d
+++ cryptsetup-2.0.3/aclocal.m4	2018-07-16 15:37:34.935817650 +0200
ac737d
@@ -31,7 +31,7 @@ To do so, use the procedure documented b
ac737d
 # WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
ac737d
 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
ac737d
 #
ac737d
-# Last-changed: 2014-10-02
ac737d
+# Last-changed: 2018-07-16
ac737d
 
ac737d
 
ac737d
 dnl AM_PATH_LIBGCRYPT([MINIMUM-VERSION,