grub2/grub2-btrfs-01-add-ability-to-boot-from-subvolumes.patch

716 lines
19 KiB
Diff
Raw Normal View History

From: Jeff Mahoney <jeffm@suse.com>
Subject: grub2/btrfs: Add ability to boot from subvolumes
This patch adds the ability to specify a different root on a btrfs
filesystem too boot from other than the default one.
btrfs-list-snapshots <dev> will list the subvolumes available on the
filesystem.
set btrfs_subvol=<path> and set btrfs_subvolid=<subvolid> will specify
which subvolume to use and any pathnames provided with either of those
variables set will start using that root. If the subvolume or subvolume id
doesn't exist, then an error case will result.
It is possible to boot into a separate GRUB instance by exporting the
variable and loading the config file from the subvolume.
Signed-off-by: Jeff Mahoney <jeffm@suse.com>
Accepting request 823469 from home:michael-chang:branches:Base:System - Fix for CVE-2020-10713 (bsc#1168994) * 0001-yylex-Make-lexer-fatal-errors-actually-be-fatal.patch - Fix for CVE-2020-14308 CVE-2020-14309, CVE-2020-14310, CVE-2020-14311 (bsc#1173812) * 0002-safemath-Add-some-arithmetic-primitives-that-check-f.patch * 0003-calloc-Make-sure-we-always-have-an-overflow-checking.patch * 0004-calloc-Use-calloc-at-most-places.patch * 0005-malloc-Use-overflow-checking-primitives-where-we-do-.patch * 0006-iso9660-Don-t-leak-memory-on-realloc-failures.patch * 0007-font-Do-not-load-more-than-one-NAME-section.patch - Fix CVE-2020-15706 (bsc#1174463) * 0008-script-Remove-unused-fields-from-grub_script_functio.patch * 0009-script-Avoid-a-use-after-free-when-redefining-a-func.patch - Fix CVE-2020-15707 (bsc#1174570) * 0010-linux-Fix-integer-overflows-in-initrd-size-handling.patch - Use overflow checking primitives where the arithmetic expression for buffer allocations may include unvalidated data - Use grub_calloc for overflow check and return NULL when it would occur * 0001-add-support-for-UEFI-network-protocols.patch * 0003-bootp-New-net_bootp6-command.patch * grub2-btrfs-01-add-ability-to-boot-from-subvolumes.patch * grub2-btrfs-09-get-default-subvolume.patch * grub2-gfxmenu-support-scrolling-menu-entry-s-text.patch * grub2-grubenv-in-btrfs-header.patch OBS-URL: https://build.opensuse.org/request/show/823469 OBS-URL: https://build.opensuse.org/package/show/Base:System/grub2?expand=0&rev=358
2020-07-30 03:36:35 +02:00
V1:
* Use overflow checking primitives where the arithmetic expression for
buffer allocations may include unvalidated data
V2:
* Fix gcc-12 error: the comparison will always evaluate as 'true' for the
address of 'label' will never be NULL [-Werror=address]
---
grub-core/fs/btrfs.c | 561 +++++++++++++++++++++++++++++++++++++++++++++++++--
include/grub/btrfs.h | 1
2 files changed, 544 insertions(+), 18 deletions(-)
--- a/grub-core/fs/btrfs.c
+++ b/grub-core/fs/btrfs.c
Accepting request 904721 from home:michael-chang:grub:2.06 - Version bump to 2.06 * rediff - 0001-add-support-for-UEFI-network-protocols.patch - 0002-net-read-bracketed-ipv6-addrs-and-port-numbers.patch - 0003-Make-grub_error-more-verbose.patch - 0003-bootp-New-net_bootp6-command.patch - 0005-grub.texi-Add-net_bootp6-doument.patch - 0006-bootp-Add-processing-DHCPACK-packet-from-HTTP-Boot.patch - 0006-efi-Set-image-base-address-before-jumping-to-the-PE-.patch - 0008-efinet-Setting-DNS-server-from-UEFI-protocol.patch - 0046-squash-verifiers-Move-verifiers-API-to-kernel-image.patch - grub-install-force-journal-draining-to-ensure-data-i.patch - grub2-btrfs-01-add-ability-to-boot-from-subvolumes.patch - grub2-diskfilter-support-pv-without-metadatacopies.patch - grub2-efi-HP-workaround.patch - grub2-efi-xen-cfg-unquote.patch - grub2-efi-xen-chainload.patch - grub2-fix-menu-in-xen-host-server.patch - grub2-gfxmenu-support-scrolling-menu-entry-s-text.patch - grub2-install-remove-useless-check-PReP-partition-is-empty.patch - grub2-lvm-allocate-metadata-buffer-from-raw-contents.patch - grub2-mkconfig-default-entry-correction.patch - grub2-pass-corret-root-for-nfsroot.patch - grub2-s390x-03-output-7-bit-ascii.patch - grub2-s390x-04-grub2-install.patch - grub2-secureboot-install-signed-grub.patch - grub2-setup-try-fs-embed-if-mbr-gap-too-small.patch - use-grub2-as-a-package-name.patch * update by patch squashed: - 0001-Add-support-for-Linux-EFI-stub-loading-on-aarch64.patch OBS-URL: https://build.opensuse.org/request/show/904721 OBS-URL: https://build.opensuse.org/package/show/Base:System/grub2?expand=0&rev=386
2021-07-08 11:03:14 +02:00
@@ -41,6 +41,9 @@
#include <grub/crypto.h>
#include <grub/diskfilter.h>
Accepting request 904721 from home:michael-chang:grub:2.06 - Version bump to 2.06 * rediff - 0001-add-support-for-UEFI-network-protocols.patch - 0002-net-read-bracketed-ipv6-addrs-and-port-numbers.patch - 0003-Make-grub_error-more-verbose.patch - 0003-bootp-New-net_bootp6-command.patch - 0005-grub.texi-Add-net_bootp6-doument.patch - 0006-bootp-Add-processing-DHCPACK-packet-from-HTTP-Boot.patch - 0006-efi-Set-image-base-address-before-jumping-to-the-PE-.patch - 0008-efinet-Setting-DNS-server-from-UEFI-protocol.patch - 0046-squash-verifiers-Move-verifiers-API-to-kernel-image.patch - grub-install-force-journal-draining-to-ensure-data-i.patch - grub2-btrfs-01-add-ability-to-boot-from-subvolumes.patch - grub2-diskfilter-support-pv-without-metadatacopies.patch - grub2-efi-HP-workaround.patch - grub2-efi-xen-cfg-unquote.patch - grub2-efi-xen-chainload.patch - grub2-fix-menu-in-xen-host-server.patch - grub2-gfxmenu-support-scrolling-menu-entry-s-text.patch - grub2-install-remove-useless-check-PReP-partition-is-empty.patch - grub2-lvm-allocate-metadata-buffer-from-raw-contents.patch - grub2-mkconfig-default-entry-correction.patch - grub2-pass-corret-root-for-nfsroot.patch - grub2-s390x-03-output-7-bit-ascii.patch - grub2-s390x-04-grub2-install.patch - grub2-secureboot-install-signed-grub.patch - grub2-setup-try-fs-embed-if-mbr-gap-too-small.patch - use-grub2-as-a-package-name.patch * update by patch squashed: - 0001-Add-support-for-Linux-EFI-stub-loading-on-aarch64.patch OBS-URL: https://build.opensuse.org/request/show/904721 OBS-URL: https://build.opensuse.org/package/show/Base:System/grub2?expand=0&rev=386
2021-07-08 11:03:14 +02:00
#include <grub/safemath.h>
+#include <grub/command.h>
+#include <grub/env.h>
+#include <grub/extcmd.h>
GRUB_MOD_LICENSE ("GPLv3+");
@@ -79,9 +82,11 @@
grub_uint64_t generation;
grub_uint64_t root_tree;
grub_uint64_t chunk_tree;
- grub_uint8_t dummy2[0x20];
+ grub_uint8_t dummy2[0x18];
+ grub_uint64_t bytes_used;
grub_uint64_t root_dir_objectid;
- grub_uint8_t dummy3[0x41];
+ grub_uint64_t num_devices;
+ grub_uint8_t dummy3[0x39];
struct grub_btrfs_device this_device;
char label[0x100];
grub_uint8_t dummy4[0x100];
@@ -121,6 +126,7 @@
grub_uint64_t exttree;
grub_size_t extsize;
struct grub_btrfs_extent_data *extent;
+ grub_uint64_t fs_tree;
};
struct grub_btrfs_chunk_item
@@ -191,6 +197,14 @@
} *data;
};
+struct grub_btrfs_root_ref
+{
+ grub_uint64_t dirid;
+ grub_uint64_t sequence;
+ grub_uint16_t name_len;
+ const char name[0];
+} __attribute__ ((packed));
+
struct grub_btrfs_time
{
grub_int64_t sec;
@@ -236,6 +250,14 @@
#define GRUB_BTRFS_OBJECT_ID_CHUNK 0x100
+#define GRUB_BTRFS_ROOT_TREE_OBJECTID 1ULL
+#define GRUB_BTRFS_FS_TREE_OBJECTID 5ULL
+#define GRUB_BTRFS_ROOT_REF_KEY 156
+#define GRUB_BTRFS_ROOT_ITEM_KEY 132
+
+static grub_uint64_t btrfs_default_subvolid = 0;
+static char *btrfs_default_subvol = NULL;
+
static grub_disk_addr_t superblock_sectors[] = { 64 * 2, 64 * 1024 * 2,
256 * 1048576 * 2, 1048576ULL * 1048576ULL * 2
};
@@ -1173,6 +1195,62 @@
return GRUB_ERR_NONE;
}
+static grub_err_t
+get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
+ grub_uint64_t objectid, grub_uint64_t offset,
+ grub_uint64_t *fs_root);
+
+static grub_err_t
+lookup_root_by_id(struct grub_btrfs_data *data, grub_uint64_t id)
+{
+ grub_err_t err;
+ grub_uint64_t tree;
+
+ err = get_fs_root(data, data->sblock.root_tree, id, -1, &tree);
+ if (!err)
+ data->fs_tree = tree;
+ return err;
+}
+
+static grub_err_t
+find_path (struct grub_btrfs_data *data,
+ const char *path, struct grub_btrfs_key *key,
+ grub_uint64_t *tree, grub_uint8_t *type);
+
+static grub_err_t
+lookup_root_by_name(struct grub_btrfs_data *data, const char *path)
+{
+ grub_err_t err;
+ grub_uint64_t tree = 0;
+ grub_uint8_t type;
+ struct grub_btrfs_key key;
+
+ err = find_path (data, path, &key, &tree, &type);
+ if (err)
+ return grub_error(GRUB_ERR_FILE_NOT_FOUND, "couldn't locate %s\n", path);
+
+ if (key.object_id != grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK) || tree == 0)
+ return grub_error(GRUB_ERR_BAD_FILE_TYPE, "%s: not a subvolume\n", path);
+
+ data->fs_tree = tree;
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+btrfs_handle_subvol(struct grub_btrfs_data *data __attribute__ ((unused)))
+{
+ if (btrfs_default_subvol)
+ return lookup_root_by_name(data, btrfs_default_subvol);
+
+ if (btrfs_default_subvolid)
+ return lookup_root_by_id(data, btrfs_default_subvolid);
+
+ data->fs_tree = 0;
+
+ return GRUB_ERR_NONE;
+}
+
+
static struct grub_btrfs_data *
grub_btrfs_mount (grub_device_t dev)
{
@@ -1208,6 +1286,13 @@
data->devices_attached[0].dev = dev;
data->devices_attached[0].id = data->sblock.this_device.device_id;
+ err = btrfs_handle_subvol (data);
+ if (err)
+ {
+ grub_free (data);
+ return NULL;
+ }
+
return data;
}
@@ -1674,6 +1759,98 @@
}
static grub_err_t
+find_pathname(struct grub_btrfs_data *data, grub_uint64_t objectid,
+ grub_uint64_t fs_root, const char *name, char **pathname)
+{
+ grub_err_t err;
+ struct grub_btrfs_key key = {
+ .object_id = objectid,
+ .type = GRUB_BTRFS_ITEM_TYPE_INODE_REF,
+ .offset = 0,
+ };
+ struct grub_btrfs_key key_out;
+ struct grub_btrfs_leaf_descriptor desc;
+ char *p = grub_strdup (name);
+ grub_disk_addr_t elemaddr;
+ grub_size_t elemsize;
+ grub_size_t alloc = grub_strlen(name) + 1;
+
+ err = lower_bound(data, &key, &key_out, fs_root,
+ &elemaddr, &elemsize, &desc, 0);
+ if (err)
+ return grub_error(err, "lower_bound caught %d\n", err);
+
+ if (key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_REF)
+ next(data, &desc, &elemaddr, &elemsize, &key_out);
+
+ if (key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_REF)
+ {
+ return grub_error(GRUB_ERR_FILE_NOT_FOUND,
+ "Can't find inode ref for {%"PRIuGRUB_UINT64_T
+ ", %u, %"PRIuGRUB_UINT64_T"} %"PRIuGRUB_UINT64_T
+ "/%"PRIuGRUB_SIZE"\n",
+ key_out.object_id, key_out.type,
+ key_out.offset, elemaddr, elemsize);
+ }
+
+
+ while (key_out.type == GRUB_BTRFS_ITEM_TYPE_INODE_REF &&
+ key_out.object_id != key_out.offset) {
+ struct grub_btrfs_inode_ref *inode_ref;
+ char *new;
Accepting request 823469 from home:michael-chang:branches:Base:System - Fix for CVE-2020-10713 (bsc#1168994) * 0001-yylex-Make-lexer-fatal-errors-actually-be-fatal.patch - Fix for CVE-2020-14308 CVE-2020-14309, CVE-2020-14310, CVE-2020-14311 (bsc#1173812) * 0002-safemath-Add-some-arithmetic-primitives-that-check-f.patch * 0003-calloc-Make-sure-we-always-have-an-overflow-checking.patch * 0004-calloc-Use-calloc-at-most-places.patch * 0005-malloc-Use-overflow-checking-primitives-where-we-do-.patch * 0006-iso9660-Don-t-leak-memory-on-realloc-failures.patch * 0007-font-Do-not-load-more-than-one-NAME-section.patch - Fix CVE-2020-15706 (bsc#1174463) * 0008-script-Remove-unused-fields-from-grub_script_functio.patch * 0009-script-Avoid-a-use-after-free-when-redefining-a-func.patch - Fix CVE-2020-15707 (bsc#1174570) * 0010-linux-Fix-integer-overflows-in-initrd-size-handling.patch - Use overflow checking primitives where the arithmetic expression for buffer allocations may include unvalidated data - Use grub_calloc for overflow check and return NULL when it would occur * 0001-add-support-for-UEFI-network-protocols.patch * 0003-bootp-New-net_bootp6-command.patch * grub2-btrfs-01-add-ability-to-boot-from-subvolumes.patch * grub2-btrfs-09-get-default-subvolume.patch * grub2-gfxmenu-support-scrolling-menu-entry-s-text.patch * grub2-grubenv-in-btrfs-header.patch OBS-URL: https://build.opensuse.org/request/show/823469 OBS-URL: https://build.opensuse.org/package/show/Base:System/grub2?expand=0&rev=358
2020-07-30 03:36:35 +02:00
+ grub_size_t sz;
+
Accepting request 823469 from home:michael-chang:branches:Base:System - Fix for CVE-2020-10713 (bsc#1168994) * 0001-yylex-Make-lexer-fatal-errors-actually-be-fatal.patch - Fix for CVE-2020-14308 CVE-2020-14309, CVE-2020-14310, CVE-2020-14311 (bsc#1173812) * 0002-safemath-Add-some-arithmetic-primitives-that-check-f.patch * 0003-calloc-Make-sure-we-always-have-an-overflow-checking.patch * 0004-calloc-Use-calloc-at-most-places.patch * 0005-malloc-Use-overflow-checking-primitives-where-we-do-.patch * 0006-iso9660-Don-t-leak-memory-on-realloc-failures.patch * 0007-font-Do-not-load-more-than-one-NAME-section.patch - Fix CVE-2020-15706 (bsc#1174463) * 0008-script-Remove-unused-fields-from-grub_script_functio.patch * 0009-script-Avoid-a-use-after-free-when-redefining-a-func.patch - Fix CVE-2020-15707 (bsc#1174570) * 0010-linux-Fix-integer-overflows-in-initrd-size-handling.patch - Use overflow checking primitives where the arithmetic expression for buffer allocations may include unvalidated data - Use grub_calloc for overflow check and return NULL when it would occur * 0001-add-support-for-UEFI-network-protocols.patch * 0003-bootp-New-net_bootp6-command.patch * grub2-btrfs-01-add-ability-to-boot-from-subvolumes.patch * grub2-btrfs-09-get-default-subvolume.patch * grub2-gfxmenu-support-scrolling-menu-entry-s-text.patch * grub2-grubenv-in-btrfs-header.patch OBS-URL: https://build.opensuse.org/request/show/823469 OBS-URL: https://build.opensuse.org/package/show/Base:System/grub2?expand=0&rev=358
2020-07-30 03:36:35 +02:00
+ if (grub_add (elemsize, 1, &sz))
+ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
+
+ inode_ref = grub_malloc(sz);
+ if (!inode_ref)
+ return grub_error(GRUB_ERR_OUT_OF_MEMORY,
+ "couldn't allocate memory for inode_ref (%"PRIuGRUB_SIZE")\n", elemsize);
+
+ err = grub_btrfs_read_logical(data, elemaddr, inode_ref, elemsize, 0);
+ if (err)
+ return grub_error(err, "read_logical caught %d\n", err);
+
Accepting request 823469 from home:michael-chang:branches:Base:System - Fix for CVE-2020-10713 (bsc#1168994) * 0001-yylex-Make-lexer-fatal-errors-actually-be-fatal.patch - Fix for CVE-2020-14308 CVE-2020-14309, CVE-2020-14310, CVE-2020-14311 (bsc#1173812) * 0002-safemath-Add-some-arithmetic-primitives-that-check-f.patch * 0003-calloc-Make-sure-we-always-have-an-overflow-checking.patch * 0004-calloc-Use-calloc-at-most-places.patch * 0005-malloc-Use-overflow-checking-primitives-where-we-do-.patch * 0006-iso9660-Don-t-leak-memory-on-realloc-failures.patch * 0007-font-Do-not-load-more-than-one-NAME-section.patch - Fix CVE-2020-15706 (bsc#1174463) * 0008-script-Remove-unused-fields-from-grub_script_functio.patch * 0009-script-Avoid-a-use-after-free-when-redefining-a-func.patch - Fix CVE-2020-15707 (bsc#1174570) * 0010-linux-Fix-integer-overflows-in-initrd-size-handling.patch - Use overflow checking primitives where the arithmetic expression for buffer allocations may include unvalidated data - Use grub_calloc for overflow check and return NULL when it would occur * 0001-add-support-for-UEFI-network-protocols.patch * 0003-bootp-New-net_bootp6-command.patch * grub2-btrfs-01-add-ability-to-boot-from-subvolumes.patch * grub2-btrfs-09-get-default-subvolume.patch * grub2-gfxmenu-support-scrolling-menu-entry-s-text.patch * grub2-grubenv-in-btrfs-header.patch OBS-URL: https://build.opensuse.org/request/show/823469 OBS-URL: https://build.opensuse.org/package/show/Base:System/grub2?expand=0&rev=358
2020-07-30 03:36:35 +02:00
+ if (grub_add (grub_le_to_cpu16 (inode_ref->n), 2, &sz) ||
+ grub_add (alloc, sz, &alloc))
+ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
+
+ new = grub_malloc(alloc);
+ if (!new)
+ return grub_error(GRUB_ERR_OUT_OF_MEMORY,
+ "couldn't allocate memory for name (%"PRIuGRUB_SIZE")\n", alloc);
+
+ grub_memcpy(new, inode_ref->name, grub_le_to_cpu16 (inode_ref->n));
+ if (p)
+ {
+ new[grub_le_to_cpu16 (inode_ref->n)] = '/';
+ grub_strcpy (new + grub_le_to_cpu16 (inode_ref->n) + 1, p);
+ grub_free(p);
+ }
+ else
+ new[grub_le_to_cpu16 (inode_ref->n)] = 0;
+ grub_free(inode_ref);
+
+ p = new;
+
+ key.object_id = key_out.offset;
+
+ err = lower_bound(data, &key, &key_out, fs_root, &elemaddr,
+ &elemsize, &desc, 0);
+ if (err)
+ return grub_error(err, "lower_bound caught %d\n", err);
+
+ if (key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_REF)
+ next(data, &desc, &elemaddr, &elemsize, &key_out);
+
+ }
+
+ *pathname = p;
+ return 0;
+}
+
+static grub_err_t
find_path (struct grub_btrfs_data *data,
const char *path, struct grub_btrfs_key *key,
grub_uint64_t *tree, grub_uint8_t *type)
@@ -1691,14 +1868,26 @@
char *origpath = NULL;
unsigned symlinks_max = 32;
- err = get_root (data, key, tree, type);
- if (err)
- return err;
-
origpath = grub_strdup (path);
if (!origpath)
return grub_errno;
+ if (data->fs_tree)
+ {
+ *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+ *tree = data->fs_tree;
+ /* This is a tree root, so everything starts at objectid 256 */
+ key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+ key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+ key->offset = 0;
+ }
+ else
+ {
+ err = get_root (data, key, tree, type);
+ if (err)
+ return err;
+ }
+
while (1)
{
while (path[0] == '/')
@@ -1871,9 +2060,21 @@
path = path_alloc = tmp;
if (path[0] == '/')
{
- err = get_root (data, key, tree, type);
- if (err)
- return err;
+ if (data->fs_tree)
+ {
+ *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+ *tree = data->fs_tree;
+ /* This is a tree root, so everything starts at objectid 256 */
+ key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+ key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+ key->offset = 0;
+ }
+ else
+ {
+ err = get_root (data, key, tree, type);
+ if (err)
+ return err;
+ }
}
continue;
}
@@ -2114,6 +2315,20 @@
data->tree, file->offset, buf, len);
}
+static char *
+btrfs_unparse_uuid(struct grub_btrfs_data *data)
+{
+ return grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
+ grub_be_to_cpu16 (data->sblock.uuid[0]),
+ grub_be_to_cpu16 (data->sblock.uuid[1]),
+ grub_be_to_cpu16 (data->sblock.uuid[2]),
+ grub_be_to_cpu16 (data->sblock.uuid[3]),
+ grub_be_to_cpu16 (data->sblock.uuid[4]),
+ grub_be_to_cpu16 (data->sblock.uuid[5]),
+ grub_be_to_cpu16 (data->sblock.uuid[6]),
+ grub_be_to_cpu16 (data->sblock.uuid[7]));
+}
+
static grub_err_t
grub_btrfs_uuid (grub_device_t device, char **uuid)
{
@@ -2125,15 +2340,7 @@
if (!data)
return grub_errno;
- *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
- grub_be_to_cpu16 (data->sblock.uuid[0]),
- grub_be_to_cpu16 (data->sblock.uuid[1]),
- grub_be_to_cpu16 (data->sblock.uuid[2]),
- grub_be_to_cpu16 (data->sblock.uuid[3]),
- grub_be_to_cpu16 (data->sblock.uuid[4]),
- grub_be_to_cpu16 (data->sblock.uuid[5]),
- grub_be_to_cpu16 (data->sblock.uuid[6]),
- grub_be_to_cpu16 (data->sblock.uuid[7]));
+ *uuid = btrfs_unparse_uuid(data);
grub_btrfs_unmount (data);
@@ -2190,6 +2397,248 @@
}
#endif
+static grub_err_t
+grub_cmd_btrfs_info (grub_command_t cmd __attribute__ ((unused)), int argc,
+ char **argv)
+{
+ grub_device_t dev;
+ char *devname;
+ struct grub_btrfs_data *data;
+ char *uuid;
+
+ if (argc < 1)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
+
+ devname = grub_file_get_device_name(argv[0]);
+
+ if (!devname)
+ return grub_errno;
+
+ dev = grub_device_open (devname);
+ grub_free (devname);
+ if (!dev)
+ return grub_errno;
+
+ data = grub_btrfs_mount (dev);
+ if (!data)
+ {
+ grub_device_close(dev);
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "failed to open fs");
+ }
+
+ if (*data->sblock.label != '\0')
+ grub_printf("Label: '%s' ", data->sblock.label);
+ else
+ grub_printf("Label: none ");
+
+ uuid = btrfs_unparse_uuid(data);
+
+ grub_printf(" uuid: %s\n\tTotal devices %" PRIuGRUB_UINT64_T
+ " FS bytes used %" PRIuGRUB_UINT64_T "\n",
+ uuid, grub_cpu_to_le64(data->sblock.num_devices),
+ grub_cpu_to_le64(data->sblock.bytes_used));
+
+ grub_btrfs_unmount (data);
+
+ return 0;
+}
+
+static grub_err_t
+get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
+ grub_uint64_t objectid, grub_uint64_t offset,
+ grub_uint64_t *fs_root)
+{
+ grub_err_t err;
+ struct grub_btrfs_key key_in = {
+ .object_id = objectid,
+ .type = GRUB_BTRFS_ROOT_ITEM_KEY,
+ .offset = offset,
+ }, key_out;
+ struct grub_btrfs_leaf_descriptor desc;
+ grub_disk_addr_t elemaddr;
+ grub_size_t elemsize;
+ struct grub_btrfs_root_item ri;
+
+ err = lower_bound(data, &key_in, &key_out, tree,
+ &elemaddr, &elemsize, &desc, 0);
+
+ if (err)
+ return err;
+
+ if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM || elemaddr == 0)
+ return grub_error(GRUB_ERR_FILE_NOT_FOUND,
+ N_("can't find fs root for subvol %"PRIuGRUB_UINT64_T"\n"),
+ key_in.object_id);
+
+ err = grub_btrfs_read_logical (data, elemaddr, &ri, sizeof (ri), 0);
+ if (err)
+ return err;
+
+ *fs_root = ri.tree;
+
+ return GRUB_ERR_NONE;
+}
+
+static const struct grub_arg_option options[] = {
+ {"output", 'o', 0, N_("Output to a variable instead of the console."),
+ N_("VARNAME"), ARG_TYPE_STRING},
+ {"path-only", 'p', 0, N_("Show only the path of the subvolume."), 0, 0},
+ {"id-only", 'i', 0, N_("Show only the id of the subvolume."), 0, 0},
+ {0, 0, 0, 0, 0, 0}
+};
+
+static grub_err_t
+grub_cmd_btrfs_list_subvols (struct grub_extcmd_context *ctxt,
+ int argc, char **argv)
+{
+ struct grub_btrfs_data *data;
+ grub_device_t dev;
+ char *devname;
+ grub_uint64_t tree;
+ struct grub_btrfs_key key_in = {
+ .object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_FS_TREE_OBJECTID),
+ .type = GRUB_BTRFS_ROOT_REF_KEY,
+ .offset = 0,
+ }, key_out;
+ struct grub_btrfs_leaf_descriptor desc;
+ grub_disk_addr_t elemaddr;
+ grub_uint64_t fs_root = 0;
+ grub_size_t elemsize;
+ grub_size_t allocated = 0;
+ int r = 0;
+ grub_err_t err;
+ char *buf = NULL;
+ int print = 1;
+ int path_only = ctxt->state[1].set;
+ int num_only = ctxt->state[2].set;
+ char *varname = NULL;
+ char *output = NULL;
+
+ if (ctxt->state[0].set) {
+ varname = ctxt->state[0].arg;
+ print = 0;
+ }
+
+ if (argc < 1)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
+
+ devname = grub_file_get_device_name(argv[0]);
+ if (!devname)
+ return grub_errno;
+
+ dev = grub_device_open (devname);
+ grub_free (devname);
+ if (!dev)
+ return grub_errno;
+
+ data = grub_btrfs_mount(dev);
+ if (!data)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "could not open device");
+
+ tree = data->sblock.root_tree;
+ err = get_fs_root(data, tree, grub_cpu_to_le64_compile_time (GRUB_BTRFS_FS_TREE_OBJECTID),
+ 0, &fs_root);
+ if (err)
+ goto out;
+
+ err = lower_bound(data, &key_in, &key_out, tree,
+ &elemaddr, &elemsize, &desc, 0);
+
+ if (err)
+ {
+ grub_btrfs_unmount(data);
+ return err;
+ }
+
+ if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_REF || elemaddr == 0)
+ {
+ r = next(data, &desc, &elemaddr, &elemsize, &key_out);
+ }
+
+ if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_REF) {
+ err = GRUB_ERR_FILE_NOT_FOUND;
+ grub_error(GRUB_ERR_FILE_NOT_FOUND, N_("can't find root refs"));
+ goto out;
+ }
+
+ do
+ {
+ struct grub_btrfs_root_ref *ref;
+ char *p = NULL;
+
+ if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_REF)
+ {
+ r = 0;
+ break;
+ }
+
+ if (elemsize > allocated)
+ {
Accepting request 823469 from home:michael-chang:branches:Base:System - Fix for CVE-2020-10713 (bsc#1168994) * 0001-yylex-Make-lexer-fatal-errors-actually-be-fatal.patch - Fix for CVE-2020-14308 CVE-2020-14309, CVE-2020-14310, CVE-2020-14311 (bsc#1173812) * 0002-safemath-Add-some-arithmetic-primitives-that-check-f.patch * 0003-calloc-Make-sure-we-always-have-an-overflow-checking.patch * 0004-calloc-Use-calloc-at-most-places.patch * 0005-malloc-Use-overflow-checking-primitives-where-we-do-.patch * 0006-iso9660-Don-t-leak-memory-on-realloc-failures.patch * 0007-font-Do-not-load-more-than-one-NAME-section.patch - Fix CVE-2020-15706 (bsc#1174463) * 0008-script-Remove-unused-fields-from-grub_script_functio.patch * 0009-script-Avoid-a-use-after-free-when-redefining-a-func.patch - Fix CVE-2020-15707 (bsc#1174570) * 0010-linux-Fix-integer-overflows-in-initrd-size-handling.patch - Use overflow checking primitives where the arithmetic expression for buffer allocations may include unvalidated data - Use grub_calloc for overflow check and return NULL when it would occur * 0001-add-support-for-UEFI-network-protocols.patch * 0003-bootp-New-net_bootp6-command.patch * grub2-btrfs-01-add-ability-to-boot-from-subvolumes.patch * grub2-btrfs-09-get-default-subvolume.patch * grub2-gfxmenu-support-scrolling-menu-entry-s-text.patch * grub2-grubenv-in-btrfs-header.patch OBS-URL: https://build.opensuse.org/request/show/823469 OBS-URL: https://build.opensuse.org/package/show/Base:System/grub2?expand=0&rev=358
2020-07-30 03:36:35 +02:00
+ grub_size_t sz;
+
+ grub_free(buf);
Accepting request 823469 from home:michael-chang:branches:Base:System - Fix for CVE-2020-10713 (bsc#1168994) * 0001-yylex-Make-lexer-fatal-errors-actually-be-fatal.patch - Fix for CVE-2020-14308 CVE-2020-14309, CVE-2020-14310, CVE-2020-14311 (bsc#1173812) * 0002-safemath-Add-some-arithmetic-primitives-that-check-f.patch * 0003-calloc-Make-sure-we-always-have-an-overflow-checking.patch * 0004-calloc-Use-calloc-at-most-places.patch * 0005-malloc-Use-overflow-checking-primitives-where-we-do-.patch * 0006-iso9660-Don-t-leak-memory-on-realloc-failures.patch * 0007-font-Do-not-load-more-than-one-NAME-section.patch - Fix CVE-2020-15706 (bsc#1174463) * 0008-script-Remove-unused-fields-from-grub_script_functio.patch * 0009-script-Avoid-a-use-after-free-when-redefining-a-func.patch - Fix CVE-2020-15707 (bsc#1174570) * 0010-linux-Fix-integer-overflows-in-initrd-size-handling.patch - Use overflow checking primitives where the arithmetic expression for buffer allocations may include unvalidated data - Use grub_calloc for overflow check and return NULL when it would occur * 0001-add-support-for-UEFI-network-protocols.patch * 0003-bootp-New-net_bootp6-command.patch * grub2-btrfs-01-add-ability-to-boot-from-subvolumes.patch * grub2-btrfs-09-get-default-subvolume.patch * grub2-gfxmenu-support-scrolling-menu-entry-s-text.patch * grub2-grubenv-in-btrfs-header.patch OBS-URL: https://build.opensuse.org/request/show/823469 OBS-URL: https://build.opensuse.org/package/show/Base:System/grub2?expand=0&rev=358
2020-07-30 03:36:35 +02:00
+
+ if (grub_mul (elemsize, 2, &allocated) ||
+ grub_add (allocated, 1, &sz))
+ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
+
+ buf = grub_malloc(sz);
+ if (!buf)
+ {
+ r = -grub_errno;
+ break;
+ }
+ }
+ ref = (struct grub_btrfs_root_ref *)buf;
+
+ err = grub_btrfs_read_logical(data, elemaddr, buf, elemsize, 0);
+ if (err)
+ {
+ r = -err;
+ break;
+ }
+ buf[elemsize] = 0;
+
+ find_pathname(data, ref->dirid, fs_root, ref->name, &p);
+
+ if (print)
+ {
+ if (num_only)
+ grub_printf("ID %"PRIuGRUB_UINT64_T"\n", key_out.offset);
+ else if (path_only)
+ grub_printf("%s\n", p);
+ else
+ grub_printf("ID %"PRIuGRUB_UINT64_T" path %s\n", key_out.offset, p);
+ } else {
+ char *old = output;
+ if (num_only)
+ output = grub_xasprintf("%s%"PRIuGRUB_UINT64_T"\n",
+ old ?: "", key_out.offset);
+ else if (path_only)
+ output = grub_xasprintf("%s%s\n", old ?: "", p);
+ else
+ output = grub_xasprintf("%sID %"PRIuGRUB_UINT64_T" path %s\n",
+ old ?: "", key_out.offset, p);
+
+ if (old)
+ grub_free(old);
+ }
+
+ r = next(data, &desc, &elemaddr, &elemsize, &key_out);
+ } while(r > 0);
+
+ if (output)
+ grub_env_set(varname, output);
+
+out:
+ free_iterator(&desc);
+ grub_btrfs_unmount(data);
+
+ grub_device_close (dev);
+
+ return 0;
+}
+
static struct grub_fs grub_btrfs_fs = {
.name = "btrfs",
.fs_dir = grub_btrfs_dir,
@@ -2205,12 +2654,88 @@
#endif
};
+static grub_command_t cmd_info;
+static grub_extcmd_t cmd_list_subvols;
+
+static char *
+subvolid_set_env (struct grub_env_var *var __attribute__ ((unused)),
+ const char *val)
+{
+ unsigned long long result = 0;
+
+ grub_errno = GRUB_ERR_NONE;
+ if (*val)
+ {
+ result = grub_strtoull(val, NULL, 10);
+ if (grub_errno)
+ return NULL;
+ }
+
+ grub_free (btrfs_default_subvol);
+ btrfs_default_subvol = NULL;
+ btrfs_default_subvolid = result;
+ return grub_strdup(val);
+}
+
+static const char *
+subvolid_get_env (struct grub_env_var *var __attribute__ ((unused)),
+ const char *val __attribute__ ((unused)))
+{
+ if (btrfs_default_subvol)
+ return grub_xasprintf("subvol:%s", btrfs_default_subvol);
+ else if (btrfs_default_subvolid)
+ return grub_xasprintf("%"PRIuGRUB_UINT64_T, btrfs_default_subvolid);
+ else
+ return "";
+}
+
+static char *
+subvol_set_env (struct grub_env_var *var __attribute__ ((unused)),
+ const char *val)
+{
+ grub_free (btrfs_default_subvol);
+ btrfs_default_subvol = grub_strdup (val);
+ btrfs_default_subvolid = 0;
+ return grub_strdup(val);
+}
+
+static const char *
+subvol_get_env (struct grub_env_var *var __attribute__ ((unused)),
+ const char *val __attribute__ ((unused)))
+{
+ if (btrfs_default_subvol)
+ return btrfs_default_subvol;
+ else if (btrfs_default_subvolid)
+ return grub_xasprintf("subvolid:%" PRIuGRUB_UINT64_T,
+ btrfs_default_subvolid);
+ else
+ return "";
+}
+
GRUB_MOD_INIT (btrfs)
{
grub_fs_register (&grub_btrfs_fs);
+ cmd_info = grub_register_command("btrfs-info", grub_cmd_btrfs_info,
+ "DEVICE",
+ "Print BtrFS info about DEVICE.");
+ cmd_list_subvols = grub_register_extcmd("btrfs-list-subvols",
+ grub_cmd_btrfs_list_subvols, 0,
+ "[-p|-n] [-o var] DEVICE",
+ "Print list of BtrFS subvolumes on "
+ "DEVICE.", options);
+ grub_register_variable_hook ("btrfs_subvol", subvol_get_env,
+ subvol_set_env);
+ grub_register_variable_hook ("btrfs_subvolid", subvolid_get_env,
+ subvolid_set_env);
}
GRUB_MOD_FINI (btrfs)
{
+ grub_register_variable_hook ("btrfs_subvol", NULL, NULL);
+ grub_register_variable_hook ("btrfs_subvolid", NULL, NULL);
+ grub_unregister_command (cmd_info);
+ grub_unregister_extcmd (cmd_list_subvols);
grub_fs_unregister (&grub_btrfs_fs);
}
+
+// vim: si et sw=2:
--- a/include/grub/btrfs.h
+++ b/include/grub/btrfs.h
@@ -29,6 +29,7 @@
GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM = 0x84,
GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF = 0x90,
GRUB_BTRFS_ITEM_TYPE_DEVICE = 0xd8,
+ GRUB_BTRFS_ITEM_TYPE_ROOT_REF = 0x9c,
GRUB_BTRFS_ITEM_TYPE_CHUNK = 0xe4
};