1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * 07/2001 John George
5 */
6
7 /*\
8 * [Description]
9 *
10 * Verify that:
11 *
12 * - truncate(2) returns -1 and sets errno to EACCES if search/write
13 * permission denied for the process on the component of the path prefix
14 * or named file.
15 * - truncate(2) returns -1 and sets errno to ENOTDIR if the component of
16 * the path prefix is not a directory.
17 * - truncate(2) returns -1 and sets errno to EFAULT if pathname points
18 * outside user's accessible address space.
19 * - truncate(2) returns -1 and sets errno to ENAMETOOLONG if the component
20 * of a pathname exceeded 255 characters or entire pathname exceeds 1023
21 * characters.
22 * - truncate(2) returns -1 and sets errno to ENOENT if the named file
23 * does not exist.
24 * - truncate(2) returns -1 and sets errno to EISDIR if the named file
25 * is a directory.
26 * - truncate(2) returns -1 and sets errno to EFBIG if the argument length
27 * is larger than the maximum file size.
28 * - truncate(2) returns -1 and sets errno to ELOOP if too many symbolic
29 * links were encountered in translating the pathname.
30 */
31
32 #define _GNU_SOURCE
33
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <sys/mman.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <signal.h>
42 #include <pwd.h>
43 #include <sys/resource.h>
44
45 #include "tst_test.h"
46
47 #define TEST_FILE1 "testfile"
48 #define TEST_FILE2 "t_file/testfile"
49 #define TEST_FILE3 "testfile3"
50 #define TEST_SYM1 "testsymlink1"
51 #define TEST_SYM2 "testsymlink2"
52 #define TEST_DIR1 "testdir"
53 #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
54 #define NEW_MODE S_IRUSR | S_IRGRP | S_IROTH
55 #define DIR_MODE S_IRWXU
56 #define TRUNC_LEN 256
57 #define MAX_FSIZE (16*1024*1024)
58
59 static char long_pathname[PATH_MAX + 2];
60
61 static struct test_case_t {
62 char *pathname;
63 off_t length;
64 int exp_errno;
65 } test_cases[] = {
66 { TEST_FILE1, TRUNC_LEN, EACCES },
67 { TEST_FILE2, TRUNC_LEN, ENOTDIR },
68 { NULL, TRUNC_LEN, EFAULT },
69 { long_pathname, TRUNC_LEN, ENAMETOOLONG },
70 { "", TRUNC_LEN, ENOENT },
71 { TEST_DIR1, TRUNC_LEN, EISDIR },
72 { TEST_FILE3, MAX_FSIZE*2, EFBIG },
73 { TEST_SYM1, TRUNC_LEN, ELOOP }
74 };
75
setup(void)76 static void setup(void)
77 {
78 struct passwd *ltpuser;
79 struct rlimit rlim = {
80 .rlim_cur = MAX_FSIZE,
81 .rlim_max = MAX_FSIZE,
82 };
83 sigset_t signalset;
84 unsigned int n;
85
86 ltpuser = SAFE_GETPWNAM("nobody");
87 SAFE_SETEUID(ltpuser->pw_uid);
88
89 SAFE_TOUCH(TEST_FILE1, NEW_MODE, NULL);
90
91 SAFE_TOUCH("t_file", FILE_MODE, NULL);
92
93 memset(long_pathname, 'a', PATH_MAX + 1);
94
95 SAFE_MKDIR(TEST_DIR1, DIR_MODE);
96
97 SAFE_TOUCH(TEST_FILE3, FILE_MODE, NULL);
98
99 SAFE_SYMLINK(TEST_SYM1, TEST_SYM2);
100 SAFE_SYMLINK(TEST_SYM2, TEST_SYM1);
101
102 SAFE_SETRLIMIT(RLIMIT_FSIZE, &rlim);
103
104 SAFE_SIGEMPTYSET(&signalset);
105 SAFE_SIGADDSET(&signalset, SIGXFSZ);
106 SAFE_SIGPROCMASK(SIG_BLOCK, &signalset, NULL);
107
108 for (n = 0; n < ARRAY_SIZE(test_cases); n++) {
109 if (!test_cases[n].pathname)
110 test_cases[n].pathname = tst_get_bad_addr(NULL);
111 }
112
113 }
114
verify_truncate(unsigned int n)115 static void verify_truncate(unsigned int n)
116 {
117 struct test_case_t *tc = &test_cases[n];
118
119 TEST(truncate(tc->pathname, tc->length));
120 if (TST_RET == 0) {
121 tst_res(TFAIL, "truncate() succeeded when failure expected");
122 return;
123 }
124
125 if (TST_RET != -1) {
126 tst_res(TFAIL, "truncate() returned invalid value %ld",
127 TST_RET);
128 return;
129 }
130
131 if (TST_ERR == tc->exp_errno) {
132 tst_res(TPASS | TTERRNO, "truncate() failed as expected");
133 } else {
134 tst_res(TFAIL | TTERRNO,
135 "truncate() failed unexpectedly; expected: %d - %s",
136 tc->exp_errno, strerror(tc->exp_errno));
137 }
138 }
139
140 static struct tst_test test = {
141 .needs_root = 1,
142 .needs_tmpdir = 1,
143 .setup = setup,
144 .tcnt = ARRAY_SIZE(test_cases),
145 .test = verify_truncate,
146 };
147