Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp862408pxa; Sat, 1 Aug 2020 08:23:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy0GEXZdQEYcTeSQCOPN9MWmZ3CEgW9htkCPK84YnazMeK+6Ue4VQJR9Ncj+EBcxB8M1cgc X-Received: by 2002:a05:6402:60e:: with SMTP id n14mr8623797edv.29.1596295415745; Sat, 01 Aug 2020 08:23:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596295415; cv=none; d=google.com; s=arc-20160816; b=ozw6pviw2holpzFaWSdkKxj3vYq+0RmZDxayp6ECtkFg9GUBACgELQ08qema7/mqUZ mlgKQrn2Z9GJ19hCd+/hH0iDC1m4rLmxIahNPQFhy6KJtMKo8qn8ORX6qg6CbYmWMJnR qzKDoP+BlaHmxSRUC07sVOAAj6ftV7GTXXKw1+E9CmSedQ7FPnlBpvInDSHuW1mqDcm2 Bz6yShkvjpzWl9LiqHxPjpdv3s63bJYU1aJJgkVql1BK/5BbIgkItfLww8IifV+VmPZb IKPEtCmhCF4fMvSPgYfTyNErKx7gx6oqwJPDC4spHTOXkEXZSt3P2cu4mgeQ9lkeemLf grXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=Aa47QBUvL1X5/ije6bSZUOh1Pa7QFATyRjxhRckOakw=; b=EoOpA3PKmF4BKVTgvLKI4R9z8OCz0iVahmoX6pzZiwEUG0VDRI4jakVkwCoiRMW7YG D8SqQd9wCe9HyhTRDa/R6WFz0I/4WunM24cnDNoUiVAcbfR0jHNDuY00Lw7qErJ4mrs7 IJPkVSf6dRpjFUyLKlEhU6/d4xZoDbpfGyc4/uvsy37rrjaNQCZQSh6HQ9H+s6n4ioOb wxJfyB9J5zCso8h7FcgxsahFr1FMNnv1odHMrDyLl6WTUP4HSNwLaNuQ6bjnwVFb+usn 449yVU3ZSxaw7gpF6vtKnvdFYozsY7vOlt8PYR9dRUGzweTIa+PpeGIVn4jJznhNbQfu UWjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dOPfhxbO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j19si5366019eja.495.2020.08.01.08.22.59; Sat, 01 Aug 2020 08:23:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dOPfhxbO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725841AbgHAPWF (ORCPT + 99 others); Sat, 1 Aug 2020 11:22:05 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:43637 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725804AbgHAPWF (ORCPT ); Sat, 1 Aug 2020 11:22:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1596295323; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc; bh=Aa47QBUvL1X5/ije6bSZUOh1Pa7QFATyRjxhRckOakw=; b=dOPfhxbO1PD51jgIL9vBA+wqrY5KaOJV+KkpUcIopYcPrgMPojIEWhiBH8HfbVrSRMCvYv nK0pa+jPvmGxGaPeNaDGK5RIb3Ndm6WAnB2vVzUi6AdkUq+Rh5r0t+gMnhxfTHHnQ5md3c 0dlmvi1ysXgfxLAkUk3FhBDew6Cdphs= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-390-YHV1N2Z1NfSQZbTBW_7QfQ-1; Sat, 01 Aug 2020 11:22:01 -0400 X-MC-Unique: YHV1N2Z1NfSQZbTBW_7QfQ-1 Received: by mail-qv1-f69.google.com with SMTP id k17so8766573qvj.12 for ; Sat, 01 Aug 2020 08:22:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Aa47QBUvL1X5/ije6bSZUOh1Pa7QFATyRjxhRckOakw=; b=Zn5AZufP3VD+nEiQvcCMnsNstjRsQNEH9y3JoBZx9UkMiOh8Zjp3obJO7bC2aE8JMT 4LXpUP1NU5e1yDS3xQq9SOLox7Sada4oHNFJev4N2V0UM77/yyP2mrgM7WLNcurQjHcJ ehl6itL4I/yF953nJYUKLo2fWjIgbgXVAPCmQM9wctIFcyUL9zKLCKLu4saOSwUGx57D qU/ITYXrrCIs3akcP0dOwfzyHnmfJkBdIHJAVbbfzqKk0xFnLmb4YMmVwvPDJAJjg+3R +ilYAcVg48s11dloULVOgXmBe8YH/zxJwhMw7PcEio70ZRSGLRYfM9rSADGM5G+SeIA8 Lx/A== X-Gm-Message-State: AOAM5312OXRqKolCCdRNSiQShyW2ZH/OsATgdqLCsCUT/OYmLJeGLzo1 9mr1E7hzVrgD3cZHBS8pQLnSlanJ3Bwgqp0gP3xk0RCrg64hiHC0f1cyNl+Kt+ugaNGNmiMh50N pMn805oZWZSuTn/R3g45nyqlJ X-Received: by 2002:a0c:ea30:: with SMTP id t16mr545402qvp.177.1596295321367; Sat, 01 Aug 2020 08:22:01 -0700 (PDT) X-Received: by 2002:a0c:ea30:: with SMTP id t16mr545381qvp.177.1596295321117; Sat, 01 Aug 2020 08:22:01 -0700 (PDT) Received: from trix.remote.csb (075-142-250-213.res.spectrum.com. [75.142.250.213]) by smtp.gmail.com with ESMTPSA id 6sm12135784qkj.134.2020.08.01.08.21.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 Aug 2020 08:22:00 -0700 (PDT) From: trix@redhat.com To: oneukum@suse.com, gregkh@linuxfoundation.org, t-herzog@gmx.de Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Tom Rix Subject: [PATCH] cdc-acm: rework notification_buffer resizing Date: Sat, 1 Aug 2020 08:21:54 -0700 Message-Id: <20200801152154.20683-1-trix@redhat.com> X-Mailer: git-send-email 2.18.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tom Rix Clang static analysis reports this error cdc-acm.c:409:3: warning: Use of memory after it is freed acm_process_notification(acm, (unsigned char *)dr); There are three problems, the first one is that dr is not reset The variable dr is set with if (acm->nb_index) dr = (struct usb_cdc_notification *)acm->notification_buffer; But if the notification_buffer is too small it is resized with if (acm->nb_size) { kfree(acm->notification_buffer); acm->nb_size = 0; } alloc_size = roundup_pow_of_two(expected_size); /* * kmalloc ensures a valid notification_buffer after a * use of kfree in case the previous allocation was too * small. Final freeing is done on disconnect. */ acm->notification_buffer = kmalloc(alloc_size, GFP_ATOMIC); dr should point to the new acm->notification_buffer. The second problem is any data in the notification_buffer is lost when the pointer is freed. In the normal case, the current data is accumulated in the notification_buffer here. memcpy(&acm->notification_buffer[acm->nb_index], urb->transfer_buffer, copy_size); When a resize happens, anything before notification_buffer[acm->nb_index] is garbage. The third problem is the acm->nb_index is not reset on a resizing buffer error. So switch resizing to using krealloc and reassign dr and reset nb_index. Fixes: ea2583529cd1 ("cdc-acm: reassemble fragmented notifications") Signed-off-by: Tom Rix --- drivers/usb/class/cdc-acm.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c index 991786876dbb..7f6f3ab5b8a6 100644 --- a/drivers/usb/class/cdc-acm.c +++ b/drivers/usb/class/cdc-acm.c @@ -378,21 +378,19 @@ static void acm_ctrl_irq(struct urb *urb) if (current_size < expected_size) { /* notification is transmitted fragmented, reassemble */ if (acm->nb_size < expected_size) { - if (acm->nb_size) { - kfree(acm->notification_buffer); - acm->nb_size = 0; - } + u8 *new_buffer; alloc_size = roundup_pow_of_two(expected_size); - /* - * kmalloc ensures a valid notification_buffer after a - * use of kfree in case the previous allocation was too - * small. Final freeing is done on disconnect. - */ - acm->notification_buffer = - kmalloc(alloc_size, GFP_ATOMIC); - if (!acm->notification_buffer) + /* Final freeing is done on disconnect. */ + new_buffer = krealloc(acm->notification_buffer, + alloc_size, GFP_ATOMIC); + if (!new_buffer) { + acm->nb_index = 0; goto exit; + } + + acm->notification_buffer = new_buffer; acm->nb_size = alloc_size; + dr = (struct usb_cdc_notification *)acm->notification_buffer; } copy_size = min(current_size, -- 2.18.1