[LTP] [PATCH v1] iocl11.c: New case check PROCMAP_QUERY ioctl() errnos
Wei Gao
wegao@suse.com
Thu Aug 7 15:48:58 CEST 2025
Signed-off-by: Wei Gao <wegao@suse.com>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/ioctl/.gitignore | 1 +
testcases/kernel/syscalls/ioctl/ioctl11.c | 183 +++++++++++++++++++++
3 files changed, 185 insertions(+)
create mode 100644 testcases/kernel/syscalls/ioctl/ioctl11.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 6a17a34f8..8bc7f60d2 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -588,6 +588,7 @@ ioctl07 ioctl07
ioctl08 ioctl08
ioctl09 ioctl09
ioctl10 ioctl10
+ioctl11 ioctl11
ioctl_loop01 ioctl_loop01
ioctl_loop02 ioctl_loop02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index dac4583fa..54685e916 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -8,6 +8,7 @@
/ioctl08
/ioctl09
/ioctl10
+/ioctl11
/ioctl_loop01
/ioctl_loop02
/ioctl_loop03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl11.c b/testcases/kernel/syscalls/ioctl/ioctl11.c
new file mode 100644
index 000000000..aef2105cc
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl11.c
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Wei Gao <wegao@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test PROCMAP_QUERY ioctl() errnos:
+ *
+ * - EINVAL if q->size is too small
+ * - E2BIG if q->size is larger than page size
+ * - EINVAL on invalid q->flags
+ * - EINVAL if only one of q->vma_name_size and q->vma_name_addr is set
+ * - EINVAL if only one of q->build_id_size and q->build_id_addr is set
+ * - ENAMETOOLONG if build_id_size or name_buf_size is too small
+ */
+
+#include "config.h"
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+#include <fnmatch.h>
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
+#include <sys/sysmacros.h>
+#include <linux/fs.h>
+#include "lapi/ioctl.h"
+
+#define PROC_MAP_PATH "/proc/self/maps"
+
+struct procmap_query *q;
+static int fd = -1;
+static char buf[PATH_MAX];
+static char small_buf[1];
+
+static void setup_normal(void);
+static void setup_big_size(void);
+
+static struct tcase {
+ uint64_t size;
+ uint64_t query_addr;
+ uint64_t query_flags;
+ uint64_t vma_name_addr;
+ uint32_t vma_name_size;
+ uint64_t build_id_addr;
+ uint32_t build_id_size;
+ int exp_errno;
+ void (*setup)(void);
+} tcases[] = {
+ {
+ .size = 1,
+ .exp_errno = EINVAL,
+ .setup = setup_normal
+ },
+ {
+ .exp_errno = E2BIG,
+ .setup = setup_big_size
+ },
+ {
+ .query_flags = -1,
+ .exp_errno = EINVAL,
+ .setup = setup_normal
+ },
+ {
+ .vma_name_size = sizeof(buf),
+ .exp_errno = EINVAL,
+ .setup = setup_normal
+ },
+ {
+ .vma_name_addr = (uint64_t)(unsigned long)buf,
+ .exp_errno = EINVAL,
+ .setup = setup_normal
+ },
+ {
+ .build_id_size = sizeof(buf),
+ .exp_errno = EINVAL,
+ .setup = setup_normal
+ },
+ {
+ .build_id_addr = (uint64_t)(unsigned long)buf,
+ .exp_errno = EINVAL,
+ .setup = setup_normal
+ },
+ {
+ .vma_name_addr = (uint64_t)(unsigned long)small_buf,
+ .vma_name_size = sizeof(small_buf),
+ .exp_errno = ENAMETOOLONG,
+ .setup = setup_normal
+ },
+ {
+ .build_id_addr = (uint64_t)(unsigned long)small_buf,
+ .build_id_size = sizeof(small_buf),
+ .exp_errno = ENAMETOOLONG,
+ .setup = setup_normal
+ },
+};
+
+static unsigned long get_vm_start(void)
+{
+ FILE *fp = SAFE_FOPEN(PROC_MAP_PATH, "r");
+ char line[1024];
+ unsigned long start_addr = 0;
+
+ if (fgets(line, sizeof(line), fp) != NULL) {
+ if (sscanf(line, "%lx-", &start_addr) != 1)
+ tst_brk(TFAIL, "parse maps file /proc/self/maps failed");
+ return start_addr;
+ }
+
+ SAFE_FCLOSE(fp);
+ tst_brk(TFAIL, "parse maps file /proc/self/maps failed");
+}
+
+static void setup_normal(void)
+{
+ q->size = sizeof(*q);
+ q->query_addr = (uint64_t)get_vm_start();
+ q->query_flags = 0;
+}
+
+static void setup_big_size(void)
+{
+ setup_normal();
+ q->size = getpagesize() + 1;
+}
+
+static void run(unsigned int n)
+{
+ struct tcase *tc = &tcases[n];
+
+ memset(q, 0, sizeof(*q));
+
+ tc->setup();
+
+ if (tc->size != 0)
+ q->size = tc->size;
+ if (tc->query_flags != 0)
+ q->query_flags = tc->query_flags;
+ if (tc->vma_name_addr != 0)
+ q->vma_name_addr = tc->vma_name_addr;
+ if (tc->vma_name_size != 0)
+ q->vma_name_size = tc->vma_name_size;
+ if (tc->build_id_addr != 0)
+ q->build_id_addr = tc->build_id_addr;
+ if (tc->build_id_size != 0)
+ q->build_id_size = tc->build_id_size;
+
+ TST_EXP_FAIL(ioctl(fd, PROCMAP_QUERY, q), tc->exp_errno);
+}
+
+static void setup(void)
+{
+ struct procmap_query q = {};
+
+ fd = SAFE_OPEN(PROC_MAP_PATH, O_RDONLY);
+
+ if (tst_kvercmp(6, 11, 0) < 0) {
+ TEST(ioctl(fd, PROCMAP_QUERY, q));
+
+ if ((TST_RET == -1) && (TST_ERR == ENOTTY))
+ tst_brk(TCONF,
+ "This system does not provide support for ioctl(PROCMAP_QUERY)");
+ }
+
+}
+
+static void cleanup(void)
+{
+ if (fd != -1)
+ SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test = run,
+ .tcnt = ARRAY_SIZE(tcases),
+ .cleanup = cleanup,
+ .bufs = (struct tst_buffers []) {
+ {&q, .size = sizeof(*q)},
+ {}
+ }
+};
--
2.49.0
More information about the ltp
mailing list