dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

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

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