Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4950261pxy; Tue, 27 Apr 2021 16:52:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzmkOryzzzJzECjRV2dEmvB0TKyR4l0plYRrowfkFJ00nQ00DgoAMa9N6Oa3wfxbkl6QkDD X-Received: by 2002:a17:907:33ce:: with SMTP id zk14mr12789361ejb.372.1619567548840; Tue, 27 Apr 2021 16:52:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619567548; cv=none; d=google.com; s=arc-20160816; b=ta+zedHSnApQ+HvIE3InrzyjjBqKG/dyEs7BoYxa0ZQz2otdqmlphg5EXf5uQ2dyj5 /e9cMGvpxUuwXAIpAVWKsckG36YzWq1Sc4R2Uf5DfwE4Jd6XkSsWKOueyInweVgtB8wy ZxTc9hVb43HHCvqr7nICOrYpiz28Cx2pC5t2Y3gZ+fiwSxvctERzzQyU77cpdS7Gsbdb ihf/a/SYyytLc2QV1MvtW/gwe9RxmAvigwu4tZcFKRN3aAS8FL1e1qUef3jgPiH/NXuu j4uparNnGNFvwc0RXmN11+KxzgM2kOaBhVW93Fc6Erg0bztyEiRrnF+6PrWweGSpsEEU zAWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:in-reply-to:cc:references:message-id:date :subject:mime-version:from:content-transfer-encoding:dkim-signature; bh=LWGe6SWA/bYZp/RAafLEiICJuTiPklY+BiBhvpohOys=; b=XEJSrKS1P/7sCkllDlzvbQnC4uuCCqCE/EqjyzxtOWij6W3U8y7HyW/kxYgPrxKgvX Ax14kF46W+UdgCi4HRkGqhGLxUNNJ3pSghDnv0CjXHaNDDjpP+hvgpoNTZjoSywRICzH kyGjZl1DQ7V77380eZb1+nBUnaQiB54Ee2NhGuCaxLip2v4FJJEME3Vjd/DciY4+bLRk gpgqdPJGscWDYStyfkUb0tMeNFBh/b3SSGMbxsNgSnAXR72yNHrBe5DmvKybQzWaf1Kr YKRGH0AJXjvJAJ1RKRYlxMqY5tZkPkNg4LEx55Fjqlu8379JeWrf018ygbrwEuyndRXg WI/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amacapital-net.20150623.gappssmtp.com header.s=20150623 header.b=ysBTlZpK; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h21si4065865ede.377.2021.04.27.16.52.05; Tue, 27 Apr 2021 16:52:28 -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=@amacapital-net.20150623.gappssmtp.com header.s=20150623 header.b=ysBTlZpK; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239429AbhD0Xvx (ORCPT + 99 others); Tue, 27 Apr 2021 19:51:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236015AbhD0Xvw (ORCPT ); Tue, 27 Apr 2021 19:51:52 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9E82C061574 for ; Tue, 27 Apr 2021 16:51:08 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id f11-20020a17090a638bb02901524d3a3d48so8010707pjj.3 for ; Tue, 27 Apr 2021 16:51:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amacapital-net.20150623.gappssmtp.com; s=20150623; h=content-transfer-encoding:from:mime-version:subject:date:message-id :references:cc:in-reply-to:to; bh=LWGe6SWA/bYZp/RAafLEiICJuTiPklY+BiBhvpohOys=; b=ysBTlZpKznZhTsHY4k3PrOcMe54kYcGklldgqwyfe6h7T6SDC8DqnRXaBG7jYNhWxS qLAKh+Ru8KQFmekqNOGzrifeQAsejkI0A+kegRMACOKX71xqe5S8KBXD9e83nxRk9iPL 8+2CSvXfXj6s/ChK+lKbPjKsMIAhNFmHSHiCneOrTRjocAPw2QrgdkGoO1F3zqOUpU22 5BI9jsz2zUXv2WnMnMtMEqV9/Yqc5MJGbnGHKgoECUDpNc+BetB5MW8+Zm1JpILklv7H cfL5qPiftKJMvN4q2amY3y4g6iBoa04flBytYxI2Labh2klH/+xWIR1mH346IccknLxe w7WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:content-transfer-encoding:from:mime-version :subject:date:message-id:references:cc:in-reply-to:to; bh=LWGe6SWA/bYZp/RAafLEiICJuTiPklY+BiBhvpohOys=; b=kIL2qQASoNg7Yw2SaMmcJyL3E12qu2w0UmraRviMmhUl3LgSGvO5/eal7tqPBfJSi7 IovQUc/ZQpPnxqEQvFfFPoZzArwfroQ4M3uZkquGictjk7pGcS9dWi7/Zoqh0OdZqwwe L+TawerX1PYMQX8d5Pfc+BDn4WPOalhYHvibl/FYZ/eyFf4RPPOL8BzacPmNj/SC28XF 6HKpYRUIcKcfBzF5Qyws+c21byVuF0sX0jjkJya67Q3zdxR8sZjHwaSQDzaG7BOix0T/ NGT1zQLhu2iRxgbWXjvvJY7qt8lOVhn+YOLzQiKF77R1HMgzOS6n3Zb0WrR2qf85NgeA kEIw== X-Gm-Message-State: AOAM531QYLwCsPQRP2CFMAGiZF0L2bHAOYNjk6t4EAorbJFRZ8EtQqN7 WTftKdjkjJpIHJELoc9M7bdSbg== X-Received: by 2002:a17:90b:1e10:: with SMTP id pg16mr893738pjb.30.1619567468235; Tue, 27 Apr 2021 16:51:08 -0700 (PDT) Received: from smtpclient.apple (2603-8001-6500-2faf-d537-2818-24ab-4c1c.res6.spectrum.com. [2603:8001:6500:2faf:d537:2818:24ab:4c1c]) by smtp.gmail.com with ESMTPSA id x90sm3176223pjj.55.2021.04.27.16.51.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 27 Apr 2021 16:51:07 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Andy Lutomirski Mime-Version: 1.0 (1.0) Subject: Re: pt_regs->ax == -ENOSYS Date: Tue, 27 Apr 2021 16:51:06 -0700 Message-Id: <78327AF2-575F-415E-98D4-6ECDFE311D0F@amacapital.net> References: <202104271619.0DBE456@keescook> Cc: "H. Peter Anvin" , Linus Torvalds , Ingo Molnar , Thomas Gleixner , Andrew Lutomirski , Borislav Petkov , linux-kernel@vger.kernel.org, oleg@redhat.com, Will Drewry In-Reply-To: <202104271619.0DBE456@keescook> To: Kees Cook X-Mailer: iPhone Mail (18E199) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On Apr 27, 2021, at 4:29 PM, Kees Cook wrote: >=20 > =EF=BB=BFOn Tue, Apr 27, 2021 at 03:58:03PM -0700, H. Peter Anvin wrote: >>> On 4/27/21 2:28 PM, Andy Lutomirski wrote: >>>=20 >>>> On Apr 27, 2021, at 2:15 PM, H. Peter Anvin wrote: >>>>=20 >>>> =EF=BB=BFTrying to stomp out some possible cargo cult programming? >>>>=20 >>>> 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 i= n the system call path. >>>>=20 >>>=20 >>> It has to get set to _something_, and copying orig_ax seems perhaps sill= y. There could also be code that relies on ptrace poking -1 into the nr res= ulting in -ENOSYS. >>>=20 >>=20 >> 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. >>=20 >>>> What is perhaps even more confusing is: >>>>=20 >>>> __visible noinstr void do_syscall_64(struct pt_regs *regs, unsigned lon= g nr) >>>> { >>>> nr =3D syscall_enter_from_user_mode(regs, nr); >>>>=20 >>>> instrumentation_begin(); >>>> if (likely(nr < NR_syscalls)) { >>>> nr =3D array_index_nospec(nr, NR_syscalls); >>>> regs->ax =3D 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 =3D array_index_nospec(nr & ~__X32_SYSCALL_BIT, >>>> X32_NR_syscalls); >>>> regs->ax =3D x32_sys_call_table[nr](regs); >>>> #endif >>>> } >>>> instrumentation_end(); >>>> syscall_exit_to_user_mode(regs); >>>> } >>>> #endif >>>>=20 >>>> Now, unless I'm completely out to sea, it seems to me that if syscall_e= nter_from_user_mode() changes the system call number to an invalid number an= d 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 int= o pt_regs->ax) or if it corresponds to a hole in the table. This seems to me= at least to be The Wrong Thing. >>>=20 >>> I think you=E2=80=99re right. >>>=20 >>>>=20 >>>> Calling regs->ax =3D sys_ni_syscall() in an else clause would arguably b= e the right thing here, except possibly in the case where nr (or (int)nr, se= e below) =3D=3D -1 or < 0. >>>=20 >>> I think the check should be -1 for 64 bit but (u32)nr =3D=3D (u32)-1 for= the 32-bit path. Does that seem reasonable? >=20 > FWIW, there is some confusion with how syscall_trac_enter() signals the > "skip syscall" condition (-1L), vs actually calling "syscall -1". Fortunately there is not, and never will be, a syscall -1. But I agree that= calling max syscall + 1 should behave identically to calling a nonexistent s= yscall in the middle of the table.=