linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Andy Lutomirski <luto@amacapital.net>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Ingo Molnar <mingo@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Andrew Lutomirski <luto@kernel.org>,
	Borislav Petkov <bp@alien8.de>,
	linux-kernel@vger.kernel.org, oleg@redhat.com,
	Kees Cook <keescook@chromium.org>, Will Drewry <wad@chromium.org>
Subject: Re: pt_regs->ax == -ENOSYS
Date: Tue, 27 Apr 2021 15:58:03 -0700	[thread overview]
Message-ID: <06a5e088-b0e6-c65e-73e6-edc740aa4256@zytor.com> (raw)
In-Reply-To: <F9F5E9D4-C1EE-455A-A6B1-4DF9D349BBAA@amacapital.net>

On 4/27/21 2:28 PM, Andy Lutomirski wrote:
> 
>> On Apr 27, 2021, at 2:15 PM, H. Peter Anvin <hpa@zytor.com> wrote:
>>
>> Trying to stomp out some possible cargo cult programming?
>>
>> In the process of going through the various entry code paths, I have to admit to being a bit confused why pt_regs->ax is set to -ENOSYS very early in the system call path.
>>
> 
> It has to get set to _something_, and copying orig_ax seems perhaps silly.  There could also be code that relies on ptrace poking -1 into the nr resulting in -ENOSYS.
> 

Yeah. I obviously ran into this working on the common entry-exit code 
for FRED; the frame has annoyingly different formats because of this, 
and I wanted to avoid slowing down the system call path.

>> What is perhaps even more confusing is:
>>
>> __visible noinstr void do_syscall_64(struct pt_regs *regs, unsigned long nr)
>> {
>>         nr = syscall_enter_from_user_mode(regs, nr);
>>
>>         instrumentation_begin();
>>         if (likely(nr < NR_syscalls)) {
>>                 nr = array_index_nospec(nr, NR_syscalls);
>>                 regs->ax = sys_call_table[nr](regs);
>> #ifdef CONFIG_X86_X32_ABI
>>         } else if (likely((nr & __X32_SYSCALL_BIT) &&
>>                           (nr & ~__X32_SYSCALL_BIT) < X32_NR_syscalls)) {
>>                 nr = array_index_nospec(nr & ~__X32_SYSCALL_BIT,
>>                                         X32_NR_syscalls);
>>                 regs->ax = x32_sys_call_table[nr](regs);
>> #endif
>>         }
>>         instrumentation_end();
>>         syscall_exit_to_user_mode(regs);
>> }
>> #endif
>>
>> Now, unless I'm completely out to sea, it seems to me that if syscall_enter_from_user_mode() changes the system call number to an invalid number and pt_regs->ax to !-ENOSYS then the system call will return a different value(!) depending on if it is out of range for the table (whatever was poked into pt_regs->ax) or if it corresponds to a hole in the table. This seems to me at least to be The Wrong Thing.
> 
> I think you’re right.
> 
>>
>> Calling regs->ax = sys_ni_syscall() in an else clause would arguably be the right thing here, except possibly in the case where nr (or (int)nr, see below) == -1 or < 0.
> 
> I think the check should be -1 for 64 bit but (u32)nr == (u32)-1 for the 32-bit path. Does that seem reasonable?

I'm thinking overall that depending on 64-bit %rax is once again a 
mistake; I realize that the assembly code that did that kept breaking 
because people messed with it, but we still have:

/*
  * Only the low 32 bits of orig_ax are meaningful, so we return int.
  * This importantly ignores the high bits on 64-bit, so comparisons
  * sign-extend the low 32 bits.
  */
static inline int syscall_get_nr(struct task_struct *task, struct 
pt_regs *regs)
{
         return regs->orig_ax;
}

"Different interpretation of the same data" is a notorious security 
trap. Zero-extending orig_ax would cause different behavior on 32 and 64 
bits and differ from the above, so I'm thinking that just once and for 
all defining the system call number as a signed int for all the x86 ABIs 
would be the sanest.

It still doesn't really answer the question if "movq $-1,%rax; syscall" 
or "movl $-1,%eax; syscall" could somehow cause bad things to happen, 
though, which makes me a little bit nervous still.

	-hpa

  reply	other threads:[~2021-04-27 22:58 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-27 21:15 pt_regs->ax == -ENOSYS H. Peter Anvin
2021-04-27 21:28 ` Andy Lutomirski
2021-04-27 22:58   ` H. Peter Anvin [this message]
2021-04-27 23:23     ` Andy Lutomirski
2021-04-28  0:05       ` H. Peter Anvin
2021-04-28  0:11         ` Andy Lutomirski
2021-04-28  0:20           ` H. Peter Anvin
2021-04-28  0:46             ` H. Peter Anvin
2021-04-27 23:29     ` Kees Cook
2021-04-27 23:51       ` Andy Lutomirski
2021-04-28  2:05         ` Kees Cook
2021-04-28  2:07           ` H. Peter Anvin

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=06a5e088-b0e6-c65e-73e6-edc740aa4256@zytor.com \
    --to=hpa@zytor.com \
    --cc=bp@alien8.de \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=luto@kernel.org \
    --cc=mingo@kernel.org \
    --cc=oleg@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=wad@chromium.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;
as well as URLs for NNTP newsgroup(s).