Skip to contentSkip to conversation

ALSA: pcm: implement the anonymous dup

PATCH In review Jaroslav Kysela 3 participants 9 messages Jan 29, 2019 – Jan 30, 2019 Sound

lore
Share & tools
lore

Jaroslav Kysela

author maintainer
#0 lore raw

cover ALSA: pcm: implement the anonymous dup

This patchset contains the anonymous dup implementation with permissions checking for the ALSA's PCM interface in kernel to enable the restricted DMA sound buffer sharing for the restricted tasks.

The code was tested through qemu and it seems to be pretty stable.

The initial tinyalsa implementation can be found here:

https://github.com/perexg/tinyalsa/commits/anondup

The filtering might be refined. It depends on the real requirements. Perhaps, we may create more ioctl groups. Any comments are more than welcome.

Cc Mark Brown <[email protected]>
Cc Leo Yan <[email protected]>
Cc Baolin Wang <[email protected]>

Jaroslav Kysela (3):
ALSA: pcm: implement the anonymous dup (inode file descriptor)
ALSA: pcm: remove the file member from struct pcm
ALSA: pcm: implement the ioctl/mmap filter for the anonymous dup

 include/sound/pcm.h         |  10 +--
 include/uapi/sound/asound.h |  12 +++-
 sound/core/oss/pcm_oss.c    |   3 +-
 sound/core/pcm.c            |  48 +++++++++-----
 sound/core/pcm_compat.c     |   1 +
 sound/core/pcm_native.c     | 154 +++++++++++++++++++++++++++++++++++++++++---
 6 files changed, 195 insertions(+), 33 deletions(-)
Signature · 1 line
2.13.6
author maintainer
#1 lore raw

PATCH 1/3 ALSA: pcm: implement the anonymous dup (inode file descriptor)

This patch implements new SNDRV_PCM_IOCTL_ANONYMOUS_DUP ioctl which returns the new duplicated anonymous inode file descriptor (anon_inode:snd-pcm) which can be passed to the restricted clients.

This patch is meant to be the alternative for the dma-buf interface. Both implementation have some pros and cons:

anon_inode:dmabuf

- a bit standard export API for the DMA buffers
- fencing for the concurrent access [1]
- driver/kernel interface for the DMA buffer [1]
- multiple attach/detach scheme [1]

[1] the real usage for the sound PCM is unknown at the moment for this feature

anon_inode:snd-pcm

- simple (no problem with ref-counting, non-standard mmap implementation etc.)
- allow to use more sound interfaces for the file descriptor like status ioctls
- more fine grained security policies (another anon_inode name unshared with other drivers)

---
 include/sound/pcm.h         |  8 +++---
 include/uapi/sound/asound.h |  3 +-
 sound/core/oss/pcm_oss.c    |  2 +-
 sound/core/pcm.c            | 48 ++++++++++++++++++++------------
 sound/core/pcm_compat.c     |  1 +
 sound/core/pcm_native.c     | 67 +++++++++++++++++++++++++++++++++++++++++----
 6 files changed, 101 insertions(+), 28 deletions(-)
include/sound/pcm.h +4 -4
index 2c30c1ad1b0d..e7deb03b6702 100644
@@ -590,11 +590,11 @@ static inline int snd_pcm_suspend_all(struct snd_pcm *pcm)
 }
 #endif
 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, unsigned int cmd, void *arg);
-int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, struct file *file,
-			   struct snd_pcm_substream **rsubstream);
+int snd_pcm_open_substream(struct snd_pcm *pcm, struct snd_pcm_substream *clone,
+			   int stream, struct file *file, struct snd_pcm_substream **rsubstream);
 void snd_pcm_release_substream(struct snd_pcm_substream *substream);
-int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, struct file *file,
-			     struct snd_pcm_substream **rsubstream);
+int snd_pcm_attach_substream(struct snd_pcm *pcm, struct snd_pcm_substream *clone, int stream,
+			     struct file *file, struct snd_pcm_substream **rsubstream);
 void snd_pcm_detach_substream(struct snd_pcm_substream *substream);
 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, struct vm_area_struct *area);
 
include/uapi/sound/asound.h +2 -1
index 404d4b9ffe76..ebc17d5a3490 100644
@@ -153,7 +153,7 @@ struct snd_hwdep_dsp_image {
  *                                                                           *
  *****************************************************************************/
 
-#define SNDRV_PCM_VERSION		SNDRV_PROTOCOL_VERSION(2, 0, 14)
+#define SNDRV_PCM_VERSION		SNDRV_PROTOCOL_VERSION(2, 0, 15)
 
 typedef unsigned long snd_pcm_uframes_t;
 typedef signed long snd_pcm_sframes_t;
@@ -576,6 +576,7 @@ enum {
 #define SNDRV_PCM_IOCTL_TSTAMP		_IOW('A', 0x02, int)
 #define SNDRV_PCM_IOCTL_TTSTAMP		_IOW('A', 0x03, int)
 #define SNDRV_PCM_IOCTL_USER_PVERSION	_IOW('A', 0x04, int)
+#define SNDRV_PCM_IOCTL_ANONYMOUS_DUP   _IOWR('A', 0x05, int)
 #define SNDRV_PCM_IOCTL_HW_REFINE	_IOWR('A', 0x10, struct snd_pcm_hw_params)
 #define SNDRV_PCM_IOCTL_HW_PARAMS	_IOWR('A', 0x11, struct snd_pcm_hw_params)
 #define SNDRV_PCM_IOCTL_HW_FREE		_IO('A', 0x12)
sound/core/oss/pcm_oss.c +1 -1
index 467039b342b5..b3738c228f39 100644
@@ -2420,7 +2420,7 @@ static int snd_pcm_oss_open_file(struct file *file,
 			if (! (f_mode & FMODE_READ))
 				continue;
 		}
-		err = snd_pcm_open_substream(pcm, idx, file, &substream);
+		err = snd_pcm_open_substream(pcm, NULL, idx, file, &substream);
 		if (err < 0) {
 			snd_pcm_oss_release_file(pcm_oss_file);
 			return err;
sound/core/pcm.c +31 -17

79 lines not on this page · raw message

sound/core/pcm_compat.c +1 -0
index 946ab080ac00..22446cd574ee 100644
@@ -675,6 +675,7 @@ static long snd_pcm_ioctl_compat(struct file *file, unsigned int cmd, unsigned l
 	case SNDRV_PCM_IOCTL_TSTAMP:
 	case SNDRV_PCM_IOCTL_TTSTAMP:
 	case SNDRV_PCM_IOCTL_USER_PVERSION:
+	case SNDRV_PCM_IOCTL_ANONYMOUS_DUP:
 	case SNDRV_PCM_IOCTL_HWSYNC:
 	case SNDRV_PCM_IOCTL_PREPARE:
 	case SNDRV_PCM_IOCTL_RESET:
sound/core/pcm_native.c +62 -5

120 lines not on this page · raw message

Signature · 1 line
2.13.6
#2 lore raw

Re: [PATCH 1/3] ALSA: pcm: implement the anonymous dup (inode file descriptor)

Quoted 15 lines · Jaroslav Kysela

This patch implements new SNDRV_PCM_IOCTL_ANONYMOUS_DUP ioctl which returns the new duplicated anonymous inode file descriptor (anon_inode:snd-pcm) which can be passed to the restricted clients.

This patch is meant to be the alternative for the dma-buf interface. Both implementation have some pros and cons:

anon_inode:dmabuf

- a bit standard export API for the DMA buffers
- fencing for the concurrent access [1]
- driver/kernel interface for the DMA buffer [1]
- multiple attach/detach scheme [1]

Quoted 8 lines

[1] the real usage for the sound PCM is unknown at the moment for this feature

anon_inode:snd-pcm

- simple (no problem with ref-counting, non-standard mmap implementation etc.)
- allow to use more sound interfaces for the file descriptor like status ioctls
- more fine grained security policies (another anon_inode name unshared with other drivers)

Your sign-off seems missing.

Quoted 18 lines
sound/core/pcm.c +5 -8
@@ -999,15 +1002,19 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
 	}
 
 	if (file->f_flags & O_APPEND) {
-		if (prefer_subdevice < 0) {
-			if (pstr->substream_count > 1)
-				return -EINVAL; /* must be unique */
-			substream = pstr->substream;
+		if (clone) {
+			substream = clone;
 		} else {
-			for (substream = pstr->substream; substream;
-			     substream = substream->next)
-				if (substream->number == prefer_subdevice)
-					break;
+			if (prefer_subdevice < 0) {
+				if (pstr->substream_count > 1)
+					return -EINVAL; /* must be unique */

sound/core/pcm.c int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, · line 1020

Quoted 10 lines
sound/core/pcm.c +7 -0
+				substream = pstr->substream;
+			} else {
+				for (substream = pstr->substream; substream;
+				     substream = substream->next)
+					if (substream->number == prefer_subdevice)
+						break;
+			}
 		}
 		if (! substream)
 			return -ENODEV;

So the clone case should return via this block, then...

Quoted 11 lines
sound/core/pcm.c +2 -5
@@ -1018,11 +1025,18 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
 		return 0;
 	}
 
-	for (substream = pstr->substream; substream; substream = substream->next) {
-		if (!SUBSTREAM_BUSY(substream) &&
-		    (prefer_subdevice == -1 ||
-		     substream->number == prefer_subdevice))
-			break;
+	if (clone) {
+		substream = clone;

sound/core/pcm.c int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, · line 1039

Quoted 10 lines
sound/core/pcm.c +10 -0
+		if (SUBSTREAM_BUSY(substream))
+			return -EAGAIN;
+	} else {
+		for (substream = pstr->substream; substream;
+		     substream = substream->next) {
+			if (!SUBSTREAM_BUSY(substream) &&
+			    (prefer_subdevice == -1 ||
+			     substream->number == prefer_subdevice))
+				break;
+		}

... do we need to support cloning without O_APPEND?

thanks,

Takashi

author maintainer
#3 lore raw

Re: [PATCH 1/3] ALSA: pcm: implement the anonymous dup (inode file descriptor)

Dne 29.1.2019 v 19:44 Takashi Iwai napsal(a):

Quoted 19 lines
Quoted 17 lines · Jaroslav Kysela

This patch implements new SNDRV_PCM_IOCTL_ANONYMOUS_DUP ioctl which returns the new duplicated anonymous inode file descriptor (anon_inode:snd-pcm) which can be passed to the restricted clients.

This patch is meant to be the alternative for the dma-buf interface. Both implementation have some pros and cons:

anon_inode:dmabuf

- a bit standard export API for the DMA buffers
- fencing for the concurrent access [1]
- driver/kernel interface for the DMA buffer [1]
- multiple attach/detach scheme [1]

[1] the real usage for the sound PCM is unknown at the moment for this feature

Quoted 8 lines
Quoted 6 lines

anon_inode:snd-pcm

- simple (no problem with ref-counting, non-standard mmap implementation etc.)
- allow to use more sound interfaces for the file descriptor like status ioctls
- more fine grained security policies (another anon_inode name unshared with other drivers)

Your sign-off seems missing.

Fixed now. Thanks.

Quoted 46 lines
Quoted 18 lines
sound/core/pcm.c +5 -8
@@ -999,15 +1002,19 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
 	}
 
 	if (file->f_flags & O_APPEND) {
-		if (prefer_subdevice < 0) {
-			if (pstr->substream_count > 1)
-				return -EINVAL; /* must be unique */
-			substream = pstr->substream;
+		if (clone) {
+			substream = clone;
 		} else {
-			for (substream = pstr->substream; substream;
-			     substream = substream->next)
-				if (substream->number == prefer_subdevice)
-					break;
+			if (prefer_subdevice < 0) {
+				if (pstr->substream_count > 1)
+					return -EINVAL; /* must be unique */

sound/core/pcm.c int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, · line 1020

Quoted 10 lines
sound/core/pcm.c +7 -0
+				substream = pstr->substream;
+			} else {
+				for (substream = pstr->substream; substream;
+				     substream = substream->next)
+					if (substream->number == prefer_subdevice)
+						break;
+			}
 		}
 		if (! substream)
 			return -ENODEV;

So the clone case should return via this block, then...

Quoted 15 lines
sound/core/pcm.c +6 -5
@@ -1018,11 +1025,18 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
 		return 0;
 	}
 
-	for (substream = pstr->substream; substream; substream = substream->next) {
-		if (!SUBSTREAM_BUSY(substream) &&
-		    (prefer_subdevice == -1 ||
-		     substream->number == prefer_subdevice))
-			break;
+	if (clone) {
+		substream = clone;
+		if (SUBSTREAM_BUSY(substream))
+			return -EAGAIN;
+	} else {
+		for (substream = pstr->substream; substream;
Quoted 8 lines

sound/core/pcm.c int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, · line 1039

Quoted 6 lines
sound/core/pcm.c +6 -0
+		     substream = substream->next) {
+			if (!SUBSTREAM_BUSY(substream) &&
+			    (prefer_subdevice == -1 ||
+			     substream->number == prefer_subdevice))
+				break;
+		}

... do we need to support cloning without O_APPEND?

I think that it would be better to pass the subdevice number to snd_pcm_attach_substream(). It will reduce the required modifications. I will change this.

Thanks,
Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
author maintainer
#4 lore raw

PATCH 2/3 ALSA: pcm: remove the file member from struct pcm

In reply to Jaroslav Kysela

This member is no longer used.

---
 include/sound/pcm.h      | 1 -
 sound/core/oss/pcm_oss.c | 1 -
 sound/core/pcm_native.c  | 4 +---
 3 files changed, 1 insertion(+), 5 deletions(-)
include/sound/pcm.h +0 -1
index e7deb03b6702..61e4c69e73c7 100644
@@ -470,7 +470,6 @@ struct snd_pcm_substream {
 	struct snd_pcm_group self_group;	/* fake group for non linked substream (with substream lock inside) */
 	struct snd_pcm_group *group;		/* pointer to current group */
 	/* -- assigned files -- */
-	void *file;
 	int ref_count;
 	atomic_t mmap_count;
 	unsigned int f_flags;
sound/core/oss/pcm_oss.c +0 -1
index b3738c228f39..222ddf9e4859 100644
@@ -2427,7 +2427,6 @@ static int snd_pcm_oss_open_file(struct file *file,
 		}
 
 		pcm_oss_file->streams[idx] = substream;
-		substream->file = pcm_oss_file;
 		snd_pcm_oss_init_substream(substream, &setup[idx], minor);
 	}
 	
sound/core/pcm_native.c +1 -3
index a6d2a5024ab5..3ab6fbd7acae 100644
@@ -2455,10 +2455,8 @@ static int snd_pcm_open_file(struct file *file,
 		return -ENOMEM;
 	}
 	pcm_file->substream = substream;
-	if (substream->ref_count == 1) {
-		substream->file = pcm_file;
+	if (substream->ref_count == 1)
 		substream->pcm_release = pcm_release_private;
-	}
 	file->private_data = pcm_file;
 
 	return 0;
Signature · 1 line
2.13.6
#5 lore raw

Re: [PATCH 2/3] ALSA: pcm: remove the file member from struct pcm

Quoted 4 lines · Jaroslav Kysela

This member is no longer used.

Heh, the very same change is already in my for-next branch :)

thanks,

Takashi

author maintainer
#6 lore raw

PATCH 3/3 ALSA: pcm: implement the ioctl/mmap filter for the anonymous dup

In reply to Jaroslav Kysela

Create seven control bits to allow the various restrictions for the anonymous file descriptor.

---
 include/sound/pcm.h         |  1 +
 include/uapi/sound/asound.h |  9 +++++
 sound/core/pcm_native.c     | 85 ++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 94 insertions(+), 1 deletion(-)
include/sound/pcm.h +1 -0
index 61e4c69e73c7..29d22a3a458c 100644
@@ -226,6 +226,7 @@ struct snd_pcm_ops {
 struct snd_pcm_file {
 	struct snd_pcm_substream *substream;
 	int no_compat_mmap;
+	unsigned int perm;		/* file descriptor permissions */
 	unsigned int user_pversion;	/* supported protocol version */
 };
 
include/uapi/sound/asound.h +9 -0
index ebc17d5a3490..29d3a16caa9a 100644
@@ -571,6 +571,15 @@ enum {
 #define SNDRV_CHMAP_PHASE_INVERSE	(0x01 << 16)
 #define SNDRV_CHMAP_DRIVER_SPEC		(0x02 << 16)
 
+#define SNDRV_PCM_PERM_MMAP		(1<<0)
+#define SNDRV_PCM_PERM_MMAP_STATUS	(1<<1)
+#define SNDRV_PCM_PERM_MMAP_CONTROL	(1<<2)
+#define SNDRV_PCM_PERM_RW		(1<<3)
+#define SNDRV_PCM_PERM_CONTROL		(1<<4)
+#define SNDRV_PCM_PERM_STATUS		(1<<5)
+#define SNDRV_PCM_PERM_SYNC		(1<<6)
+#define SNDRV_PCM_PERM_MAX		((SNDRV_PCM_PERM_SYNC<<1)-1)
+
 #define SNDRV_PCM_IOCTL_PVERSION	_IOR('A', 0x00, int)
 #define SNDRV_PCM_IOCTL_INFO		_IOR('A', 0x01, struct snd_pcm_info)
 #define SNDRV_PCM_IOCTL_TSTAMP		_IOW('A', 0x02, int)
sound/core/pcm_native.c +84 -1

141 lines not on this page · raw message

Signature · 1 line
2.13.6
#7 lore raw

Re: [PATCH 3/3] ALSA: pcm: implement the ioctl/mmap filter for the anonymous dup

Quoted 29 lines · Jaroslav Kysela

Create seven control bits to allow the various restrictions for the anonymous file descriptor.

---
 include/sound/pcm.h         |  1 +
 include/uapi/sound/asound.h |  9 +++++
 sound/core/pcm_native.c     | 85 ++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 94 insertions(+), 1 deletion(-)
include/sound/pcm.h +1 -0
index 61e4c69e73c7..29d22a3a458c 100644
@@ -226,6 +226,7 @@ struct snd_pcm_ops {
 struct snd_pcm_file {
 	struct snd_pcm_substream *substream;
 	int no_compat_mmap;
+	unsigned int perm;		/* file descriptor permissions */
 	unsigned int user_pversion;	/* supported protocol version */
 };
 
include/uapi/sound/asound.h +0 -0
index ebc17d5a3490..29d3a16caa9a 100644
@@ -571,6 +571,15 @@ enum {
 #define SNDRV_CHMAP_PHASE_INVERSE	(0x01 << 16)

include/uapi/sound/asound.h enum { · line 581

Quoted 10 lines
include/uapi/sound/asound.h +8 -0
 #define SNDRV_CHMAP_DRIVER_SPEC		(0x02 << 16)
 
+#define SNDRV_PCM_PERM_MMAP		(1<<0)
+#define SNDRV_PCM_PERM_MMAP_STATUS	(1<<1)
+#define SNDRV_PCM_PERM_MMAP_CONTROL	(1<<2)
+#define SNDRV_PCM_PERM_RW		(1<<3)
+#define SNDRV_PCM_PERM_CONTROL		(1<<4)
+#define SNDRV_PCM_PERM_STATUS		(1<<5)
+#define SNDRV_PCM_PERM_SYNC		(1<<6)
+#define SNDRV_PCM_PERM_MAX		((SNDRV_PCM_PERM_SYNC<<1)-1)

I'd name it SNDRV_PCM_PERM_MASK, and ...

include/uapi/sound/asound.h static int snd_pcm_anonymous_dup(struct file *file, · line 2859

Quoted 11 lines
include/uapi/sound/asound.h +2 -1
@@ -2850,10 +2851,11 @@ static int snd_pcm_anonymous_dup(struct file *file,
 	int flags;
 	struct file *nfile;
 	struct snd_pcm *pcm = substream->pcm;
+	struct snd_pcm_file *pcm_file;
 
 	if (get_user(perm, (int __user *)arg))
 		return -EFAULT;
-	if (perm < 0)
+	if (perm < 0 || perm > SNDRV_PCM_PERM_MAX)
 		return -ENOSYS;

... check like

	if (perm & ~SNDRV_PCM_PER_MASK)
		return -EINVAL;

thanks,

Takashi

#8 lore raw
In reply to Jaroslav Kysela
Quoted 6 lines · Jaroslav Kysela

This patchset contains the anonymous dup implementation with permissions checking for the ALSA's PCM interface in kernel to enable the restricted DMA sound buffer sharing for the restricted tasks.

The code was tested through qemu and it seems to be pretty stable.

Quoted 7 lines

The initial tinyalsa implementation can be found here:

https://github.com/perexg/tinyalsa/commits/anondup

The filtering might be refined. It depends on the real requirements. Perhaps, we may create more ioctl groups. Any comments are more than welcome.

Thanks for looking at this! Copying in Phil who is probably best placed to review these from an Android perspective.

Quoted 19 lines
Cc Mark Brown <[email protected]>
Cc Leo Yan <[email protected]>
Cc Baolin Wang <[email protected]>

Jaroslav Kysela (3):
ALSA: pcm: implement the anonymous dup (inode file descriptor)
ALSA: pcm: remove the file member from struct pcm
ALSA: pcm: implement the ioctl/mmap filter for the anonymous dup

 include/sound/pcm.h         |  10 +--
 include/uapi/sound/asound.h |  12 +++-
 sound/core/oss/pcm_oss.c    |   3 +-
 sound/core/pcm.c            |  48 +++++++++-----
 sound/core/pcm_compat.c     |   1 +
 sound/core/pcm_native.c     | 154 +++++++++++++++++++++++++++++++++++++++++---
 6 files changed, 195 insertions(+), 33 deletions(-)
Signature · 1 line
2.13.6