From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by mx.groups.io with SMTP id smtpd.web09.1470.1582623565664241991 for ; Tue, 25 Feb 2020 01:39:25 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@linaro.org header.s=google header.b=BDAL2qPd; spf=pass (domain: linaro.org, ip: 209.85.221.66, mailfrom: ard.biesheuvel@linaro.org) Received: by mail-wr1-f66.google.com with SMTP id j7so1332819wrp.13 for ; Tue, 25 Feb 2020 01:39:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=EH3GVtGq8OoMv8WgT1c0rlKWHdhlu2xPRBdBnQV2AQ0=; b=BDAL2qPdZcFvGaN8MhXtOtAfGHhEPgM+lyOU9zpal7/8YhLuyvNRoeG7z7IYBgCXX/ BrC2AgBjiAz+N4OhLwx+kj02manExa2Dl2VlfFAF/wJFBn/QR84/TKW9XNe5bZszJPeg TUUBQYErvVHR8U2O5iQgaOpcTQfDMXdq5lYisXoNI7SPn9vfMEWHPH/nL+KcSyU3vx2b 3FQgT3VnXAQnfe1/iHArMeubH1592QA22tIwqkiHANQQetjW668drC5gYjkN+8wKNoVs hlBEOLfOwtI8scJAN7ep4BLBGCPgSuJiTxTC/RKOTjSyG51rlECZrObjrUSaZ63WMo3+ /A+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; bh=EH3GVtGq8OoMv8WgT1c0rlKWHdhlu2xPRBdBnQV2AQ0=; b=AykMZqW5JMQvZzKfyZv2II8cXpqwgMgxhaciuoJJCdlLWbQSEksBUrADNQ0mCHoWta OlF61wv6AqYxUnckab1dGepYCVvQ0bDHVukuCF3SK6msWnd5Exn+yZXPlFgR39Bgkyks jHOgOZzTM7Q1rrdmVc64M43mS2Q2iczoP11Pv/Yhk9/hemyWIUV6eNKjWDsGMyP40D/C irAkPis71kebb6oJYZTPVImvmn4ashENZQxNuZ2vzi1TuJIuqDHuiRKTYmTD4If1F34s 2nnClJPUGSeWIAhDJhyt7joeJOZKuQpZGPFwA7of1pb2/oXn3iBbY4hZ/pZ++TBa5cll DYLg== X-Gm-Message-State: APjAAAWJXUjaNyKoGaXhi59WCCT+q8tS3lSXFjWl3Rio+3DBeFfFXEPN hVwIVh4gWfXl9wOlhVeGr3iHlG1kAVwB8A== X-Google-Smtp-Source: APXvYqzqRKkOjd6MnygyYzSFG0l7ivt6y4cPl1ADb/hVsewc5bnw4VA2dl4a/ePB1dVuf3mTY8zq9w== X-Received: by 2002:adf:e3cd:: with SMTP id k13mr48029288wrm.302.1582623563871; Tue, 25 Feb 2020 01:39:23 -0800 (PST) Return-Path: Received: from e123331-lin.home ([2a01:cb1d:112:6f00:816e:ff0d:fb69:f613]) by smtp.gmail.com with ESMTPSA id y12sm23353079wrw.88.2020.02.25.01.39.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2020 01:39:23 -0800 (PST) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , lersek@redhat.com, leif@nuviainc.com, pjones@redhat.com, mjg59@google.com, agraf@csgraf.de, daniel.kiper@oracle.com, michael.d.kinney@intel.com, jian.j.wang@intel.com, hao.a.wu@intel.com, ray.ni@intel.com, zhichao.gao@intel.com Subject: [PATCH v2 5/6] MdeModulePkg/DxeCore: defer PE/COFF emulator registration to StartImage Date: Tue, 25 Feb 2020 10:39:07 +0100 Message-Id: <20200225093908.6707-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200225093908.6707-1-ard.biesheuvel@linaro.org> References: <20200225093908.6707-1-ard.biesheuvel@linaro.org> EDK2's implementation of the LoadImage() boot service permits non-native binaries to be loaded (i.e., X64 images on IA32 firmware), but any attempts to start such an image using StartImage() will return EFI_UNSUPPORTED. The integration of the PE/COFF emulator protocol into the DXE core deviates slightly from this paradigm, given that its IsImageSupported hook as well as its RegisterImage hook are invoked from LoadImage, and by the time StartImage is called, no opportunity is given to the provider of the PE/COFF emulator protocol to prevent an image from being started if it only supports loading it. To address this disparity, let's move the invocation of RegisterImage() to the implementation of the StartImage() boot service, allowing the emulator to permit LoadImage() but reject StartImage() on images that turn out not to meet the requirements of the emulator as it is being started. Signed-off-by: Ard Biesheuvel --- MdeModulePkg/Core/Dxe/Image/Image.c | 24 +++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Image/Image.c b/MdeModulePkg/Core/Dxe/Image/Image.c index 22a87ecf6d7c..d86da89ee704 100644 --- a/MdeModulePkg/Core/Dxe/Image/Image.c +++ b/MdeModulePkg/Core/Dxe/Image/Image.c @@ -756,17 +756,6 @@ CoreLoadPeImage ( // Get the image entry point. // Image->EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)Image->ImageContext.EntryPoint; - if (Image->PeCoffEmu != NULL) { - Status = Image->PeCoffEmu->RegisterImage (Image->PeCoffEmu, - Image->ImageBasePage, - EFI_PAGES_TO_SIZE (Image->NumberOfPages), - &Image->EntryPoint); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_LOAD | DEBUG_ERROR, - "CoreLoadPeImage: Failed to register foreign image with emulator.\n")); - goto Done; - } - } // // Fill in the image information for the Loaded Image Protocol @@ -1603,6 +1592,19 @@ CoreStartImage ( return EFI_UNSUPPORTED; } + if (Image->PeCoffEmu != NULL) { + Status = Image->PeCoffEmu->RegisterImage (Image->PeCoffEmu, + Image->ImageBasePage, + EFI_PAGES_TO_SIZE (Image->NumberOfPages), + &Image->EntryPoint); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_LOAD | DEBUG_ERROR, + "CoreLoadPeImage: Failed to register foreign image with emulator - %r\n", + Status)); + return Status; + } + } + PERF_START_IMAGE_BEGIN (Handle); -- 2.17.1