Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1639176ybt; Thu, 9 Jul 2020 11:38:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCMxooDIPrbXnjW68Njak0470+PTP13D9PAquPo/JBNdGP3QYxr7pGCSfyi7mhtzOPjzex X-Received: by 2002:aa7:c5d4:: with SMTP id h20mr71996989eds.115.1594319881522; Thu, 09 Jul 2020 11:38:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594319881; cv=none; d=google.com; s=arc-20160816; b=gaj9Bb5HSgJJUy+C6yj60+cQfcwHfv76IgkMluERSsNamZW+yMptSgHQ/455cdIRij w30mAzgRw1ArUgBmSPWBTE3h58K5p8sCbgC2OtHgvDiuYt+NFkDwLIkhcwqm5l77CAvW RLurqJSN3QDLiZklzZPlBTSQ6oVgNVlRTez5ADkGUV9XQ8p2Sui5aXdpG5jbNy9E43Mu 8fEBWTg5YNXdwpE/EmGZdLm/nxj5i2W2wMV9sQoAhCBh2iaYAO0OJllIPu9s5hzFSRn9 DXbK+WR+/iKTDBzS0JbyUqVKajmvbkqqlwdUOoplWSFULiC5gby5wbNzfIq+5ARk+KbT n8Og== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:message-id :in-reply-to:date:references:organization:subject:cc:to:from; bh=dSLZMr6ilDEI4K+oi77kuw8SEJespjzINg6vWErGTQE=; b=YVVVFjDoAaN7reWwFpQP5+sTfSKMSqSloLMRSyyCso6ZsqahPotVk/sjeexAd6leZI /C7oznFx/vGAX+OzqK5IbpK4QcrCakUNGRgcbcNRqWwpwWpeKzG/7GYNSBpICwfwRQQI iEenH+2QF+PPjT/Su7txHEhxIy9NwM2fShvWgI6AG5I0FajLffRCzU8weIeCGji13ld0 +HIe4A91A7sxogqSIDgcTOzwd+b3rgte5Ld4Bm0qlkrdVWSz2hW6MJRb20/LANLxdLnx onZ/RyiyUcP2NA1Bb7YoveLLZfyq7J9gqpdrk8OZGbBR2b4IkrOTz8ZV01JE5S/LPoYi IMRA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n10si2499535ejk.580.2020.07.09.11.37.39; Thu, 09 Jul 2020 11:38:01 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726275AbgGISg6 (ORCPT + 99 others); Thu, 9 Jul 2020 14:36:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726163AbgGISg6 (ORCPT ); Thu, 9 Jul 2020 14:36:58 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D28C3C08C5CE for ; Thu, 9 Jul 2020 11:36:57 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: krisman) with ESMTPSA id 16DD426D948 From: Gabriel Krisman Bertazi To: Matthew Wilcox Cc: tglx@linutronix.de, linux-kernel@vger.kernel.org, kernel@collabora.com, Andy Lutomirski , Paul Gofman , Kees Cook Subject: Re: [PATCH v2] kernel: Implement selective syscall userspace redirection Organization: Collabora References: <20200709043840.4189530-1-krisman@collabora.com> <20200709114706.GB12769@casper.infradead.org> Date: Thu, 09 Jul 2020 14:36:52 -0400 In-Reply-To: <20200709114706.GB12769@casper.infradead.org> (Matthew Wilcox's message of "Thu, 9 Jul 2020 12:47:06 +0100") Message-ID: <87a7087cxn.fsf@collabora.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Matthew Wilcox writes: > On Thu, Jul 09, 2020 at 12:38:40AM -0400, Gabriel Krisman Bertazi wrote: >> The proposed interface looks like this: >> >> prctl(PR_SET_SYSCALL_USER_DISPATCH, , , [selector]) >> >> Dispatcher is the address of a syscall instruction that is allowed to >> by-pass the blockage, such that in fast paths you don't need to disable >> the trap nor check the selector. This is essential to return from >> SIGSYS to a blocked area without triggering another SIGSYS from the >> rt_sigreturn. > > Should be a single pointer or should the interface specify > a range from which syscalls may be made without being redirected? eg, > one could specify the whole of libc. > > prctl(PR_SET_SYSCALL_USER_DISPATCH, , , , > [selector]) I liked this suggestion a lot, since user can just pass a single address to get the original interface, but it still let us not pay the cost of __get_user on more paths. I will add it to v3. > >> +++ b/include/linux/syscall_user_dispatch.h >> @@ -0,0 +1,45 @@ >> +/* SPDX-License-Identifier: GPL-2.0 */ >> +#ifndef _SYSCALL_USER_DISPATCH_H >> +#define _SYSCALL_USER_DISPATCH_H >> + >> +struct task_struct; >> +static void clear_tsk_thread_flag(struct task_struct *tsk, int flag); >> + >> +#ifdef CONFIG_SYSCALL_USER_DISPATCH >> +struct syscall_user_dispatch { >> + int __user *selector; >> + unsigned long __user dispatcher; > > The __user annotation is on the pointer, not the value. ie, it's > > unsigned long foo; > unsigned long __user *p; > > get_user(foo, p) > >> +++ b/include/uapi/asm-generic/siginfo.h >> @@ -285,6 +285,7 @@ typedef struct siginfo { >> */ >> #define SYS_SECCOMP 1 /* seccomp triggered */ >> #define NSIGSYS 1 >> +#define SYS_USER_REDIRECT 2 > > I'd suggest that SYS_USER_REDIRECT should be moved up by one line. > >> +int set_syscall_user_dispatch(int mode, unsigned long __user dispatcher, >> + int __user *selector) >> +{ >> + switch (mode) { >> + case PR_SYSCALL_DISPATCH_DISABLE: >> + if (dispatcher || selector) >> + return -EINVAL; >> + break; >> + case PR_SYSCALL_DISPATCH_ENABLE: >> + break; >> + default: >> + return -EINVAL; >> + } >> + >> + if (selector) { >> + if (!access_ok(selector, sizeof(int))) >> + return -EFAULT; >> + } > > You're not enforcing the alignment requirement here. > >> + spin_lock_irq(¤t->sighand->siglock); >> + >> + current->syscall_dispatch.selector = selector; >> + current->syscall_dispatch.dispatcher = dispatcher; >> + >> + /* make sure fastlock is committed before setting the flag. */ > > fastlock? ;-) Gee, keeping variable renames updated on comments is hard, compiler won't catch those. :) > I don't think you actually need this. You're setting per-thread state on > yourself, so what's the race that you're concerned about? Good point. I was assuming this would be modifiable from under it, but it is not the case. > >> + smp_mb__before_atomic(); >> + >> + if (mode == PR_SYSCALL_DISPATCH_ENABLE) >> + set_tsk_thread_flag(current, TIF_SYSCALL_USER_DISPATCH); >> + else >> + clear_tsk_thread_flag(current, TIF_SYSCALL_USER_DISPATCH); >> + >> + spin_unlock_irq(¤t->sighand->siglock); >> + >> + return 0; >> +} >> -- >> 2.27.0 >> -- Gabriel Krisman Bertazi