Skip to contentSkip to conversation

xf86drm: Fix error handling for drmGetDevice()

PATCH In review Matt Roper 3 participants 6 messages Oct 16, 2015 – Oct 20, 2015

lore
Share & tools
lore

Matt Roper

author
#0 lore raw

PATCH 1/2 xf86drm: Fix error handling for drmGetDevice()

Some of the error conditions in drmGetDevice() can lead to us calling closedir(NULL) or leaking memory. Fix these conditions the same way we did for drmGetDevices() in commit:

commit 8c4a1cbd98bd8d185d489395f33302a17db643a9
Author: Matt Roper <[email protected]>
Date: Wed Sep 30 09:30:51 2015 -0700

xf86drm: Fix error handling for drmGetDevices()

---
 xf86drm.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
xf86drm.c +5 -5
index a29db42..951edbb 100644
@@ -3108,7 +3108,7 @@ int drmGetDevice(int fd, drmDevicePtr *device)
     sysdir = opendir(DRM_DIR_NAME);
     if (!sysdir) {
         ret = -errno;
-        goto close_sysdir;
+        goto free_locals;
     }
 
     i = 0;
@@ -3165,16 +3165,16 @@ int drmGetDevice(int fd, drmDevicePtr *device)
     for (i = 1; i < node_count && local_devices[i]; i++)
             drmFreeDevice(&local_devices[i]);
 
-    free(local_devices);
     closedir(sysdir);
+    free(local_devices);
     return 0;
 
 free_devices:
     drmFreeDevices(local_devices, i);
-    free(local_devices);
-
-close_sysdir:
     closedir(sysdir);
+
+free_locals:
+    free(local_devices);
     return ret;
 }
 
Signature · 6 lines
2.1.4

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
author
#1 lore raw

PATCH 2/2 xf86drm: Handle unrecognized subsystems safely in drmGetDevice[s]()

Both drmGetDevice() and drmGetDevices() currently print a warning when they encounter an unknown (non-PCI) subsystem type for a device node, but they still proceed to assume that the drmDevicePtr was initialized and try to add it to the local device array. Add a 'continue' to the error case handling to bypass the rest of the processing for devices we can't handle.

---
 xf86drm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xf86drm.c +2 -2
index 951edbb..7e28b4f 100644
@@ -3140,7 +3140,7 @@ int drmGetDevice(int fd, drmDevicePtr *device)
             break;
         default:
             fprintf(stderr, "The subsystem type is not supported yet\n");
-            break;
+            continue;
         }
 
         if (i >= max_count) {
@@ -3244,7 +3244,7 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)
             break;
         default:
             fprintf(stderr, "The subsystem type is not supported yet\n");
-            break;
+            continue;
         }
 
         if (i >= max_count) {
Signature · 6 lines
2.1.4

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH libdrm 2/2] xf86drm: Handle unrecognized subsystems safely in drmGetDevice[s]()

Quoted 9 lines · Matt Roper

Both drmGetDevice() and drmGetDevices() currently print a warning when they encounter an unknown (non-PCI) subsystem type for a device node, but they still proceed to assume that the drmDevicePtr was initialized and try to add it to the local device array. Add a 'continue' to the error case handling to bypass the rest of the processing for devices we can't handle.

Quoted 29 lines
---
 xf86drm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xf86drm.c +2 -3
index 951edbb..7e28b4f 100644
@@ -3140,7 +3140,7 @@ int drmGetDevice(int fd, drmDevicePtr *device)
             break;
         default:
             fprintf(stderr, "The subsystem type is not supported yet\n");
-            break;
+            continue;
         }
 
         if (i >= max_count) {
@@ -3244,7 +3244,7 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)
             break;
         default:
             fprintf(stderr, "The subsystem type is not supported yet\n");
-            break;
+            continue;
         }
 
         if (i >= max_count) {
--

2.1.4

Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH libdrm 2/2] xf86drm: Handle unrecognized subsystems safely in drmGetDevice[s]()

In reply to Matt Roper
Quoted 8 lines · Matt Roper

Both drmGetDevice() and drmGetDevices() currently print a warning when they encounter an unknown (non-PCI) subsystem type for a device node, but they still proceed to assume that the drmDevicePtr was initialized and try to add it to the local device array. Add a 'continue' to the error case handling to bypass the rest of the processing for devices we can't handle.

Cc Emil Velikov <[email protected]>

Looks like a left over as I moved the realloc() after the switch statement.

For the series:

Out of curiosity did you notice these while going through the code or do you actually have (work on) platform drm devices ? Can I volunteer you to add support for them ;-)

Thanks
Emil
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

author
#4 lore raw

Re: [PATCH libdrm 2/2] xf86drm: Handle unrecognized subsystems safely in drmGetDevice[s]()

Quoted 9 lines · Emil Velikov
Quoted 8 lines · Matt Roper

Both drmGetDevice() and drmGetDevices() currently print a warning when they encounter an unknown (non-PCI) subsystem type for a device node, but they still proceed to assume that the drmDevicePtr was initialized and try to add it to the local device array. Add a 'continue' to the error case handling to bypass the rest of the processing for devices we can't handle.

Cc Emil Velikov <[email protected]>
Quoted 8 lines

Looks like a left over as I moved the realloc() after the switch statement.

For the series:

Out of curiosity did you notice these while going through the code or do you actually have (work on) platform drm devices ? Can I volunteer you to add support for them ;-)

Naw, these were just caught by some static analysis tools we run internally. We're not doing anything with platform devices, but the whole libdrm source tree gets run through the tool, and I figured it was easy enough to just go ahead and write the fixes.

Matt

Quoted 3 lines

Thanks
Emil

Signature · 9 lines
Matt Roper
Graphics Software Engineer
IoTG Platform Enabling & Development
Intel Corporation
(916) 356-2795
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw

Re: [PATCH libdrm 2/2] xf86drm: Handle unrecognized subsystems safely in drmGetDevice[s]()

Quoted 15 lines · Matt Roper
Quoted 14 lines · Emil Velikov
Quoted 8 lines · Matt Roper

Both drmGetDevice() and drmGetDevices() currently print a warning when they encounter an unknown (non-PCI) subsystem type for a device node, but they still proceed to assume that the drmDevicePtr was initialized and try to add it to the local device array. Add a 'continue' to the error case handling to bypass the rest of the processing for devices we can't handle.

Cc Emil Velikov <[email protected]>

Looks like a left over as I moved the realloc() after the switch statement.

For the series:

Quoted 9 lines
Quoted 3 lines

Out of curiosity did you notice these while going through the code or do you actually have (work on) platform drm devices ? Can I volunteer you to add support for them ;-)

Naw, these were just caught by some static analysis tools we run internally. We're not doing anything with platform devices, but the whole libdrm source tree gets run through the tool, and I figured it was easy enough to just go ahead and write the fixes.

Unfortunate :( Fwiw if there are other issues and you don't have the time to look into them feel free to forward the summary (if possible of course).
These should be in master now. Thanks for the patches!

Emil
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel