0001-restool-fix-get_device_file-function.patch 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. From 37f0f1550e7822584b858edde416a694fb902236 Mon Sep 17 00:00:00 2001
  2. From: Ioana Ciornei <ioana.ciornei@nxp.com>
  3. Date: Tue, 31 Jul 2018 13:33:20 +0300
  4. Subject: [PATCH] restool: fix get_device_file() function
  5. This patch fixes multiple problems encountered in the
  6. get_device_file() function:
  7. - The deprecated atoi() function is replaced by strtoul
  8. - An invalid memory access was being performed by using
  9. memory from dir->d_name even after closedir(). This is
  10. fixed by a strdup() on the device filename.
  11. - Also, error prints now print any relevant error code.
  12. Signed-off-by: Ioana Ciornei <ioana.ciornei@nxp.com>
  13. ---
  14. restool.c | 44 ++++++++++++++++++++++++++++----------------
  15. 1 file changed, 28 insertions(+), 16 deletions(-)
  16. diff --git a/restool.c b/restool.c
  17. index 7553659..78fd1bf 100644
  18. --- a/restool.c
  19. +++ b/restool.c
  20. @@ -1185,8 +1185,13 @@ out:
  21. static int get_device_file(void)
  22. {
  23. + int num_dev_files = 0;
  24. + struct dirent *dir;
  25. int error = 0;
  26. + char *device;
  27. int num_char;
  28. + long val;
  29. + DIR *d;
  30. memset(restool.device_file, '\0', DEV_FILE_SIZE);
  31. @@ -1214,10 +1219,6 @@ static int get_device_file(void)
  32. goto out;
  33. }
  34. } else {
  35. - DIR *d;
  36. - struct dirent *dir;
  37. - int num_dev_files = 0;
  38. - char *dprc_index;
  39. d = opendir("/dev");
  40. if (!d) {
  41. @@ -1227,26 +1228,34 @@ static int get_device_file(void)
  42. }
  43. while ((dir = readdir(d)) != NULL) {
  44. if (strncmp(dir->d_name, "dprc.", 5) == 0) {
  45. - dprc_index = &dir->d_name[5];
  46. - num_dev_files += 1;
  47. + if (num_dev_files == 0)
  48. + device = strdup(dir->d_name);
  49. + num_dev_files++;
  50. }
  51. }
  52. closedir(d);
  53. if (num_dev_files == 1) {
  54. - int temp_len = strlen(dprc_index);
  55. + errno = 0;
  56. + val = strtoul(&device[5], NULL, 0);
  57. + if ((errno == ERANGE && val == LONG_MAX) ||
  58. + ( errno != 0 && val == 0 )) {
  59. + ERROR_PRINTF("error: device file malformed\n");
  60. + error = -1;
  61. + goto out_free_device;;
  62. + }
  63. + restool.root_dprc_id = val;
  64. - temp_len += 10;
  65. - num_char = sprintf(restool.device_file, "/dev/dprc.%s",
  66. - dprc_index);
  67. - if (num_char != temp_len) {
  68. - ERROR_PRINTF("sprintf error\n");
  69. + num_char = snprintf(restool.device_file, DEV_FILE_SIZE,
  70. + "/dev/dprc.%d", restool.root_dprc_id);
  71. + if (num_char < 0 || num_char >= DEV_FILE_SIZE) {
  72. + ERROR_PRINTF("error: device file malformed\n");
  73. error = -1;
  74. - goto out;
  75. + goto out_free_device;
  76. }
  77. - restool.root_dprc_id = atoi(dprc_index);
  78. - if (access(restool.device_file, F_OK) != 0)
  79. - printf("no such dev file\n");
  80. + error = access(restool.device_file, F_OK);
  81. + if (error != 0)
  82. + ERROR_PRINTF("error: access(%s) = %d\n", restool.device_file, error);
  83. } else {
  84. error = -1;
  85. if (num_dev_files == 0)
  86. @@ -1255,6 +1264,9 @@ static int get_device_file(void)
  87. ERROR_PRINTF("error: multiple root containers\n");
  88. }
  89. }
  90. +
  91. +out_free_device:
  92. + free(device);
  93. out:
  94. return error;
  95. }
  96. --
  97. 2.17.1