Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752875AbbKZJCo (ORCPT ); Thu, 26 Nov 2015 04:02:44 -0500 Received: from mail-bn1on0130.outbound.protection.outlook.com ([157.56.110.130]:44278 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751968AbbKZJCi (ORCPT ); Thu, 26 Nov 2015 04:02:38 -0500 Authentication-Results: spf=permerror (sender IP is 192.88.168.50) smtp.mailfrom=freescale.com; oracle.com; dkim=none (message not signed) header.d=none;oracle.com; dmarc=none action=none header.from=freescale.com; Date: Thu, 26 Nov 2015 16:59:25 +0800 From: Peter Chen To: Emilio =?iso-8859-1?Q?L=F3pez?= CC: , , , , , , , , , Subject: Re: [PATCH v1] usb: devio: Add ioctl to disallow detaching kernel USB drivers. Message-ID: <20151126085924.GD26397@shlinux2> References: <1448466334-21346-1-git-send-email-emilio.lopez@collabora.co.uk> <1448466334-21346-2-git-send-email-emilio.lopez@collabora.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1448466334-21346-2-git-send-email-emilio.lopez@collabora.co.uk> User-Agent: Mutt/1.5.21 (2010-09-15) X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1;BN1BFFO11FD028;1:TK/TzYSbst8ZFMN5V94gjsz6j6oPNttSr/XcgRqlzqSAe9e2lUmNmZNlMp6DCljzmTcys/dDl+/1wIrjDyPv44BTdl1GVK/RPFN6Cf7jyctQSG6mTIXXTffReej1BUxXq8B6Ci8g+q1gw9014U5wUeLoMP/d44RWny/7rn29PCaD65mK4pNnkoFPD1nt5V+06xPoebm2qncrlqFbhzoWDEcGD/iwU26+f36P5Un5lev53VH13PEJDx/OvNpEK7ZY+Yrfks/E47vpqh8GL/QJ9SL6rKPSMiJYeaEm8JfRwTVsRoX93A+ZDG646cMIo+Qa5PhrWcd53eoSYjQR0WpvVie2VyrOqJ7AOF0Ej8CiwVFhXZGi1nQdJhEiFYNxM8IMePgITI0I3ULJzX8RgKAFjziTbi8s3KmktetzRkZxCbs= X-Forefront-Antispam-Report: CIP:192.88.168.50;CTRY:US;IPV:NLI;EFV:NLI;SFV:NSPM;SFS:(10019020)(6009001)(2980300002)(448002)(199003)(189002)(24454002)(50986999)(92566002)(54356999)(2950100001)(106466001)(19580395003)(47776003)(85326001)(1220700001)(77096005)(86362001)(76176999)(97736004)(5001960100002)(189998001)(83506001)(23756003)(104016004)(33656002)(87936001)(5008740100001)(15975445007)(4001350100001)(19580405001)(81156007)(50466002)(6806005)(1076002)(586003)(33716001)(11100500001)(110136002)(1096002);DIR:OUT;SFP:1102;SCL:1;SRVR:BN3PR0301MB1218;H:tx30smr01.am.freescale.net;FPR:;SPF:PermError;PTR:InfoDomainNonexistent;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;BN3PR0301MB1218;2:JZlxd1YADzNZfl+YqNYQBdkV8ZHlcl2zoYgkrtUNw4bp8wcNx75DEb5JbUchwMEnSiPA1Jga1wauc61u2iGXFQ8jypi1ZghiM5dcUtQMId+CjZ9JZ9vwCMi16hGrF2fUuM8ejEdQBHIXIr7f2pyEfQ==;3:iEuAxzCLfE+dYvu50fYtepIrnGKENP9Fn8hGICVXlHbQv0Yw1NqjYSp1IcVR90gBK8S9+xT1cOvTdSo/J8HLQM8zGrTMRE96ytf+DSLetYfDtvzaR1ORjk801RQUzBy/brEyFO88ol7C+Qqml5PaWFUeB8yuUTeywFHjGubG2dEVzfMzI+1IyDSeVOxueqD3+igbyRhRtXsYt5QG1xZJ4Flz5abfTm/0v/5gxVMVg274fWjtHOOzlGOzRPeEjlGjsaIL9TLrNMIezAASSFJGEQ==;25:HO+BbNUaJkOylRAkZkYaJ5m4k30j+crB/uqbTf6eBY+Q2z/HVU6EDV9lYONUJvpQEfHlv1bmUAJDXKPR8LpaiBarMO5ev8cu9UBhredwKkcq/So0lCLJlmWvaDzFVao4Q0nlHArRH+NMrpMq1pHwgxbLgpm8CJQARGc8fELiUESQQiZhC2/f7sl/+gcp+ihP0vxiQul3Dcxph1b7LO+ZDuGczjSGZk47uZAq81Mssox8B++Wn5glA4fJqOCye8V836UxF81b8Haue/DNIrJukA== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(42134001)(42139001);SRVR:BN3PR0301MB1218; X-Microsoft-Exchange-Diagnostics: 1;BN3PR0301MB1218;20:/xrI+JHI6zpKsm3/RQ8W3VPGoLOXy1iM9IZ9epx80ZFKXUdInVjnfzqDzc5lqSkGOH9fS6fuI5wqZav3L/xzDG6hsVQXUZd9i7LcVVuuU01QInHCsKl3vs768yF49fJv3WKd+8HWSteWjOedWF/HTWn4r3heUtxruldOc7Y9uWJ+QS4z+tWsw8lDpbUAA8NUXKUOQ+YiXMPZrQd3SSOCfqS4DrcSNifpm10EV0YoIyHbkOhM2mo5exubqLj6Q6eIyV6hi4dGaZwvt/r6bGVH9Zr1Nd8f1y/8BIkoLB89t+8OYiFNboKvQKCjPqW/K5TpKQzxHK5ckClY3iF8ajDdVMGWqik3TvbywjTaDY6XrWg=;4:8xEeigPydMxT6O5uP36DsFIqgvL0uZ7B1pE5PtoevU3NGSmlFbuVeeZVltKtGC2iZjAUZZAju0cg35BfURdaf62ENsKinuZH3k4T1ju+K6glpqVHf2Z9l/2ZtQbs8ac7DrYftjwPu+ATUX2R6Ix6m8N2CE3hziBYoK/Rxz7jMXY6KMkJj0gGMBeLBIugvWoZwJmvAlHwpubePtBIzDki08nUNRi5w97Pab0QEHoaC8KB2ELLgkQehuXK/IzVQjjcHhdFhIz7rn3Kcg5snQmA2qcj8nIyWETFJ6m52bT/UdRwzZR4AFgIjygIBuNaqDetCuAD7Cgw48wyz3gRlhm79XosqPiJruqKosGGth6whZZW69Z7qNRQePJDUmN5xj72 X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(2401047)(5005006)(520078)(8121501046)(3002001)(10201501046);SRVR:BN3PR0301MB1218;BCL:0;PCL:0;RULEID:;SRVR:BN3PR0301MB1218; X-Forefront-PRVS: 0772E5DAD5 X-Microsoft-Exchange-Diagnostics: =?iso-8859-1?Q?1;BN3PR0301MB1218;23:WUW6gGCUS8crgtn9Psy2eLmVkvpqZo6LgEvXT?= =?iso-8859-1?Q?+TM6JN669iLDkmRIqhBXu7S1a3vuF8zuZlX0fiSToGO1GMBcp6QCEPPpSv?= =?iso-8859-1?Q?0xgj71++jO8+3ThgoG6TuB7rCg6l7m6All25tJcl3giiqKmBS0KPf+v5z7?= =?iso-8859-1?Q?SytHvHG6NrY5z/CIrastAgmYS1XlL/nSWKqCky1VcWWw7/+qYVsfAIrq7P?= =?iso-8859-1?Q?S+pbdyPch7svJcymIg9ZauWmpQx6m+CVe0Nw04ywsaQx5bMb1mii1icoTL?= =?iso-8859-1?Q?vrIdU7ODZknBjvkkbbtpK5lFpe/8eefJk67Q/V5767ugBe/tPwuaqEd+E4?= =?iso-8859-1?Q?kgbZRMmJ89ajR7/d9qE0x62jPBvbCTEt96DorCZDET6AMV2M6QHJS38Sh4?= =?iso-8859-1?Q?6t8VybJ3cWmfz6mP9V9b7Lki5gdFIHYjALG8P+r2WiveqF36oorFzL8Hgv?= =?iso-8859-1?Q?y6Em2cEI7AUwTrRqERVSk9TIfOr6UTDe0JtFGoxbZbLW0H5TnASXHyYDBe?= =?iso-8859-1?Q?1++JN/gA3avO72tqdGM8TAr9AOIuEH4jVjRkn8EAfR0Aed7OXkBNaVk7Sm?= =?iso-8859-1?Q?8r7Fp9MLVRj6bXwRUk94gGqafOBDkJpG33VwaRpAD2B4/wsW6OturY2BGI?= =?iso-8859-1?Q?luj52YrZLSr1GPFn5QnFvsyYs83buQ7xSSe79YPfaN28EIeMdmLtNeiYXz?= =?iso-8859-1?Q?M7ZB7QcIcgAf1ycdATjJF31zB8JAH9bGEHbANHR6z8cVvKytD4UUdeSEhC?= =?iso-8859-1?Q?qPBLtcgBtKMEY92GmRbCILWu/wz+6hNfRd1JFezbXvDO5/xWi1cHEAbJYH?= =?iso-8859-1?Q?dOMn2xzRVUWve+44QrFFmQTA64S8CbIA1sAV5rQWBg8IgeJc5ERIvezcW8?= =?iso-8859-1?Q?P5cfHzyzJPgSzcS+olCk4Erwq4twPSvklLWhd3D02Hv+9vk0K6/40JtCif?= =?iso-8859-1?Q?iE1Np8W2+44VwWE1eKvyU/N9uB19Mab0YVefsP3b4CZ1oldUmMIl/Abhzv?= =?iso-8859-1?Q?O7xCAEambv/o/IdxDIkCoNRCnb0ZUfZGj2ODvNGf212x03oqwLhMwkA058?= =?iso-8859-1?Q?PSUnbmAxIZpHBuzxMRr1WxXm26UXXWRxJKBtsUYQAeI4=3D?= X-Microsoft-Exchange-Diagnostics: 1;BN3PR0301MB1218;5:sRmIdsS7UDCeCxVDSOssYwJjn3X5+feWlHRS3EFuANdjoEzLNTEElbXNYni3EU87XyvbhnXidILqd3jmsKxlTETJ4IZupKx9Jz7FzItISWkdsgmO0IAornYTgfM56MwjXKOSeKB6ybWfoRhRkzPcMg==;24:AWP3djl9uOAcWF6FU18EbEx6CTQjadGMCPS7/HbkeJ2s1CzMqvBo3JfMkn1vJUYacNB8Wy7KEgDOjFwQqzg3nxHmjgu2QVfRjzR9BAcPMcg= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: freescale.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Nov 2015 09:02:35.9654 (UTC) X-MS-Exchange-CrossTenant-Id: 710a03f5-10f6-4d38-9ff4-a80b81da590d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=710a03f5-10f6-4d38-9ff4-a80b81da590d;Ip=[192.88.168.50];Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN3PR0301MB1218 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 5463 Lines: 166 On Wed, Nov 25, 2015 at 12:45:34PM -0300, Emilio L?pez wrote: > From: Reilly Grant > > The new USBDEVFS_DROP_PRIVILEGES ioctl allows a process to voluntarily > relinquish the ability to issue other ioctls that may interfere with > other processes and drivers that have claimed an interface on the > device. > > Signed-off-by: Reilly Grant > Reviewed-by: Jorge Lucangeli Obes > Reviewed-by: Kees Cook > [emilio.lopez: fix build for v4.4-rc2 and adjust patch title prefix] > Signed-off-by: Emilio L?pez > > --- > > drivers/usb/core/devio.c | 50 +++++++++++++++++++++++++++++++++++---- > include/uapi/linux/usbdevice_fs.h | 1 + > 2 files changed, 47 insertions(+), 4 deletions(-) > > diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c > index 38ae877c..a5ccc50 100644 > --- a/drivers/usb/core/devio.c > +++ b/drivers/usb/core/devio.c > @@ -77,6 +77,7 @@ struct usb_dev_state { > unsigned long ifclaimed; > u32 secid; > u32 disabled_bulk_eps; > + bool privileges_dropped; > }; > > struct async { > @@ -878,7 +879,7 @@ static int usbdev_open(struct inode *inode, struct file *file) > int ret; > > ret = -ENOMEM; > - ps = kmalloc(sizeof(struct usb_dev_state), GFP_KERNEL); > + ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL); > if (!ps) > goto out_free_ps; > > @@ -911,11 +912,8 @@ static int usbdev_open(struct inode *inode, struct file *file) > INIT_LIST_HEAD(&ps->async_pending); > INIT_LIST_HEAD(&ps->async_completed); > init_waitqueue_head(&ps->wait); > - ps->discsignr = 0; > ps->disc_pid = get_pid(task_pid(current)); > ps->cred = get_current_cred(); > - ps->disccontext = NULL; > - ps->ifclaimed = 0; The above changes seem to be not related with this change. > security_task_getsecid(current, &ps->secid); > smp_wmb(); > list_add_tail(&ps->list, &dev->filelist); > @@ -1215,6 +1213,35 @@ static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg) > > static int proc_resetdevice(struct usb_dev_state *ps) > { > + if (ps->privileges_dropped) { > + struct usb_host_config *actconfig = ps->dev->actconfig; > + > + /* Don't touch the device if any interfaces are claimed. It > + * could interfere with other drivers' operations and this > + * process has dropped its privileges to do such things. > + */ > + if (actconfig) { > + int i; > + > + for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) { > + if (usb_interface_claimed( > + actconfig->interface[i])) { > + dev_warn(&ps->dev->dev, > + "usbfs: interface %d claimed by" > + " %s while '%s'" > + " resets device\n", > + actconfig->interface[i] > + ->cur_altsetting > + ->desc.bInterfaceNumber, The length of line is 80 characters > + actconfig->interface[i] > + ->dev.driver->name, > + current->comm); > + return -EACCES; > + } > + } > + } > + } > + > return usb_reset_device(ps->dev); > } > > @@ -1922,6 +1949,9 @@ static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl) > struct usb_interface *intf = NULL; > struct usb_driver *driver = NULL; > > + if (ps->privileges_dropped) > + return -EACCES; > + > /* alloc buffer */ > size = _IOC_SIZE(ctl->ioctl_code); > if (size > 0) { > @@ -2084,6 +2114,9 @@ static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg) > sizeof(dc.driver)) == 0) > return -EBUSY; > > + if (ps->privileges_dropped) > + return -EACCES; > + > dev_dbg(&intf->dev, "disconnect by usbfs\n"); > usb_driver_release_interface(driver, intf); > } > @@ -2130,6 +2163,12 @@ static int proc_free_streams(struct usb_dev_state *ps, void __user *arg) > return r; > } > > +static int proc_drop_privileges(struct usb_dev_state *ps) > +{ > + ps->privileges_dropped = true; > + return 0; > +} > + > /* > * NOTE: All requests here that have interface numbers as parameters > * are assuming that somehow the configuration has been prevented from > @@ -2318,6 +2357,9 @@ static long usbdev_do_ioctl(struct file *file, unsigned int cmd, > case USBDEVFS_FREE_STREAMS: > ret = proc_free_streams(ps, p); > break; > + case USBDEVFS_DROP_PRIVILEGES: > + ret = proc_drop_privileges(ps); > + break; > } > > done: > diff --git a/include/uapi/linux/usbdevice_fs.h b/include/uapi/linux/usbdevice_fs.h > index 019ba1e..99c296b 100644 > --- a/include/uapi/linux/usbdevice_fs.h > +++ b/include/uapi/linux/usbdevice_fs.h > @@ -187,5 +187,6 @@ struct usbdevfs_streams { > #define USBDEVFS_DISCONNECT_CLAIM _IOR('U', 27, struct usbdevfs_disconnect_claim) > #define USBDEVFS_ALLOC_STREAMS _IOR('U', 28, struct usbdevfs_streams) > #define USBDEVFS_FREE_STREAMS _IOR('U', 29, struct usbdevfs_streams) > +#define USBDEVFS_DROP_PRIVILEGES _IO('U', 30) > > #endif /* _UAPI_LINUX_USBDEVICE_FS_H */ > -- > 2.5.0 > > -- > To unsubscribe from this list: send the line "unsubscribe linux-usb" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html -- Best Regards, Peter Chen -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/