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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. From 37f0f1550e7822584b858edde416a694fb902236 Mon Sep 17 00:00:00 2001
  2. From: Ioana Ciornei <[email protected]>
  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 <[email protected]>
  13. ---
  14. restool.c | 44 ++++++++++++++++++++++++++++----------------
  15. 1 file changed, 28 insertions(+), 16 deletions(-)
  16. --- a/restool.c
  17. +++ b/restool.c
  18. @@ -1193,8 +1193,13 @@ out:
  19. static int get_device_file(void)
  20. {
  21. + int num_dev_files = 0;
  22. + struct dirent *dir;
  23. int error = 0;
  24. + char *device;
  25. int num_char;
  26. + long val;
  27. + DIR *d;
  28. memset(restool.device_file, '\0', DEV_FILE_SIZE);
  29. @@ -1222,10 +1227,6 @@ static int get_device_file(void)
  30. goto out;
  31. }
  32. } else {
  33. - DIR *d;
  34. - struct dirent *dir;
  35. - int num_dev_files = 0;
  36. - char *dprc_index;
  37. d = opendir("/dev");
  38. if (!d) {
  39. @@ -1235,26 +1236,34 @@ static int get_device_file(void)
  40. }
  41. while ((dir = readdir(d)) != NULL) {
  42. if (strncmp(dir->d_name, "dprc.", 5) == 0) {
  43. - dprc_index = &dir->d_name[5];
  44. - num_dev_files += 1;
  45. + if (num_dev_files == 0)
  46. + device = strdup(dir->d_name);
  47. + num_dev_files++;
  48. }
  49. }
  50. closedir(d);
  51. if (num_dev_files == 1) {
  52. - int temp_len = strlen(dprc_index);
  53. + errno = 0;
  54. + val = strtoul(&device[5], NULL, 0);
  55. + if ((errno == ERANGE && val == LONG_MAX) ||
  56. + ( errno != 0 && val == 0 )) {
  57. + ERROR_PRINTF("error: device file malformed\n");
  58. + error = -1;
  59. + goto out_free_device;;
  60. + }
  61. + restool.root_dprc_id = val;
  62. - temp_len += 10;
  63. - num_char = sprintf(restool.device_file, "/dev/dprc.%s",
  64. - dprc_index);
  65. - if (num_char != temp_len) {
  66. - ERROR_PRINTF("sprintf error\n");
  67. + num_char = snprintf(restool.device_file, DEV_FILE_SIZE,
  68. + "/dev/dprc.%d", restool.root_dprc_id);
  69. + if (num_char < 0 || num_char >= DEV_FILE_SIZE) {
  70. + ERROR_PRINTF("error: device file malformed\n");
  71. error = -1;
  72. - goto out;
  73. + goto out_free_device;
  74. }
  75. - restool.root_dprc_id = atoi(dprc_index);
  76. - if (access(restool.device_file, F_OK) != 0)
  77. - printf("no such dev file\n");
  78. + error = access(restool.device_file, F_OK);
  79. + if (error != 0)
  80. + ERROR_PRINTF("error: access(%s) = %d\n", restool.device_file, error);
  81. } else {
  82. error = -1;
  83. if (num_dev_files == 0)
  84. @@ -1263,6 +1272,9 @@ static int get_device_file(void)
  85. ERROR_PRINTF("error: multiple root containers\n");
  86. }
  87. }
  88. +
  89. +out_free_device:
  90. + free(device);
  91. out:
  92. return error;
  93. }