xref: /aosp_15_r20/external/toybox/toys/posix/cpio.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1 /* cpio.c - a basic cpio
2  *
3  * Copyright 2013 Isaac Dunham <[email protected]>
4  * Copyright 2015 Frontier Silicon Ltd.
5  *
6  * see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt
7  * and http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cpio.html
8  * and http://pubs.opengroup.org/onlinepubs/7908799/xcu/cpio.html
9  *
10  * Yes, that's SUSv2, newer versions removed it, but RPM and initramfs use
11  * this archive format. We implement (only) the modern "-H newc" variant which
12  * expanded headers to 110 bytes (first field 6 bytes, rest are 8).
13  * In order: magic ino mode uid gid nlink mtime filesize devmajor devminor
14  * rdevmajor rdevminor namesize check
15  * This is the equivalent of mode -H newc in other implementations.
16  * We always do --quiet, but accept it as a compatibility NOP.
17  *
18  * TODO: export/import linux file list text format ala gen_initramfs_list.sh
19  * TODO: hardlink support, -A, -0, -a, -L, --sparse
20  * TODO: --renumber-archives (probably always?) --ignore-devno --reproducible
21 
22 USE_CPIO(NEWTOY(cpio, "(ignore-devno)(renumber-inodes)(quiet)(no-preserve-owner)R(owner):md(make-directories)uLH:p|i|t|F:v(verbose)o|[!pio][!pot][!pF]", TOYFLAG_BIN))
23 
24 config CPIO
25   bool "cpio"
26   default y
27   help
28     usage: cpio -{o|t|i|p DEST} [-dLtuv] [--verbose] [-F FILE] [-R [USER][:GROUP] [--no-preserve-owner]
29 
30     Copy files into and out of a "newc" format cpio archive.
31 
32     -d	Create directories if needed
33     -F FILE	Use archive FILE instead of stdin/stdout
34     -i	Extract from archive into file system (stdin=archive)
35     -L	Follow symlinks
36     -o	Create archive (stdin=list of files, stdout=archive)
37     -p DEST	Copy-pass mode, copy stdin file list to directory DEST
38     -R USER	Replace owner with USER[:GROUP]
39     -t	Test files (list only, stdin=archive, stdout=list of files)
40     -u	Unlink existing files when extracting
41     -v	Verbose
42     --no-preserve-owner     Don't set ownership during extract
43 */
44 
45 #define FOR_cpio
46 #include "toys.h"
47 
48 GLOBALS(
49   char *F, *H, *R;
50 )
51 
52 // Read strings, tail padded to 4 byte alignment. Argument "align" is amount
53 // by which start of string isn't aligned (usually 0, but header is 110 bytes
54 // which is 2 bytes off because the first field wasn't expanded from 6 to 8).
strpad(int fd,unsigned len,unsigned align)55 static char *strpad(int fd, unsigned len, unsigned align)
56 {
57   char *str;
58 
59   align = (align + len) & 3;
60   if (align) len += (4-align);
61   xreadall(fd, str = xmalloc(len+1), len);
62   str[len]=0; // redundant, in case archive is bad
63 
64   return str;
65 }
66 
67 //convert hex to uint; mostly to allow using bits of non-terminated strings
x8u(char * hex)68 static unsigned x8u(char *hex)
69 {
70   unsigned val, inpos = 8, outpos;
71   char pattern[6];
72 
73   while (*hex == '0') {
74     hex++;
75     if (!--inpos) return 0;
76   }
77   // Because scanf gratuitously treats %*X differently than printf does.
78   sprintf(pattern, "%%%dX%%n", inpos);
79   sscanf(hex, pattern, &val, &outpos);
80   if (inpos != outpos) error_exit("bad hex");
81 
82   return val;
83 }
84 
cpio_main(void)85 void cpio_main(void)
86 {
87   int pipe, afd = FLAG(o), reown = !geteuid() && !FLAG(no_preserve_owner),
88       empty = 1;
89   pid_t pid = 0;
90   long Ruid = -1, Rgid = -1;
91   char *tofree = 0;
92 
93   if (TT.R) {
94     char *group = TT.R+strcspn(TT.R, ":.");
95 
96     if (*group) {
97       Rgid = xgetgid(group+1);
98       *group = 0;
99     }
100     if (group != TT.R) Ruid = xgetuid(TT.R);
101   }
102 
103   // In passthrough mode, parent stays in original dir and generates archive
104   // to pipe, child does chdir to new dir and reads archive from stdin (pipe).
105   if (FLAG(p)) {
106     if (FLAG(d)) {
107       if (!*toys.optargs) error_exit("need directory for -p");
108       if (mkdir(*toys.optargs, 0700) == -1 && errno != EEXIST)
109         perror_msg("mkdir %s", *toys.optargs);
110     }
111     if (toys.stacktop) {
112       // xpopen() doesn't return from child due to vfork(), instead restarts
113       // with !toys.stacktop
114       pid = xpopen(0, &pipe, 0);
115       afd = pipe;
116     } else {
117       // child
118       toys.optflags |= FLAG_i;
119       xchdir(*toys.optargs);
120     }
121   }
122 
123   if (TT.F) {
124     int perm = FLAG(o) ? O_CREAT|O_WRONLY|O_TRUNC : O_RDONLY;
125 
126     afd = xcreate(TT.F, perm, 0644);
127   }
128 
129   // read cpio archive
130 
131   if (FLAG(i) || FLAG(t)) for (;; empty = 0) {
132     char *name, *data;
133     unsigned mode, uid, gid, timestamp;
134     int test = FLAG(t), err = 0, size = 0, len;
135 
136     free(tofree);
137     tofree = 0;
138     // read header, skipping arbitrary leading NUL bytes (concatenated archives)
139     for (;;) {
140       if (1>(len = readall(afd, toybuf+size, 110-size))) break;
141       if (size || *toybuf) {
142         size += len;
143         break;
144       }
145       for (size = 0; size<len; size++) if (toybuf[size]) break;
146       memmove(toybuf, toybuf+size, len-size);
147       size = len-size;
148     }
149     if (!size) {
150       if (empty) error_exit("empty archive");
151       else break;
152     }
153     if (size != 110 || smemcmp(toybuf, "070701", 6)) error_exit("bad header");
154     tofree = name = strpad(afd, x8u(toybuf+94), 110);
155     // TODO: this flushes hardlink detection via major/minor/ino match
156     if (!strcmp("TRAILER!!!", name)) continue;
157 
158     // If you want to extract absolute paths, "cd /" and run cpio.
159     while (*name == '/') name++;
160     // TODO: remove .. entries
161 
162     size = x8u(toybuf+54);
163     mode = x8u(toybuf+14);
164     uid = (Ruid>=0) ? Ruid : x8u(toybuf+22);
165     gid = (Rgid>=0) ? Rgid : x8u(toybuf+30);
166     timestamp = x8u(toybuf+46); // unsigned 32 bit, so year 2100 problem
167 
168     // (This output is unaffected by --quiet.)
169     if (FLAG(t) || FLAG(v)) puts(name);
170 
171     if (FLAG(u) && !test) if (unlink(name) && errno == EISDIR) rmdir(name);
172 
173     if (!test && FLAG(d) && strrchr(name, '/') && mkpath(name)) {
174       perror_msg("mkpath '%s'", name);
175       test++;
176     }
177 
178     // Consume entire record even if it couldn't create file, so we're
179     // properly aligned with next file.
180 
181     if (S_ISDIR(mode)) {
182       if (test) continue;
183       err = mkdir(name, mode) && (errno != EEXIST && !FLAG(u));
184 
185       // Creading dir/dev doesn't give us a filehandle, we have to refer to it
186       // by name to chown/utime, but how do we know it's the same item?
187       // Check that we at least have the right type of entity open, and do
188       // NOT restore dropped suid bit in this case.
189       if (S_ISDIR(mode) && reown) {
190         int fd = open(name, O_RDONLY|O_NOFOLLOW);
191         struct stat st;
192 
193         if (fd != -1 && !fstat(fd, &st) && (st.st_mode&S_IFMT) == (mode&S_IFMT))
194           err = fchown(fd, uid, gid);
195         else err = 1;
196 
197         close(fd);
198       }
199     } else if (S_ISREG(mode)) {
200       int fd = test ? 0 : open(name, O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, mode);
201 
202       // If write fails, we still need to read/discard data to continue with
203       // archive. Since doing so overwrites errno, report error now
204       if (fd < 0) {
205         perror_msg("create %s", name);
206         test++;
207       }
208 
209       data = toybuf;
210       while (size) {
211         if (size < sizeof(toybuf)) data = strpad(afd, size, 0);
212         else xreadall(afd, toybuf, sizeof(toybuf));
213         if (!test) xwrite(fd, data, data == toybuf ? sizeof(toybuf) : size);
214         if (data != toybuf) {
215           free(data);
216           break;
217         }
218         size -= sizeof(toybuf);
219       }
220 
221       if (!test) {
222         // set owner, restore dropped suid bit
223         if (reown) err = fchown(fd, uid, gid) && fchmod(fd, mode);
224         close(fd);
225       }
226     } else {
227       data = S_ISLNK(mode) ? strpad(afd, size, 0) : 0;
228       if (!test) {
229         err = data ? symlink(data, name)
230           : mknod(name, mode, dev_makedev(x8u(toybuf+78), x8u(toybuf+86)));
231 
232         // Can't get a filehandle to a symlink or a node on nodev mount,
233         // so do special chown that at least doesn't follow symlinks.
234         // We also don't chmod after, so dropped suid bit isn't restored
235         if (!err && reown) err = lchown(name, uid, gid);
236       }
237       free(data);
238     }
239 
240     // Set timestamp.
241     if (!test && !err) {
242       struct timespec times[2];
243 
244       memset(times, 0, sizeof(struct timespec)*2);
245       times[0].tv_sec = times[1].tv_sec = timestamp;
246       err = utimensat(AT_FDCWD, name, times, AT_SYMLINK_NOFOLLOW);
247     }
248 
249     if (err) perror_msg_raw(name);
250 
251   // Output cpio archive
252 
253   } else {
254     char *name = 0;
255     size_t size = 0;
256     unsigned inode = 0;
257 
258     for (;;) {
259       struct stat st;
260       unsigned nlen, error = 0, zero = 0;
261       int len, fd = -1;
262       char *link = 0;
263       ssize_t llen;
264 
265       len = getline(&name, &size, stdin);
266       if (len<1) break;
267       if (name[len-1] == '\n') name[--len] = 0;
268       if (!len) continue;
269       nlen = len+1;
270       if ((FLAG(L)?stat:lstat)(name, &st) || (S_ISREG(st.st_mode)
271           && st.st_size && (fd = open(name, O_RDONLY))<0)
272           || (S_ISLNK(st.st_mode) && !(link = xreadlink(name))))
273       {
274         perror_msg_raw(name);
275         continue;
276       }
277       // encrypted filesystems can stat the wrong link size
278       if (link) st.st_size = strlen(link);
279 
280       if (Ruid>=0) st.st_uid = Ruid;
281       if (Rgid>=0) st.st_gid = Rgid;
282       if (FLAG(no_preserve_owner)) st.st_uid = st.st_gid = 0;
283       if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) st.st_size = 0;
284       if (st.st_size >> 32) perror_msg("skipping >2G file '%s'", name);
285       else {
286         if (FLAG(renumber_inodes)) st.st_ino = ++inode;
287         if (FLAG(ignore_devno)) st.st_rdev = 0;
288         llen = sprintf(toybuf,
289           "070701%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
290           (int)st.st_ino, st.st_mode, st.st_uid, st.st_gid, (int)st.st_nlink,
291           (int)st.st_mtime, (int)st.st_size, dev_major(st.st_dev),
292           dev_minor(st.st_dev), dev_major(st.st_rdev), dev_minor(st.st_rdev),
293           nlen, 0);
294         xwrite(afd, toybuf, llen);
295         xwrite(afd, name, nlen);
296 
297         // NUL Pad header up to 4 multiple bytes.
298         llen = (llen + nlen) & 3;
299         if (llen) xwrite(afd, &zero, 4-llen);
300 
301         // Write out body for symlink or regular file
302         if (link) xwrite(afd, link, st.st_size);
303         else for (llen = st.st_size; llen; llen -= nlen) {
304           nlen = llen > sizeof(toybuf) ? sizeof(toybuf) : llen;
305           // If read fails, write anyway (already wrote size in header)
306           if (nlen != readall(fd, toybuf, nlen))
307             if (!error++) perror_msg("bad read from file '%s'", name);
308           xwrite(afd, toybuf, nlen);
309         }
310         llen = st.st_size & 3;
311         if (llen) xwrite(afd, &zero, 4-llen);
312       }
313       free(link);
314       xclose(fd);
315     }
316     if (CFG_TOYBOX_FREE) free(name);
317 
318     // nlink=1, namesize=11, with padding
319     dprintf(afd, "070701%040X%056X%08XTRAILER!!!%c%c%c%c", 1, 11, 0, 0, 0, 0,0);
320   }
321   if (TT.F) xclose(afd);
322 
323   if (FLAG(p) && pid) toys.exitval |= xpclose(pid, pipe);
324 }
325