• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <sys/utsname.h>
23 #include <sys/vfs.h>
24 
25 #include <android-base/file.h>
26 #include <android-base/stringprintf.h>
27 
28 // Glibc v2.19 doesn't include these in fcntl.h so host builds will fail without.
29 #if !defined(FALLOC_FL_PUNCH_HOLE) || !defined(FALLOC_FL_KEEP_SIZE)
30 #include <linux/falloc.h>
31 #endif
32 #if !defined(EXT4_SUPER_MAGIC)
33 #include <linux/magic.h>
34 #endif
35 
TEST(fcntl,fcntl_smoke)36 TEST(fcntl, fcntl_smoke) {
37   int fd = open("/proc/version", O_RDONLY);
38   ASSERT_TRUE(fd != -1);
39 
40   int flags = fcntl(fd, F_GETFD);
41   ASSERT_TRUE(flags != -1);
42   ASSERT_EQ(0, flags & FD_CLOEXEC);
43 
44   int rc = fcntl(fd, F_SETFD, FD_CLOEXEC);
45   ASSERT_EQ(0, rc);
46 
47   flags = fcntl(fd, F_GETFD);
48   ASSERT_TRUE(flags != -1);
49   ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
50 
51   close(fd);
52 }
53 
TEST(fcntl,open_open64)54 TEST(fcntl, open_open64) {
55   int fd;
56 
57   fd = open("/proc/version", O_RDONLY);
58   ASSERT_TRUE(fd != -1);
59   close(fd);
60 
61   fd = open64("/proc/version", O_RDONLY);
62   ASSERT_TRUE(fd != -1);
63   close(fd);
64 }
65 
TEST(fcntl,openat_openat64)66 TEST(fcntl, openat_openat64) {
67   int fd;
68 
69   fd = openat(AT_FDCWD, "/proc/version", O_RDONLY);
70   ASSERT_TRUE(fd != -1);
71   close(fd);
72 
73   fd = openat64(AT_FDCWD, "/proc/version", O_RDONLY);
74   ASSERT_TRUE(fd != -1);
75   close(fd);
76 }
77 
TEST(fcntl,creat_creat64)78 TEST(fcntl, creat_creat64) {
79   ASSERT_EQ(-1, creat("", 0666));
80   ASSERT_EQ(ENOENT, errno);
81   ASSERT_EQ(-1, creat64("", 0666));
82   ASSERT_EQ(ENOENT, errno);
83 }
84 
TEST(fcntl,posix_fadvise)85 TEST(fcntl, posix_fadvise) {
86   TemporaryFile tf;
87   errno = 0;
88 
89   EXPECT_EQ(EBADF, posix_fadvise(-1, 0, 0, POSIX_FADV_NORMAL));
90   EXPECT_EQ(0, errno);
91 
92   EXPECT_EQ(EBADF, posix_fadvise64(-1, 0, 0, POSIX_FADV_NORMAL));
93   EXPECT_EQ(0, errno);
94 
95   EXPECT_EQ(EINVAL, posix_fadvise(tf.fd, 0, 0, -1));
96   EXPECT_EQ(0, errno);
97 
98   EXPECT_EQ(EINVAL, posix_fadvise64(tf.fd, 0, 0, -1));
99   EXPECT_EQ(0, errno);
100 
101   EXPECT_EQ(0, posix_fadvise(tf.fd, 0, 0, POSIX_FADV_NORMAL));
102   EXPECT_EQ(0, posix_fadvise64(tf.fd, 0, 0, POSIX_FADV_NORMAL));
103 }
104 
TEST(fcntl,fallocate_EINVAL)105 TEST(fcntl, fallocate_EINVAL) {
106   TemporaryFile tf;
107 
108   // fallocate/fallocate64 set errno.
109   // posix_fallocate/posix_fallocate64 return an errno value.
110 
111   errno = 0;
112   ASSERT_EQ(-1, fallocate(tf.fd, 0, 0, -1));
113   ASSERT_EQ(EINVAL, errno);
114 
115   errno = 0;
116   ASSERT_EQ(-1, fallocate64(tf.fd, 0, 0, -1));
117   ASSERT_EQ(EINVAL, errno);
118 
119   errno = 0;
120   ASSERT_EQ(EINVAL, posix_fallocate(tf.fd, 0, -1));
121   ASSERT_EQ(0, errno);
122 
123   errno = 0;
124   ASSERT_EQ(EINVAL, posix_fallocate64(tf.fd, 0, -1));
125   ASSERT_EQ(0, errno);
126 }
127 
TEST(fcntl,fallocate)128 TEST(fcntl, fallocate) {
129   TemporaryFile tf;
130   struct stat sb;
131   ASSERT_EQ(0, fstat(tf.fd, &sb));
132   ASSERT_EQ(0, sb.st_size);
133 
134 #if defined(__BIONIC__)
135   ASSERT_EQ(0, fallocate(tf.fd, 0, 0, 1));
136   ASSERT_EQ(0, fstat(tf.fd, &sb));
137   ASSERT_EQ(1, sb.st_size);
138 
139   ASSERT_EQ(0, fallocate64(tf.fd, 0, 0, 2));
140   ASSERT_EQ(0, fstat(tf.fd, &sb));
141   ASSERT_EQ(2, sb.st_size);
142 #endif
143 
144   ASSERT_EQ(0, posix_fallocate(tf.fd, 0, 3));
145   ASSERT_EQ(0, fstat(tf.fd, &sb));
146   ASSERT_EQ(3, sb.st_size);
147 
148   ASSERT_EQ(0, posix_fallocate64(tf.fd, 0, 4));
149   ASSERT_EQ(0, fstat(tf.fd, &sb));
150   ASSERT_EQ(4, sb.st_size);
151 }
152 
TEST(fcntl,f_getlk)153 TEST(fcntl, f_getlk) {
154   int fd = open("/proc/version", O_RDONLY);
155   ASSERT_TRUE(fd != -1);
156 
157   struct flock check_lock;
158   check_lock.l_type = F_WRLCK;
159   check_lock.l_start = 0;
160   check_lock.l_whence = SEEK_SET;
161   check_lock.l_len = 0;
162 
163   ASSERT_EQ(0, fcntl(fd, F_GETLK, &check_lock));
164   close(fd);
165 }
166 
TEST(fcntl,f_getlk64)167 TEST(fcntl, f_getlk64) {
168   int fd = open64("/proc/version", O_RDONLY);
169   ASSERT_TRUE(fd != -1);
170 
171   struct flock64 check_lock;
172   check_lock.l_type = F_WRLCK;
173   check_lock.l_start = 0;
174   check_lock.l_whence = SEEK_SET;
175   check_lock.l_len = 0;
176 
177   ASSERT_EQ(0, fcntl(fd, F_GETLK64, &check_lock));
178   close(fd);
179 }
180 
TEST(fcntl,splice)181 TEST(fcntl, splice) {
182   int pipe_fds[2];
183   ASSERT_EQ(0, pipe(pipe_fds));
184 
185   int in = open("/proc/cpuinfo", O_RDONLY);
186   ASSERT_NE(in, -1);
187 
188   TemporaryFile tf;
189 
190   ssize_t bytes_read = splice(in, nullptr, pipe_fds[1], nullptr, 8*1024, SPLICE_F_MORE | SPLICE_F_MOVE);
191   ASSERT_NE(bytes_read, -1);
192 
193   ssize_t bytes_written = splice(pipe_fds[0], nullptr, tf.fd, nullptr, bytes_read, SPLICE_F_MORE | SPLICE_F_MOVE);
194   ASSERT_EQ(bytes_read, bytes_written);
195 
196   close(pipe_fds[0]);
197   close(pipe_fds[1]);
198   close(in);
199 }
200 
TEST(fcntl,vmsplice)201 TEST(fcntl, vmsplice) {
202   int pipe_fds[2];
203   ASSERT_EQ(0, pipe(pipe_fds));
204 
205   iovec v[2];
206   v[0].iov_base = const_cast<char*>("hello ");
207   v[0].iov_len = 6;
208   v[1].iov_base = const_cast<char*>("world\n");
209   v[1].iov_len = 6;
210   ssize_t bytes_written = vmsplice(pipe_fds[1], v, sizeof(v)/sizeof(iovec), 0);
211   ASSERT_EQ(v[0].iov_len + v[1].iov_len, static_cast<size_t>(bytes_written));
212   close(pipe_fds[1]);
213 
214   char buf[BUFSIZ];
215   FILE* fp = fdopen(pipe_fds[0], "r");
216   ASSERT_TRUE(fp != nullptr);
217   ASSERT_TRUE(fgets(buf, sizeof(buf), fp) != nullptr);
218   fclose(fp);
219   ASSERT_STREQ("hello world\n", buf);
220 }
221 
TEST(fcntl,tee)222 TEST(fcntl, tee) {
223   char expected[BUFSIZ];
224   FILE* expected_fp = fopen("/proc/version", "r");
225   ASSERT_TRUE(expected_fp != nullptr);
226   ASSERT_TRUE(fgets(expected, sizeof(expected), expected_fp) != nullptr);
227   fclose(expected_fp);
228 
229   int pipe1[2];
230   ASSERT_EQ(0, pipe(pipe1));
231 
232   int pipe2[2];
233   ASSERT_EQ(0, pipe(pipe2));
234 
235   int in = open("/proc/version", O_RDONLY);
236   ASSERT_NE(in, -1);
237 
238   // Write /proc/version into pipe1.
239   ssize_t bytes_read = splice(in, nullptr, pipe1[1], nullptr, 8*1024, SPLICE_F_MORE | SPLICE_F_MOVE);
240   ASSERT_NE(bytes_read, -1);
241   close(pipe1[1]);
242 
243   // Tee /proc/version from pipe1 into pipe2.
244   ssize_t bytes_teed = tee(pipe1[0], pipe2[1], SIZE_MAX, 0);
245   ASSERT_EQ(bytes_read, bytes_teed);
246   close(pipe2[1]);
247 
248   // The out fds of both pipe1 and pipe2 should now contain /proc/version.
249   char buf1[BUFSIZ];
250   FILE* fp1 = fdopen(pipe1[0], "r");
251   ASSERT_TRUE(fp1 != nullptr);
252   ASSERT_TRUE(fgets(buf1, sizeof(buf1), fp1) != nullptr);
253   fclose(fp1);
254 
255   char buf2[BUFSIZ];
256   FILE* fp2 = fdopen(pipe2[0], "r");
257   ASSERT_TRUE(fp2 != nullptr);
258   ASSERT_TRUE(fgets(buf2, sizeof(buf2), fp2) != nullptr);
259   fclose(fp2);
260 
261   ASSERT_STREQ(expected, buf1);
262   ASSERT_STREQ(expected, buf2);
263 }
264 
TEST(fcntl,readahead)265 TEST(fcntl, readahead) {
266   // Just check that the function is available.
267   errno = 0;
268   ASSERT_EQ(-1, readahead(-1, 0, 123));
269   ASSERT_EQ(EBADF, errno);
270 }
271 
TEST(fcntl,sync_file_range)272 TEST(fcntl, sync_file_range) {
273   // Just check that the function is available.
274   errno = 0;
275   ASSERT_EQ(-1, sync_file_range(-1, 0, 0, 0));
276   ASSERT_EQ(EBADF, errno);
277 
278   TemporaryFile tf;
279   ASSERT_EQ(0, sync_file_range(tf.fd, 0, 0, 0));
280 
281   // The arguments to the underlying system call are in a different order on 32-bit ARM.
282   // Check that the `flags` argument gets passed to the kernel correctly.
283   errno = 0;
284   ASSERT_EQ(-1, sync_file_range(tf.fd, 0, 0, ~0));
285   ASSERT_EQ(EINVAL, errno);
286 }
287 
parse_kernel_release(long * const major,long * const minor)288 static bool parse_kernel_release(long* const major, long* const minor) {
289   struct utsname buf;
290   if (uname(&buf) == -1) {
291     return false;
292   }
293   return sscanf(buf.release, "%ld.%ld", major, minor) == 2;
294 }
295 
296 /*
297  * b/28760453:
298  * Kernels older than 4.1 should have ext4 FALLOC_FL_PUNCH_HOLE disabled due to CVE-2015-8839.
299  * Devices that fail this test should cherry-pick the following commit:
300  * https://android.googlesource.com/kernel/msm/+/bdba352e898cbf57c8620ad68c8abf749c784d1f
301  */
TEST(fcntl,falloc_punch)302 TEST(fcntl, falloc_punch) {
303   long major = 0, minor = 0;
304   ASSERT_TRUE(parse_kernel_release(&major, &minor));
305 
306   if (major < 4 || (major == 4 && minor < 1)) {
307     TemporaryFile tf;
308     struct statfs sfs;
309     ASSERT_EQ(0, fstatfs(tf.fd, &sfs));
310     if (sfs.f_type == EXT4_SUPER_MAGIC) {
311       ASSERT_EQ(-1, fallocate(tf.fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1));
312       ASSERT_EQ(errno, EOPNOTSUPP);
313     }
314   }
315 }
316 
TEST(fcntl,open_O_TMPFILE_mode)317 TEST(fcntl, open_O_TMPFILE_mode) {
318 #if __BIONIC__ // Our glibc is too old for O_TMPFILE.
319   TemporaryDir dir;
320   // Without O_EXCL, we're allowed to give this a name later.
321   // (This is unrelated to the O_CREAT interaction with O_EXCL.)
322   const mode_t perms = S_IRUSR | S_IWUSR;
323   int fd = open(dir.path, O_TMPFILE | O_RDWR, perms);
324 
325   // Ignore kernels without O_TMPFILE support (< 3.11).
326   if (fd == -1 && (errno == EISDIR || errno == EINVAL || errno == EOPNOTSUPP)) return;
327 
328   ASSERT_TRUE(fd != -1) << strerror(errno);
329 
330   // Does the fd claim to have the mode we set?
331   struct stat sb = {};
332   ASSERT_EQ(0, fstat(fd, &sb));
333   ASSERT_EQ(perms, (sb.st_mode & ~S_IFMT));
334 
335   // On Android if we're not root, we won't be able to create links anyway...
336   if (getuid() != 0) return;
337 
338   std::string final_path = android::base::StringPrintf("%s/named_now", dir.path);
339   ASSERT_EQ(0, linkat(AT_FDCWD, android::base::StringPrintf("/proc/self/fd/%d", fd).c_str(),
340                       AT_FDCWD, final_path.c_str(),
341                       AT_SYMLINK_FOLLOW));
342   ASSERT_EQ(0, close(fd));
343 
344   // Does the resulting file claim to have the mode we set?
345   ASSERT_EQ(0, stat(final_path.c_str(), &sb));
346   ASSERT_EQ(perms, (sb.st_mode & ~S_IFMT));
347 
348   // With O_EXCL, you're not allowed to add a name later.
349   fd = open(dir.path, O_TMPFILE | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
350   ASSERT_TRUE(fd != -1) << strerror(errno);
351   errno = 0;
352   ASSERT_EQ(-1, linkat(AT_FDCWD, android::base::StringPrintf("/proc/self/fd/%d", fd).c_str(),
353                        AT_FDCWD, android::base::StringPrintf("%s/no_chance", dir.path).c_str(),
354                        AT_SYMLINK_FOLLOW));
355   ASSERT_EQ(ENOENT, errno);
356   ASSERT_EQ(0, close(fd));
357 #endif
358 }
359