Re: [PATCH v9 03/24] virt: sev-guest: Make payload a variable length array

Nikunj A. Dadhania
2024-06-24T18:12:59Z

On 6/24/2024 7:09 PM, Borislav Petkov wrote:
> On Mon, Jun 24, 2024 at 08:00:38AM -0500, Tom Lendacky wrote:
>> An alternative to the #defines would be something like:
>>
>> struct snp_guest_msg {
>> struct snp_guest_msg_hdr hdr;
>> u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)];
>> } __packed;
>>
>> Not sure it matters, but does reduce the changes while ensuring the
>> payload plus header doesn't exceed a page.

Yes, it does reduce a lot of churn.

>
> Yeah, because that would've been my next question - the requirement to keep it
> <= PAGE_SIZE.
>
> So yeah, Nikunj, please do that also and add a
>
> BUILD_BUG_ON(sizeof(struct snp_guest_msg) > PAGE_SIZE);
>
> somewhere in the driver to catch all kinds of funky stuff.

Sure, here is the new patch. I have separated the variable name changes to a new patch.

Subject: [PATCH] virt: sev-guest: Ensure the SNP guest messages do not exceed
a page

Currently, snp_guest_msg includes a message header (96 bytes) and a
payload (4000 bytes). There is an implicit assumption here that the SNP
message header will always be 96 bytes, and with that assumption the
payload array size has been set to 4000 bytes magic number. If any new
member is added to the SNP message header, the SNP guest message will span
more than a page.

Instead of using magic number '4000' for the payload, declare the
snp_guest_msg in a way that payload plus the message header do not exceed a
page.

Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
Suggested-by: Tom Lendacky <thomas.lendacky@amd.com>
---
drivers/virt/coco/sev-guest/sev-guest.h | 2 +-
drivers/virt/coco/sev-guest/sev-guest.c | 3 +++
2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/virt/coco/sev-guest/sev-guest.h b/drivers/virt/coco/sev-guest/sev-guest.h
index ceb798a404d6..de14a4f01b9d 100644
--- a/drivers/virt/coco/sev-guest/sev-guest.h
+++ b/drivers/virt/coco/sev-guest/sev-guest.h
@@ -60,7 +60,7 @@ struct snp_guest_msg_hdr {

struct snp_guest_msg {
struct snp_guest_msg_hdr hdr;
- u8 payload[4000];
+ u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)];
} __packed;

#endif /* __VIRT_SEVGUEST_H__ */
diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c
index 427571a2d1a2..c4aae5d4308e 100644
--- a/drivers/virt/coco/sev-guest/sev-guest.c
+++ b/drivers/virt/coco/sev-guest/sev-guest.c
@@ -1033,6 +1033,9 @@ static int __init sev_guest_probe(struct platform_device *pdev)
snp_dev->dev = dev;
snp_dev->secrets = secrets;

+ /* Ensure SNP guest messages do not span more than a page */
+ BUILD_BUG_ON(sizeof(struct snp_guest_msg) > PAGE_SIZE);
+
/* Allocate the shared page used for the request and response message. */
snp_dev->request = alloc_shared_pages(dev, sizeof(struct snp_guest_msg));
if (!snp_dev->request)
--
2.34.1