From patchwork Wed Oct 21 18:43:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khalid Elmously X-Patchwork-Id: 1385700 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CGfZB04Jyz9sRR; Thu, 22 Oct 2020 05:45:21 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1kVJ6n-0002wx-FH; Wed, 21 Oct 2020 18:45:13 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1kVJ6m-0002wr-8t for kernel-team@lists.ubuntu.com; Wed, 21 Oct 2020 18:45:12 +0000 Received: from mail-qt1-f197.google.com ([209.85.160.197]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1kVJ6l-0000Sa-Uj for kernel-team@lists.ubuntu.com; Wed, 21 Oct 2020 18:45:12 +0000 Received: by mail-qt1-f197.google.com with SMTP id o25so2359470qtt.3 for ; Wed, 21 Oct 2020 11:45:11 -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=Up5s/oEcXckcPjolMCSVi+jbd+YnQhQHsUhKSDgEYjk=; b=oU5V28oxCcG7vVHLV4H+HCo8CxInkNlzJIstpH8V34SiiUiRtt6DD4rK7o3d6Ue+5e 9Q6AKm29NWmjLzTrpD8l3+HoEROdqtNcoqat7lGY/YVuYCaoibNHPnwyG9urtLEa/xms Xfw6KaV3ZQ9QkusAToJ5k9Rk6BA3AMd0tBN7NLxH5V4ZS4SWhHGQ1boYgn4HN61pdNBh D8V2mbTBXaFv+W7gSrJ2EKFYm2MFj6umV9Cm9tAwvdfT6Kjnn/QJzcaXOLl+T2WE1u9K rRZmL/LbFilN0/v62k7NuSB6hOAvgWqmNK2EUjJJeuJdsNX0/hJZxl26Wj+1Io2wojho J3/Q== X-Gm-Message-State: AOAM533M0411RTdl092natpAYCei22pSwolq9eHYggk5b2XADNQHAxa5 OvI4+X/K1FpmEYu4W8rwyNLjKlRU2ssHhQV0deCIFfNav1dbkU6UXla3dJK5l+RhOy6ir67yelx bX37A9PPbWvkkeKiUfp3iqLHCXfbPel+nN/pCx8l/HQ== X-Received: by 2002:a05:620a:1187:: with SMTP id b7mr4595621qkk.212.1603305910604; Wed, 21 Oct 2020 11:45:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzRIfpEc2xcPYxlRj5Gu/fYj+VE5UDSpx+uTyEY/lG48uwUboDsu6TroWynn6AFbxfd2qb0lQ== X-Received: by 2002:a05:620a:1187:: with SMTP id b7mr4595587qkk.212.1603305910202; Wed, 21 Oct 2020 11:45:10 -0700 (PDT) Received: from kbuntu2.fuzzbuzz.org (dhcp-24-53-242-104.cable.user.start.ca. [24.53.242.104]) by smtp.gmail.com with ESMTPSA id e1sm1060368qkm.35.2020.10.21.11.45.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Oct 2020 11:45:09 -0700 (PDT) From: Khalid Elmously To: kernel-team@lists.ubuntu.com Subject: [SRU][X][PATCH 1/1] pinctrl: devicetree: Avoid taking direct reference to device name string Date: Wed, 21 Oct 2020 14:43:51 -0400 Message-Id: <20201021184351.17328-1-khalid.elmously@canonical.com> X-Mailer: git-send-email 2.17.1 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Will Deacon CVE-2020-0427 When populating the pinctrl mapping table entries for a device, the 'dev_name' field for each entry is initialised to point directly at the string returned by 'dev_name()' for the device and subsequently used by 'create_pinctrl()' when looking up the mappings for the device being probed. This is unreliable in the presence of calls to 'dev_set_name()', which may reallocate the device name string leaving the pinctrl mappings with a dangling reference. This then leads to a use-after-free every time the name is dereferenced by a device probe: | BUG: KASAN: invalid-access in strcmp+0x20/0x64 | Read of size 1 at addr 13ffffc153494b00 by task modprobe/590 | Pointer tag: [13], memory tag: [fe] | | Call trace: | __kasan_report+0x16c/0x1dc | kasan_report+0x10/0x18 | check_memory_region | __hwasan_load1_noabort+0x4c/0x54 | strcmp+0x20/0x64 | create_pinctrl+0x18c/0x7f4 | pinctrl_get+0x90/0x114 | devm_pinctrl_get+0x44/0x98 | pinctrl_bind_pins+0x5c/0x450 | really_probe+0x1c8/0x9a4 | driver_probe_device+0x120/0x1d8 Follow the example of sysfs, and duplicate the device name string before stashing it away in the pinctrl mapping entries. Cc: Linus Walleij Reported-by: Elena Petrova Tested-by: Elena Petrova Signed-off-by: Will Deacon Link: https://lore.kernel.org/r/20191002124206.22928-1-will@kernel.org Signed-off-by: Linus Walleij (backported from commit be4c60b563edee3712d392aaeb0943a768df7023) [ kmously: Extra dev_err() in dt_remember_or_free_map() required manual merging ] Signed-off-by: Khalid Elmously Acked-by: Stefan Bader Acked-by: Andrea Righi --- drivers/pinctrl/devicetree.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/drivers/pinctrl/devicetree.c b/drivers/pinctrl/devicetree.c index fe04e748dfe4b..74eebcbd4e057 100644 --- a/drivers/pinctrl/devicetree.c +++ b/drivers/pinctrl/devicetree.c @@ -40,6 +40,13 @@ struct pinctrl_dt_map { static void dt_free_map(struct pinctrl_dev *pctldev, struct pinctrl_map *map, unsigned num_maps) { + int i; + + for (i = 0; i < num_maps; ++i) { + kfree_const(map[i].dev_name); + map[i].dev_name = NULL; + } + if (pctldev) { const struct pinctrl_ops *ops = pctldev->desc->pctlops; ops->dt_free_map(pctldev, map, num_maps); @@ -73,7 +80,13 @@ static int dt_remember_or_free_map(struct pinctrl *p, const char *statename, /* Initialize common mapping table entry fields */ for (i = 0; i < num_maps; i++) { - map[i].dev_name = dev_name(p->dev); + const char *devname; + + devname = kstrdup_const(dev_name(p->dev), GFP_KERNEL); + if (!devname) + goto err_free_map; + + map[i].dev_name = devname; map[i].name = statename; if (pctldev) map[i].ctrl_dev_name = dev_name(pctldev->dev); @@ -83,8 +96,7 @@ static int dt_remember_or_free_map(struct pinctrl *p, const char *statename, dt_map = kzalloc(sizeof(*dt_map), GFP_KERNEL); if (!dt_map) { dev_err(p->dev, "failed to alloc struct pinctrl_dt_map\n"); - dt_free_map(pctldev, map, num_maps); - return -ENOMEM; + goto err_free_map; } dt_map->pctldev = pctldev; @@ -93,6 +105,10 @@ static int dt_remember_or_free_map(struct pinctrl *p, const char *statename, list_add_tail(&dt_map->node, &p->dt_maps); return pinctrl_register_map(map, num_maps, false); + +err_free_map: + dt_free_map(pctldev, map, num_maps); + return -ENOMEM; } struct pinctrl_dev *of_pinctrl_get(struct device_node *np)