]> sourceware.org Git - glibc.git/blame - io/test-lfs.c
syslog: Fix integer overflow in __vsyslog_internal (CVE-2023-6780)
[glibc.git] / io / test-lfs.c
CommitLineData
4c524b81 1/* Some basic tests for LFS.
dff8da6b 2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
41bdb6e2 3 This file is part of the GNU C Library.
4c524b81
AJ
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
4c524b81
AJ
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
4c524b81 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
4c524b81
AJ
18
19#include <unistd.h>
20#include <stdlib.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <error.h>
25#include <errno.h>
feb27893 26#include <sys/resource.h>
aa42b3db 27#include <support/check.h>
4c524b81
AJ
28
29/* Prototype for our test function. */
30extern void do_prepare (int argc, char *argv[]);
31extern int do_test (int argc, char *argv[]);
32
33/* We have a preparation function. */
34#define PREPARE do_prepare
35
4c524b81
AJ
36/* This defines the `main' function and some more. */
37#include <test-skeleton.c>
38
39/* These are for the temporary file we generate. */
40char *name;
41int fd;
42
43/* 2^31 = 2GB. */
44#define TWO_GB 2147483648LL
45
46void
47do_prepare (int argc, char *argv[])
48{
30aa5785 49 size_t name_len;
85471284 50 struct rlimit64 rlim;
4c524b81
AJ
51
52 name_len = strlen (test_dir);
850c6760 53 name = xmalloc (name_len + sizeof ("/lfsXXXXXX"));
4c524b81
AJ
54 mempcpy (mempcpy (name, test_dir, name_len),
55 "/lfsXXXXXX", sizeof ("/lfsXXXXXX"));
4c524b81
AJ
56
57 /* Open our test file. */
85471284
UD
58 fd = mkstemp64 (name);
59 if (fd == -1)
4c524b81 60 {
85471284
UD
61 if (errno == ENOSYS)
62 {
63 /* Fail silently. */
207390f7 64 error (0, 0, "open64 is not supported");
85471284
UD
65 exit (EXIT_SUCCESS);
66 }
67 else
68 error (EXIT_FAILURE, errno, "cannot create temporary file");
4c524b81 69 }
aa42b3db
FW
70 if (!support_descriptor_supports_holes (fd))
71 FAIL_UNSUPPORTED ("File %s does not support holes", name);
cf145565 72 add_temp_file (name);
4c524b81 73
85471284
UD
74 if (getrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
75 {
76 error (0, errno, "cannot get resource limit");
77 exit (0);
78 }
79 if (rlim.rlim_cur < TWO_GB + 200)
80 {
81 rlim.rlim_cur = TWO_GB + 200;
82 if (setrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
83 {
84 error (0, errno, "cannot reset file size limits");
85 exit (0);
86 }
87 }
4c524b81
AJ
88}
89
725c76a6
AJ
90static void
91test_ftello (void)
92{
93 FILE *f;
94 int ret;
95 off64_t pos;
96
97 f = fopen64 (name, "w");
98
99 ret = fseeko64 (f, TWO_GB+100, SEEK_SET);
100 if (ret == -1 && errno == ENOSYS)
101 {
207390f7 102 error (0, 0, "fseeko64 is not supported.");
725c76a6
AJ
103 exit (EXIT_SUCCESS);
104 }
105 if (ret == -1 && errno == EINVAL)
106 {
207390f7 107 error (0, 0, "LFS seems not to be supported");
725c76a6
AJ
108 exit (EXIT_SUCCESS);
109 }
110 if (ret == -1)
111 {
207390f7 112 error (0, errno, "fseeko64 failed with error");
725c76a6
AJ
113 exit (EXIT_FAILURE);
114 }
115
116 ret = fwrite ("Hello", 1, 5, f);
207390f7
AJ
117 if (ret == -1 && errno == EFBIG)
118 {
119 error (0, errno, "LFS seems not to be supported");
120 exit (EXIT_SUCCESS);
121 }
122
123 if (ret == -1 && errno == ENOSPC)
725c76a6 124 {
207390f7 125 error (0, 0, "Not enough space to write file.");
725c76a6
AJ
126 exit (EXIT_SUCCESS);
127 }
128
129 if (ret != 5)
207390f7 130 error (EXIT_FAILURE, errno, "Cannot write test string to large file");
725c76a6
AJ
131
132 pos = ftello64 (f);
133
134 if (pos != TWO_GB+105)
135 {
136 error (0, 0, "ftello64 gives wrong result.");
137 exit (EXIT_FAILURE);
138 }
139
140 fclose (f);
141}
142
4c524b81
AJ
143int
144do_test (int argc, char *argv[])
145{
eb32b0d4 146 int ret, fd2;
4c524b81
AJ
147 struct stat64 statbuf;
148
149 ret = lseek64 (fd, TWO_GB+100, SEEK_SET);
150 if (ret == -1 && errno == ENOSYS)
151 {
207390f7 152 error (0, 0, "lseek64 is not supported.");
4c524b81
AJ
153 exit (EXIT_SUCCESS);
154 }
0b795736
UD
155 if (ret == -1 && errno == EINVAL)
156 {
207390f7 157 error (0, 0, "LFS seems not to be supported.");
0b795736
UD
158 exit (EXIT_SUCCESS);
159 }
725c76a6
AJ
160 if (ret == -1)
161 {
207390f7 162 error (0, errno, "lseek64 failed with error");
725c76a6
AJ
163 exit (EXIT_FAILURE);
164 }
df19fdcf
JM
165 off64_t offset64 = lseek64 (fd, 0, SEEK_CUR);
166 if (offset64 != TWO_GB + 100)
167 {
168 error (0, 0, "lseek64 did not return expected offset");
169 exit (EXIT_FAILURE);
170 }
171 off_t offset = lseek (fd, 0, SEEK_CUR);
172 if (sizeof (off_t) < sizeof (off64_t))
173 {
174 if (offset != -1 || errno != EOVERFLOW)
175 {
176 error (0, 0, "lseek did not fail with EOVERFLOW");
177 exit (EXIT_FAILURE);
178 }
179 }
180 else
181 if (offset != TWO_GB + 100)
182 {
183 error (0, 0, "lseek did not return expected offset");
184 exit (EXIT_FAILURE);
185 }
4c524b81
AJ
186
187 ret = write (fd, "Hello", 5);
207390f7
AJ
188 if (ret == -1 && errno == EFBIG)
189 {
190 error (0, 0, "LFS seems not to be supported.");
191 exit (EXIT_SUCCESS);
192 }
193
194 if (ret == -1 && errno == ENOSPC)
4c524b81 195 {
207390f7 196 error (0, 0, "Not enough space to write file.");
4c524b81
AJ
197 exit (EXIT_SUCCESS);
198 }
56b2223e 199
4c524b81
AJ
200 if (ret != 5)
201 error (EXIT_FAILURE, errno, "cannot write test string to large file");
202
203 ret = close (fd);
204
205 if (ret == -1)
206 error (EXIT_FAILURE, errno, "error closing file");
207
208 ret = stat64 (name, &statbuf);
209
56b2223e 210 if (ret == -1 && (errno == ENOSYS || errno == EOVERFLOW))
207390f7 211 error (0, 0, "stat64 is not supported.");
4c524b81
AJ
212 else if (ret == -1)
213 error (EXIT_FAILURE, errno, "cannot stat file `%s'", name);
59553897 214 else if (statbuf.st_size != (TWO_GB + 100 + 5))
4c524b81 215 error (EXIT_FAILURE, 0, "stat reported size %lld instead of %lld.",
59553897 216 (long long int) statbuf.st_size, (TWO_GB + 100 + 5));
4c524b81 217
eb32b0d4
AS
218 fd2 = openat64 (AT_FDCWD, name, O_RDWR);
219 if (fd2 == -1)
220 {
221 if (errno == ENOSYS)
222 {
223 /* Silently ignore this test. */
224 error (0, 0, "openat64 is not supported");
225 }
226 else
227 error (EXIT_FAILURE, errno, "openat64 failed to open big file");
228 }
229 else
230 {
231 ret = close (fd2);
232
233 if (ret == -1)
234 error (EXIT_FAILURE, errno, "error closing file");
235 }
236
725c76a6
AJ
237 test_ftello ();
238
4c524b81
AJ
239 return 0;
240}
This page took 0.520946 seconds and 5 git commands to generate.