linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* pt_regs->ax == -ENOSYS
@ 2021-04-27 21:15 H. Peter Anvin
  2021-04-27 21:28 ` Andy Lutomirski
  0 siblings, 1 reply; 12+ messages in thread
From: H. Peter Anvin @ 2021-04-27 21:15 UTC (permalink / raw)
  To: Linus Torvalds, Ingo Molnar, Thomas Gleixner, Andrew Lutomirski,
	Borislav Petkov, linux-kernel, oleg, Kees Cook, Will Drewry

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.

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.

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.

Now, syscall_get_nr() returns the low 32 bits of the system call number 
unconditionally. There are places where we look at the sign of this 
number, which means that 0xffffffff7fffffff is "positive" and 
0x7fffffffffffffff is "negative". We have gone back and forth more than 
once on if we should look at %rax or just %eax on a system call... I 
have to admit that the current design makes me a bit nervous.

Finally, can anything bad happen in some weird corner case inside one of 
the syscall_*_mode() calls or after an interrupt if someone tries to 
call syscall(-1) or another negative number?

Food for thought or just my not being up to date?

Thanks,

	-hpa


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2021-04-28  2:08 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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).