| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/stat.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "copy.h" |
| 7 |
#include "data-fd-util.h" |
| 8 |
#include "fd-util.h" |
| 9 |
#include "fs-util.h" |
| 10 |
#include "memfd-util.h" |
| 11 |
#include "tmpfile-util.h" |
| 12 |
|
| 13 |
/* When the data is smaller or equal to 64K, try to place the copy in a memfd */ |
| 14 |
#define DATA_FD_MEMORY_LIMIT (64U * U64_KB) |
| 15 |
|
| 16 |
/* If memfd didn't work out, then let's use a file in /tmp up to a size of 1M. If it's large than that use /var/tmp/ instead. */ |
| 17 |
#define DATA_FD_TMP_LIMIT (1U * U64_MB) |
| 18 |
|
| 19 |
int copy_data_fd(int fd) { |
| 20 |
_cleanup_close_ int copy_fd = -EBADF, tmp_fd = -EBADF; |
| 21 |
const char *td; |
| 22 |
struct stat st; |
| 23 |
int r; |
| 24 |
|
| 25 |
/* Creates a 'data' fd from the specified source fd, containing all the same data in a read-only |
| 26 |
* fashion, but independent of it (i.e. the source fd can be closed and unmounted after this call |
| 27 |
* succeeded). Tries to be somewhat smart about where to place the data. In the best case uses a |
| 28 |
* memfd(). For larger data will use an unlinked file in /tmp/, and for even larger data one in |
| 29 |
* /var/tmp/. */ |
| 30 |
|
| 31 |
if (fstat(fd, &st) < 0) |
| 32 |
return -errno; |
| 33 |
|
| 34 |
/* For now, let's only accept regular files, sockets, pipes and char devices */ |
| 35 |
if (S_ISDIR(st.st_mode)) |
| 36 |
return -EISDIR; |
| 37 |
if (S_ISLNK(st.st_mode)) |
| 38 |
return -ELOOP; |
| 39 |
if (!S_ISREG(st.st_mode) && !S_ISSOCK(st.st_mode) && !S_ISFIFO(st.st_mode) && !S_ISCHR(st.st_mode)) |
| 40 |
return -EBADFD; |
| 41 |
|
| 42 |
/* If we have reason to believe the data is bounded in size, then let's use memfds as backing |
| 43 |
* fd. Note that we use the reported regular file size only as a hint, given that there are plenty |
| 44 |
* special files in /proc/ and /sys/ which report a zero file size but can be read from. */ |
| 45 |
|
| 46 |
if (!S_ISREG(st.st_mode) || (uint64_t) st.st_size < DATA_FD_MEMORY_LIMIT) { |
| 47 |
|
| 48 |
/* Try a memfd first */ |
| 49 |
copy_fd = memfd_new_full("data-fd", MFD_ALLOW_SEALING); |
| 50 |
if (copy_fd < 0) |
| 51 |
return copy_fd; |
| 52 |
|
| 53 |
r = copy_bytes(fd, copy_fd, DATA_FD_MEMORY_LIMIT, COPY_REFLINK); |
| 54 |
if (r < 0) |
| 55 |
return r; |
| 56 |
|
| 57 |
off_t f = lseek(copy_fd, 0, SEEK_SET); |
| 58 |
if (f < 0) |
| 59 |
return -errno; |
| 60 |
if (f != 0) |
| 61 |
return -EIO; |
| 62 |
|
| 63 |
if (r == 0) { |
| 64 |
/* Did it fit into the limit? If so, we are done. */ |
| 65 |
r = memfd_set_sealed(copy_fd); |
| 66 |
if (r < 0) |
| 67 |
return r; |
| 68 |
|
| 69 |
return TAKE_FD(copy_fd); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/* If we have reason to believe this will fit fine in /tmp, then use that as first fallback. */ |
| 74 |
if ((!S_ISREG(st.st_mode) || (uint64_t) st.st_size < DATA_FD_TMP_LIMIT)) { |
| 75 |
tmp_fd = open_tmpfile_unlinkable(NULL /* NULL as directory means /tmp */, O_RDWR|O_CLOEXEC); |
| 76 |
if (tmp_fd < 0) |
| 77 |
return tmp_fd; |
| 78 |
|
| 79 |
if (copy_fd >= 0) { |
| 80 |
/* If we tried a memfd first and it ended up being too large, then copy this into the |
| 81 |
* temporary file first. */ |
| 82 |
|
| 83 |
r = copy_bytes(copy_fd, tmp_fd, UINT64_MAX, COPY_REFLINK); |
| 84 |
if (r < 0) |
| 85 |
return r; |
| 86 |
|
| 87 |
assert(r == 0); |
| 88 |
} |
| 89 |
|
| 90 |
r = copy_bytes(fd, tmp_fd, DATA_FD_TMP_LIMIT - DATA_FD_MEMORY_LIMIT, COPY_REFLINK); |
| 91 |
if (r < 0) |
| 92 |
return r; |
| 93 |
if (r == 0) |
| 94 |
goto finish; /* Yay, it fit in */ |
| 95 |
|
| 96 |
/* It didn't fit in. Let's not forget to use what we already used */ |
| 97 |
off_t f = lseek(tmp_fd, 0, SEEK_SET); |
| 98 |
if (f < 0) |
| 99 |
return -errno; |
| 100 |
if (f != 0) |
| 101 |
return -EIO; |
| 102 |
|
| 103 |
close_and_replace(copy_fd, tmp_fd); |
| 104 |
} |
| 105 |
|
| 106 |
/* As last fallback use /var/tmp/ */ |
| 107 |
r = var_tmp_dir(&td); |
| 108 |
if (r < 0) |
| 109 |
return r; |
| 110 |
|
| 111 |
tmp_fd = open_tmpfile_unlinkable(td, O_RDWR|O_CLOEXEC); |
| 112 |
if (tmp_fd < 0) |
| 113 |
return tmp_fd; |
| 114 |
|
| 115 |
if (copy_fd >= 0) { |
| 116 |
/* If we tried a memfd first, or a file in /tmp/, and it ended up being too large, than copy this |
| 117 |
* into the temporary file first. */ |
| 118 |
r = copy_bytes(copy_fd, tmp_fd, UINT64_MAX, COPY_REFLINK); |
| 119 |
if (r < 0) |
| 120 |
return r; |
| 121 |
|
| 122 |
assert(r == 0); |
| 123 |
} |
| 124 |
|
| 125 |
/* Copy in the rest */ |
| 126 |
r = copy_bytes(fd, tmp_fd, UINT64_MAX, COPY_REFLINK); |
| 127 |
if (r < 0) |
| 128 |
return r; |
| 129 |
|
| 130 |
assert(r == 0); |
| 131 |
|
| 132 |
finish: |
| 133 |
/* Now convert the O_RDWR file descriptor into an O_RDONLY one (and as side effect seek to the beginning of the |
| 134 |
* file again */ |
| 135 |
|
| 136 |
return fd_reopen(tmp_fd, O_RDONLY|O_CLOEXEC); |
| 137 |
} |
| 138 |
|
| 139 |
int memfd_clone_fd(int fd, const char *name, int mode) { |
| 140 |
_cleanup_close_ int mfd = -EBADF; |
| 141 |
struct stat st; |
| 142 |
bool ro, exec; |
| 143 |
int r; |
| 144 |
|
| 145 |
/* Creates a clone of a regular file in a memfd. Unlike copy_data_fd() this returns strictly a memfd |
| 146 |
* (and if it can't it will fail). Thus the resulting fd is seekable, and definitely reports as |
| 147 |
* S_ISREG. */ |
| 148 |
|
| 149 |
assert(fd >= 0); |
| 150 |
assert(name); |
| 151 |
assert(IN_SET(mode & O_ACCMODE_STRICT, O_RDONLY, O_RDWR)); |
| 152 |
assert((mode & ~(O_RDONLY|O_RDWR|O_CLOEXEC)) == 0); |
| 153 |
|
| 154 |
if (fstat(fd, &st) < 0) |
| 155 |
return -errno; |
| 156 |
|
| 157 |
ro = (mode & O_ACCMODE_STRICT) == O_RDONLY; |
| 158 |
exec = st.st_mode & 0111; |
| 159 |
|
| 160 |
mfd = memfd_create_wrapper(name, |
| 161 |
((FLAGS_SET(mode, O_CLOEXEC) || ro) ? MFD_CLOEXEC : 0) | |
| 162 |
(ro ? MFD_ALLOW_SEALING : 0) | |
| 163 |
(exec ? MFD_EXEC : MFD_NOEXEC_SEAL)); |
| 164 |
if (mfd < 0) |
| 165 |
return mfd; |
| 166 |
|
| 167 |
r = copy_bytes(fd, mfd, UINT64_MAX, COPY_REFLINK); |
| 168 |
if (r < 0) |
| 169 |
return r; |
| 170 |
|
| 171 |
if (ro) { |
| 172 |
r = memfd_set_sealed(mfd); |
| 173 |
if (r < 0) |
| 174 |
return r; |
| 175 |
|
| 176 |
return fd_reopen(mfd, mode); |
| 177 |
} |
| 178 |
|
| 179 |
off_t f = lseek(mfd, 0, SEEK_SET); |
| 180 |
if (f < 0) |
| 181 |
return -errno; |
| 182 |
|
| 183 |
return TAKE_FD(mfd); |
| 184 |
} |