Commit 36df2427 authored by Takashi Iwai's avatar Takashi Iwai

ALSA: pcm: Add more disconnection checks at file ops

In the case of hot-disconnection of a PCM device, all file operations
except for close should be rejected.  This patch adds more sanity
checks in the file operation code paths.

Link: https://lore.kernel.org/r/20211006142214.3089-1-tiwai@suse.deSigned-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent 23939115
...@@ -3218,6 +3218,9 @@ static int snd_pcm_common_ioctl(struct file *file, ...@@ -3218,6 +3218,9 @@ static int snd_pcm_common_ioctl(struct file *file,
if (PCM_RUNTIME_CHECK(substream)) if (PCM_RUNTIME_CHECK(substream))
return -ENXIO; return -ENXIO;
if (substream->runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
return -EBADFD;
res = snd_power_wait(substream->pcm->card); res = snd_power_wait(substream->pcm->card);
if (res < 0) if (res < 0)
return res; return res;
...@@ -3344,6 +3347,9 @@ int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, ...@@ -3344,6 +3347,9 @@ int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
snd_pcm_uframes_t *frames = arg; snd_pcm_uframes_t *frames = arg;
snd_pcm_sframes_t result; snd_pcm_sframes_t result;
if (substream->runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
return -EBADFD;
switch (cmd) { switch (cmd) {
case SNDRV_PCM_IOCTL_FORWARD: case SNDRV_PCM_IOCTL_FORWARD:
{ {
...@@ -3386,7 +3392,8 @@ static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, ...@@ -3386,7 +3392,8 @@ static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
if (PCM_RUNTIME_CHECK(substream)) if (PCM_RUNTIME_CHECK(substream))
return -ENXIO; return -ENXIO;
runtime = substream->runtime; runtime = substream->runtime;
if (runtime->status->state == SNDRV_PCM_STATE_OPEN) if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
return -EBADFD; return -EBADFD;
if (!frame_aligned(runtime, count)) if (!frame_aligned(runtime, count))
return -EINVAL; return -EINVAL;
...@@ -3410,7 +3417,8 @@ static ssize_t snd_pcm_write(struct file *file, const char __user *buf, ...@@ -3410,7 +3417,8 @@ static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
if (PCM_RUNTIME_CHECK(substream)) if (PCM_RUNTIME_CHECK(substream))
return -ENXIO; return -ENXIO;
runtime = substream->runtime; runtime = substream->runtime;
if (runtime->status->state == SNDRV_PCM_STATE_OPEN) if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
return -EBADFD; return -EBADFD;
if (!frame_aligned(runtime, count)) if (!frame_aligned(runtime, count))
return -EINVAL; return -EINVAL;
...@@ -3436,7 +3444,8 @@ static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to) ...@@ -3436,7 +3444,8 @@ static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
if (PCM_RUNTIME_CHECK(substream)) if (PCM_RUNTIME_CHECK(substream))
return -ENXIO; return -ENXIO;
runtime = substream->runtime; runtime = substream->runtime;
if (runtime->status->state == SNDRV_PCM_STATE_OPEN) if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
return -EBADFD; return -EBADFD;
if (!iter_is_iovec(to)) if (!iter_is_iovec(to))
return -EINVAL; return -EINVAL;
...@@ -3472,7 +3481,8 @@ static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from) ...@@ -3472,7 +3481,8 @@ static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
if (PCM_RUNTIME_CHECK(substream)) if (PCM_RUNTIME_CHECK(substream))
return -ENXIO; return -ENXIO;
runtime = substream->runtime; runtime = substream->runtime;
if (runtime->status->state == SNDRV_PCM_STATE_OPEN) if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
return -EBADFD; return -EBADFD;
if (!iter_is_iovec(from)) if (!iter_is_iovec(from))
return -EINVAL; return -EINVAL;
...@@ -3511,6 +3521,9 @@ static __poll_t snd_pcm_poll(struct file *file, poll_table *wait) ...@@ -3511,6 +3521,9 @@ static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
return ok | EPOLLERR; return ok | EPOLLERR;
runtime = substream->runtime; runtime = substream->runtime;
if (runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
return ok | EPOLLERR;
poll_wait(file, &runtime->sleep, wait); poll_wait(file, &runtime->sleep, wait);
mask = 0; mask = 0;
...@@ -3820,6 +3833,8 @@ static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area) ...@@ -3820,6 +3833,8 @@ static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
substream = pcm_file->substream; substream = pcm_file->substream;
if (PCM_RUNTIME_CHECK(substream)) if (PCM_RUNTIME_CHECK(substream))
return -ENXIO; return -ENXIO;
if (substream->runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
return -EBADFD;
offset = area->vm_pgoff << PAGE_SHIFT; offset = area->vm_pgoff << PAGE_SHIFT;
switch (offset) { switch (offset) {
...@@ -3856,6 +3871,8 @@ static int snd_pcm_fasync(int fd, struct file * file, int on) ...@@ -3856,6 +3871,8 @@ static int snd_pcm_fasync(int fd, struct file * file, int on)
if (PCM_RUNTIME_CHECK(substream)) if (PCM_RUNTIME_CHECK(substream))
return -ENXIO; return -ENXIO;
runtime = substream->runtime; runtime = substream->runtime;
if (runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
return -EBADFD;
return fasync_helper(fd, file, on, &runtime->fasync); return fasync_helper(fd, file, on, &runtime->fasync);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment