Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4854007pxy; Tue, 27 Apr 2021 14:18:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx5RbbLpNfRd6nXVglPH90vcOb2hMAy5IoDFlOebRSXjwZQCawmbiZcyBOWIXEeR9IPgbPv X-Received: by 2002:a17:907:3e27:: with SMTP id hp39mr26085569ejc.373.1619558330811; Tue, 27 Apr 2021 14:18:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619558330; cv=none; d=google.com; s=arc-20160816; b=iXrBSWRaYe9AGAD5C546592CXRwfvM/wbuIfpmPBUPmttTmQErhBYNq21t8CgcCM6W Sh2bBaSsN535MKx8pB0qz9BZQ1t3gFqZ7QrFc2vfSxt2Kmm358IQmZyRrv2nUZQmD8hN /hhXhVmBQxl/Vtd+wacTrCkRS0dsBRTqhLuBfdla90lcwU91uyEaw9o6AXTlk9lVEXPG 7zSf8tAWwP1HLlFErqNEE4v1mGmJbeR1xJGWfBJmIs13adfpS3Gf0/4JVD1aDfGAYnh/ 5yqjB3xdogGasTdV8sqa1pWUNjyOmVhanC3A09zE0lS6Srco6RlNbcnQhsMJMDzPFgQa rghg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :mime-version:user-agent:date:message-id:subject:from:to :dkim-signature:dkim-filter; bh=OH6Ee28eLwng9NYWF9RLVZIFXvtSO6Pwr8F3RrF9GlY=; b=yPHzWmxqfXxReaKEXAB0Nn9nDIUVlSn0ihTaf0j4zS2TizhH3afBi6AGOYFjcsYWhL sGNYNDgBEj1kIjYWKvEtjY5K9V74+n7fsloZMFtuz7C9lps9NKws4Y+8KLHK+Fvnx8dB r/Cv58ax4wmzRK/pVADVHAtY5XS6p8IXk18LOBlGGSOE1jOi4Bi59fdwnRTcLruMzC77 /jeQfx3roL9e/9R5PHWwswfIr4nPNLbE/cLx5Gvk/m5J1f+ivy5BCD12Prr1RCsHaQ0O 1UVtGaWfdlO3fnWlHiC0zHP4+hOwJR9YxEJWz36PUrnvxVbOcmmPJdAT2evUi4EYdmn4 vt5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zytor.com header.s=2021032801 header.b=Uw7v2h3E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c95si3997798edf.106.2021.04.27.14.18.26; Tue, 27 Apr 2021 14:18:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@zytor.com header.s=2021032801 header.b=Uw7v2h3E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236965AbhD0VQ7 (ORCPT + 99 others); Tue, 27 Apr 2021 17:16:59 -0400 Received: from terminus.zytor.com ([198.137.202.136]:50945 "EHLO mail.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237057AbhD0VQ6 (ORCPT ); Tue, 27 Apr 2021 17:16:58 -0400 Received: from tazenda.hos.anvin.org ([IPv6:2601:646:8602:8be0:7285:c2ff:fefb:fd4]) (authenticated bits=0) by mail.zytor.com (8.16.1/8.15.2) with ESMTPSA id 13RLFkI1925360 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NO); Tue, 27 Apr 2021 14:15:46 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 13RLFkI1925360 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2021032801; t=1619558148; bh=OH6Ee28eLwng9NYWF9RLVZIFXvtSO6Pwr8F3RrF9GlY=; h=To:From:Subject:Date:From; b=Uw7v2h3EZN0hAByfXfQ8XoYHYhJJuNog0vbYT4rVplgAKQwOmnarEEgHAZfL0zH2W bW0ndvcZtdJ1T5ZSDX49A2zC4kIU3eVR0yE8+FIlh+EJCNcnRScOlJ/X4UFyOUq3U6 OJOncIDGsrNDDcOzzCpJA3vWsxYfjYzfMGlFuVlggyg6KJBu8jP/+VWTgQtOWDpMJ4 Z1TTdlMgOvA9ArNivwStWxkxacleOqqmCC/BIazPMGvqb9nXutfSgaCf6YbaeJ58hz xuabJOpU2mDs5ZQF9xCBaA443vuH6ny7xF1V3zq7ecBGGyhlEQLSFGKPpJqVuIljOF cnZwghlWsv2MQ== To: Linus Torvalds , Ingo Molnar , Thomas Gleixner , Andrew Lutomirski , Borislav Petkov , linux-kernel@vger.kernel.org, oleg@redhat.com, Kees Cook , Will Drewry From: "H. Peter Anvin" Subject: pt_regs->ax == -ENOSYS Message-ID: Date: Tue, 27 Apr 2021 14:15:41 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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