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