validate.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. //
  3. // Validation of USB-audio class descriptors
  4. //
  5. #include <linux/init.h>
  6. #include <linux/usb.h>
  7. #include <linux/usb/audio.h>
  8. #include <linux/usb/audio-v2.h>
  9. #include <linux/usb/audio-v3.h>
  10. #include <linux/usb/midi.h>
  11. #include "usbaudio.h"
  12. #include "helper.h"
  13. struct usb_desc_validator {
  14. unsigned char protocol;
  15. unsigned char type;
  16. bool (*func)(const void *p, const struct usb_desc_validator *v);
  17. size_t size;
  18. };
  19. #define UAC_VERSION_ALL (unsigned char)(-1)
  20. /* UAC1 only */
  21. static bool validate_uac1_header(const void *p,
  22. const struct usb_desc_validator *v)
  23. {
  24. const struct uac1_ac_header_descriptor *d = p;
  25. return d->bLength >= sizeof(*d) &&
  26. d->bLength >= sizeof(*d) + d->bInCollection;
  27. }
  28. /* for mixer unit; covering all UACs */
  29. static bool validate_mixer_unit(const void *p,
  30. const struct usb_desc_validator *v)
  31. {
  32. const struct uac_mixer_unit_descriptor *d = p;
  33. size_t len;
  34. if (d->bLength < sizeof(*d) || !d->bNrInPins)
  35. return false;
  36. len = sizeof(*d) + d->bNrInPins;
  37. /* We can't determine the bitmap size only from this unit descriptor,
  38. * so just check with the remaining length.
  39. * The actual bitmap is checked at mixer unit parser.
  40. */
  41. switch (v->protocol) {
  42. case UAC_VERSION_1:
  43. default:
  44. len += 2 + 1; /* wChannelConfig, iChannelNames */
  45. /* bmControls[n*m] */
  46. len += 1; /* iMixer */
  47. break;
  48. case UAC_VERSION_2:
  49. len += 4 + 1; /* bmChannelConfig, iChannelNames */
  50. /* bmMixerControls[n*m] */
  51. len += 1 + 1; /* bmControls, iMixer */
  52. break;
  53. case UAC_VERSION_3:
  54. len += 2; /* wClusterDescrID */
  55. /* bmMixerControls[n*m] */
  56. break;
  57. }
  58. return d->bLength >= len;
  59. }
  60. /* both for processing and extension units; covering all UACs */
  61. static bool validate_processing_unit(const void *p,
  62. const struct usb_desc_validator *v)
  63. {
  64. const struct uac_processing_unit_descriptor *d = p;
  65. const unsigned char *hdr = p;
  66. size_t len, m;
  67. if (d->bLength < sizeof(*d))
  68. return false;
  69. len = sizeof(*d) + d->bNrInPins;
  70. if (d->bLength < len)
  71. return false;
  72. switch (v->protocol) {
  73. case UAC_VERSION_1:
  74. default:
  75. /* bNrChannels, wChannelConfig, iChannelNames */
  76. len += 1 + 2 + 1;
  77. if (d->bLength < len + 1) /* bControlSize */
  78. return false;
  79. m = hdr[len];
  80. len += 1 + m + 1; /* bControlSize, bmControls, iProcessing */
  81. break;
  82. case UAC_VERSION_2:
  83. /* bNrChannels, bmChannelConfig, iChannelNames */
  84. len += 1 + 4 + 1;
  85. if (v->type == UAC2_PROCESSING_UNIT_V2)
  86. len += 2; /* bmControls -- 2 bytes for PU */
  87. else
  88. len += 1; /* bmControls -- 1 byte for EU */
  89. len += 1; /* iProcessing */
  90. break;
  91. case UAC_VERSION_3:
  92. /* wProcessingDescrStr, bmControls */
  93. len += 2 + 4;
  94. break;
  95. }
  96. if (d->bLength < len)
  97. return false;
  98. switch (v->protocol) {
  99. case UAC_VERSION_1:
  100. default:
  101. if (v->type == UAC1_EXTENSION_UNIT)
  102. return true; /* OK */
  103. switch (le16_to_cpu(d->wProcessType)) {
  104. case UAC_PROCESS_UP_DOWNMIX:
  105. case UAC_PROCESS_DOLBY_PROLOGIC:
  106. if (d->bLength < len + 1) /* bNrModes */
  107. return false;
  108. m = hdr[len];
  109. len += 1 + m * 2; /* bNrModes, waModes(n) */
  110. break;
  111. default:
  112. break;
  113. }
  114. break;
  115. case UAC_VERSION_2:
  116. if (v->type == UAC2_EXTENSION_UNIT_V2)
  117. return true; /* OK */
  118. switch (le16_to_cpu(d->wProcessType)) {
  119. case UAC2_PROCESS_UP_DOWNMIX:
  120. case UAC2_PROCESS_DOLBY_PROLOCIC: /* SiC! */
  121. if (d->bLength < len + 1) /* bNrModes */
  122. return false;
  123. m = hdr[len];
  124. len += 1 + m * 4; /* bNrModes, daModes(n) */
  125. break;
  126. default:
  127. break;
  128. }
  129. break;
  130. case UAC_VERSION_3:
  131. if (v->type == UAC3_EXTENSION_UNIT) {
  132. len += 2; /* wClusterDescrID */
  133. break;
  134. }
  135. switch (le16_to_cpu(d->wProcessType)) {
  136. case UAC3_PROCESS_UP_DOWNMIX:
  137. if (d->bLength < len + 1) /* bNrModes */
  138. return false;
  139. m = hdr[len];
  140. len += 1 + m * 2; /* bNrModes, waClusterDescrID(n) */
  141. break;
  142. case UAC3_PROCESS_MULTI_FUNCTION:
  143. len += 2 + 4; /* wClusterDescrID, bmAlgorighms */
  144. break;
  145. default:
  146. break;
  147. }
  148. break;
  149. }
  150. if (d->bLength < len)
  151. return false;
  152. return true;
  153. }
  154. /* both for selector and clock selector units; covering all UACs */
  155. static bool validate_selector_unit(const void *p,
  156. const struct usb_desc_validator *v)
  157. {
  158. const struct uac_selector_unit_descriptor *d = p;
  159. size_t len;
  160. if (d->bLength < sizeof(*d))
  161. return false;
  162. len = sizeof(*d) + d->bNrInPins;
  163. switch (v->protocol) {
  164. case UAC_VERSION_1:
  165. default:
  166. len += 1; /* iSelector */
  167. break;
  168. case UAC_VERSION_2:
  169. len += 1 + 1; /* bmControls, iSelector */
  170. break;
  171. case UAC_VERSION_3:
  172. len += 4 + 2; /* bmControls, wSelectorDescrStr */
  173. break;
  174. }
  175. return d->bLength >= len;
  176. }
  177. static bool validate_uac1_feature_unit(const void *p,
  178. const struct usb_desc_validator *v)
  179. {
  180. const struct uac_feature_unit_descriptor *d = p;
  181. if (d->bLength < sizeof(*d) || !d->bControlSize)
  182. return false;
  183. /* at least bmaControls(0) for master channel + iFeature */
  184. return d->bLength >= sizeof(*d) + d->bControlSize + 1;
  185. }
  186. static bool validate_uac2_feature_unit(const void *p,
  187. const struct usb_desc_validator *v)
  188. {
  189. const struct uac2_feature_unit_descriptor *d = p;
  190. if (d->bLength < sizeof(*d))
  191. return false;
  192. /* at least bmaControls(0) for master channel + iFeature */
  193. return d->bLength >= sizeof(*d) + 4 + 1;
  194. }
  195. static bool validate_uac3_feature_unit(const void *p,
  196. const struct usb_desc_validator *v)
  197. {
  198. const struct uac3_feature_unit_descriptor *d = p;
  199. if (d->bLength < sizeof(*d))
  200. return false;
  201. /* at least bmaControls(0) for master channel + wFeatureDescrStr */
  202. return d->bLength >= sizeof(*d) + 4 + 2;
  203. }
  204. static bool validate_midi_out_jack(const void *p,
  205. const struct usb_desc_validator *v)
  206. {
  207. const struct usb_midi_out_jack_descriptor *d = p;
  208. return d->bLength >= sizeof(*d) &&
  209. d->bLength >= sizeof(*d) + d->bNrInputPins * 2;
  210. }
  211. #define FIXED(p, t, s) { .protocol = (p), .type = (t), .size = sizeof(s) }
  212. #define FUNC(p, t, f) { .protocol = (p), .type = (t), .func = (f) }
  213. static const struct usb_desc_validator audio_validators[] = {
  214. /* UAC1 */
  215. FUNC(UAC_VERSION_1, UAC_HEADER, validate_uac1_header),
  216. FIXED(UAC_VERSION_1, UAC_INPUT_TERMINAL,
  217. struct uac_input_terminal_descriptor),
  218. FIXED(UAC_VERSION_1, UAC_OUTPUT_TERMINAL,
  219. struct uac1_output_terminal_descriptor),
  220. FUNC(UAC_VERSION_1, UAC_MIXER_UNIT, validate_mixer_unit),
  221. FUNC(UAC_VERSION_1, UAC_SELECTOR_UNIT, validate_selector_unit),
  222. FUNC(UAC_VERSION_1, UAC_FEATURE_UNIT, validate_uac1_feature_unit),
  223. FUNC(UAC_VERSION_1, UAC1_PROCESSING_UNIT, validate_processing_unit),
  224. FUNC(UAC_VERSION_1, UAC1_EXTENSION_UNIT, validate_processing_unit),
  225. /* UAC2 */
  226. FIXED(UAC_VERSION_2, UAC_HEADER, struct uac2_ac_header_descriptor),
  227. FIXED(UAC_VERSION_2, UAC_INPUT_TERMINAL,
  228. struct uac2_input_terminal_descriptor),
  229. FIXED(UAC_VERSION_2, UAC_OUTPUT_TERMINAL,
  230. struct uac2_output_terminal_descriptor),
  231. FUNC(UAC_VERSION_2, UAC_MIXER_UNIT, validate_mixer_unit),
  232. FUNC(UAC_VERSION_2, UAC_SELECTOR_UNIT, validate_selector_unit),
  233. FUNC(UAC_VERSION_2, UAC_FEATURE_UNIT, validate_uac2_feature_unit),
  234. /* UAC_VERSION_2, UAC2_EFFECT_UNIT: not implemented yet */
  235. FUNC(UAC_VERSION_2, UAC2_PROCESSING_UNIT_V2, validate_processing_unit),
  236. FUNC(UAC_VERSION_2, UAC2_EXTENSION_UNIT_V2, validate_processing_unit),
  237. FIXED(UAC_VERSION_2, UAC2_CLOCK_SOURCE,
  238. struct uac_clock_source_descriptor),
  239. FUNC(UAC_VERSION_2, UAC2_CLOCK_SELECTOR, validate_selector_unit),
  240. FIXED(UAC_VERSION_2, UAC2_CLOCK_MULTIPLIER,
  241. struct uac_clock_multiplier_descriptor),
  242. /* UAC_VERSION_2, UAC2_SAMPLE_RATE_CONVERTER: not implemented yet */
  243. /* UAC3 */
  244. FIXED(UAC_VERSION_2, UAC_HEADER, struct uac3_ac_header_descriptor),
  245. FIXED(UAC_VERSION_3, UAC_INPUT_TERMINAL,
  246. struct uac3_input_terminal_descriptor),
  247. FIXED(UAC_VERSION_3, UAC_OUTPUT_TERMINAL,
  248. struct uac3_output_terminal_descriptor),
  249. /* UAC_VERSION_3, UAC3_EXTENDED_TERMINAL: not implemented yet */
  250. FUNC(UAC_VERSION_3, UAC3_MIXER_UNIT, validate_mixer_unit),
  251. FUNC(UAC_VERSION_3, UAC3_SELECTOR_UNIT, validate_selector_unit),
  252. FUNC(UAC_VERSION_3, UAC_FEATURE_UNIT, validate_uac3_feature_unit),
  253. /* UAC_VERSION_3, UAC3_EFFECT_UNIT: not implemented yet */
  254. FUNC(UAC_VERSION_3, UAC3_PROCESSING_UNIT, validate_processing_unit),
  255. FUNC(UAC_VERSION_3, UAC3_EXTENSION_UNIT, validate_processing_unit),
  256. FIXED(UAC_VERSION_3, UAC3_CLOCK_SOURCE,
  257. struct uac3_clock_source_descriptor),
  258. FUNC(UAC_VERSION_3, UAC3_CLOCK_SELECTOR, validate_selector_unit),
  259. FIXED(UAC_VERSION_3, UAC3_CLOCK_MULTIPLIER,
  260. struct uac3_clock_multiplier_descriptor),
  261. /* UAC_VERSION_3, UAC3_SAMPLE_RATE_CONVERTER: not implemented yet */
  262. /* UAC_VERSION_3, UAC3_CONNECTORS: not implemented yet */
  263. { } /* terminator */
  264. };
  265. static const struct usb_desc_validator midi_validators[] = {
  266. FIXED(UAC_VERSION_ALL, USB_MS_HEADER,
  267. struct usb_ms_header_descriptor),
  268. FIXED(UAC_VERSION_ALL, USB_MS_MIDI_IN_JACK,
  269. struct usb_midi_in_jack_descriptor),
  270. FUNC(UAC_VERSION_ALL, USB_MS_MIDI_OUT_JACK,
  271. validate_midi_out_jack),
  272. { } /* terminator */
  273. };
  274. /* Validate the given unit descriptor, return true if it's OK */
  275. static bool validate_desc(unsigned char *hdr, int protocol,
  276. const struct usb_desc_validator *v)
  277. {
  278. if (hdr[1] != USB_DT_CS_INTERFACE)
  279. return true; /* don't care */
  280. for (; v->type; v++) {
  281. if (v->type == hdr[2] &&
  282. (v->protocol == UAC_VERSION_ALL ||
  283. v->protocol == protocol)) {
  284. if (v->func)
  285. return v->func(hdr, v);
  286. /* check for the fixed size */
  287. return hdr[0] >= v->size;
  288. }
  289. }
  290. return true; /* not matching, skip validation */
  291. }
  292. bool snd_usb_validate_audio_desc(void *p, int protocol)
  293. {
  294. unsigned char *c = p;
  295. bool valid;
  296. valid = validate_desc(p, protocol, audio_validators);
  297. if (!valid && snd_usb_skip_validation) {
  298. print_hex_dump(KERN_ERR, "USB-audio: buggy audio desc: ",
  299. DUMP_PREFIX_NONE, 16, 1, c, c[0], true);
  300. valid = true;
  301. }
  302. return valid;
  303. }
  304. bool snd_usb_validate_midi_desc(void *p)
  305. {
  306. unsigned char *c = p;
  307. bool valid;
  308. valid = validate_desc(p, UAC_VERSION_1, midi_validators);
  309. if (!valid && snd_usb_skip_validation) {
  310. print_hex_dump(KERN_ERR, "USB-audio: buggy midi desc: ",
  311. DUMP_PREFIX_NONE, 16, 1, c, c[0], true);
  312. valid = true;
  313. }
  314. return valid;
  315. }