Skip to contentSkip to conversation

ALSA: pcm: implement the anonymous dup v3

PATCH Superseded Jaroslav Kysela 6 participants 21 messages Jan 30, 2019 – Feb 1, 2019 Sound

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Jaroslav Kysela

author maintainer
#0 lore raw

cover ALSA: pcm: implement the anonymous dup v3

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.

v2 of the patches:

- change clone parameter to subdevice number for the pcm attach
- change SNDRV_PCM_PERM_MAX to SNDRV_PCM_PERM_MASK
- the tinyalsa implementation was a little updated (restructured)

v3 of the patches:

- group integer declarations in snd_pcm_anonymous_dup()
- replaced substream->pcm with pcm in snd_pcm_anonymous_dup()
- added SNDRV_PCM_PERM_RW check for read/write/readv/writev syscalls

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

Jaroslav Kysela (2):
ALSA: pcm: implement the anonymous dup (inode file descriptor)
ALSA: pcm: implement the ioctl/mmap filter for the anonymous dup

 include/sound/pcm.h         |   9 +--
 include/uapi/sound/asound.h |  12 +++-
 sound/core/oss/pcm_oss.c    |   2 +-
 sound/core/pcm.c            |  13 ++--
 sound/core/pcm_compat.c     |   1 +
 sound/core/pcm_native.c     | 158 ++++++++++++++++++++++++++++++++++++++++++--
 6 files changed, 176 insertions(+), 19 deletions(-)
Signature · 1 line
2.13.6
author maintainer
#1 lore raw

PATCH 1/2 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            | 13 ++++-----
 sound/core/pcm_compat.c     |  1 +
 sound/core/pcm_native.c     | 67 +++++++++++++++++++++++++++++++++++++++++----
 6 files changed, 75 insertions(+), 19 deletions(-)
include/sound/pcm.h +4 -4
index ca20f80f8976..b79ffaa0241d 100644
@@ -579,11 +579,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, int stream, int subdevice,
+                           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, int stream, int subdevice,
+			     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 d5b0d7ba83c4..2ed609b65c45 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, idx, -1, file, &substream);
 		if (err < 0) {
 			snd_pcm_oss_release_file(pcm_oss_file);
 			return err;
sound/core/pcm.c +5 -8
index 4f45b3000347..af6f7fc3687b 100644
@@ -918,15 +918,14 @@ static int snd_pcm_dev_free(struct snd_device *device)
 	return snd_pcm_free(pcm);
 }
 
-int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
-			     struct file *file,
+int snd_pcm_attach_substream(struct snd_pcm *pcm,
+			     int stream, int subdevice, struct file *file,
 			     struct snd_pcm_substream **rsubstream)
 {
 	struct snd_pcm_str * pstr;
 	struct snd_pcm_substream *substream;
 	struct snd_pcm_runtime *runtime;
 	struct snd_card *card;
-	int prefer_subdevice;
 	size_t size;
 
 	if (snd_BUG_ON(!pcm || !rsubstream))
@@ -940,7 +939,6 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
 		return -ENODEV;
 
 	card = pcm->card;
-	prefer_subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_PCM);
 
 	if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
 		int opposite = !stream;
@@ -953,14 +951,14 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
 	}
 
 	if (file->f_flags & O_APPEND) {
-		if (prefer_subdevice < 0) {
+		if (subdevice < 0) {
 			if (pstr->substream_count > 1)
 				return -EINVAL; /* must be unique */
 			substream = pstr->substream;
 		} else {
 			for (substream = pstr->substream; substream;
 			     substream = substream->next)
-				if (substream->number == prefer_subdevice)
+				if (substream->number == subdevice)
 					break;
 		}
 		if (! substream)
@@ -974,8 +972,7 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
 
 	for (substream = pstr->substream; substream; substream = substream->next) {
 		if (!SUBSTREAM_BUSY(substream) &&
-		    (prefer_subdevice == -1 ||
-		     substream->number == prefer_subdevice))
+		    (subdevice == -1 || substream->number == subdevice))
 			break;
 	}
 	if (substream == NULL)
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
author maintainer
#2 lore raw

PATCH 2/2 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     | 93 ++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 102 insertions(+), 1 deletion(-)
include/sound/pcm.h +1 -0
index b79ffaa0241d..e0469b2c1115 100644
@@ -227,6 +227,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..8d99aa8916f0 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_MASK		((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 +92 -1

173 lines not on this page · raw message

Signature · 1 line
2.13.6
#3 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.

My understanding based on some off-list discussion is that the Android security people are going to see anything that involves passing more than a block of memory (and in particular anything that gives access to the sound APIs) as a problem. That's obviously going to be an issue for anything O_APPEND based. My understanding is that this is fundamentally a risk mitigation thing - by not having any of the sound kernel interfaces available to the applications affected there's no possibility that any problems in the sound code can cause security issues.

#4 lore raw

Hello Mark,

Our security team was very concerned about the old ALSA FD. It provided too much access to the guts of ALSA.

I assume they will not like anything other than a plain "anon_inode:dmabuf". If it is a new FD, then the code would have to be reviewed. Even if it looked OK there might be some holes that we don't find. So it would probably be rejected.

I cannot speak for our security team so I am working on setting up a meeting or conversation between Mark and Zach, our security expert.

Adding the anon_inode:snd-pcm might be great for ALSA. That could be used by the HAL for STATUS and CONTROL. But it is likely that we will need an additional anon_inode:dmabuf FD that is only associated with the PCM buffer. It can then be safely passed to an Android app.

Thanks,
Phil Burk

Quoted 24 lines · Mark Brown
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.

My understanding based on some off-list discussion is that the Android security people are going to see anything that involves passing more than a block of memory (and in particular anything that gives access to the sound APIs) as a problem. That's obviously going to be an issue for anything O_APPEND based. My understanding is that this is fundamentally a risk mitigation thing - by not having any of the sound kernel interfaces available to the applications affected there's no possibility that any problems in the sound code can cause security issues.

#5 lore raw

Hi Phil & all,

Quoted 10 lines · Phil Burk

Hello Mark,

Our security team was very concerned about the old ALSA FD. It provided too much access to the guts of ALSA.

I assume they will not like anything other than a plain "anon_inode:dmabuf". If it is a new FD, then the code would have to be reviewed. Even if it looked OK there might be some holes that we don't find. So it would probably be rejected.

Quoted 7 lines

I cannot speak for our security team so I am working on setting up a meeting or conversation between Mark and Zach, our security expert.

Adding the anon_inode:snd-pcm might be great for ALSA. That could be used by the HAL for STATUS and CONTROL. But it is likely that we will need an additional anon_inode:dmabuf FD that is only associated with the PCM buffer. It can then be safely passed to an Android app.

Thanks for the inputs. I went through discussions, I'd like to divide the work into two mainly parts:

- The first part is to use dmabuf to dynamically import buffer to sound device; so the buffer is not bound to sound device at the initialization phase;

- The second part is to use dmabuf to export buffer with anon_inode; thus it can meet the security requirement.

I go through Jaroslav implementation (thanks a lot for the quick moving for this part!), it tries to implement the second part, but it misses the first part support for dynamically binding audio buffer; and as Mark/Phil mentioned, Jaroslav patch tries to use the same one FD for both sound device and audio buffer.

I think it's good to firstly use one test case to demonstrate to dynamically import buffer to sound device, then this buffer can be exported with anon_inode for user space. Is this doable?

Thanks,
Leo Yan

Quoted 26 lines · Mark Brown
Quoted 25 lines · Jaroslav Kysela
Quoted 6 lines

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.

My understanding based on some off-list discussion is that the Android security people are going to see anything that involves passing more than a block of memory (and in particular anything that gives access to the sound APIs) as a problem. That's obviously going to be an issue for anything O_APPEND based. My understanding is that this is fundamentally a risk mitigation thing - by not having any of the sound kernel interfaces available to the applications affected there's no possibility that any problems in the sound code can cause security issues.

#6 lore raw
In reply to Phil Burk
Quoted 10 lines · Phil Burk

Hello Mark,

Our security team was very concerned about the old ALSA FD. It provided too much access to the guts of ALSA.

I assume they will not like anything other than a plain "anon_inode:dmabuf". If it is a new FD, then the code would have to be reviewed. Even if it looked OK there might be some holes that we don't find. So it would probably be rejected.

The review is appreciated, sure! ;)

Above all, it'd be helpful if you can tell us exactly which feature is requested and exactly what have to be avoided. Jaroslav's patchset tries to provide the generic implementation, and this can do more than you guys needed. But it can restrict the permissions well, too.

Quoted 7 lines

I cannot speak for our security team so I am working on setting up a meeting or conversation between Mark and Zach, our security expert.

Adding the anon_inode:snd-pcm might be great for ALSA. That could be used by the HAL for STATUS and CONTROL. But it is likely that we will need an additional anon_inode:dmabuf FD that is only associated with the PCM buffer. It can then be safely passed to an Android app.

I find it fine to add a dma-buf implementation, too -- but only if it's really safely, sanely and simply implemented. The suggested implementation so far has a way too many holes, unfortunately, and it can easily lead to kernel Oops when something wrong happens in the master stream side. And we need to cover some corner cases (e.g. a hardware buffer setup that isn't supposed to be shared) that are overlooked, too.

thanks,

Takashi

Quoted 21 lines

Thanks,
Phil Burk

On Wed, Jan 30, 2019 at 2:32 PM Mark Brown <[email protected]> wrote:

On Wed, Jan 30, 2019 at 01:41:37PM +0100, Jaroslav Kysela wrote:
> 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.

Quoted 10 lines

My understanding based on some off-list discussion is that the Android security people are going to see anything that involves passing more than a block of memory (and in particular anything that gives access to the sound APIs) as a problem.  That's obviously going to be an issue for anything O_APPEND based.  My understanding is that this is fundamentally a risk mitigation thing - by not having any of the sound kernel interfaces available to the applications affected there's no possibility that any problems in the sound code can cause security issues.

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

author maintainer
#7 lore raw
In reply to Phil Burk

Dne 31.1.2019 v 01:45 Phil Burk napsal(a):

Quoted 9 lines

Hello Mark,

Our security team was very concerned about the old ALSA FD. It provided too much access to the guts of ALSA.

I assume they will not like anything other than a plain "anon_inode:dmabuf". If it is a new FD, then the code would have to be reviewed. Even if it looked OK there might be some holes that we don't find. So it would probably be rejected.

Hello Phil,

My point is that the dma-buf -> sound pcm buffer maping interface is more complex, error prone and the code review/audit expensive than reusing the current code without any functionality or security benefits.

We can nicely restrict the file operations to allow to mmap only the pcm sound buffer and eventually, if we are too much paranoid (to bypass the the bitmap like permission checking as I suggested), we can create a special case for the Android usage to return the file descriptor with very restricted 'struct file_operations' with just the mmap and release callbacks. We can also change the name for this file descriptor to distinguish it from the "anon_inode:snd-pcm" (for example "anon_inode:snd-pcm-paranoid") to let SELinux do it's work properly.

The mmap implementation for the sound driver is few lines of the code (for the standard devices - very easy to review), so we cannot speak about security holes at all. If there is a problem with the kernel page allocation/management in the sound driver, there will be problem with dmabuf -> sound pcm buffer mapping, too (plus other problems caused by the concurrent access to the buffer which is managed /alloc/free/ by the sound driver - not dma-buf).

Quoted 2 lines

I cannot speak for our security team so I am working on setting up a meeting or conversation between Mark and Zach, our security expert.

Thanks. Let us know the result. Eventually, your security expert can freely join to our conversation here.

Jaroslav

Signature · 6 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#8 lore raw
In reply to Mark Brown
Quoted 16 lines · Mark Brown
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.

Quoted 8 lines

My understanding based on some off-list discussion is that the Android security people are going to see anything that involves passing more than a block of memory (and in particular anything that gives access to the sound APIs) as a problem. That's obviously going to be an issue for anything O_APPEND based. My understanding is that this is fundamentally a risk mitigation thing - by not having any of the sound kernel interfaces available to the applications affected there's no possibility that any problems in the sound code can cause security issues.

The patch 2 implements exactly that kind of access restriction, so that the passed fd won't do anything else than wished.

If we want to be super-conservative, the implementation could be even simpler -- instead of filtering, we may pass a minimum fd ops that contains only mmap and release for the anon-dup fd...

thanks,

Takashi

#9 lore raw
Quoted 9 lines · Takashi Iwai
Quoted 4 lines · Mark Brown

anything O_APPEND based. My understanding is that this is fundamentally a risk mitigation thing - by not having any of the sound kernel interfaces available to the applications affected there's no possibility that any problems in the sound code can cause security issues.

The patch 2 implements exactly that kind of access restriction, so that the passed fd won't do anything else than wished.

Yeah.

Quoted 3 lines

If we want to be super-conservative, the implementation could be even simpler -- instead of filtering, we may pass a minimum fd ops that contains only mmap and release for the anon-dup fd...

I think that'd definitely help address the concerns.

author maintainer
#10 lore raw

Dne 31.1.2019 v 13:26 Mark Brown napsal(a):

Quoted 11 lines · Takashi Iwai
Quoted 9 lines
Quoted 4 lines · Mark Brown

anything O_APPEND based. My understanding is that this is fundamentally a risk mitigation thing - by not having any of the sound kernel interfaces available to the applications affected there's no possibility that any problems in the sound code can cause security issues.

The patch 2 implements exactly that kind of access restriction, so that the passed fd won't do anything else than wished.

Quoted 7 lines

Yeah.

Quoted 3 lines

If we want to be super-conservative, the implementation could be even simpler -- instead of filtering, we may pass a minimum fd ops that contains only mmap and release for the anon-dup fd...

I think that'd definitely help address the concerns.

A possible implementation:

http://git.alsa-project.org/?p=alsa-kernel.git;a=commitdiff;h=ca15bc69a984cc0eae2c43d0a49c66a20c937f39

Jaroslav

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

+Zach Riggle <[email protected]>

Hello,

Quoted 1 line

Eventually, your security expert can freely join to our conversation here.

Thanks. The biggest unanswered question is whether Android security will allow the file descriptor to be passed to an app. So I have added our security person, Zach Riggle, who originally requested the anon_inode:dmabuf FD. If Zach is happy then I am happy.

We will need two file descriptors, one with full permissions for the HAL, and one with only PCM access for the app to use.
It seems we are considering two options for the app's FD:
1) provide an anon_inode:dmabuf that never has CONTROL permissions, which seems safe, but requires more changes to the driver and is a bit of a hack
2) provide an anon_inode:snd-pcm that has CONTROL permissions turned off, which seems seems less safe, but requires fewer changes and fits with the design

Which one is actually better for security?

Here is an earlier argument for snd-pcm from Jaroslav:

My point is that the dma-buf -> sound pcm buffer maping interface is

Quoted 3 lines

more complex, error prone and the code review/audit expensive than reusing the current code without any functionality or security benefits.

We can nicely restrict the file operations to allow to mmap only the pcm

Quoted 8 lines

sound buffer and eventually, if we are too much paranoid (to bypass the the bitmap like permission checking as I suggested), we can create a special case for the Android usage to return the file descriptor with very restricted 'struct file_operations' with just the mmap and release callbacks. We can also change the name for this file descriptor to distinguish it from the "anon_inode:snd-pcm" (for example "anon_inode:snd-pcm-paranoid") to let SELinux do it's work properly.

The mmap implementation for the sound driver is few lines of the code

Quoted 6 lines

(for the standard devices - very easy to review), so we cannot speak about security holes at all. If there is a problem with the kernel page allocation/management in the sound driver, there will be problem with dmabuf -> sound pcm buffer mapping, too (plus other problems caused by the concurrent access to the buffer which is managed /alloc/free/ by the sound driver - not dma-buf).

Also note that my emails bounce off the alsa-project mail list.

Phil Burk

Quoted 33 lines · Jaroslav Kysela

Dne 31.1.2019 v 13:26 Mark Brown napsal(a):

Quoted 4 lines · Takashi Iwai
Quoted 3 lines
Quoted 1 line · Mark Brown

anything O_APPEND based. My understanding is that this is

fundamentally

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

a risk mitigation thing - by not having any of the sound kernel interfaces available to the applications affected there's no

possibility

Quoted 5 lines
Quoted 4 lines
Quoted 1 line

that any problems in the sound code can cause security issues.

The patch 2 implements exactly that kind of access restriction, so that the passed fd won't do anything else than wished.

Quoted 7 lines

Yeah.

Quoted 3 lines

If we want to be super-conservative, the implementation could be even simpler -- instead of filtering, we may pass a minimum fd ops that contains only mmap and release for the anon-dup fd...

I think that'd definitely help address the concerns.

A possible implementation:

http://git.alsa-project.org/?p=alsa-kernel.git;a=commitdiff;h=ca15bc69a984cc0eae2c43d0a49c66a20c937f39

Jaroslav

--
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.

#12 lore raw

Mark and Zach and I talked.

Zach said that "dmabuf" is not a hard requirement. Another "anon_inode" would probably be OK as long as the app cannot turn on any permissions besides PCM access. Our security team will just need to review the changes.

So I think you should proceed with the "anon_inode:snd-pcm" if you think that will be more secure. Thanks for proposing this.

Zach has some notes in his initial review of Jaroslav's code. Zach?

One thing Zach mentioned is that the API should only allow *removing* permissions and not adding permissions.

What permissions would be set on the FD given to the app?

Also Mark mentioned that the FD app would have PCM access and "close" permission. What flag allows close? What else is permitted under that flag? Or is close permission just a generic "FD" permission unrelated to ALSA?

Thanks for all your work on this. Sorry if I caused alarm. I just wanted to make sure we could use the solution you provide.

Phil Burk

#13 lore raw

The big concerns on our end are:

(1) Can the buffer be mremap()ed with a different *offset* into the buffer? This was a concern in the past and the reason for the anon_inode stuff at all. I believe that as long as the *size* of the mapping doesn't change, Linux mm will gladly permit mremap() without informing the driver.
(2) Can we ensure that permissions can only ever be dropped? (new_perms = old_perms & requested_perms) . It's probably useful to throw an error code if new permissions are requested.
(3) It looks like the code for snd_pcm_anonymous_dup in the patchset takes a *perm* argument from user-space and discards it. Is this intended?
(4) I'm not familiar with the lifecycle of all of the objects, and introducing a custom *dup* routine might cause unexpected problems (e.g. use-after-free, double-free). I'm not well-versed-enough in how the driver-specific stuff is handled underneath e.g. snd_card_file_add / snd_card_file_remove
to be sure about it.

Zach Riggle | Android Security | [email protected] | Austin, TX 🇨🇱

Quoted 16 lines · Phil Burk

Mark and Zach and I talked.

Zach said that "dmabuf" is not a hard requirement. Another "anon_inode" would probably be OK as long as the app cannot turn on any permissions besides PCM access. Our security team will just need to review the changes.

So I think you should proceed with the "anon_inode:snd-pcm" if you think that will be more secure. Thanks for proposing this.

Zach has some notes in his initial review of Jaroslav's code. Zach?

One thing Zach mentioned is that the API should only allow *removing* permissions and not adding permissions.

What permissions would be set on the FD given to the app?

Quoted 13 lines

Also Mark mentioned that the FD app would have PCM access and "close" permission. What flag allows close? What else is permitted under that flag? Or is close permission just a generic "FD" permission unrelated to ALSA?

Thanks for all your work on this. Sorry if I caused alarm. I just wanted to make sure we could use the solution you provide.

Phil Burk

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#14 lore raw
Quoted 7 lines · Zach Riggle 🖖

The big concerns on our end are:

(1) Can the buffer be mremap()ed with a different offset into the buffer? This was a concern in the past and the reason for the anon_inode stuff at all.  I believe that as long as the *size* of the mapping doesn't change, Linux mm will gladly permit mremap() without informing the driver.

Could you elaborate which perspective of mremap() can be a big problem? The driver interface does nothing but a standard mmap for now.

Quoted 3 lines

(2) Can we ensure that permissions can only ever be dropped? (new_perms = old_perms & requested_perms) . It's probably useful to throw an error code if new permissions are requested.

The permission is bound with each fd, and determined only at creation time, and unchangeable.

Also, the patches Jaroslav posted can become even simpler / safer; i.e. we don't need to introduce the perms bits as a start. IMO, we can begin with the minimum, mmap-only file ops. The API should be defined properly from the beginning (e.g. passing perms argument, etc), of course. Then, if any request comes up, we may extend later.

Quoted 2 lines

(3) It looks like the code for snd_pcm_anonymous_dup in the patchset takes a perm argument from user-space and discards it.  Is this intended?

My understanding is that it's the design to be simple. But we don't need to stick with it, if you can suggest any better interface.

Quoted 5 lines

(4) I'm not familiar with the lifecycle of all of the objects, and introducing a custom dup routine might cause unexpected problems (e.g. use-after-free, double-free).  I'm not well-versed-enough in how the driver-specific stuff is handled underneath e.g. snd_card_file_add / snd_card_file_remove to be sure about it.

That's the advantage of anon-dup implementation; the PCM stream handling with O_APPEND has been heavily used by alsa-lib dmix PCM implementations. So it already survives over decades.

thanks,

Takashi

Quoted 26 lines

Zach Riggle |  Android Security |  [email protected] |  Austin, TX 🇨🇱

On Thu, Jan 31, 2019 at 1:36 PM Phil Burk <[email protected]> wrote:

Mark and Zach and I talked.

Zach said that "dmabuf" is not a hard requirement. Another "anon_inode" would probably be OK as long as the app cannot turn on any permissions besides PCM access. Our security team will just need to review the changes.

So I think you should proceed with the "anon_inode:snd-pcm" if you think that will be more secure. Thanks for proposing this.

Zach has some notes in his initial review of Jaroslav's code. Zach?

One thing Zach mentioned is that the API should only allow removing permissions and not adding permissions.

What permissions would be set on the FD given to the app?

Also Mark mentioned that the FD app would have PCM access and "close" permission. What flag allows close? What else is permitted under that flag? Or is close permission  just a generic "FD" permission unrelated to ALSA?

Quoted 6 lines

Thanks for all your work on this. Sorry if I caused alarm. I just wanted to make sure we could use the solution you provide.

Phil Burk

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

author maintainer
#15 lore raw

Dne 31.1.2019 v 21:32 Takashi Iwai napsal(a):

Quoted 5 lines
Quoted 3 lines · Zach Riggle 🖖

The big concerns on our end are:

Quoted 8 lines
Quoted 4 lines

(1) Can the buffer be mremap()ed with a different offset into the buffer? This was a concern in the past and the reason for the anon_inode stuff at all.  I believe that as long as the *size* of the mapping doesn't change, Linux mm will gladly permit mremap() without informing the driver.

Could you elaborate which perspective of mremap() can be a big problem? The driver interface does nothing but a standard mmap for now.

Yes, basically, the sound buffer (including it's size) is locked in the kernel space until the last mmaped area is active as it should be. See substream->mmap_count . The mmap implementation is similar what the dma-buf file interface does.

Quoted 6 lines
Quoted 3 lines

(2) Can we ensure that permissions can only ever be dropped? (new_perms = old_perms & requested_perms) . It's probably useful to throw an error code if new permissions are requested.

The permission is bound with each fd, and determined only at creation time, and unchangeable.

Exactly. The caller who creates the duplicated file descriptor should specify the permissions which are locked for the lifetime of the restricted file descriptor.

Quoted 5 lines

Also, the patches Jaroslav posted can become even simpler / safer; i.e. we don't need to introduce the perms bits as a start. IMO, we can begin with the minimum, mmap-only file ops. The API should be defined properly from the beginning (e.g. passing perms argument, etc), of course. Then, if any request comes up, we may extend later.

I agree. We can have just two modes for the beginning:

a) full one (useful for testing)
b) buffer only (allow just sound data mmap)

The question, if we should use different names (line anon_inode:snd-pcm and anon_inode:snd-pcm-buffer) for the anonymous inodes remains. I believe it might be good to distinguish this to allow the proper SELinux audit.

Quoted 5 lines
Quoted 2 lines

(3) It looks like the code for snd_pcm_anonymous_dup in the patchset takes a perm argument from user-space and discards it.  Is this intended?

My understanding is that it's the design to be simple. But we don't need to stick with it, if you can suggest any better interface.

Yes, I preferred the simplicity (so only one integer argument in and out).

Quoted 9 lines
Quoted 5 lines

(4) I'm not familiar with the lifecycle of all of the objects, and introducing a custom dup routine might cause unexpected problems (e.g. use-after-free, double-free).  I'm not well-versed-enough in how the driver-specific stuff is handled underneath e.g. snd_card_file_add / snd_card_file_remove to be sure about it.

That's the advantage of anon-dup implementation; the PCM stream handling with O_APPEND has been heavily used by alsa-lib dmix PCM implementations. So it already survives over decades.

Yes, there is simple reference counter which contains the count of the file decriptors assigned to the pcm substream - substream->ref_count . See snd_pcm_release_substream() for more details.

Quoted 4 lines
Quoted 4 lines · Phil Burk
Quoted 1 line

permission. What flag allows close? What else is permitted under that

flag? Or is close permission  just a generic "FD" permission unrelated to ALSA?

All file descriptors must have defined the 'release' callback in the kernel to free the resources when the close syscall is executed.

Jaroslav

Signature · 6 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#16 lore raw
Quoted 9 lines · Jaroslav Kysela

I agree. We can have just two modes for the beginning:

a) full one (useful for testing)
b) buffer only (allow just sound data mmap)

The question, if we should use different names (line anon_inode:snd-pcm and anon_inode:snd-pcm-buffer) for the anonymous inodes remains. I believe it might be good to distinguish this to allow the proper SELinux audit.

I agree that the separte names seems better, it gives more flexibility and control to people writing policies.

#17 lore raw

Thank you all for sorting this out. It seems like we are moving in a really good direction.

Quoted 3 lines

I agree. We can have just two modes for the beginning:
a) full one (useful for testing)
b) buffer only (allow just sound data mmap)

The full one is would also be used by the HAL for querying the DSP position.

Quoted 1 line

if we should use different names (like anon_inode:snd-pcm and

anon_inode:snd-pcm-buffer)

That would be helpful.

I have attached a revised requirements doc. The original doc was more of a HowTo for OEMs to create the "anon_inode:dmabuf" FD.. This clarifies the requirements and allows for the use of "anon_inode:snd-pcm*". It should match what we have arrived at by discussion. Let me know if it makes sense.

Thanks,
Phil Burk

Quoted 15 lines · Mark Brown
Quoted 9 lines · Jaroslav Kysela

I agree. We can have just two modes for the beginning:

a) full one (useful for testing)
b) buffer only (allow just sound data mmap)

The question, if we should use different names (line anon_inode:snd-pcm and anon_inode:snd-pcm-buffer) for the anonymous inodes remains. I believe it might be good to distinguish this to allow the proper SELinux audit.

I agree that the separte names seems better, it gives more flexibility and control to people writing policies.

author maintainer
#18 lore raw

Dne 1.2.2019 v 16:31 Phil Burk napsal(a):

Quoted 12 lines

Thank you all for sorting this out. It seems like we are moving in a really good direction.

Quoted 3 lines

I agree. We can have just two modes for the beginning:
a) full one (useful for testing)
b) buffer only (allow just sound data mmap)

The full one is would also be used by the HAL for querying the DSP position.

Quoted 1 line

  if we should use different names (like anon_inode:snd-pcm and

anon_inode:snd-pcm-buffer)

Quoted 7 lines

That would be helpful.

I have attached a revised requirements doc. The original doc was more of a HowTo for OEMs to create the "anon_inode:dmabuf" FD.. This clarifies the requirements and allows for the use of "anon_inode:snd-pcm*". It should match what we have arrived at by discussion. Let me know if it makes sense.

It looks fine, but the HAL will use probably the standard sound device open (/dev/snd/), doesn't? So:

fd1 - /dev/snd/pcm (HAL) - standard sound device inode (no restrictions) fd2 - anon_inode:snd-pcm-buffer (for the EXCLUSIVE access)

With modes, I talked about the anonymous dup ioctl parameter. If there's another resource manager above HAL, the situation might be:

fd1 - /dev/snd/pcm (resource manager) - standard sound device inode fd2 - anon_inode:snd-pcm (access to the full pcm sound API using the anonymous inode)
fd3 - anon_inode:snd-pcm-buffer (for the EXCLUSIVE access)

Perhaps you have different layers in Android.

Thanks,
Jaroslav

Signature · 6 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#19 lore raw

Thanks for the clarification. I was thinking the anon_inode:snd-pcm FD replaced the /dev/snd/pcm FD.

Quoted 2 lines

fd1 - /dev/snd/pcm (HAL) - standard sound device inode (no restrictions) fd2 - anon_inode:snd-pcm-buffer (for the EXCLUSIVE access)

That sounds right.

Thanks again for this work. ALSA is very important for Android Audio.

Phil Burk

Quoted 46 lines · Jaroslav Kysela

Dne 1.2.2019 v 16:31 Phil Burk napsal(a):

Quoted 8 lines

Thank you all for sorting this out. It seems like we are moving in a really good direction.

Quoted 3 lines

I agree. We can have just two modes for the beginning:
a) full one (useful for testing)
b) buffer only (allow just sound data mmap)

The full one is would also be used by the HAL for querying the DSP

position.

Quoted 11 lines
Quoted 2 lines

if we should use different names (like anon_inode:snd-pcm and

anon_inode:snd-pcm-buffer)

That would be helpful.

I have attached a revised requirements doc. The original doc was more of a HowTo for OEMs to create the "anon_inode:dmabuf" FD.. This clarifies the requirements and allows for the use of "anon_inode:snd-pcm*". It should match what we have arrived at by discussion. Let me know if it makes sense.

It looks fine, but the HAL will use probably the standard sound device open (/dev/snd/), doesn't? So:

fd1 - /dev/snd/pcm (HAL) - standard sound device inode (no restrictions) fd2 - anon_inode:snd-pcm-buffer (for the EXCLUSIVE access)

With modes, I talked about the anonymous dup ioctl parameter. If there's another resource manager above HAL, the situation might be:

fd1 - /dev/snd/pcm (resource manager) - standard sound device inode fd2 - anon_inode:snd-pcm (access to the full pcm sound API using the anonymous inode)
fd3 - anon_inode:snd-pcm-buffer (for the EXCLUSIVE access)

Perhaps you have different layers in Android.

Thanks,
Jaroslav

--
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.

#20 lore raw
In reply to Takashi Iwai
Quoted 11 lines · Takashi Iwai
Quoted 4 lines · Zach Riggle 🖖

(1) Can the buffer be mremap()ed with a different offset into the buffer? This was a concern in the past and the reason for the anon_inode stuff at all.  I believe that as long as the *size* of the mapping doesn't change, Linux mm will gladly permit mremap() without informing the driver.

Could you elaborate which perspective of mremap() can be a big problem? The driver interface does nothing but a standard mmap for now.

I believe the issue was that if someone could remap the buffer to gain access to memory outside the memory allocated for the PCM buffer that would be a problem. To be honest I'm surprised that this might be a general issue with mmap().