* [PATCH] tools: iio: Keep the buffer index separate from the acquired fd
@ 2026-11-20 6:19 Zhu Jun
2026-11-20 21:03 ` Arnav Kapoor
0 siblings, 1 reply; 2+ messages in thread
From: Zhu Jun @ 2026-11-20 6:19 UTC (permalink / raw)
To: jic23; +Cc: dlechner, nuno.sa, andy, linux-iio, linux-kernel, Zhu Jun
IIO_BUFFER_GET_FD_IOCTL takes the buffer index as input and replaces
it with a file descriptor on success. On failure, cleanup can mistake
the unchanged index for an open fd and close a different file. Only
assign the returned descriptor to buf_fd after successful acquisition.
Fixes: 8827faab2c8b ("tools: iio: convert iio_generic_buffer to use new IIO buffer API")
Signed-off-by: Zhu Jun <zhujun2@cmss.chinamobile.com>
---
tools/iio/iio_generic_buffer.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/tools/iio/iio_generic_buffer.c b/tools/iio/iio_generic_buffer.c
index 000193612aad..fd955cf444ad 100644
--- a/tools/iio/iio_generic_buffer.c
+++ b/tools/iio/iio_generic_buffer.c
@@ -404,7 +404,7 @@ int main(int argc, char **argv)
int ret, c;
struct stat st;
int fd = -1;
- int buf_fd = -1;
+ int buf_fd = -1, requested_fd;
int num_channels = 0;
char *trigger_name = NULL, *device_name = NULL;
@@ -714,11 +714,11 @@ int main(int argc, char **argv)
}
/* specify for which buffer index we want an FD */
- buf_fd = buffer_idx;
+ requested_fd = buffer_idx;
- ret = ioctl(fd, IIO_BUFFER_GET_FD_IOCTL, &buf_fd);
- if (ret == -1 || buf_fd == -1) {
- ret = -errno;
+ ret = ioctl(fd, IIO_BUFFER_GET_FD_IOCTL, &requested_fd);
+ if (ret < 0 || requested_fd < 0) {
+ ret = ret < 0 ? -errno : -EIO;
if (ret == -ENODEV || ret == -EINVAL)
fprintf(stderr,
"Device does not have this many buffers\n");
@@ -728,6 +728,8 @@ int main(int argc, char **argv)
goto error;
}
+ buf_fd = requested_fd;
+
/* Setup ring buffer parameters */
ret = write_sysfs_int("length", buf_dir_name, buf_len);
if (ret < 0)
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] tools: iio: Keep the buffer index separate from the acquired fd
2026-11-20 6:19 [PATCH] tools: iio: Keep the buffer index separate from the acquired fd Zhu Jun
@ 2026-11-20 21:03 ` Arnav Kapoor
0 siblings, 0 replies; 2+ messages in thread
From: Arnav Kapoor @ 2026-11-20 21:03 UTC (permalink / raw)
To: Zhu Jun; +Cc: jic23, dlechner, nuno.sa, andy, linux-iio, linux-kernel
On Fri, Nov 20, 2026 at 02:19:03PM +0800, Zhu Jun wrote:
> IIO_BUFFER_GET_FD_IOCTL takes the buffer index as input and replaces
> it with a file descriptor on success. On failure, cleanup can mistake
> the unchanged index for an open fd and close a different file. Only
> assign the returned descriptor to buf_fd after successful acquisition.
Checked against iio_device_buffer_getfd(): on every error path it
returns before copy_to_user(), so the argument keeps the index, and the
error: label then closes it because buf_fd >= 0. With the default
buffer index 0 that closes stdin, with -b 1 stdout. The patch fixes
that, buffer_idx is already rejected when negative, and it applies to
iio testing, builds without warnings and is checkpatch --strict clean.
Reviewed-by: Arnav Kapoor <kapoorarnav43@gmail.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-20 21:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 6:19 [PATCH] tools: iio: Keep the buffer index separate from the acquired fd Zhu Jun
2026-11-20 21:03 ` Arnav Kapoor
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®