From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1kbG94-0000xK-U2 for mharc-grub-devel@gnu.org; Fri, 06 Nov 2020 23:48:10 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:45516) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kbG94-0000vt-5D for grub-devel@gnu.org; Fri, 06 Nov 2020 23:48:10 -0500 Received: from mail-ot1-x32e.google.com ([2607:f8b0:4864:20::32e]:38367) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kbG92-0002yp-DC for grub-devel@gnu.org; Fri, 06 Nov 2020 23:48:09 -0500 Received: by mail-ot1-x32e.google.com with SMTP id a15so1611137otf.5 for ; Fri, 06 Nov 2020 20:48:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficientek-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lkbvev1Ht7wOvqSFbPsrmK32HkoKgwYwiogqyw3lok8=; b=JKpCD3lhi353fDs3U9yy2vzZnsDffRW2P1cTBBI4n/BM6EpR4tRPEcQnQRrcnzXpk1 eKIJdbO/P77InsHtFJg/b1Zt1Rh//kmfIi72fzL++DIK7lManp6fEItZKnjvvylk2AX1 B1qoxrn7HB128ocs62Q+YyrPfi6hVXLlSgIN6OsZIzxJLXdfsLgNW6S+WHgypZCMuKgo PF4/lCt1C14nArcWlFafbKZOWGBKsyTSqX7JvOPIxoYXP9QSPiRxH9snQszQfsGeQKIV IQdV5jlg5zhLszrpLKgeNI529iaq/TgbLWm7kwfnVIPgGdHMx4xPyEOZ7M4414+RZowP 6h+g== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=lkbvev1Ht7wOvqSFbPsrmK32HkoKgwYwiogqyw3lok8=; b=hMS+OUYXmsKJHmR4ixPClNimqGwmBBxH0bMZdZe/pG6mLyKBloMG1ewLLAIxOdy9lR 9m1vEGUS/iWtfwUoXnU2LaBCb2O8mV9kHjfxYy9Jo+xKq17LIVSkfY3MM6h8ysOKSYVL ja/pV40VQC9I4sT9qx866jrjdfJhGmB71MmbnATEFwB25atPAFz+6O/Z5OraYLBR/aEG HZADWExuWyH9HR14oRwKze5spCNArHaywPhVzIEWcuRqnpHzdYGigUAzvGt0AO5LVNs7 M3dqOeRRHhKA8NtHv2hojOz93jT/S5No3Ghv+y8L7t3b8ZYUoj0h5Qpm7xN54HlAzcjE K2SA== X-Gm-Message-State: AOAM532Y7NluQcRqQ6CqzAMvoObEvTz6pwiCnMHmxv+W0mF9sdt+wCmG kCew9S4Zu9HCg/stHssDfweZUJzthjXlXMIW X-Google-Smtp-Source: ABdhPJyE3j9q1inxOQV2n9CLM3ZLmOqD1/Q975UkQXCo81cVs18BoZhM4uUk6XT4iCBtmHqUp7ysjQ== X-Received: by 2002:a05:6830:1644:: with SMTP id h4mr3416765otr.185.1604724487098; Fri, 06 Nov 2020 20:48:07 -0800 (PST) Received: from localhost.localdomain ([136.49.44.103]) by smtp.gmail.com with ESMTPSA id x25sm836888oie.17.2020.11.06.20.48.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Nov 2020 20:48:06 -0800 (PST) From: Glenn Washburn To: grub-devel@gnu.org Cc: Patrick Steinhardt , Daniel Kiper , Glenn Washburn Subject: [PATCH v4 14/15] luks2: Better error handling when setting up the cryptodisk. Date: Fri, 6 Nov 2020 22:44:34 -0600 Message-Id: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::32e; envelope-from=development@efficientek.com; helo=mail-ot1-x32e.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 07 Nov 2020 04:48:10 -0000 Signed-off-by: Glenn Washburn --- grub-core/disk/luks2.c | 70 +++++++++++++++++++++++++++++++++++++++--- include/grub/misc.h | 2 ++ 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/grub-core/disk/luks2.c b/grub-core/disk/luks2.c index 4a4a0dec4..751b48d6a 100644 --- a/grub-core/disk/luks2.c +++ b/grub-core/disk/luks2.c @@ -600,9 +600,16 @@ luks2_recover_key (grub_disk_t source, goto err; } + if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN) + { + ret = grub_error (GRUB_ERR_BUG, "not a luks2 device"); + goto err; + } + /* Try all keyslot */ for (i = 0; i < size; i++) { + grub_errno = GRUB_ERR_NONE; ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i); if (ret) goto err; @@ -617,13 +624,66 @@ luks2_recover_key (grub_disk_t source, /* Set up disk according to keyslot's segment. */ crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL); - crypt->log_sector_size = sizeof (unsigned int) * 8 - - __builtin_clz ((unsigned int) segment.sector_size) - 1; + crypt->log_sector_size = grub_log2ull (segment.sector_size); if (grub_strcmp (segment.size, "dynamic") == 0) - crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size)) - - crypt->offset_sectors; + { + /* Convert source sized number of sectors to cryptodisk sized sectors */ + crypt->total_sectors = source->total_sectors >> (crypt->log_sector_size - source->log_sector_size); + if (crypt->total_sectors < crypt->offset_sectors) + { + grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" offset" + " is greater than disk size.", + segment.slot_key); + continue; + } + + crypt->total_sectors -= crypt->offset_sectors; + } else - crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size; + { + crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size; + if (grub_errno == GRUB_ERR_NONE) + ; + else if(grub_errno == GRUB_ERR_BAD_NUMBER) + { + /* TODO: Unparsable number-string, try to use the whole disk */ + grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size" + " is not a parsable number.", + segment.slot_key); + continue; + } + else if(grub_errno == GRUB_ERR_OUT_OF_RANGE) + { + /* There was an overflow in parsing segment.size, so disk must + * be very large or the string is incorrect. */ + if ((source->total_sectors + >> (crypt->log_sector_size - source->log_sector_size)) + > crypt->total_sectors) + { + grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\"" + " size is very large. The end may be" + " inaccessible.", + segment.slot_key); + } + else + { + /* FIXME: Set total_sectors as in "dynamic" case. */ + grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\"" + " size greater than the source" + " device.", + segment.slot_key); + continue; + } + } + } + + if (crypt->total_sectors == 0) + { + grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has" + " zero sectors, skipping.", + segment.slot_key); + continue; + } ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot, (const grub_uint8_t *) passphrase, grub_strlen (passphrase)); diff --git a/include/grub/misc.h b/include/grub/misc.h index b7ca6dd58..ec25131ba 100644 --- a/include/grub/misc.h +++ b/include/grub/misc.h @@ -481,5 +481,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file, #define grub_max(a, b) (((a) > (b)) ? (a) : (b)) #define grub_min(a, b) (((a) < (b)) ? (a) : (b)) +#define grub_log2ull(n) (GRUB_TYPE_BITS (grub_uint64_t) \ + - __builtin_clzll (n) - 1) #endif /* ! GRUB_MISC_HEADER */ -- 2.27.0