From patchwork Fri Jul 1 14:22:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12903381 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33C9AC43334 for ; Fri, 1 Jul 2022 14:24:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C3B0A6B007B; Fri, 1 Jul 2022 10:24:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BEB8B6B0080; Fri, 1 Jul 2022 10:24:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A64CE6B0081; Fri, 1 Jul 2022 10:24:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 96B506B007B for ; Fri, 1 Jul 2022 10:24:24 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 619C332E61 for ; Fri, 1 Jul 2022 14:24:24 +0000 (UTC) X-FDA: 79638751248.23.DF0903D Received: from mail-lf1-f74.google.com (mail-lf1-f74.google.com [209.85.167.74]) by imf09.hostedemail.com (Postfix) with ESMTP id 0266014004A for ; Fri, 1 Jul 2022 14:24:23 +0000 (UTC) Received: by mail-lf1-f74.google.com with SMTP id cf10-20020a056512280a00b0047f5a295656so1179054lfb.15 for ; Fri, 01 Jul 2022 07:24:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=zhMMZRuLDF6Pq/3lj4DVrVpRjyIDnZPpkOwtCQdBukQ=; b=lkUxcVIUd6qYJWLZjSG/W4rDF4tKMzJAIsWUfl6gMo2TbAtriVTJcbmnvCG0fDmLtR DSxHoi2jn1iZ6FTySbjTu0l2dKeJKYobkKMLVr3syT3UqAPU7Gicf7xbtJFBAnO8MUY4 vTjs9vt2d4hTA5DzWjclXDwG6pCooj2Y6P8QdUCwb/eQteblrOr5loBK0JOs3GeXIjiT bTlvokzLiEC8wCntWUwPevBsZ4yMr8XgzW+9JpT56ZFCmuIv1st0kOtDttQvmEkHDh8L 94SedVfvAPCYp0ve6D8T2GRRbwpPBMgE9SCNcHcKSge6nGUJRq/FSujqYTyhaNQGmAwj kbcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=zhMMZRuLDF6Pq/3lj4DVrVpRjyIDnZPpkOwtCQdBukQ=; b=vzmgVgwGLXEMnWoorgu5XS2IuimuGmc3EqeOLVUpVmKfKWD1qpdoMYt7J5GfGFNAgS jGUWCjdZuUnoUyg8WTAks6HQ1BtuwCFVCUUm4d8Kr+vFMtRMHo+hdD3id8+DwONGVU23 d6IU0UIbMuC2iWqb3daNDYTIlojSs2ra4JUjLolqavBmtUv1fgZAAV9chSonCsbZjbWG /tOHF1bmQgL0HV1X04T5OBjSk4U403KME8LCsXImOo7pkyjccQNCEY2/C9w/tuKWnycI 56pfa38bO69nlyb77uAD9RL4e0iDRl1AGMbIM8gn0cn2yRKXR7wtwYUy9DQz1/pAxk5b Dq6A== X-Gm-Message-State: AJIora/di7/rd91R2aba26XKclO+Pd2mAbEZhDew7ZyGxasYdvVup5oz 1iRe9ybsJ861Xu/FZlJ4hn5zuCgd1oU= X-Google-Smtp-Source: AGRyM1ujhhEkPAeMzba+3rjROrqZ1qk7eONuMn4gI/gNE3ySm/CnDDnQvH3rDtZnfoMmy4wbS0DjU1LDOLQ= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:a6f5:f713:759c:abb6]) (user=glider job=sendgmr) by 2002:a05:6512:1291:b0:47f:6ece:310e with SMTP id u17-20020a056512129100b0047f6ece310emr9097403lfs.389.1656685462649; Fri, 01 Jul 2022 07:24:22 -0700 (PDT) Date: Fri, 1 Jul 2022 16:22:49 +0200 In-Reply-To: <20220701142310.2188015-1-glider@google.com> Message-Id: <20220701142310.2188015-25-glider@google.com> Mime-Version: 1.0 References: <20220701142310.2188015-1-glider@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v4 24/45] kmsan: handle memory sent to/from USB From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , Andrew Morton , Andrey Konovalov , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Christoph Hellwig , Christoph Lameter , David Rientjes , Dmitry Vyukov , Eric Dumazet , Greg Kroah-Hartman , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jens Axboe , Joonsoo Kim , Kees Cook , Marco Elver , Mark Rutland , Matthew Wilcox , "Michael S. Tsirkin" , Pekka Enberg , Peter Zijlstra , Petr Mladek , Steven Rostedt , Thomas Gleixner , Vasily Gorbik , Vegard Nossum , Vlastimil Babka , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1656685464; a=rsa-sha256; cv=none; b=bZ2mZMpaWWgHV9fcNHPEiYOZXnaymniuHmZnwzCJfOluc4xXY/adzvFy0HIzPMbu0HEFt9 dh8ofvhZXj2Swy7SdcvvMdTgT9FxOINkM++OlY6fqpJnu06pDaSaHsoRBblotr8THcIid3 n8dSArRKAvFURWsmIHF4nQUtWT5TPBI= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=lkUxcVIU; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3lgO_YgYKCLUbgdYZmbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--glider.bounces.google.com designates 209.85.167.74 as permitted sender) smtp.mailfrom=3lgO_YgYKCLUbgdYZmbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1656685464; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zhMMZRuLDF6Pq/3lj4DVrVpRjyIDnZPpkOwtCQdBukQ=; b=z1BpUM4VIegOpVa50T52F/2YkhtRUyHVLGmkvBOvGupeyc/VXR39cC9ls/yNWLqTdxiKFu 23prLU98usBivemD1xB3qzacIdOrNTWj7yv11TFGIKAAtJUmVmNWILFrVv2zv/kQdjB0py jakoP+Bds1WKPo22SbfpGYN0N8ZpnH4= X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 0266014004A Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=lkUxcVIU; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3lgO_YgYKCLUbgdYZmbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--glider.bounces.google.com designates 209.85.167.74 as permitted sender) smtp.mailfrom=3lgO_YgYKCLUbgdYZmbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--glider.bounces.google.com X-Rspam-User: X-Stat-Signature: siaqzb9dqmtce5sd3gbecndr4z3sux6m X-HE-Tag: 1656685463-920020 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Depending on the value of is_out kmsan_handle_urb() KMSAN either marks the data copied to the kernel from a USB device as initialized, or checks the data sent to the device for being initialized. Signed-off-by: Alexander Potapenko --- v2: -- move kmsan_handle_urb() implementation to this patch Link: https://linux-review.googlesource.com/id/Ifa67fb72015d4de14c30e971556f99fc8b2ee506 --- drivers/usb/core/urb.c | 2 ++ include/linux/kmsan.h | 15 +++++++++++++++ mm/kmsan/hooks.c | 17 +++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c index 33d62d7e3929f..1fe3f23205624 100644 --- a/drivers/usb/core/urb.c +++ b/drivers/usb/core/urb.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -426,6 +427,7 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags) URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL | URB_DMA_SG_COMBINED); urb->transfer_flags |= (is_out ? URB_DIR_OUT : URB_DIR_IN); + kmsan_handle_urb(urb, is_out); if (xfertype != USB_ENDPOINT_XFER_CONTROL && dev->state < USB_STATE_CONFIGURED) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index 55fe673ee1e84..e8b5c306c4aa1 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -19,6 +19,7 @@ struct page; struct kmem_cache; struct task_struct; struct scatterlist; +struct urb; #ifdef CONFIG_KMSAN @@ -235,6 +236,16 @@ void kmsan_handle_dma(struct page *page, size_t offset, size_t size, void kmsan_handle_dma_sg(struct scatterlist *sg, int nents, enum dma_data_direction dir); +/** + * kmsan_handle_urb() - Handle a USB data transfer. + * @urb: struct urb pointer. + * @is_out: data transfer direction (true means output to hardware). + * + * If @is_out is true, KMSAN checks the transfer buffer of @urb. Otherwise, + * KMSAN initializes the transfer buffer. + */ +void kmsan_handle_urb(const struct urb *urb, bool is_out); + #else static inline void kmsan_init_shadow(void) @@ -327,6 +338,10 @@ static inline void kmsan_handle_dma_sg(struct scatterlist *sg, int nents, { } +static inline void kmsan_handle_urb(const struct urb *urb, bool is_out) +{ +} + #endif #endif /* _LINUX_KMSAN_H */ diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 8a6947a2a2f22..9aecbf2825837 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "../internal.h" #include "../slab.h" @@ -252,6 +253,22 @@ void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy, } EXPORT_SYMBOL(kmsan_copy_to_user); +/* Helper function to check an URB. */ +void kmsan_handle_urb(const struct urb *urb, bool is_out) +{ + if (!urb) + return; + if (is_out) + kmsan_internal_check_memory(urb->transfer_buffer, + urb->transfer_buffer_length, + /*user_addr*/ 0, REASON_SUBMIT_URB); + else + kmsan_internal_unpoison_memory(urb->transfer_buffer, + urb->transfer_buffer_length, + /*checked*/ false); +} +EXPORT_SYMBOL(kmsan_handle_urb); + static void kmsan_handle_dma_page(const void *addr, size_t size, enum dma_data_direction dir) {