Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7733956imu; Thu, 15 Nov 2018 00:15:28 -0800 (PST) X-Google-Smtp-Source: AJdET5cB3hd+QxGFjlgjbihWdoaf0aSIr4fmA1KDAGiJUvnLL2+W7ASWYnrOgPBDBWNP7VEdK8jZ X-Received: by 2002:a65:514c:: with SMTP id g12mr4849695pgq.169.1542269728595; Thu, 15 Nov 2018 00:15:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542269728; cv=none; d=google.com; s=arc-20160816; b=X4aNwfr220PTNAVN1xE5T3Me3ezz8YzHHtQ1egGsy+wECkWwZzdAPCT3kAnbv2xaZA bs/v/kdGL/Erq6zSIeWtl02JZ2S8ytryDEZ6iKpF86zLMge/nF4kPAVU1X9zo66FDFf+ hm9WWkI3DWqfmXXX5VRXpMv1g1+2pqgWe77OPFgX8G8+NBeXzmqvjILor0rnd4jQkmi2 muAoXW28MVvXSlKzbR3O2G23+ORcTcJUifX+NMLvfRtreYRiGQVQBbF/dHJVA2mcFkiU eegMOLlCfUEXqOGuBeZ9u9hXq4jd2edqPo543mVwn7+T13NS2bCKEJBQdSTcueJF+4SF 7pdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version; bh=7Y/YYCQ5YtJXszyF1A2o32FlHv2Tk3YOK5ss2MF05vs=; b=Io8UD+05xGqW6+u51H9EvfJGyilDFG+9tAoGMEjviCb0AEal9wJ0xsky052jbOmjbQ ATBWrV+X9GAs5cTdhSfLapCJIbUmEMruw8MtWyYYIHhZ3kPAMT+Sf2DgmGKftNrFbLWY TUlRaPSYHUPj66JcqQGn0JdSJA+kVThjFOlaSchXJxMNaXSHJIEXaRfcvEr47k5NtADo Z1RouQ3djWHH6nL/4R18gIneyPulS0+/qMh2NmRymaNxwa+5gdw8JVvEdqwEuEVsGEXh dtQKNP5v7mRfrXKQG1Tjp4x+wKs3PEedCS2YyjEvf49xl1+tQNHwn033zwLuqgaranuY 1r0A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f1-v6si26478704pld.395.2018.11.15.00.15.13; Thu, 15 Nov 2018 00:15:28 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728812AbeKOSVX (ORCPT + 99 others); Thu, 15 Nov 2018 13:21:23 -0500 Received: from mail-qk1-f194.google.com ([209.85.222.194]:35659 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728634AbeKOSVX (ORCPT ); Thu, 15 Nov 2018 13:21:23 -0500 Received: by mail-qk1-f194.google.com with SMTP id w204so30525783qka.2 for ; Thu, 15 Nov 2018 00:14:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=7Y/YYCQ5YtJXszyF1A2o32FlHv2Tk3YOK5ss2MF05vs=; b=pOU7/exFS62I+rrKuC4BID8NKv3DFEonRAwZY+/6TAv9juMt+YN0CJFpDdMyjQIzBz ZEP3wqSyu6RV657TreKWk8DhEN+THF1CrV4EsUpqLpqZUQy3cOrhVqKl2Y0fk7R9G2hd zI/bS4lkAJwoA7Qae7gl6JSBQjFFhzelWqMEHFZZPx7OL9olgSFFpVV/ANIldc1SPzJT n3fZNnLUPNKGUFXepMOEkFOdv1i6FL0FDqvwmxMttY+DABz1sDIAK8dzyJZUaslFhHov WYGQaVznVMtRq25gSmbVcYWfieNCxKHQXpJkjDGTB8W1m6Csc2QdacPbRfj2LPbszTFu k+aA== X-Gm-Message-State: AGRZ1gLB+S8emkfWFmYAoFGM2MCGJzdAqthR4CJPFLrMRSpcxm2RZ6v3 6rcA56t5pxVR4Zg2Fjva+8RESETj0AgVemZ1qCbHDg== X-Received: by 2002:a37:b2c2:: with SMTP id b185mr5022460qkf.194.1542269674041; Thu, 15 Nov 2018 00:14:34 -0800 (PST) MIME-Version: 1.0 References: <20181114215509.163600-1-ebiggers@kernel.org> <20181114230046.GC87768@gmail.com> In-Reply-To: From: Benjamin Tissoires Date: Thu, 15 Nov 2018 09:14:22 +0100 Message-ID: Subject: Re: [PATCH v2] HID: uhid: forbid UHID_CREATE under KERNEL_DS or elevated privileges To: Dmitry Torokhov Cc: ebiggers@kernel.org, jannh@google.com, dh.herrmann@googlemail.com, Jiri Kosina , "open list:HID CORE LAYER" , lkml , syzkaller-bugs@googlegroups.com, dvyukov@google.com, syzbot+72473edc9bf4eb1c6556@syzkaller.appspotmail.com, "3.8+" , luto@kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 15, 2018 at 12:20 AM Dmitry Torokhov wrote: > > On Wed, Nov 14, 2018 at 3:00 PM Eric Biggers wrote: > > > > Hi Dmitry, > > > > On Wed, Nov 14, 2018 at 02:28:56PM -0800, 'Dmitry Torokhov' via syzkaller-bugs wrote: > > > On Wed, Nov 14, 2018 at 2:05 PM Jann Horn wrote: > > > > > > > > On Wed, Nov 14, 2018 at 10:55 PM Eric Biggers wrote: > > > > > > > > > > From: Eric Biggers > > > > > > > > > > When a UHID_CREATE command is written to the uhid char device, a > > > > > copy_from_user() is done from a user pointer embedded in the command. > > > > > When the address limit is KERNEL_DS, e.g. as is the case during > > > > > sys_sendfile(), this can read from kernel memory. Alternatively, > > > > > information can be leaked from a setuid binary that is tricked to write > > > > > to the file descriptor. Therefore, forbid UHID_CREATE in these cases. > > > > > > > > > > No other commands in uhid_char_write() are affected by this bug and > > > > > UHID_CREATE is marked as "obsolete", so apply the restriction to > > > > > UHID_CREATE only rather than to uhid_char_write() entirely. > > > > > > > > > > Thanks to Dmitry Vyukov for adding uhid definitions to syzkaller and to > > > > > Jann Horn for commit 9da3f2b740544 ("x86/fault: BUG() when uaccess > > > > > helpers fault on kernel addresses"), allowing this bug to be found. > > > > > > > > > > Reported-by: syzbot+72473edc9bf4eb1c6556@syzkaller.appspotmail.com > > > > > Fixes: d365c6cfd337 ("HID: uhid: add UHID_CREATE and UHID_DESTROY events") > > > > > Cc: # v3.6+ > > > > > Cc: Jann Horn > > > > > Cc: Andy Lutomirski > > > > > Signed-off-by: Eric Biggers > > > > > > > > Reviewed-by: Jann Horn > > > > > > > > > --- > > > > > drivers/hid/uhid.c | 12 ++++++++++++ > > > > > 1 file changed, 12 insertions(+) > > > > > > > > > > diff --git a/drivers/hid/uhid.c b/drivers/hid/uhid.c > > > > > index 3c55073136064..051639c09f728 100644 > > > > > --- a/drivers/hid/uhid.c > > > > > +++ b/drivers/hid/uhid.c > > > > > @@ -12,6 +12,7 @@ > > > > > > > > > > #include > > > > > #include > > > > > +#include > > > > > #include > > > > > #include > > > > > #include > > > > > @@ -722,6 +723,17 @@ static ssize_t uhid_char_write(struct file *file, const char __user *buffer, > > > > > > > > > > switch (uhid->input_buf.type) { > > > > > case UHID_CREATE: > > > > > + /* > > > > > + * 'struct uhid_create_req' contains a __user pointer which is > > > > > + * copied from, so it's unsafe to allow this with elevated > > > > > + * privileges (e.g. from a setuid binary) or via kernel_write(). > > > > > + */ > > > > > > uhid is a privileged interface so we would go from root to less > > > privileged (if at all). If non-privileged process can open uhid it can > > > construct virtual keyboard and inject whatever keystrokes it wants. > > > > > > Also, instead of disallowing access, can we ensure that we switch back > > > to USER_DS before trying to load data from the user pointer? > > > > > > > Actually uhid doesn't have any capability checks, so it's up to userspace to > > assign permissions to the device node. > > Yes. There are quite a few such instances where kernel does not bother > implementing superfluous checks and instead relies on userspace to > provide sane environment. IIRC nobody in the kernel enforces root > filesystem not be accessible to ordinary users, it is done with > generic permission checks. > > > I think it's best not to make > > assumptions about how the interface will be used and to be consistent with how > > other ->write() methods in the kernel handle the misfeature where a __user > > pointer in the write() or read() payload is dereferenced. > > I can see that you might want to check credentials, etc, if interface > can be accessed by unprivileged process, however is it a big no no for > uhid/userio/uinput devices. Yep, any sane distribution would restrict the permissions of uhid/userio/uinput to only be accessed by root. If that ever changes, there is already an issue with the system and it was compromised either by a terribly dizzy sysadmin. > > > Temporarily switching > > to USER_DS would only avoid one of the two problems. > > So because of the above there is only one problem. If your system > opened access to uhid to random processes you have much bigger > problems than exposing some data from a suid binary. You can spam "rm > -rf .; rm -rf /" though uhid if there is interactive session > somewhere. > > > > > Do you think the proposed restrictions would actually break anything? > > It would break if someone uses UHID_CREATE with sendpage. I do not > know if anyone does. If we were certain there are no users we'd simply > removed UHID_CREATE altogether. AFAICT, there are 2 users of uhid: - bluez for BLE devices (using HID over GATT) - hid-replay for debugging. There might be a few other users that are making some user space driver out of opencv, but I wouldn't expect those to be really widespread. IIRC, bluez uses UHID_CREATE2 and hid-replay should also (or ought to be, but this can be easily fixed as I maintain the code and I am the almost sole user). Regarding the sendpage fix, doesn't David's patch fixes the issue already (https://patchwork.kernel.org/patch/10682549/). I am fine applying whatever patch that fixes the security issues, as long as it doesn't break bluez nor the hid-replay uses I have for debugging and regression testing. Cheers, Benjamin > > > > > - Eric > > > > > > > + if (file->f_cred != current_cred() || uaccess_kernel()) { > > > > > + pr_err_once("UHID_CREATE from different security context by process %d (%s), this is not allowed.\n", > > > > > + task_tgid_vnr(current), current->comm); > > > > > + ret = -EACCES; > > > > > + goto unlock; > > > > > + } > > > > > ret = uhid_dev_create(uhid, &uhid->input_buf); > > > > > break; > > > > > case UHID_CREATE2: > > > > > -- > > Thanks. > > -- > Dmitry