util/build-id: check dlpi_name before strstr call
authorTapani Pälli <tapani.palli@intel.com>
Thu, 16 Mar 2017 06:10:57 +0000 (08:10 +0200)
committerTapani Pälli <tapani.palli@intel.com>
Fri, 17 Mar 2017 05:34:26 +0000 (07:34 +0200)
According to dl_iterate_phdr man page first object visited is the
main program where dlpi_name is an empty string. This fixes segfault
on Android when using build-id as identifier.

Fixes: d4fa083e11f ("util: Add utility build-id code.")
Signed-off-by: Tapani Pälli <tapani.palli@intel.com>
Reviewed-by: Plamena Manolova <plamena.manolova@intel.com>
Reviewed-by: Emil Velikov <emil.velikov@collabora.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
src/util/build_id.c

index c53e71ddfd1b07a656f804739178743f15bb8636..898a15f2b316702a219a1c1959fdb278200bdbcb 100644 (file)
@@ -55,6 +55,12 @@ build_id_find_nhdr_callback(struct dl_phdr_info *info, size_t size, void *data_)
 {
    struct callback_data *data = data_;
 
+   /* The first object visited by callback is the main program.
+    * Android's libc returns a NULL pointer for the first executable.
+    */
+   if (info->dlpi_name == NULL)
+      return 0;
+
    char *ptr = strstr(info->dlpi_name, data->filename);
    if (ptr == NULL || ptr[strlen(data->filename)] != '\0')
       return 0;