Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp5819689imm; Wed, 12 Sep 2018 11:36:18 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZvkVvxnhB+GimxyXz1Kf2Eh6NyGv1UwewHEHf/NhVZwVbGyGV/4ioIB3OK5z6rzuf7+2Gd X-Received: by 2002:a17:902:9a06:: with SMTP id v6-v6mr3748367plp.316.1536777378852; Wed, 12 Sep 2018 11:36:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536777378; cv=none; d=google.com; s=arc-20160816; b=FjyWdSCcJzNMl7h7cSL6NDoJJA6SnKZCaQVWLb9lXooqdVBRucrLOI7+80OZrbv+SY 8dXM+lL0Bl8jtbjQZrDZVvm+ySiOxHZNkEpI0y1Xwqr4utuTrRK0GsDwhjnH2nQMUBTz zBr/g26xl+lmKx+RKtslxfFy1X/E4hA1iza8W0qFjiCts3RE/XO0MU8c50f0dVi7u+jZ Ag80PkFsB2NVFAmibDdOiVhmGY24ZXTPGAT2eO8PhVkoOzFIURRulJOnVvCsKJu+A0Ag xH+fgAYyDstSKJwLT2f1qPsWGhu4EdibdbxGD57iLn1y380mjIVVX9OZbTmFeFPGjwCy d04g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:mail-followup-to:message-id:subject:to:from:date :dkim-signature; bh=ecIsW9IyKqy9C+GprLkMa0/3hlbYSHkBLQqcH/0CXKg=; b=mxetAXDs5Vn7cWQjg26jEuilg6+b+T0P6UdadlhosG36Dz8/tTxL8owaDEX/6Lrhct YKbcaekdCEzq6kjkI7qt1stj88hsYFtpLcMFphj5GmnCrD1d1Wgm3F+EEL1OMW9zHj9S 4Fh+SSfT1yFEO2SdwUiBbnxhbjyIkQHOBUPP4W9jcdLsJMkIdR6ajBZu4lgXMkW+/x3Q UBAnkA12wW31pqz+FD17PWPu4/+unl1WDZ5resVuxA23C9doLnoQrBgJO4NVbtrQvH9D k4OJEy3f3DzmlfgKIXfyt2djxcWcv9iLYe81dOynVa0uZcOP2proo5WAlMVWXnAswrmp XNUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Mi9+z1+v; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y22-v6si1690095pgj.436.2018.09.12.11.36.04; Wed, 12 Sep 2018 11:36:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Mi9+z1+v; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728139AbeILXlM (ORCPT + 99 others); Wed, 12 Sep 2018 19:41:12 -0400 Received: from mail-lf1-f68.google.com ([209.85.167.68]:42221 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727651AbeILXlM (ORCPT ); Wed, 12 Sep 2018 19:41:12 -0400 Received: by mail-lf1-f68.google.com with SMTP id z11-v6so2568712lff.9; Wed, 12 Sep 2018 11:35:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mail-followup-to:mime-version :content-disposition:user-agent; bh=ecIsW9IyKqy9C+GprLkMa0/3hlbYSHkBLQqcH/0CXKg=; b=Mi9+z1+vebXim6B8HqKVMhkYp/yJEyF1BDHEzr0hvgbQWqAimCSFIfkrJdfG1bbt5y wS/UneehNE48qrc/Q40/fuXyDKuz6OC0dlL7/83y4sX3QFo0CU52on+QjP5MROMwtG0u KjxQ2hwIJZpAHv4IZefy/ZrNjwSmkXZxUPZkJ6mzQpXqpiYJxE6UaiB3wyMng1RUeh7q 1I/V3CfKlzbHf2T8w+2jkMP8vQDuwrfczPA5vRXzzD8X9Y3SKnMYsgRWp/Zc5+bDfgcQ DHdZAqiEpzHqNJmQ5MrKojyKruxbMD52toTokQcTSNPLLdLy4W9r08C17aE1XkNpkBHm Fv3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mail-followup-to :mime-version:content-disposition:user-agent; bh=ecIsW9IyKqy9C+GprLkMa0/3hlbYSHkBLQqcH/0CXKg=; b=VprI11/cbRj2Ln0zavTlVh0lQfdAEgrLDTShlKWXuz51OrgQS2qSAnDMUYa/e4cWqC u75W9MRTR50HGVdz+rliMt5m28f4cy8hd2ezzuhbenU6GjLNF1HhMtASKWgeVMxODPR2 QAaAk7/RlbPH4hsZzZCoWdtP6hu2Y9JMBkumJQ0hwjbMZ4rqWOGKeJ2tqp8u5pNDYH/F 6PkMh2aY8WGcYIDFzkBXAlj7jvoaEbs3w2daHlLbeGv0S/zn/ppOuUPfXwy/OAeSbv8g jnqtavnluElTPVF0N5iSSlgqTel1Wt5gfbCArBtCEhcrVxMGnfl0cCx4KvoktMq7mpFV zPbg== X-Gm-Message-State: APzg51BSQsSeU6y0ETlu/GCsAv/beIjzTqXElpaHyxsVissukC/wOdkK Q/6nza19ZExiAG4Nrnj2s8U= X-Received: by 2002:a19:18c:: with SMTP id 134-v6mr2320546lfb.16.1536777323797; Wed, 12 Sep 2018 11:35:23 -0700 (PDT) Received: from vnote ([95.72.40.207]) by smtp.gmail.com with ESMTPSA id r22-v6sm295052ljh.22.2018.09.12.11.35.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 12 Sep 2018 11:35:22 -0700 (PDT) Date: Wed, 12 Sep 2018 21:35:19 +0300 From: Eugene Korenevsky To: Davidlohr Bueso , linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, Ard Biesheuvel Subject: [PATCH v3] efi: take size of partition entry from GPT header Message-ID: <20180912183519.GA31506@vnote> Mail-Followup-To: Eugene Korenevsky , Davidlohr Bueso , linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, Ard Biesheuvel MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Use gpt_header.sizeof_partition_entry instead of sizeof(gpt_entry) for GPT entry size. According to UEFI 2.7 spec 5.3.1 "GPT overview":, the size of a GUID Partition Entry element is defined in the Size Of Partition Entry field of GPT header. The GPT with entries sized more than sizeof(gpt_entry) is not illegal. OVMF firmware from EDK2 perfectly works with it, see edk2-tianocore source code. Changes since v1: refactoring (extract get_gpt_entry function), fix (&ptes[i] -> pte) Changes since v2: use le32_to_cpu, fix typo, sanity check for sizeof_partition_entry Signed-off-by: Eugene Korenevsky --- block/partitions/efi.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/block/partitions/efi.c b/block/partitions/efi.c index 39f70d968754..c8ff7860973d 100644 --- a/block/partitions/efi.c +++ b/block/partitions/efi.c @@ -429,8 +429,8 @@ static int is_gpt_valid(struct parsed_partitions *state, u64 lba, goto fail; } /* Check that sizeof_partition_entry has the correct value */ - if (le32_to_cpu((*gpt)->sizeof_partition_entry) != sizeof(gpt_entry)) { - pr_debug("GUID Partition Entry Size check failed.\n"); + if (le32_to_cpu((*gpt)->sizeof_partition_entry) < sizeof(gpt_entry)) { + pr_debug("GUID Partition Entry Size is too small.\n"); goto fail; } @@ -670,6 +670,12 @@ static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt, return 0; } +static gpt_entry *get_gpt_entry(gpt_header *gpt, gpt_entry *ptes, u32 index) +{ + return (gpt_entry *)((u8 *)ptes + + le32_to_cpu(gpt->sizeof_partition_entry) * index); +} + /** * efi_partition(struct parsed_partitions *state) * @state: disk parsed partitions @@ -704,32 +710,36 @@ int efi_partition(struct parsed_partitions *state) pr_debug("GUID Partition Table is valid! Yea!\n"); - for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; i++) { + for (i = 0; + i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; + i++) { + gpt_entry *pte = get_gpt_entry(gpt, ptes, i); struct partition_meta_info *info; unsigned label_count = 0; unsigned label_max; - u64 start = le64_to_cpu(ptes[i].starting_lba); - u64 size = le64_to_cpu(ptes[i].ending_lba) - - le64_to_cpu(ptes[i].starting_lba) + 1ULL; + u64 start = le64_to_cpu(pte->starting_lba); + u64 size = le64_to_cpu(pte->ending_lba) - + le64_to_cpu(pte->starting_lba) + 1ULL; - if (!is_pte_valid(&ptes[i], last_lba(state->bdev))) + if (!is_pte_valid(pte, last_lba(state->bdev))) continue; put_partition(state, i+1, start * ssz, size * ssz); /* If this is a RAID volume, tell md */ - if (!efi_guidcmp(ptes[i].partition_type_guid, PARTITION_LINUX_RAID_GUID)) + if (!efi_guidcmp( + pte->partition_type_guid, PARTITION_LINUX_RAID_GUID)) state->parts[i + 1].flags = ADDPART_FLAG_RAID; info = &state->parts[i + 1].info; - efi_guid_to_str(&ptes[i].unique_partition_guid, info->uuid); + efi_guid_to_str(&pte->unique_partition_guid, info->uuid); /* Naively convert UTF16-LE to 7 bits. */ label_max = min(ARRAY_SIZE(info->volname) - 1, - ARRAY_SIZE(ptes[i].partition_name)); + ARRAY_SIZE(pte->partition_name)); info->volname[label_max] = 0; while (label_count < label_max) { - u8 c = ptes[i].partition_name[label_count] & 0xff; + u8 c = pte->partition_name[label_count] & 0xff; if (c && !isprint(c)) c = '!'; info->volname[label_count] = c; -- 2.18.0