public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Mirsad Todorovac <mtodorovac69@gmail.com>
To: "Gustavo A. R. Silva" <gustavo@embeddedor.com>,
	Kees Cook <kees@kernel.org>, Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	Daniel Sneddon <daniel.sneddon@linux.intel.com>,
	Arnd Bergmann <arnd@arndb.de>, Brian Gerst <brgerst@gmail.com>,
	Josh Poimboeuf <jpoimboe@kernel.org>,
	Pawan Gupta <pawan.kumar.gupta@linux.intel.com>,
	Peter Collingbourne <pcc@google.com>,
	linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH] x86/syscall: Avoid memcpy() for ia32 syscall_get_arguments()
Date: Tue, 9 Jul 2024 20:20:17 +0200	[thread overview]
Message-ID: <39b94091-d452-4dac-9012-ae43024462cd@gmail.com> (raw)
In-Reply-To: <e95852cf-231a-4525-9075-fad42930d328@embeddedor.com>



On 7/9/24 01:44, Gustavo A. R. Silva wrote:
> 
> 
> On 7/8/24 14:22, Kees Cook wrote:
>> Modern (fortified) memcpy() prefers to avoid writing (or reading) beyond
>> the end of the addressed destination (or source) struct member:
>>
>> In function ‘fortify_memcpy_chk’,
>>      inlined from ‘syscall_get_arguments’ at ./arch/x86/include/asm/syscall.h:85:2,
>>      inlined from ‘populate_seccomp_data’ at kernel/seccomp.c:258:2,
>>      inlined from ‘__seccomp_filter’ at kernel/seccomp.c:1231:3:
>> ./include/linux/fortify-string.h:580:25: error: call to ‘__read_overflow2_field’ declared with attribute warning: detected read beyond size of field (2nd parameter); maybe use struct_group()? [-Werror=attribute-warning]
>>    580 |                         __read_overflow2_field(q_size_field, size);
>>        |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> As already done for x86_64 and compat mode, do not use memcpy() to
>> extract syscall arguments from struct pt_regs but rather just perform
>> direct assignments. Binary output differences are negligible, and actually
>> ends up using less stack space:
>>
>> -       sub    $0x84,%esp
>> +       sub    $0x6c,%esp
>>
>> and less text size:
>>
>>     text    data     bss     dec     hex filename
>>    10794     252       0   11046    2b26 gcc-32b/kernel/seccomp.o.stock
>>    10714     252       0   10966    2ad6 gcc-32b/kernel/seccomp.o.after
>>
>> Reported-by: Mirsad Todorovac <mtodorovac69@gmail.com>
>> Closes: https://lore.kernel.org/lkml/9b69fb14-df89-4677-9c82-056ea9e706f5@gmail.com/
>> Signed-off-by: Kees Cook <kees@kernel.org>
>> ---
>> Cc: Thomas Gleixner <tglx@linutronix.de>
>> Cc: Ingo Molnar <mingo@redhat.com>
>> Cc: Borislav Petkov <bp@alien8.de>
>> Cc: Dave Hansen <dave.hansen@linux.intel.com>
>> Cc: x86@kernel.org
>> Cc: "H. Peter Anvin" <hpa@zytor.com>
>> Cc: Daniel Sneddon <daniel.sneddon@linux.intel.com>
>> Cc: Arnd Bergmann <arnd@arndb.de>
>> Cc: Brian Gerst <brgerst@gmail.com>
>> Cc: Josh Poimboeuf <jpoimboe@kernel.org>
>> Cc: Pawan Gupta <pawan.kumar.gupta@linux.intel.com>
>> Cc: Peter Collingbourne <pcc@google.com>
> 
> Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> 
> Thanks

I can confirm that the error was fixed after applying the patch, in the same build environment.

Tested-by: Mirsad Todorovac <mtodorovac69@gmail.com>

However, why memcpy() directly from struct pt_regs doesn't work is beyond my understanding :-/

FWIW, bulk memcpy() might be replaced by a single assembler instruction? Or am I thinking still
in 6502 mode? :-)

Best regards,
Mirsad Todorovac

> -- 
> Gustavo
> 
>> ---
>>   arch/x86/include/asm/syscall.h | 7 ++++++-
>>   1 file changed, 6 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/x86/include/asm/syscall.h b/arch/x86/include/asm/syscall.h
>> index 2fc7bc3863ff..7c488ff0c764 100644
>> --- a/arch/x86/include/asm/syscall.h
>> +++ b/arch/x86/include/asm/syscall.h
>> @@ -82,7 +82,12 @@ static inline void syscall_get_arguments(struct task_struct *task,
>>                        struct pt_regs *regs,
>>                        unsigned long *args)
>>   {
>> -    memcpy(args, &regs->bx, 6 * sizeof(args[0]));
>> +    args[0] = regs->bx;
>> +    args[1] = regs->cx;
>> +    args[2] = regs->dx;
>> +    args[3] = regs->si;
>> +    args[4] = regs->di;
>> +    args[5] = regs->bp;
>>   }
>>     static inline int syscall_get_arch(struct task_struct *task)

  reply	other threads:[~2024-07-09 18:20 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-08 20:22 [PATCH] x86/syscall: Avoid memcpy() for ia32 syscall_get_arguments() Kees Cook
2024-07-08 23:44 ` Gustavo A. R. Silva
2024-07-09 18:20   ` Mirsad Todorovac [this message]
2024-07-09 18:37     ` Gustavo A. R. Silva
2024-07-11 21:01 ` Dave Hansen
2024-08-23  0:12   ` Kees Cook
2024-07-11 21:44 ` Peter Zijlstra
2024-07-11 23:10   ` Kees Cook
2024-07-12  9:00     ` Peter Zijlstra
2024-07-12 17:55       ` Kees Cook
2024-07-15  8:37         ` Peter Zijlstra
2024-07-15 17:01           ` Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=39b94091-d452-4dac-9012-ae43024462cd@gmail.com \
    --to=mtodorovac69@gmail.com \
    --cc=arnd@arndb.de \
    --cc=bp@alien8.de \
    --cc=brgerst@gmail.com \
    --cc=daniel.sneddon@linux.intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=gustavo@embeddedor.com \
    --cc=hpa@zytor.com \
    --cc=jpoimboe@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pawan.kumar.gupta@linux.intel.com \
    --cc=pcc@google.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox