Skip to content

Commit 4954f5b

Browse files
joergroedelsuryasaimadhu
authored andcommitted
x86/sev-es: Use __put_user()/__get_user() for data accesses
The put_user() and get_user() functions do checks on the address which is passed to them. They check whether the address is actually a user-space address and whether its fine to access it. They also call might_fault() to indicate that they could fault and possibly sleep. All of these checks are neither wanted nor needed in the #VC exception handler, which can be invoked from almost any context and also for MMIO instructions from kernel space on kernel memory. All the #VC handler wants to know is whether a fault happened when the access was tried. This is provided by __put_user()/__get_user(), which just do the access no matter what. Also add comments explaining why __get_user() and __put_user() are the best choice here and why it is safe to use them in this context. Also explain why copy_to/from_user can't be used. In addition, also revert commit 7024f60 ("x86/sev-es: Handle string port IO to kernel memory properly") because using __get_user()/__put_user() fixes the same problem while the above commit introduced several problems: 1) It uses access_ok() which is only allowed in task context. 2) It uses memcpy() which has no fault handling at all and is thus unsafe to use here. [ bp: Fix up commit ID of the reverted commit above. ] Fixes: f980f9c ("x86/sev-es: Compile early handler code into kernel image") Signed-off-by: Joerg Roedel <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Cc: [email protected] # v5.10+ Link: https://lkml.kernel.org/r/[email protected]
1 parent c25bbdb commit 4954f5b

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

arch/x86/kernel/sev.c

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -315,31 +315,44 @@ static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
315315
u16 d2;
316316
u8 d1;
317317

318-
/* If instruction ran in kernel mode and the I/O buffer is in kernel space */
319-
if (!user_mode(ctxt->regs) && !access_ok(target, size)) {
320-
memcpy(dst, buf, size);
321-
return ES_OK;
322-
}
323-
318+
/*
319+
* This function uses __put_user() independent of whether kernel or user
320+
* memory is accessed. This works fine because __put_user() does no
321+
* sanity checks of the pointer being accessed. All that it does is
322+
* to report when the access failed.
323+
*
324+
* Also, this function runs in atomic context, so __put_user() is not
325+
* allowed to sleep. The page-fault handler detects that it is running
326+
* in atomic context and will not try to take mmap_sem and handle the
327+
* fault, so additional pagefault_enable()/disable() calls are not
328+
* needed.
329+
*
330+
* The access can't be done via copy_to_user() here because
331+
* vc_write_mem() must not use string instructions to access unsafe
332+
* memory. The reason is that MOVS is emulated by the #VC handler by
333+
* splitting the move up into a read and a write and taking a nested #VC
334+
* exception on whatever of them is the MMIO access. Using string
335+
* instructions here would cause infinite nesting.
336+
*/
324337
switch (size) {
325338
case 1:
326339
memcpy(&d1, buf, 1);
327-
if (put_user(d1, target))
340+
if (__put_user(d1, target))
328341
goto fault;
329342
break;
330343
case 2:
331344
memcpy(&d2, buf, 2);
332-
if (put_user(d2, target))
345+
if (__put_user(d2, target))
333346
goto fault;
334347
break;
335348
case 4:
336349
memcpy(&d4, buf, 4);
337-
if (put_user(d4, target))
350+
if (__put_user(d4, target))
338351
goto fault;
339352
break;
340353
case 8:
341354
memcpy(&d8, buf, 8);
342-
if (put_user(d8, target))
355+
if (__put_user(d8, target))
343356
goto fault;
344357
break;
345358
default:
@@ -370,30 +383,43 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
370383
u16 d2;
371384
u8 d1;
372385

373-
/* If instruction ran in kernel mode and the I/O buffer is in kernel space */
374-
if (!user_mode(ctxt->regs) && !access_ok(s, size)) {
375-
memcpy(buf, src, size);
376-
return ES_OK;
377-
}
378-
386+
/*
387+
* This function uses __get_user() independent of whether kernel or user
388+
* memory is accessed. This works fine because __get_user() does no
389+
* sanity checks of the pointer being accessed. All that it does is
390+
* to report when the access failed.
391+
*
392+
* Also, this function runs in atomic context, so __get_user() is not
393+
* allowed to sleep. The page-fault handler detects that it is running
394+
* in atomic context and will not try to take mmap_sem and handle the
395+
* fault, so additional pagefault_enable()/disable() calls are not
396+
* needed.
397+
*
398+
* The access can't be done via copy_from_user() here because
399+
* vc_read_mem() must not use string instructions to access unsafe
400+
* memory. The reason is that MOVS is emulated by the #VC handler by
401+
* splitting the move up into a read and a write and taking a nested #VC
402+
* exception on whatever of them is the MMIO access. Using string
403+
* instructions here would cause infinite nesting.
404+
*/
379405
switch (size) {
380406
case 1:
381-
if (get_user(d1, s))
407+
if (__get_user(d1, s))
382408
goto fault;
383409
memcpy(buf, &d1, 1);
384410
break;
385411
case 2:
386-
if (get_user(d2, s))
412+
if (__get_user(d2, s))
387413
goto fault;
388414
memcpy(buf, &d2, 2);
389415
break;
390416
case 4:
391-
if (get_user(d4, s))
417+
if (__get_user(d4, s))
392418
goto fault;
393419
memcpy(buf, &d4, 4);
394420
break;
395421
case 8:
396-
if (get_user(d8, s))
422+
if (__get_user(d8, s))
397423
goto fault;
398424
memcpy(buf, &d8, 8);
399425
break;

0 commit comments

Comments
 (0)