diff mbox series

[v2,10/10] Add setxattrat02 test

Message ID 20251007-xattrat-v2-10-bf458fa66358@suse.com
State Changes Requested
Delegated to: Andrea Cervesato
Headers show
Series setxattrat coverage | expand

Checks

Context Check Description
ltpci/debian_oldstable_gcc fail failure
ltpci/debian_stable_s390x-linux-gnu-gcc_s390x success success
ltpci/debian_stable_powerpc64le-linux-gnu-gcc_ppc64el success success
ltpci/debian_stable_aarch64-linux-gnu-gcc_arm64 success success
ltpci/ubuntu_jammy_gcc success success
ltpci/quay-io-centos-centos_stream9_gcc success success
ltpci/debian_stable_gcc success success
ltpci/ubuntu_bionic_gcc success success
ltpci/opensuse-leap_latest_gcc success success
ltpci/debian_testing_clang success success
ltpci/fedora_latest_clang success success
ltpci/alpine_latest_gcc success success
ltpci/debian_testing_gcc success success
ltpci/debian_oldstable_clang success success
ltpci/debian_stable_gcc success success
ltpci/opensuse-archive_42-2_gcc success success
ltpci/debian_oldstable_gcc success success

Commit Message

Andrea Cervesato Oct. 7, 2025, 6:47 a.m. UTC
From: Andrea Cervesato <andrea.cervesato@suse.com>

Test if setxattrat() syscall is correctly raising errors when giving
invalid inputs.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/syscalls/setxattrat/.gitignore    |   1 +
 .../kernel/syscalls/setxattrat/setxattrat02.c      | 126 +++++++++++++++++++++
 2 files changed, 127 insertions(+)

Comments

Cyril Hrubis Oct. 8, 2025, 10:02 a.m. UTC | #1
Hi!
> +} tcases[] = {
> +	{
> +		.dfd = &invalid_fd,
> +		.args = &args,
> +		.args_size = sizeof(struct xattr_args),
> +		.exp_errno = EBADF,
> +		.reason = "Invalid directory file descriptor",
> +	},

Maybe it would make sense to use tst_fd to hammer the syscall with all
kinds of strange file descriptors.

> +	{
> +		.dfd = &tmpdir_fd,
> +		.at_flags = -1,
> +		.args = &args,
> +		.args_size = sizeof(struct xattr_args),
> +		.exp_errno = EINVAL,
> +		.reason = "Invalid AT flags",
> +	},
> +	{
> +		.dfd = &tmpdir_fd,
> +		.at_flags = AT_SYMLINK_NOFOLLOW + 1,
> +		.args = &args,
> +		.args_size = sizeof(struct xattr_args),
> +		.exp_errno = EINVAL,
> +		.reason = "Out of bound AT flags",
> +	},
> +	{
> +		.dfd = &tmpdir_fd,
> +		.args = &null_args,
> +		.args_size = sizeof(struct xattr_args),
> +		.exp_errno = EINVAL,
> +		.reason = "Invalid arguments",
> +	},
> +	{
> +		.dfd = &tmpdir_fd,
> +		.args = &args,
> +		.args_size = SIZE_MAX,
> +		.exp_errno = E2BIG,
> +		.reason = "Arguments size is too big",
> +	},
> +	{
> +		.dfd = &tmpdir_fd,
> +		.args = &args,
> +		.args_size = sizeof(struct xattr_args) - 1,
> +		.exp_errno = EINVAL,
> +		.reason = "Invalid arguments size",
> +	},
> +};
> +
> +static void run(unsigned int i)
> +{
> +	struct tcase *tc = &tcases[i];
> +
> +	args->flags = XATTR_CREATE;
> +	args->value = (uint64_t)XATTR_TEST_VALUE;
> +	args->size = XATTR_TEST_VALUE_SIZE;
> +
> +	TST_EXP_FAIL(tst_syscall(__NR_setxattrat,
> +		tc->dfd, FNAME, tc->at_flags, XATTR_TEST_KEY,
> +		tc->args, tc->args_size),
> +		tc->exp_errno, "%s", tc->reason);
> +}
> +
> +static void setup(void)
> +{
> +	char *tmpdir;
> +
> +	tmpdir = tst_tmpdir_path();
> +	tmpdir_fd = SAFE_OPEN(tmpdir, O_DIRECTORY);

Here as well, just use AT_FDCWD.

> +	SAFE_TOUCH(FNAME, 0777, NULL);
> +}
> +
> +static void cleanup(void)
> +{
> +	if (tmpdir_fd != -1)
> +		SAFE_CLOSE(tmpdir_fd);
> +
> +	SAFE_UNLINK(FNAME);
> +}
> +
> +static struct tst_test test = {
> +	.test = run,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.tcnt = ARRAY_SIZE(tcases),
> +	.needs_root = 1,
> +	.needs_tmpdir = 1,
> +	.bufs = (struct tst_buffers []) {
> +		{&args, .size = sizeof(struct xattr_args)},
> +		{},
> +	}
> +};
> 
> -- 
> 2.51.0
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp
Andrea Cervesato Oct. 10, 2025, 11:05 a.m. UTC | #2
Hi,

On Wed Oct 8, 2025 at 12:02 PM CEST, Cyril Hrubis wrote:
> Hi!
>> +} tcases[] = {
>> +	{
>> +		.dfd = &invalid_fd,
>> +		.args = &args,
>> +		.args_size = sizeof(struct xattr_args),
>> +		.exp_errno = EBADF,
>> +		.reason = "Invalid directory file descriptor",
>> +	},
>
> Maybe it would make sense to use tst_fd to hammer the syscall with all
> kinds of strange file descriptors.
>

This makes sense for a new test, in this test it's better to keep this
approach.

>> +	{
>> +		.dfd = &tmpdir_fd,
>> +		.at_flags = -1,
>> +		.args = &args,
>> +		.args_size = sizeof(struct xattr_args),
>> +		.exp_errno = EINVAL,
>> +		.reason = "Invalid AT flags",
>> +	},
>> +	{
>> +		.dfd = &tmpdir_fd,
>> +		.at_flags = AT_SYMLINK_NOFOLLOW + 1,
>> +		.args = &args,
>> +		.args_size = sizeof(struct xattr_args),
>> +		.exp_errno = EINVAL,
>> +		.reason = "Out of bound AT flags",
>> +	},
>> +	{
>> +		.dfd = &tmpdir_fd,
>> +		.args = &null_args,
>> +		.args_size = sizeof(struct xattr_args),
>> +		.exp_errno = EINVAL,
>> +		.reason = "Invalid arguments",
>> +	},
>> +	{
>> +		.dfd = &tmpdir_fd,
>> +		.args = &args,
>> +		.args_size = SIZE_MAX,
>> +		.exp_errno = E2BIG,
>> +		.reason = "Arguments size is too big",
>> +	},
>> +	{
>> +		.dfd = &tmpdir_fd,
>> +		.args = &args,
>> +		.args_size = sizeof(struct xattr_args) - 1,
>> +		.exp_errno = EINVAL,
>> +		.reason = "Invalid arguments size",
>> +	},
>> +};
>> +
>> +static void run(unsigned int i)
>> +{
>> +	struct tcase *tc = &tcases[i];
>> +
>> +	args->flags = XATTR_CREATE;
>> +	args->value = (uint64_t)XATTR_TEST_VALUE;
>> +	args->size = XATTR_TEST_VALUE_SIZE;
>> +
>> +	TST_EXP_FAIL(tst_syscall(__NR_setxattrat,
>> +		tc->dfd, FNAME, tc->at_flags, XATTR_TEST_KEY,
>> +		tc->args, tc->args_size),
>> +		tc->exp_errno, "%s", tc->reason);
>> +}
>> +
>> +static void setup(void)
>> +{
>> +	char *tmpdir;
>> +
>> +	tmpdir = tst_tmpdir_path();
>> +	tmpdir_fd = SAFE_OPEN(tmpdir, O_DIRECTORY);
>
> Here as well, just use AT_FDCWD.
>

Ok.

>> +	SAFE_TOUCH(FNAME, 0777, NULL);
>> +}
>> +
>> +static void cleanup(void)
>> +{
>> +	if (tmpdir_fd != -1)
>> +		SAFE_CLOSE(tmpdir_fd);
>> +
>> +	SAFE_UNLINK(FNAME);
>> +}
>> +
>> +static struct tst_test test = {
>> +	.test = run,
>> +	.setup = setup,
>> +	.cleanup = cleanup,
>> +	.tcnt = ARRAY_SIZE(tcases),
>> +	.needs_root = 1,
>> +	.needs_tmpdir = 1,
>> +	.bufs = (struct tst_buffers []) {
>> +		{&args, .size = sizeof(struct xattr_args)},
>> +		{},
>> +	}
>> +};
>> 
>> -- 
>> 2.51.0
>> 
>> 
>> -- 
>> Mailing list info: https://lists.linux.it/listinfo/ltp
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/setxattrat/.gitignore b/testcases/kernel/syscalls/setxattrat/.gitignore
index e636401d7..9d007a44d 100644
--- a/testcases/kernel/syscalls/setxattrat/.gitignore
+++ b/testcases/kernel/syscalls/setxattrat/.gitignore
@@ -1 +1,2 @@ 
 setxattrat01
+setxattrat02
diff --git a/testcases/kernel/syscalls/setxattrat/setxattrat02.c b/testcases/kernel/syscalls/setxattrat/setxattrat02.c
new file mode 100644
index 000000000..8092fef26
--- /dev/null
+++ b/testcases/kernel/syscalls/setxattrat/setxattrat02.c
@@ -0,0 +1,126 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2025 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test if setxattrat() syscall is correctly raising errors when giving invalid
+ * inputs.
+ */
+
+#include "tst_test.h"
+#include "lapi/xattr.h"
+#include "lapi/syscalls.h"
+
+#include <sys/xattr.h>
+
+#define FNAME "ltp_file"
+#define XATTR_TEST_KEY "trusted.ltptestkey"
+#define XATTR_TEST_VALUE "ltprulez"
+#define XATTR_TEST_VALUE_SIZE 8
+
+static struct xattr_args *args;
+static struct xattr_args *null_args;
+static int invalid_fd = -1;
+static int tmpdir_fd = -1;
+
+static struct tcase {
+	int *dfd;
+	int at_flags;
+	struct xattr_args **args;
+	size_t args_size;
+	int exp_errno;
+	char *reason;
+} tcases[] = {
+	{
+		.dfd = &invalid_fd,
+		.args = &args,
+		.args_size = sizeof(struct xattr_args),
+		.exp_errno = EBADF,
+		.reason = "Invalid directory file descriptor",
+	},
+	{
+		.dfd = &tmpdir_fd,
+		.at_flags = -1,
+		.args = &args,
+		.args_size = sizeof(struct xattr_args),
+		.exp_errno = EINVAL,
+		.reason = "Invalid AT flags",
+	},
+	{
+		.dfd = &tmpdir_fd,
+		.at_flags = AT_SYMLINK_NOFOLLOW + 1,
+		.args = &args,
+		.args_size = sizeof(struct xattr_args),
+		.exp_errno = EINVAL,
+		.reason = "Out of bound AT flags",
+	},
+	{
+		.dfd = &tmpdir_fd,
+		.args = &null_args,
+		.args_size = sizeof(struct xattr_args),
+		.exp_errno = EINVAL,
+		.reason = "Invalid arguments",
+	},
+	{
+		.dfd = &tmpdir_fd,
+		.args = &args,
+		.args_size = SIZE_MAX,
+		.exp_errno = E2BIG,
+		.reason = "Arguments size is too big",
+	},
+	{
+		.dfd = &tmpdir_fd,
+		.args = &args,
+		.args_size = sizeof(struct xattr_args) - 1,
+		.exp_errno = EINVAL,
+		.reason = "Invalid arguments size",
+	},
+};
+
+static void run(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+
+	args->flags = XATTR_CREATE;
+	args->value = (uint64_t)XATTR_TEST_VALUE;
+	args->size = XATTR_TEST_VALUE_SIZE;
+
+	TST_EXP_FAIL(tst_syscall(__NR_setxattrat,
+		tc->dfd, FNAME, tc->at_flags, XATTR_TEST_KEY,
+		tc->args, tc->args_size),
+		tc->exp_errno, "%s", tc->reason);
+}
+
+static void setup(void)
+{
+	char *tmpdir;
+
+	tmpdir = tst_tmpdir_path();
+	tmpdir_fd = SAFE_OPEN(tmpdir, O_DIRECTORY);
+
+	SAFE_TOUCH(FNAME, 0777, NULL);
+}
+
+static void cleanup(void)
+{
+	if (tmpdir_fd != -1)
+		SAFE_CLOSE(tmpdir_fd);
+
+	SAFE_UNLINK(FNAME);
+}
+
+static struct tst_test test = {
+	.test = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.tcnt = ARRAY_SIZE(tcases),
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.bufs = (struct tst_buffers []) {
+		{&args, .size = sizeof(struct xattr_args)},
+		{},
+	}
+};