From nobody Thu Mar 10 08:35:33 2022 X-Original-To: dev-commits-src-branches@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 2A8F11A03279; Thu, 10 Mar 2022 08:35:34 +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 4KDj7V0TpWz4Yqf; Thu, 10 Mar 2022 08:35:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646901334; 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=MfmUOvv/ZOyOx2YKDs2NHKyjhwSj1RFUeSqXXf0t1Qg=; b=B7M4l03jHUQdl60yLmTgKBzIEzrZUBAkLsAB7s0d9SA/BOtgbkZ0Dj3Y1xwuzOKWxYoKXx /aCCq/coPqNk5LnebwJH+/4SEJVQuyeBm8NjWNU/qQmw3R6r6so+gbfySbf68+UuOPQwFH QGhngSocIOVPvrj02iRApjw6+QABMx2pRb6N5qn92a4ZptKm/ExcmJfiDBBeysQ2rcmpOy F6xHNzB4l67N+ApXCa7nv8dCOe5YdW9i40GvMqu7UXtWQBv++DNF2ckNO5QO46hgd5TZr3 S0C/JUpsAhEX8XxVKtquA1dcEIoDMfAffT7o0mQBfRx/eLeW/iXt2hc2ZQ9ALA== 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 E0BE8214E6; Thu, 10 Mar 2022 08:35:33 +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 22A8ZXtQ003322; Thu, 10 Mar 2022 08:35:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 22A8ZXAc003321; Thu, 10 Mar 2022 08:35:33 GMT (envelope-from git) Date: Thu, 10 Mar 2022 08:35:33 GMT Message-Id: <202203100835.22A8ZXAc003321@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Hans Petter Selasky Subject: git: 9157566d7ad5 - stable/13 - usb(4): Factor out the usb_check_request() function. List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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/stable/13 X-Git-Reftype: branch X-Git-Commit: 9157566d7ad58560c6fa0ba6c2b88587688ee02c Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646901334; 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=MfmUOvv/ZOyOx2YKDs2NHKyjhwSj1RFUeSqXXf0t1Qg=; b=uZwCwe+bebfVBSgP1+tN5Z4ysJpVZtaM4++Zb61EjeU5R6q78pA0ThpRKrnEeUJDCcSRRn GCjm+CoqHfifadVPWkw7tjCRi9+AFKr5YPQvz2l4cYbMwuDoT2WYsoYZlPnK+KJSSPVevh mpTN/+09JnqGBqxVKzu5swvYNs9jIyu0mHeKPChX1hJN+K5gqIlOpvv9T+2SxcWrsWu931 /per+tGVqhFK+2vCu0bpu5JF8+GEm3T5CdoKfkBm32KYBdKuQVUh9q6a4yzpDndsEJyHrY hqP4iaCDYAwGv5UvCPaDjb/SY1DyZf9pcHB/Qq2WgA6jcgHEs1n70jQfLbkz4Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1646901334; a=rsa-sha256; cv=none; b=CfF2e5clzzVowuiEDL4fI+Rza6zzoflcjDEfEFvJz74/KlIZzFOYuAxKDiBpafXadsk8bq TYgaQnP1XBOZ4zgGkoA6URVa3BkyQb3jOhLAKexMqJAW4U/mlmOK4rzxRm4FrHhOlhfG44 bL4xYrfytMGTBgJMKJm+ltiXxV1O2wH6Wg6AiCxJSb3POieVixMDBXsuXUkxZxRj1Hde/5 LV057IfGvFMgs6CLqUzD67IbdpiFy2GTKgEdGQ7SwCjHrM2Xq+eR2weebGJnS/FhNBTncV 8BSkdHMnNFiwtto5nSVKi0fLZNPdkhgOovxf+cTEMFbqSBGxkIECZadF87LfsA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by hselasky: URL: https://cgit.FreeBSD.org/src/commit/?id=9157566d7ad58560c6fa0ba6c2b88587688ee02c commit 9157566d7ad58560c6fa0ba6c2b88587688ee02c Author: Hans Petter Selasky AuthorDate: 2022-03-03 09:22:41 +0000 Commit: Hans Petter Selasky CommitDate: 2022-03-10 08:18:00 +0000 usb(4): Factor out the usb_check_request() function. No functional change. Sponsored by: NVIDIA Networking (cherry picked from commit 8ed5bb59e913c61e61a4232e8221f35256f72ad2) --- 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 65af2bd6979d..8c9d910ee92e 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 @@ -862,54 +862,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) { @@ -917,7 +869,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); @@ -1105,7 +1057,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_ */