Message ID | 20240206164543.46834-1-n.zhandarovich@fintech.ru |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-55333-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1668543dyb; Tue, 6 Feb 2024 08:46:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IG8AmtXwZ+auOEuklAHT+Astmk5NapBD1C7BBPkjKN3yzD7zhkrcOLBMWZtJAQzuj+ZsrY0 X-Received: by 2002:a05:6512:3d0f:b0:511:4d99:9ee with SMTP id d15-20020a0565123d0f00b005114d9909eemr2680206lfv.53.1707237973174; Tue, 06 Feb 2024 08:46:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707237973; cv=pass; d=google.com; s=arc-20160816; b=ckM+GZMy9/rD2dFuN42I76WtUVOWtul0Ts5dDBKt0KdkXvww8KADhD9S+wF2wx/YIi jEXjH5vmaQQr+Z4ZooCW236FJAuh6VJx/gv1lAiKZ3f0Lfm75WaCJIPtGFIt0gAI+xdw j+mEPmnAZ9pJIsusO3pXkEcicsGzoQ/GqmnR+Oz524ZNsjgs3zTZNcoPiQP66opFopr6 VKcK4NKWYwVpc3SWoQY7ZfJcOtuz5hkscFF0rpWKgpLBFVDJYVAbrHAVdgvM8xvjI5Ee 2kyUBU1ia4c0CnNbJX9xhlHS5FdmrRlj+E7uPZS7IU6NZFU2qRkwcQtBRz1SfOvchWWA NW6g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=8RXcL1anLYq/GaUr9DeUoiaQc/kdHrSxAgXBLmTHP3E=; fh=4XOjf1N/RBYFRB2zG8E6EV5YEcIUMVeKQ1Jq0+zGi+o=; b=JQA5oObRUIihyO5d3zEKZqwiq3kAi2XbhvGVTQTq4k/6y6uqS5hbnA02+9k7v2fngh 5wBsI+x4aC2pt2c9hje1eX41oJRaIKANriFZR22CQ3uMkl//yUvG3aRFvQSlt6r3Ir9e cWHEPKbgb9bCaPdvWoxLI+uP7qNYEoz6r0Zzl3KMKWgcicgunAOzKiu3YIUyClww4wiY dgxl37H2ggAzcKGrkb/6pIktQgwQ40JipsrAwEIZspgAw4J7/PjWmVnk5Uw77B1Pgy6d fEBT+rb3ECTrmUmfdexqkS6j9Au2BJftQVpKDa2vZopkjId8KRqiO4Baah+ljH4gGAA4 ReQg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=fintech.ru); spf=pass (google.com: domain of linux-kernel+bounces-55333-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55333-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCUlxjngSFtRAEqtCmO1aiT14gmeWj2j9ZpdqWNJJalrZFvyiQfndTQlPTBDZXRQOm+uJ+u/nU3OzBXWTMIuF+gUajtlcw== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id k6-20020a1709063e0600b00a379796643dsi1313378eji.367.2024.02.06.08.46.12 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 08:46:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55333-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=fintech.ru); spf=pass (google.com: domain of linux-kernel+bounces-55333-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55333-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id C0BBA1F26FCD for <ouuuleilei@gmail.com>; Tue, 6 Feb 2024 16:46:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 869EF7484; Tue, 6 Feb 2024 16:45:58 +0000 (UTC) Received: from exchange.fintech.ru (exchange.fintech.ru [195.54.195.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E20217C8 for <linux-kernel@vger.kernel.org>; Tue, 6 Feb 2024 16:45:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.54.195.159 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707237957; cv=none; b=deL2h3vWfps+jb/CY86Kid8iwe106UVsVcv41xZvCZVJ2eJNqeMRX3qdH5CYJnGdSgAjzqmvxRI2g9ZfpxorJ9UNMRHjeqSkPDqXJxK6/rJj1RhL88EI9gLKb5UiLzKP4dwfZuSbyhqSSmEihVqVWFptNFFwTL8wPLJeAK2NRyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707237957; c=relaxed/simple; bh=TR+lH7ISxKIARyqROnyJyvS+Y3ZOyzG8fwAigwII1l0=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=uvZlPqJEsCllGz8C0eRA/wkcwQ0OIMt4DYDHJSQLoDZhTxZ2kmhib5VEc7zEHGtbvVHVX/TqeQ9IjIVL9VdwWemvlzggzkMj5zxS1Eoem8btZ3ruKmNTEK/SWiniETS2VkQ346iJNceY4XOcr2v3KvJ9tDBwZB83Wy/2CqvXIJw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=fintech.ru; spf=pass smtp.mailfrom=fintech.ru; arc=none smtp.client-ip=195.54.195.159 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=fintech.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fintech.ru Received: from Ex16-01.fintech.ru (10.0.10.18) by exchange.fintech.ru (195.54.195.169) with Microsoft SMTP Server (TLS) id 14.3.498.0; Tue, 6 Feb 2024 19:45:48 +0300 Received: from localhost (10.0.253.138) by Ex16-01.fintech.ru (10.0.10.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.4; Tue, 6 Feb 2024 19:45:48 +0300 From: Nikita Zhandarovich <n.zhandarovich@fintech.ru> To: Jani Nikula <jani.nikula@linux.intel.com> CC: Nikita Zhandarovich <n.zhandarovich@fintech.ru>, Joonas Lahtinen <joonas.lahtinen@linux.intel.com>, Rodrigo Vivi <rodrigo.vivi@intel.com>, Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>, David Airlie <airlied@gmail.com>, Daniel Vetter <daniel@ffwll.ch>, <intel-gfx@lists.freedesktop.org>, <dri-devel@lists.freedesktop.org>, <linux-kernel@vger.kernel.org>, <lvc-project@linuxtesting.org> Subject: [PATCH] drm/i915/gt: Prevent possible NULL dereference in __caps_show() Date: Tue, 6 Feb 2024 08:45:43 -0800 Message-ID: <20240206164543.46834-1-n.zhandarovich@fintech.ru> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: Ex16-02.fintech.ru (10.0.10.19) To Ex16-01.fintech.ru (10.0.10.18) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790168765057338696 X-GMAIL-MSGID: 1790168765057338696 |
Series |
drm/i915/gt: Prevent possible NULL dereference in __caps_show()
|
|
Commit Message
Nikita Zhandarovich
Feb. 6, 2024, 4:45 p.m. UTC
After falling through the switch statement to default case 'repr' is
initialized with NULL, which will lead to incorrect dereference of
'!repr[n]' in the following loop.
Fix it with the help of an additional check for NULL.
Found by Linux Verification Center (linuxtesting.org) with static
analysis tool SVACE.
Fixes: 4ec76dbeb62b ("drm/i915/gt: Expose engine properties via sysfs")
Signed-off-by: Nikita Zhandarovich <n.zhandarovich@fintech.ru>
---
P.S. The NULL-deref problem might be dealt with this way but I am
not certain that the rest of the __caps_show() behaviour remains
correct if we end up in default case. For instance, as far as I
can tell, buf might turn out to be w/o '\0'. I could use some
direction if this has to be addressed as well.
drivers/gpu/drm/i915/gt/sysfs_engines.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
Hi, On 06/02/2024 16:45, Nikita Zhandarovich wrote: > After falling through the switch statement to default case 'repr' is > initialized with NULL, which will lead to incorrect dereference of > '!repr[n]' in the following loop. > > Fix it with the help of an additional check for NULL. > > Found by Linux Verification Center (linuxtesting.org) with static > analysis tool SVACE. > > Fixes: 4ec76dbeb62b ("drm/i915/gt: Expose engine properties via sysfs") > Signed-off-by: Nikita Zhandarovich <n.zhandarovich@fintech.ru> > --- > P.S. The NULL-deref problem might be dealt with this way but I am > not certain that the rest of the __caps_show() behaviour remains > correct if we end up in default case. For instance, as far as I > can tell, buf might turn out to be w/o '\0'. I could use some > direction if this has to be addressed as well. > > drivers/gpu/drm/i915/gt/sysfs_engines.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/gpu/drm/i915/gt/sysfs_engines.c b/drivers/gpu/drm/i915/gt/sysfs_engines.c > index 021f51d9b456..6b130b732867 100644 > --- a/drivers/gpu/drm/i915/gt/sysfs_engines.c > +++ b/drivers/gpu/drm/i915/gt/sysfs_engines.c > @@ -105,7 +105,7 @@ __caps_show(struct intel_engine_cs *engine, > > len = 0; > for_each_set_bit(n, &caps, show_unknown ? BITS_PER_LONG : count) { > - if (n >= count || !repr[n]) { > + if (n >= count || !repr || !repr[n]) { There are two input combinations to this function when repr is NULL. First is show_unknown=true and caps=0, which means the for_each_set_bit will not execute its body. (No bits set.) Second is show_unknown=false and caps=~0, which means count is zero so for_each_set_bit will again not run. (Bitfield size input param is zero.) So unless I am missing something I do not see the null pointer dereference. What could theoretically happen is that a third input combination appears, where caps is not zero in the show_unknown=true case, either via a fully un-handled engine->class (switch), or a new capability bit not added to the static array a bit above. That would assert during driver development here: if (GEM_WARN_ON(show_unknown)) Granted that could be after the dereference in "if (n >= count || !repr[n])", but would be caught in debug builds (CI) and therefore not be able to "ship" (get merge to the repo). Your second question is about empty buffer returned i.e. len=0 at the end of the function? (Which is when the buffer will not be null terminated - or you see another option?) That I think is safe too since it just results in a zero length read in sysfs. Regards, Tvrtko > if (GEM_WARN_ON(show_unknown)) > len += sysfs_emit_at(buf, len, "[%x] ", n); > } else {
Hello, On 2/7/24 01:16, Tvrtko Ursulin wrote: > > Hi, > > On 06/02/2024 16:45, Nikita Zhandarovich wrote: >> After falling through the switch statement to default case 'repr' is >> initialized with NULL, which will lead to incorrect dereference of >> '!repr[n]' in the following loop. >> >> Fix it with the help of an additional check for NULL. >> >> Found by Linux Verification Center (linuxtesting.org) with static >> analysis tool SVACE. >> >> Fixes: 4ec76dbeb62b ("drm/i915/gt: Expose engine properties via sysfs") >> Signed-off-by: Nikita Zhandarovich <n.zhandarovich@fintech.ru> >> --- >> P.S. The NULL-deref problem might be dealt with this way but I am >> not certain that the rest of the __caps_show() behaviour remains >> correct if we end up in default case. For instance, as far as I >> can tell, buf might turn out to be w/o '\0'. I could use some >> direction if this has to be addressed as well. >> >> drivers/gpu/drm/i915/gt/sysfs_engines.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/drivers/gpu/drm/i915/gt/sysfs_engines.c >> b/drivers/gpu/drm/i915/gt/sysfs_engines.c >> index 021f51d9b456..6b130b732867 100644 >> --- a/drivers/gpu/drm/i915/gt/sysfs_engines.c >> +++ b/drivers/gpu/drm/i915/gt/sysfs_engines.c >> @@ -105,7 +105,7 @@ __caps_show(struct intel_engine_cs *engine, >> len = 0; >> for_each_set_bit(n, &caps, show_unknown ? BITS_PER_LONG : count) { >> - if (n >= count || !repr[n]) { >> + if (n >= count || !repr || !repr[n]) { > > There are two input combinations to this function when repr is NULL. > > First is show_unknown=true and caps=0, which means the for_each_set_bit > will not execute its body. (No bits set.) > > Second is show_unknown=false and caps=~0, which means count is zero so > for_each_set_bit will again not run. (Bitfield size input param is zero.) > > So unless I am missing something I do not see the null pointer dereference. > > What could theoretically happen is that a third input combination > appears, where caps is not zero in the show_unknown=true case, either > via a fully un-handled engine->class (switch), or a new capability bit > not added to the static array a bit above. > > That would assert during driver development here: > > if (GEM_WARN_ON(show_unknown)) > > Granted that could be after the dereference in "if (n >= count || > !repr[n])", but would be caught in debug builds (CI) and therefore not > be able to "ship" (get merge to the repo). > > Your second question is about empty buffer returned i.e. len=0 at the > end of the function? (Which is when the buffer will not be null > terminated - or you see another option?) > > That I think is safe too since it just results in a zero length read in > sysfs. > > Regards, > > Tvrtko > >> if (GEM_WARN_ON(show_unknown)) >> len += sysfs_emit_at(buf, len, "[%x] ", n); >> } else { Thank you for such a full response. I think you are right. I was under the impression that either currently or in the future there might be an input combination, as you mentioned, that may trigger the NULL dereference. If you feel it will be caught beforehand, I am satisfied as well. Same goes for the empty buffer stuff. I think dropping the patch is the best option then. Apologies for any inconvenience. Nikita
diff --git a/drivers/gpu/drm/i915/gt/sysfs_engines.c b/drivers/gpu/drm/i915/gt/sysfs_engines.c index 021f51d9b456..6b130b732867 100644 --- a/drivers/gpu/drm/i915/gt/sysfs_engines.c +++ b/drivers/gpu/drm/i915/gt/sysfs_engines.c @@ -105,7 +105,7 @@ __caps_show(struct intel_engine_cs *engine, len = 0; for_each_set_bit(n, &caps, show_unknown ? BITS_PER_LONG : count) { - if (n >= count || !repr[n]) { + if (n >= count || !repr || !repr[n]) { if (GEM_WARN_ON(show_unknown)) len += sysfs_emit_at(buf, len, "[%x] ", n); } else {