[PATCH] lib/xe/xe_gt: Guard against fopen failure in xe_gt_stats_get_count

Kamil Konieczny kamil.konieczny at linux.intel.com
Wed Dec 4 12:51:29 UTC 2024


Hi Jonathan,
On 2024-12-03 at 20:26:00 +0000, Jonathan Cavitt wrote:
> The function xe_gt_stats_get_count currently performs an fopen on the
> /sys/kernel/debug/dri/0/gt*/stats directory without checking that the
> fopen is successful.  This can cause a system crash when trying to
> perform fgets on a NULL pointer on unsuccessful cases.
> 
> Check the results of fopen and guard against this.
> 
> Closes: https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/3212
> Signed-off-by: Jonathan Cavitt <jonathan.cavitt at intel.com>
> ---
>  lib/xe/xe_gt.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/lib/xe/xe_gt.c b/lib/xe/xe_gt.c
> index cee892af96..101b887a41 100644
> --- a/lib/xe/xe_gt.c
> +++ b/lib/xe/xe_gt.c
> @@ -159,6 +159,8 @@ int xe_gt_stats_get_count(int fd, int gt, const char *stat)
>  	sprintf(path, "/sys/kernel/debug/dri/0/gt%d/stats", gt);
>  	f = fopen(path, "r");
>  
> +	igt_assert_f(f, "Failed to open /sys/kernel/debug/dri/0/gt%d/stats", gt);

It turns SIGSEGV into igt assert fails, not best solution.
Could you just reuse solution from the same file above?

sprintf(path, "/sys/kernel/debug/dri/%d/gt%d/stats",
        minor(st.st_rdev), gt);

Btw there is also fclose() missing.

Regards,
Kamil

> +
>  	while (fgets(tlb_path, sizeof(tlb_path), f)) {
>  		if (strstr(tlb_path, stat) != NULL) {
>  			sscanf(tlb_path, "%*[^:]: %d", &count);
> -- 
> 2.43.0
> 


More information about the igt-dev mailing list