diff mbox series

[v3,2/2] syscalls/lremovexattr: Add lremovexattr() test

Message ID 20181108183320.11262-2-rafael.tinoco@linaro.org
State Accepted
Commit 650c22c3a961c8a759b69f4f069acd1b43c1bd53
Headers show
Series [v3,1/2] syscalls/fremovexattr: Add fremovexattr() tests | expand

Commit Message

Rafael David Tinoco Nov. 8, 2018, 6:33 p.m. UTC
Fixes: #276

This commit implements a test for lremovexattr(). According to attr(5),
extended attributes are interpreted differently among files, directories
and symbolic links. User attributes are only allowed for regular files
and directories, thus the need to test security.* attributes being
removed from symbolic links.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/syscalls                              |   2 +
 .../kernel/syscalls/lremovexattr/.gitignore   |   1 +
 .../kernel/syscalls/lremovexattr/Makefile     |   8 ++
 .../syscalls/lremovexattr/lremovexattr01.c    | 132 ++++++++++++++++++
 4 files changed, 143 insertions(+)
 create mode 100644 testcases/kernel/syscalls/lremovexattr/.gitignore
 create mode 100644 testcases/kernel/syscalls/lremovexattr/Makefile
 create mode 100644 testcases/kernel/syscalls/lremovexattr/lremovexattr01.c

Comments

Cyril Hrubis Nov. 16, 2018, 7:37 p.m. UTC | #1
Hi!
Pushed with minor changes, see below, thanks.

diff --git a/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c b/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c
index 2cf46ebdf..26194f114 100644
--- a/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c
+++ b/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c
@@ -18,7 +18,7 @@
  * Note:
  * According to attr(5), extended attributes are interpreted differently from
  * regular files, directories and symbolic links. User attributes are only
- * allowed for regular files and directories, thus the need of using security.*
+ * allowed for regular files and directories, thus the need of using trusted.*
  * attributes for this test.
  */
 
@@ -39,7 +39,7 @@
 
 #define ENOATTR ENODATA
 
-#define XATTR_KEY		"security.key1"
+#define XATTR_KEY		"trusted.key1"
 #define XATTR_VALUE		"file and link"
 #define XATTR_VALUE_SIZE	13
 
@@ -79,8 +79,8 @@ static void verify_lremovexattr(void)
 		return;
 	}
 
-	if (TST_RET < 0 && TST_ERR != ENOATTR) {
-		tst_brk(TBROK, "lgetxattr(2) failed unexpectedly");
+	if (TST_ERR != ENOATTR) {
+		tst_brk(TBROK | TTERRNO, "lgetxattr(2) failed unexpectedly");
 		return;
 	}
 
@@ -111,10 +111,8 @@ static void setup(void)
 {
 	SAFE_TOUCH(FILENAME, 0644, NULL);
 
-	if (symlink(FILENAME, SYMLINK) < 0) {
+	if (symlink(FILENAME, SYMLINK) < 0)
 		tst_brk(TCONF, "symlink() not supported");
-		return;
-	}
 }
 
 static struct tst_test test = {



I've found that the trusted attributes works as well and cannot be
disabled in kernel (at least for ext3 security attributes have their own
CONFIG switches).

The rest is just removed unreachable or always true code.
Rafael David Tinoco Nov. 18, 2018, 1:37 p.m. UTC | #2
On 11/16/18 5:37 PM, Cyril Hrubis wrote:
> Hi!
> Pushed with minor changes, see below, thanks.

Thanks a lot Cyril!

Best,
diff mbox series

Patch

diff --git a/runtest/syscalls b/runtest/syscalls
index 24110b623..26c142bf8 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -582,6 +582,8 @@  llseek01 llseek01
 llseek02 llseek02
 llseek03 llseek03
 
+lremovexattr01 lremovexattr01
+
 lseek01 lseek01
 lseek02 lseek02
 lseek07 lseek07
diff --git a/testcases/kernel/syscalls/lremovexattr/.gitignore b/testcases/kernel/syscalls/lremovexattr/.gitignore
new file mode 100644
index 000000000..810f86214
--- /dev/null
+++ b/testcases/kernel/syscalls/lremovexattr/.gitignore
@@ -0,0 +1 @@ 
+lremovexattr01
diff --git a/testcases/kernel/syscalls/lremovexattr/Makefile b/testcases/kernel/syscalls/lremovexattr/Makefile
new file mode 100644
index 000000000..f71e4fc25
--- /dev/null
+++ b/testcases/kernel/syscalls/lremovexattr/Makefile
@@ -0,0 +1,8 @@ 
+# Copyright (c) 2018 - Linaro Limited. All rights reserved.
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
\ No newline at end of file
diff --git a/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c b/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c
new file mode 100644
index 000000000..2cf46ebdf
--- /dev/null
+++ b/testcases/kernel/syscalls/lremovexattr/lremovexattr01.c
@@ -0,0 +1,132 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Test Name: lremovexattr01
+ *
+ * Description:
+ * lremovexattr(2) removes the extended attribute identified by a name and
+ * associated with a given path in the filesystem. Unlike removexattr(2),
+ * lremovexattr(2) removes the attribute from the symbolic link only, and not
+ * the file. This test verifies that a simple call to lremovexattr(2) removes,
+ * indeed, a previously set attribute key/value from a symbolic link, and the
+ * symbolic link _only_.
+ *
+ * Note:
+ * According to attr(5), extended attributes are interpreted differently from
+ * regular files, directories and symbolic links. User attributes are only
+ * allowed for regular files and directories, thus the need of using security.*
+ * attributes for this test.
+ */
+
+#include "config.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+#endif
+
+#include "tst_test.h"
+
+#ifdef HAVE_SYS_XATTR_H
+
+#define ENOATTR ENODATA
+
+#define XATTR_KEY		"security.key1"
+#define XATTR_VALUE		"file and link"
+#define XATTR_VALUE_SIZE	13
+
+#define MNTPOINT "mntpoint"
+#define FILENAME MNTPOINT"/lremovexattr01testfile"
+#define SYMLINK  MNTPOINT"/lremovexattr01symlink"
+
+static char got_value[XATTR_VALUE_SIZE];
+
+static void verify_lremovexattr(void)
+{
+	/* set attribute on both: file and symlink */
+
+	SAFE_SETXATTR(FILENAME, XATTR_KEY, XATTR_VALUE, XATTR_VALUE_SIZE,
+			XATTR_CREATE);
+
+	SAFE_LSETXATTR(SYMLINK, XATTR_KEY, XATTR_VALUE, XATTR_VALUE_SIZE,
+			XATTR_CREATE);
+
+	/* remove attribute from symlink only */
+
+	TEST(lremovexattr(SYMLINK, XATTR_KEY));
+
+	if (TST_RET != 0) {
+		tst_res(TFAIL | TTERRNO, "lremovexattr(2) failed");
+		return;
+	}
+
+	/* make sure attribute is gone from symlink */
+
+	memset(&got_value, 0, XATTR_VALUE_SIZE);
+
+	TEST(lgetxattr(SYMLINK, XATTR_KEY, &got_value, XATTR_VALUE_SIZE));
+
+	if (TST_RET >= 0) {
+		tst_res(TFAIL, "lremovexattr(2) did not work");
+		return;
+	}
+
+	if (TST_RET < 0 && TST_ERR != ENOATTR) {
+		tst_brk(TBROK, "lgetxattr(2) failed unexpectedly");
+		return;
+	}
+
+	/* check if file is unchanged, like it should be */
+
+	memset(&got_value, 0, XATTR_VALUE_SIZE);
+
+	TEST(getxattr(FILENAME, XATTR_KEY, &got_value, XATTR_VALUE_SIZE));
+
+	if (TST_RET <= 0) {
+		tst_res(TFAIL, "lremovexattr(2) deleted file attribute");
+		return;
+	}
+
+	if (strcmp(got_value, XATTR_VALUE)) {
+		tst_res(TFAIL, "lremovexattr(2) changed file attribute");
+		return;
+	}
+
+	/* cleanup file attribute for iteration */
+
+	SAFE_REMOVEXATTR(FILENAME, XATTR_KEY);
+
+	tst_res(TPASS, "lremovexattr(2) removed attribute as expected");
+}
+
+static void setup(void)
+{
+	SAFE_TOUCH(FILENAME, 0644, NULL);
+
+	if (symlink(FILENAME, SYMLINK) < 0) {
+		tst_brk(TCONF, "symlink() not supported");
+		return;
+	}
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = verify_lremovexattr,
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
+	.all_filesystems = 1,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
+
+#else /* HAVE_SYS_XATTR_H */
+TST_TEST_TCONF("<sys/xattr.h> does not exist");
+#endif