[PATCH i-g-t v4 5/5] lib/gem_engine_topology: Fix broken compare of device links

Kamil Konieczny kamil.konieczny at linux.intel.com
Thu Jul 18 12:17:02 UTC 2024


Hi Janusz,
On 2024-07-18 at 10:55:16 +0200, Janusz Krzysztofik wrote:
> When looking for a primary counterpart of a render device, we compare
> "device" links of both nodes.  If those links point to the same device
> then we know we've found the correct primary node.
> 
> However, readlinkat() function we use doesn't explicitly terminate read in
> strings with null characters, and then the comparison occasionally fails.
> 
> Process the second (potential primary counterpart) node only if its
> "device" link is of the same length as that of the render node, and limit
> the number of compared characters to that length.
> 
> Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/6268
> Signed-off-by: Janusz Krzysztofik <janusz.krzysztofik at linux.intel.com>

Reviewed-by: Kamil Konieczny <kamil.konieczny at linux.intel.com>

> ---
>  lib/i915/gem_engine_topology.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/i915/gem_engine_topology.c b/lib/i915/gem_engine_topology.c
> index c8c1079223..c251060341 100644
> --- a/lib/i915/gem_engine_topology.c
> +++ b/lib/i915/gem_engine_topology.c
> @@ -467,7 +467,8 @@ static int __open_primary(int dir)
>  	if (minor < 64)
>  		return dir;
>  
> -	if (igt_debug_on(readlinkat(dir, "device", target, sizeof(target)) < 0))
> +	len = readlinkat(dir, "device", target, sizeof(target));
> +	if (igt_debug_on(len <= 0))
>  		return dir;
>  
>  	close(dir);
> @@ -477,8 +478,8 @@ static int __open_primary(int dir)
>  		if (dir < 0)
>  			continue;
>  
> -		if (readlinkat(dir, "device", device, sizeof(device)) > 0 &&
> -		    !strcmp(device, target))
> +		if (readlinkat(dir, "device", device, sizeof(device)) == len &&
> +		    !strncmp(device, target, len))
>  			break;
>  
>  		close(dir);
> -- 
> 2.45.2
> 


More information about the igt-dev mailing list