blob: dff0f88d7e781099001da3d2b8fa5e481bda0b25 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2** Copyright 1998-2003 University of Illinois Board of Trustees
3** Copyright 1998-2003 Mark D. Roth
4** All rights reserved.
5**
6** append.c - libtar code to append files to a tar archive
7**
8** Mark D. Roth <roth@uiuc.edu>
9** Campus Information Technologies and Educational Services
10** University of Illinois at Urbana-Champaign
11*/
12
13#include <internal.h>
14
15#include <stdio.h>
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050016#include <stdlib.h>
17#include <string.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050018#include <errno.h>
19#include <fcntl.h>
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050020#include <time.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050021#include <sys/param.h>
22#include <sys/types.h>
Ethan Yonker79f88bd2016-12-09 14:52:12 -060023#include <stdbool.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050024
Ethan Yonker71187742017-01-13 13:30:10 -060025#include <sys/capability.h>
26#include <sys/xattr.h>
bigbiff7ba75002020-04-11 20:47:09 -040027#include <linux/fs.h>
Ethan Yonker71187742017-01-13 13:30:10 -060028#include <linux/xattr.h>
29
bigbiff bigbiff9c754052013-01-09 09:09:08 -050030#ifdef STDC_HEADERS
bigbiff7ba75002020-04-11 20:47:09 -040031#include <stdlib.h>
32#include <string.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050033#endif
34
35#ifdef HAVE_UNISTD_H
bigbiff7ba75002020-04-11 20:47:09 -040036#include <unistd.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050037#endif
38
Matt Mower87413642017-01-17 21:14:46 -060039#include <selinux/selinux.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050040
bigbiff7ba75002020-04-11 20:47:09 -040041#ifdef USE_FSCRYPT
42#include "fscrypt_policy.h"
43#endif
44
Ethan Yonker8d039f72017-02-03 14:26:15 -060045#include "android_utils.h"
Ethan Yonker79f88bd2016-12-09 14:52:12 -060046
bigbiffa957f072021-03-07 18:20:29 -050047#ifdef TW_LIBTAR_DEBUG
48#define DEBUG 1
49#endif
50
bigbiff bigbiff9c754052013-01-09 09:09:08 -050051struct tar_dev
52{
53 dev_t td_dev;
54 libtar_hash_t *td_h;
55};
56typedef struct tar_dev tar_dev_t;
57
58struct tar_ino
59{
60 ino_t ti_ino;
61 char ti_name[MAXPATHLEN];
62};
63typedef struct tar_ino tar_ino_t;
64
65
66/* free memory associated with a tar_dev_t */
67void
68tar_dev_free(tar_dev_t *tdp)
69{
70 libtar_hash_free(tdp->td_h, free);
71 free(tdp);
72}
73
74
75/* appends a file to the tar archive */
76int
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050077tar_append_file(TAR *t, const char *realname, const char *savename)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050078{
79 struct stat s;
80 int i;
81 libtar_hashptr_t hp;
82 tar_dev_t *td = NULL;
83 tar_ino_t *ti = NULL;
84 char path[MAXPATHLEN];
85
86#ifdef DEBUG
bigbiffa957f072021-03-07 18:20:29 -050087 printf("==> tar_append_file(TAR=0x%p (\"%s\"), realname=\"%s\", "
88 "savename=\"%s\")\n", (void*) t, t->pathname, realname,
bigbiff bigbiff9c754052013-01-09 09:09:08 -050089 (savename ? savename : "[NULL]"));
90#endif
91
92 if (lstat(realname, &s) != 0)
93 {
94#ifdef DEBUG
95 perror("lstat()");
96#endif
97 return -1;
98 }
99
100 /* set header block */
101#ifdef DEBUG
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500102 puts("tar_append_file(): setting header block...");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500103#endif
104 memset(&(t->th_buf), 0, sizeof(struct tar_header));
105 th_set_from_stat(t, &s);
106
107 /* set the header path */
108#ifdef DEBUG
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500109 puts("tar_append_file(): setting header path...");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500110#endif
111 th_set_path(t, (savename ? savename : realname));
112
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200113 /* get selinux context */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500114 if (t->options & TAR_STORE_SELINUX)
115 {
116 if (t->th_buf.selinux_context != NULL)
117 {
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200118 free(t->th_buf.selinux_context);
119 t->th_buf.selinux_context = NULL;
120 }
121
122 security_context_t selinux_context = NULL;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500123 if (lgetfilecon(realname, &selinux_context) >= 0)
124 {
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200125 t->th_buf.selinux_context = strdup(selinux_context);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500126 printf(" ==> set selinux context: %s\n", selinux_context);
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200127 freecon(selinux_context);
128 }
129 else
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500130 {
131#ifdef DEBUG
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200132 perror("Failed to get selinux context");
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500133#endif
134 }
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200135 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500136
bigbiff7ba75002020-04-11 20:47:09 -0400137#ifdef USE_FSCRYPT
138 if (TH_ISDIR(t) && t->options & TAR_STORE_FSCRYPT_POL)
139 {
140 if (t->th_buf.fep != NULL)
141 {
142 free(t->th_buf.fep);
143 t->th_buf.fep = NULL;
144 }
bigbiffa957f072021-03-07 18:20:29 -0500145#ifdef USE_FSCRYPT_POLICY_V1
146 t->th_buf.fep = (struct fscrypt_policy_v1 *)malloc(sizeof(struct fscrypt_policy_v1));
147#else
148 t->th_buf.fep = (struct fscrypt_policy_v2 *)malloc(sizeof(struct fscrypt_policy_v2));
149#endif
bigbiff7ba75002020-04-11 20:47:09 -0400150 if (!t->th_buf.fep) {
151 printf("malloc fs_encryption_policy\n");
152 return -1;
153 }
154
155 if (fscrypt_policy_get_struct(realname, t->th_buf.fep)) {
bigbiff2e344ab2021-05-07 10:41:55 -0400156#ifdef USE_FSCRYPT_POLICY_V1
157 uint8_t tar_policy[FS_KEY_DESCRIPTOR_SIZE];
158 char policy_hex[FS_KEY_DESCRIPTOR_SIZE_HEX];
159#else
bigbiffa957f072021-03-07 18:20:29 -0500160 uint8_t tar_policy[FSCRYPT_KEY_IDENTIFIER_SIZE];
bigbiffa957f072021-03-07 18:20:29 -0500161 char policy_hex[FSCRYPT_KEY_IDENTIFIER_HEX_SIZE];
bigbiff2e344ab2021-05-07 10:41:55 -0400162#endif
163 memset(tar_policy, 0, sizeof(tar_policy));
164#ifdef USE_FSCRYPT_POLICY_V1
165 bytes_to_hex(t->th_buf.fep->master_key_descriptor, FS_KEY_DESCRIPTOR_SIZE, policy_hex);
166#else
bigbiffa957f072021-03-07 18:20:29 -0500167 bytes_to_hex(t->th_buf.fep->master_key_identifier, FSCRYPT_KEY_IDENTIFIER_SIZE, policy_hex);
bigbiff2e344ab2021-05-07 10:41:55 -0400168#endif
bigbiffa957f072021-03-07 18:20:29 -0500169 if (lookup_ref_key(t->th_buf.fep, &tar_policy[0])) {
bigbiff2e344ab2021-05-07 10:41:55 -0400170 if (strncmp((char *) tar_policy, USER_CE_FSCRYPT_POLICY, sizeof(USER_CE_FSCRYPT_POLICY) - 1) == 0
171 || strncmp((char *) tar_policy, USER_DE_FSCRYPT_POLICY, sizeof(USER_DE_FSCRYPT_POLICY) - 1) == 0
172 || strncmp((char *) tar_policy, SYSTEM_DE_FSCRYPT_POLICY, sizeof(SYSTEM_DE_FSCRYPT_POLICY)) == 0) {
173#ifdef USE_FSCRYPT_POLICY_V1
174 memcpy(t->th_buf.fep->master_key_descriptor, tar_policy, FS_KEY_DESCRIPTOR_SIZE);
175#else
bigbiffa957f072021-03-07 18:20:29 -0500176 memcpy(t->th_buf.fep->master_key_identifier, tar_policy, FSCRYPT_KEY_IDENTIFIER_SIZE);
177 printf("found fscrypt policy '%s' - '%s' - '%s'\n", realname, t->th_buf.fep->master_key_identifier, policy_hex);
bigbiff2e344ab2021-05-07 10:41:55 -0400178#endif
bigbiffa957f072021-03-07 18:20:29 -0500179 } else {
180 printf("failed to match fscrypt tar policy for '%s' - '%s'\n", realname, policy_hex);
181 free(t->th_buf.fep);
182 t->th_buf.fep = NULL;
183 }
bigbiff7ba75002020-04-11 20:47:09 -0400184 } else {
185 printf("failed to lookup fscrypt tar policy for '%s' - '%s'\n", realname, policy_hex);
186 free(t->th_buf.fep);
187 t->th_buf.fep = NULL;
bigbiff7ba75002020-04-11 20:47:09 -0400188 }
189 }
190 else {
191 // no policy found, but this is not an error as not all dirs will have a policy
192 free(t->th_buf.fep);
193 t->th_buf.fep = NULL;
194 }
195 }
196#endif
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600197
Ethan Yonker71187742017-01-13 13:30:10 -0600198 /* get posix file capabilities */
199 if (TH_ISREG(t) && t->options & TAR_STORE_POSIX_CAP)
200 {
201 if (t->th_buf.has_cap_data)
202 {
203 memset(&t->th_buf.cap_data, 0, sizeof(struct vfs_cap_data));
204 t->th_buf.has_cap_data = 0;
205 }
206
207 if (getxattr(realname, XATTR_NAME_CAPS, &t->th_buf.cap_data, sizeof(struct vfs_cap_data)) >= 0)
208 {
209 t->th_buf.has_cap_data = 1;
210#if 1 //def DEBUG
211 print_caps(&t->th_buf.cap_data);
212#endif
213 }
214 }
215
Ethan Yonker8d039f72017-02-03 14:26:15 -0600216 /* get android user.default xattr */
217 if (TH_ISDIR(t) && t->options & TAR_STORE_ANDROID_USER_XATTR)
218 {
219 if (getxattr(realname, "user.default", NULL, 0) >= 0)
220 {
221 t->th_buf.has_user_default = 1;
222#if 1 //def DEBUG
223 printf("storing xattr user.default\n");
224#endif
225 }
226 if (getxattr(realname, "user.inode_cache", NULL, 0) >= 0)
227 {
228 t->th_buf.has_user_cache = 1;
229#if 1 //def DEBUG
230 printf("storing xattr user.inode_cache\n");
231#endif
232 }
233 if (getxattr(realname, "user.inode_code_cache", NULL, 0) >= 0)
234 {
235 t->th_buf.has_user_code_cache = 1;
236#if 1 //def DEBUG
237 printf("storing xattr user.inode_code_cache\n");
238#endif
239 }
240 }
241
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500242 /* check if it's a hardlink */
243#ifdef DEBUG
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500244 puts("tar_append_file(): checking inode cache for hardlink...");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500245#endif
246 libtar_hashptr_reset(&hp);
247 if (libtar_hash_getkey(t->h, &hp, &(s.st_dev),
248 (libtar_matchfunc_t)dev_match) != 0)
249 td = (tar_dev_t *)libtar_hashptr_data(&hp);
250 else
251 {
252#ifdef DEBUG
bigbiffa957f072021-03-07 18:20:29 -0500253 printf("+++ adding hash for device (0x%x, 0x%x)...\n",
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500254 major(s.st_dev), minor(s.st_dev));
255#endif
256 td = (tar_dev_t *)calloc(1, sizeof(tar_dev_t));
257 td->td_dev = s.st_dev;
258 td->td_h = libtar_hash_new(256, (libtar_hashfunc_t)ino_hash);
259 if (td->td_h == NULL)
260 return -1;
261 if (libtar_hash_add(t->h, td) == -1)
262 return -1;
263 }
264 libtar_hashptr_reset(&hp);
265 if (libtar_hash_getkey(td->td_h, &hp, &(s.st_ino),
266 (libtar_matchfunc_t)ino_match) != 0)
267 {
268 ti = (tar_ino_t *)libtar_hashptr_data(&hp);
269#ifdef DEBUG
270 printf(" tar_append_file(): encoding hard link \"%s\" "
271 "to \"%s\"...\n", realname, ti->ti_name);
272#endif
273 t->th_buf.typeflag = LNKTYPE;
274 th_set_link(t, ti->ti_name);
275 }
276 else
277 {
278#ifdef DEBUG
bigbiffa957f072021-03-07 18:20:29 -0500279 printf("+++ adding entry: device (0x%d,0x%x), inode %lu"
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500280 "(\"%s\")...\n", major(s.st_dev), minor(s.st_dev),
bigbiffa957f072021-03-07 18:20:29 -0500281 (unsigned long) s.st_ino, realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500282#endif
283 ti = (tar_ino_t *)calloc(1, sizeof(tar_ino_t));
284 if (ti == NULL)
285 return -1;
286 ti->ti_ino = s.st_ino;
287 snprintf(ti->ti_name, sizeof(ti->ti_name), "%s",
288 savename ? savename : realname);
289 libtar_hash_add(td->td_h, ti);
290 }
291
292 /* check if it's a symlink */
293 if (TH_ISSYM(t))
294 {
295 i = readlink(realname, path, sizeof(path));
296 if (i == -1)
297 return -1;
298 if (i >= MAXPATHLEN)
299 i = MAXPATHLEN - 1;
300 path[i] = '\0';
301#ifdef DEBUG
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500302 printf("tar_append_file(): encoding symlink \"%s\" -> "
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500303 "\"%s\"...\n", realname, path);
304#endif
305 th_set_link(t, path);
306 }
307
308 /* print file info */
309 if (t->options & TAR_VERBOSE)
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500310 printf("%s\n", th_get_pathname(t));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500311
312#ifdef DEBUG
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500313 puts("tar_append_file(): writing header");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500314#endif
315 /* write header */
316 if (th_write(t) != 0)
317 {
318#ifdef DEBUG
bigbiffa957f072021-03-07 18:20:29 -0500319 printf("t->fd = %ld\n", t->fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500320#endif
321 return -1;
322 }
323#ifdef DEBUG
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500324 puts("tar_append_file(): back from th_write()");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500325#endif
326
327 /* if it's a regular file, write the contents as well */
328 if (TH_ISREG(t) && tar_append_regfile(t, realname) != 0)
329 return -1;
330
331 return 0;
332}
333
334
335/* write EOF indicator */
336int
337tar_append_eof(TAR *t)
338{
339 int i, j;
340 char block[T_BLOCKSIZE];
341
342 memset(&block, 0, T_BLOCKSIZE);
343 for (j = 0; j < 2; j++)
344 {
345 i = tar_block_write(t, &block);
346 if (i != T_BLOCKSIZE)
347 {
348 if (i != -1)
349 errno = EINVAL;
350 return -1;
351 }
352 }
353
354 return 0;
355}
356
357
358/* add file contents to a tarchive */
359int
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500360tar_append_regfile(TAR *t, const char *realname)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500361{
362 char block[T_BLOCKSIZE];
363 int filefd;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500364 int64_t i, size;
365 ssize_t j;
366 int rv = -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500367
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500368#if defined(O_BINARY)
369 filefd = open(realname, O_RDONLY|O_BINARY);
370#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500371 filefd = open(realname, O_RDONLY);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500372#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500373 if (filefd == -1)
374 {
375#ifdef DEBUG
376 perror("open()");
377#endif
378 return -1;
379 }
380
381 size = th_get_size(t);
382 for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE)
383 {
384 j = read(filefd, &block, T_BLOCKSIZE);
385 if (j != T_BLOCKSIZE)
386 {
387 if (j != -1)
388 errno = EINVAL;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500389 goto fail;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500390 }
391 if (tar_block_write(t, &block) == -1)
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500392 goto fail;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500393 }
394
395 if (i > 0)
396 {
397 j = read(filefd, &block, i);
398 if (j == -1)
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500399 goto fail;
400 memset(&(block[i]), 0, T_BLOCKSIZE - i);
401 if (tar_block_write(t, &block) == -1)
402 goto fail;
403 }
404
405 /* success! */
406 rv = 0;
407fail:
408 close(filefd);
409
410 return rv;
411}
412
413
414/* add file contents to a tarchive */
415int
416tar_append_file_contents(TAR *t, const char *savename, mode_t mode,
417 uid_t uid, gid_t gid, void *buf, size_t len)
418{
419 struct stat st;
420
421 memset(&st, 0, sizeof(st));
422 st.st_mode = S_IFREG | mode;
423 st.st_uid = uid;
424 st.st_gid = gid;
425 st.st_mtime = time(NULL);
426 st.st_size = len;
427
428 th_set_from_stat(t, &st);
429 th_set_path(t, savename);
430
431 /* write header */
432 if (th_write(t) != 0)
433 {
434#ifdef DEBUG
bigbiffa957f072021-03-07 18:20:29 -0500435 fprintf(stderr, "tar_append_file_contents(): could not write header, t->fd = %ld\n", t->fd);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500436#endif
437 return -1;
438 }
439
440 return tar_append_buffer(t, buf, len);
441}
442
443int
444tar_append_buffer(TAR *t, void *buf, size_t len)
445{
446 char block[T_BLOCKSIZE];
Ethan Yonker58f21322018-08-24 11:17:36 -0500447 int i;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500448 size_t size = len;
449
450 for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE)
451 {
452 if (tar_block_write(t, buf) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500453 return -1;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500454 buf = (char *)buf + T_BLOCKSIZE;
455 }
456
457 if (i > 0)
458 {
459 memcpy(block, buf, i);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500460 memset(&(block[i]), 0, T_BLOCKSIZE - i);
461 if (tar_block_write(t, &block) == -1)
462 return -1;
463 }
464
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500465 return 0;
466}
467