File Index
Files and directories
Sign in
FILE

control_compat.c

File preview with syntax highlighting.

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