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

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