Skip to content

Commit 03f5a99

Browse files
rpedgecoliuw
authored andcommitted
Drivers: hv: vmbus: Leak pages if set_memory_encrypted() fails
In CoCo VMs it is possible for the untrusted host to cause set_memory_encrypted() or set_memory_decrypted() to fail such that an error is returned and the resulting memory is shared. Callers need to take care to handle these errors to avoid returning decrypted (shared) memory to the page allocator, which could lead to functional or security issues. VMBus code could free decrypted pages if set_memory_encrypted()/decrypted() fails. Leak the pages if this happens. Signed-off-by: Rick Edgecombe <[email protected]> Signed-off-by: Michael Kelley <[email protected]> Reviewed-by: Kuppuswamy Sathyanarayanan <[email protected]> Acked-by: Kirill A. Shutemov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Wei Liu <[email protected]> Message-ID: <[email protected]>
1 parent f971f6d commit 03f5a99

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

drivers/hv/connection.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,17 @@ int vmbus_connect(void)
237237
vmbus_connection.monitor_pages[0], 1);
238238
ret |= set_memory_decrypted((unsigned long)
239239
vmbus_connection.monitor_pages[1], 1);
240-
if (ret)
240+
if (ret) {
241+
/*
242+
* If set_memory_decrypted() fails, the encryption state
243+
* of the memory is unknown. So leak the memory instead
244+
* of risking returning decrypted memory to the free list.
245+
* For simplicity, always handle both pages the same.
246+
*/
247+
vmbus_connection.monitor_pages[0] = NULL;
248+
vmbus_connection.monitor_pages[1] = NULL;
241249
goto cleanup;
250+
}
242251

243252
/*
244253
* Set_memory_decrypted() will change the memory contents if
@@ -337,13 +346,19 @@ void vmbus_disconnect(void)
337346
vmbus_connection.int_page = NULL;
338347
}
339348

340-
set_memory_encrypted((unsigned long)vmbus_connection.monitor_pages[0], 1);
341-
set_memory_encrypted((unsigned long)vmbus_connection.monitor_pages[1], 1);
349+
if (vmbus_connection.monitor_pages[0]) {
350+
if (!set_memory_encrypted(
351+
(unsigned long)vmbus_connection.monitor_pages[0], 1))
352+
hv_free_hyperv_page(vmbus_connection.monitor_pages[0]);
353+
vmbus_connection.monitor_pages[0] = NULL;
354+
}
342355

343-
hv_free_hyperv_page(vmbus_connection.monitor_pages[0]);
344-
hv_free_hyperv_page(vmbus_connection.monitor_pages[1]);
345-
vmbus_connection.monitor_pages[0] = NULL;
346-
vmbus_connection.monitor_pages[1] = NULL;
356+
if (vmbus_connection.monitor_pages[1]) {
357+
if (!set_memory_encrypted(
358+
(unsigned long)vmbus_connection.monitor_pages[1], 1))
359+
hv_free_hyperv_page(vmbus_connection.monitor_pages[1]);
360+
vmbus_connection.monitor_pages[1] = NULL;
361+
}
347362
}
348363

349364
/*

0 commit comments

Comments
 (0)