0009-nms_adpcm-fix-int-overflow-in-signal-estimate.patch 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. From 71565532463b22c24824101845a533a67bff4c9c Mon Sep 17 00:00:00 2001
  2. From: Alex Stewart <alex.stewart@ni.com>
  3. Date: Thu, 19 Oct 2023 14:07:19 -0400
  4. Subject: [PATCH] nms_adpcm: fix int overflow in signal estimate
  5. It is possible (though functionally incorrect) for the signal estimate
  6. calculation in nms_adpcm_update() to overflow the int value of s_e,
  7. resulting in undefined behavior.
  8. Since adpcm state signal values are never practically larger than
  9. 16 bits, use smaller numeric sizes throughout the file to avoid the
  10. overflow.
  11. CVE: CVE-2022-33065
  12. Fixes: https://github.com/libsndfile/libsndfile/issues/833
  13. Authored-by: Arthur Taylor <art@ified.ca>
  14. Signed-off-by: Alex Stewart <alex.stewart@ni.com>
  15. Upstream: https://github.com/libsndfile/libsndfile/commit/71565532463b22c24824101845a533a67bff4c9c
  16. [Peter: adjust for 1.2.2]
  17. Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
  18. ---
  19. src/nms_adpcm.c | 81 ++++++++++++++++++++++++-------------------------
  20. 1 file changed, 40 insertions(+), 41 deletions(-)
  21. diff --git a/src/nms_adpcm.c b/src/nms_adpcm.c
  22. index 5999be1f..dca85f0b 100644
  23. --- a/src/nms_adpcm.c
  24. +++ b/src/nms_adpcm.c
  25. @@ -48,36 +48,36 @@
  26. /* Variable names from ITU G.726 spec */
  27. struct nms_adpcm_state
  28. { /* Log of the step size multiplier. Operated on by codewords. */
  29. - int yl ;
  30. + short yl ;
  31. /* Quantizer step size multiplier. Generated from yl. */
  32. - int y ;
  33. + short y ;
  34. /* Coefficents of the pole predictor */
  35. - int a [2] ;
  36. + short a [2] ;
  37. /* Coefficents of the zero predictor */
  38. - int b [6] ;
  39. + short b [6] ;
  40. /* Previous quantized deltas (multiplied by 2^14) */
  41. - int d_q [7] ;
  42. + short d_q [7] ;
  43. /* d_q [x] + s_ez [x], used by the pole-predictor for signs only. */
  44. - int p [3] ;
  45. + short p [3] ;
  46. /* Previous reconstructed signal values. */
  47. - int s_r [2] ;
  48. + short s_r [2] ;
  49. /* Zero predictor components of the signal estimate. */
  50. - int s_ez ;
  51. + short s_ez ;
  52. /* Signal estimate, (including s_ez). */
  53. - int s_e ;
  54. + short s_e ;
  55. /* The most recent codeword (enc:generated, dec:inputted) */
  56. - int Ik ;
  57. + char Ik ;
  58. - int parity ;
  59. + char parity ;
  60. /*
  61. ** Offset into code tables for the bitrate.
  62. @@ -109,7 +109,7 @@ typedef struct
  63. } NMS_ADPCM_PRIVATE ;
  64. /* Pre-computed exponential interval used in the antilog approximation. */
  65. -static unsigned int table_expn [] =
  66. +static unsigned short table_expn [] =
  67. { 0x4000, 0x4167, 0x42d5, 0x444c, 0x45cb, 0x4752, 0x48e2, 0x4a7a,
  68. 0x4c1b, 0x4dc7, 0x4f7a, 0x5138, 0x52ff, 0x54d1, 0x56ac, 0x5892,
  69. 0x5a82, 0x5c7e, 0x5e84, 0x6096, 0x62b4, 0x64dd, 0x6712, 0x6954,
  70. @@ -117,21 +117,21 @@ static unsigned int table_expn [] =
  71. } ;
  72. /* Table mapping codewords to scale factor deltas. */
  73. -static int table_scale_factor_step [] =
  74. +static short table_scale_factor_step [] =
  75. { 0x0, 0x0, 0x0, 0x0, 0x4b0, 0x0, 0x0, 0x0, /* 2-bit */
  76. -0x3c, 0x0, 0x90, 0x0, 0x2ee, 0x0, 0x898, 0x0, /* 3-bit */
  77. -0x30, 0x12, 0x6b, 0xc8, 0x188, 0x2e0, 0x551, 0x1150, /* 4-bit */
  78. } ;
  79. /* Table mapping codewords to quantized delta interval steps. */
  80. -static unsigned int table_step [] =
  81. +static unsigned short table_step [] =
  82. { 0x73F, 0, 0, 0, 0x1829, 0, 0, 0, /* 2-bit */
  83. 0x3EB, 0, 0xC18, 0, 0x1581, 0, 0x226E, 0, /* 3-bit */
  84. 0x20C, 0x635, 0xA83, 0xF12, 0x1418, 0x19E3, 0x211A, 0x2BBA, /* 4-bit */
  85. } ;
  86. /* Binary search lookup table for quantizing using table_step. */
  87. -static int table_step_search [] =
  88. +static short table_step_search [] =
  89. { 0, 0x1F6D, 0, -0x1F6D, 0, 0, 0, 0, /* 2-bit */
  90. 0x1008, 0x1192, 0, -0x219A, 0x1656, -0x1656, 0, 0, /* 3-bit */
  91. 0x872, 0x1277, -0x8E6, -0x232B, 0xD06, -0x17D7, -0x11D3, 0, /* 4-bit */
  92. @@ -179,23 +179,23 @@ static sf_count_t nms_adpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
  93. ** Maps [1,20480] to [1,1024] in an exponential relationship. This is
  94. ** approximately ret = b^exp where b = e^(ln(1024)/ln(20480)) ~= 1.0003385
  95. */
  96. -static inline int
  97. -nms_adpcm_antilog (int exp)
  98. -{ int ret ;
  99. +static inline short
  100. +nms_adpcm_antilog (short exp)
  101. +{ int_fast32_t r ;
  102. - ret = 0x1000 ;
  103. - ret += (((exp & 0x3f) * 0x166b) >> 12) ;
  104. - ret *= table_expn [(exp & 0x7c0) >> 6] ;
  105. - ret >>= (26 - (exp >> 11)) ;
  106. + r = 0x1000 ;
  107. + r += (((int_fast32_t) (exp & 0x3f) * 0x166b) >> 12) ;
  108. + r *= table_expn [(exp & 0x7c0) >> 6] ;
  109. + r >>= (26 - (exp >> 11)) ;
  110. - return ret ;
  111. + return (short) r ;
  112. } /* nms_adpcm_antilog */
  113. static void
  114. nms_adpcm_update (struct nms_adpcm_state *s)
  115. { /* Variable names from ITU G.726 spec */
  116. - int a1ul ;
  117. - int fa1 ;
  118. + short a1ul, fa1 ;
  119. + int_fast32_t se ;
  120. int i ;
  121. /* Decay and Modify the scale factor in the log domain based on the codeword. */
  122. @@ -222,7 +222,7 @@ nms_adpcm_update (struct nms_adpcm_state *s)
  123. else if (fa1 > 256)
  124. fa1 = 256 ;
  125. - s->a [0] = (0xff * s->a [0]) >> 8 ;
  126. + s->a [0] = (s->a [0] * 0xff) >> 8 ;
  127. if (s->p [0] != 0 && s->p [1] != 0 && ((s->p [0] ^ s->p [1]) < 0))
  128. s->a [0] -= 192 ;
  129. else
  130. @@ -230,7 +230,7 @@ nms_adpcm_update (struct nms_adpcm_state *s)
  131. fa1 = -fa1 ;
  132. }
  133. - s->a [1] = fa1 + ((0xfe * s->a [1]) >> 8) ;
  134. + s->a [1] = fa1 + ((s->a [1] * 0xfe) >> 8) ;
  135. if (s->p [0] != 0 && s->p [2] != 0 && ((s->p [0] ^ s->p [2]) < 0))
  136. s->a [1] -= 128 ;
  137. else
  138. @@ -250,19 +250,18 @@ nms_adpcm_update (struct nms_adpcm_state *s)
  139. s->a [0] = a1ul ;
  140. } ;
  141. - /* Compute the zero predictor estimate. Rotate past deltas too. */
  142. - s->s_ez = 0 ;
  143. + /* Compute the zero predictor estimate and rotate past deltas. */
  144. + se = 0 ;
  145. for (i = 5 ; i >= 0 ; i--)
  146. - { s->s_ez += s->d_q [i] * s->b [i] ;
  147. + { se += (int_fast32_t) s->d_q [i] * s->b [i] ;
  148. s->d_q [i + 1] = s->d_q [i] ;
  149. } ;
  150. + s->s_ez = se >> 14 ;
  151. - /* Compute the signal estimate. */
  152. - s->s_e = s->a [0] * s->s_r [0] + s->a [1] * s->s_r [1] + s->s_ez ;
  153. -
  154. - /* Return to scale */
  155. - s->s_ez >>= 14 ;
  156. - s->s_e >>= 14 ;
  157. + /* Complete the signal estimate. */
  158. + se += (int_fast32_t) s->a [0] * s->s_r [0] ;
  159. + se += (int_fast32_t) s->a [1] * s->s_r [1] ;
  160. + s->s_e = se >> 14 ;
  161. /* Rotate members to prepare for next iteration. */
  162. s->s_r [1] = s->s_r [0] ;
  163. @@ -274,7 +273,7 @@ nms_adpcm_update (struct nms_adpcm_state *s)
  164. static int16_t
  165. nms_adpcm_reconstruct_sample (struct nms_adpcm_state *s, uint8_t I)
  166. { /* Variable names from ITU G.726 spec */
  167. - int dqx ;
  168. + int_fast32_t dqx ;
  169. /*
  170. ** The ordering of the 12-bit right-shift is a precision loss. It agrees
  171. @@ -308,17 +307,17 @@ nms_adpcm_codec_init (struct nms_adpcm_state *s, enum nms_enc_type type)
  172. /*
  173. ** nms_adpcm_encode_sample()
  174. **
  175. -** Encode a linear 16-bit pcm sample into a 2,3, or 4 bit NMS-ADPCM codeword
  176. +** Encode a linear 16-bit pcm sample into a 2, 3, or 4 bit NMS-ADPCM codeword
  177. ** using and updating the predictor state.
  178. */
  179. static uint8_t
  180. nms_adpcm_encode_sample (struct nms_adpcm_state *s, int16_t sl)
  181. { /* Variable names from ITU G.726 spec */
  182. - int d ;
  183. + int_fast32_t d ;
  184. uint8_t I ;
  185. /* Down scale the sample from 16 => ~14 bits. */
  186. - sl = (sl * 0x1fdf) / 0x7fff ;
  187. + sl = ((int_fast32_t) sl * 0x1fdf) / 0x7fff ;
  188. /* Compute estimate, and delta from actual value */
  189. nms_adpcm_update (s) ;
  190. @@ -407,7 +406,7 @@ nms_adpcm_encode_sample (struct nms_adpcm_state *s, int16_t sl)
  191. */
  192. static int16_t
  193. nms_adpcm_decode_sample (struct nms_adpcm_state *s, uint8_t I)
  194. -{ int sl ;
  195. +{ int_fast32_t sl ;
  196. nms_adpcm_update (s) ;
  197. sl = nms_adpcm_reconstruct_sample (s, I) ;
  198. --
  199. 2.39.5