0003-more-strict-overflow-fixes.patch 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. From 7656b1be8dc5425d5af03ffa6af711599fc07e80 Mon Sep 17 00:00:00 2001
  2. From: Baruch Siach <baruch@tkos.co.il>
  3. Date: Tue, 22 Jan 2019 08:16:50 +0200
  4. Subject: [PATCH] buffer: Convert argc to size_t in ssh_buffer_unpack() as well
  5. Commit c306a693f3fb ("buffer: Use size_t for argc argument in
  6. ssh_buffer_(un)pack()") mentioned unpack in the commit log, but it only
  7. touches the pack variants. Extend the conversion to unpack.
  8. Pre-initialize the p pointer to avoid possible use before
  9. initialization in case of early argc check failure.
  10. This fixes build failure:
  11. .../libssh-0.8.6/src/buffer.c: In function 'ssh_buffer_unpack_va':
  12. .../libssh-0.8.6/src/buffer.c:1229:16: error: assuming signed overflow does not occur when simplifying conditional to constant [-Werror=strict-overflow]
  13. if (argc == -1){
  14. ^
  15. Signed-off-by: Baruch Siach <baruch@tkos.co.il>
  16. ---
  17. Upstream status: https://www.libssh.org/archive/libssh/2019-01/0000032.html
  18. include/libssh/buffer.h | 4 ++--
  19. src/buffer.c | 25 +++++++++++++------------
  20. 2 files changed, 15 insertions(+), 14 deletions(-)
  21. diff --git a/include/libssh/buffer.h b/include/libssh/buffer.h
  22. index 1c375343ee14..cd2dea6a7ecc 100644
  23. --- a/include/libssh/buffer.h
  24. +++ b/include/libssh/buffer.h
  25. @@ -50,11 +50,11 @@ int _ssh_buffer_pack(struct ssh_buffer_struct *buffer,
  26. _ssh_buffer_pack((buffer), (format), __VA_NARG__(__VA_ARGS__), __VA_ARGS__, SSH_BUFFER_PACK_END)
  27. int ssh_buffer_unpack_va(struct ssh_buffer_struct *buffer,
  28. - const char *format, int argc,
  29. + const char *format, size_t argc,
  30. va_list ap);
  31. int _ssh_buffer_unpack(struct ssh_buffer_struct *buffer,
  32. const char *format,
  33. - int argc,
  34. + size_t argc,
  35. ...);
  36. #define ssh_buffer_unpack(buffer, format, ...) \
  37. _ssh_buffer_unpack((buffer), (format), __VA_NARG__(__VA_ARGS__), __VA_ARGS__, SSH_BUFFER_PACK_END)
  38. diff --git a/src/buffer.c b/src/buffer.c
  39. index 99863747fc3c..c8ad20f24e43 100644
  40. --- a/src/buffer.c
  41. +++ b/src/buffer.c
  42. @@ -1082,11 +1082,11 @@ int _ssh_buffer_pack(struct ssh_buffer_struct *buffer,
  43. */
  44. int ssh_buffer_unpack_va(struct ssh_buffer_struct *buffer,
  45. const char *format,
  46. - int argc,
  47. + size_t argc,
  48. va_list ap)
  49. {
  50. int rc = SSH_ERROR;
  51. - const char *p, *last;
  52. + const char *p = format, *last;
  53. union {
  54. uint8_t *byte;
  55. uint16_t *word;
  56. @@ -1098,16 +1098,21 @@ int ssh_buffer_unpack_va(struct ssh_buffer_struct *buffer,
  57. } o;
  58. size_t len, rlen, max_len;
  59. va_list ap_copy;
  60. - int count; /* int for size comparison with argc */
  61. + size_t count;
  62. max_len = ssh_buffer_get_len(buffer);
  63. /* copy the argument list in case a rollback is needed */
  64. va_copy(ap_copy, ap);
  65. - for (p = format, count = 0; *p != '\0'; p++, count++) {
  66. + if (argc > 256) {
  67. + rc = SSH_ERROR;
  68. + goto cleanup;
  69. + }
  70. +
  71. + for (count = 0; *p != '\0'; p++, count++) {
  72. /* Invalid number of arguments passed */
  73. - if (argc != -1 && count > argc) {
  74. + if (count > argc) {
  75. rc = SSH_ERROR;
  76. goto cleanup;
  77. }
  78. @@ -1217,7 +1222,7 @@ int ssh_buffer_unpack_va(struct ssh_buffer_struct *buffer,
  79. }
  80. }
  81. - if (argc != -1 && argc != count) {
  82. + if (argc != count) {
  83. rc = SSH_ERROR;
  84. }
  85. @@ -1226,11 +1231,7 @@ cleanup:
  86. /* Check if our canary is intact, if not something really bad happened */
  87. uint32_t canary = va_arg(ap, uint32_t);
  88. if (canary != SSH_BUFFER_PACK_END){
  89. - if (argc == -1){
  90. - rc = SSH_ERROR;
  91. - } else {
  92. - abort();
  93. - }
  94. + abort();
  95. }
  96. }
  97. @@ -1320,7 +1321,7 @@ cleanup:
  98. */
  99. int _ssh_buffer_unpack(struct ssh_buffer_struct *buffer,
  100. const char *format,
  101. - int argc,
  102. + size_t argc,
  103. ...)
  104. {
  105. va_list ap;
  106. --
  107. 2.20.1