control_compat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * compat ioctls for control API
  4. *
  5. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  6. */
  7. /* this file included from control.c */
  8. #include <linux/compat.h>
  9. #include <linux/slab.h>
  10. struct snd_ctl_elem_list32 {
  11. u32 offset;
  12. u32 space;
  13. u32 used;
  14. u32 count;
  15. u32 pids;
  16. unsigned char reserved[50];
  17. } /* don't set packed attribute here */;
  18. static int snd_ctl_elem_list_compat(struct snd_card *card,
  19. struct snd_ctl_elem_list32 __user *data32)
  20. {
  21. struct snd_ctl_elem_list data = {};
  22. compat_caddr_t ptr;
  23. int err;
  24. /* offset, space, used, count */
  25. if (copy_from_user(&data, data32, 4 * sizeof(u32)))
  26. return -EFAULT;
  27. /* pids */
  28. if (get_user(ptr, &data32->pids))
  29. return -EFAULT;
  30. data.pids = compat_ptr(ptr);
  31. err = snd_ctl_elem_list(card, &data);
  32. if (err < 0)
  33. return err;
  34. /* copy the result */
  35. if (copy_to_user(data32, &data, 4 * sizeof(u32)))
  36. return -EFAULT;
  37. return 0;
  38. }
  39. /*
  40. * control element info
  41. * it uses union, so the things are not easy..
  42. */
  43. struct snd_ctl_elem_info32 {
  44. struct snd_ctl_elem_id id; // the size of struct is same
  45. s32 type;
  46. u32 access;
  47. u32 count;
  48. s32 owner;
  49. union {
  50. struct {
  51. s32 min;
  52. s32 max;
  53. s32 step;
  54. } integer;
  55. struct {
  56. u64 min;
  57. u64 max;
  58. u64 step;
  59. } integer64;
  60. struct {
  61. u32 items;
  62. u32 item;
  63. char name[64];
  64. u64 names_ptr;
  65. u32 names_length;
  66. } enumerated;
  67. unsigned char reserved[128];
  68. } value;
  69. unsigned char reserved[64];
  70. } __packed;
  71. static int snd_ctl_elem_info_compat(struct snd_ctl_file *ctl,
  72. struct snd_ctl_elem_info32 __user *data32)
  73. {
  74. struct snd_card *card = ctl->card;
  75. struct snd_ctl_elem_info *data __free(kfree) = NULL;
  76. int err;
  77. data = kzalloc(sizeof(*data), GFP_KERNEL);
  78. if (! data)
  79. return -ENOMEM;
  80. /* copy id */
  81. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
  82. return -EFAULT;
  83. /* we need to copy the item index.
  84. * hope this doesn't break anything..
  85. */
  86. if (get_user(data->value.enumerated.item, &data32->value.enumerated.item))
  87. return -EFAULT;
  88. err = snd_power_ref_and_wait(card);
  89. if (err < 0)
  90. return err;
  91. err = snd_ctl_elem_info(ctl, data);
  92. snd_power_unref(card);
  93. if (err < 0)
  94. return err;
  95. /* restore info to 32bit */
  96. /* id, type, access, count */
  97. if (copy_to_user(&data32->id, &data->id, sizeof(data->id)) ||
  98. copy_to_user(&data32->type, &data->type, 3 * sizeof(u32)))
  99. return -EFAULT;
  100. if (put_user(data->owner, &data32->owner))
  101. return -EFAULT;
  102. switch (data->type) {
  103. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  104. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  105. if (put_user(data->value.integer.min, &data32->value.integer.min) ||
  106. put_user(data->value.integer.max, &data32->value.integer.max) ||
  107. put_user(data->value.integer.step, &data32->value.integer.step))
  108. return -EFAULT;
  109. break;
  110. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  111. if (copy_to_user(&data32->value.integer64,
  112. &data->value.integer64,
  113. sizeof(data->value.integer64)))
  114. return -EFAULT;
  115. break;
  116. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  117. if (copy_to_user(&data32->value.enumerated,
  118. &data->value.enumerated,
  119. sizeof(data->value.enumerated)))
  120. return -EFAULT;
  121. break;
  122. default:
  123. break;
  124. }
  125. return 0;
  126. }
  127. /* read / write */
  128. struct snd_ctl_elem_value32 {
  129. struct snd_ctl_elem_id id;
  130. unsigned int indirect; /* bit-field causes misalignment */
  131. union {
  132. s32 integer[128];
  133. unsigned char data[512];
  134. #ifndef CONFIG_X86_64
  135. s64 integer64[64];
  136. #endif
  137. } value;
  138. unsigned char reserved[128];
  139. };
  140. #ifdef CONFIG_X86_X32_ABI
  141. /* x32 has a different alignment for 64bit values from ia32 */
  142. struct snd_ctl_elem_value_x32 {
  143. struct snd_ctl_elem_id id;
  144. unsigned int indirect; /* bit-field causes misalignment */
  145. union {
  146. s32 integer[128];
  147. unsigned char data[512];
  148. s64 integer64[64];
  149. } value;
  150. unsigned char reserved[128];
  151. };
  152. #endif /* CONFIG_X86_X32_ABI */
  153. /* get the value type and count of the control */
  154. static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,
  155. int *countp)
  156. {
  157. struct snd_kcontrol *kctl;
  158. struct snd_ctl_elem_info *info __free(kfree) = NULL;
  159. int err;
  160. guard(rwsem_read)(&card->controls_rwsem);
  161. kctl = snd_ctl_find_id(card, id);
  162. if (!kctl)
  163. return -ENOENT;
  164. info = kzalloc(sizeof(*info), GFP_KERNEL);
  165. if (info == NULL)
  166. return -ENOMEM;
  167. info->id = *id;
  168. err = kctl->info(kctl, info);
  169. if (err >= 0) {
  170. err = info->type;
  171. *countp = info->count;
  172. }
  173. return err;
  174. }
  175. static int get_elem_size(snd_ctl_elem_type_t type, int count)
  176. {
  177. switch (type) {
  178. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  179. return sizeof(s64) * count;
  180. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  181. return sizeof(int) * count;
  182. case SNDRV_CTL_ELEM_TYPE_BYTES:
  183. return 512;
  184. case SNDRV_CTL_ELEM_TYPE_IEC958:
  185. return sizeof(struct snd_aes_iec958);
  186. default:
  187. return -1;
  188. }
  189. }
  190. static int copy_ctl_value_from_user(struct snd_card *card,
  191. struct snd_ctl_elem_value *data,
  192. void __user *userdata,
  193. void __user *valuep,
  194. int *typep, int *countp)
  195. {
  196. struct snd_ctl_elem_value32 __user *data32 = userdata;
  197. int i, type, size;
  198. int count;
  199. unsigned int indirect;
  200. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
  201. return -EFAULT;
  202. if (get_user(indirect, &data32->indirect))
  203. return -EFAULT;
  204. if (indirect)
  205. return -EINVAL;
  206. type = get_ctl_type(card, &data->id, &count);
  207. if (type < 0)
  208. return type;
  209. if (type == (__force int)SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  210. type == (__force int)SNDRV_CTL_ELEM_TYPE_INTEGER) {
  211. for (i = 0; i < count; i++) {
  212. s32 __user *intp = valuep;
  213. int val;
  214. if (get_user(val, &intp[i]))
  215. return -EFAULT;
  216. data->value.integer.value[i] = val;
  217. }
  218. } else {
  219. size = get_elem_size((__force snd_ctl_elem_type_t)type, count);
  220. if (size < 0) {
  221. dev_err(card->dev, "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
  222. return -EINVAL;
  223. }
  224. if (copy_from_user(data->value.bytes.data, valuep, size))
  225. return -EFAULT;
  226. }
  227. *typep = type;
  228. *countp = count;
  229. return 0;
  230. }
  231. /* restore the value to 32bit */
  232. static int copy_ctl_value_to_user(void __user *userdata,
  233. void __user *valuep,
  234. struct snd_ctl_elem_value *data,
  235. int type, int count)
  236. {
  237. struct snd_ctl_elem_value32 __user *data32 = userdata;
  238. int i, size;
  239. if (type == (__force int)SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  240. type == (__force int)SNDRV_CTL_ELEM_TYPE_INTEGER) {
  241. for (i = 0; i < count; i++) {
  242. s32 __user *intp = valuep;
  243. int val;
  244. val = data->value.integer.value[i];
  245. if (put_user(val, &intp[i]))
  246. return -EFAULT;
  247. }
  248. } else {
  249. size = get_elem_size((__force snd_ctl_elem_type_t)type, count);
  250. if (copy_to_user(valuep, data->value.bytes.data, size))
  251. return -EFAULT;
  252. }
  253. if (copy_to_user(&data32->id, &data->id, sizeof(data32->id)))
  254. return -EFAULT;
  255. return 0;
  256. }
  257. static int __ctl_elem_read_user(struct snd_card *card,
  258. void __user *userdata, void __user *valuep)
  259. {
  260. struct snd_ctl_elem_value *data __free(kfree) = NULL;
  261. int err, type, count;
  262. data = kzalloc(sizeof(*data), GFP_KERNEL);
  263. if (data == NULL)
  264. return -ENOMEM;
  265. err = copy_ctl_value_from_user(card, data, userdata, valuep,
  266. &type, &count);
  267. if (err < 0)
  268. return err;
  269. err = snd_ctl_elem_read(card, data);
  270. if (err < 0)
  271. return err;
  272. return copy_ctl_value_to_user(userdata, valuep, data, type, count);
  273. }
  274. static int ctl_elem_read_user(struct snd_card *card,
  275. void __user *userdata, void __user *valuep)
  276. {
  277. int err;
  278. err = snd_power_ref_and_wait(card);
  279. if (err < 0)
  280. return err;
  281. err = __ctl_elem_read_user(card, userdata, valuep);
  282. snd_power_unref(card);
  283. return err;
  284. }
  285. static int __ctl_elem_write_user(struct snd_ctl_file *file,
  286. void __user *userdata, void __user *valuep)
  287. {
  288. struct snd_ctl_elem_value *data __free(kfree) = NULL;
  289. struct snd_card *card = file->card;
  290. int err, type, count;
  291. data = kzalloc(sizeof(*data), GFP_KERNEL);
  292. if (data == NULL)
  293. return -ENOMEM;
  294. err = copy_ctl_value_from_user(card, data, userdata, valuep,
  295. &type, &count);
  296. if (err < 0)
  297. return err;
  298. err = snd_ctl_elem_write(card, file, data);
  299. if (err < 0)
  300. return err;
  301. return copy_ctl_value_to_user(userdata, valuep, data, type, count);
  302. }
  303. static int ctl_elem_write_user(struct snd_ctl_file *file,
  304. void __user *userdata, void __user *valuep)
  305. {
  306. struct snd_card *card = file->card;
  307. int err;
  308. err = snd_power_ref_and_wait(card);
  309. if (err < 0)
  310. return err;
  311. err = __ctl_elem_write_user(file, userdata, valuep);
  312. snd_power_unref(card);
  313. return err;
  314. }
  315. static int snd_ctl_elem_read_user_compat(struct snd_card *card,
  316. struct snd_ctl_elem_value32 __user *data32)
  317. {
  318. return ctl_elem_read_user(card, data32, &data32->value);
  319. }
  320. static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
  321. struct snd_ctl_elem_value32 __user *data32)
  322. {
  323. return ctl_elem_write_user(file, data32, &data32->value);
  324. }
  325. #ifdef CONFIG_X86_X32_ABI
  326. static int snd_ctl_elem_read_user_x32(struct snd_card *card,
  327. struct snd_ctl_elem_value_x32 __user *data32)
  328. {
  329. return ctl_elem_read_user(card, data32, &data32->value);
  330. }
  331. static int snd_ctl_elem_write_user_x32(struct snd_ctl_file *file,
  332. struct snd_ctl_elem_value_x32 __user *data32)
  333. {
  334. return ctl_elem_write_user(file, data32, &data32->value);
  335. }
  336. #endif /* CONFIG_X86_X32_ABI */
  337. /* add or replace a user control */
  338. static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
  339. struct snd_ctl_elem_info32 __user *data32,
  340. int replace)
  341. {
  342. struct snd_ctl_elem_info *data __free(kfree) = NULL;
  343. data = kzalloc(sizeof(*data), GFP_KERNEL);
  344. if (! data)
  345. return -ENOMEM;
  346. /* id, type, access, count */ \
  347. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
  348. copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
  349. return -EFAULT;
  350. if (get_user(data->owner, &data32->owner))
  351. return -EFAULT;
  352. switch (data->type) {
  353. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  354. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  355. if (get_user(data->value.integer.min, &data32->value.integer.min) ||
  356. get_user(data->value.integer.max, &data32->value.integer.max) ||
  357. get_user(data->value.integer.step, &data32->value.integer.step))
  358. return -EFAULT;
  359. break;
  360. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  361. if (copy_from_user(&data->value.integer64,
  362. &data32->value.integer64,
  363. sizeof(data->value.integer64)))
  364. return -EFAULT;
  365. break;
  366. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  367. if (copy_from_user(&data->value.enumerated,
  368. &data32->value.enumerated,
  369. sizeof(data->value.enumerated)))
  370. return -EFAULT;
  371. data->value.enumerated.names_ptr =
  372. (uintptr_t)compat_ptr(data->value.enumerated.names_ptr);
  373. break;
  374. default:
  375. break;
  376. }
  377. return snd_ctl_elem_add(file, data, replace);
  378. }
  379. enum {
  380. SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
  381. SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
  382. SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
  383. SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
  384. SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
  385. SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
  386. #ifdef CONFIG_X86_X32_ABI
  387. SNDRV_CTL_IOCTL_ELEM_READ_X32 = _IOWR('U', 0x12, struct snd_ctl_elem_value_x32),
  388. SNDRV_CTL_IOCTL_ELEM_WRITE_X32 = _IOWR('U', 0x13, struct snd_ctl_elem_value_x32),
  389. #endif /* CONFIG_X86_X32_ABI */
  390. };
  391. static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  392. {
  393. struct snd_ctl_file *ctl;
  394. struct snd_kctl_ioctl *p;
  395. void __user *argp = compat_ptr(arg);
  396. int err;
  397. ctl = file->private_data;
  398. if (snd_BUG_ON(!ctl || !ctl->card))
  399. return -ENXIO;
  400. switch (cmd) {
  401. case SNDRV_CTL_IOCTL_PVERSION:
  402. case SNDRV_CTL_IOCTL_CARD_INFO:
  403. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  404. case SNDRV_CTL_IOCTL_POWER:
  405. case SNDRV_CTL_IOCTL_POWER_STATE:
  406. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  407. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  408. case SNDRV_CTL_IOCTL_ELEM_REMOVE:
  409. case SNDRV_CTL_IOCTL_TLV_READ:
  410. case SNDRV_CTL_IOCTL_TLV_WRITE:
  411. case SNDRV_CTL_IOCTL_TLV_COMMAND:
  412. return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
  413. case SNDRV_CTL_IOCTL_ELEM_LIST32:
  414. return snd_ctl_elem_list_compat(ctl->card, argp);
  415. case SNDRV_CTL_IOCTL_ELEM_INFO32:
  416. return snd_ctl_elem_info_compat(ctl, argp);
  417. case SNDRV_CTL_IOCTL_ELEM_READ32:
  418. return snd_ctl_elem_read_user_compat(ctl->card, argp);
  419. case SNDRV_CTL_IOCTL_ELEM_WRITE32:
  420. return snd_ctl_elem_write_user_compat(ctl, argp);
  421. case SNDRV_CTL_IOCTL_ELEM_ADD32:
  422. return snd_ctl_elem_add_compat(ctl, argp, 0);
  423. case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
  424. return snd_ctl_elem_add_compat(ctl, argp, 1);
  425. #ifdef CONFIG_X86_X32_ABI
  426. case SNDRV_CTL_IOCTL_ELEM_READ_X32:
  427. return snd_ctl_elem_read_user_x32(ctl->card, argp);
  428. case SNDRV_CTL_IOCTL_ELEM_WRITE_X32:
  429. return snd_ctl_elem_write_user_x32(ctl, argp);
  430. #endif /* CONFIG_X86_X32_ABI */
  431. }
  432. guard(rwsem_read)(&snd_ioctl_rwsem);
  433. list_for_each_entry(p, &snd_control_compat_ioctls, list) {
  434. if (p->fioctl) {
  435. err = p->fioctl(ctl->card, ctl, cmd, arg);
  436. if (err != -ENOIOCTLCMD)
  437. return err;
  438. }
  439. }
  440. return -ENOIOCTLCMD;
  441. }