nvclock-0.8_p20110102-buffers.patch 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. https://bugs.gentoo.org/364667
  2. clean up random buffer overflows in the code
  3. --- a/src/backend/back_linux.c
  4. +++ b/src/backend/back_linux.c
  5. @@ -42,7 +42,9 @@ static int probe_devices();
  6. static int check_driver()
  7. {
  8. FILE *proc;
  9. - char buffer[80];
  10. + char *buffer = NULL;
  11. + size_t len;
  12. + int ret = 0;
  13. proc = fopen("/proc/modules", "r");
  14. @@ -50,9 +52,9 @@ static int check_driver()
  15. if(proc == NULL)
  16. return 0;
  17. - while(fgets(buffer, 80, proc) != NULL)
  18. + while(getline(&buffer, &len, proc) != -1)
  19. {
  20. - char name[80];
  21. + char name[100];
  22. int size;
  23. int used;
  24. @@ -64,26 +66,23 @@ static int check_driver()
  25. {
  26. if(strcmp(name, "NVdriver") == 0)
  27. {
  28. - fclose(proc);
  29. if(used)
  30. - return 1;
  31. -
  32. - return 0;
  33. + ret = 1;
  34. + break;
  35. }
  36. if(strcmp(name, "nvidia") == 0)
  37. {
  38. - fclose(proc);
  39. if(used)
  40. - return 2;
  41. -
  42. - return 0;
  43. + ret = 2;
  44. + break;
  45. }
  46. }
  47. }
  48. + free(buffer);
  49. fclose(proc);
  50. - return 0;
  51. + return ret;
  52. }
  53. int init_nvclock()
  54. @@ -114,7 +113,8 @@ static int probe_devices()
  55. {
  56. int dev, irq, reg_addr, i=0;
  57. unsigned short devbusfn;
  58. - char buf[256];
  59. + char *buf = NULL;
  60. + size_t len;
  61. FILE *proc;
  62. proc = fopen("/proc/bus/pci/devices", "r");
  63. @@ -124,7 +124,7 @@ static int probe_devices()
  64. return 0;
  65. }
  66. - while(fgets(buf, sizeof(buf)-1, proc))
  67. + while(getline(&buf, &len, proc) != -1)
  68. {
  69. if(sscanf(buf,"%hx %x %x %x",&devbusfn, &dev, &irq, &reg_addr) != 4) continue;
  70. @@ -181,6 +181,7 @@ static int probe_devices()
  71. i++;
  72. }
  73. }
  74. + free(buf);
  75. fclose(proc);
  76. if(i==0)
  77. --- a/src/backend/config.c
  78. +++ b/src/backend/config.c
  79. @@ -286,8 +286,9 @@ int open_config()
  80. {
  81. if(mkdir(nvclock.path, 0755))
  82. {
  83. - char buf[80];
  84. - sprintf(buf, "Can't create '%s'. Do you have sufficient permissions?\n", nvclock.path);
  85. + char buf[1024];
  86. + snprintf(buf, sizeof(buf), "Can't create path (do you have sufficient permissions?): %s", nvclock.path);
  87. + buf[sizeof(buf) - 1] = '\0';
  88. set_error_str(buf);
  89. return 0;
  90. }
  91. @@ -297,8 +298,9 @@ int open_config()
  92. /* Check if .nvclock really is a directory. For some users it was a file and this led to a segfault. */
  93. if(!S_ISDIR(stat_buf.st_mode))
  94. {
  95. - char buf[80];
  96. - sprintf(buf, "Can't open '%s'. Is it really a directory?\n", nvclock.path);
  97. + char buf[1024];
  98. + snprintf(buf, sizeof(buf), "Can't open path (Is it really a directory?): %s", nvclock.path);
  99. + buf[sizeof(buf) - 1] = '\0';
  100. set_error_str(buf);
  101. return 0;
  102. }
  103. --- a/src/backend/error.c
  104. +++ b/src/backend/error.c
  105. @@ -41,23 +41,19 @@ void set_error_str(const char *str)
  106. }
  107. -char *get_error(char *buf, int size)
  108. +const char *get_error(void)
  109. {
  110. switch(nvclock.nv_errno)
  111. {
  112. case NV_ERR_NO_DEVICES_FOUND:
  113. - strcpy(buf, "No nvidia cards found in your system!");
  114. - break;
  115. + return "No nvidia cards found in your system!";
  116. case NV_ERR_NO_DRIVERS_FOUND:
  117. - strcpy(buf, "You don't have enough permissions to run NVClock! Retry as root or install the Nvidia drivers.");
  118. - break;
  119. + return "You don't have enough permissions to run NVClock! Retry as root or install the Nvidia drivers.";
  120. case NV_ERR_NOT_ENOUGH_PERMISSIONS:
  121. - strcpy(buf, "You don't have enough permissions to run NVClock! Retry as root.");
  122. - break;
  123. + return "You don't have enough permissions to run NVClock! Retry as root.";
  124. case NV_ERR_OTHER:
  125. - strcpy(buf, nvclock.nv_err_str);
  126. - break;
  127. + return nvclock.nv_err_str;
  128. }
  129. - return buf;
  130. + return "unknown";
  131. }
  132. --- a/src/backend/nvclock.h
  133. +++ b/src/backend/nvclock.h
  134. @@ -335,7 +335,7 @@ cfg_entry* lookup_entry(cfg_entry **cfg, char *section, char *name);
  135. void destroy(cfg_entry **cfg);
  136. /* error handling */
  137. -char *get_error(char *buf, int size);
  138. +const char *get_error(void);
  139. void set_error(int code);
  140. void set_error_str(const char *err);
  141. --- a/src/gtk/main.c
  142. +++ b/src/gtk/main.c
  143. @@ -264,8 +264,7 @@ int main (int argc, char *argv[])
  144. */
  145. if(!init_nvclock())
  146. {
  147. - char buf[80];
  148. - GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", get_error(buf, 80));
  149. + GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", get_error());
  150. gtk_dialog_run(GTK_DIALOG(dialog));
  151. g_signal_connect_swapped(GTK_OBJECT(dialog), "response", G_CALLBACK(gtk_widget_destroy), GTK_OBJECT(dialog));
  152. gtk_widget_destroy(dialog);
  153. --- a/src/nvclock.c
  154. +++ b/src/nvclock.c
  155. @@ -604,8 +604,7 @@ int main(int argc, char *argv[])
  156. if(!init_nvclock())
  157. {
  158. - char buf[80];
  159. - fprintf(stderr, "Error: %s\n", get_error(buf, 80));
  160. + fprintf(stderr, "Error: %s\n", get_error());
  161. return 0;
  162. }
  163. @@ -851,8 +850,7 @@ int main(int argc, char *argv[])
  164. {
  165. if(!set_card(i))
  166. {
  167. - char buf[80];
  168. - fprintf(stderr, "Error: %s\n", get_error(buf, 80));
  169. + fprintf(stderr, "Error: %s\n", get_error());
  170. return 0;
  171. }
  172. @@ -887,8 +885,7 @@ int main(int argc, char *argv[])
  173. /* set the card object to the requested card */
  174. if(!set_card(card_number))
  175. {
  176. - char buf[80];
  177. - fprintf(stderr, "Error: %s\n", get_error(buf, 80));
  178. + fprintf(stderr, "Error: %s\n", get_error());
  179. return 0;
  180. }