fix-buffer-overflows.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. Description: Fix buffer overflows when building strings
  2. The first occurs in lockfile_create_save_tmplock() when building the
  3. filename for the temporary lock file. The p buffer may not be large enough to
  4. hold large pid numbers or long hostnames. This issue is fixed by using
  5. snprintf(), rather than sprintf(), and adding appropriate field widths to the
  6. conversion string. Long hostnames will be truncated to fit in the remainder of
  7. the buffer length.
  8. .
  9. The second occurs in lockfile_create_save_tmplock() when buf is not long
  10. enough to store large pid numbers. This issue is fixed by using snprintf().
  11. Also, the length of buf is increased to 40, which is enough to hold a 128 bit
  12. signed int. This will be sufficient for holding pid values for quite some time
  13. into the future.
  14. .
  15. Additionally, the sprintf() in do_extern() is changed to snprintf() for
  16. general security hardening. An overflow of buf is not currently possible.
  17. Bug-Debian: http://bugs.debian.org/677225
  18. Bug-Ubuntu: https://launchpad.net/bugs/941968
  19. Bug-Ubuntu: https://launchpad.net/bugs/1011477
  20. Author: Tyler Hicks <tyhicks@canonical.com>
  21. Index: liblockfile-1.09/lockfile.c
  22. ===================================================================
  23. --- liblockfile-1.09.orig/lockfile.c 2013-01-09 10:54:49.948588615 -0800
  24. +++ liblockfile-1.09/lockfile.c 2013-01-09 12:19:07.328708811 -0800
  25. @@ -158,7 +158,7 @@
  26. if ((pid = fork()) < 0)
  27. return L_ERROR;
  28. if (pid == 0) {
  29. - sprintf(buf, "%d", retries % 1000);
  30. + snprintf(buf, sizeof(buf), "%d", retries % 1000);
  31. execl(LOCKPROG, LOCKPROG, opt, "-r", buf, "-q",
  32. (flags & L_PID) ? "-p" : "-N", lockfile, NULL);
  33. _exit(L_ERROR);
  34. @@ -185,6 +185,14 @@
  35. #endif
  36. +#define TMPLOCKSTR ".lk"
  37. +#define TMPLOCKSTRSZ strlen(TMPLOCKSTR)
  38. +#define TMPLOCKPIDSZ 5
  39. +#define TMPLOCKTIMESZ 1
  40. +#define TMPLOCKSYSNAMESZ 23
  41. +#define TMPLOCKFILENAMESZ (TMPLOCKSTRSZ + TMPLOCKPIDSZ + \
  42. + TMPLOCKTIMESZ + TMPLOCKSYSNAMESZ)
  43. +
  44. /*
  45. * Create a lockfile.
  46. */
  47. @@ -196,7 +204,7 @@
  48. {
  49. struct stat st, st1;
  50. char sysname[256];
  51. - char buf[8];
  52. + char buf[40];
  53. char *p;
  54. int sleeptime = 0;
  55. int statfailed = 0;
  56. @@ -209,13 +217,13 @@
  57. /*
  58. * Safety measure.
  59. */
  60. - if (strlen(lockfile) + 32 > MAXPATHLEN) {
  61. + if (strlen(lockfile) + TMPLOCKFILENAMESZ > MAXPATHLEN) {
  62. errno = ENAMETOOLONG;
  63. return L_ERROR;
  64. }
  65. #endif
  66. - if (strlen(lockfile) + 32 + 1 > tmplocksz) {
  67. + if (strlen(lockfile) + TMPLOCKFILENAMESZ + 1 > tmplocksz) {
  68. errno = EINVAL;
  69. return L_ERROR;
  70. }
  71. @@ -233,14 +241,16 @@
  72. return L_ERROR;
  73. if ((p = strchr(sysname, '.')) != NULL)
  74. *p = 0;
  75. - /* strcpy is safe: length-check above, limited at sprintf below */
  76. + /* strcpy is safe: length-check above, limited at snprintf below */
  77. strcpy(tmplock, lockfile);
  78. if ((p = strrchr(tmplock, '/')) == NULL)
  79. p = tmplock;
  80. else
  81. p++;
  82. - sprintf(p, ".lk%05d%x%s",
  83. - (int)getpid(), (int)time(NULL) & 15, sysname);
  84. + snprintf(p, TMPLOCKFILENAMESZ, "%s%0*d%0*x%s", TMPLOCKSTR,
  85. + TMPLOCKPIDSZ, (int)getpid(),
  86. + TMPLOCKTIMESZ, (int)time(NULL) & 15,
  87. + sysname);
  88. i = umask(022);
  89. fd = open(tmplock, O_WRONLY|O_CREAT|O_EXCL, 0644);
  90. e = errno;
  91. @@ -251,8 +261,8 @@
  92. return L_TMPLOCK;
  93. }
  94. if (flags & (L_PID | L_PPID)) {
  95. - sprintf(buf, "%d\n",
  96. - (flags & L_PID) ? (int)getpid() : (int)getppid());
  97. + snprintf(buf, sizeof(buf), "%d\n",
  98. + (flags & L_PID) ? (int)getpid() : (int)getppid());
  99. p = buf;
  100. len = strlen(buf);
  101. } else {
  102. @@ -363,7 +373,7 @@
  103. char *tmplock;
  104. int l, r, e;
  105. - l = strlen(lockfile)+32+1;
  106. + l = strlen(lockfile)+TMPLOCKFILENAMESZ+1;
  107. if ((tmplock = (char *)malloc(l)) == NULL)
  108. return L_ERROR;
  109. tmplock[0] = 0;