1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Ported to LTP: Wayne Boyer
5 */
6
7 /*\
8 * [Description]
9 *
10 * Verify that, readlink(2) returns -1 and sets errno to
11 *
12 * 1. EACCES if search/write permission is denied in the directory where the
13 * symbolic link esides.
14 * 2. EINVAL if the buffer size is not positive.
15 * 3. EINVAL if the specified file is not a symbolic link file.
16 * 4. ENAMETOOLONG if the pathname component of symbolic link is too long
17 * (ie, > PATH_MAX).
18 * 5. ENOENT if the component of symbolic link points to an empty string.
19 * 6. ENOTDIR if a component of the path prefix is not a directory.
20 * 7. ELOOP if too many symbolic links were encountered in translating the
21 * pathname.
22 * 8. EFAULT if buf outside the process allocated address space.
23 */
24
25 #include <pwd.h>
26 #include <errno.h>
27 #include <string.h>
28
29 #include "tst_test.h"
30
31 #define DIR_TEMP "test_dir_1"
32 #define TEST_FILE1 "test_dir_1/test_file_1"
33 #define SYM_FILE1 "test_dir_1/slink_file_1"
34 #define TEST_FILE2 "test_file_2"
35 #define SYM_FILE2 "slink_file_2"
36 #define TEST_FILE3 "test_file_3"
37 #define SYM_FILE3 "test_file_3/slink_file_3"
38 #define ELOOPFILE "/test_eloop"
39 #define TESTFILE "test_file"
40 #define SYMFILE "slink_file"
41
42 static char longpathname[PATH_MAX + 2];
43 static char elooppathname[sizeof(ELOOPFILE) * 43] = ".";
44 static char buffer[256];
45
46 static struct tcase {
47 char *link;
48 char *buf;
49 size_t buf_size;
50 int exp_errno;
51 } tcases[] = {
52 {SYM_FILE1, buffer, sizeof(buffer), EACCES},
53 {SYM_FILE2, buffer, 0, EINVAL},
54 {TEST_FILE2, buffer, sizeof(buffer), EINVAL},
55 {longpathname, buffer, sizeof(buffer), ENAMETOOLONG},
56 {"", buffer, sizeof(buffer), ENOENT},
57 {SYM_FILE3, buffer, sizeof(buffer), ENOTDIR},
58 {elooppathname, buffer, sizeof(buffer), ELOOP},
59 {SYMFILE, (char *)-1, sizeof(buffer), EFAULT},
60 };
61
verify_readlink(unsigned int n)62 static void verify_readlink(unsigned int n)
63 {
64 struct tcase *tc = &tcases[n];
65
66 TEST(readlink(tc->link, tc->buf, tc->buf_size));
67 if (TST_RET != -1) {
68 tst_res(TFAIL, "readlink() sueeeeded unexpectedly");
69 return;
70 }
71
72 if (TST_ERR != tc->exp_errno) {
73 tst_res(TFAIL | TTERRNO,
74 "readlink() failed unexpectedly; expected: %d - %s, got",
75 tc->exp_errno, tst_strerrno(tc->exp_errno));
76
77 if (tc->exp_errno == ENOENT && TST_ERR == EINVAL) {
78 tst_res(TWARN | TTERRNO,
79 "It may be a Kernel Bug, see the patch:"
80 "http://git.kernel.org/linus/1fa1e7f6");
81 }
82 } else {
83 tst_res(TPASS | TTERRNO, "readlink() failed as expected");
84 }
85 }
86
setup(void)87 static void setup(void)
88 {
89 int i;
90 struct passwd *pwent;
91
92 pwent = SAFE_GETPWNAM("nobody");
93 SAFE_SETEUID(pwent->pw_uid);
94
95 SAFE_MKDIR(DIR_TEMP, 0777);
96 SAFE_TOUCH(TEST_FILE1, 0666, NULL);
97 SAFE_SYMLINK(TEST_FILE1, SYM_FILE1);
98 SAFE_CHMOD(DIR_TEMP, 0444);
99
100 SAFE_TOUCH(TEST_FILE2, 0666, NULL);
101 SAFE_SYMLINK(TEST_FILE2, SYM_FILE2);
102
103 memset(longpathname, 'a', PATH_MAX + 1);
104
105 SAFE_TOUCH(TEST_FILE3, 0666, NULL);
106
107 SAFE_MKDIR("test_eloop", 0777);
108 SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");
109
110 for (i = 0; i < 43; i++)
111 strcat(elooppathname, ELOOPFILE);
112
113 SAFE_TOUCH(TESTFILE, 0666, NULL);
114 SAFE_SYMLINK(TESTFILE, SYMFILE);
115 }
116
117 static struct tst_test test = {
118 .tcnt = ARRAY_SIZE(tcases),
119 .test = verify_readlink,
120 .setup = setup,
121 .needs_tmpdir = 1,
122 .needs_root = 1,
123 };
124