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

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