drm/amdgpu/aca: Robustness and safety fixes for ACA driver
Subsystems (2)
cover drm/amdgpu/aca: Robustness and safety fixes for ACA driver
This patch series addresses several race conditions, boundary check bugs, logic inversions, NULL pointer checks, and buffer size parameters across the AMDGPU ACA (Accelerated Compute Architecture) driver subsystem:
Sreeraj S Kurup (5):
drm/amdgpu/aca: Fix race condition and UAF in error cache logging drm/amdgpu/aca: Add upper bounds check in aca_bank_hwip_is_matched drm/amdgpu/aca: Fix inverted validation logic in
amdgpu_aca_get_error_data
drm/amdgpu/aca: Add missing NULL check for banks parameter in
aca_banks_add_bank
drm/amdgpu/aca: Fix off-by-one buffer size parameter in add_aca_sysfs
drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c | 31 ++++++++++--------------- 1 file changed, 12 insertions(+), 19 deletions(-)
Signature · 1 line
2.54.0
PATCH 1/5 drm/amdgpu/aca: Fix race condition and UAF in error cache logging
In aca_error_cache_log_bank_error(), find_bank_error() released aerr->lock prior to returning bank_error. This created a time-of-check to time-of-use (TOCTOU) race window where a concurrent caller of aca_log_aca_error() could acquire aerr->lock and free the bank_error node via aca_bank_error_remove().
When execution returned to aca_error_cache_log_bank_error(), incrementing bank_error->count resulted in a Use-After-Free and potential kernel memory corruption. Additionally, bank_error->count was updated outside mutex lock protection.
Fix this by acquiring aerr->lock at the start of aca_error_cache_log_bank_error() and holding it continuously across lookup, creation, and counter updates, while removing redundant internal lock acquisitions in helper functions.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c +7 -11
@@ -237,10 +237,8 @@ static struct aca_bank_error *new_bank_error(struct aca_error *aerr, struct aca_INIT_LIST_HEAD (&bank_error->node);memcpy (&bank_error->info, info,sizeof (*info)); -mutex_lock (&aerr->lock);list_add_tail (&bank_error->node, &aerr->list); aerr->nr_errors++; -mutex_unlock (&aerr->lock);return bank_error; }
@@ -249,22 +247,16 @@ static struct aca_bank_error *find_bank_error(struct aca_error *aerr, struct aca {struct aca_bank_error *bank_error =NULL ;struct aca_bank_info *tmp_info; -bool found = false; -mutex_lock (&aerr->lock);list_for_each_entry (bank_error, &aerr->list, node) { tmp_info = &bank_error->info;if (tmp_info->socket_id == info->socket_id && tmp_info->die_id == info->die_id) { - found = true; - goto out_unlock; +return bank_error; } } -out_unlock: -mutex_unlock (&aerr->lock); - -return found ? bank_error :NULL ; +return NULL ; }static void aca_bank_error_remove (struct aca_error *aerr,struct aca_bank_error *bank_error)
@@ -306,11 +298,15 @@ int aca_error_cache_log_bank_error(struct aca_handle *handle, struct aca_bank_inreturn 0 ; aerr = &error_cache->errors[type]; +mutex_lock (&aerr->lock); bank_error =get_bank_error (aerr, info); -if (!bank_error) +if (!bank_error) { +mutex_unlock (&aerr->lock);return -ENOMEM ; + } bank_error->count += count; +mutex_unlock (&aerr->lock);return 0 ; }
Signature · 1 line
2.54.0
PATCH 2/5 drm/amdgpu/aca: Add upper bounds check in aca_bank_hwip_is_matched
In reply to Sreeraj S KurupIn aca_bank_hwip_is_matched(), the 'type' parameter is used directly as an array index into aca_hwid_mcatypes[]. The function previously checked whether 'type' was equal to ACA_HWIP_TYPE_UNKNOW, but did not validate whether 'type' was less than ACA_HWIP_TYPE_COUNT or negative.
If an invalid or out-of-bounds enum value is passed, an out-of-bounds memory read occurs on the aca_hwid_mcatypes array.
Fix this by validating that 'type' is strictly greater than ACA_HWIP_TYPE_UNKNOW and less than ACA_HWIP_TYPE_COUNT before performing the array lookup.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c +1 -1
@@ -138,7 +138,7 @@ static bool aca_bank_hwip_is_matched(struct aca_bank *bank, enum aca_hwip_type tint hwid, mcatype;u64 ipid; -if (!bank || type ==ACA_HWIP_TYPE_UNKNOW ) +if (!bank || type <=ACA_HWIP_TYPE_UNKNOW || type >=ACA_HWIP_TYPE_COUNT )return false; hwip = &aca_hwid_mcatypes[type];
Signature · 1 line
2.54.0
PATCH 3/5 drm/amdgpu/aca: Fix inverted validation logic in amdgpu_aca_get_error_data
In reply to Sreeraj S Kurupaca_handle_is_valid() returned false if !list_empty(&handle->node) evaluated to true. Because active registered handles have non-empty nodes in the handle list, valid handles evaluated as invalid. Consequently, amdgpu_aca_get_error_data() returned -EOPNOTSUPP whenever aca_handle_is_valid() evaluated to true.
Fix the logic in aca_handle_is_valid() to verify that the handle is non-NULL, contains a valid mask, and is currently registered in the list. Negate the check in amdgpu_aca_get_error_data() so valid handles are processed properly.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c +2 -5
@@ -586,10 +586,7 @@ static int __aca_get_error_data(struct amdgpu_device *adev, struct aca_handle *hstatic bool aca_handle_is_valid (struct aca_handle *handle) { -if (!handle->mask || !list_empty (&handle->node)) -return false; - -return true; +return handle && handle->mask && !list_empty (&handle->node); } int amdgpu_aca_get_error_data(struct amdgpu_device *adev, struct aca_handle *handle,
@@ -599,7 +596,7 @@ int amdgpu_aca_get_error_data(struct amdgpu_device *adev, struct aca_handle *hanif (!handle || !err_data)return -EINVAL ; -if (aca_handle_is_valid (handle)) +if (!aca_handle_is_valid (handle))return -EOPNOTSUPP ;if ((type <0 ) || (!(BIT (type) & handle->mask)))
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 3/5] drm/amdgpu/aca: Fix inverted validation logic in amdgpu_aca_get_error_data
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] `aca_handle_is_valid()` incorrectly evaluates removed handles as valid, leading to a…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] aca_handle_is_valid() incorrectly evaluates removed handles as valid, leading to a Use-After-Free and mutex_lock on a destroyed mutex during device teardown. --
--- Patch [3]: [PATCH 3/5] drm/amdgpu/aca: Fix inverted validation logic in amdgpu_aca_get_error_data --- commit c1268f69334da0d7e73d9ad9988d74c06033118e
Author: Sreeraj S Kurup <[email protected]>
drm/amdgpu/aca: Fix inverted validation logic in amdgpu_aca_get_error_data
This commit updates aca_handle_is_valid() to verify handles have non-empty list nodes, and negates the check in amdgpu_aca_get_error_data() to process valid handles properly.
drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c static int __aca_get_error_data(struct amdgpu_device *adev, struct aca_handle *h · line 589
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c +1
-4@@ -586,10 +586,7 @@ static int __aca_get_error_data(struct amdgpu_device *adev, struct aca_handle *hstatic bool aca_handle_is_valid (struct aca_handle *handle) { -if (!handle->mask || !list_empty (&handle->node)) -return false; - -return true; +return handle && handle->mask && !list_empty (&handle->node);
[Severity: High]
Does this change cause removed handles to be evaluated as valid?
When amdgpu_aca_fini() removes handles, it uses list_del() rather than list_del_init():
drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c:remove_aca_handle() {
...
list_del(&handle->node);
...
}Because list_del() poisons the list node, list_empty(&handle->node) will evaluate to false. With this patch, !list_empty(&handle->node) now evaluates to true, causing aca_handle_is_valid() to return true for a removed handle. The original code correctly returned false for poisoned nodes.
During device teardown or module unload, if the delayed work amdgpu_ras_counte_dw() executes concurrently, it calls amdgpu_aca_get_error_data(). Since the removed handle is now incorrectly deemed valid, the code would proceed into __aca_get_error_data() and attempt to call mutex_lock(&aerr->lock) on an already destroyed mutex, potentially causing a use-after-free or kernel panic.
Should remove_aca_handle() use list_del_init() so that list_empty() can safely identify removed handles?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH 4/5 drm/amdgpu/aca: Add missing NULL check for banks parameter in aca_banks_add_bank
In reply to Sreeraj S Kurupaca_banks_add_bank() verified that the 'bank' parameter was non-NULL, but passed 'banks' directly into list_add_tail(&node->node, &banks->list) and incremented 'banks->nr_banks' without validating whether 'banks' was NULL.
Add a NULL check for 'banks' to prevent a kernel NULL pointer dereference if an invalid pointer is passed by a caller.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c +1 -1
@@ -49,7 +49,7 @@ static int aca_banks_add_bank(struct aca_banks *banks, struct aca_bank *bank) {struct aca_bank_node *node; -if (!bank) +if (!banks || !bank)return -EINVAL ; node =kvzalloc_obj (*node);
Signature · 1 line
2.54.0
PATCH 5/5 drm/amdgpu/aca: Fix off-by-one buffer size parameter in add_aca_sysfs
In reply to Sreeraj S Kurupsnprintf() guarantees NUL-termination within the size limit specified by its second argument. Passing 'sizeof(handle->attr_name) - 1' unnecessarily reduces the usable buffer capacity by one byte, causing sysfs attribute names to be truncated early.
Pass 'sizeof(handle->attr_name)' directly to snprintf() in add_aca_sysfs() to allow full use of the allocated buffer space.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c +1 -1
@@ -682,7 +682,7 @@ static int add_aca_sysfs(struct amdgpu_device *adev, struct aca_handle *handle) {struct device_attribute *aca_attr = &handle->aca_attr; -snprintf (handle->attr_name,sizeof (handle->attr_name) -1 ,"aca_%s" , handle->name); +snprintf (handle->attr_name,sizeof (handle->attr_name),"aca_%s" , handle->name); aca_attr->show = aca_sysfs_read; aca_attr->attr.name = handle->attr_name; aca_attr->attr.mode =S_IRUGO ;
Signature · 1 line
2.54.0