dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/0036-libblkid-add-BitLocker-detection.patch

31b890
From ae7b79ff8a7fb576c018bc9a7eaf9e135b7b553e Mon Sep 17 00:00:00 2001
31b890
From: Karel Zak <kzak@redhat.com>
31b890
Date: Tue, 24 Apr 2018 10:57:48 +0200
31b890
Subject: [PATCH 36/40] libblkid: add BitLocker detection
31b890
31b890
Supported:
31b890
* WinVista version
31b890
* Win7 and later versions (based on NTFS)
31b890
* BitLockerToGo (for removable media; based on FAT32)
31b890
31b890
Unfortunately, it's without LABEL and UUID. It seems BitLocker does
31b890
not use volume_label and volume_serial stuff from NTFS header.
31b890
31b890
Upstream: http://github.com/karelzak/util-linux/commit/136f89ce5ed8cd159a1c56b5a775dada2363ecd3
31b890
Upstream: http://github.com/karelzak/util-linux/commit/47afae0caaa2b3440d6ac812079e3ada5f2aa0bd (bitlocker.c part)
31b890
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1812576
31b890
Addresses: https://github.com/karelzak/util-linux/issues/617
31b890
Signed-off-by: Karel Zak <kzak@redhat.com>
31b890
---
31b890
 libblkid/src/Makemodule.am             |   1 +
31b890
 libblkid/src/superblocks/bitlocker.c   | 191 +++++++++++++++++++++++++
31b890
 libblkid/src/superblocks/superblocks.c |   1 +
31b890
 libblkid/src/superblocks/superblocks.h |   3 +
31b890
 libblkid/src/superblocks/vfat.c        |   3 +
31b890
 5 files changed, 199 insertions(+)
31b890
 create mode 100644 libblkid/src/superblocks/bitlocker.c
31b890
31b890
diff --git a/libblkid/src/Makemodule.am b/libblkid/src/Makemodule.am
31b890
index 0e1c765fb..ea0230702 100644
31b890
--- a/libblkid/src/Makemodule.am
31b890
+++ b/libblkid/src/Makemodule.am
31b890
@@ -47,6 +47,7 @@ libblkid_la_SOURCES = \
31b890
 	libblkid/src/superblocks/bcache.c \
31b890
 	libblkid/src/superblocks/befs.c \
31b890
 	libblkid/src/superblocks/bfs.c \
31b890
+	libblkid/src/superblocks/bitlocker.c \
31b890
 	libblkid/src/superblocks/btrfs.c \
31b890
 	libblkid/src/superblocks/cramfs.c \
31b890
 	libblkid/src/superblocks/ddf_raid.c \
31b890
diff --git a/libblkid/src/superblocks/bitlocker.c b/libblkid/src/superblocks/bitlocker.c
31b890
new file mode 100644
31b890
index 000000000..111edf39b
31b890
--- /dev/null
31b890
+++ b/libblkid/src/superblocks/bitlocker.c
31b890
@@ -0,0 +1,191 @@
31b890
+/*
31b890
+ * Copyright (C) 2018 Karel Zak <kzak@redhat.com>
31b890
+ *
31b890
+ * This file may be redistributed under the terms of the
31b890
+ * GNU Lesser General Public License.
31b890
+ */
31b890
+#include <stdio.h>
31b890
+#include <stdlib.h>
31b890
+#include <unistd.h>
31b890
+#include <string.h>
31b890
+#include <errno.h>
31b890
+#include <ctype.h>
31b890
+#include <stdint.h>
31b890
+
31b890
+#include "superblocks.h"
31b890
+
31b890
+#define BDE_HDR_SIZE	512
31b890
+#define BDE_HDR_OFFSET	0
31b890
+
31b890
+struct bde_header_win7 {
31b890
+/*   0 */ unsigned char	boot_entry_point[3];
31b890
+/*   3 */ unsigned char	fs_signature[8];
31b890
+/*  11 */ unsigned char	__dummy1[67 - 11];
31b890
+/*  67 */ uint32_t      volume_serial;		/* NTFS uses 64bit serial number */
31b890
+/*  71 */ unsigned char volume_label[11];	/* "NO NAME\x20\x20\x20\x20" only */
31b890
+/*  82 */ unsigned char __dummy2[160 - 82];
31b890
+/* 160 */ unsigned char guid[16];		/* BitLocker specific GUID */
31b890
+/* 176 */ uint64_t      fve_metadata_offset;
31b890
+} __attribute__((packed));
31b890
+
31b890
+
31b890
+struct bde_header_togo {
31b890
+/*   0 */ unsigned char	boot_entry_point[3];
31b890
+/*   3 */ unsigned char	fs_signature[8];
31b890
+/*  11 */ unsigned char	__dummy[424 - 11];
31b890
+/* 424 */ unsigned char guid[16];
31b890
+/* 440 */ uint64_t      fve_metadata_offset;
31b890
+} __attribute__((packed));
31b890
+
31b890
+
31b890
+struct bde_fve_metadata {
31b890
+/*   0 */ unsigned char  signature[8];
31b890
+/*   8 */ uint16_t       size;
31b890
+/*  10 */ uint16_t       version;
31b890
+};
31b890
+
31b890
+enum {
31b890
+	BDE_VERSION_VISTA = 0,
31b890
+	BDE_VERSION_WIN7,
31b890
+	BDE_VERSION_TOGO
31b890
+};
31b890
+
31b890
+#define BDE_MAGIC_VISTA		"\xeb\x52\x90-FVE-FS-"
31b890
+#define BDE_MAGIC_WIN7		"\xeb\x58\x90-FVE-FS-"
31b890
+#define BDE_MAGIC_TOGO		"\xeb\x58\x90MSWIN4.1"
31b890
+
31b890
+#define BDE_MAGIC_FVE		"-FVE-FS-"
31b890
+
31b890
+static int get_bitlocker_type(const unsigned char *buf)
31b890
+{
31b890
+	size_t i;
31b890
+	static const char *map[] = {
31b890
+		[BDE_VERSION_VISTA] = BDE_MAGIC_VISTA,
31b890
+		[BDE_VERSION_WIN7]  = BDE_MAGIC_WIN7,
31b890
+		[BDE_VERSION_TOGO]  = BDE_MAGIC_TOGO
31b890
+	};
31b890
+
31b890
+	for (i = 0; i < ARRAY_SIZE(map); i++) {
31b890
+		if (memcmp(buf, map[i], 11) == 0)
31b890
+			return (int) i;
31b890
+	}
31b890
+
31b890
+	return -1;
31b890
+}
31b890
+
31b890
+/* Returns: < 0 error, 1 nothing, 0 success
31b890
+ */
31b890
+static int get_bitlocker_headers(blkid_probe pr,
31b890
+				int *type,
31b890
+				const unsigned char **buf_hdr,
31b890
+				const unsigned char **buf_fve)
31b890
+{
31b890
+
31b890
+	const unsigned char *buf;
31b890
+	const struct bde_fve_metadata *fve;
31b890
+	uint64_t off = 0;
31b890
+	int kind;
31b890
+
31b890
+	if (buf_hdr)
31b890
+		*buf_hdr = NULL;
31b890
+	if (buf_fve)
31b890
+		*buf_fve = NULL;
31b890
+	if (type)
31b890
+		*type = -1;
31b890
+
31b890
+	buf = blkid_probe_get_buffer(pr, BDE_HDR_OFFSET, BDE_HDR_SIZE);
31b890
+	if (!buf)
31b890
+		return errno ? -errno : 1;
31b890
+
31b890
+	kind = get_bitlocker_type(buf);
31b890
+
31b890
+	/* Check BitLocker header */
31b890
+	switch (kind) {
31b890
+	case BDE_VERSION_WIN7:
31b890
+		off = le64_to_cpu(((const struct bde_header_win7 *) buf)->fve_metadata_offset);
31b890
+		break;
31b890
+	case BDE_VERSION_TOGO:
31b890
+		off = le64_to_cpu(((const struct bde_header_togo *) buf)->fve_metadata_offset);
31b890
+		break;
31b890
+	case BDE_VERSION_VISTA:
31b890
+		goto done;
31b890
+	default:
31b890
+		goto nothing;
31b890
+	}
31b890
+
31b890
+	if (!off)
31b890
+		goto nothing;
31b890
+	if (buf_hdr)
31b890
+		*buf_hdr = buf;
31b890
+
31b890
+	/* Check Bitlocker FVE metadata header */
31b890
+	buf = blkid_probe_get_buffer(pr, off, sizeof(struct bde_fve_metadata));
31b890
+	if (!buf)
31b890
+		return errno ? -errno : 1;
31b890
+
31b890
+	fve = (const struct bde_fve_metadata *) buf;
31b890
+	if (memcmp(fve->signature, BDE_MAGIC_FVE, sizeof(fve->signature)) != 0)
31b890
+		goto nothing;
31b890
+	if (buf_fve)
31b890
+		*buf_fve = buf;
31b890
+done:
31b890
+	if (type)
31b890
+		*type = kind;
31b890
+	return 0;
31b890
+nothing:
31b890
+	return 1;
31b890
+}
31b890
+
31b890
+/*
31b890
+ * This is used by vFAT and NTFS prober to avoid collisions with bitlocker.
31b890
+ */
31b890
+int blkid_probe_is_bitlocker(blkid_probe pr)
31b890
+{
31b890
+	return get_bitlocker_headers(pr, NULL, NULL, NULL) == 0;
31b890
+}
31b890
+
31b890
+static int probe_bitlocker(blkid_probe pr,
31b890
+		const struct blkid_idmag *mag __attribute__((__unused__)))
31b890
+{
31b890
+	const unsigned char *buf_fve = NULL;
31b890
+	const unsigned char *buf_hdr = NULL;
31b890
+	int rc, kind;
31b890
+
31b890
+	rc = get_bitlocker_headers(pr, &kind, &buf_hdr, &buf_fve);
31b890
+	if (rc)
31b890
+		return rc;
31b890
+
31b890
+	if (kind == BDE_VERSION_WIN7) {
31b890
+		const struct bde_header_win7 *hdr = (const struct bde_header_win7 *) buf_hdr;
31b890
+
31b890
+		/* Unfortunately, it seems volume_serial is always zero */
31b890
+		blkid_probe_sprintf_uuid(pr,
31b890
+				(const unsigned char *) &hdr->volume_serial,
31b890
+				sizeof(hdr->volume_serial),
31b890
+				"%016d", le32_to_cpu(hdr->volume_serial));
31b890
+	}
31b890
+
31b890
+	if (buf_fve) {
31b890
+		const struct bde_fve_metadata *fve = (const struct bde_fve_metadata *) buf_fve;
31b890
+
31b890
+		blkid_probe_sprintf_version(pr, "%d", fve->version);
31b890
+	}
31b890
+	return 0;
31b890
+}
31b890
+
31b890
+/* See header details:
31b890
+ * https://github.com/libyal/libbde/blob/master/documentation/BitLocker%20Drive%20Encryption%20(BDE)%20format.asciidoc
31b890
+ */
31b890
+const struct blkid_idinfo bitlocker_idinfo =
31b890
+{
31b890
+	.name		= "BitLocker",
31b890
+	.usage		= BLKID_USAGE_CRYPTO,
31b890
+	.probefunc	= probe_bitlocker,
31b890
+	.magics		=
31b890
+	{
31b890
+		{ .magic = BDE_MAGIC_VISTA, .len = 11 },
31b890
+		{ .magic = BDE_MAGIC_WIN7,  .len = 11 },
31b890
+		{ .magic = BDE_MAGIC_TOGO,  .len = 11 },
31b890
+		{ NULL }
31b890
+	}
31b890
+};
31b890
diff --git a/libblkid/src/superblocks/superblocks.c b/libblkid/src/superblocks/superblocks.c
31b890
index 076541d1a..6dfd2be64 100644
31b890
--- a/libblkid/src/superblocks/superblocks.c
31b890
+++ b/libblkid/src/superblocks/superblocks.c
31b890
@@ -115,6 +115,7 @@ static const struct blkid_idinfo *idinfos[] =
31b890
 	&ubi_idinfo,
31b890
 	&vdo_idinfo,
31b890
 	&stratis_idinfo,
31b890
+	&bitlocker_idinfo,
31b890
 
31b890
 	/* Filesystems */
31b890
 	&vfat_idinfo,
31b890
diff --git a/libblkid/src/superblocks/superblocks.h b/libblkid/src/superblocks/superblocks.h
31b890
index 2723fb1d5..d677f85bc 100644
31b890
--- a/libblkid/src/superblocks/superblocks.h
31b890
+++ b/libblkid/src/superblocks/superblocks.h
31b890
@@ -81,6 +81,7 @@ extern const struct blkid_idinfo bcache_idinfo;
31b890
 extern const struct blkid_idinfo mpool_idinfo;
31b890
 extern const struct blkid_idinfo vdo_idinfo;
31b890
 extern const struct blkid_idinfo stratis_idinfo;
31b890
+extern const struct blkid_idinfo bitlocker_idinfo;
31b890
 
31b890
 /*
31b890
  * superblock functions
31b890
@@ -105,4 +106,6 @@ extern int blkid_probe_set_id_label(blkid_probe pr, const char *name,
31b890
 extern int blkid_probe_set_utf8_id_label(blkid_probe pr, const char *name,
31b890
 			     unsigned char *data, size_t len, int enc);
31b890
 
31b890
+extern int blkid_probe_is_bitlocker(blkid_probe pr);
31b890
+
31b890
 #endif /* _BLKID_SUPERBLOCKS_H */
31b890
diff --git a/libblkid/src/superblocks/vfat.c b/libblkid/src/superblocks/vfat.c
31b890
index 3aeba018a..29b3c501c 100644
31b890
--- a/libblkid/src/superblocks/vfat.c
31b890
+++ b/libblkid/src/superblocks/vfat.c
31b890
@@ -268,6 +268,9 @@ static int fat_valid_superblock(blkid_probe pr,
31b890
 		}
31b890
 	}
31b890
 
31b890
+	if (blkid_probe_is_bitlocker(pr))
31b890
+		return 0;
31b890
+
31b890
 	return 1;	/* valid */
31b890
 }
31b890
 
31b890
-- 
31b890
2.25.4
31b890