• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016 Fujitsu Ltd.
4  * Author: Jinbao Huang <huangjb.jy@cn.fujitsu.com>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Verify that, lgetxattr(2) returns -1 and sets errno to
11  *
12  * 1. ENODATA if the named attribute does not exist.
13  * 2. ERANGE if the size of the value buffer is too small to hold the result.
14  * 3. EFAULT when reading from an invalid address.
15  */
16 
17 #include "config.h"
18 #include <errno.h>
19 #include <sys/types.h>
20 #include <string.h>
21 
22 #ifdef HAVE_SYS_XATTR_H
23 # include <sys/xattr.h>
24 #endif
25 
26 #include "tst_test.h"
27 
28 #ifdef HAVE_SYS_XATTR_H
29 
30 #define SECURITY_KEY	"security.ltptest"
31 #define VALUE	"this is a test value"
32 
33 static struct test_case {
34 	const char *path;
35 	size_t size;
36 	int exp_err;
37 } tcase[] = {
38 	{"testfile", sizeof(VALUE), ENODATA},
39 	{"symlink", 1, ERANGE},
40 	{(char *)-1, sizeof(VALUE), EFAULT}
41 };
42 
verify_lgetxattr(unsigned int n)43 static void verify_lgetxattr(unsigned int n)
44 {
45 	struct test_case *tc = tcase + n;
46 	char buf[tc->size];
47 
48 	TEST(lgetxattr(tc->path, SECURITY_KEY, buf, sizeof(buf)));
49 	if (TST_RET != -1) {
50 		tst_res(TFAIL, "lgetxattr() succeeded unexpectedly");
51 		return;
52 	}
53 
54 	if (TST_ERR != tc->exp_err) {
55 		tst_res(TFAIL | TTERRNO, "lgetxattr() failed unexpectedlly, "
56 			"expected %s", tst_strerrno(tc->exp_err));
57 	} else {
58 		tst_res(TPASS | TTERRNO, "lgetxattr() failed as expected");
59 	}
60 }
61 
setup(void)62 static void setup(void)
63 {
64 	int res;
65 
66 	SAFE_TOUCH("testfile", 0644, NULL);
67 	SAFE_SYMLINK("testfile", "symlink");
68 
69 	res = lsetxattr("symlink", SECURITY_KEY, VALUE, strlen(VALUE), XATTR_CREATE);
70 	if (res == -1) {
71 		if (errno == ENOTSUP) {
72 			tst_brk(TCONF, "no xattr support in fs or "
73 				"mounted without user_xattr option");
74 		} else {
75 			tst_brk(TBROK | TERRNO, "lsetxattr(%s) failed",
76 				SECURITY_KEY);
77 		}
78 	}
79 }
80 
81 static struct tst_test test = {
82 	.needs_tmpdir = 1,
83 	.needs_root = 1,
84 	.test = verify_lgetxattr,
85 	.tcnt = ARRAY_SIZE(tcase),
86 	.setup = setup
87 };
88 
89 #else /* HAVE_SYS_XATTR_H */
90 	TST_TEST_TCONF("<sys/xattr.h> does not exist.");
91 #endif
92