1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 Fujitsu Ltd.
4 * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5 */
6
7 /*\
8 * [Description]
9 *
10 * Verify llistxattr(2) returns -1 and set proper errno:
11 *
12 * - ERANGE if the size of the list buffer is too small to hold the result
13 * - ENOENT if path is an empty string
14 * - EFAULT when attempted to read from a invalid address
15 * - ENAMETOOLONG if path is longer than allowed
16 */
17
18 #include "config.h"
19 #include <errno.h>
20 #include <sys/types.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 "test"
32 #define VALUE_SIZE (sizeof(VALUE) - 1)
33 #define TESTFILE "testfile"
34 #define SYMLINK "symlink"
35
36 static char longpathname[PATH_MAX + 2];
37
38 static struct test_case {
39 const char *path;
40 size_t size;
41 int exp_err;
42 } tc[] = {
43 {SYMLINK, 1, ERANGE},
44 {"", 20, ENOENT},
45 {(char *)-1, 20, EFAULT},
46 {longpathname, 20, ENAMETOOLONG}
47 };
48
verify_llistxattr(unsigned int n)49 static void verify_llistxattr(unsigned int n)
50 {
51 struct test_case *t = tc + n;
52 char buf[t->size];
53
54 TEST(llistxattr(t->path, buf, sizeof(buf)));
55 if (TST_RET != -1) {
56 tst_res(TFAIL,
57 "llistxattr() succeeded unexpectedly (returned %ld)",
58 TST_RET);
59 return;
60 }
61
62 if (TST_ERR != t->exp_err) {
63 tst_res(TFAIL | TTERRNO, "llistxattr() failed "
64 "unexpectedlly, expected %s",
65 tst_strerrno(t->exp_err));
66 } else {
67 tst_res(TPASS | TTERRNO,
68 "llistxattr() failed as expected");
69 }
70 }
71
setup(void)72 static void setup(void)
73 {
74 SAFE_TOUCH(TESTFILE, 0644, NULL);
75
76 SAFE_SYMLINK(TESTFILE, SYMLINK);
77
78 SAFE_LSETXATTR(SYMLINK, SECURITY_KEY, VALUE, VALUE_SIZE, XATTR_CREATE);
79
80 memset(longpathname, 'a', sizeof(longpathname) - 1);
81 }
82
83 static struct tst_test test = {
84 .needs_tmpdir = 1,
85 .needs_root = 1,
86 .test = verify_llistxattr,
87 .tcnt = ARRAY_SIZE(tc),
88 .setup = setup,
89 };
90
91 #else /* HAVE_SYS_XATTR_H */
92 TST_TEST_TCONF("<sys/xattr.h> does not exist.");
93 #endif
94