From patchwork Sun Jan 15 22:31:56 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alden Tondettar X-Patchwork-Id: 9517941 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 87181601B7 for ; Sun, 15 Jan 2017 22:44:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4FB3E2841C for ; Sun, 15 Jan 2017 22:44:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 43B8B28438; Sun, 15 Jan 2017 22:44:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E3EB82841C for ; Sun, 15 Jan 2017 22:44:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751185AbdAOWoT (ORCPT ); Sun, 15 Jan 2017 17:44:19 -0500 Received: from mail-pg0-f68.google.com ([74.125.83.68]:32866 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751173AbdAOWoR (ORCPT ); Sun, 15 Jan 2017 17:44:17 -0500 Received: by mail-pg0-f68.google.com with SMTP id 194so3702847pgd.0; Sun, 15 Jan 2017 14:44:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Sqw/mJruobWan8KApob6fgBzAM6/T+gc99bgTyBWM5s=; b=on/ffXTIF3n8znyJm3CxA/Gqyy/8/ZHO+oeUv8Q+qjQdHceNwxuXUSOpffmPH4UYdZ X/V7IUAzfrgwCqFTNq2Yx99hW47pXMSY0ZDCgzZmfR71bLPKe71TWHtnN9JwjSRCPa/q 1JSBEMLb7Llk9BWVIBBIGw4trnxQzwku8rEELVuy+npGa3vcYcE6R2JIGKfgqCFZnKxF sk1Y9Kk01CcYXMzaB+Z6iwi4xCE023Qtyp/xy+NIG0SYwuTQPpAleRWHAiu9TK6Y0YGX +HMOTTG0QLiZn49iiiftdVIasKT0YTiZpImUkWt7HGNxqCtkT/CRQE8ApsAIKd+kpcgG RQcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Sqw/mJruobWan8KApob6fgBzAM6/T+gc99bgTyBWM5s=; b=BXNsKmqs0gpu+sM25QUXkPmH16lne7RKd4zc0UHPwzYcTFORddSPrK7FYuJcBYjF78 C7opdPStr/ldO2xqGorSwQlzGPffZkb05z3Taw8+fp/Dr4meQESdnu3yMI7dJcqUzC+/ yVsb5uJCDwZShP+cZmsxtf7+mL9B2CDJ84MkmDwzkvTv6oonGmHWtKUHkIAUJlyq1Mgo r3IWLKJyKt8GayZsXXMWDQvs5bkFHi37GhLzFG3S9lG/0Q7pTk5UCCNLv+7EjIM/8WOY gi3DyxWCXf+EmDzWRnuYKCeBv5Rl8oCQLUMVH5Q79R0qQpC2DeQRo+xLUkvAymBiAKJm hIVw== X-Gm-Message-State: AIkVDXKMyONs50Ef+BcJTkwgofOqwYmSxFdItcM0i0XemRFmhoujRlmv2nayf8FF3xbGLw== X-Received: by 10.98.3.7 with SMTP id 7mr34528925pfd.9.1484520257179; Sun, 15 Jan 2017 14:44:17 -0800 (PST) Received: from rincewind.rincewind (57.sub-97-44-128.myvzw.com. [97.44.128.57]) by smtp.gmail.com with ESMTPSA id r78sm26605017pfl.63.2017.01.15.14.44.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 15 Jan 2017 14:44:15 -0800 (PST) From: Alden Tondettar To: Davidlohr Bueso , Jens Axboe , linux-efi@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Alden Tondettar Subject: [PATCH] partitions/efi: Fix integer overflow in GPT size calculation Date: Sun, 15 Jan 2017 15:31:56 -0700 Message-Id: <1484519516-18295-1-git-send-email-alden.tondettar@gmail.com> X-Mailer: git-send-email 2.1.4 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If a GUID Partition Table claims to have more than 2**25 entries, the calculation of the partition table size in alloc_read_gpt_entries() will overflow a 32-bit integer and not enough space will be allocated for the table. Nothing seems to get written out of bounds, but later efi_partition() will read up to 32768 bytes from a 128 byte buffer, possibly OOPSing or exposing information to /proc/partitions and uevents. The problem exists on both 64-bit and 32-bit platforms. Fix the overflow and also print a meaningful debug message if the table size is too large. Signed-off-by: Alden Tondettar Acked-by: Ard Biesheuvel --- block/partitions/efi.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/block/partitions/efi.c b/block/partitions/efi.c index bcd86e5..39f70d9 100644 --- a/block/partitions/efi.c +++ b/block/partitions/efi.c @@ -293,7 +293,7 @@ static gpt_entry *alloc_read_gpt_entries(struct parsed_partitions *state, if (!gpt) return NULL; - count = le32_to_cpu(gpt->num_partition_entries) * + count = (size_t)le32_to_cpu(gpt->num_partition_entries) * le32_to_cpu(gpt->sizeof_partition_entry); if (!count) return NULL; @@ -352,7 +352,7 @@ static int is_gpt_valid(struct parsed_partitions *state, u64 lba, gpt_header **gpt, gpt_entry **ptes) { u32 crc, origcrc; - u64 lastlba; + u64 lastlba, pt_size; if (!ptes) return 0; @@ -434,13 +434,20 @@ static int is_gpt_valid(struct parsed_partitions *state, u64 lba, goto fail; } + /* Sanity check partition table size */ + pt_size = (u64)le32_to_cpu((*gpt)->num_partition_entries) * + le32_to_cpu((*gpt)->sizeof_partition_entry); + if (pt_size > KMALLOC_MAX_SIZE) { + pr_debug("GUID Partition Table is too large: %llu > %lu bytes\n", + (unsigned long long)pt_size, KMALLOC_MAX_SIZE); + goto fail; + } + if (!(*ptes = alloc_read_gpt_entries(state, *gpt))) goto fail; /* Check the GUID Partition Entry Array CRC */ - crc = efi_crc32((const unsigned char *) (*ptes), - le32_to_cpu((*gpt)->num_partition_entries) * - le32_to_cpu((*gpt)->sizeof_partition_entry)); + crc = efi_crc32((const unsigned char *) (*ptes), pt_size); if (crc != le32_to_cpu((*gpt)->partition_entry_array_crc32)) { pr_debug("GUID Partition Entry Array CRC check failed.\n");