From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1gMSEo-0002a4-6V for mharc-grub-devel@gnu.org; Tue, 13 Nov 2018 01:31:50 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43669) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gMSEk-0002Zb-Pc for grub-devel@gnu.org; Tue, 13 Nov 2018 01:31:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gMSEf-0002GX-MO for grub-devel@gnu.org; Tue, 13 Nov 2018 01:31:46 -0500 Received: from smtp2.provo.novell.com ([137.65.250.81]:41337) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gMSEd-00025f-Jl for grub-devel@gnu.org; Tue, 13 Nov 2018 01:31:40 -0500 Received: from mazu (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by smtp2.provo.novell.com with ESMTP (TLS encrypted); Mon, 12 Nov 2018 23:31:23 -0700 Date: Tue, 13 Nov 2018 14:31:18 +0800 From: Michael Chang To: grub-devel@gnu.org Subject: [PATCH] verifiers: fix double close on pgp's sig file descriptor Message-ID: <20181113063118.GA15556@mazu> Mail-Followup-To: grub-devel@gnu.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 137.65.250.81 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Nov 2018 06:31:48 -0000 An error emerged as when I was tesing the verifiers branch, so instead of putting it in pgp prefix, the verifiers is used to reflect what the patch is based on. While running verify_detached, grub aborts with error. verify_detached /@/.snapshots/1/snapshot/boot/grub/grub.cfg /@/.snapshots/1/snapshot/boot/grub/grub.cfg.sig alloc magic is broken at 0x7beea660: 0 Aborted. Press any key to exit. The error is caused by sig file desciptor been closed twice, first time in grub_verify_signature() to which it is passed as parameter. Second in grub_cmd_verify_signature() or in whichever opens the sig file decriptor. The second close is not consider as bug to me either, as in common rule of what opens a file has to close it to avoid file descriptor leakage. Afterall the design of grub_verify_signature() makes it diffcult to keep a good trace on opened file descriptor from it's caller. Let's refine the application interface to accept file path rather than descriptor, in this way the caller doesn't have to care about closing the descriptor by delegating it to grub_verify_signature() with full tracing to opened file descriptor by itself. Signed-off-by: Michael Chang --- grub-core/commands/pgp.c | 33 ++++++++++++++++----------------- include/grub/pubkey.h | 2 +- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/grub-core/commands/pgp.c b/grub-core/commands/pgp.c index d5d7c0f0a..f294057b5 100644 --- a/grub-core/commands/pgp.c +++ b/grub-core/commands/pgp.c @@ -495,13 +495,12 @@ grub_verify_signature_init (struct grub_pubkey_context *ctxt, grub_file_t sig) grub_dprintf ("crypt", "alive\n"); - ctxt->sig = sig; - ctxt->hash_context = grub_zalloc (ctxt->hash->contextsize); if (!ctxt->hash_context) return grub_errno; ctxt->hash->init (ctxt->hash_context); + ctxt->sig = sig; return GRUB_ERR_NONE; } @@ -684,15 +683,26 @@ grub_pubkey_close (void *ctxt) } grub_err_t -grub_verify_signature (grub_file_t f, grub_file_t sig, +grub_verify_signature (grub_file_t f, const char *fsig, struct grub_public_key *pkey) { + grub_file_t sig; grub_err_t err; struct grub_pubkey_context ctxt; grub_uint8_t *readbuf = NULL; + + sig = grub_file_open (fsig, + GRUB_FILE_TYPE_SIGNATURE + | GRUB_FILE_TYPE_NO_DECOMPRESS); + if (!sig) + return grub_errno; + err = grub_verify_signature_init (&ctxt, sig); if (err) - return err; + { + grub_file_close (sig); + return err; + } readbuf = grub_zalloc (READBUF_SIZE); if (!readbuf) @@ -806,7 +816,7 @@ static grub_err_t grub_cmd_verify_signature (grub_extcmd_context_t ctxt, int argc, char **args) { - grub_file_t f = NULL, sig = NULL; + grub_file_t f = NULL; grub_err_t err = GRUB_ERR_NONE; struct grub_public_key *pk = NULL; @@ -844,19 +854,8 @@ grub_cmd_verify_signature (grub_extcmd_context_t ctxt, goto fail; } - sig = grub_file_open (args[1], - GRUB_FILE_TYPE_SIGNATURE - | GRUB_FILE_TYPE_NO_DECOMPRESS); - if (!sig) - { - err = grub_errno; - goto fail; - } - - err = grub_verify_signature (f, sig, pk); + err = grub_verify_signature (f, args[1], pk); fail: - if (sig) - grub_file_close (sig); if (f) grub_file_close (f); if (pk) diff --git a/include/grub/pubkey.h b/include/grub/pubkey.h index 4a9d04b43..fb8be9cbb 100644 --- a/include/grub/pubkey.h +++ b/include/grub/pubkey.h @@ -25,7 +25,7 @@ struct grub_public_key * grub_load_public_key (grub_file_t f); grub_err_t -grub_verify_signature (grub_file_t f, grub_file_t sig, +grub_verify_signature (grub_file_t f, const char *fsig, struct grub_public_key *pk); -- 2.19.0