Re: [PATCH v2 7/8] x86/insn: Extend error reporting from insn_fetch_from_user[_inatomic]()

From: Borislav Petkov
Date: Fri May 21 2021 - 10:34:46 EST


On Wed, May 19, 2021 at 03:52:50PM +0200, Joerg Roedel wrote:
> diff --git a/arch/x86/lib/insn-eval.c b/arch/x86/lib/insn-eval.c
> index 4eecb9c7c6a0..d8a057ba0895 100644
> --- a/arch/x86/lib/insn-eval.c
> +++ b/arch/x86/lib/insn-eval.c
> @@ -1442,27 +1442,36 @@ static int insn_get_effective_ip(struct pt_regs *regs, unsigned long *ip)
> * insn_fetch_from_user() - Copy instruction bytes from user-space memory
> * @regs: Structure with register values as seen when entering kernel mode
> * @buf: Array to store the fetched instruction
> + * @copied: Pointer to an int where the number of copied instruction bytes
> + * is stored. Can be NULL.
> *
> * Gets the linear address of the instruction and copies the instruction bytes
> * to the buf.
> *
> * Returns:
> *
> - * Number of instruction bytes copied.
> + * -EINVAL if the linear address of the instruction could not be calculated
> + * -EFAULT if nothing was copied
> + * 0 on success
> *
> - * 0 if nothing was copied.
> */
> -int insn_fetch_from_user(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE])
> +int insn_fetch_from_user(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE],
> + int *copied)
> {
> unsigned long ip;
> int not_copied;
> + int bytes;
>
> if (insn_get_effective_ip(regs, &ip))
> - return 0;
> + return -EINVAL;
>
> not_copied = copy_from_user(buf, (void __user *)ip, MAX_INSN_SIZE);
>
> - return MAX_INSN_SIZE - not_copied;
> + bytes = MAX_INSN_SIZE - not_copied;
> + if (copied)
> + *copied = bytes;
> +
> + return bytes ? 0 : -EFAULT;

Why not simpler?

return value >= 0 says how many bytes were copied
return value < 0 means some kind of error

And then you don't need @copied...

Ditto for the other one.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette