From patchwork Tue Aug 27 18:56:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 823249 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E99E91D47D2 for ; Tue, 27 Aug 2024 18:56:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724784998; cv=none; b=dE6owNoNYu/jbehVGswdvUWOvvTVxikjRRSb0xx7Qf8T0zPRKo6QWTvxrg/+l+pNsDCGk/GWuIlRMh1zX+JwYii92nMXhlh6me7cews95auaodlpcCyvM0Xbtu3OP4Bv/0ajf2xahH5Uvy3kpC3qhqfzu4tcxwZadrpoHi3eORc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724784998; c=relaxed/simple; bh=JMbg4eMibkjbCBRT/iJAl8N9RNBguY/qcuyAy9Bi6uc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iYn3ttNpfKgoidMd7oY24yk0AwLd+jVfpWd90f/Q+8z81Z7sKkVkrc44U54P6IU8ixngohAN7DN9ZdoVxF252dB3/+0ZmvqqFxPL37oYMIYrTXsWFmd+IYeT4fRYqVALzuNGPGEGiztbCvRZOPcmE+1CfFEnEGwD4lhnhE5BnAs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=MdGx0qho; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MdGx0qho" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724784995; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=326BlglD5Q8k45ugPSVf+4k9+mzBOEJ61jidx8Y66gA=; b=MdGx0qhomxAqqDjTQS/o6mPjfTv92DUOnkRc0gIj+1t8jYVtg7V4cvDJzVcSeR6XPh04iy BWosEL9r0maJ2+v8UYy4U2wtoEME24CCWT3YMgZ2CpBmsEzcn37p77EyZQl+zGiKGuh4RT EIjEBmnZLEw7D8pha4wGwet0MQOOc2w= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-679-f2flbfCoMXaLKJ537Yfw7g-1; Tue, 27 Aug 2024 14:56:31 -0400 X-MC-Unique: f2flbfCoMXaLKJ537Yfw7g-1 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-a869078ff9bso585867766b.2 for ; Tue, 27 Aug 2024 11:56:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724784990; x=1725389790; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=326BlglD5Q8k45ugPSVf+4k9+mzBOEJ61jidx8Y66gA=; b=J23jy2DrT5GZPesI8g0D1YS1+zCOYIl5tlih0MoZ5mqgu628VgMZ1Hu9pBpHaVerrA oRzMzxKxgv9skDOR/sCQ/+dKDCQ8BJeb1YoPDzWh8bn/BzsnGB1JsKIRJWda/HxpeUNh s6wfZFy6u/2Sni0j0Gic2YjLB2nig/pxipr5GeAWU0/uspZVm1RI1H8qpleuE0BRs0lz KnqWXQOgC2QFvBJKbKkb7rhc0Jt/Emp+Rtri5iWBe2YBPbLP+4SKateeoVn3Vt6AhjkV unhuu6qYV8PsOo5ZYoR+jzphdvW3G+nxlRjgR7YugzqYHE1l0NWKOlCdDK3RAxSyizL1 AeEA== X-Forwarded-Encrypted: i=1; AJvYcCU3sUbbc5FiQ3Ctg3QoT3Fe44y7vPXmFcCSlXlIdnARa5ecLjzryyWF+XCOoliPE5h6cXPqxSMC1c5+@vger.kernel.org X-Gm-Message-State: AOJu0YzpeaxHW8CJD7i73q/6JY0m5qAdo+ZF0rB2zwz2D6Eo/Ow7PTNG susnMND9ylDY0LVYT7KW/7zgnqeluNIOJsS3/2a+t4K3wg8h5s8/tt9HQCAXqHma9XTr+9n4LUZ tqCdylGD/O0hK/BGWX57PXX18lEZftP1WGAiEyCLxDMpw/nae8pVJyIQeW1g= X-Received: by 2002:a17:907:7295:b0:a86:97c0:9bb3 with SMTP id a640c23a62f3a-a86a54b8d24mr1205130566b.51.1724784990406; Tue, 27 Aug 2024 11:56:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF3LQz+Ovtpn7UQw3Ih17gexf8/42HNMXwzrnqKpk9+fwQeRpCxA2TD7y7c22AXPuu4p6dfJQ== X-Received: by 2002:a17:907:7295:b0:a86:97c0:9bb3 with SMTP id a640c23a62f3a-a86a54b8d24mr1205127066b.51.1724784989954; Tue, 27 Aug 2024 11:56:29 -0700 (PDT) Received: from eisenberg.fritz.box ([2001:16b8:3dbc:3c00:460c:db7e:8195:ddb5]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a86e549c4e9sm141473066b.47.2024.08.27.11.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Aug 2024 11:56:29 -0700 (PDT) From: Philipp Stanner To: ens Axboe , Wu Hao , Tom Rix , Moritz Fischer , Xu Yilun , Andy Shevchenko , Linus Walleij , Bartosz Golaszewski , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Bjorn Helgaas , Alvaro Karsz , "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , Richard Cochran , Damien Le Moal , Hannes Reinecke , Keith Busch , Philipp Stanner Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fpga@vger.kernel.org, linux-gpio@vger.kernel.org, netdev@vger.kernel.org, linux-pci@vger.kernel.org, virtualization@lists.linux.dev, stable@vger.kernel.org, Christophe JAILLET Subject: [PATCH v4 6/7] vdpa: solidrun: Fix UB bug with devres Date: Tue, 27 Aug 2024 20:56:11 +0200 Message-ID: <20240827185616.45094-7-pstanner@redhat.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240827185616.45094-1-pstanner@redhat.com> References: <20240827185616.45094-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In psnet_open_pf_bar() and snet_open_vf_bar() a string later passed to pcim_iomap_regions() is placed on the stack. Neither pcim_iomap_regions() nor the functions it calls copy that string. Should the string later ever be used, this, consequently, causes undefined behavior since the stack frame will by then have disappeared. Fix the bug by allocating the strings on the heap through devm_kasprintf(). Cc: stable@vger.kernel.org # v6.3 Fixes: 51a8f9d7f587 ("virtio: vdpa: new SolidNET DPU driver.") Reported-by: Christophe JAILLET Closes: https://lore.kernel.org/all/74e9109a-ac59-49e2-9b1d-d825c9c9f891@wanadoo.fr/ Suggested-by: Andy Shevchenko Signed-off-by: Philipp Stanner --- drivers/vdpa/solidrun/snet_main.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/vdpa/solidrun/snet_main.c b/drivers/vdpa/solidrun/snet_main.c index 99428a04068d..c8b74980dbd1 100644 --- a/drivers/vdpa/solidrun/snet_main.c +++ b/drivers/vdpa/solidrun/snet_main.c @@ -555,7 +555,7 @@ static const struct vdpa_config_ops snet_config_ops = { static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet) { - char name[50]; + char *name; int ret, i, mask = 0; /* We don't know which BAR will be used to communicate.. * We will map every bar with len > 0. @@ -573,7 +573,10 @@ static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet) return -ENODEV; } - snprintf(name, sizeof(name), "psnet[%s]-bars", pci_name(pdev)); + name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "psnet[%s]-bars", pci_name(pdev)); + if (!name) + return -ENOMEM; + ret = pcim_iomap_regions(pdev, mask, name); if (ret) { SNET_ERR(pdev, "Failed to request and map PCI BARs\n"); @@ -590,10 +593,13 @@ static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet) static int snet_open_vf_bar(struct pci_dev *pdev, struct snet *snet) { - char name[50]; + char *name; int ret; - snprintf(name, sizeof(name), "snet[%s]-bar", pci_name(pdev)); + name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "snet[%s]-bars", pci_name(pdev)); + if (!name) + return -ENOMEM; + /* Request and map BAR */ ret = pcim_iomap_regions(pdev, BIT(snet->psnet->cfg.vf_bar), name); if (ret) {