From nobody Thu Mar 03 09:25:47 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 83D8219FB13B; Thu, 3 Mar 2022 09:25:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4K8QZg3GdZz4b3M; Thu, 3 Mar 2022 09:25:47 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646299547; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=G3iCLb81pp08WpmRYddu3ED8yC4cUY15K3Q1azPq+GA=; b=PGQ1Il8NmWk985y7Wef10Fj1VXU5pW55u1NoYZ2rm+H6add6ofgS8RSMGuSQ/M6UXCl6wM mUYeDFbJL6hXJINa7xyW2fisY+03Pr65b6JaR378IIlvj/qjRuaXc78DF0HcArFyZ8quN+ NhucyyxXr9dkfFoma8Hfsu2qYOl55zZt3zhxlGPLSSmbG+1Ikmy5uf84Sh3K9i3wU9iKb9 EK9JQVFoG2x8m5wJ7MGxXsTiRJj5BnG76vkBUJuCNC9i2zXPq3GIGj+w2DA61DkONEoegj ABgCdQOEfY8VjXtdmLIU4t/o6ljCzxgRk3eTd7W/5ts0SoxziGW6jtP7Hy5z/g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4FD7B15168; Thu, 3 Mar 2022 09:25:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2239Pl47092917; Thu, 3 Mar 2022 09:25:47 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2239PlDB092916; Thu, 3 Mar 2022 09:25:47 GMT (envelope-from git) Date: Thu, 3 Mar 2022 09:25:47 GMT Message-Id: <202203030925.2239PlDB092916@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Hans Petter Selasky Subject: git: 8ed5bb59e913 - main - usb(4): Factor out the usb_check_request() function. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: hselasky X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8ed5bb59e913c61e61a4232e8221f35256f72ad2 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646299547; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=G3iCLb81pp08WpmRYddu3ED8yC4cUY15K3Q1azPq+GA=; b=QLe1DlLkA1ELQGkC6ykZiT8Yo7UXQQWCLHPmWCAaAmIp1BEafpwSBJHKrDdivNw/n0LxJn CK8C4OhEYAgGD4bkap1QmlORnQH7+WcGbQnHIPGm/0PJzL4DVHyi+u+4/8FtfC4Yf+M5uP T2u+mgnFtV3vI48YJHXTQtbhlHMF4s+1h4a2vS1xS3BFTKpqiEO9yysrdR0RA+8MErX6Zr nOY/JJrEFOahvPuL94I2P0MTvOzKCKy362BxXhM8BrZZe19SVXH7voI7lw13QaPpHC+zmi ksjttKl3ZDNBfctyEnHcCkAO6mBgHkQM6bEjyhvN6LckZABz1NBU1s5tN66ZJQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1646299547; a=rsa-sha256; cv=none; b=aHPRuIZhiBj5/M1KlD8nFemc4ICYplUlj43BOirnB1A09iWK3Jm1AkjNh7WJrfUR9pjqNk +1Bm/UstGRUV2KgrOXKNLaQeMbKeHI14sZpRajXjR+ziHwwCplPBH8JfuFezVdArf6I8pU +BetBttPsd7HZ287esQdZ3+fTOuwm/13K6pfwLrEVnPU3VxaL4ldieECNXl9Nsxf+XC41e 5NYCLU3LkhnvUrZTmjHvquFHy5nTAewWAMVBjersqt5LTUXuAzDjo54QdCV3PTXXXAceAF fF4Ni1J+QefRJzO+tzlRPBpk/iJ1e+Em7HSwTqQDwo5ttCrmMjdhiVgbl1bPgA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by hselasky: URL: https://cgit.FreeBSD.org/src/commit/?id=8ed5bb59e913c61e61a4232e8221f35256f72ad2 commit 8ed5bb59e913c61e61a4232e8221f35256f72ad2 Author: Hans Petter Selasky AuthorDate: 2022-03-03 09:22:41 +0000 Commit: Hans Petter Selasky CommitDate: 2022-03-03 09:24:24 +0000 usb(4): Factor out the usb_check_request() function. No functional change. MFC after: 1 week Sponsored by: NVIDIA Networking --- sys/dev/usb/usb_generic.c | 54 +++-------------------------------------------- sys/dev/usb/usb_util.c | 51 +++++++++++++++++++++++++++++++++++++++++++- sys/dev/usb/usb_util.h | 4 ++++ 3 files changed, 57 insertions(+), 52 deletions(-) diff --git a/sys/dev/usb/usb_generic.c b/sys/dev/usb/usb_generic.c index ab076d7234fb..fdbc35d47169 100644 --- a/sys/dev/usb/usb_generic.c +++ b/sys/dev/usb/usb_generic.c @@ -2,7 +2,7 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * - * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. + * Copyright (c) 2008-2022 Hans Petter Selasky * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -869,54 +869,6 @@ ugen_fill_deviceinfo(struct usb_fifo *f, struct usb_device_info *di) return (0); } -/*------------------------------------------------------------------------* - * ugen_check_request - * - * Return values: - * 0: Access allowed - * Else: No access - *------------------------------------------------------------------------*/ -static int -ugen_check_request(struct usb_device *udev, struct usb_device_request *req) -{ - struct usb_endpoint *ep; - int error; - - /* - * Avoid requests that would damage the bus integrity: - */ - if (((req->bmRequestType == UT_WRITE_DEVICE) && - (req->bRequest == UR_SET_ADDRESS)) || - ((req->bmRequestType == UT_WRITE_DEVICE) && - (req->bRequest == UR_SET_CONFIG)) || - ((req->bmRequestType == UT_WRITE_INTERFACE) && - (req->bRequest == UR_SET_INTERFACE))) { - /* - * These requests can be useful for testing USB drivers. - */ - error = priv_check(curthread, PRIV_DRIVER); - if (error) { - return (error); - } - } - /* - * Special case - handle clearing of stall - */ - if (req->bmRequestType == UT_WRITE_ENDPOINT) { - ep = usbd_get_ep_by_addr(udev, req->wIndex[0]); - if (ep == NULL) { - return (EINVAL); - } - if ((req->bRequest == UR_CLEAR_FEATURE) && - (UGETW(req->wValue) == UF_ENDPOINT_HALT)) { - usbd_clear_data_toggle(udev, ep); - } - } - /* TODO: add more checks to verify the interface index */ - - return (0); -} - int ugen_do_request(struct usb_fifo *f, struct usb_ctl_request *ur) { @@ -924,7 +876,7 @@ ugen_do_request(struct usb_fifo *f, struct usb_ctl_request *ur) uint16_t len; uint16_t actlen; - if (ugen_check_request(f->udev, &ur->ucr_request)) { + if (usb_check_request(f->udev, &ur->ucr_request)) { return (EPERM); } len = UGETW(ur->ucr_request.wLength); @@ -1165,7 +1117,7 @@ ugen_fs_copy_in(struct usb_fifo *f, uint8_t ep_index) return (error); } } - if (ugen_check_request(f->udev, req)) { + if (usb_check_request(f->udev, req)) { xfer->error = USB_ERR_INVAL; goto complete; } diff --git a/sys/dev/usb/usb_util.c b/sys/dev/usb/usb_util.c index 8a85bf6506bb..b333180b6551 100644 --- a/sys/dev/usb/usb_util.c +++ b/sys/dev/usb/usb_util.c @@ -2,7 +2,7 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * - * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. + * Copyright (c) 2008-2022 Hans Petter Selasky * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -223,3 +223,52 @@ usb_make_str_desc(void *ptr, uint16_t max_len, const char *s) } return (totlen); } + +/*------------------------------------------------------------------------* + * usb_check_request - prevent damaging USB requests + * + * Return values: + * 0: Access allowed + * Else: No access + *------------------------------------------------------------------------*/ +#if USB_HAVE_UGEN +int +usb_check_request(struct usb_device *udev, struct usb_device_request *req) +{ + struct usb_endpoint *ep; + int error; + + /* + * Avoid requests that would damage the bus integrity: + */ + if (((req->bmRequestType == UT_WRITE_DEVICE) && + (req->bRequest == UR_SET_ADDRESS)) || + ((req->bmRequestType == UT_WRITE_DEVICE) && + (req->bRequest == UR_SET_CONFIG)) || + ((req->bmRequestType == UT_WRITE_INTERFACE) && + (req->bRequest == UR_SET_INTERFACE))) { + /* + * These requests can be useful for testing USB drivers. + */ + error = priv_check(curthread, PRIV_DRIVER); + if (error) + return (error); + } + + /* + * Special case - handle clearing of stall + */ + if (req->bmRequestType == UT_WRITE_ENDPOINT) { + ep = usbd_get_ep_by_addr(udev, req->wIndex[0]); + if (ep == NULL) + return (EINVAL); + if ((req->bRequest == UR_CLEAR_FEATURE) && + (UGETW(req->wValue) == UF_ENDPOINT_HALT)) + usbd_clear_data_toggle(udev, ep); + } + + /* TODO: add more checks to verify the interface index */ + + return (0); +} +#endif diff --git a/sys/dev/usb/usb_util.h b/sys/dev/usb/usb_util.h index 87e54d35150e..1bb20b86914b 100644 --- a/sys/dev/usb/usb_util.h +++ b/sys/dev/usb/usb_util.h @@ -29,8 +29,12 @@ #ifndef _USB_UTIL_H_ #define _USB_UTIL_H_ +struct usb_device; +struct usb_device_request; + uint8_t usb_make_str_desc(void *ptr, uint16_t max_len, const char *s); void usb_printbcd(char *p, uint16_t p_len, uint16_t bcd); void usb_trim_spaces(char *p); +int usb_check_request(struct usb_device *, struct usb_device_request *); #endif /* _USB_UTIL_H_ */